1 /*
2 * Copyright (C) 2013-2019 Red Hat
3 * Copyright (c) 2018 DisplayLink (UK) Ltd.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18 * 02111-1307, USA.
19 */
20
21 #include "config.h"
22
23 #include "backends/native/meta-kms-utils.h"
24
25 #include <drm_fourcc.h>
26 #include <glib.h>
27
28 /* added in libdrm 2.4.95 */
29 #ifndef DRM_FORMAT_INVALID
30 #define DRM_FORMAT_INVALID 0
31 #endif
32
33 float
meta_calculate_drm_mode_refresh_rate(const drmModeModeInfo * drm_mode)34 meta_calculate_drm_mode_refresh_rate (const drmModeModeInfo *drm_mode)
35 {
36 double numerator;
37 double denominator;
38
39 if (drm_mode->htotal <= 0 || drm_mode->vtotal <= 0)
40 return 0.0;
41
42 numerator = drm_mode->clock * 1000.0;
43 denominator = (double) drm_mode->vtotal * drm_mode->htotal;
44 if (drm_mode->vscan > 1)
45 denominator *= drm_mode->vscan;
46
47 return numerator / denominator;
48 }
49
50 int64_t
meta_calculate_drm_mode_vblank_duration_us(const drmModeModeInfo * drm_mode)51 meta_calculate_drm_mode_vblank_duration_us (const drmModeModeInfo *drm_mode)
52 {
53 int64_t value;
54
55 if (drm_mode->htotal <= 0 || drm_mode->vtotal <= 0)
56 return 0;
57
58 /* Convert to int64_t early. */
59 value = drm_mode->vtotal - drm_mode->vdisplay;
60 value *= drm_mode->htotal;
61
62 if (drm_mode->flags & DRM_MODE_FLAG_DBLSCAN)
63 value *= 2;
64
65 /* Round the duration up as it is used for buffer swap deadline computation. */
66 value = (value * 1000 + drm_mode->clock - 1) / drm_mode->clock;
67
68 return value;
69 }
70
71 /**
72 * meta_drm_format_to_string:
73 * @tmp: temporary buffer
74 * @drm_format: DRM fourcc pixel format
75 *
76 * Returns a pointer to a string naming the given pixel format,
77 * usually a pointer to the temporary buffer but not always.
78 * Invalid formats may return nonsense names.
79 *
80 * When calling this, allocate one MetaDrmFormatBuf on the stack to
81 * be used as the temporary buffer.
82 */
83 const char *
meta_drm_format_to_string(MetaDrmFormatBuf * tmp,uint32_t drm_format)84 meta_drm_format_to_string (MetaDrmFormatBuf *tmp,
85 uint32_t drm_format)
86 {
87 int i;
88
89 if (drm_format == DRM_FORMAT_INVALID)
90 return "INVALID";
91
92 G_STATIC_ASSERT (sizeof (tmp->s) == 5);
93 for (i = 0; i < 4; i++)
94 {
95 char c = (drm_format >> (i * 8)) & 0xff;
96 tmp->s[i] = g_ascii_isgraph (c) ? c : '.';
97 }
98
99 tmp->s[i] = 0;
100
101 return tmp->s;
102 }
103
104