2 * Mesa 3-D graphics library
5 * Copyright (C) 2009-2010 Chia-I Wu <olv@0xlab.org>
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
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 "util/u_hash_table.h"
30 #include "pipe/p_compiler.h"
31 #include "pipe/p_screen.h"
32 #include "pipe/p_context.h"
33 #include "pipe/p_state.h"
34 #include "state_tracker/drm_api.h"
37 #include "native_x11.h"
38 #include "x11_screen.h"
40 enum dri2_surface_type
{
41 DRI2_SURFACE_TYPE_WINDOW
,
42 DRI2_SURFACE_TYPE_PIXMAP
,
43 DRI2_SURFACE_TYPE_PBUFFER
47 struct native_display base
;
52 struct x11_screen
*xscr
;
54 const char *dri_driver
;
55 int dri_major
, dri_minor
;
57 struct dri2_config
*configs
;
60 struct util_hash_table
*surfaces
;
64 struct native_surface base
;
66 enum dri2_surface_type type
;
67 enum pipe_format color_format
;
68 struct dri2_display
*dri2dpy
;
70 unsigned int server_stamp
;
71 unsigned int client_stamp
;
73 struct pipe_texture
*textures
[NUM_NATIVE_ATTACHMENTS
];
76 boolean have_back
, have_fake
;
78 struct x11_drawable_buffer
*last_xbufs
;
83 struct native_config base
;
86 static INLINE
struct dri2_display
*
87 dri2_display(const struct native_display
*ndpy
)
89 return (struct dri2_display
*) ndpy
;
92 static INLINE
struct dri2_surface
*
93 dri2_surface(const struct native_surface
*nsurf
)
95 return (struct dri2_surface
*) nsurf
;
98 static INLINE
struct dri2_config
*
99 dri2_config(const struct native_config
*nconf
)
101 return (struct dri2_config
*) nconf
;
105 * Process the buffers returned by the server.
108 dri2_surface_process_drawable_buffers(struct native_surface
*nsurf
,
109 struct x11_drawable_buffer
*xbufs
,
112 struct dri2_surface
*dri2surf
= dri2_surface(nsurf
);
113 struct dri2_display
*dri2dpy
= dri2surf
->dri2dpy
;
114 struct pipe_texture templ
;
118 /* free the old textures */
119 for (i
= 0; i
< NUM_NATIVE_ATTACHMENTS
; i
++)
120 pipe_texture_reference(&dri2surf
->textures
[i
], NULL
);
121 dri2surf
->valid_mask
= 0x0;
123 dri2surf
->have_back
= FALSE
;
124 dri2surf
->have_fake
= FALSE
;
129 memset(&templ
, 0, sizeof(templ
));
130 templ
.target
= PIPE_TEXTURE_2D
;
131 templ
.last_level
= 0;
132 templ
.width0
= dri2surf
->width
;
133 templ
.height0
= dri2surf
->height
;
135 templ
.format
= dri2surf
->color_format
;
136 templ
.tex_usage
= PIPE_TEXTURE_USAGE_RENDER_TARGET
;
139 for (i
= 0; i
< num_xbufs
; i
++) {
140 struct x11_drawable_buffer
*xbuf
= &xbufs
[i
];
142 enum native_attachment natt
;
144 switch (xbuf
->attachment
) {
145 case DRI2BufferFrontLeft
:
146 natt
= NATIVE_ATTACHMENT_FRONT_LEFT
;
147 desc
= "DRI2 Front Buffer";
149 case DRI2BufferFakeFrontLeft
:
150 natt
= NATIVE_ATTACHMENT_FRONT_LEFT
;
151 desc
= "DRI2 Fake Front Buffer";
152 dri2surf
->have_fake
= TRUE
;
154 case DRI2BufferBackLeft
:
155 natt
= NATIVE_ATTACHMENT_BACK_LEFT
;
156 desc
= "DRI2 Back Buffer";
157 dri2surf
->have_back
= TRUE
;
164 if (!desc
|| dri2surf
->textures
[natt
]) {
166 _eglLog(_EGL_WARNING
, "unknown buffer %d", xbuf
->attachment
);
168 _eglLog(_EGL_WARNING
, "both real and fake front buffers are listed");
172 dri2surf
->textures
[natt
] =
173 dri2dpy
->api
->texture_from_shared_handle(dri2dpy
->api
,
174 dri2dpy
->base
.screen
, &templ
, desc
, xbuf
->pitch
, xbuf
->name
);
175 if (dri2surf
->textures
[natt
])
176 valid_mask
|= 1 << natt
;
179 dri2surf
->valid_mask
= valid_mask
;
183 * Get the buffers from the server.
186 dri2_surface_get_buffers(struct native_surface
*nsurf
, uint buffer_mask
)
188 struct dri2_surface
*dri2surf
= dri2_surface(nsurf
);
189 struct dri2_display
*dri2dpy
= dri2surf
->dri2dpy
;
190 unsigned int dri2atts
[NUM_NATIVE_ATTACHMENTS
];
191 int num_ins
, num_outs
, att
;
192 struct x11_drawable_buffer
*xbufs
;
194 /* prepare the attachments */
196 for (att
= 0; att
< NUM_NATIVE_ATTACHMENTS
; att
++) {
197 if (native_attachment_mask_test(buffer_mask
, att
)) {
198 unsigned int dri2att
;
201 case NATIVE_ATTACHMENT_FRONT_LEFT
:
202 dri2att
= DRI2BufferFrontLeft
;
204 case NATIVE_ATTACHMENT_BACK_LEFT
:
205 dri2att
= DRI2BufferBackLeft
;
207 case NATIVE_ATTACHMENT_FRONT_RIGHT
:
208 dri2att
= DRI2BufferFrontRight
;
210 case NATIVE_ATTACHMENT_BACK_RIGHT
:
211 dri2att
= DRI2BufferBackRight
;
219 dri2atts
[num_ins
] = dri2att
;
224 xbufs
= x11_drawable_get_buffers(dri2dpy
->xscr
, dri2surf
->drawable
,
225 &dri2surf
->width
, &dri2surf
->height
,
226 dri2atts
, FALSE
, num_ins
, &num_outs
);
228 /* we should be able to do better... */
229 if (xbufs
&& dri2surf
->last_num_xbufs
== num_outs
&&
230 memcmp(dri2surf
->last_xbufs
, xbufs
, sizeof(*xbufs
) * num_outs
) == 0) {
232 dri2surf
->client_stamp
= dri2surf
->server_stamp
;
236 dri2_surface_process_drawable_buffers(&dri2surf
->base
, xbufs
, num_outs
);
238 dri2surf
->server_stamp
++;
239 dri2surf
->client_stamp
= dri2surf
->server_stamp
;
241 if (dri2surf
->last_xbufs
)
242 free(dri2surf
->last_xbufs
);
243 dri2surf
->last_xbufs
= xbufs
;
244 dri2surf
->last_num_xbufs
= num_outs
;
248 * Update the buffers of the surface. This is a slow function due to the
249 * round-trip to the server.
252 dri2_surface_update_buffers(struct native_surface
*nsurf
, uint buffer_mask
)
254 struct dri2_surface
*dri2surf
= dri2_surface(nsurf
);
255 struct dri2_display
*dri2dpy
= dri2surf
->dri2dpy
;
257 /* create textures for pbuffer */
258 if (dri2surf
->type
== DRI2_SURFACE_TYPE_PBUFFER
) {
259 struct pipe_screen
*screen
= dri2dpy
->base
.screen
;
260 struct pipe_texture templ
;
261 uint new_valid
= 0x0;
264 buffer_mask
&= ~dri2surf
->valid_mask
;
268 memset(&templ
, 0, sizeof(templ
));
269 templ
.target
= PIPE_TEXTURE_2D
;
270 templ
.last_level
= 0;
271 templ
.width0
= dri2surf
->width
;
272 templ
.height0
= dri2surf
->height
;
274 templ
.format
= dri2surf
->color_format
;
275 templ
.tex_usage
= PIPE_TEXTURE_USAGE_RENDER_TARGET
;
277 for (att
= 0; att
< NUM_NATIVE_ATTACHMENTS
; att
++) {
278 if (native_attachment_mask_test(buffer_mask
, att
)) {
279 assert(!dri2surf
->textures
[att
]);
281 dri2surf
->textures
[att
] = screen
->texture_create(screen
, &templ
);
282 if (!dri2surf
->textures
[att
])
285 new_valid
|= 1 << att
;
286 if (new_valid
== buffer_mask
)
290 dri2surf
->valid_mask
|= new_valid
;
291 /* no need to update the stamps */
294 dri2_surface_get_buffers(&dri2surf
->base
, buffer_mask
);
297 return ((dri2surf
->valid_mask
& buffer_mask
) == buffer_mask
);
301 * Return TRUE if the surface receives DRI2_InvalidateBuffers events.
303 static INLINE boolean
304 dri2_surface_receive_events(struct native_surface
*nsurf
)
306 struct dri2_surface
*dri2surf
= dri2_surface(nsurf
);
307 return (dri2surf
->dri2dpy
->dri_minor
>= 3);
311 dri2_surface_flush_frontbuffer(struct native_surface
*nsurf
)
313 struct dri2_surface
*dri2surf
= dri2_surface(nsurf
);
314 struct dri2_display
*dri2dpy
= dri2surf
->dri2dpy
;
316 /* pbuffer is private */
317 if (dri2surf
->type
== DRI2_SURFACE_TYPE_PBUFFER
)
320 /* copy to real front buffer */
321 if (dri2surf
->have_fake
)
322 x11_drawable_copy_buffers(dri2dpy
->xscr
, dri2surf
->drawable
,
323 0, 0, dri2surf
->width
, dri2surf
->height
,
324 DRI2BufferFakeFrontLeft
, DRI2BufferFrontLeft
);
326 /* force buffers to be updated in next validation call */
327 if (!dri2_surface_receive_events(&dri2surf
->base
))
328 dri2surf
->server_stamp
++;
334 dri2_surface_swap_buffers(struct native_surface
*nsurf
)
336 struct dri2_surface
*dri2surf
= dri2_surface(nsurf
);
337 struct dri2_display
*dri2dpy
= dri2surf
->dri2dpy
;
339 /* pbuffer is private */
340 if (dri2surf
->type
== DRI2_SURFACE_TYPE_PBUFFER
)
343 /* copy to front buffer */
344 if (dri2surf
->have_back
)
345 x11_drawable_copy_buffers(dri2dpy
->xscr
, dri2surf
->drawable
,
346 0, 0, dri2surf
->width
, dri2surf
->height
,
347 DRI2BufferBackLeft
, DRI2BufferFrontLeft
);
349 /* and update fake front buffer */
350 if (dri2surf
->have_fake
)
351 x11_drawable_copy_buffers(dri2dpy
->xscr
, dri2surf
->drawable
,
352 0, 0, dri2surf
->width
, dri2surf
->height
,
353 DRI2BufferFrontLeft
, DRI2BufferFakeFrontLeft
);
355 /* force buffers to be updated in next validation call */
356 if (!dri2_surface_receive_events(&dri2surf
->base
))
357 dri2surf
->server_stamp
++;
363 dri2_surface_validate(struct native_surface
*nsurf
, uint attachment_mask
,
364 unsigned int *seq_num
, struct pipe_texture
**textures
,
365 int *width
, int *height
)
367 struct dri2_surface
*dri2surf
= dri2_surface(nsurf
);
369 if (dri2surf
->server_stamp
!= dri2surf
->client_stamp
||
370 (dri2surf
->valid_mask
& attachment_mask
) != attachment_mask
) {
371 if (!dri2_surface_update_buffers(&dri2surf
->base
, attachment_mask
))
376 *seq_num
= dri2surf
->client_stamp
;
380 for (att
= 0; att
< NUM_NATIVE_ATTACHMENTS
; att
++) {
381 if (native_attachment_mask_test(attachment_mask
, att
)) {
382 struct pipe_texture
*ptex
= dri2surf
->textures
[att
];
384 textures
[att
] = NULL
;
385 pipe_texture_reference(&textures
[att
], ptex
);
391 *width
= dri2surf
->width
;
393 *height
= dri2surf
->height
;
399 dri2_surface_wait(struct native_surface
*nsurf
)
401 struct dri2_surface
*dri2surf
= dri2_surface(nsurf
);
402 struct dri2_display
*dri2dpy
= dri2surf
->dri2dpy
;
404 if (dri2surf
->have_fake
) {
405 x11_drawable_copy_buffers(dri2dpy
->xscr
, dri2surf
->drawable
,
406 0, 0, dri2surf
->width
, dri2surf
->height
,
407 DRI2BufferFrontLeft
, DRI2BufferFakeFrontLeft
);
412 dri2_surface_destroy(struct native_surface
*nsurf
)
414 struct dri2_surface
*dri2surf
= dri2_surface(nsurf
);
417 if (dri2surf
->last_xbufs
)
418 free(dri2surf
->last_xbufs
);
420 for (i
= 0; i
< NUM_NATIVE_ATTACHMENTS
; i
++) {
421 struct pipe_texture
*ptex
= dri2surf
->textures
[i
];
422 pipe_texture_reference(&ptex
, NULL
);
425 if (dri2surf
->drawable
) {
426 x11_drawable_enable_dri2(dri2surf
->dri2dpy
->xscr
,
427 dri2surf
->drawable
, FALSE
);
429 util_hash_table_remove(dri2surf
->dri2dpy
->surfaces
,
430 (void *) dri2surf
->drawable
);
435 static struct dri2_surface
*
436 dri2_display_create_surface(struct native_display
*ndpy
,
437 enum dri2_surface_type type
,
439 const struct native_config
*nconf
)
441 struct dri2_display
*dri2dpy
= dri2_display(ndpy
);
442 struct dri2_config
*dri2conf
= dri2_config(nconf
);
443 struct dri2_surface
*dri2surf
;
445 dri2surf
= CALLOC_STRUCT(dri2_surface
);
449 dri2surf
->dri2dpy
= dri2dpy
;
450 dri2surf
->type
= type
;
451 dri2surf
->drawable
= drawable
;
452 dri2surf
->color_format
= dri2conf
->base
.color_format
;
454 dri2surf
->base
.destroy
= dri2_surface_destroy
;
455 dri2surf
->base
.swap_buffers
= dri2_surface_swap_buffers
;
456 dri2surf
->base
.flush_frontbuffer
= dri2_surface_flush_frontbuffer
;
457 dri2surf
->base
.validate
= dri2_surface_validate
;
458 dri2surf
->base
.wait
= dri2_surface_wait
;
461 x11_drawable_enable_dri2(dri2dpy
->xscr
, drawable
, TRUE
);
462 /* initialize the geometry */
463 dri2_surface_update_buffers(&dri2surf
->base
, 0x0);
465 util_hash_table_set(dri2surf
->dri2dpy
->surfaces
,
466 (void *) dri2surf
->drawable
, (void *) &dri2surf
->base
);
472 static struct native_surface
*
473 dri2_display_create_window_surface(struct native_display
*ndpy
,
474 EGLNativeWindowType win
,
475 const struct native_config
*nconf
)
477 struct dri2_surface
*dri2surf
;
479 dri2surf
= dri2_display_create_surface(ndpy
, DRI2_SURFACE_TYPE_WINDOW
,
480 (Drawable
) win
, nconf
);
481 return (dri2surf
) ? &dri2surf
->base
: NULL
;
484 static struct native_surface
*
485 dri2_display_create_pixmap_surface(struct native_display
*ndpy
,
486 EGLNativePixmapType pix
,
487 const struct native_config
*nconf
)
489 struct dri2_surface
*dri2surf
;
491 dri2surf
= dri2_display_create_surface(ndpy
, DRI2_SURFACE_TYPE_PIXMAP
,
492 (Drawable
) pix
, nconf
);
493 return (dri2surf
) ? &dri2surf
->base
: NULL
;
496 static struct native_surface
*
497 dri2_display_create_pbuffer_surface(struct native_display
*ndpy
,
498 const struct native_config
*nconf
,
499 uint width
, uint height
)
501 struct dri2_surface
*dri2surf
;
503 dri2surf
= dri2_display_create_surface(ndpy
, DRI2_SURFACE_TYPE_PBUFFER
,
504 (Drawable
) None
, nconf
);
506 dri2surf
->width
= width
;
507 dri2surf
->height
= height
;
509 return (dri2surf
) ? &dri2surf
->base
: NULL
;
513 choose_color_format(const __GLcontextModes
*mode
, enum pipe_format formats
[32])
517 switch (mode
->rgbBits
) {
519 formats
[count
++] = PIPE_FORMAT_A8R8G8B8_UNORM
;
520 formats
[count
++] = PIPE_FORMAT_B8G8R8A8_UNORM
;
523 formats
[count
++] = PIPE_FORMAT_X8R8G8B8_UNORM
;
524 formats
[count
++] = PIPE_FORMAT_B8G8R8X8_UNORM
;
525 formats
[count
++] = PIPE_FORMAT_A8R8G8B8_UNORM
;
526 formats
[count
++] = PIPE_FORMAT_B8G8R8A8_UNORM
;
529 formats
[count
++] = PIPE_FORMAT_R5G6B5_UNORM
;
539 choose_depth_stencil_format(const __GLcontextModes
*mode
,
540 enum pipe_format formats
[32])
544 switch (mode
->depthBits
) {
546 formats
[count
++] = PIPE_FORMAT_Z32_UNORM
;
549 if (mode
->stencilBits
) {
550 formats
[count
++] = PIPE_FORMAT_S8Z24_UNORM
;
551 formats
[count
++] = PIPE_FORMAT_Z24S8_UNORM
;
554 formats
[count
++] = PIPE_FORMAT_X8Z24_UNORM
;
555 formats
[count
++] = PIPE_FORMAT_Z24X8_UNORM
;
559 formats
[count
++] = PIPE_FORMAT_Z16_UNORM
;
569 is_format_supported(struct pipe_screen
*screen
,
570 enum pipe_format fmt
, boolean is_color
)
572 return screen
->is_format_supported(screen
, fmt
, PIPE_TEXTURE_2D
,
573 (is_color
) ? PIPE_TEXTURE_USAGE_RENDER_TARGET
:
574 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
, 0);
578 dri2_display_convert_config(struct native_display
*ndpy
,
579 const __GLcontextModes
*mode
,
580 struct native_config
*nconf
)
582 enum pipe_format formats
[32];
585 if (!(mode
->renderType
& GLX_RGBA_BIT
) || !mode
->rgbMode
)
588 /* skip single-buffered configs */
589 if (!mode
->doubleBufferMode
)
593 nconf
->mode
.renderType
= GLX_RGBA_BIT
;
594 nconf
->mode
.rgbMode
= TRUE
;
595 /* pbuffer is allocated locally and is always supported */
596 nconf
->mode
.drawableType
|= GLX_PBUFFER_BIT
;
597 /* the swap method is always copy */
598 nconf
->mode
.swapMethod
= GLX_SWAP_COPY_OML
;
601 nconf
->mode
.rgbBits
=
602 nconf
->mode
.redBits
+ nconf
->mode
.greenBits
+
603 nconf
->mode
.blueBits
+ nconf
->mode
.alphaBits
;
604 if (!(nconf
->mode
.drawableType
& GLX_WINDOW_BIT
)) {
605 nconf
->mode
.visualID
= 0;
606 nconf
->mode
.visualType
= GLX_NONE
;
608 if (!(nconf
->mode
.drawableType
& GLX_PBUFFER_BIT
)) {
609 nconf
->mode
.bindToTextureRgb
= FALSE
;
610 nconf
->mode
.bindToTextureRgba
= FALSE
;
613 nconf
->color_format
= PIPE_FORMAT_NONE
;
614 nconf
->depth_format
= PIPE_FORMAT_NONE
;
615 nconf
->stencil_format
= PIPE_FORMAT_NONE
;
617 /* choose color format */
618 num_formats
= choose_color_format(mode
, formats
);
619 for (i
= 0; i
< num_formats
; i
++) {
620 if (is_format_supported(ndpy
->screen
, formats
[i
], TRUE
)) {
621 nconf
->color_format
= formats
[i
];
625 if (nconf
->color_format
== PIPE_FORMAT_NONE
)
628 /* choose depth/stencil format */
629 num_formats
= choose_depth_stencil_format(mode
, formats
);
630 for (i
= 0; i
< num_formats
; i
++) {
631 if (is_format_supported(ndpy
->screen
, formats
[i
], FALSE
)) {
632 nconf
->depth_format
= formats
[i
];
633 nconf
->stencil_format
= formats
[i
];
637 if ((nconf
->mode
.depthBits
&& nconf
->depth_format
== PIPE_FORMAT_NONE
) ||
638 (nconf
->mode
.stencilBits
&& nconf
->stencil_format
== PIPE_FORMAT_NONE
))
644 static const struct native_config
**
645 dri2_display_get_configs(struct native_display
*ndpy
, int *num_configs
)
647 struct dri2_display
*dri2dpy
= dri2_display(ndpy
);
648 const struct native_config
**configs
;
652 if (!dri2dpy
->configs
) {
653 const __GLcontextModes
*modes
;
654 int num_modes
, count
;
656 modes
= x11_screen_get_glx_configs(dri2dpy
->xscr
);
659 num_modes
= x11_context_modes_count(modes
);
661 dri2dpy
->configs
= calloc(num_modes
, sizeof(*dri2dpy
->configs
));
662 if (!dri2dpy
->configs
)
666 for (i
= 0; i
< num_modes
; i
++) {
667 struct native_config
*nconf
= &dri2dpy
->configs
[count
].base
;
668 if (dri2_display_convert_config(&dri2dpy
->base
, modes
, nconf
))
673 dri2dpy
->num_configs
= count
;
676 configs
= malloc(dri2dpy
->num_configs
* sizeof(*configs
));
678 for (i
= 0; i
< dri2dpy
->num_configs
; i
++)
679 configs
[i
] = (const struct native_config
*) &dri2dpy
->configs
[i
];
681 *num_configs
= dri2dpy
->num_configs
;
688 dri2_display_is_pixmap_supported(struct native_display
*ndpy
,
689 EGLNativePixmapType pix
,
690 const struct native_config
*nconf
)
692 struct dri2_display
*dri2dpy
= dri2_display(ndpy
);
693 uint depth
, nconf_depth
;
695 depth
= x11_drawable_get_depth(dri2dpy
->xscr
, (Drawable
) pix
);
696 nconf_depth
= util_format_get_blocksizebits(nconf
->color_format
);
698 /* simple depth match for now */
699 return (depth
== nconf_depth
|| (depth
== 24 && depth
+ 8 == nconf_depth
));
703 dri2_display_destroy(struct native_display
*ndpy
)
705 struct dri2_display
*dri2dpy
= dri2_display(ndpy
);
707 if (dri2dpy
->configs
)
708 free(dri2dpy
->configs
);
710 if (dri2dpy
->base
.screen
)
711 dri2dpy
->base
.screen
->destroy(dri2dpy
->base
.screen
);
713 if (dri2dpy
->surfaces
)
714 util_hash_table_destroy(dri2dpy
->surfaces
);
717 x11_screen_destroy(dri2dpy
->xscr
);
718 if (dri2dpy
->own_dpy
)
719 XCloseDisplay(dri2dpy
->dpy
);
720 if (dri2dpy
->api
&& dri2dpy
->api
->destroy
)
721 dri2dpy
->api
->destroy(dri2dpy
->api
);
726 dri2_display_invalidate_buffers(struct x11_screen
*xscr
, Drawable drawable
,
729 struct native_display
*ndpy
= (struct native_display
* ) user_data
;
730 struct dri2_display
*dri2dpy
= dri2_display(ndpy
);
731 struct native_surface
*nsurf
;
732 struct dri2_surface
*dri2surf
;
734 nsurf
= (struct native_surface
*)
735 util_hash_table_get(dri2dpy
->surfaces
, (void *) drawable
);
739 dri2surf
= dri2_surface(nsurf
);
740 dri2surf
->server_stamp
++;
744 * Initialize DRI2 and pipe screen.
747 dri2_display_init_screen(struct native_display
*ndpy
)
749 struct dri2_display
*dri2dpy
= dri2_display(ndpy
);
750 const char *driver
= dri2dpy
->api
->name
;
751 struct drm_create_screen_arg arg
;
754 if (!x11_screen_support(dri2dpy
->xscr
, X11_SCREEN_EXTENSION_DRI2
) ||
755 !x11_screen_support(dri2dpy
->xscr
, X11_SCREEN_EXTENSION_GLX
)) {
756 _eglLog(_EGL_WARNING
, "GLX/DRI2 is not supported");
760 dri2dpy
->dri_driver
= x11_screen_probe_dri2(dri2dpy
->xscr
,
761 &dri2dpy
->dri_major
, &dri2dpy
->dri_minor
);
762 if (!dri2dpy
->dri_driver
|| !driver
||
763 strcmp(dri2dpy
->dri_driver
, driver
) != 0) {
764 _eglLog(_EGL_WARNING
, "Driver mismatch: %s != %s",
765 dri2dpy
->dri_driver
, dri2dpy
->api
->name
);
769 fd
= x11_screen_enable_dri2(dri2dpy
->xscr
,
770 dri2_display_invalidate_buffers
, &dri2dpy
->base
);
774 memset(&arg
, 0, sizeof(arg
));
775 arg
.mode
= DRM_CREATE_NORMAL
;
776 dri2dpy
->base
.screen
= dri2dpy
->api
->create_screen(dri2dpy
->api
, fd
, &arg
);
777 if (!dri2dpy
->base
.screen
) {
778 _eglLog(_EGL_WARNING
, "failed to create DRM screen");
786 dri2_display_hash_table_hash(void *key
)
788 XID drawable
= pointer_to_uintptr(key
);
789 return (unsigned) drawable
;
793 dri2_display_hash_table_compare(void *key1
, void *key2
)
795 return (key1
- key2
);
798 struct native_display
*
799 x11_create_dri2_display(EGLNativeDisplayType dpy
, struct drm_api
*api
)
801 struct dri2_display
*dri2dpy
;
803 dri2dpy
= CALLOC_STRUCT(dri2_display
);
811 dri2dpy
->dpy
= XOpenDisplay(NULL
);
813 dri2_display_destroy(&dri2dpy
->base
);
816 dri2dpy
->own_dpy
= TRUE
;
819 dri2dpy
->xscr_number
= DefaultScreen(dri2dpy
->dpy
);
820 dri2dpy
->xscr
= x11_screen_create(dri2dpy
->dpy
, dri2dpy
->xscr_number
);
821 if (!dri2dpy
->xscr
) {
822 dri2_display_destroy(&dri2dpy
->base
);
826 if (!dri2_display_init_screen(&dri2dpy
->base
)) {
827 dri2_display_destroy(&dri2dpy
->base
);
831 dri2dpy
->surfaces
= util_hash_table_create(dri2_display_hash_table_hash
,
832 dri2_display_hash_table_compare
);
833 if (!dri2dpy
->surfaces
) {
834 dri2_display_destroy(&dri2dpy
->base
);
838 dri2dpy
->base
.destroy
= dri2_display_destroy
;
839 dri2dpy
->base
.get_configs
= dri2_display_get_configs
;
840 dri2dpy
->base
.is_pixmap_supported
= dri2_display_is_pixmap_supported
;
841 dri2dpy
->base
.create_window_surface
= dri2_display_create_window_surface
;
842 dri2dpy
->base
.create_pixmap_surface
= dri2_display_create_pixmap_surface
;
843 dri2dpy
->base
.create_pbuffer_surface
= dri2_display_create_pbuffer_surface
;
845 return &dri2dpy
->base
;