Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/egl/drivers/dri2/platform_x11.c
4570 views
1
/*
2
* Copyright © 2011 Intel Corporation
3
*
4
* Permission is hereby granted, free of charge, to any person obtaining a
5
* copy of this software and associated documentation files (the "Software"),
6
* to deal in the Software without restriction, including without limitation
7
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
* and/or sell copies of the Software, and to permit persons to whom the
9
* Software is furnished to do so, subject to the following conditions:
10
*
11
* The above copyright notice and this permission notice (including the next
12
* paragraph) shall be included in all copies or substantial portions of the
13
* Software.
14
*
15
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
* DEALINGS IN THE SOFTWARE.
23
*
24
* Authors:
25
* Kristian Høgsberg <[email protected]>
26
*/
27
28
#include <stdbool.h>
29
#include <stdint.h>
30
#include <stdlib.h>
31
#include <string.h>
32
#include <stdio.h>
33
#include <limits.h>
34
#include <dlfcn.h>
35
#include <fcntl.h>
36
#include <errno.h>
37
#include <unistd.h>
38
#ifdef HAVE_LIBDRM
39
#include <xf86drm.h>
40
#endif
41
#include <sys/types.h>
42
#include <sys/stat.h>
43
#include "util/debug.h"
44
#include "util/macros.h"
45
#include "util/bitscan.h"
46
47
#include "egl_dri2.h"
48
#include "loader.h"
49
50
#ifdef HAVE_DRI3
51
#include "platform_x11_dri3.h"
52
#endif
53
54
static EGLBoolean
55
dri2_x11_swap_interval(_EGLDisplay *disp, _EGLSurface *surf, EGLint interval);
56
57
uint32_t
58
dri2_format_for_depth(struct dri2_egl_display *dri2_dpy, uint32_t depth);
59
60
static void
61
swrastCreateDrawable(struct dri2_egl_display * dri2_dpy,
62
struct dri2_egl_surface * dri2_surf)
63
{
64
uint32_t mask;
65
const uint32_t function = GXcopy;
66
uint32_t valgc[2];
67
68
/* create GC's */
69
dri2_surf->gc = xcb_generate_id(dri2_dpy->conn);
70
mask = XCB_GC_FUNCTION;
71
xcb_create_gc(dri2_dpy->conn, dri2_surf->gc, dri2_surf->drawable, mask, &function);
72
73
dri2_surf->swapgc = xcb_generate_id(dri2_dpy->conn);
74
mask = XCB_GC_FUNCTION | XCB_GC_GRAPHICS_EXPOSURES;
75
valgc[0] = function;
76
valgc[1] = False;
77
xcb_create_gc(dri2_dpy->conn, dri2_surf->swapgc, dri2_surf->drawable, mask, valgc);
78
switch (dri2_surf->depth) {
79
case 32:
80
case 30:
81
case 24:
82
dri2_surf->bytes_per_pixel = 4;
83
break;
84
case 16:
85
dri2_surf->bytes_per_pixel = 2;
86
break;
87
case 8:
88
dri2_surf->bytes_per_pixel = 1;
89
break;
90
case 0:
91
dri2_surf->bytes_per_pixel = 0;
92
break;
93
default:
94
_eglLog(_EGL_WARNING, "unsupported depth %d", dri2_surf->depth);
95
}
96
}
97
98
static void
99
swrastDestroyDrawable(struct dri2_egl_display * dri2_dpy,
100
struct dri2_egl_surface * dri2_surf)
101
{
102
xcb_free_gc(dri2_dpy->conn, dri2_surf->gc);
103
xcb_free_gc(dri2_dpy->conn, dri2_surf->swapgc);
104
}
105
106
static bool
107
x11_get_drawable_info(__DRIdrawable * draw,
108
int *x, int *y, int *w, int *h,
109
void *loaderPrivate)
110
{
111
struct dri2_egl_surface *dri2_surf = loaderPrivate;
112
struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display);
113
114
xcb_get_geometry_cookie_t cookie;
115
xcb_get_geometry_reply_t *reply;
116
xcb_generic_error_t *error;
117
bool ret;
118
119
cookie = xcb_get_geometry (dri2_dpy->conn, dri2_surf->drawable);
120
reply = xcb_get_geometry_reply (dri2_dpy->conn, cookie, &error);
121
if (reply == NULL)
122
return false;
123
124
if (error != NULL) {
125
ret = false;
126
_eglLog(_EGL_WARNING, "error in xcb_get_geometry");
127
free(error);
128
} else {
129
*x = reply->x;
130
*y = reply->y;
131
*w = reply->width;
132
*h = reply->height;
133
ret = true;
134
}
135
free(reply);
136
return ret;
137
}
138
139
static void
140
swrastGetDrawableInfo(__DRIdrawable * draw,
141
int *x, int *y, int *w, int *h,
142
void *loaderPrivate)
143
{
144
*x = *y = *w = *h = 0;
145
x11_get_drawable_info(draw, x, y, w, h, loaderPrivate);
146
}
147
148
static void
149
swrastPutImage(__DRIdrawable * draw, int op,
150
int x, int y, int w, int h,
151
char *data, void *loaderPrivate)
152
{
153
struct dri2_egl_surface *dri2_surf = loaderPrivate;
154
struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display);
155
156
xcb_gcontext_t gc;
157
158
switch (op) {
159
case __DRI_SWRAST_IMAGE_OP_DRAW:
160
gc = dri2_surf->gc;
161
break;
162
case __DRI_SWRAST_IMAGE_OP_SWAP:
163
gc = dri2_surf->swapgc;
164
break;
165
default:
166
return;
167
}
168
169
xcb_put_image(dri2_dpy->conn, XCB_IMAGE_FORMAT_Z_PIXMAP, dri2_surf->drawable,
170
gc, w, h, x, y, 0, dri2_surf->depth,
171
w*h*dri2_surf->bytes_per_pixel, (const uint8_t *)data);
172
}
173
174
static void
175
swrastGetImage(__DRIdrawable * read,
176
int x, int y, int w, int h,
177
char *data, void *loaderPrivate)
178
{
179
struct dri2_egl_surface *dri2_surf = loaderPrivate;
180
struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display);
181
182
xcb_get_image_cookie_t cookie;
183
xcb_get_image_reply_t *reply;
184
xcb_generic_error_t *error;
185
186
cookie = xcb_get_image (dri2_dpy->conn, XCB_IMAGE_FORMAT_Z_PIXMAP,
187
dri2_surf->drawable, x, y, w, h, ~0);
188
reply = xcb_get_image_reply (dri2_dpy->conn, cookie, &error);
189
if (reply == NULL)
190
return;
191
192
if (error != NULL) {
193
_eglLog(_EGL_WARNING, "error in xcb_get_image");
194
free(error);
195
} else {
196
uint32_t bytes = xcb_get_image_data_length(reply);
197
uint8_t *idata = xcb_get_image_data(reply);
198
memcpy(data, idata, bytes);
199
}
200
free(reply);
201
}
202
203
204
static xcb_screen_t *
205
get_xcb_screen(xcb_screen_iterator_t iter, int screen)
206
{
207
for (; iter.rem; --screen, xcb_screen_next(&iter))
208
if (screen == 0)
209
return iter.data;
210
211
return NULL;
212
}
213
214
static xcb_visualtype_t *
215
get_xcb_visualtype_for_depth(struct dri2_egl_display *dri2_dpy, int depth)
216
{
217
xcb_visualtype_iterator_t visual_iter;
218
xcb_screen_t *screen = dri2_dpy->screen;
219
xcb_depth_iterator_t depth_iter = xcb_screen_allowed_depths_iterator(screen);
220
221
for (; depth_iter.rem; xcb_depth_next(&depth_iter)) {
222
if (depth_iter.data->depth != depth)
223
continue;
224
225
visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
226
if (visual_iter.rem)
227
return visual_iter.data;
228
}
229
230
return NULL;
231
}
232
233
/* Get red channel mask for given depth. */
234
unsigned int
235
dri2_x11_get_red_mask_for_depth(struct dri2_egl_display *dri2_dpy, int depth)
236
{
237
xcb_visualtype_t *visual = get_xcb_visualtype_for_depth(dri2_dpy, depth);
238
239
if (visual)
240
return visual->red_mask;
241
242
return 0;
243
}
244
245
/**
246
* Called via eglCreateWindowSurface(), drv->CreateWindowSurface().
247
*/
248
static _EGLSurface *
249
dri2_x11_create_surface(_EGLDisplay *disp, EGLint type, _EGLConfig *conf,
250
void *native_surface, const EGLint *attrib_list)
251
{
252
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
253
struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
254
struct dri2_egl_surface *dri2_surf;
255
xcb_get_geometry_cookie_t cookie;
256
xcb_get_geometry_reply_t *reply;
257
xcb_generic_error_t *error;
258
const __DRIconfig *config;
259
260
dri2_surf = calloc(1, sizeof *dri2_surf);
261
if (!dri2_surf) {
262
_eglError(EGL_BAD_ALLOC, "dri2_create_surface");
263
return NULL;
264
}
265
266
if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list,
267
false, native_surface))
268
goto cleanup_surf;
269
270
dri2_surf->region = XCB_NONE;
271
if (type == EGL_PBUFFER_BIT) {
272
dri2_surf->drawable = xcb_generate_id(dri2_dpy->conn);
273
xcb_create_pixmap(dri2_dpy->conn, conf->BufferSize,
274
dri2_surf->drawable, dri2_dpy->screen->root,
275
dri2_surf->base.Width, dri2_surf->base.Height);
276
} else {
277
STATIC_ASSERT(sizeof(uintptr_t) == sizeof(native_surface));
278
dri2_surf->drawable = (uintptr_t) native_surface;
279
}
280
281
config = dri2_get_dri_config(dri2_conf, type,
282
dri2_surf->base.GLColorspace);
283
284
if (!config) {
285
_eglError(EGL_BAD_MATCH, "Unsupported surfacetype/colorspace configuration");
286
goto cleanup_pixmap;
287
}
288
289
if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf))
290
goto cleanup_pixmap;
291
292
if (type != EGL_PBUFFER_BIT) {
293
cookie = xcb_get_geometry (dri2_dpy->conn, dri2_surf->drawable);
294
reply = xcb_get_geometry_reply (dri2_dpy->conn, cookie, &error);
295
if (error != NULL) {
296
if (error->error_code == BadAlloc)
297
_eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
298
else if (type == EGL_WINDOW_BIT)
299
_eglError(EGL_BAD_NATIVE_WINDOW, "xcb_get_geometry");
300
else
301
_eglError(EGL_BAD_NATIVE_PIXMAP, "xcb_get_geometry");
302
free(error);
303
free(reply);
304
goto cleanup_dri_drawable;
305
} else if (reply == NULL) {
306
_eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
307
goto cleanup_dri_drawable;
308
}
309
310
dri2_surf->base.Width = reply->width;
311
dri2_surf->base.Height = reply->height;
312
dri2_surf->depth = reply->depth;
313
free(reply);
314
}
315
316
if (dri2_dpy->dri2) {
317
xcb_void_cookie_t cookie;
318
int conn_error;
319
320
cookie = xcb_dri2_create_drawable_checked(dri2_dpy->conn,
321
dri2_surf->drawable);
322
error = xcb_request_check(dri2_dpy->conn, cookie);
323
conn_error = xcb_connection_has_error(dri2_dpy->conn);
324
if (conn_error || error != NULL) {
325
if (type == EGL_PBUFFER_BIT || conn_error || error->error_code == BadAlloc)
326
_eglError(EGL_BAD_ALLOC, "xcb_dri2_create_drawable_checked");
327
else if (type == EGL_WINDOW_BIT)
328
_eglError(EGL_BAD_NATIVE_WINDOW,
329
"xcb_dri2_create_drawable_checked");
330
else
331
_eglError(EGL_BAD_NATIVE_PIXMAP,
332
"xcb_dri2_create_drawable_checked");
333
free(error);
334
goto cleanup_dri_drawable;
335
}
336
} else {
337
if (type == EGL_PBUFFER_BIT) {
338
dri2_surf->depth = conf->BufferSize;
339
}
340
swrastCreateDrawable(dri2_dpy, dri2_surf);
341
}
342
343
/* we always copy the back buffer to front */
344
dri2_surf->base.PostSubBufferSupportedNV = EGL_TRUE;
345
346
return &dri2_surf->base;
347
348
cleanup_dri_drawable:
349
dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
350
cleanup_pixmap:
351
if (type == EGL_PBUFFER_BIT)
352
xcb_free_pixmap(dri2_dpy->conn, dri2_surf->drawable);
353
cleanup_surf:
354
free(dri2_surf);
355
356
return NULL;
357
}
358
359
/**
360
* Called via eglCreateWindowSurface(), drv->CreateWindowSurface().
361
*/
362
static _EGLSurface *
363
dri2_x11_create_window_surface(_EGLDisplay *disp, _EGLConfig *conf,
364
void *native_window, const EGLint *attrib_list)
365
{
366
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
367
_EGLSurface *surf;
368
369
surf = dri2_x11_create_surface(disp, EGL_WINDOW_BIT, conf,
370
native_window, attrib_list);
371
if (surf != NULL) {
372
/* When we first create the DRI2 drawable, its swap interval on the
373
* server side is 1.
374
*/
375
surf->SwapInterval = 1;
376
377
/* Override that with a driconf-set value. */
378
dri2_x11_swap_interval(disp, surf, dri2_dpy->default_swap_interval);
379
}
380
381
return surf;
382
}
383
384
static _EGLSurface *
385
dri2_x11_create_pixmap_surface(_EGLDisplay *disp, _EGLConfig *conf,
386
void *native_pixmap, const EGLint *attrib_list)
387
{
388
return dri2_x11_create_surface(disp, EGL_PIXMAP_BIT, conf,
389
native_pixmap, attrib_list);
390
}
391
392
static _EGLSurface *
393
dri2_x11_create_pbuffer_surface(_EGLDisplay *disp, _EGLConfig *conf,
394
const EGLint *attrib_list)
395
{
396
return dri2_x11_create_surface(disp, EGL_PBUFFER_BIT, conf,
397
NULL, attrib_list);
398
}
399
400
static EGLBoolean
401
dri2_x11_destroy_surface(_EGLDisplay *disp, _EGLSurface *surf)
402
{
403
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
404
struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
405
406
dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
407
408
if (dri2_dpy->dri2) {
409
xcb_dri2_destroy_drawable (dri2_dpy->conn, dri2_surf->drawable);
410
} else {
411
assert(dri2_dpy->swrast);
412
swrastDestroyDrawable(dri2_dpy, dri2_surf);
413
}
414
415
if (surf->Type == EGL_PBUFFER_BIT)
416
xcb_free_pixmap (dri2_dpy->conn, dri2_surf->drawable);
417
418
dri2_fini_surface(surf);
419
free(surf);
420
421
return EGL_TRUE;
422
}
423
424
/**
425
* Function utilizes swrastGetDrawableInfo to get surface
426
* geometry from x server and calls default query surface
427
* implementation that returns the updated values.
428
*
429
* In case of errors we still return values that we currently
430
* have.
431
*/
432
static EGLBoolean
433
dri2_query_surface(_EGLDisplay *disp, _EGLSurface *surf,
434
EGLint attribute, EGLint *value)
435
{
436
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
437
struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
438
int x, y, w, h;
439
440
__DRIdrawable *drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
441
442
switch (attribute) {
443
case EGL_WIDTH:
444
case EGL_HEIGHT:
445
if (x11_get_drawable_info(drawable, &x, &y, &w, &h, dri2_surf)) {
446
surf->Width = w;
447
surf->Height = h;
448
}
449
break;
450
default:
451
break;
452
}
453
return _eglQuerySurface(disp, surf, attribute, value);
454
}
455
456
/**
457
* Process list of buffer received from the server
458
*
459
* Processes the list of buffers received in a reply from the server to either
460
* \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
461
*/
462
static void
463
dri2_x11_process_buffers(struct dri2_egl_surface *dri2_surf,
464
xcb_dri2_dri2_buffer_t *buffers, unsigned count)
465
{
466
struct dri2_egl_display *dri2_dpy =
467
dri2_egl_display(dri2_surf->base.Resource.Display);
468
xcb_rectangle_t rectangle;
469
470
dri2_surf->have_fake_front = false;
471
472
/* This assumes the DRI2 buffer attachment tokens matches the
473
* __DRIbuffer tokens. */
474
for (unsigned i = 0; i < count; i++) {
475
dri2_surf->buffers[i].attachment = buffers[i].attachment;
476
dri2_surf->buffers[i].name = buffers[i].name;
477
dri2_surf->buffers[i].pitch = buffers[i].pitch;
478
dri2_surf->buffers[i].cpp = buffers[i].cpp;
479
dri2_surf->buffers[i].flags = buffers[i].flags;
480
481
/* We only use the DRI drivers single buffer configs. This
482
* means that if we try to render to a window, DRI2 will give us
483
* the fake front buffer, which we'll use as a back buffer.
484
* Note that EGL doesn't require that several clients rendering
485
* to the same window must see the same aux buffers. */
486
if (dri2_surf->buffers[i].attachment == __DRI_BUFFER_FAKE_FRONT_LEFT)
487
dri2_surf->have_fake_front = true;
488
}
489
490
if (dri2_surf->region != XCB_NONE)
491
xcb_xfixes_destroy_region(dri2_dpy->conn, dri2_surf->region);
492
493
rectangle.x = 0;
494
rectangle.y = 0;
495
rectangle.width = dri2_surf->base.Width;
496
rectangle.height = dri2_surf->base.Height;
497
dri2_surf->region = xcb_generate_id(dri2_dpy->conn);
498
xcb_xfixes_create_region(dri2_dpy->conn, dri2_surf->region, 1, &rectangle);
499
}
500
501
static __DRIbuffer *
502
dri2_x11_get_buffers(__DRIdrawable * driDrawable,
503
int *width, int *height,
504
unsigned int *attachments, int count,
505
int *out_count, void *loaderPrivate)
506
{
507
struct dri2_egl_surface *dri2_surf = loaderPrivate;
508
struct dri2_egl_display *dri2_dpy =
509
dri2_egl_display(dri2_surf->base.Resource.Display);
510
xcb_dri2_dri2_buffer_t *buffers;
511
xcb_dri2_get_buffers_reply_t *reply;
512
xcb_dri2_get_buffers_cookie_t cookie;
513
514
(void) driDrawable;
515
516
cookie = xcb_dri2_get_buffers_unchecked (dri2_dpy->conn,
517
dri2_surf->drawable,
518
count, count, attachments);
519
reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn, cookie, NULL);
520
if (reply == NULL)
521
return NULL;
522
buffers = xcb_dri2_get_buffers_buffers (reply);
523
if (buffers == NULL) {
524
free(reply);
525
return NULL;
526
}
527
528
*out_count = reply->count;
529
dri2_surf->base.Width = *width = reply->width;
530
dri2_surf->base.Height = *height = reply->height;
531
dri2_x11_process_buffers(dri2_surf, buffers, *out_count);
532
533
free(reply);
534
535
return dri2_surf->buffers;
536
}
537
538
static __DRIbuffer *
539
dri2_x11_get_buffers_with_format(__DRIdrawable * driDrawable,
540
int *width, int *height,
541
unsigned int *attachments, int count,
542
int *out_count, void *loaderPrivate)
543
{
544
struct dri2_egl_surface *dri2_surf = loaderPrivate;
545
struct dri2_egl_display *dri2_dpy =
546
dri2_egl_display(dri2_surf->base.Resource.Display);
547
xcb_dri2_dri2_buffer_t *buffers;
548
xcb_dri2_get_buffers_with_format_reply_t *reply;
549
xcb_dri2_get_buffers_with_format_cookie_t cookie;
550
xcb_dri2_attach_format_t *format_attachments;
551
552
(void) driDrawable;
553
554
format_attachments = (xcb_dri2_attach_format_t *) attachments;
555
cookie = xcb_dri2_get_buffers_with_format_unchecked (dri2_dpy->conn,
556
dri2_surf->drawable,
557
count, count,
558
format_attachments);
559
560
reply = xcb_dri2_get_buffers_with_format_reply (dri2_dpy->conn,
561
cookie, NULL);
562
if (reply == NULL)
563
return NULL;
564
565
buffers = xcb_dri2_get_buffers_with_format_buffers (reply);
566
dri2_surf->base.Width = *width = reply->width;
567
dri2_surf->base.Height = *height = reply->height;
568
*out_count = reply->count;
569
dri2_x11_process_buffers(dri2_surf, buffers, *out_count);
570
571
free(reply);
572
573
return dri2_surf->buffers;
574
}
575
576
static void
577
dri2_x11_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
578
{
579
(void) driDrawable;
580
581
/* FIXME: Does EGL support front buffer rendering at all? */
582
583
#if 0
584
struct dri2_egl_surface *dri2_surf = loaderPrivate;
585
586
dri2WaitGL(dri2_surf);
587
#else
588
(void) loaderPrivate;
589
#endif
590
}
591
592
static int
593
dri2_x11_do_authenticate(struct dri2_egl_display *dri2_dpy, uint32_t id)
594
{
595
xcb_dri2_authenticate_reply_t *authenticate;
596
xcb_dri2_authenticate_cookie_t authenticate_cookie;
597
int ret = 0;
598
599
authenticate_cookie =
600
xcb_dri2_authenticate_unchecked(dri2_dpy->conn, dri2_dpy->screen->root, id);
601
authenticate =
602
xcb_dri2_authenticate_reply(dri2_dpy->conn, authenticate_cookie, NULL);
603
604
if (authenticate == NULL || !authenticate->authenticated)
605
ret = -1;
606
607
free(authenticate);
608
609
return ret;
610
}
611
612
static EGLBoolean
613
dri2_x11_local_authenticate(struct dri2_egl_display *dri2_dpy)
614
{
615
#ifdef HAVE_LIBDRM
616
drm_magic_t magic;
617
618
if (drmGetMagic(dri2_dpy->fd, &magic)) {
619
_eglLog(_EGL_WARNING, "DRI2: failed to get drm magic");
620
return EGL_FALSE;
621
}
622
623
if (dri2_x11_do_authenticate(dri2_dpy, magic) < 0) {
624
_eglLog(_EGL_WARNING, "DRI2: failed to authenticate");
625
return EGL_FALSE;
626
}
627
#endif
628
return EGL_TRUE;
629
}
630
631
static EGLBoolean
632
dri2_x11_connect(struct dri2_egl_display *dri2_dpy)
633
{
634
xcb_xfixes_query_version_reply_t *xfixes_query;
635
xcb_xfixes_query_version_cookie_t xfixes_query_cookie;
636
xcb_dri2_query_version_reply_t *dri2_query;
637
xcb_dri2_query_version_cookie_t dri2_query_cookie;
638
xcb_dri2_connect_reply_t *connect;
639
xcb_dri2_connect_cookie_t connect_cookie;
640
xcb_generic_error_t *error;
641
char *driver_name, *loader_driver_name, *device_name;
642
const xcb_query_extension_reply_t *extension;
643
644
xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_xfixes_id);
645
xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_dri2_id);
646
647
extension = xcb_get_extension_data(dri2_dpy->conn, &xcb_xfixes_id);
648
if (!(extension && extension->present))
649
return EGL_FALSE;
650
651
extension = xcb_get_extension_data(dri2_dpy->conn, &xcb_dri2_id);
652
if (!(extension && extension->present))
653
return EGL_FALSE;
654
655
xfixes_query_cookie = xcb_xfixes_query_version(dri2_dpy->conn,
656
XCB_XFIXES_MAJOR_VERSION,
657
XCB_XFIXES_MINOR_VERSION);
658
659
dri2_query_cookie = xcb_dri2_query_version (dri2_dpy->conn,
660
XCB_DRI2_MAJOR_VERSION,
661
XCB_DRI2_MINOR_VERSION);
662
663
connect_cookie = xcb_dri2_connect_unchecked(dri2_dpy->conn, dri2_dpy->screen->root,
664
XCB_DRI2_DRIVER_TYPE_DRI);
665
666
xfixes_query =
667
xcb_xfixes_query_version_reply (dri2_dpy->conn,
668
xfixes_query_cookie, &error);
669
if (xfixes_query == NULL ||
670
error != NULL || xfixes_query->major_version < 2) {
671
_eglLog(_EGL_WARNING, "DRI2: failed to query xfixes version");
672
free(error);
673
free(xfixes_query);
674
return EGL_FALSE;
675
}
676
free(xfixes_query);
677
678
dri2_query =
679
xcb_dri2_query_version_reply (dri2_dpy->conn, dri2_query_cookie, &error);
680
if (dri2_query == NULL || error != NULL) {
681
_eglLog(_EGL_WARNING, "DRI2: failed to query version");
682
free(error);
683
free(dri2_query);
684
return EGL_FALSE;
685
}
686
dri2_dpy->dri2_major = dri2_query->major_version;
687
dri2_dpy->dri2_minor = dri2_query->minor_version;
688
free(dri2_query);
689
690
connect = xcb_dri2_connect_reply (dri2_dpy->conn, connect_cookie, NULL);
691
if (connect == NULL ||
692
connect->driver_name_length + connect->device_name_length == 0) {
693
_eglLog(_EGL_WARNING, "DRI2: failed to authenticate");
694
free(connect);
695
return EGL_FALSE;
696
}
697
698
device_name = xcb_dri2_connect_device_name (connect);
699
700
dri2_dpy->fd = loader_open_device(device_name);
701
if (dri2_dpy->fd == -1) {
702
_eglLog(_EGL_WARNING,
703
"DRI2: could not open %s (%s)", device_name, strerror(errno));
704
free(connect);
705
return EGL_FALSE;
706
}
707
708
if (!dri2_x11_local_authenticate(dri2_dpy)) {
709
close(dri2_dpy->fd);
710
free(connect);
711
return EGL_FALSE;
712
}
713
714
driver_name = xcb_dri2_connect_driver_name (connect);
715
716
/* If Mesa knows about the appropriate driver for this fd, then trust it.
717
* Otherwise, default to the server's value.
718
*/
719
loader_driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
720
if (loader_driver_name) {
721
dri2_dpy->driver_name = loader_driver_name;
722
} else {
723
dri2_dpy->driver_name =
724
strndup(driver_name,
725
xcb_dri2_connect_driver_name_length(connect));
726
}
727
728
if (dri2_dpy->driver_name == NULL) {
729
close(dri2_dpy->fd);
730
free(connect);
731
return EGL_FALSE;
732
}
733
734
#ifdef HAVE_WAYLAND_PLATFORM
735
dri2_dpy->device_name =
736
strndup(device_name,
737
xcb_dri2_connect_device_name_length(connect));
738
#endif
739
740
free(connect);
741
742
return EGL_TRUE;
743
}
744
745
static int
746
dri2_x11_authenticate(_EGLDisplay *disp, uint32_t id)
747
{
748
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
749
750
return dri2_x11_do_authenticate(dri2_dpy, id);
751
}
752
753
static EGLBoolean
754
dri2_x11_add_configs_for_visuals(struct dri2_egl_display *dri2_dpy,
755
_EGLDisplay *disp, bool supports_preserved)
756
{
757
xcb_depth_iterator_t d;
758
xcb_visualtype_t *visuals;
759
int config_count = 0;
760
EGLint surface_type;
761
762
d = xcb_screen_allowed_depths_iterator(dri2_dpy->screen);
763
764
surface_type =
765
EGL_WINDOW_BIT |
766
EGL_PIXMAP_BIT |
767
EGL_PBUFFER_BIT;
768
769
if (supports_preserved)
770
surface_type |= EGL_SWAP_BEHAVIOR_PRESERVED_BIT;
771
772
while (d.rem > 0) {
773
EGLBoolean class_added[6] = { 0, };
774
775
visuals = xcb_depth_visuals(d.data);
776
777
for (int i = 0; i < xcb_depth_visuals_length(d.data); i++) {
778
if (class_added[visuals[i]._class])
779
continue;
780
781
class_added[visuals[i]._class] = EGL_TRUE;
782
783
for (int j = 0; dri2_dpy->driver_configs[j]; j++) {
784
struct dri2_egl_config *dri2_conf;
785
const __DRIconfig *config = dri2_dpy->driver_configs[j];
786
787
const EGLint config_attrs[] = {
788
EGL_NATIVE_VISUAL_ID, visuals[i].visual_id,
789
EGL_NATIVE_VISUAL_TYPE, visuals[i]._class,
790
EGL_NONE
791
};
792
793
int rgba_shifts[4] = {
794
ffs(visuals[i].red_mask) - 1,
795
ffs(visuals[i].green_mask) - 1,
796
ffs(visuals[i].blue_mask) - 1,
797
-1,
798
};
799
800
unsigned int rgba_sizes[4] = {
801
util_bitcount(visuals[i].red_mask),
802
util_bitcount(visuals[i].green_mask),
803
util_bitcount(visuals[i].blue_mask),
804
0,
805
};
806
807
dri2_conf = dri2_add_config(disp, config, config_count + 1,
808
surface_type, config_attrs,
809
rgba_shifts, rgba_sizes);
810
if (dri2_conf)
811
if (dri2_conf->base.ConfigID == config_count + 1)
812
config_count++;
813
814
/* Allow a 24-bit RGB visual to match a 32-bit RGBA EGLConfig.
815
* Ditto for 30-bit RGB visuals to match a 32-bit RGBA EGLConfig.
816
* Otherwise it will only match a 32-bit RGBA visual. On a
817
* composited window manager on X11, this will make all of the
818
* EGLConfigs with destination alpha get blended by the
819
* compositor. This is probably not what the application
820
* wants... especially on drivers that only have 32-bit RGBA
821
* EGLConfigs! */
822
if (d.data->depth == 24 || d.data->depth == 30) {
823
unsigned int rgba_mask = ~(visuals[i].red_mask |
824
visuals[i].green_mask |
825
visuals[i].blue_mask);
826
rgba_shifts[3] = ffs(rgba_mask) - 1;
827
rgba_sizes[3] = util_bitcount(rgba_mask);
828
dri2_conf = dri2_add_config(disp, config, config_count + 1,
829
surface_type, config_attrs,
830
rgba_shifts, rgba_sizes);
831
if (dri2_conf)
832
if (dri2_conf->base.ConfigID == config_count + 1)
833
config_count++;
834
}
835
}
836
}
837
838
xcb_depth_next(&d);
839
}
840
841
if (!config_count) {
842
_eglLog(_EGL_WARNING, "DRI2: failed to create any config");
843
return EGL_FALSE;
844
}
845
846
return EGL_TRUE;
847
}
848
849
static EGLBoolean
850
dri2_copy_region(_EGLDisplay *disp,
851
_EGLSurface *draw, xcb_xfixes_region_t region)
852
{
853
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
854
struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
855
enum xcb_dri2_attachment_t render_attachment;
856
xcb_dri2_copy_region_cookie_t cookie;
857
858
/* No-op for a pixmap or pbuffer surface */
859
if (draw->Type == EGL_PIXMAP_BIT || draw->Type == EGL_PBUFFER_BIT)
860
return EGL_TRUE;
861
862
dri2_dpy->flush->flush(dri2_surf->dri_drawable);
863
864
if (dri2_surf->have_fake_front)
865
render_attachment = XCB_DRI2_ATTACHMENT_BUFFER_FAKE_FRONT_LEFT;
866
else
867
render_attachment = XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT;
868
869
cookie = xcb_dri2_copy_region_unchecked(dri2_dpy->conn,
870
dri2_surf->drawable,
871
region,
872
XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
873
render_attachment);
874
free(xcb_dri2_copy_region_reply(dri2_dpy->conn, cookie, NULL));
875
876
return EGL_TRUE;
877
}
878
879
static int64_t
880
dri2_x11_swap_buffers_msc(_EGLDisplay *disp, _EGLSurface *draw,
881
int64_t msc, int64_t divisor, int64_t remainder)
882
{
883
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
884
struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
885
uint32_t msc_hi = msc >> 32;
886
uint32_t msc_lo = msc & 0xffffffff;
887
uint32_t divisor_hi = divisor >> 32;
888
uint32_t divisor_lo = divisor & 0xffffffff;
889
uint32_t remainder_hi = remainder >> 32;
890
uint32_t remainder_lo = remainder & 0xffffffff;
891
xcb_dri2_swap_buffers_cookie_t cookie;
892
xcb_dri2_swap_buffers_reply_t *reply;
893
int64_t swap_count = -1;
894
895
if (draw->SwapBehavior == EGL_BUFFER_PRESERVED || !dri2_dpy->swap_available) {
896
swap_count = dri2_copy_region(disp, draw, dri2_surf->region) ? 0 : -1;
897
} else {
898
dri2_flush_drawable_for_swapbuffers(disp, draw);
899
900
cookie = xcb_dri2_swap_buffers_unchecked(dri2_dpy->conn,
901
dri2_surf->drawable, msc_hi,
902
msc_lo, divisor_hi, divisor_lo,
903
remainder_hi, remainder_lo);
904
905
reply = xcb_dri2_swap_buffers_reply(dri2_dpy->conn, cookie, NULL);
906
907
if (reply) {
908
swap_count = combine_u32_into_u64(reply->swap_hi, reply->swap_lo);
909
free(reply);
910
}
911
}
912
913
/* Since we aren't watching for the server's invalidate events like we're
914
* supposed to (due to XCB providing no mechanism for filtering the events
915
* the way xlib does), and SwapBuffers is a common cause of invalidate
916
* events, just shove one down to the driver, even though we haven't told
917
* the driver that we're the kind of loader that provides reliable
918
* invalidate events. This causes the driver to request buffers again at
919
* its next draw, so that we get the correct buffers if a pageflip
920
* happened. The driver should still be using the viewport hack to catch
921
* window resizes.
922
*/
923
if (dri2_dpy->flush->base.version >= 3 && dri2_dpy->flush->invalidate)
924
dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
925
926
return swap_count;
927
}
928
929
static EGLBoolean
930
dri2_x11_swap_buffers(_EGLDisplay *disp, _EGLSurface *draw)
931
{
932
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
933
struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
934
935
if (!dri2_dpy->flush) {
936
dri2_dpy->core->swapBuffers(dri2_surf->dri_drawable);
937
return EGL_TRUE;
938
}
939
940
if (dri2_x11_swap_buffers_msc(disp, draw, 0, 0, 0) == -1) {
941
/* Swap failed with a window drawable. */
942
return _eglError(EGL_BAD_NATIVE_WINDOW, __func__);
943
}
944
return EGL_TRUE;
945
}
946
947
static EGLBoolean
948
dri2_x11_swap_buffers_region(_EGLDisplay *disp, _EGLSurface *draw,
949
EGLint numRects, const EGLint *rects)
950
{
951
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
952
struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
953
EGLBoolean ret;
954
xcb_xfixes_region_t region;
955
xcb_rectangle_t rectangles[16];
956
957
if (numRects > (int)ARRAY_SIZE(rectangles))
958
return dri2_copy_region(disp, draw, dri2_surf->region);
959
960
for (int i = 0; i < numRects; i++) {
961
rectangles[i].x = rects[i * 4];
962
rectangles[i].y = dri2_surf->base.Height - rects[i * 4 + 1] - rects[i * 4 + 3];
963
rectangles[i].width = rects[i * 4 + 2];
964
rectangles[i].height = rects[i * 4 + 3];
965
}
966
967
region = xcb_generate_id(dri2_dpy->conn);
968
xcb_xfixes_create_region(dri2_dpy->conn, region, numRects, rectangles);
969
ret = dri2_copy_region(disp, draw, region);
970
xcb_xfixes_destroy_region(dri2_dpy->conn, region);
971
972
return ret;
973
}
974
975
static EGLBoolean
976
dri2_x11_post_sub_buffer(_EGLDisplay *disp, _EGLSurface *draw,
977
EGLint x, EGLint y, EGLint width, EGLint height)
978
{
979
const EGLint rect[4] = { x, y, width, height };
980
981
if (x < 0 || y < 0 || width < 0 || height < 0)
982
_eglError(EGL_BAD_PARAMETER, "eglPostSubBufferNV");
983
984
return dri2_x11_swap_buffers_region(disp, draw, 1, rect);
985
}
986
987
static EGLBoolean
988
dri2_x11_swap_interval(_EGLDisplay *disp, _EGLSurface *surf, EGLint interval)
989
{
990
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
991
struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
992
993
if (dri2_dpy->swap_available)
994
xcb_dri2_swap_interval(dri2_dpy->conn, dri2_surf->drawable, interval);
995
996
return EGL_TRUE;
997
}
998
999
static EGLBoolean
1000
dri2_x11_copy_buffers(_EGLDisplay *disp, _EGLSurface *surf, void *native_pixmap_target)
1001
{
1002
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1003
struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1004
xcb_gcontext_t gc;
1005
xcb_pixmap_t target;
1006
1007
STATIC_ASSERT(sizeof(uintptr_t) == sizeof(native_pixmap_target));
1008
target = (uintptr_t) native_pixmap_target;
1009
1010
dri2_dpy->flush->flush(dri2_surf->dri_drawable);
1011
1012
gc = xcb_generate_id(dri2_dpy->conn);
1013
xcb_create_gc(dri2_dpy->conn, gc, target, 0, NULL);
1014
xcb_copy_area(dri2_dpy->conn,
1015
dri2_surf->drawable,
1016
target,
1017
gc,
1018
0, 0,
1019
0, 0,
1020
dri2_surf->base.Width,
1021
dri2_surf->base.Height);
1022
xcb_free_gc(dri2_dpy->conn, gc);
1023
1024
return EGL_TRUE;
1025
}
1026
1027
uint32_t
1028
dri2_format_for_depth(struct dri2_egl_display *dri2_dpy, uint32_t depth)
1029
{
1030
switch (depth) {
1031
case 16:
1032
return __DRI_IMAGE_FORMAT_RGB565;
1033
case 24:
1034
return __DRI_IMAGE_FORMAT_XRGB8888;
1035
case 30:
1036
/* Different preferred formats for different hw */
1037
if (dri2_x11_get_red_mask_for_depth(dri2_dpy, 30) == 0x3ff)
1038
return __DRI_IMAGE_FORMAT_XBGR2101010;
1039
else
1040
return __DRI_IMAGE_FORMAT_XRGB2101010;
1041
case 32:
1042
return __DRI_IMAGE_FORMAT_ARGB8888;
1043
default:
1044
return __DRI_IMAGE_FORMAT_NONE;
1045
}
1046
}
1047
1048
static _EGLImage *
1049
dri2_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx,
1050
EGLClientBuffer buffer, const EGLint *attr_list)
1051
{
1052
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1053
struct dri2_egl_image *dri2_img;
1054
unsigned int attachments[1];
1055
xcb_drawable_t drawable;
1056
xcb_dri2_get_buffers_cookie_t buffers_cookie;
1057
xcb_dri2_get_buffers_reply_t *buffers_reply;
1058
xcb_dri2_dri2_buffer_t *buffers;
1059
xcb_get_geometry_cookie_t geometry_cookie;
1060
xcb_get_geometry_reply_t *geometry_reply;
1061
xcb_generic_error_t *error;
1062
int stride, format;
1063
1064
(void) ctx;
1065
1066
drawable = (xcb_drawable_t) (uintptr_t) buffer;
1067
xcb_dri2_create_drawable (dri2_dpy->conn, drawable);
1068
attachments[0] = XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT;
1069
buffers_cookie =
1070
xcb_dri2_get_buffers_unchecked (dri2_dpy->conn,
1071
drawable, 1, 1, attachments);
1072
geometry_cookie = xcb_get_geometry (dri2_dpy->conn, drawable);
1073
buffers_reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn,
1074
buffers_cookie, NULL);
1075
if (buffers_reply == NULL)
1076
return NULL;
1077
1078
buffers = xcb_dri2_get_buffers_buffers (buffers_reply);
1079
if (buffers == NULL) {
1080
free(buffers_reply);
1081
return NULL;
1082
}
1083
1084
geometry_reply = xcb_get_geometry_reply (dri2_dpy->conn,
1085
geometry_cookie, &error);
1086
if (geometry_reply == NULL || error != NULL) {
1087
_eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
1088
free(error);
1089
free(buffers_reply);
1090
free(geometry_reply);
1091
return NULL;
1092
}
1093
1094
format = dri2_format_for_depth(dri2_dpy, geometry_reply->depth);
1095
if (format == __DRI_IMAGE_FORMAT_NONE) {
1096
_eglError(EGL_BAD_PARAMETER,
1097
"dri2_create_image_khr: unsupported pixmap depth");
1098
free(buffers_reply);
1099
free(geometry_reply);
1100
return NULL;
1101
}
1102
1103
dri2_img = malloc(sizeof *dri2_img);
1104
if (!dri2_img) {
1105
free(buffers_reply);
1106
free(geometry_reply);
1107
_eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
1108
return EGL_NO_IMAGE_KHR;
1109
}
1110
1111
_eglInitImage(&dri2_img->base, disp);
1112
1113
stride = buffers[0].pitch / buffers[0].cpp;
1114
dri2_img->dri_image =
1115
dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
1116
buffers_reply->width,
1117
buffers_reply->height,
1118
format,
1119
buffers[0].name,
1120
stride,
1121
dri2_img);
1122
1123
free(buffers_reply);
1124
free(geometry_reply);
1125
1126
return &dri2_img->base;
1127
}
1128
1129
static _EGLImage *
1130
dri2_x11_create_image_khr(_EGLDisplay *disp, _EGLContext *ctx, EGLenum target,
1131
EGLClientBuffer buffer, const EGLint *attr_list)
1132
{
1133
switch (target) {
1134
case EGL_NATIVE_PIXMAP_KHR:
1135
return dri2_create_image_khr_pixmap(disp, ctx, buffer, attr_list);
1136
default:
1137
return dri2_create_image_khr(disp, ctx, target, buffer, attr_list);
1138
}
1139
}
1140
1141
static EGLBoolean
1142
dri2_x11_get_sync_values(_EGLDisplay *display, _EGLSurface *surface,
1143
EGLuint64KHR *ust, EGLuint64KHR *msc,
1144
EGLuint64KHR *sbc)
1145
{
1146
struct dri2_egl_display *dri2_dpy = dri2_egl_display(display);
1147
struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
1148
xcb_dri2_get_msc_cookie_t cookie;
1149
xcb_dri2_get_msc_reply_t *reply;
1150
1151
cookie = xcb_dri2_get_msc(dri2_dpy->conn, dri2_surf->drawable);
1152
reply = xcb_dri2_get_msc_reply(dri2_dpy->conn, cookie, NULL);
1153
1154
if (!reply)
1155
return _eglError(EGL_BAD_ACCESS, __func__);
1156
1157
*ust = ((EGLuint64KHR) reply->ust_hi << 32) | reply->ust_lo;
1158
*msc = ((EGLuint64KHR) reply->msc_hi << 32) | reply->msc_lo;
1159
*sbc = ((EGLuint64KHR) reply->sbc_hi << 32) | reply->sbc_lo;
1160
free(reply);
1161
1162
return EGL_TRUE;
1163
}
1164
1165
static const struct dri2_egl_display_vtbl dri2_x11_swrast_display_vtbl = {
1166
.authenticate = NULL,
1167
.create_window_surface = dri2_x11_create_window_surface,
1168
.create_pixmap_surface = dri2_x11_create_pixmap_surface,
1169
.create_pbuffer_surface = dri2_x11_create_pbuffer_surface,
1170
.destroy_surface = dri2_x11_destroy_surface,
1171
.create_image = dri2_create_image_khr,
1172
.swap_buffers = dri2_x11_swap_buffers,
1173
/* XXX: should really implement this since X11 has pixmaps */
1174
.query_surface = dri2_query_surface,
1175
.get_dri_drawable = dri2_surface_get_dri_drawable,
1176
};
1177
1178
static const struct dri2_egl_display_vtbl dri2_x11_display_vtbl = {
1179
.authenticate = dri2_x11_authenticate,
1180
.create_window_surface = dri2_x11_create_window_surface,
1181
.create_pixmap_surface = dri2_x11_create_pixmap_surface,
1182
.create_pbuffer_surface = dri2_x11_create_pbuffer_surface,
1183
.destroy_surface = dri2_x11_destroy_surface,
1184
.create_image = dri2_x11_create_image_khr,
1185
.swap_interval = dri2_x11_swap_interval,
1186
.swap_buffers = dri2_x11_swap_buffers,
1187
.swap_buffers_region = dri2_x11_swap_buffers_region,
1188
.post_sub_buffer = dri2_x11_post_sub_buffer,
1189
.copy_buffers = dri2_x11_copy_buffers,
1190
.query_surface = dri2_query_surface,
1191
.get_sync_values = dri2_x11_get_sync_values,
1192
.get_dri_drawable = dri2_surface_get_dri_drawable,
1193
};
1194
1195
static const __DRIswrastLoaderExtension swrast_loader_extension = {
1196
.base = { __DRI_SWRAST_LOADER, 1 },
1197
1198
.getDrawableInfo = swrastGetDrawableInfo,
1199
.putImage = swrastPutImage,
1200
.getImage = swrastGetImage,
1201
};
1202
1203
static const __DRIextension *swrast_loader_extensions[] = {
1204
&swrast_loader_extension.base,
1205
&image_lookup_extension.base,
1206
NULL,
1207
};
1208
1209
static int
1210
dri2_find_screen_for_display(const _EGLDisplay *disp, int fallback_screen)
1211
{
1212
const EGLAttrib *attr;
1213
1214
if (!disp->Options.Attribs)
1215
return fallback_screen;
1216
1217
for (attr = disp->Options.Attribs; attr[0] != EGL_NONE; attr += 2) {
1218
if (attr[0] == EGL_PLATFORM_X11_SCREEN_EXT ||
1219
attr[0] == EGL_PLATFORM_XCB_SCREEN_EXT)
1220
return attr[1];
1221
}
1222
1223
return fallback_screen;
1224
}
1225
1226
static EGLBoolean
1227
dri2_get_xcb_connection(_EGLDisplay *disp,
1228
struct dri2_egl_display *dri2_dpy)
1229
{
1230
xcb_screen_iterator_t s;
1231
int screen;
1232
const char *msg;
1233
1234
disp->DriverData = (void *) dri2_dpy;
1235
if (disp->PlatformDisplay == NULL) {
1236
dri2_dpy->conn = xcb_connect(NULL, &screen);
1237
dri2_dpy->own_device = true;
1238
screen = dri2_find_screen_for_display(disp, screen);
1239
} else if (disp->Platform == _EGL_PLATFORM_X11) {
1240
Display *dpy = disp->PlatformDisplay;
1241
dri2_dpy->conn = XGetXCBConnection(dpy);
1242
screen = DefaultScreen(dpy);
1243
} else {
1244
/* _EGL_PLATFORM_XCB */
1245
dri2_dpy->conn = disp->PlatformDisplay;
1246
screen = dri2_find_screen_for_display(disp, 0);
1247
}
1248
1249
if (!dri2_dpy->conn || xcb_connection_has_error(dri2_dpy->conn)) {
1250
msg = "xcb_connect failed";
1251
goto disconnect;
1252
}
1253
1254
s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
1255
dri2_dpy->screen = get_xcb_screen(s, screen);
1256
if (!dri2_dpy->screen) {
1257
msg = "failed to get xcb screen";
1258
goto disconnect;
1259
}
1260
1261
return EGL_TRUE;
1262
disconnect:
1263
if (disp->PlatformDisplay == NULL)
1264
xcb_disconnect(dri2_dpy->conn);
1265
1266
return _eglError(EGL_BAD_ALLOC, msg);
1267
}
1268
1269
static EGLBoolean
1270
dri2_initialize_x11_swrast(_EGLDisplay *disp)
1271
{
1272
_EGLDevice *dev;
1273
struct dri2_egl_display *dri2_dpy;
1274
1275
dri2_dpy = calloc(1, sizeof *dri2_dpy);
1276
if (!dri2_dpy)
1277
return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1278
1279
dri2_dpy->fd = -1;
1280
if (!dri2_get_xcb_connection(disp, dri2_dpy))
1281
goto cleanup;
1282
1283
dev = _eglAddDevice(dri2_dpy->fd, true);
1284
if (!dev) {
1285
_eglError(EGL_NOT_INITIALIZED, "DRI2: failed to find EGLDevice");
1286
goto cleanup;
1287
}
1288
1289
disp->Device = dev;
1290
1291
/*
1292
* Every hardware driver_name is set using strdup. Doing the same in
1293
* here will allow is to simply free the memory at dri2_terminate().
1294
*/
1295
dri2_dpy->driver_name = strdup("swrast");
1296
if (!dri2_load_driver_swrast(disp))
1297
goto cleanup;
1298
1299
dri2_dpy->loader_extensions = swrast_loader_extensions;
1300
1301
if (!dri2_create_screen(disp))
1302
goto cleanup;
1303
1304
if (!dri2_setup_extensions(disp))
1305
goto cleanup;
1306
1307
dri2_setup_screen(disp);
1308
1309
if (!dri2_x11_add_configs_for_visuals(dri2_dpy, disp, true))
1310
goto cleanup;
1311
1312
/* Fill vtbl last to prevent accidentally calling virtual function during
1313
* initialization.
1314
*/
1315
dri2_dpy->vtbl = &dri2_x11_swrast_display_vtbl;
1316
1317
return EGL_TRUE;
1318
1319
cleanup:
1320
dri2_display_destroy(disp);
1321
return EGL_FALSE;
1322
}
1323
1324
static void
1325
dri2_x11_setup_swap_interval(_EGLDisplay *disp)
1326
{
1327
struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1328
int arbitrary_max_interval = 1000;
1329
1330
/* default behavior for no SwapBuffers support: no vblank syncing
1331
* either.
1332
*/
1333
dri2_dpy->min_swap_interval = 0;
1334
dri2_dpy->max_swap_interval = 0;
1335
dri2_dpy->default_swap_interval = 0;
1336
1337
if (!dri2_dpy->swap_available)
1338
return;
1339
1340
/* If we do have swapbuffers, then we can support pretty much any swap
1341
* interval.
1342
*/
1343
dri2_setup_swap_interval(disp, arbitrary_max_interval);
1344
}
1345
1346
#ifdef HAVE_DRI3
1347
1348
static const __DRIextension *dri3_image_loader_extensions[] = {
1349
&dri3_image_loader_extension.base,
1350
&image_lookup_extension.base,
1351
&use_invalidate.base,
1352
&background_callable_extension.base,
1353
NULL,
1354
};
1355
1356
static EGLBoolean
1357
dri2_initialize_x11_dri3(_EGLDisplay *disp)
1358
{
1359
_EGLDevice *dev;
1360
struct dri2_egl_display *dri2_dpy;
1361
1362
dri2_dpy = calloc(1, sizeof *dri2_dpy);
1363
if (!dri2_dpy)
1364
return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1365
1366
dri2_dpy->fd = -1;
1367
if (!dri2_get_xcb_connection(disp, dri2_dpy))
1368
goto cleanup;
1369
1370
if (!dri3_x11_connect(dri2_dpy))
1371
goto cleanup;
1372
1373
dev = _eglAddDevice(dri2_dpy->fd, false);
1374
if (!dev) {
1375
_eglError(EGL_NOT_INITIALIZED, "DRI2: failed to find EGLDevice");
1376
goto cleanup;
1377
}
1378
1379
disp->Device = dev;
1380
1381
if (!dri2_load_driver_dri3(disp))
1382
goto cleanup;
1383
1384
dri2_dpy->loader_extensions = dri3_image_loader_extensions;
1385
1386
dri2_dpy->swap_available = true;
1387
dri2_dpy->invalidate_available = true;
1388
1389
if (!dri2_create_screen(disp))
1390
goto cleanup;
1391
1392
if (!dri2_setup_extensions(disp))
1393
goto cleanup;
1394
1395
dri2_setup_screen(disp);
1396
1397
dri2_x11_setup_swap_interval(disp);
1398
1399
if (!dri2_dpy->is_different_gpu)
1400
disp->Extensions.KHR_image_pixmap = EGL_TRUE;
1401
disp->Extensions.NOK_texture_from_pixmap = EGL_TRUE;
1402
disp->Extensions.CHROMIUM_sync_control = EGL_TRUE;
1403
disp->Extensions.EXT_buffer_age = EGL_TRUE;
1404
disp->Extensions.EXT_swap_buffers_with_damage = EGL_TRUE;
1405
1406
dri2_set_WL_bind_wayland_display(disp);
1407
1408
if (!dri2_x11_add_configs_for_visuals(dri2_dpy, disp, false))
1409
goto cleanup;
1410
1411
dri2_dpy->loader_dri3_ext.core = dri2_dpy->core;
1412
dri2_dpy->loader_dri3_ext.image_driver = dri2_dpy->image_driver;
1413
dri2_dpy->loader_dri3_ext.flush = dri2_dpy->flush;
1414
dri2_dpy->loader_dri3_ext.tex_buffer = dri2_dpy->tex_buffer;
1415
dri2_dpy->loader_dri3_ext.image = dri2_dpy->image;
1416
dri2_dpy->loader_dri3_ext.config = dri2_dpy->config;
1417
1418
/* Fill vtbl last to prevent accidentally calling virtual function during
1419
* initialization.
1420
*/
1421
dri2_dpy->vtbl = &dri3_x11_display_vtbl;
1422
1423
_eglLog(_EGL_INFO, "Using DRI3");
1424
1425
return EGL_TRUE;
1426
1427
cleanup:
1428
dri2_display_destroy(disp);
1429
return EGL_FALSE;
1430
}
1431
#endif
1432
1433
static const __DRIdri2LoaderExtension dri2_loader_extension_old = {
1434
.base = { __DRI_DRI2_LOADER, 2 },
1435
1436
.getBuffers = dri2_x11_get_buffers,
1437
.flushFrontBuffer = dri2_x11_flush_front_buffer,
1438
.getBuffersWithFormat = NULL,
1439
};
1440
1441
static const __DRIdri2LoaderExtension dri2_loader_extension = {
1442
.base = { __DRI_DRI2_LOADER, 3 },
1443
1444
.getBuffers = dri2_x11_get_buffers,
1445
.flushFrontBuffer = dri2_x11_flush_front_buffer,
1446
.getBuffersWithFormat = dri2_x11_get_buffers_with_format,
1447
};
1448
1449
static const __DRIextension *dri2_loader_extensions_old[] = {
1450
&dri2_loader_extension_old.base,
1451
&image_lookup_extension.base,
1452
&background_callable_extension.base,
1453
NULL,
1454
};
1455
1456
static const __DRIextension *dri2_loader_extensions[] = {
1457
&dri2_loader_extension.base,
1458
&image_lookup_extension.base,
1459
&use_invalidate.base,
1460
&background_callable_extension.base,
1461
NULL,
1462
};
1463
1464
static EGLBoolean
1465
dri2_initialize_x11_dri2(_EGLDisplay *disp)
1466
{
1467
_EGLDevice *dev;
1468
struct dri2_egl_display *dri2_dpy;
1469
1470
dri2_dpy = calloc(1, sizeof *dri2_dpy);
1471
if (!dri2_dpy)
1472
return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1473
1474
dri2_dpy->fd = -1;
1475
if (!dri2_get_xcb_connection(disp, dri2_dpy))
1476
goto cleanup;
1477
1478
if (!dri2_x11_connect(dri2_dpy))
1479
goto cleanup;
1480
1481
dev = _eglAddDevice(dri2_dpy->fd, false);
1482
if (!dev) {
1483
_eglError(EGL_NOT_INITIALIZED, "DRI2: failed to find EGLDevice");
1484
goto cleanup;
1485
}
1486
1487
disp->Device = dev;
1488
1489
if (!dri2_load_driver(disp))
1490
goto cleanup;
1491
1492
if (dri2_dpy->dri2_minor >= 1)
1493
dri2_dpy->loader_extensions = dri2_loader_extensions;
1494
else
1495
dri2_dpy->loader_extensions = dri2_loader_extensions_old;
1496
1497
dri2_dpy->swap_available = (dri2_dpy->dri2_minor >= 2);
1498
dri2_dpy->invalidate_available = (dri2_dpy->dri2_minor >= 3);
1499
1500
if (!dri2_create_screen(disp))
1501
goto cleanup;
1502
1503
if (!dri2_setup_extensions(disp))
1504
goto cleanup;
1505
1506
dri2_setup_screen(disp);
1507
1508
dri2_x11_setup_swap_interval(disp);
1509
1510
disp->Extensions.KHR_image_pixmap = EGL_TRUE;
1511
disp->Extensions.NOK_swap_region = EGL_TRUE;
1512
disp->Extensions.NOK_texture_from_pixmap = EGL_TRUE;
1513
disp->Extensions.NV_post_sub_buffer = EGL_TRUE;
1514
disp->Extensions.CHROMIUM_sync_control = EGL_TRUE;
1515
1516
dri2_set_WL_bind_wayland_display(disp);
1517
1518
if (!dri2_x11_add_configs_for_visuals(dri2_dpy, disp, true))
1519
goto cleanup;
1520
1521
/* Fill vtbl last to prevent accidentally calling virtual function during
1522
* initialization.
1523
*/
1524
dri2_dpy->vtbl = &dri2_x11_display_vtbl;
1525
1526
_eglLog(_EGL_INFO, "Using DRI2");
1527
1528
return EGL_TRUE;
1529
1530
cleanup:
1531
dri2_display_destroy(disp);
1532
return EGL_FALSE;
1533
}
1534
1535
EGLBoolean
1536
dri2_initialize_x11(_EGLDisplay *disp)
1537
{
1538
if (disp->Options.ForceSoftware)
1539
return dri2_initialize_x11_swrast(disp);
1540
1541
#ifdef HAVE_DRI3
1542
if (!env_var_as_boolean("LIBGL_DRI3_DISABLE", false))
1543
if (dri2_initialize_x11_dri3(disp))
1544
return EGL_TRUE;
1545
#endif
1546
1547
if (!env_var_as_boolean("LIBGL_DRI2_DISABLE", false))
1548
if (dri2_initialize_x11_dri2(disp))
1549
return EGL_TRUE;
1550
1551
return EGL_FALSE;
1552
}
1553
1554
void
1555
dri2_teardown_x11(struct dri2_egl_display *dri2_dpy)
1556
{
1557
if (dri2_dpy->own_device)
1558
xcb_disconnect(dri2_dpy->conn);
1559
}
1560
1561