st/egl: Add dri2InvalidateBuffers.
[mesa.git] / src / gallium / state_trackers / egl / x11 / native_dri2.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.8
4 *
5 * Copyright (C) 2009-2010 Chia-I Wu <olv@0xlab.org>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include "util/u_memory.h"
26 #include "util/u_math.h"
27 #include "util/u_format.h"
28 #include "util/u_inlines.h"
29 #include "pipe/p_compiler.h"
30 #include "pipe/p_screen.h"
31 #include "pipe/p_context.h"
32 #include "pipe/p_state.h"
33 #include "state_tracker/drm_api.h"
34 #include "egllog.h"
35
36 #include "native_x11.h"
37 #include "x11_screen.h"
38
39 enum dri2_surface_type {
40 DRI2_SURFACE_TYPE_WINDOW,
41 DRI2_SURFACE_TYPE_PIXMAP,
42 DRI2_SURFACE_TYPE_PBUFFER
43 };
44
45 struct dri2_display {
46 struct native_display base;
47 Display *dpy;
48 boolean own_dpy;
49
50 struct drm_api *api;
51 struct x11_screen *xscr;
52 int xscr_number;
53 const char *dri_driver;
54 int dri_major, dri_minor;
55
56 struct dri2_config *configs;
57 int num_configs;
58 };
59
60 struct dri2_surface {
61 struct native_surface base;
62 Drawable drawable;
63 enum dri2_surface_type type;
64 enum pipe_format color_format;
65 struct dri2_display *dri2dpy;
66
67 unsigned int sequence_number;
68 int width, height;
69 struct pipe_texture *textures[NUM_NATIVE_ATTACHMENTS];
70 uint valid_mask;
71
72 boolean have_back, have_fake;
73
74 struct x11_drawable_buffer *last_xbufs;
75 int last_num_xbufs;
76 };
77
78 struct dri2_config {
79 struct native_config base;
80 };
81
82 static INLINE struct dri2_display *
83 dri2_display(const struct native_display *ndpy)
84 {
85 return (struct dri2_display *) ndpy;
86 }
87
88 static INLINE struct dri2_surface *
89 dri2_surface(const struct native_surface *nsurf)
90 {
91 return (struct dri2_surface *) nsurf;
92 }
93
94 static INLINE struct dri2_config *
95 dri2_config(const struct native_config *nconf)
96 {
97 return (struct dri2_config *) nconf;
98 }
99
100 /**
101 * Get the buffers from the server.
102 */
103 static void
104 dri2_surface_get_buffers(struct native_surface *nsurf, uint buffer_mask)
105 {
106 struct dri2_surface *dri2surf = dri2_surface(nsurf);
107 struct dri2_display *dri2dpy = dri2surf->dri2dpy;
108 unsigned int dri2atts[NUM_NATIVE_ATTACHMENTS];
109 int num_ins, num_outs, att, i;
110 struct x11_drawable_buffer *xbufs;
111 struct pipe_texture templ;
112 uint valid_mask;
113
114 /* prepare the attachments */
115 num_ins = 0;
116 for (att = 0; att < NUM_NATIVE_ATTACHMENTS; att++) {
117 if (native_attachment_mask_test(buffer_mask, att)) {
118 unsigned int dri2att;
119
120 switch (att) {
121 case NATIVE_ATTACHMENT_FRONT_LEFT:
122 dri2att = DRI2BufferFrontLeft;
123 break;
124 case NATIVE_ATTACHMENT_BACK_LEFT:
125 dri2att = DRI2BufferBackLeft;
126 break;
127 case NATIVE_ATTACHMENT_FRONT_RIGHT:
128 dri2att = DRI2BufferFrontRight;
129 break;
130 case NATIVE_ATTACHMENT_BACK_RIGHT:
131 dri2att = DRI2BufferBackRight;
132 break;
133 default:
134 assert(0);
135 dri2att = 0;
136 break;
137 }
138
139 dri2atts[num_ins] = dri2att;
140 num_ins++;
141 }
142 }
143
144 xbufs = x11_drawable_get_buffers(dri2dpy->xscr, dri2surf->drawable,
145 &dri2surf->width, &dri2surf->height,
146 dri2atts, FALSE, num_ins, &num_outs);
147
148 /* we should be able to do better... */
149 if (xbufs && dri2surf->last_num_xbufs == num_outs &&
150 memcmp(dri2surf->last_xbufs, xbufs, sizeof(*xbufs) * num_outs) == 0) {
151 free(xbufs);
152 return;
153 }
154
155 /* free the old buffers */
156 for (i = 0; i < NUM_NATIVE_ATTACHMENTS; i++)
157 pipe_texture_reference(&dri2surf->textures[i], NULL);
158 dri2surf->valid_mask = 0x0;
159 dri2surf->sequence_number++;
160
161 dri2surf->have_back = FALSE;
162 dri2surf->have_fake = FALSE;
163
164 if (!xbufs)
165 return;
166
167 memset(&templ, 0, sizeof(templ));
168 templ.target = PIPE_TEXTURE_2D;
169 templ.last_level = 0;
170 templ.width0 = dri2surf->width;
171 templ.height0 = dri2surf->height;
172 templ.depth0 = 1;
173 templ.format = dri2surf->color_format;
174 templ.tex_usage = PIPE_TEXTURE_USAGE_RENDER_TARGET;
175
176 valid_mask = 0x0;
177 for (i = 0; i < num_outs; i++) {
178 struct x11_drawable_buffer *xbuf = &xbufs[i];
179 const char *desc;
180 enum native_attachment natt;
181
182 switch (xbuf->attachment) {
183 case DRI2BufferFrontLeft:
184 natt = NATIVE_ATTACHMENT_FRONT_LEFT;
185 desc = "DRI2 Front Buffer";
186 break;
187 case DRI2BufferFakeFrontLeft:
188 natt = NATIVE_ATTACHMENT_FRONT_LEFT;
189 desc = "DRI2 Fake Front Buffer";
190 dri2surf->have_fake = TRUE;
191 break;
192 case DRI2BufferBackLeft:
193 natt = NATIVE_ATTACHMENT_BACK_LEFT;
194 desc = "DRI2 Back Buffer";
195 dri2surf->have_back = TRUE;
196 break;
197 default:
198 desc = NULL;
199 break;
200 }
201
202 if (!desc || dri2surf->textures[natt]) {
203 if (!desc)
204 _eglLog(_EGL_WARNING, "unknown buffer %d", xbuf->attachment);
205 else
206 _eglLog(_EGL_WARNING, "both real and fake front buffers are listed");
207 continue;
208 }
209
210 dri2surf->textures[natt] =
211 dri2dpy->api->texture_from_shared_handle(dri2dpy->api,
212 dri2dpy->base.screen, &templ, desc, xbuf->pitch, xbuf->name);
213 if (dri2surf->textures[natt])
214 valid_mask |= 1 << natt;
215 }
216
217 if (dri2surf->last_xbufs)
218 free(dri2surf->last_xbufs);
219 dri2surf->last_xbufs = xbufs;
220 dri2surf->last_num_xbufs = num_outs;
221
222 dri2surf->valid_mask = valid_mask;
223 }
224
225 /**
226 * Update the buffers of the surface. This is a slow function due to the
227 * round-trip to the server.
228 */
229 static boolean
230 dri2_surface_update_buffers(struct native_surface *nsurf, uint buffer_mask)
231 {
232 struct dri2_surface *dri2surf = dri2_surface(nsurf);
233 struct dri2_display *dri2dpy = dri2surf->dri2dpy;
234
235 /* create textures for pbuffer */
236 if (dri2surf->type == DRI2_SURFACE_TYPE_PBUFFER) {
237 struct pipe_screen *screen = dri2dpy->base.screen;
238 struct pipe_texture templ;
239 uint new_valid = 0x0;
240 int att;
241
242 buffer_mask &= ~dri2surf->valid_mask;
243 if (!buffer_mask)
244 return TRUE;
245
246 memset(&templ, 0, sizeof(templ));
247 templ.target = PIPE_TEXTURE_2D;
248 templ.last_level = 0;
249 templ.width0 = dri2surf->width;
250 templ.height0 = dri2surf->height;
251 templ.depth0 = 1;
252 templ.format = dri2surf->color_format;
253 templ.tex_usage = PIPE_TEXTURE_USAGE_RENDER_TARGET;
254
255 for (att = 0; att < NUM_NATIVE_ATTACHMENTS; att++) {
256 if (native_attachment_mask_test(buffer_mask, att)) {
257 assert(!dri2surf->textures[att]);
258
259 dri2surf->textures[att] = screen->texture_create(screen, &templ);
260 if (!dri2surf->textures[att])
261 break;
262
263 new_valid |= 1 << att;
264 if (new_valid == buffer_mask)
265 break;
266 }
267 }
268 dri2surf->valid_mask |= new_valid;
269 /* no need to update sequence number */
270 }
271 else {
272 dri2_surface_get_buffers(&dri2surf->base, buffer_mask);
273 }
274
275 return ((dri2surf->valid_mask & buffer_mask) == buffer_mask);
276 }
277
278 static boolean
279 dri2_surface_flush_frontbuffer(struct native_surface *nsurf)
280 {
281 struct dri2_surface *dri2surf = dri2_surface(nsurf);
282 struct dri2_display *dri2dpy = dri2surf->dri2dpy;
283
284 /* pbuffer is private */
285 if (dri2surf->type == DRI2_SURFACE_TYPE_PBUFFER)
286 return TRUE;
287
288 /* copy to real front buffer */
289 if (dri2surf->have_fake)
290 x11_drawable_copy_buffers(dri2dpy->xscr, dri2surf->drawable,
291 0, 0, dri2surf->width, dri2surf->height,
292 DRI2BufferFakeFrontLeft, DRI2BufferFrontLeft);
293
294 return TRUE;
295 }
296
297 static boolean
298 dri2_surface_swap_buffers(struct native_surface *nsurf)
299 {
300 struct dri2_surface *dri2surf = dri2_surface(nsurf);
301 struct dri2_display *dri2dpy = dri2surf->dri2dpy;
302
303 /* pbuffer is private */
304 if (dri2surf->type == DRI2_SURFACE_TYPE_PBUFFER)
305 return TRUE;
306
307 /* copy to front buffer */
308 if (dri2surf->have_back)
309 x11_drawable_copy_buffers(dri2dpy->xscr, dri2surf->drawable,
310 0, 0, dri2surf->width, dri2surf->height,
311 DRI2BufferBackLeft, DRI2BufferFrontLeft);
312
313 /* and update fake front buffer */
314 if (dri2surf->have_fake)
315 x11_drawable_copy_buffers(dri2dpy->xscr, dri2surf->drawable,
316 0, 0, dri2surf->width, dri2surf->height,
317 DRI2BufferFrontLeft, DRI2BufferFakeFrontLeft);
318
319 return TRUE;
320 }
321
322 static boolean
323 dri2_surface_validate(struct native_surface *nsurf, uint attachment_mask,
324 unsigned int *seq_num, struct pipe_texture **textures,
325 int *width, int *height)
326 {
327 struct dri2_surface *dri2surf = dri2_surface(nsurf);
328
329 if (!dri2_surface_update_buffers(&dri2surf->base, attachment_mask))
330 return FALSE;
331
332 if (seq_num)
333 *seq_num = dri2surf->sequence_number;
334
335 if (textures) {
336 int att;
337 for (att = 0; att < NUM_NATIVE_ATTACHMENTS; att++) {
338 if (native_attachment_mask_test(attachment_mask, att)) {
339 struct pipe_texture *ptex = dri2surf->textures[att];
340
341 textures[att] = NULL;
342 pipe_texture_reference(&textures[att], ptex);
343 }
344 }
345 }
346
347 if (width)
348 *width = dri2surf->width;
349 if (height)
350 *height = dri2surf->height;
351
352 return TRUE;
353 }
354
355 static void
356 dri2_surface_wait(struct native_surface *nsurf)
357 {
358 struct dri2_surface *dri2surf = dri2_surface(nsurf);
359 struct dri2_display *dri2dpy = dri2surf->dri2dpy;
360
361 if (dri2surf->have_fake) {
362 x11_drawable_copy_buffers(dri2dpy->xscr, dri2surf->drawable,
363 0, 0, dri2surf->width, dri2surf->height,
364 DRI2BufferFrontLeft, DRI2BufferFakeFrontLeft);
365 }
366 }
367
368 static void
369 dri2_surface_destroy(struct native_surface *nsurf)
370 {
371 struct dri2_surface *dri2surf = dri2_surface(nsurf);
372 int i;
373
374 if (dri2surf->last_xbufs)
375 free(dri2surf->last_xbufs);
376
377 for (i = 0; i < NUM_NATIVE_ATTACHMENTS; i++) {
378 struct pipe_texture *ptex = dri2surf->textures[i];
379 pipe_texture_reference(&ptex, NULL);
380 }
381
382 if (dri2surf->drawable)
383 x11_drawable_enable_dri2(dri2surf->dri2dpy->xscr,
384 dri2surf->drawable, FALSE);
385 free(dri2surf);
386 }
387
388 static struct dri2_surface *
389 dri2_display_create_surface(struct native_display *ndpy,
390 enum dri2_surface_type type,
391 Drawable drawable,
392 const struct native_config *nconf)
393 {
394 struct dri2_display *dri2dpy = dri2_display(ndpy);
395 struct dri2_config *dri2conf = dri2_config(nconf);
396 struct dri2_surface *dri2surf;
397
398 dri2surf = CALLOC_STRUCT(dri2_surface);
399 if (!dri2surf)
400 return NULL;
401
402 dri2surf->dri2dpy = dri2dpy;
403 dri2surf->type = type;
404 dri2surf->drawable = drawable;
405 dri2surf->color_format = dri2conf->base.color_format;
406
407 dri2surf->base.destroy = dri2_surface_destroy;
408 dri2surf->base.swap_buffers = dri2_surface_swap_buffers;
409 dri2surf->base.flush_frontbuffer = dri2_surface_flush_frontbuffer;
410 dri2surf->base.validate = dri2_surface_validate;
411 dri2surf->base.wait = dri2_surface_wait;
412
413 if (drawable)
414 x11_drawable_enable_dri2(dri2dpy->xscr, drawable, TRUE);
415
416 return dri2surf;
417 }
418
419 static struct native_surface *
420 dri2_display_create_window_surface(struct native_display *ndpy,
421 EGLNativeWindowType win,
422 const struct native_config *nconf)
423 {
424 struct dri2_surface *dri2surf;
425
426 dri2surf = dri2_display_create_surface(ndpy, DRI2_SURFACE_TYPE_WINDOW,
427 (Drawable) win, nconf);
428 return (dri2surf) ? &dri2surf->base : NULL;
429 }
430
431 static struct native_surface *
432 dri2_display_create_pixmap_surface(struct native_display *ndpy,
433 EGLNativePixmapType pix,
434 const struct native_config *nconf)
435 {
436 struct dri2_surface *dri2surf;
437
438 dri2surf = dri2_display_create_surface(ndpy, DRI2_SURFACE_TYPE_PIXMAP,
439 (Drawable) pix, nconf);
440 return (dri2surf) ? &dri2surf->base : NULL;
441 }
442
443 static struct native_surface *
444 dri2_display_create_pbuffer_surface(struct native_display *ndpy,
445 const struct native_config *nconf,
446 uint width, uint height)
447 {
448 struct dri2_surface *dri2surf;
449
450 dri2surf = dri2_display_create_surface(ndpy, DRI2_SURFACE_TYPE_PBUFFER,
451 (Drawable) None, nconf);
452 if (dri2surf) {
453 dri2surf->width = width;
454 dri2surf->height = height;
455 }
456 return (dri2surf) ? &dri2surf->base : NULL;
457 }
458
459 static int
460 choose_color_format(const __GLcontextModes *mode, enum pipe_format formats[32])
461 {
462 int count = 0;
463
464 switch (mode->rgbBits) {
465 case 32:
466 formats[count++] = PIPE_FORMAT_A8R8G8B8_UNORM;
467 formats[count++] = PIPE_FORMAT_B8G8R8A8_UNORM;
468 break;
469 case 24:
470 formats[count++] = PIPE_FORMAT_X8R8G8B8_UNORM;
471 formats[count++] = PIPE_FORMAT_B8G8R8X8_UNORM;
472 formats[count++] = PIPE_FORMAT_A8R8G8B8_UNORM;
473 formats[count++] = PIPE_FORMAT_B8G8R8A8_UNORM;
474 break;
475 case 16:
476 formats[count++] = PIPE_FORMAT_R5G6B5_UNORM;
477 break;
478 default:
479 break;
480 }
481
482 return count;
483 }
484
485 static int
486 choose_depth_stencil_format(const __GLcontextModes *mode,
487 enum pipe_format formats[32])
488 {
489 int count = 0;
490
491 switch (mode->depthBits) {
492 case 32:
493 formats[count++] = PIPE_FORMAT_Z32_UNORM;
494 break;
495 case 24:
496 if (mode->stencilBits) {
497 formats[count++] = PIPE_FORMAT_S8Z24_UNORM;
498 formats[count++] = PIPE_FORMAT_Z24S8_UNORM;
499 }
500 else {
501 formats[count++] = PIPE_FORMAT_X8Z24_UNORM;
502 formats[count++] = PIPE_FORMAT_Z24X8_UNORM;
503 }
504 break;
505 case 16:
506 formats[count++] = PIPE_FORMAT_Z16_UNORM;
507 break;
508 default:
509 break;
510 }
511
512 return count;
513 }
514
515 static boolean
516 is_format_supported(struct pipe_screen *screen,
517 enum pipe_format fmt, boolean is_color)
518 {
519 return screen->is_format_supported(screen, fmt, PIPE_TEXTURE_2D,
520 (is_color) ? PIPE_TEXTURE_USAGE_RENDER_TARGET :
521 PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0);
522 }
523
524 static boolean
525 dri2_display_convert_config(struct native_display *ndpy,
526 const __GLcontextModes *mode,
527 struct native_config *nconf)
528 {
529 enum pipe_format formats[32];
530 int num_formats, i;
531
532 if (!(mode->renderType & GLX_RGBA_BIT) || !mode->rgbMode)
533 return FALSE;
534
535 /* skip single-buffered configs */
536 if (!mode->doubleBufferMode)
537 return FALSE;
538
539 nconf->mode = *mode;
540 nconf->mode.renderType = GLX_RGBA_BIT;
541 nconf->mode.rgbMode = TRUE;
542 /* pbuffer is allocated locally and is always supported */
543 nconf->mode.drawableType |= GLX_PBUFFER_BIT;
544 /* the swap method is always copy */
545 nconf->mode.swapMethod = GLX_SWAP_COPY_OML;
546
547 /* fix up */
548 nconf->mode.rgbBits =
549 nconf->mode.redBits + nconf->mode.greenBits +
550 nconf->mode.blueBits + nconf->mode.alphaBits;
551 if (!(nconf->mode.drawableType & GLX_WINDOW_BIT)) {
552 nconf->mode.visualID = 0;
553 nconf->mode.visualType = GLX_NONE;
554 }
555 if (!(nconf->mode.drawableType & GLX_PBUFFER_BIT)) {
556 nconf->mode.bindToTextureRgb = FALSE;
557 nconf->mode.bindToTextureRgba = FALSE;
558 }
559
560 nconf->color_format = PIPE_FORMAT_NONE;
561 nconf->depth_format = PIPE_FORMAT_NONE;
562 nconf->stencil_format = PIPE_FORMAT_NONE;
563
564 /* choose color format */
565 num_formats = choose_color_format(mode, formats);
566 for (i = 0; i < num_formats; i++) {
567 if (is_format_supported(ndpy->screen, formats[i], TRUE)) {
568 nconf->color_format = formats[i];
569 break;
570 }
571 }
572 if (nconf->color_format == PIPE_FORMAT_NONE)
573 return FALSE;
574
575 /* choose depth/stencil format */
576 num_formats = choose_depth_stencil_format(mode, formats);
577 for (i = 0; i < num_formats; i++) {
578 if (is_format_supported(ndpy->screen, formats[i], FALSE)) {
579 nconf->depth_format = formats[i];
580 nconf->stencil_format = formats[i];
581 break;
582 }
583 }
584 if ((nconf->mode.depthBits && nconf->depth_format == PIPE_FORMAT_NONE) ||
585 (nconf->mode.stencilBits && nconf->stencil_format == PIPE_FORMAT_NONE))
586 return FALSE;
587
588 return TRUE;
589 }
590
591 static const struct native_config **
592 dri2_display_get_configs(struct native_display *ndpy, int *num_configs)
593 {
594 struct dri2_display *dri2dpy = dri2_display(ndpy);
595 const struct native_config **configs;
596 int i;
597
598 /* first time */
599 if (!dri2dpy->configs) {
600 const __GLcontextModes *modes;
601 int num_modes, count;
602
603 modes = x11_screen_get_glx_configs(dri2dpy->xscr);
604 if (!modes)
605 return NULL;
606 num_modes = x11_context_modes_count(modes);
607
608 dri2dpy->configs = calloc(num_modes, sizeof(*dri2dpy->configs));
609 if (!dri2dpy->configs)
610 return NULL;
611
612 count = 0;
613 for (i = 0; i < num_modes; i++) {
614 struct native_config *nconf = &dri2dpy->configs[count].base;
615 if (dri2_display_convert_config(&dri2dpy->base, modes, nconf))
616 count++;
617 modes = modes->next;
618 }
619
620 dri2dpy->num_configs = count;
621 }
622
623 configs = malloc(dri2dpy->num_configs * sizeof(*configs));
624 if (configs) {
625 for (i = 0; i < dri2dpy->num_configs; i++)
626 configs[i] = (const struct native_config *) &dri2dpy->configs[i];
627 if (num_configs)
628 *num_configs = dri2dpy->num_configs;
629 }
630
631 return configs;
632 }
633
634 static boolean
635 dri2_display_is_pixmap_supported(struct native_display *ndpy,
636 EGLNativePixmapType pix,
637 const struct native_config *nconf)
638 {
639 struct dri2_display *dri2dpy = dri2_display(ndpy);
640 uint depth, nconf_depth;
641
642 depth = x11_drawable_get_depth(dri2dpy->xscr, (Drawable) pix);
643 nconf_depth = util_format_get_blocksizebits(nconf->color_format);
644
645 /* simple depth match for now */
646 return (depth == nconf_depth || (depth == 24 && depth + 8 == nconf_depth));
647 }
648
649 static void
650 dri2_display_destroy(struct native_display *ndpy)
651 {
652 struct dri2_display *dri2dpy = dri2_display(ndpy);
653
654 if (dri2dpy->configs)
655 free(dri2dpy->configs);
656
657 if (dri2dpy->base.screen)
658 dri2dpy->base.screen->destroy(dri2dpy->base.screen);
659
660 if (dri2dpy->xscr)
661 x11_screen_destroy(dri2dpy->xscr);
662 if (dri2dpy->own_dpy)
663 XCloseDisplay(dri2dpy->dpy);
664 if (dri2dpy->api && dri2dpy->api->destroy)
665 dri2dpy->api->destroy(dri2dpy->api);
666 free(dri2dpy);
667 }
668
669 /**
670 * Initialize DRI2 and pipe screen.
671 */
672 static boolean
673 dri2_display_init_screen(struct native_display *ndpy)
674 {
675 struct dri2_display *dri2dpy = dri2_display(ndpy);
676 const char *driver = dri2dpy->api->name;
677 struct drm_create_screen_arg arg;
678 int fd;
679
680 if (!x11_screen_support(dri2dpy->xscr, X11_SCREEN_EXTENSION_DRI2) ||
681 !x11_screen_support(dri2dpy->xscr, X11_SCREEN_EXTENSION_GLX)) {
682 _eglLog(_EGL_WARNING, "GLX/DRI2 is not supported");
683 return FALSE;
684 }
685
686 dri2dpy->dri_driver = x11_screen_probe_dri2(dri2dpy->xscr,
687 &dri2dpy->dri_major, &dri2dpy->dri_minor);
688 if (!dri2dpy->dri_driver || !driver ||
689 strcmp(dri2dpy->dri_driver, driver) != 0) {
690 _eglLog(_EGL_WARNING, "Driver mismatch: %s != %s",
691 dri2dpy->dri_driver, dri2dpy->api->name);
692 return FALSE;
693 }
694
695 fd = x11_screen_enable_dri2(dri2dpy->xscr, NULL, NULL);
696 if (fd < 0)
697 return FALSE;
698
699 memset(&arg, 0, sizeof(arg));
700 arg.mode = DRM_CREATE_NORMAL;
701 dri2dpy->base.screen = dri2dpy->api->create_screen(dri2dpy->api, fd, &arg);
702 if (!dri2dpy->base.screen) {
703 _eglLog(_EGL_WARNING, "failed to create DRM screen");
704 return FALSE;
705 }
706
707 return TRUE;
708 }
709
710 struct native_display *
711 x11_create_dri2_display(EGLNativeDisplayType dpy, struct drm_api *api)
712 {
713 struct dri2_display *dri2dpy;
714
715 dri2dpy = CALLOC_STRUCT(dri2_display);
716 if (!dri2dpy)
717 return NULL;
718
719 dri2dpy->api = api;
720 if (!dri2dpy->api) {
721 _eglLog(_EGL_WARNING, "failed to create DRM API");
722 free(dri2dpy);
723 return NULL;
724 }
725
726 dri2dpy->dpy = dpy;
727 if (!dri2dpy->dpy) {
728 dri2dpy->dpy = XOpenDisplay(NULL);
729 if (!dri2dpy->dpy) {
730 dri2_display_destroy(&dri2dpy->base);
731 return NULL;
732 }
733 dri2dpy->own_dpy = TRUE;
734 }
735
736 dri2dpy->xscr_number = DefaultScreen(dri2dpy->dpy);
737 dri2dpy->xscr = x11_screen_create(dri2dpy->dpy, dri2dpy->xscr_number);
738 if (!dri2dpy->xscr) {
739 dri2_display_destroy(&dri2dpy->base);
740 return NULL;
741 }
742
743 if (!dri2_display_init_screen(&dri2dpy->base)) {
744 dri2_display_destroy(&dri2dpy->base);
745 return NULL;
746 }
747
748 dri2dpy->base.destroy = dri2_display_destroy;
749 dri2dpy->base.get_configs = dri2_display_get_configs;
750 dri2dpy->base.is_pixmap_supported = dri2_display_is_pixmap_supported;
751 dri2dpy->base.create_window_surface = dri2_display_create_window_surface;
752 dri2dpy->base.create_pixmap_surface = dri2_display_create_pixmap_surface;
753 dri2dpy->base.create_pbuffer_surface = dri2_display_create_pbuffer_surface;
754
755 return &dri2dpy->base;
756 }