1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
/*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting documentation, and
* that the name of the copyright holders not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. The copyright holders make no representations
* about the suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <sys/types.h>
#include <GL/gl.h> /* mesa_interface needs GL types */
#include "mesa_interface.h"
#include "drm-uapi/drm_fourcc.h"
#include "loader_dri_helper.h"
#include "util/driconf.h"
__DRIimage *loader_dri_create_image(__DRIscreen *screen,
const __DRIimageExtension *image,
uint32_t width, uint32_t height,
uint32_t dri_format, uint32_t dri_usage,
const uint64_t *modifiers,
unsigned int modifiers_count,
void *loaderPrivate)
{
if (modifiers && modifiers_count > 0) {
bool has_valid_modifier = false;
int i;
/* It's acceptable to create an image with INVALID modifier in the list,
* but it cannot be on the only modifier (since it will certainly fail
* later). While we could easily catch this after modifier creation, doing
* the check here is a convenient debug check likely pointing at whatever
* interface the client is using to build its modifier list.
*/
for (i = 0; i < modifiers_count; i++) {
if (modifiers[i] != DRM_FORMAT_MOD_INVALID) {
has_valid_modifier = true;
break;
}
}
if (!has_valid_modifier)
return NULL;
}
return image->createImage(screen, width, height, dri_format,
modifiers, modifiers_count, dri_usage,
loaderPrivate);
}
static int dri_vblank_mode(__DRIscreen *driScreen, const __DRI2configQueryExtension *config)
{
GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
if (config)
config->configQueryi(driScreen, "vblank_mode", &vblank_mode);
return vblank_mode;
}
int dri_get_initial_swap_interval(__DRIscreen *driScreen,
const __DRI2configQueryExtension *config)
{
int vblank_mode = dri_vblank_mode(driScreen, config);
switch (vblank_mode) {
case DRI_CONF_VBLANK_NEVER:
case DRI_CONF_VBLANK_DEF_INTERVAL_0:
return 0;
case DRI_CONF_VBLANK_DEF_INTERVAL_1:
case DRI_CONF_VBLANK_ALWAYS_SYNC:
default:
return 1;
}
}
bool dri_valid_swap_interval(__DRIscreen *driScreen,
const __DRI2configQueryExtension *config, int interval)
{
int vblank_mode = dri_vblank_mode(driScreen, config);
switch (vblank_mode) {
case DRI_CONF_VBLANK_NEVER:
if (interval != 0)
return false;
break;
case DRI_CONF_VBLANK_ALWAYS_SYNC:
if (interval <= 0)
return false;
break;
default:
break;
}
return true;
}
/* the DRIimage createImage function takes __DRI_IMAGE_FORMAT codes, while
* the createImageFromDmaBufs call takes DRM_FORMAT codes. To avoid
* complete confusion, just deal in __DRI_IMAGE_FORMAT codes for now and
* translate to DRM_FORMAT codes in the call to createImageFromDmaBufs
*/
int
loader_fourcc_to_image_format(int fourcc)
{
/* Convert from DRM_FORMAT to __DRI_IMAGE_FORMAT (sigh) */
switch (fourcc) {
case __DRI_IMAGE_FOURCC_SARGB8888: return __DRI_IMAGE_FORMAT_SARGB8;
case __DRI_IMAGE_FOURCC_SABGR8888: return __DRI_IMAGE_FORMAT_SABGR8;
case __DRI_IMAGE_FOURCC_SXRGB8888: return __DRI_IMAGE_FORMAT_SXRGB8;
case DRM_FORMAT_RGB565: return __DRI_IMAGE_FORMAT_RGB565;
case DRM_FORMAT_XRGB8888: return __DRI_IMAGE_FORMAT_XRGB8888;
case DRM_FORMAT_ARGB8888: return __DRI_IMAGE_FORMAT_ARGB8888;
case DRM_FORMAT_ABGR8888: return __DRI_IMAGE_FORMAT_ABGR8888;
case DRM_FORMAT_XBGR8888: return __DRI_IMAGE_FORMAT_XBGR8888;
case DRM_FORMAT_XRGB2101010: return __DRI_IMAGE_FORMAT_XRGB2101010;
case DRM_FORMAT_ARGB2101010: return __DRI_IMAGE_FORMAT_ARGB2101010;
case DRM_FORMAT_XBGR2101010: return __DRI_IMAGE_FORMAT_XBGR2101010;
case DRM_FORMAT_ABGR2101010: return __DRI_IMAGE_FORMAT_ABGR2101010;
case DRM_FORMAT_ABGR16161616: return __DRI_IMAGE_FORMAT_ABGR16161616;
case DRM_FORMAT_XBGR16161616: return __DRI_IMAGE_FORMAT_XBGR16161616;
case DRM_FORMAT_XBGR16161616F: return __DRI_IMAGE_FORMAT_XBGR16161616F;
case DRM_FORMAT_ABGR16161616F: return __DRI_IMAGE_FORMAT_ABGR16161616F;
}
return 0;
}
int
loader_image_format_to_fourcc(int format)
{
/* Convert from __DRI_IMAGE_FORMAT to DRM_FORMAT (sigh) */
switch (format) {
case __DRI_IMAGE_FORMAT_SARGB8: return __DRI_IMAGE_FOURCC_SARGB8888;
case __DRI_IMAGE_FORMAT_SABGR8: return __DRI_IMAGE_FOURCC_SABGR8888;
case __DRI_IMAGE_FORMAT_SXRGB8: return __DRI_IMAGE_FOURCC_SXRGB8888;
case __DRI_IMAGE_FORMAT_RGB565: return DRM_FORMAT_RGB565;
case __DRI_IMAGE_FORMAT_XRGB8888: return DRM_FORMAT_XRGB8888;
case __DRI_IMAGE_FORMAT_ARGB8888: return DRM_FORMAT_ARGB8888;
case __DRI_IMAGE_FORMAT_ABGR8888: return DRM_FORMAT_ABGR8888;
case __DRI_IMAGE_FORMAT_XBGR8888: return DRM_FORMAT_XBGR8888;
case __DRI_IMAGE_FORMAT_XRGB2101010: return DRM_FORMAT_XRGB2101010;
case __DRI_IMAGE_FORMAT_ARGB2101010: return DRM_FORMAT_ARGB2101010;
case __DRI_IMAGE_FORMAT_XBGR2101010: return DRM_FORMAT_XBGR2101010;
case __DRI_IMAGE_FORMAT_ABGR2101010: return DRM_FORMAT_ABGR2101010;
case __DRI_IMAGE_FORMAT_ABGR16161616: return DRM_FORMAT_ABGR16161616;
case __DRI_IMAGE_FORMAT_XBGR16161616: return DRM_FORMAT_XBGR16161616;
case __DRI_IMAGE_FORMAT_XBGR16161616F: return DRM_FORMAT_XBGR16161616F;
case __DRI_IMAGE_FORMAT_ABGR16161616F: return DRM_FORMAT_ABGR16161616F;
}
return 0;
}
#ifdef HAVE_X11_PLATFORM
void
loader_init_screen_resources(struct loader_screen_resources *res,
xcb_connection_t *conn,
xcb_screen_t *screen)
{
res->conn = conn;
res->screen = screen;
res->crtcs = NULL;
mtx_init(&res->mtx, mtx_plain);
}
void
loader_destroy_screen_resources(struct loader_screen_resources *res)
{
mtx_destroy(&res->mtx);
}
static unsigned
gcd_u32(unsigned a, unsigned b)
{
assert(a > 0 || b > 0);
while (b != 0) {
unsigned remainder = a % b;
a = b;
b = remainder;
}
return a;
}
static void
calculate_refresh_rate(const xcb_randr_mode_info_t *mode,
unsigned *numerator, unsigned *denominator)
{
unsigned vtotal = mode->vtotal;
/* Double-scan doubles the number of lines */
if (mode->mode_flags & XCB_RANDR_MODE_FLAG_DOUBLE_SCAN)
vtotal *= 2;
/* Interlace splits the frame into two fields; typically the monitor
* reports field rate.
*/
if (mode->mode_flags & XCB_RANDR_MODE_FLAG_INTERLACE)
vtotal /= 2;
uint32_t dots = mode->htotal * vtotal;
if (dots == 0) {
*numerator = 0;
*denominator = 1;
} else {
uint32_t gcd = gcd_u32(mode->dot_clock, dots);
*numerator = mode->dot_clock / gcd;
*denominator = dots / gcd;
}
}
bool
loader_update_screen_resources(struct loader_screen_resources *res)
{
xcb_randr_get_crtc_info_cookie_t *crtc_cookies;
/* If we have cached screen resources information, check each CRTC to
* see if it's up to date. Ideally, we'd watch PresentConfigureNotify
* events on the root window to see if something changed, but those only
* fire if the geometry changes. It misses CRTC changes which only
* alter the refresh rate. We also can't watch RandR events internally
* because they aren't XGE events. So, we just check every CRTC for now.
*/
bool config_unchanged = res->crtcs != NULL;
crtc_cookies = malloc(res->num_crtcs * sizeof(*crtc_cookies));
for (unsigned c = 0; c < res->num_crtcs; c++) {
crtc_cookies[c] =
xcb_randr_get_crtc_info_unchecked(res->conn, res->crtcs[c].id,
res->config_timestamp);
}
for (unsigned c = 0; c < res->num_crtcs; c++) {
xcb_randr_get_crtc_info_reply_t *reply =
xcb_randr_get_crtc_info_reply(res->conn, crtc_cookies[c], NULL);
/* Although randrproto 1.4.0 says that RRGetCrtcInfo is supposed to
* return InvalidConfigTime if config_timestamp is out of date, the
* implementation in xserver as of 21.x doesn't actually do so. To
* detect changes in refresh rate, we check the returned timestamp
* on each tracked CRTC.
*/
if (!reply ||
reply->status == XCB_RANDR_SET_CONFIG_INVALID_CONFIG_TIME ||
reply->timestamp != res->crtcs[c].timestamp) {
config_unchanged = false;
/* continue to consume all replies */
}
free(reply);
}
free(crtc_cookies);
if (config_unchanged)
return false;
/* Do RRGetScreenResourcesCurrent to query the list of CRTCs and modes,
* then RRGetCrtcInfo on each CRTC to determine what mode each uses, and
* use the mode to calculate the refresh rate.
*/
mtx_lock(&res->mtx);
xcb_randr_get_screen_resources_current_cookie_t cookie =
xcb_randr_get_screen_resources_current_unchecked(res->conn,
res->screen->root);
xcb_randr_get_screen_resources_current_reply_t *reply =
xcb_randr_get_screen_resources_current_reply(res->conn, cookie, NULL);
xcb_randr_crtc_t *new_crtcs =
xcb_randr_get_screen_resources_current_crtcs(reply);
xcb_randr_mode_info_t *new_modes =
xcb_randr_get_screen_resources_current_modes(reply);
res->config_timestamp = reply->config_timestamp;
free(res->crtcs);
res->crtcs = calloc(reply->num_crtcs, sizeof(*res->crtcs));
crtc_cookies = malloc(reply->num_crtcs * sizeof(*crtc_cookies));
for (unsigned c = 0; c < reply->num_crtcs; c++) {
crtc_cookies[c] =
xcb_randr_get_crtc_info_unchecked(res->conn, new_crtcs[c],
res->config_timestamp);
}
unsigned i = 0;
for (unsigned c = 0; c < reply->num_crtcs; c++) {
xcb_randr_get_crtc_info_reply_t *crtc_info =
xcb_randr_get_crtc_info_reply(res->conn, crtc_cookies[c], NULL);
if (!crtc_info || crtc_info->mode == XCB_NONE)
continue;
res->crtcs[i].id = new_crtcs[c];
res->crtcs[i].timestamp = crtc_info->timestamp;
res->crtcs[i].x = crtc_info->x;
res->crtcs[i].y = crtc_info->y;
res->crtcs[i].width = crtc_info->width;
res->crtcs[i].height = crtc_info->height;
for (int m = 0; m < reply->num_modes; m++) {
if (new_modes[m].id == crtc_info->mode) {
calculate_refresh_rate(&new_modes[m],
&res->crtcs[i].refresh_numerator,
&res->crtcs[i].refresh_denominator);
break;
}
}
i++;
free(crtc_info);
}
res->num_crtcs = i;
free(crtc_cookies);
free(reply);
mtx_unlock(&res->mtx);
return true;
}
#endif
|