• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <krh@bitplanet.net>
26  */
27 
28 #include <dlfcn.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <limits.h>
32 #include <stdbool.h>
33 #include <stdint.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 /* clang-format off */
39 #include <xcb/xcb.h>
40 #include <xcb/shm.h>
41 #include <vulkan/vulkan_core.h>
42 #include <vulkan/vulkan_xcb.h>
43 /* clang-format on */
44 #ifdef HAVE_LIBDRM
45 #include <xf86drm.h>
46 #include "platform_x11_dri3.h"
47 #endif
48 #include "util/bitscan.h"
49 #include "util/macros.h"
50 #include "util/u_debug.h"
51 #include "util/log.h"
52 #include <sys/stat.h>
53 #include <sys/types.h>
54 #include "loader_x11.h"
55 #include "kopper_interface.h"
56 #include "loader.h"
57 #include "platform_x11.h"
58 #include "drm-uapi/drm_fourcc.h"
59 #include "dri_util.h"
60 
61 
62 static EGLBoolean
63 dri2_x11_swap_interval(_EGLDisplay *disp, _EGLSurface *surf, EGLint interval);
64 
65 static void
swrastCreateDrawable(struct dri2_egl_display * dri2_dpy,struct dri2_egl_surface * dri2_surf)66 swrastCreateDrawable(struct dri2_egl_display *dri2_dpy,
67                      struct dri2_egl_surface *dri2_surf)
68 {
69    uint32_t mask;
70    const uint32_t function = GXcopy;
71    uint32_t valgc[2];
72 
73    /* create GC's */
74    dri2_surf->gc = xcb_generate_id(dri2_dpy->conn);
75    mask = XCB_GC_FUNCTION;
76    xcb_create_gc(dri2_dpy->conn, dri2_surf->gc, dri2_surf->drawable, mask,
77                  &function);
78 
79    dri2_surf->swapgc = xcb_generate_id(dri2_dpy->conn);
80    mask = XCB_GC_FUNCTION | XCB_GC_GRAPHICS_EXPOSURES;
81    valgc[0] = function;
82    valgc[1] = False;
83    xcb_create_gc(dri2_dpy->conn, dri2_surf->swapgc, dri2_surf->drawable, mask,
84                  valgc);
85    switch (dri2_surf->depth) {
86    case 32:
87    case 30:
88    case 24:
89       dri2_surf->bytes_per_pixel = 4;
90       break;
91    case 16:
92       dri2_surf->bytes_per_pixel = 2;
93       break;
94    case 8:
95       dri2_surf->bytes_per_pixel = 1;
96       break;
97    case 0:
98       dri2_surf->bytes_per_pixel = 0;
99       break;
100    default:
101       _eglLog(_EGL_WARNING, "unsupported depth %d", dri2_surf->depth);
102    }
103 }
104 
105 static void
swrastDestroyDrawable(struct dri2_egl_display * dri2_dpy,struct dri2_egl_surface * dri2_surf)106 swrastDestroyDrawable(struct dri2_egl_display *dri2_dpy,
107                       struct dri2_egl_surface *dri2_surf)
108 {
109    xcb_free_gc(dri2_dpy->conn, dri2_surf->gc);
110    xcb_free_gc(dri2_dpy->conn, dri2_surf->swapgc);
111 }
112 
113 static bool
x11_get_drawable_info(struct dri_drawable * draw,int * x,int * y,int * w,int * h,void * loaderPrivate)114 x11_get_drawable_info(struct dri_drawable *draw, int *x, int *y, int *w, int *h,
115                       void *loaderPrivate)
116 {
117    struct dri2_egl_surface *dri2_surf = loaderPrivate;
118    struct dri2_egl_display *dri2_dpy =
119       dri2_egl_display(dri2_surf->base.Resource.Display);
120 
121    xcb_get_geometry_cookie_t cookie;
122    xcb_get_geometry_reply_t *reply;
123    xcb_generic_error_t *error;
124    bool ret;
125 
126    cookie = xcb_get_geometry(dri2_dpy->conn, dri2_surf->drawable);
127    reply = xcb_get_geometry_reply(dri2_dpy->conn, cookie, &error);
128    if (reply == NULL)
129       return false;
130 
131    if (error != NULL) {
132       ret = false;
133       _eglLog(_EGL_WARNING, "error in xcb_get_geometry");
134       free(error);
135    } else {
136       *x = reply->x;
137       *y = reply->y;
138       *w = reply->width;
139       *h = reply->height;
140       ret = true;
141    }
142    free(reply);
143    return ret;
144 }
145 
146 static void
swrastGetDrawableInfo(struct dri_drawable * draw,int * x,int * y,int * w,int * h,void * loaderPrivate)147 swrastGetDrawableInfo(struct dri_drawable *draw, int *x, int *y, int *w, int *h,
148                       void *loaderPrivate)
149 {
150    *x = *y = *w = *h = 0;
151    x11_get_drawable_info(draw, x, y, w, h, loaderPrivate);
152 }
153 
154 static void
swrastPutImage2(struct dri_drawable * draw,int op,int x,int y,int w,int h,int stride,char * data,void * loaderPrivate)155 swrastPutImage2(struct dri_drawable *draw, int op, int x, int y, int w, int h,
156                 int stride, char *data, void *loaderPrivate)
157 {
158    struct dri2_egl_surface *dri2_surf = loaderPrivate;
159    struct dri2_egl_display *dri2_dpy =
160       dri2_egl_display(dri2_surf->base.Resource.Display);
161    int stride_b = dri2_surf->bytes_per_pixel * w;
162    size_t hdr_len = sizeof(xcb_put_image_request_t);
163    size_t size = (hdr_len + stride_b * h) >> 2;
164    uint64_t max_req_len = xcb_get_maximum_request_length(dri2_dpy->conn);
165 
166    xcb_gcontext_t gc;
167    xcb_void_cookie_t cookie;
168    switch (op) {
169    case __DRI_SWRAST_IMAGE_OP_DRAW:
170       gc = dri2_surf->gc;
171       break;
172    case __DRI_SWRAST_IMAGE_OP_SWAP:
173       gc = dri2_surf->swapgc;
174       break;
175    default:
176       return;
177    }
178 
179    /* clamp to drawable size */
180    if (y + h > dri2_surf->base.Height)
181       h = dri2_surf->base.Height - y;
182 
183    /* If stride of pixels to copy is different from the surface stride
184     * then we need to copy lines one by one.
185     */
186    if (stride_b != stride) {
187       for (unsigned i = 0; i < h; i++) {
188          cookie = xcb_put_image(
189             dri2_dpy->conn, XCB_IMAGE_FORMAT_Z_PIXMAP, dri2_surf->drawable, gc, w,
190             1, x, y+i, 0, dri2_surf->depth, stride_b, (uint8_t*)data);
191          xcb_discard_reply(dri2_dpy->conn, cookie.sequence);
192 
193          data += stride;
194       }
195    } else if (size < max_req_len) {
196       cookie = xcb_put_image(
197          dri2_dpy->conn, XCB_IMAGE_FORMAT_Z_PIXMAP, dri2_surf->drawable, gc, w,
198          h, x, y, 0, dri2_surf->depth, h * stride_b, (const uint8_t *)data);
199       xcb_discard_reply(dri2_dpy->conn, cookie.sequence);
200    } else {
201       int num_lines = ((max_req_len << 2) - hdr_len) / stride_b;
202       int y_start = 0;
203       int y_todo = h;
204       while (y_todo) {
205          int this_lines = MIN2(num_lines, y_todo);
206          cookie =
207             xcb_put_image(dri2_dpy->conn, XCB_IMAGE_FORMAT_Z_PIXMAP,
208                           dri2_surf->drawable, gc, w, this_lines, x, y_start, 0,
209                           dri2_surf->depth, this_lines * stride_b,
210                           ((const uint8_t *)data + y_start * stride_b));
211          xcb_discard_reply(dri2_dpy->conn, cookie.sequence);
212          y_start += this_lines;
213          y_todo -= this_lines;
214       }
215    }
216    xcb_flush(dri2_dpy->conn);
217 }
218 
219 static void
swrastPutImage(struct dri_drawable * draw,int op,int x,int y,int w,int h,char * data,void * loaderPrivate)220 swrastPutImage(struct dri_drawable *draw, int op, int x, int y, int w, int h,
221                char *data, void *loaderPrivate)
222 {
223    struct dri2_egl_surface *dri2_surf = loaderPrivate;
224    int stride_b = dri2_surf->bytes_per_pixel * w;
225    swrastPutImage2(draw, op, x, y, w, h, stride_b, data, loaderPrivate);
226 }
227 
228 static void
swrastGetImage2(struct dri_drawable * read,int x,int y,int w,int h,int stride,char * data,void * loaderPrivate)229 swrastGetImage2(struct dri_drawable * read,
230                 int x, int y, int w, int h, int stride,
231                 char *data, void *loaderPrivate)
232 {
233    struct dri2_egl_surface *dri2_surf = loaderPrivate;
234    struct dri2_egl_display *dri2_dpy =
235       dri2_egl_display(dri2_surf->base.Resource.Display);
236 
237    xcb_get_image_cookie_t cookie;
238    xcb_get_image_reply_t *reply;
239    xcb_generic_error_t *error;
240 
241    cookie = xcb_get_image(dri2_dpy->conn, XCB_IMAGE_FORMAT_Z_PIXMAP,
242                           dri2_surf->drawable, x, y, w, h, ~0);
243    reply = xcb_get_image_reply(dri2_dpy->conn, cookie, &error);
244    if (reply == NULL)
245       return;
246 
247    if (error != NULL) {
248       _eglLog(_EGL_WARNING, "error in xcb_get_image");
249       free(error);
250    } else {
251       uint32_t bytes = xcb_get_image_data_length(reply);
252       uint8_t *idata = xcb_get_image_data(reply);
253       int stride_b = w * dri2_surf->bytes_per_pixel;
254       /* Only copy line by line if we have a different stride */
255       if (stride != stride_b) {
256          for (int i = 0; i < h; i++) {
257             memcpy(data, idata, stride_b);
258             data += stride;
259             idata += stride_b;
260          }
261       } else {
262          memcpy(data, idata, bytes);
263       }
264    }
265    free(reply);
266 }
267 
268 static void
swrastGetImage(struct dri_drawable * read,int x,int y,int w,int h,char * data,void * loaderPrivate)269 swrastGetImage(struct dri_drawable *read, int x, int y, int w, int h, char *data,
270                void *loaderPrivate)
271 {
272    struct dri2_egl_surface *dri2_surf = loaderPrivate;
273    int stride_b = w * dri2_surf->bytes_per_pixel;
274    swrastGetImage2(read, x, y, w, h, stride_b, data, loaderPrivate);
275 }
276 
277 static void
swrastPutImageShm(struct dri_drawable * draw,int op,int x,int y,int w,int h,int stride,int shmid,char * shmaddr,unsigned offset,void * loaderPrivate)278 swrastPutImageShm(struct dri_drawable * draw, int op,
279                   int x, int y, int w, int h, int stride,
280                   int shmid, char *shmaddr, unsigned offset,
281                   void *loaderPrivate)
282 {
283    struct dri2_egl_surface *dri2_surf = loaderPrivate;
284    struct dri2_egl_display *dri2_dpy =
285       dri2_egl_display(dri2_surf->base.Resource.Display);
286    xcb_generic_error_t *error = NULL;
287 
288    xcb_shm_seg_t shm_seg = xcb_generate_id(dri2_dpy->conn);
289    error = xcb_request_check(dri2_dpy->conn,
290                              xcb_shm_attach_checked(dri2_dpy->conn,
291                                                     shm_seg, shmid, 0));
292    if (error) {
293       mesa_loge("Failed to attach to x11 shm");
294       _eglError(EGL_BAD_SURFACE, "xcb_shm_attach_checked");
295       free(error);
296       return;
297    }
298 
299    xcb_gcontext_t gc;
300    xcb_void_cookie_t cookie;
301    switch (op) {
302    case __DRI_SWRAST_IMAGE_OP_DRAW:
303       gc = dri2_surf->gc;
304       break;
305    case __DRI_SWRAST_IMAGE_OP_SWAP:
306       gc = dri2_surf->swapgc;
307       break;
308    default:
309       return;
310    }
311 
312    cookie = xcb_shm_put_image(dri2_dpy->conn,
313          dri2_surf->drawable,
314          gc,
315          stride / dri2_surf->bytes_per_pixel, h,
316          x, 0,
317          w, h,
318          x, y,
319          dri2_surf->depth,
320          XCB_IMAGE_FORMAT_Z_PIXMAP,
321          0, shm_seg, stride * y);
322    xcb_discard_reply(dri2_dpy->conn, cookie.sequence);
323 
324    xcb_flush(dri2_dpy->conn);
325    xcb_shm_detach(dri2_dpy->conn, shm_seg);
326 }
327 
328 static void
swrastGetImageShm(struct dri_drawable * read,int x,int y,int w,int h,int shmid,void * loaderPrivate)329 swrastGetImageShm(struct dri_drawable * read,
330                   int x, int y, int w, int h,
331                   int shmid, void *loaderPrivate)
332 {
333    struct dri2_egl_surface *dri2_surf = loaderPrivate;
334    struct dri2_egl_display *dri2_dpy =
335       dri2_egl_display(dri2_surf->base.Resource.Display);
336    xcb_generic_error_t *error = NULL;
337 
338    xcb_shm_seg_t shm_seg = xcb_generate_id(dri2_dpy->conn);
339    error = xcb_request_check(dri2_dpy->conn,
340                              xcb_shm_attach_checked(dri2_dpy->conn,
341                                                     shm_seg, shmid, 0));
342    if (error) {
343       mesa_loge("Failed to attach to x11 shm");
344       _eglError(EGL_BAD_SURFACE, "xcb_shm_attach_checked");
345       free(error);
346       return;
347    }
348 
349    xcb_shm_get_image_cookie_t cookie;
350    xcb_shm_get_image_reply_t *reply;
351 
352    cookie = xcb_shm_get_image(dri2_dpy->conn,
353          dri2_surf->drawable,
354          x, y,
355          w, h,
356          ~0, XCB_IMAGE_FORMAT_Z_PIXMAP,
357          shm_seg, 0);
358    reply = xcb_shm_get_image_reply(dri2_dpy->conn, cookie, NULL);
359    if (reply == NULL)
360       _eglLog(_EGL_WARNING, "error in xcb_shm_get_image");
361    else
362       free(reply);
363 
364    xcb_shm_detach(dri2_dpy->conn, shm_seg);
365 }
366 
367 static xcb_screen_t *
get_xcb_screen(xcb_screen_iterator_t iter,int screen)368 get_xcb_screen(xcb_screen_iterator_t iter, int screen)
369 {
370    for (; iter.rem; --screen, xcb_screen_next(&iter))
371       if (screen == 0)
372          return iter.data;
373 
374    return NULL;
375 }
376 
377 static xcb_visualtype_t *
get_xcb_visualtype_for_depth(struct dri2_egl_display * dri2_dpy,int depth)378 get_xcb_visualtype_for_depth(struct dri2_egl_display *dri2_dpy, int depth)
379 {
380    xcb_visualtype_iterator_t visual_iter;
381    xcb_screen_t *screen = dri2_dpy->screen;
382    xcb_depth_iterator_t depth_iter = xcb_screen_allowed_depths_iterator(screen);
383 
384    for (; depth_iter.rem; xcb_depth_next(&depth_iter)) {
385       if (depth_iter.data->depth != depth)
386          continue;
387 
388       visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
389       if (visual_iter.rem)
390          return visual_iter.data;
391    }
392 
393    return NULL;
394 }
395 
396 /* Get red channel mask for given depth. */
397 unsigned int
dri2_x11_get_red_mask_for_depth(struct dri2_egl_display * dri2_dpy,int depth)398 dri2_x11_get_red_mask_for_depth(struct dri2_egl_display *dri2_dpy, int depth)
399 {
400    xcb_visualtype_t *visual = get_xcb_visualtype_for_depth(dri2_dpy, depth);
401 
402    if (visual)
403       return visual->red_mask;
404 
405    return 0;
406 }
407 
408 /**
409  * Called via eglCreateWindowSurface(), drv->CreateWindowSurface().
410  */
411 static _EGLSurface *
dri2_x11_create_surface(_EGLDisplay * disp,EGLint type,_EGLConfig * conf,void * native_surface,const EGLint * attrib_list)412 dri2_x11_create_surface(_EGLDisplay *disp, EGLint type, _EGLConfig *conf,
413                         void *native_surface, const EGLint *attrib_list)
414 {
415    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
416    struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
417    struct dri2_egl_surface *dri2_surf;
418    xcb_get_geometry_cookie_t cookie;
419    xcb_get_geometry_reply_t *reply;
420    xcb_generic_error_t *error;
421    const struct dri_config *config;
422 
423    dri2_surf = calloc(1, sizeof *dri2_surf);
424    if (!dri2_surf) {
425       _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
426       return NULL;
427    }
428 
429    if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list,
430                           false, native_surface))
431       goto cleanup_surf;
432 
433    dri2_surf->region = XCB_NONE;
434    if (type == EGL_PBUFFER_BIT) {
435       dri2_surf->drawable = xcb_generate_id(dri2_dpy->conn);
436       xcb_create_pixmap(dri2_dpy->conn, conf->BufferSize, dri2_surf->drawable,
437                         dri2_dpy->screen->root, dri2_surf->base.Width,
438                         dri2_surf->base.Height);
439    } else {
440       STATIC_ASSERT(sizeof(uintptr_t) == sizeof(native_surface));
441       dri2_surf->drawable = (uintptr_t)native_surface;
442    }
443 
444    config = dri2_get_dri_config(dri2_conf, type, dri2_surf->base.GLColorspace);
445 
446    if (!config) {
447       _eglError(EGL_BAD_MATCH,
448                 "Unsupported surfacetype/colorspace configuration");
449       goto cleanup_pixmap;
450    }
451 
452    if (type != EGL_PBUFFER_BIT) {
453       cookie = xcb_get_geometry(dri2_dpy->conn, dri2_surf->drawable);
454       reply = xcb_get_geometry_reply(dri2_dpy->conn, cookie, &error);
455       if (error != NULL) {
456          if (error->error_code == BadAlloc)
457             _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
458          else if (type == EGL_WINDOW_BIT)
459             _eglError(EGL_BAD_NATIVE_WINDOW, "xcb_get_geometry");
460          else
461             _eglError(EGL_BAD_NATIVE_PIXMAP, "xcb_get_geometry");
462          free(error);
463          free(reply);
464          goto cleanup_dri_drawable;
465       } else if (reply == NULL) {
466          _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
467          goto cleanup_dri_drawable;
468       }
469 
470       dri2_surf->base.Width = reply->width;
471       dri2_surf->base.Height = reply->height;
472       dri2_surf->depth = reply->depth;
473       free(reply);
474    }
475 
476    if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf))
477       goto cleanup_pixmap;
478 
479 #ifdef HAVE_X11_DRI2
480    if (!dri2_dpy->swrast) {
481       xcb_void_cookie_t cookie;
482       int conn_error;
483 
484       cookie =
485          xcb_dri2_create_drawable_checked(dri2_dpy->conn, dri2_surf->drawable);
486       error = xcb_request_check(dri2_dpy->conn, cookie);
487       conn_error = xcb_connection_has_error(dri2_dpy->conn);
488       if (conn_error || error != NULL) {
489          if (type == EGL_PBUFFER_BIT || conn_error ||
490              error->error_code == BadAlloc)
491             _eglError(EGL_BAD_ALLOC, "xcb_dri2_create_drawable_checked");
492          else if (type == EGL_WINDOW_BIT)
493             _eglError(EGL_BAD_NATIVE_WINDOW,
494                       "xcb_dri2_create_drawable_checked");
495          else
496             _eglError(EGL_BAD_NATIVE_PIXMAP,
497                       "xcb_dri2_create_drawable_checked");
498          free(error);
499          goto cleanup_dri_drawable;
500       }
501    } else
502 #endif
503    {
504       if (type == EGL_PBUFFER_BIT) {
505          dri2_surf->depth = conf->BufferSize;
506       }
507       swrastCreateDrawable(dri2_dpy, dri2_surf);
508    }
509 
510    /* we always copy the back buffer to front */
511    dri2_surf->base.PostSubBufferSupportedNV = EGL_TRUE;
512 
513    return &dri2_surf->base;
514 
515 cleanup_dri_drawable:
516    driDestroyDrawable(dri2_surf->dri_drawable);
517 cleanup_pixmap:
518    if (type == EGL_PBUFFER_BIT)
519       xcb_free_pixmap(dri2_dpy->conn, dri2_surf->drawable);
520 cleanup_surf:
521    free(dri2_surf);
522 
523    return NULL;
524 }
525 
526 /**
527  * Called via eglCreateWindowSurface(), drv->CreateWindowSurface().
528  */
529 static _EGLSurface *
dri2_x11_create_window_surface(_EGLDisplay * disp,_EGLConfig * conf,void * native_window,const EGLint * attrib_list)530 dri2_x11_create_window_surface(_EGLDisplay *disp, _EGLConfig *conf,
531                                void *native_window, const EGLint *attrib_list)
532 {
533    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
534    _EGLSurface *surf;
535 
536    surf = dri2_x11_create_surface(disp, EGL_WINDOW_BIT, conf, native_window,
537                                   attrib_list);
538    if (surf != NULL) {
539       /* When we first create the DRI2 drawable, its swap interval on the
540        * server side is 1.
541        */
542       surf->SwapInterval = 1;
543 
544       /* Override that with a driconf-set value. */
545       dri2_x11_swap_interval(disp, surf, dri2_dpy->default_swap_interval);
546    }
547 
548    return surf;
549 }
550 
551 static _EGLSurface *
dri2_x11_create_pixmap_surface(_EGLDisplay * disp,_EGLConfig * conf,void * native_pixmap,const EGLint * attrib_list)552 dri2_x11_create_pixmap_surface(_EGLDisplay *disp, _EGLConfig *conf,
553                                void *native_pixmap, const EGLint *attrib_list)
554 {
555    return dri2_x11_create_surface(disp, EGL_PIXMAP_BIT, conf, native_pixmap,
556                                   attrib_list);
557 }
558 
559 static _EGLSurface *
dri2_x11_create_pbuffer_surface(_EGLDisplay * disp,_EGLConfig * conf,const EGLint * attrib_list)560 dri2_x11_create_pbuffer_surface(_EGLDisplay *disp, _EGLConfig *conf,
561                                 const EGLint *attrib_list)
562 {
563    return dri2_x11_create_surface(disp, EGL_PBUFFER_BIT, conf, NULL,
564                                   attrib_list);
565 }
566 
567 static EGLBoolean
dri2_x11_destroy_surface(_EGLDisplay * disp,_EGLSurface * surf)568 dri2_x11_destroy_surface(_EGLDisplay *disp, _EGLSurface *surf)
569 {
570    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
571    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
572 
573    driDestroyDrawable(dri2_surf->dri_drawable);
574 
575    if (!dri2_dpy->swrast) {
576 #ifdef HAVE_X11_DRI2
577       xcb_dri2_destroy_drawable(dri2_dpy->conn, dri2_surf->drawable);
578 #endif
579    } else {
580       swrastDestroyDrawable(dri2_dpy, dri2_surf);
581    }
582 
583    if (surf->Type == EGL_PBUFFER_BIT)
584       xcb_free_pixmap(dri2_dpy->conn, dri2_surf->drawable);
585 
586    dri2_fini_surface(surf);
587    free(surf);
588 
589    return EGL_TRUE;
590 }
591 
592 /**
593  * Function utilizes swrastGetDrawableInfo to get surface
594  * geometry from x server and calls default query surface
595  * implementation that returns the updated values.
596  *
597  * In case of errors we still return values that we currently
598  * have.
599  */
600 static EGLBoolean
dri2_query_surface(_EGLDisplay * disp,_EGLSurface * surf,EGLint attribute,EGLint * value)601 dri2_query_surface(_EGLDisplay *disp, _EGLSurface *surf, EGLint attribute,
602                    EGLint *value)
603 {
604    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
605    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
606    int x, y, w, h;
607 
608    struct dri_drawable *drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
609 
610    switch (attribute) {
611    case EGL_WIDTH:
612    case EGL_HEIGHT:
613       if (x11_get_drawable_info(drawable, &x, &y, &w, &h, dri2_surf)) {
614          bool changed = surf->Width != w || surf->Height != h;
615          surf->Width = w;
616          surf->Height = h;
617          if (changed && !dri2_dpy->swrast_not_kms)
618             dri_invalidate_drawable(drawable);
619       }
620       break;
621    default:
622       break;
623    }
624    return _eglQuerySurface(disp, surf, attribute, value);
625 }
626 
627 #ifdef HAVE_X11_DRI2
628 /**
629  * Process list of buffer received from the server
630  *
631  * Processes the list of buffers received in a reply from the server to either
632  * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
633  */
634 static void
dri2_x11_process_buffers(struct dri2_egl_surface * dri2_surf,xcb_dri2_dri2_buffer_t * buffers,unsigned count)635 dri2_x11_process_buffers(struct dri2_egl_surface *dri2_surf,
636                          xcb_dri2_dri2_buffer_t *buffers, unsigned count)
637 {
638    struct dri2_egl_display *dri2_dpy =
639       dri2_egl_display(dri2_surf->base.Resource.Display);
640    xcb_rectangle_t rectangle;
641 
642    dri2_surf->have_fake_front = false;
643 
644    /* This assumes the DRI2 buffer attachment tokens matches the
645     * __DRIbuffer tokens. */
646    for (unsigned i = 0; i < count; i++) {
647       dri2_surf->buffers[i].attachment = buffers[i].attachment;
648       dri2_surf->buffers[i].name = buffers[i].name;
649       dri2_surf->buffers[i].pitch = buffers[i].pitch;
650       dri2_surf->buffers[i].cpp = buffers[i].cpp;
651       dri2_surf->buffers[i].flags = buffers[i].flags;
652 
653       /* We only use the DRI drivers single buffer configs.  This
654        * means that if we try to render to a window, DRI2 will give us
655        * the fake front buffer, which we'll use as a back buffer.
656        * Note that EGL doesn't require that several clients rendering
657        * to the same window must see the same aux buffers. */
658       if (dri2_surf->buffers[i].attachment == __DRI_BUFFER_FAKE_FRONT_LEFT)
659          dri2_surf->have_fake_front = true;
660    }
661 
662    if (dri2_surf->region != XCB_NONE)
663       xcb_xfixes_destroy_region(dri2_dpy->conn, dri2_surf->region);
664 
665    rectangle.x = 0;
666    rectangle.y = 0;
667    rectangle.width = dri2_surf->base.Width;
668    rectangle.height = dri2_surf->base.Height;
669    dri2_surf->region = xcb_generate_id(dri2_dpy->conn);
670    xcb_xfixes_create_region(dri2_dpy->conn, dri2_surf->region, 1, &rectangle);
671 }
672 
673 static __DRIbuffer *
dri2_x11_get_buffers(struct dri_drawable * driDrawable,int * width,int * height,unsigned int * attachments,int count,int * out_count,void * loaderPrivate)674 dri2_x11_get_buffers(struct dri_drawable *driDrawable, int *width, int *height,
675                      unsigned int *attachments, int count, int *out_count,
676                      void *loaderPrivate)
677 {
678    struct dri2_egl_surface *dri2_surf = loaderPrivate;
679    struct dri2_egl_display *dri2_dpy =
680       dri2_egl_display(dri2_surf->base.Resource.Display);
681    xcb_dri2_dri2_buffer_t *buffers;
682    xcb_dri2_get_buffers_reply_t *reply;
683    xcb_dri2_get_buffers_cookie_t cookie;
684 
685    (void)driDrawable;
686 
687    cookie = xcb_dri2_get_buffers_unchecked(dri2_dpy->conn, dri2_surf->drawable,
688                                            count, count, attachments);
689    reply = xcb_dri2_get_buffers_reply(dri2_dpy->conn, cookie, NULL);
690    if (reply == NULL)
691       return NULL;
692    buffers = xcb_dri2_get_buffers_buffers(reply);
693    if (buffers == NULL) {
694       free(reply);
695       return NULL;
696    }
697 
698    *out_count = reply->count;
699    dri2_surf->base.Width = *width = reply->width;
700    dri2_surf->base.Height = *height = reply->height;
701    dri2_x11_process_buffers(dri2_surf, buffers, *out_count);
702 
703    free(reply);
704 
705    return dri2_surf->buffers;
706 }
707 
708 static __DRIbuffer *
dri2_x11_get_buffers_with_format(struct dri_drawable * driDrawable,int * width,int * height,unsigned int * attachments,int count,int * out_count,void * loaderPrivate)709 dri2_x11_get_buffers_with_format(struct dri_drawable *driDrawable, int *width,
710                                  int *height, unsigned int *attachments,
711                                  int count, int *out_count, void *loaderPrivate)
712 {
713    struct dri2_egl_surface *dri2_surf = loaderPrivate;
714    struct dri2_egl_display *dri2_dpy =
715       dri2_egl_display(dri2_surf->base.Resource.Display);
716    xcb_dri2_dri2_buffer_t *buffers;
717    xcb_dri2_get_buffers_with_format_reply_t *reply;
718    xcb_dri2_get_buffers_with_format_cookie_t cookie;
719    xcb_dri2_attach_format_t *format_attachments;
720 
721    (void)driDrawable;
722 
723    format_attachments = (xcb_dri2_attach_format_t *)attachments;
724    cookie = xcb_dri2_get_buffers_with_format_unchecked(
725       dri2_dpy->conn, dri2_surf->drawable, count, count, format_attachments);
726 
727    reply = xcb_dri2_get_buffers_with_format_reply(dri2_dpy->conn, cookie, NULL);
728    if (reply == NULL)
729       return NULL;
730 
731    buffers = xcb_dri2_get_buffers_with_format_buffers(reply);
732    dri2_surf->base.Width = *width = reply->width;
733    dri2_surf->base.Height = *height = reply->height;
734    *out_count = reply->count;
735    dri2_x11_process_buffers(dri2_surf, buffers, *out_count);
736 
737    free(reply);
738 
739    return dri2_surf->buffers;
740 }
741 
742 static void
dri2_x11_flush_front_buffer(struct dri_drawable * driDrawable,void * loaderPrivate)743 dri2_x11_flush_front_buffer(struct dri_drawable *driDrawable, void *loaderPrivate)
744 {
745    (void)driDrawable;
746 
747    /* FIXME: Does EGL support front buffer rendering at all? */
748 
749 #if 0
750    struct dri2_egl_surface *dri2_surf = loaderPrivate;
751 
752    dri2WaitGL(dri2_surf);
753 #else
754    (void)loaderPrivate;
755 #endif
756 }
757 
758 static int
dri2_x11_do_authenticate(struct dri2_egl_display * dri2_dpy,uint32_t id)759 dri2_x11_do_authenticate(struct dri2_egl_display *dri2_dpy, uint32_t id)
760 {
761    xcb_dri2_authenticate_reply_t *authenticate;
762    xcb_dri2_authenticate_cookie_t authenticate_cookie;
763    int ret = 0;
764 
765    authenticate_cookie = xcb_dri2_authenticate_unchecked(
766       dri2_dpy->conn, dri2_dpy->screen->root, id);
767    authenticate =
768       xcb_dri2_authenticate_reply(dri2_dpy->conn, authenticate_cookie, NULL);
769 
770    if (authenticate == NULL || !authenticate->authenticated)
771       ret = -1;
772 
773    free(authenticate);
774 
775    return ret;
776 }
777 
778 static EGLBoolean
dri2_x11_local_authenticate(struct dri2_egl_display * dri2_dpy)779 dri2_x11_local_authenticate(struct dri2_egl_display *dri2_dpy)
780 {
781 #ifdef HAVE_LIBDRM
782    drm_magic_t magic;
783 
784    if (drmGetMagic(dri2_dpy->fd_render_gpu, &magic)) {
785       _eglLog(_EGL_WARNING, "DRI2: failed to get drm magic");
786       return EGL_FALSE;
787    }
788 
789    if (dri2_x11_do_authenticate(dri2_dpy, magic) < 0) {
790       _eglLog(_EGL_WARNING, "DRI2: failed to authenticate");
791       return EGL_FALSE;
792    }
793 #endif
794    return EGL_TRUE;
795 }
796 
797 static EGLBoolean
dri2_x11_connect(struct dri2_egl_display * dri2_dpy)798 dri2_x11_connect(struct dri2_egl_display *dri2_dpy)
799 {
800    xcb_xfixes_query_version_reply_t *xfixes_query;
801    xcb_xfixes_query_version_cookie_t xfixes_query_cookie;
802    xcb_dri2_query_version_reply_t *dri2_query;
803    xcb_dri2_query_version_cookie_t dri2_query_cookie;
804    xcb_dri2_connect_reply_t *connect;
805    xcb_dri2_connect_cookie_t connect_cookie;
806    xcb_generic_error_t *error;
807    char *driver_name, *loader_driver_name, *device_name;
808    const xcb_query_extension_reply_t *extension;
809 
810    xcb_prefetch_extension_data(dri2_dpy->conn, &xcb_xfixes_id);
811    xcb_prefetch_extension_data(dri2_dpy->conn, &xcb_dri2_id);
812 
813    extension = xcb_get_extension_data(dri2_dpy->conn, &xcb_xfixes_id);
814    if (!(extension && extension->present))
815       return EGL_FALSE;
816 
817    extension = xcb_get_extension_data(dri2_dpy->conn, &xcb_dri2_id);
818    if (!(extension && extension->present))
819       return EGL_FALSE;
820 
821    xfixes_query_cookie = xcb_xfixes_query_version(
822       dri2_dpy->conn, XCB_XFIXES_MAJOR_VERSION, XCB_XFIXES_MINOR_VERSION);
823 
824    dri2_query_cookie = xcb_dri2_query_version(
825       dri2_dpy->conn, XCB_DRI2_MAJOR_VERSION, XCB_DRI2_MINOR_VERSION);
826 
827    connect_cookie = xcb_dri2_connect_unchecked(
828       dri2_dpy->conn, dri2_dpy->screen->root, XCB_DRI2_DRIVER_TYPE_DRI);
829 
830    xfixes_query = xcb_xfixes_query_version_reply(dri2_dpy->conn,
831                                                  xfixes_query_cookie, &error);
832    if (xfixes_query == NULL || error != NULL ||
833        xfixes_query->major_version < 2) {
834       _eglLog(_EGL_WARNING, "DRI2: failed to query xfixes version");
835       free(error);
836       free(xfixes_query);
837       return EGL_FALSE;
838    }
839    free(xfixes_query);
840 
841    dri2_query =
842       xcb_dri2_query_version_reply(dri2_dpy->conn, dri2_query_cookie, &error);
843    if (dri2_query == NULL || error != NULL) {
844       _eglLog(_EGL_WARNING, "DRI2: failed to query version");
845       free(error);
846       free(dri2_query);
847       return EGL_FALSE;
848    }
849    dri2_dpy->dri2_major = dri2_query->major_version;
850    dri2_dpy->dri2_minor = dri2_query->minor_version;
851    free(dri2_query);
852 
853    connect = xcb_dri2_connect_reply(dri2_dpy->conn, connect_cookie, NULL);
854    if (connect == NULL ||
855        connect->driver_name_length + connect->device_name_length == 0) {
856       _eglLog(_EGL_WARNING, "DRI2: failed to authenticate");
857       free(connect);
858       return EGL_FALSE;
859    }
860 
861    device_name = xcb_dri2_connect_device_name(connect);
862 
863    dri2_dpy->fd_render_gpu = loader_open_device(device_name);
864    if (dri2_dpy->fd_render_gpu == -1) {
865       _eglLog(_EGL_WARNING, "DRI2: could not open %s (%s)", device_name,
866               strerror(errno));
867       free(connect);
868       return EGL_FALSE;
869    }
870 
871    if (!dri2_x11_local_authenticate(dri2_dpy)) {
872       close(dri2_dpy->fd_render_gpu);
873       free(connect);
874       return EGL_FALSE;
875    }
876 
877    driver_name = xcb_dri2_connect_driver_name(connect);
878 
879    /* If Mesa knows about the appropriate driver for this fd, then trust it.
880     * Otherwise, default to the server's value.
881     */
882    loader_driver_name = loader_get_driver_for_fd(dri2_dpy->fd_render_gpu);
883    if (loader_driver_name) {
884       dri2_dpy->driver_name = loader_driver_name;
885    } else {
886       dri2_dpy->driver_name =
887          strndup(driver_name, xcb_dri2_connect_driver_name_length(connect));
888    }
889 
890    if (!strcmp(dri2_dpy->driver_name, "zink")) {
891       close(dri2_dpy->fd_render_gpu);
892       return EGL_FALSE;
893    }
894 
895    if (dri2_dpy->driver_name == NULL) {
896       close(dri2_dpy->fd_render_gpu);
897       free(connect);
898       return EGL_FALSE;
899    }
900 
901 #ifdef HAVE_WAYLAND_PLATFORM
902    dri2_dpy->device_name =
903       strndup(device_name, xcb_dri2_connect_device_name_length(connect));
904 #endif
905 
906    free(connect);
907 
908    return EGL_TRUE;
909 }
910 
911 static int
dri2_x11_authenticate(_EGLDisplay * disp,uint32_t id)912 dri2_x11_authenticate(_EGLDisplay *disp, uint32_t id)
913 {
914    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
915 
916    return dri2_x11_do_authenticate(dri2_dpy, id);
917 }
918 #endif
919 
920 static void
dri2_x11_add_configs_for_visuals(struct dri2_egl_display * dri2_dpy,_EGLDisplay * disp,bool supports_preserved)921 dri2_x11_add_configs_for_visuals(struct dri2_egl_display *dri2_dpy,
922                                  _EGLDisplay *disp, bool supports_preserved)
923 {
924    xcb_depth_iterator_t d;
925    xcb_visualtype_t *visuals;
926    EGLint surface_type;
927 
928    d = xcb_screen_allowed_depths_iterator(dri2_dpy->screen);
929 
930    surface_type = EGL_WINDOW_BIT | EGL_PIXMAP_BIT | EGL_PBUFFER_BIT;
931 
932    if (supports_preserved)
933       surface_type |= EGL_SWAP_BEHAVIOR_PRESERVED_BIT;
934 
935    while (d.rem > 0) {
936       EGLBoolean class_added[6] = {
937          0,
938       };
939 
940       visuals = xcb_depth_visuals(d.data);
941 
942       for (int i = 0; i < xcb_depth_visuals_length(d.data); i++) {
943          if (class_added[visuals[i]._class])
944             continue;
945 
946          class_added[visuals[i]._class] = EGL_TRUE;
947 
948          const int rgb_shifts[3] = {
949             ffs(visuals[i].red_mask) - 1,
950             ffs(visuals[i].green_mask) - 1,
951             ffs(visuals[i].blue_mask) - 1,
952          };
953 
954          const unsigned int rgb_sizes[3] = {
955             util_bitcount(visuals[i].red_mask),
956             util_bitcount(visuals[i].green_mask),
957             util_bitcount(visuals[i].blue_mask),
958          };
959 
960          const EGLint config_attrs[] = {
961             EGL_NATIVE_VISUAL_ID,
962             visuals[i].visual_id,
963             EGL_NATIVE_VISUAL_TYPE,
964             visuals[i]._class,
965             EGL_NONE,
966          };
967 
968          const EGLint config_attrs_2nd_group[] = {
969             EGL_NATIVE_VISUAL_ID,
970             visuals[i].visual_id,
971             EGL_NATIVE_VISUAL_TYPE,
972             visuals[i]._class,
973             EGL_CONFIG_SELECT_GROUP_EXT,
974             1,
975             EGL_NONE,
976          };
977 
978          for (int j = 0; dri2_dpy->driver_configs[j]; j++) {
979             const struct dri_config *config = dri2_dpy->driver_configs[j];
980             int shifts[4];
981             unsigned int sizes[4];
982 
983             dri2_get_shifts_and_sizes(config, shifts, sizes);
984 
985             if (memcmp(shifts, rgb_shifts, sizeof(rgb_shifts)) != 0 ||
986                 memcmp(sizes, rgb_sizes, sizeof(rgb_sizes)) != 0) {
987                continue;
988             }
989 
990             /* Allows RGB visuals to match a 32-bit RGBA EGLConfig.
991              * Otherwise it will only match a 32-bit RGBA visual.  On a
992              * composited window manager on X11, this will make all of the
993              * EGLConfigs with destination alpha get blended by the
994              * compositor.  This is probably not what the application
995              * wants... especially on drivers that only have 32-bit RGBA
996              * EGLConfigs! */
997             if (sizes[3] != 0) {
998                unsigned int rgba_mask =
999                   ~(visuals[i].red_mask | visuals[i].green_mask |
1000                     visuals[i].blue_mask);
1001 
1002                if (shifts[3] != ffs(rgba_mask) - 1 ||
1003                    sizes[3] != util_bitcount(rgba_mask))
1004                   continue;
1005             }
1006 
1007             unsigned int bit_per_pixel = sizes[0] + sizes[1] + sizes[2] + sizes[3];
1008             if (sizes[3] != 0 && d.data->depth == bit_per_pixel) {
1009                dri2_add_config(disp, config, surface_type, config_attrs_2nd_group);
1010             } else {
1011                dri2_add_config(disp, config, surface_type, config_attrs);
1012             }
1013          }
1014       }
1015 
1016       xcb_depth_next(&d);
1017    }
1018 }
1019 
1020 #ifdef HAVE_X11_DRI2
1021 static EGLBoolean
dri2_copy_region(_EGLDisplay * disp,_EGLSurface * draw,xcb_xfixes_region_t region)1022 dri2_copy_region(_EGLDisplay *disp, _EGLSurface *draw,
1023                  xcb_xfixes_region_t region)
1024 {
1025    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1026    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
1027    enum xcb_dri2_attachment_t render_attachment;
1028    xcb_dri2_copy_region_cookie_t cookie;
1029 
1030    /* No-op for a pixmap or pbuffer surface */
1031    if (draw->Type == EGL_PIXMAP_BIT || draw->Type == EGL_PBUFFER_BIT)
1032       return EGL_TRUE;
1033 
1034    assert(!dri2_dpy->kopper);
1035    dri_flush_drawable(dri2_surf->dri_drawable);
1036 
1037    if (dri2_surf->have_fake_front)
1038       render_attachment = XCB_DRI2_ATTACHMENT_BUFFER_FAKE_FRONT_LEFT;
1039    else
1040       render_attachment = XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT;
1041 
1042    cookie = xcb_dri2_copy_region_unchecked(
1043       dri2_dpy->conn, dri2_surf->drawable, region,
1044       XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT, render_attachment);
1045    free(xcb_dri2_copy_region_reply(dri2_dpy->conn, cookie, NULL));
1046 
1047    return EGL_TRUE;
1048 }
1049 
1050 static int64_t
dri2_x11_swap_buffers_msc(_EGLDisplay * disp,_EGLSurface * draw,int64_t msc,int64_t divisor,int64_t remainder)1051 dri2_x11_swap_buffers_msc(_EGLDisplay *disp, _EGLSurface *draw, int64_t msc,
1052                           int64_t divisor, int64_t remainder)
1053 {
1054    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1055    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
1056    uint32_t msc_hi = msc >> 32;
1057    uint32_t msc_lo = msc & 0xffffffff;
1058    uint32_t divisor_hi = divisor >> 32;
1059    uint32_t divisor_lo = divisor & 0xffffffff;
1060    uint32_t remainder_hi = remainder >> 32;
1061    uint32_t remainder_lo = remainder & 0xffffffff;
1062    xcb_dri2_swap_buffers_cookie_t cookie;
1063    xcb_dri2_swap_buffers_reply_t *reply;
1064    int64_t swap_count = -1;
1065 
1066    if (draw->SwapBehavior == EGL_BUFFER_PRESERVED ||
1067        !dri2_dpy->swap_available) {
1068       swap_count = dri2_copy_region(disp, draw, dri2_surf->region) ? 0 : -1;
1069    } else {
1070       dri2_flush_drawable_for_swapbuffers(disp, draw);
1071 
1072       cookie = xcb_dri2_swap_buffers_unchecked(
1073          dri2_dpy->conn, dri2_surf->drawable, msc_hi, msc_lo, divisor_hi,
1074          divisor_lo, remainder_hi, remainder_lo);
1075 
1076       reply = xcb_dri2_swap_buffers_reply(dri2_dpy->conn, cookie, NULL);
1077 
1078       if (reply) {
1079          swap_count = combine_u32_into_u64(reply->swap_hi, reply->swap_lo);
1080          free(reply);
1081       }
1082    }
1083 
1084    /* Since we aren't watching for the server's invalidate events like we're
1085     * supposed to (due to XCB providing no mechanism for filtering the events
1086     * the way xlib does), and SwapBuffers is a common cause of invalidate
1087     * events, just shove one down to the driver, even though we haven't told
1088     * the driver that we're the kind of loader that provides reliable
1089     * invalidate events.  This causes the driver to request buffers again at
1090     * its next draw, so that we get the correct buffers if a pageflip
1091     * happened.  The driver should still be using the viewport hack to catch
1092     * window resizes.
1093     */
1094    dri_invalidate_drawable(dri2_surf->dri_drawable);
1095 
1096    return swap_count;
1097 }
1098 #endif
1099 
1100 static EGLBoolean
dri2_x11_swap_buffers(_EGLDisplay * disp,_EGLSurface * draw)1101 dri2_x11_swap_buffers(_EGLDisplay *disp, _EGLSurface *draw)
1102 {
1103    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1104    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
1105 
1106    if (dri2_dpy->kopper) {
1107       /* From the EGL 1.4 spec (page 52):
1108        *
1109        *     "The contents of ancillary buffers are always undefined
1110        *      after calling eglSwapBuffers."
1111        */
1112       kopperSwapBuffers(dri2_surf->dri_drawable,
1113                                     __DRI2_FLUSH_INVALIDATE_ANCILLARY);
1114       return EGL_TRUE;
1115    } else if (dri2_dpy->swrast) {
1116       /* aka the swrast path, which does the swap in the gallium driver. */
1117       driSwapBuffers(dri2_surf->dri_drawable);
1118       return EGL_TRUE;
1119    }
1120 
1121 #ifdef HAVE_X11_DRI2
1122    if (dri2_x11_swap_buffers_msc(disp, draw, 0, 0, 0) == -1) {
1123       /* Swap failed with a window drawable. */
1124       return _eglError(EGL_BAD_NATIVE_WINDOW, __func__);
1125    }
1126 #endif
1127    return EGL_TRUE;
1128 }
1129 
1130 #ifdef HAVE_X11_DRI2
1131 static EGLBoolean
dri2_x11_swap_buffers_region(_EGLDisplay * disp,_EGLSurface * draw,EGLint numRects,const EGLint * rects)1132 dri2_x11_swap_buffers_region(_EGLDisplay *disp, _EGLSurface *draw,
1133                              EGLint numRects, const EGLint *rects)
1134 {
1135    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1136    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
1137    EGLBoolean ret;
1138    xcb_xfixes_region_t region;
1139    xcb_rectangle_t rectangles[16];
1140 
1141    if (numRects > (int)ARRAY_SIZE(rectangles))
1142       return dri2_copy_region(disp, draw, dri2_surf->region);
1143 
1144    for (int i = 0; i < numRects; i++) {
1145       rectangles[i].x = rects[i * 4];
1146       rectangles[i].y =
1147          dri2_surf->base.Height - rects[i * 4 + 1] - rects[i * 4 + 3];
1148       rectangles[i].width = rects[i * 4 + 2];
1149       rectangles[i].height = rects[i * 4 + 3];
1150    }
1151 
1152    region = xcb_generate_id(dri2_dpy->conn);
1153    xcb_xfixes_create_region(dri2_dpy->conn, region, numRects, rectangles);
1154    ret = dri2_copy_region(disp, draw, region);
1155    xcb_xfixes_destroy_region(dri2_dpy->conn, region);
1156 
1157    return ret;
1158 }
1159 
1160 static EGLBoolean
dri2_x11_post_sub_buffer(_EGLDisplay * disp,_EGLSurface * draw,EGLint x,EGLint y,EGLint width,EGLint height)1161 dri2_x11_post_sub_buffer(_EGLDisplay *disp, _EGLSurface *draw, EGLint x,
1162                          EGLint y, EGLint width, EGLint height)
1163 {
1164    const EGLint rect[4] = {x, y, width, height};
1165 
1166    if (x < 0 || y < 0 || width < 0 || height < 0)
1167       _eglError(EGL_BAD_PARAMETER, "eglPostSubBufferNV");
1168 
1169    return dri2_x11_swap_buffers_region(disp, draw, 1, rect);
1170 }
1171 #else
1172 #define dri2_x11_swap_buffers_region NULL
1173 #define dri2_x11_post_sub_buffer NULL
1174 #endif
1175 
1176 static EGLBoolean
dri2_x11_kopper_swap_buffers_with_damage(_EGLDisplay * disp,_EGLSurface * draw,const EGLint * rects,EGLint numRects)1177 dri2_x11_kopper_swap_buffers_with_damage(_EGLDisplay *disp, _EGLSurface *draw,
1178                                          const EGLint *rects, EGLint numRects)
1179 {
1180    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1181    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
1182    /* swrast path unsupported for now */
1183    assert(dri2_dpy->kopper);
1184    if (numRects) {
1185       if (dri2_dpy->kopper)
1186          kopperSwapBuffersWithDamage(dri2_surf->dri_drawable, __DRI2_FLUSH_INVALIDATE_ANCILLARY, numRects, rects);
1187       else
1188          driSwapBuffersWithDamage(dri2_surf->dri_drawable, numRects, rects);
1189    } else {
1190       if (dri2_dpy->kopper)
1191          kopperSwapBuffers(dri2_surf->dri_drawable, __DRI2_FLUSH_INVALIDATE_ANCILLARY);
1192       else
1193          driSwapBuffers(dri2_surf->dri_drawable);
1194    }
1195    return EGL_TRUE;
1196 }
1197 
1198 static EGLBoolean
dri2_x11_swap_buffers_with_damage(_EGLDisplay * disp,_EGLSurface * draw,const EGLint * rects,EGLint numRects)1199 dri2_x11_swap_buffers_with_damage(_EGLDisplay *disp, _EGLSurface *draw,
1200                                   const EGLint *rects, EGLint numRects)
1201 {
1202    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
1203    if (numRects)
1204       driSwapBuffersWithDamage(dri2_surf->dri_drawable, numRects, rects);
1205    else
1206       driSwapBuffers(dri2_surf->dri_drawable);
1207    return EGL_TRUE;
1208 }
1209 
1210 static EGLBoolean
dri2_x11_swap_interval(_EGLDisplay * disp,_EGLSurface * surf,EGLint interval)1211 dri2_x11_swap_interval(_EGLDisplay *disp, _EGLSurface *surf, EGLint interval)
1212 {
1213    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1214    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1215 
1216    if (dri2_dpy->kopper) {
1217       kopperSetSwapInterval(dri2_surf->dri_drawable, interval);
1218       return EGL_TRUE;
1219    }
1220 
1221 #ifdef HAVE_X11_DRI2
1222    if (dri2_dpy->swap_available)
1223       xcb_dri2_swap_interval(dri2_dpy->conn, dri2_surf->drawable, interval);
1224 #endif
1225 
1226    return EGL_TRUE;
1227 }
1228 
1229 static EGLBoolean
dri2_x11_copy_buffers(_EGLDisplay * disp,_EGLSurface * surf,void * native_pixmap_target)1230 dri2_x11_copy_buffers(_EGLDisplay *disp, _EGLSurface *surf,
1231                       void *native_pixmap_target)
1232 {
1233    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1234    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1235    xcb_gcontext_t gc;
1236    xcb_pixmap_t target;
1237 
1238    STATIC_ASSERT(sizeof(uintptr_t) == sizeof(native_pixmap_target));
1239    target = (uintptr_t)native_pixmap_target;
1240 
1241    if (!dri2_dpy->swrast_not_kms)
1242       dri_flush_drawable(dri2_surf->dri_drawable);
1243    else {
1244       /* This should not be a swapBuffers, because it could present an
1245        * incomplete frame, and it could invalidate the back buffer if it's not
1246        * preserved.  We really do want to flush.  But it ends up working out
1247        * okay-ish on swrast because those aren't invalidating the back buffer on
1248        * swap.
1249        */
1250       driSwapBuffers(dri2_surf->dri_drawable);
1251    }
1252 
1253    gc = xcb_generate_id(dri2_dpy->conn);
1254    xcb_create_gc(dri2_dpy->conn, gc, target, 0, NULL);
1255    xcb_copy_area(dri2_dpy->conn, dri2_surf->drawable, target, gc, 0, 0, 0, 0,
1256                  dri2_surf->base.Width, dri2_surf->base.Height);
1257    xcb_free_gc(dri2_dpy->conn, gc);
1258 
1259    return EGL_TRUE;
1260 }
1261 
1262 uint32_t
dri2_fourcc_for_depth(struct dri2_egl_display * dri2_dpy,uint32_t depth)1263 dri2_fourcc_for_depth(struct dri2_egl_display *dri2_dpy, uint32_t depth)
1264 {
1265    switch (depth) {
1266    case 16:
1267       return DRM_FORMAT_RGB565;
1268    case 24:
1269       return DRM_FORMAT_XRGB8888;
1270    case 30:
1271       /* Different preferred formats for different hw */
1272       if (dri2_x11_get_red_mask_for_depth(dri2_dpy, 30) == 0x3ff)
1273          return DRM_FORMAT_XBGR2101010;
1274       else
1275          return DRM_FORMAT_XRGB2101010;
1276    case 32:
1277       return DRM_FORMAT_ARGB8888;
1278    default:
1279       return DRM_FORMAT_INVALID;
1280    }
1281 }
1282 
1283 #ifdef HAVE_X11_DRI2
1284 static _EGLImage *
dri2_create_image_khr_pixmap(_EGLDisplay * disp,_EGLContext * ctx,EGLClientBuffer buffer,const EGLint * attr_list)1285 dri2_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx,
1286                              EGLClientBuffer buffer, const EGLint *attr_list)
1287 {
1288    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1289    struct dri2_egl_image *dri2_img;
1290    unsigned int attachments[1];
1291    xcb_drawable_t drawable;
1292    xcb_dri2_get_buffers_cookie_t buffers_cookie;
1293    xcb_dri2_get_buffers_reply_t *buffers_reply;
1294    xcb_dri2_dri2_buffer_t *buffers;
1295    xcb_get_geometry_cookie_t geometry_cookie;
1296    xcb_get_geometry_reply_t *geometry_reply;
1297    xcb_generic_error_t *error;
1298    int fourcc;
1299 
1300    (void)ctx;
1301 
1302    drawable = (xcb_drawable_t)(uintptr_t)buffer;
1303    xcb_dri2_create_drawable(dri2_dpy->conn, drawable);
1304    attachments[0] = XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT;
1305    buffers_cookie = xcb_dri2_get_buffers_unchecked(dri2_dpy->conn, drawable, 1,
1306                                                    1, attachments);
1307    geometry_cookie = xcb_get_geometry(dri2_dpy->conn, drawable);
1308    buffers_reply =
1309       xcb_dri2_get_buffers_reply(dri2_dpy->conn, buffers_cookie, NULL);
1310    if (buffers_reply == NULL)
1311       return NULL;
1312 
1313    buffers = xcb_dri2_get_buffers_buffers(buffers_reply);
1314    if (buffers == NULL) {
1315       free(buffers_reply);
1316       return NULL;
1317    }
1318 
1319    geometry_reply =
1320       xcb_get_geometry_reply(dri2_dpy->conn, geometry_cookie, &error);
1321    if (geometry_reply == NULL || error != NULL) {
1322       _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
1323       free(error);
1324       free(buffers_reply);
1325       free(geometry_reply);
1326       return NULL;
1327    }
1328 
1329    fourcc = dri2_fourcc_for_depth(dri2_dpy, geometry_reply->depth);
1330    if (fourcc == DRM_FORMAT_INVALID) {
1331       _eglError(EGL_BAD_PARAMETER,
1332                 "dri2_create_image_khr: unsupported pixmap depth");
1333       free(buffers_reply);
1334       free(geometry_reply);
1335       return NULL;
1336    }
1337 
1338    dri2_img = malloc(sizeof *dri2_img);
1339    if (!dri2_img) {
1340       free(buffers_reply);
1341       free(geometry_reply);
1342       _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
1343       return EGL_NO_IMAGE_KHR;
1344    }
1345 
1346    _eglInitImage(&dri2_img->base, disp);
1347 
1348    int offset = 0;
1349    dri2_img->dri_image = dri2_from_names(
1350       dri2_dpy->dri_screen_render_gpu, buffers_reply->width,
1351       buffers_reply->height, fourcc, (int *) &buffers[0].name, 1,
1352       (int *) &buffers[0].pitch, &offset, dri2_img);
1353 
1354    free(buffers_reply);
1355    free(geometry_reply);
1356 
1357    return &dri2_img->base;
1358 }
1359 
1360 static _EGLImage *
dri2_x11_create_image_khr(_EGLDisplay * disp,_EGLContext * ctx,EGLenum target,EGLClientBuffer buffer,const EGLint * attr_list)1361 dri2_x11_create_image_khr(_EGLDisplay *disp, _EGLContext *ctx, EGLenum target,
1362                           EGLClientBuffer buffer, const EGLint *attr_list)
1363 {
1364    switch (target) {
1365    case EGL_NATIVE_PIXMAP_KHR:
1366       return dri2_create_image_khr_pixmap(disp, ctx, buffer, attr_list);
1367    default:
1368       return dri2_create_image_khr(disp, ctx, target, buffer, attr_list);
1369    }
1370 }
1371 
1372 static EGLBoolean
dri2_x11_get_sync_values(_EGLDisplay * display,_EGLSurface * surface,EGLuint64KHR * ust,EGLuint64KHR * msc,EGLuint64KHR * sbc)1373 dri2_x11_get_sync_values(_EGLDisplay *display, _EGLSurface *surface,
1374                          EGLuint64KHR *ust, EGLuint64KHR *msc,
1375                          EGLuint64KHR *sbc)
1376 {
1377    struct dri2_egl_display *dri2_dpy = dri2_egl_display(display);
1378    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
1379    xcb_dri2_get_msc_cookie_t cookie;
1380    xcb_dri2_get_msc_reply_t *reply;
1381 
1382    cookie = xcb_dri2_get_msc(dri2_dpy->conn, dri2_surf->drawable);
1383    reply = xcb_dri2_get_msc_reply(dri2_dpy->conn, cookie, NULL);
1384 
1385    if (!reply)
1386       return _eglError(EGL_BAD_ACCESS, __func__);
1387 
1388    *ust = ((EGLuint64KHR)reply->ust_hi << 32) | reply->ust_lo;
1389    *msc = ((EGLuint64KHR)reply->msc_hi << 32) | reply->msc_lo;
1390    *sbc = ((EGLuint64KHR)reply->sbc_hi << 32) | reply->sbc_lo;
1391    free(reply);
1392 
1393    return EGL_TRUE;
1394 }
1395 #endif
1396 
1397 static int
box_intersection_area(int16_t a_x,int16_t a_y,int16_t a_width,int16_t a_height,int16_t b_x,int16_t b_y,int16_t b_width,int16_t b_height)1398 box_intersection_area(int16_t a_x, int16_t a_y, int16_t a_width,
1399                       int16_t a_height, int16_t b_x, int16_t b_y,
1400                       int16_t b_width, int16_t b_height)
1401 {
1402    int w = MIN2(a_x + a_width, b_x + b_width) - MAX2(a_x, b_x);
1403    int h = MIN2(a_y + a_height, b_y + b_height) - MAX2(a_y, b_y);
1404 
1405    return (w < 0 || h < 0) ? 0 : w * h;
1406 }
1407 
1408 EGLBoolean
dri2_x11_get_msc_rate(_EGLDisplay * display,_EGLSurface * surface,EGLint * numerator,EGLint * denominator)1409 dri2_x11_get_msc_rate(_EGLDisplay *display, _EGLSurface *surface,
1410                       EGLint *numerator, EGLint *denominator)
1411 {
1412    struct dri2_egl_display *dri2_dpy = dri2_egl_display(display);
1413 
1414 #ifdef HAVE_LIBDRM
1415    loader_update_screen_resources(&dri2_dpy->screen_resources);
1416 
1417    if (dri2_dpy->screen_resources.num_crtcs == 0) {
1418       /* If there's no CRTC active, use the present fake vblank of 1Hz */
1419       *numerator = 1;
1420       *denominator = 1;
1421       return EGL_TRUE;
1422    }
1423 
1424    /* Default to the first CRTC in the list */
1425    *numerator = dri2_dpy->screen_resources.crtcs[0].refresh_numerator;
1426    *denominator = dri2_dpy->screen_resources.crtcs[0].refresh_denominator;
1427 
1428    /* If there's only one active CRTC, we're done */
1429    if (dri2_dpy->screen_resources.num_crtcs == 1)
1430       return EGL_TRUE;
1431 #else
1432    *numerator = 0;
1433    *denominator = 1;
1434 #endif
1435 
1436 
1437    /* In a multi-monitor setup, look at each CRTC and perform a box
1438     * intersection between the CRTC and surface.  Use the CRTC whose
1439     * box intersection has the largest area.
1440     */
1441    if (surface->Type != EGL_WINDOW_BIT)
1442       return EGL_TRUE;
1443 
1444    xcb_window_t window = (uintptr_t)surface->NativeSurface;
1445 
1446    xcb_translate_coordinates_cookie_t cookie =
1447       xcb_translate_coordinates_unchecked(dri2_dpy->conn, window,
1448                                           dri2_dpy->screen->root, 0, 0);
1449    xcb_translate_coordinates_reply_t *reply =
1450       xcb_translate_coordinates_reply(dri2_dpy->conn, cookie, NULL);
1451 
1452    if (!reply) {
1453       _eglError(EGL_BAD_SURFACE,
1454                 "eglGetMscRateANGLE failed to translate coordinates");
1455       return EGL_FALSE;
1456    }
1457 
1458 #ifdef HAVE_LIBDRM
1459    int area = 0;
1460 
1461    for (unsigned c = 0; c < dri2_dpy->screen_resources.num_crtcs; c++) {
1462       struct loader_crtc_info *crtc = &dri2_dpy->screen_resources.crtcs[c];
1463 
1464       int c_area = box_intersection_area(
1465          reply->dst_x, reply->dst_y, surface->Width, surface->Height, crtc->x,
1466          crtc->y, crtc->width, crtc->height);
1467       if (c_area > area) {
1468          *numerator = crtc->refresh_numerator;
1469          *denominator = crtc->refresh_denominator;
1470          area = c_area;
1471       }
1472    }
1473 #endif
1474    /* If the window is entirely off-screen, then area will still be 0.
1475     * We defaulted to the first CRTC in the list's refresh rate, earlier.
1476     */
1477 
1478    return EGL_TRUE;
1479 }
1480 
1481 static EGLBoolean
dri2_kopper_swap_interval(_EGLDisplay * disp,_EGLSurface * surf,EGLint interval)1482 dri2_kopper_swap_interval(_EGLDisplay *disp, _EGLSurface *surf, EGLint interval)
1483 {
1484    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1485    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1486 
1487    /* This can legitimately be null for lavapipe */
1488    if (dri2_dpy->kopper)
1489       kopperSetSwapInterval(dri2_surf->dri_drawable, interval);
1490 
1491    return EGL_TRUE;
1492 }
1493 
1494 static _EGLSurface *
dri2_kopper_create_window_surface(_EGLDisplay * disp,_EGLConfig * conf,void * native_window,const EGLint * attrib_list)1495 dri2_kopper_create_window_surface(_EGLDisplay *disp, _EGLConfig *conf,
1496                                   void *native_window,
1497                                   const EGLint *attrib_list)
1498 {
1499    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1500    _EGLSurface *surf;
1501 
1502    surf = dri2_x11_create_surface(disp, EGL_WINDOW_BIT, conf, native_window,
1503                                   attrib_list);
1504    if (surf != NULL) {
1505       /* When we first create the DRI2 drawable, its swap interval on the
1506        * server side is 1.
1507        */
1508       surf->SwapInterval = 1;
1509 
1510       /* Override that with a driconf-set value. */
1511       dri2_kopper_swap_interval(disp, surf, dri2_dpy->default_swap_interval);
1512    }
1513 
1514    return surf;
1515 }
1516 
1517 static EGLint
dri2_kopper_query_buffer_age(_EGLDisplay * disp,_EGLSurface * surf)1518 dri2_kopper_query_buffer_age(_EGLDisplay *disp, _EGLSurface *surf)
1519 {
1520    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1521    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1522 
1523    /* This can legitimately be null for lavapipe */
1524    if (dri2_dpy->kopper)
1525       return kopperQueryBufferAge(dri2_surf->dri_drawable);
1526    else
1527       return driSWRastQueryBufferAge(dri2_surf->dri_drawable);
1528 
1529    return 0;
1530 }
1531 
1532 static EGLint
dri2_swrast_query_buffer_age(_EGLDisplay * disp,_EGLSurface * surf)1533 dri2_swrast_query_buffer_age(_EGLDisplay *disp, _EGLSurface *surf)
1534 {
1535    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1536 
1537    return driSWRastQueryBufferAge(dri2_surf->dri_drawable);
1538 }
1539 
1540 static const struct dri2_egl_display_vtbl dri2_x11_swrast_display_vtbl = {
1541    .authenticate = NULL,
1542    .create_window_surface = dri2_x11_create_window_surface,
1543    .create_pixmap_surface = dri2_x11_create_pixmap_surface,
1544    .create_pbuffer_surface = dri2_x11_create_pbuffer_surface,
1545    .destroy_surface = dri2_x11_destroy_surface,
1546    .create_image = dri2_create_image_khr,
1547    .swap_buffers = dri2_x11_swap_buffers,
1548    .swap_buffers_region = dri2_x11_swap_buffers_region,
1549    .swap_buffers_with_damage = dri2_x11_swap_buffers_with_damage,
1550    .post_sub_buffer = dri2_x11_post_sub_buffer,
1551    .copy_buffers = dri2_x11_copy_buffers,
1552    .query_buffer_age = dri2_swrast_query_buffer_age,
1553    /* XXX: should really implement this since X11 has pixmaps */
1554    .query_surface = dri2_query_surface,
1555    .get_msc_rate = dri2_x11_get_msc_rate,
1556    .get_dri_drawable = dri2_surface_get_dri_drawable,
1557 };
1558 
1559 static const struct dri2_egl_display_vtbl dri2_x11_kopper_display_vtbl = {
1560    .authenticate = NULL,
1561    .create_window_surface = dri2_kopper_create_window_surface,
1562    .create_pixmap_surface = dri2_x11_create_pixmap_surface,
1563    .create_pbuffer_surface = dri2_x11_create_pbuffer_surface,
1564    .destroy_surface = dri2_x11_destroy_surface,
1565    .create_image = dri2_create_image_khr,
1566    .swap_interval = dri2_kopper_swap_interval,
1567    .swap_buffers = dri2_x11_swap_buffers,
1568    .swap_buffers_region = dri2_x11_swap_buffers_region,
1569    .swap_buffers_with_damage = dri2_x11_kopper_swap_buffers_with_damage,
1570    .post_sub_buffer = dri2_x11_post_sub_buffer,
1571    .copy_buffers = dri2_x11_copy_buffers,
1572    .query_buffer_age = dri2_kopper_query_buffer_age,
1573    /* XXX: should really implement this since X11 has pixmaps */
1574    .query_surface = dri2_query_surface,
1575    .get_msc_rate = dri2_x11_get_msc_rate,
1576    .get_dri_drawable = dri2_surface_get_dri_drawable,
1577 };
1578 
1579 #ifdef HAVE_X11_DRI2
1580 static const struct dri2_egl_display_vtbl dri2_x11_display_vtbl = {
1581    .authenticate = dri2_x11_authenticate,
1582    .create_window_surface = dri2_x11_create_window_surface,
1583    .create_pixmap_surface = dri2_x11_create_pixmap_surface,
1584    .create_pbuffer_surface = dri2_x11_create_pbuffer_surface,
1585    .destroy_surface = dri2_x11_destroy_surface,
1586    .create_image = dri2_x11_create_image_khr,
1587    .swap_interval = dri2_x11_swap_interval,
1588    .swap_buffers = dri2_x11_swap_buffers,
1589    .swap_buffers_region = dri2_x11_swap_buffers_region,
1590    .post_sub_buffer = dri2_x11_post_sub_buffer,
1591    .copy_buffers = dri2_x11_copy_buffers,
1592    .query_surface = dri2_query_surface,
1593    .get_sync_values = dri2_x11_get_sync_values,
1594    .get_msc_rate = dri2_x11_get_msc_rate,
1595    .get_dri_drawable = dri2_surface_get_dri_drawable,
1596 };
1597 #endif
1598 
1599 static const __DRIswrastLoaderExtension swrast_loader_extension = {
1600    .base = {__DRI_SWRAST_LOADER, 1},
1601 
1602    .getDrawableInfo = swrastGetDrawableInfo,
1603    .putImage = swrastPutImage,
1604    .putImage2 = swrastPutImage2,
1605    .getImage = swrastGetImage,
1606 };
1607 
1608 static const __DRIswrastLoaderExtension swrast_loader_shm_extension = {
1609    .base = {__DRI_SWRAST_LOADER, 4},
1610 
1611    .getDrawableInfo = swrastGetDrawableInfo,
1612    .putImage = swrastPutImage,
1613    .putImage2 = swrastPutImage2,
1614    .putImageShm = swrastPutImageShm,
1615    .getImage = swrastGetImage,
1616    .getImage2 = swrastGetImage2,
1617    .getImageShm = swrastGetImageShm,
1618 };
1619 
1620 static_assert(sizeof(struct kopper_vk_surface_create_storage) >=
1621                  sizeof(VkXcbSurfaceCreateInfoKHR),
1622               "");
1623 
1624 static void
kopperSetSurfaceCreateInfo(void * _draw,struct kopper_loader_info * ci)1625 kopperSetSurfaceCreateInfo(void *_draw, struct kopper_loader_info *ci)
1626 {
1627    struct dri2_egl_surface *dri2_surf = _draw;
1628    struct dri2_egl_display *dri2_dpy =
1629       dri2_egl_display(dri2_surf->base.Resource.Display);
1630    VkXcbSurfaceCreateInfoKHR *xcb = (VkXcbSurfaceCreateInfoKHR *)&ci->bos;
1631 
1632    if (dri2_surf->base.Type != EGL_WINDOW_BIT)
1633       return;
1634    xcb->sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
1635    xcb->pNext = NULL;
1636    xcb->flags = 0;
1637    xcb->connection = dri2_dpy->conn;
1638    xcb->window = dri2_surf->drawable;
1639    ci->has_alpha = dri2_surf->depth == 32;
1640    ci->present_opaque = dri2_surf->base.PresentOpaque;
1641 }
1642 
1643 static const __DRIkopperLoaderExtension kopper_loader_extension = {
1644    .base = {__DRI_KOPPER_LOADER, 1},
1645 
1646    .SetSurfaceCreateInfo = kopperSetSurfaceCreateInfo,
1647 };
1648 
1649 static const __DRIextension *kopper_loader_extensions[] = {
1650    &swrast_loader_extension.base,
1651    &image_lookup_extension.base,
1652    &kopper_loader_extension.base,
1653    &use_invalidate.base,
1654    NULL,
1655 };
1656 
1657 static const __DRIextension *swrast_loader_extensions[] = {
1658    &swrast_loader_extension.base,
1659    &image_lookup_extension.base,
1660    &kopper_loader_extension.base,
1661    NULL,
1662 };
1663 
1664 static const __DRIextension *swrast_loader_shm_extensions[] = {
1665    &swrast_loader_shm_extension.base,
1666    &image_lookup_extension.base,
1667    &kopper_loader_extension.base,
1668    NULL,
1669 };
1670 
1671 static int
dri2_find_screen_for_display(const _EGLDisplay * disp,int fallback_screen)1672 dri2_find_screen_for_display(const _EGLDisplay *disp, int fallback_screen)
1673 {
1674    const EGLAttrib *attr;
1675 
1676    if (!disp->Options.Attribs)
1677       return fallback_screen;
1678 
1679    for (attr = disp->Options.Attribs; attr[0] != EGL_NONE; attr += 2) {
1680       if (attr[0] == EGL_PLATFORM_X11_SCREEN_EXT ||
1681           attr[0] == EGL_PLATFORM_XCB_SCREEN_EXT)
1682          return attr[1];
1683    }
1684 
1685    return fallback_screen;
1686 }
1687 
1688 static EGLBoolean
dri2_get_xcb_connection(_EGLDisplay * disp,struct dri2_egl_display * dri2_dpy)1689 dri2_get_xcb_connection(_EGLDisplay *disp, struct dri2_egl_display *dri2_dpy)
1690 {
1691    xcb_screen_iterator_t s;
1692    int screen;
1693    const char *msg;
1694 
1695    disp->DriverData = (void *)dri2_dpy;
1696    if (disp->PlatformDisplay == NULL) {
1697       dri2_dpy->conn = xcb_connect(NULL, &screen);
1698       dri2_dpy->own_device = true;
1699       screen = dri2_find_screen_for_display(disp, screen);
1700    } else if (disp->Platform == _EGL_PLATFORM_X11) {
1701       Display *dpy = disp->PlatformDisplay;
1702       dri2_dpy->conn = XGetXCBConnection(dpy);
1703       screen = DefaultScreen(dpy);
1704    } else {
1705       /*   _EGL_PLATFORM_XCB   */
1706       dri2_dpy->conn = disp->PlatformDisplay;
1707       screen = dri2_find_screen_for_display(disp, 0);
1708    }
1709 
1710    if (!dri2_dpy->conn || xcb_connection_has_error(dri2_dpy->conn)) {
1711       msg = "xcb_connect failed";
1712       goto disconnect;
1713    }
1714 
1715    s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
1716    dri2_dpy->screen = get_xcb_screen(s, screen);
1717    if (!dri2_dpy->screen) {
1718       msg = "failed to get xcb screen";
1719       goto disconnect;
1720    }
1721 
1722    return EGL_TRUE;
1723 disconnect:
1724    if (disp->PlatformDisplay == NULL)
1725       xcb_disconnect(dri2_dpy->conn);
1726 
1727    return _eglError(EGL_BAD_ALLOC, msg);
1728 }
1729 
1730 static void
dri2_x11_setup_swap_interval(_EGLDisplay * disp)1731 dri2_x11_setup_swap_interval(_EGLDisplay *disp)
1732 {
1733    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1734    int arbitrary_max_interval = 1000;
1735 
1736    /* default behavior for no SwapBuffers support: no vblank syncing
1737     * either.
1738     */
1739    dri2_dpy->min_swap_interval = 0;
1740    dri2_dpy->max_swap_interval = 0;
1741    dri2_dpy->default_swap_interval = 0;
1742 
1743    if (!dri2_dpy->swap_available)
1744       return;
1745 
1746    /* If we do have swapbuffers, then we can support pretty much any swap
1747     * interval. Unless we're kopper, for now.
1748     */
1749    if (dri2_dpy->kopper)
1750       arbitrary_max_interval = 1;
1751 
1752    dri2_setup_swap_interval(disp, arbitrary_max_interval);
1753 }
1754 
1755 static bool
check_xshm(struct dri2_egl_display * dri2_dpy)1756 check_xshm(struct dri2_egl_display *dri2_dpy)
1757 {
1758    xcb_void_cookie_t cookie;
1759    xcb_generic_error_t *error;
1760    int ret = true;
1761    xcb_query_extension_cookie_t shm_cookie;
1762    xcb_query_extension_reply_t *shm_reply;
1763    bool has_mit_shm;
1764 
1765    shm_cookie = xcb_query_extension(dri2_dpy->conn, 7, "MIT-SHM");
1766    shm_reply = xcb_query_extension_reply(dri2_dpy->conn, shm_cookie, NULL);
1767 
1768    has_mit_shm = shm_reply->present;
1769    free(shm_reply);
1770    if (!has_mit_shm)
1771       return false;
1772 
1773    cookie = xcb_shm_detach_checked(dri2_dpy->conn, 0);
1774    if ((error = xcb_request_check(dri2_dpy->conn, cookie))) {
1775       /* BadRequest means we're a remote client. If we were local we'd
1776        * expect BadValue since 'info' has an invalid segment name.
1777        */
1778       if (error->error_code == BadRequest)
1779          ret = false;
1780       free(error);
1781    }
1782 
1783    return ret;
1784 }
1785 
1786 static EGLBoolean
dri2_x11_check_multibuffers(_EGLDisplay * disp)1787 dri2_x11_check_multibuffers(_EGLDisplay *disp)
1788 {
1789    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1790 
1791 #ifdef HAVE_X11_DRM
1792    bool err;
1793    dri2_dpy->multibuffers_available = x11_dri3_check_multibuffer(dri2_dpy->conn, &err, &dri2_dpy->explicit_modifiers);
1794 
1795    if (disp->Options.Zink && !disp->Options.ForceSoftware &&
1796        !dri2_dpy->multibuffers_available &&
1797        !dri2_dpy->kopper_without_modifiers)
1798       return EGL_FALSE;
1799 #endif
1800 
1801    return EGL_TRUE;
1802 }
1803 
1804 static EGLBoolean
dri2_initialize_x11_swrast(_EGLDisplay * disp)1805 dri2_initialize_x11_swrast(_EGLDisplay *disp)
1806 {
1807    struct dri2_egl_display *dri2_dpy = dri2_display_create();
1808    if (!dri2_dpy)
1809       return EGL_FALSE;
1810 
1811    if (!dri2_get_xcb_connection(disp, dri2_dpy))
1812       goto cleanup;
1813 
1814    /*
1815     * Every hardware driver_name is set using strdup. Doing the same in
1816     * here will allow is to simply free the memory at dri2_terminate().
1817     */
1818    dri2_dpy->driver_name = strdup(disp->Options.Zink ? "zink" : "swrast");
1819 
1820 #ifdef HAVE_LIBDRM
1821    if (disp->Options.Zink &&
1822        !debug_get_bool_option("LIBGL_DRI3_DISABLE", false) &&
1823        (!disp->Options.Zink || !debug_get_bool_option("LIBGL_KOPPER_DRI2", false)))
1824       dri3_x11_connect(dri2_dpy, disp->Options.Zink, disp->Options.ForceSoftware);
1825 #endif
1826 
1827    if (!dri2_load_driver(disp))
1828       goto cleanup;
1829 
1830    if (disp->Options.Zink && !disp->Options.ForceSoftware) {
1831       dri2_dpy->loader_extensions = kopper_loader_extensions;
1832    } else if (check_xshm(dri2_dpy)) {
1833       dri2_dpy->loader_extensions = swrast_loader_shm_extensions;
1834    } else {
1835       dri2_dpy->loader_extensions = swrast_loader_extensions;
1836    }
1837 
1838    if (!dri2_x11_check_multibuffers(disp))
1839       goto cleanup;
1840 
1841    if (!dri2_create_screen(disp))
1842       goto cleanup;
1843 
1844    if (!dri2_setup_device(disp, disp->Options.ForceSoftware || dri2_dpy->kopper_without_modifiers)) {
1845       _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to setup EGLDevice");
1846       goto cleanup;
1847    }
1848 
1849    dri2_setup_screen(disp);
1850 
1851    if (disp->Options.Zink) {
1852       /* kopper */
1853 #ifdef HAVE_WAYLAND_PLATFORM
1854       dri2_dpy->device_name = strdup("zink");
1855 #endif
1856       dri2_dpy->swap_available = EGL_TRUE;
1857       dri2_x11_setup_swap_interval(disp);
1858       if (dri2_dpy->fd_render_gpu == dri2_dpy->fd_display_gpu)
1859          disp->Extensions.KHR_image_pixmap = EGL_TRUE;
1860       disp->Extensions.NOK_texture_from_pixmap = EGL_TRUE;
1861       disp->Extensions.CHROMIUM_sync_control = EGL_TRUE;
1862       disp->Extensions.EXT_swap_buffers_with_damage = !!dri2_dpy->kopper;
1863 
1864 #ifdef HAVE_LIBDRM
1865       if (dri2_dpy->multibuffers_available)
1866          dri2_set_WL_bind_wayland_display(disp);
1867 #endif
1868    } else {
1869       disp->Extensions.EXT_swap_buffers_with_damage = EGL_TRUE;
1870    }
1871    disp->Extensions.EXT_buffer_age = EGL_TRUE;
1872    disp->Extensions.ANGLE_sync_control_rate = EGL_TRUE;
1873 
1874    dri2_x11_add_configs_for_visuals(dri2_dpy, disp, !disp->Options.Zink);
1875 
1876    /* Fill vtbl last to prevent accidentally calling virtual function during
1877     * initialization.
1878     */
1879    if (disp->Options.Zink)
1880       dri2_dpy->vtbl = &dri2_x11_kopper_display_vtbl;
1881    else
1882       dri2_dpy->vtbl = &dri2_x11_swrast_display_vtbl;
1883 
1884    return EGL_TRUE;
1885 
1886 cleanup:
1887    dri2_display_destroy(disp);
1888    return EGL_FALSE;
1889 }
1890 
1891 #ifdef HAVE_LIBDRM
1892 static const __DRIextension *dri3_image_loader_extensions[] = {
1893    &dri3_image_loader_extension.base,
1894    &image_lookup_extension.base,
1895    &use_invalidate.base,
1896    &background_callable_extension.base,
1897    NULL,
1898 };
1899 
1900 static enum dri2_egl_driver_fail
dri2_initialize_x11_dri3(_EGLDisplay * disp)1901 dri2_initialize_x11_dri3(_EGLDisplay *disp)
1902 {
1903    struct dri2_egl_display *dri2_dpy = dri2_display_create();
1904    enum dri2_egl_driver_fail status = DRI2_EGL_DRIVER_FAILED;
1905    if (!dri2_dpy)
1906       return DRI2_EGL_DRIVER_FAILED;
1907 
1908    if (!dri2_get_xcb_connection(disp, dri2_dpy))
1909       goto cleanup;
1910 
1911    status = dri3_x11_connect(dri2_dpy, disp->Options.Zink, disp->Options.ForceSoftware);
1912    if (status != DRI2_EGL_DRIVER_LOADED)
1913       goto cleanup;
1914 
1915    if (!dri2_load_driver(disp))
1916       goto cleanup;
1917 
1918    dri2_dpy->loader_extensions = dri3_image_loader_extensions;
1919 
1920    dri2_dpy->swap_available = true;
1921    dri2_dpy->invalidate_available = true;
1922 
1923    if (!dri2_x11_check_multibuffers(disp))
1924       goto cleanup;
1925 
1926    if (!dri2_create_screen(disp))
1927       goto cleanup;
1928 
1929    if (!dri2_setup_device(disp, false)) {
1930       _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to setup EGLDevice");
1931       goto cleanup;
1932    }
1933 
1934    dri2_setup_screen(disp);
1935 
1936    dri2_x11_setup_swap_interval(disp);
1937 
1938    if (dri2_dpy->fd_render_gpu == dri2_dpy->fd_display_gpu)
1939       disp->Extensions.KHR_image_pixmap = EGL_TRUE;
1940    disp->Extensions.NOK_texture_from_pixmap = EGL_TRUE;
1941    disp->Extensions.CHROMIUM_sync_control = EGL_TRUE;
1942    disp->Extensions.ANGLE_sync_control_rate = EGL_TRUE;
1943    disp->Extensions.EXT_buffer_age = EGL_TRUE;
1944    disp->Extensions.EXT_swap_buffers_with_damage = EGL_TRUE;
1945 
1946    dri2_set_WL_bind_wayland_display(disp);
1947 
1948    dri2_x11_add_configs_for_visuals(dri2_dpy, disp, false);
1949 
1950    loader_init_screen_resources(&dri2_dpy->screen_resources, dri2_dpy->conn,
1951                                 dri2_dpy->screen);
1952 
1953    /* Fill vtbl last to prevent accidentally calling virtual function during
1954     * initialization.
1955     */
1956    dri2_dpy->vtbl = &dri3_x11_display_vtbl;
1957 
1958    _eglLog(_EGL_INFO, "Using DRI3");
1959 
1960    return DRI2_EGL_DRIVER_LOADED;
1961 
1962 cleanup:
1963    dri2_display_destroy(disp);
1964    return status == DRI2_EGL_DRIVER_PREFER_ZINK ?
1965           DRI2_EGL_DRIVER_PREFER_ZINK :
1966           DRI2_EGL_DRIVER_FAILED;
1967 }
1968 #endif
1969 
1970 #ifdef HAVE_X11_DRI2
1971 static const __DRIdri2LoaderExtension dri2_loader_extension_old = {
1972    .base = {__DRI_DRI2_LOADER, 2},
1973 
1974    .getBuffers = dri2_x11_get_buffers,
1975    .flushFrontBuffer = dri2_x11_flush_front_buffer,
1976    .getBuffersWithFormat = NULL,
1977 };
1978 
1979 static const __DRIdri2LoaderExtension dri2_loader_extension = {
1980    .base = {__DRI_DRI2_LOADER, 3},
1981 
1982    .getBuffers = dri2_x11_get_buffers,
1983    .flushFrontBuffer = dri2_x11_flush_front_buffer,
1984    .getBuffersWithFormat = dri2_x11_get_buffers_with_format,
1985 };
1986 
1987 static const __DRIextension *dri2_loader_extensions_old[] = {
1988    &dri2_loader_extension_old.base,
1989    &image_lookup_extension.base,
1990    &background_callable_extension.base,
1991    NULL,
1992 };
1993 
1994 static const __DRIextension *dri2_loader_extensions[] = {
1995    &dri2_loader_extension.base,
1996    &image_lookup_extension.base,
1997    &use_invalidate.base,
1998    &background_callable_extension.base,
1999    NULL,
2000 };
2001 
2002 static EGLBoolean
dri2_initialize_x11_dri2(_EGLDisplay * disp)2003 dri2_initialize_x11_dri2(_EGLDisplay *disp)
2004 {
2005    struct dri2_egl_display *dri2_dpy = dri2_display_create();
2006    if (!dri2_dpy)
2007       return EGL_FALSE;
2008 
2009    if (!dri2_get_xcb_connection(disp, dri2_dpy))
2010       goto cleanup;
2011 
2012    if (!dri2_x11_connect(dri2_dpy))
2013       goto cleanup;
2014 
2015    if (!dri2_load_driver(disp))
2016       goto cleanup;
2017 
2018    if (dri2_dpy->dri2_minor >= 1)
2019       dri2_dpy->loader_extensions = dri2_loader_extensions;
2020    else
2021       dri2_dpy->loader_extensions = dri2_loader_extensions_old;
2022 
2023    dri2_dpy->swap_available = (dri2_dpy->dri2_minor >= 2);
2024    dri2_dpy->invalidate_available = (dri2_dpy->dri2_minor >= 3);
2025 
2026    if (!dri2_x11_check_multibuffers(disp))
2027       goto cleanup;
2028 
2029    if (!dri2_create_screen(disp))
2030       goto cleanup;
2031 
2032    if (!dri2_setup_device(disp, false)) {
2033       _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to setup EGLDevice");
2034       goto cleanup;
2035    }
2036 
2037    dri2_setup_screen(disp);
2038 
2039    dri2_x11_setup_swap_interval(disp);
2040 
2041    disp->Extensions.KHR_image_pixmap = EGL_TRUE;
2042    disp->Extensions.NOK_swap_region = EGL_TRUE;
2043    disp->Extensions.NOK_texture_from_pixmap = EGL_TRUE;
2044    disp->Extensions.NV_post_sub_buffer = EGL_TRUE;
2045    disp->Extensions.CHROMIUM_sync_control = EGL_TRUE;
2046    disp->Extensions.ANGLE_sync_control_rate = EGL_TRUE;
2047 
2048    dri2_set_WL_bind_wayland_display(disp);
2049 
2050    dri2_x11_add_configs_for_visuals(dri2_dpy, disp, true);
2051 
2052    /* Fill vtbl last to prevent accidentally calling virtual function during
2053     * initialization.
2054     */
2055    dri2_dpy->vtbl = &dri2_x11_display_vtbl;
2056 
2057    _eglLog(_EGL_INFO, "Using DRI2");
2058 
2059    return EGL_TRUE;
2060 
2061 cleanup:
2062    dri2_display_destroy(disp);
2063    return EGL_FALSE;
2064 }
2065 #endif
2066 
2067 EGLBoolean
dri2_initialize_x11(_EGLDisplay * disp)2068 dri2_initialize_x11(_EGLDisplay *disp)
2069 {
2070    enum dri2_egl_driver_fail status = DRI2_EGL_DRIVER_FAILED;
2071    if (disp->Options.ForceSoftware ||
2072        (disp->Options.Zink && !debug_get_bool_option("LIBGL_KOPPER_DISABLE", false)))
2073       return dri2_initialize_x11_swrast(disp);
2074 
2075 #ifdef HAVE_LIBDRM
2076    if (!debug_get_bool_option("LIBGL_DRI3_DISABLE", false)) {
2077       status = dri2_initialize_x11_dri3(disp);
2078       if (status == DRI2_EGL_DRIVER_LOADED)
2079          return EGL_TRUE;
2080    }
2081 #endif
2082 
2083 #ifdef HAVE_X11_DRI2
2084    if (!debug_get_bool_option("LIBGL_DRI2_DISABLE", false) &&
2085        status != DRI2_EGL_DRIVER_PREFER_ZINK)
2086       if (dri2_initialize_x11_dri2(disp))
2087          return EGL_TRUE;
2088 #endif
2089 
2090    return EGL_FALSE;
2091 }
2092 
2093 void
dri2_teardown_x11(struct dri2_egl_display * dri2_dpy)2094 dri2_teardown_x11(struct dri2_egl_display *dri2_dpy)
2095 {
2096 #ifdef HAVE_LIBDRM
2097    if (dri2_dpy->dri2_major >= 3)
2098       loader_destroy_screen_resources(&dri2_dpy->screen_resources);
2099 #endif
2100 
2101    if (dri2_dpy->own_device)
2102       xcb_disconnect(dri2_dpy->conn);
2103 }
2104