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 "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"
36 #include "native_x11.h"
37 #include "x11_screen.h"
39 enum dri2_surface_type
{
40 DRI2_SURFACE_TYPE_WINDOW
,
41 DRI2_SURFACE_TYPE_PIXMAP
,
42 DRI2_SURFACE_TYPE_PBUFFER
46 struct native_display base
;
51 struct x11_screen
*xscr
;
54 struct dri2_config
*configs
;
59 struct native_surface base
;
61 enum dri2_surface_type type
;
62 enum pipe_format color_format
;
63 struct dri2_display
*dri2dpy
;
65 struct pipe_texture
*pbuffer_textures
[NUM_NATIVE_ATTACHMENTS
];
66 boolean have_back
, have_fake
;
68 unsigned int sequence_number
;
72 struct native_config base
;
75 static INLINE
struct dri2_display
*
76 dri2_display(const struct native_display
*ndpy
)
78 return (struct dri2_display
*) ndpy
;
81 static INLINE
struct dri2_surface
*
82 dri2_surface(const struct native_surface
*nsurf
)
84 return (struct dri2_surface
*) nsurf
;
87 static INLINE
struct dri2_config
*
88 dri2_config(const struct native_config
*nconf
)
90 return (struct dri2_config
*) nconf
;
94 dri2_surface_flush_frontbuffer(struct native_surface
*nsurf
)
96 struct dri2_surface
*dri2surf
= dri2_surface(nsurf
);
97 struct dri2_display
*dri2dpy
= dri2surf
->dri2dpy
;
99 /* pbuffer is private */
100 if (dri2surf
->type
== DRI2_SURFACE_TYPE_PBUFFER
)
103 /* copy to real front buffer */
104 if (dri2surf
->have_fake
)
105 x11_drawable_copy_buffers(dri2dpy
->xscr
, dri2surf
->drawable
,
106 0, 0, dri2surf
->width
, dri2surf
->height
,
107 DRI2BufferFakeFrontLeft
, DRI2BufferFrontLeft
);
113 dri2_surface_swap_buffers(struct native_surface
*nsurf
)
115 struct dri2_surface
*dri2surf
= dri2_surface(nsurf
);
116 struct dri2_display
*dri2dpy
= dri2surf
->dri2dpy
;
118 /* pbuffer is private */
119 if (dri2surf
->type
== DRI2_SURFACE_TYPE_PBUFFER
)
122 /* copy to front buffer */
123 if (dri2surf
->have_back
)
124 x11_drawable_copy_buffers(dri2dpy
->xscr
, dri2surf
->drawable
,
125 0, 0, dri2surf
->width
, dri2surf
->height
,
126 DRI2BufferBackLeft
, DRI2BufferFrontLeft
);
128 /* and update fake front buffer */
129 if (dri2surf
->have_fake
)
130 x11_drawable_copy_buffers(dri2dpy
->xscr
, dri2surf
->drawable
,
131 0, 0, dri2surf
->width
, dri2surf
->height
,
132 DRI2BufferFrontLeft
, DRI2BufferFakeFrontLeft
);
138 dri2_surface_validate(struct native_surface
*nsurf
, uint attachment_mask
,
139 unsigned int *seq_num
, struct pipe_texture
**textures
,
140 int *width
, int *height
)
142 struct dri2_surface
*dri2surf
= dri2_surface(nsurf
);
143 struct dri2_display
*dri2dpy
= dri2surf
->dri2dpy
;
144 unsigned int dri2atts
[NUM_NATIVE_ATTACHMENTS
];
145 struct pipe_texture templ
;
146 struct x11_drawable_buffer
*xbufs
;
147 int num_ins
, num_outs
, att
, i
;
149 if (attachment_mask
) {
150 memset(&templ
, 0, sizeof(templ
));
151 templ
.target
= PIPE_TEXTURE_2D
;
152 templ
.last_level
= 0;
153 templ
.width0
= dri2surf
->width
;
154 templ
.height0
= dri2surf
->height
;
156 templ
.format
= dri2surf
->color_format
;
157 templ
.tex_usage
= PIPE_TEXTURE_USAGE_RENDER_TARGET
;
160 memset(textures
, 0, sizeof(*textures
) * NUM_NATIVE_ATTACHMENTS
);
163 /* create textures for pbuffer */
164 if (dri2surf
->type
== DRI2_SURFACE_TYPE_PBUFFER
) {
165 struct pipe_screen
*screen
= dri2dpy
->base
.screen
;
167 for (att
= 0; att
< NUM_NATIVE_ATTACHMENTS
; att
++) {
168 struct pipe_texture
*ptex
= dri2surf
->pbuffer_textures
[att
];
170 /* delay the allocation */
171 if (!native_attachment_mask_test(attachment_mask
, att
))
175 ptex
= screen
->texture_create(screen
, &templ
);
176 dri2surf
->pbuffer_textures
[att
] = ptex
;
180 pipe_texture_reference(&textures
[att
], ptex
);
184 *seq_num
= dri2surf
->sequence_number
;
186 *width
= dri2surf
->width
;
188 *height
= dri2surf
->height
;
193 /* prepare the attachments */
195 for (att
= 0; att
< NUM_NATIVE_ATTACHMENTS
; att
++) {
196 if (native_attachment_mask_test(attachment_mask
, att
)) {
197 unsigned int dri2att
;
200 case NATIVE_ATTACHMENT_FRONT_LEFT
:
201 dri2att
= DRI2BufferFrontLeft
;
203 case NATIVE_ATTACHMENT_BACK_LEFT
:
204 dri2att
= DRI2BufferBackLeft
;
206 case NATIVE_ATTACHMENT_FRONT_RIGHT
:
207 dri2att
= DRI2BufferFrontRight
;
209 case NATIVE_ATTACHMENT_BACK_RIGHT
:
210 dri2att
= DRI2BufferBackRight
;
218 dri2atts
[num_ins
] = dri2att
;
223 dri2surf
->have_back
= FALSE
;
224 dri2surf
->have_fake
= FALSE
;
226 /* remember old geometry */
227 templ
.width0
= dri2surf
->width
;
228 templ
.height0
= dri2surf
->height
;
230 xbufs
= x11_drawable_get_buffers(dri2dpy
->xscr
, dri2surf
->drawable
,
231 &dri2surf
->width
, &dri2surf
->height
,
232 dri2atts
, FALSE
, num_ins
, &num_outs
);
236 if (templ
.width0
!= dri2surf
->width
|| templ
.height0
!= dri2surf
->height
) {
237 /* are there cases where the buffers change and the geometry doesn't? */
238 dri2surf
->sequence_number
++;
240 templ
.width0
= dri2surf
->width
;
241 templ
.height0
= dri2surf
->height
;
244 for (i
= 0; i
< num_outs
; i
++) {
245 struct x11_drawable_buffer
*xbuf
= &xbufs
[i
];
247 enum native_attachment natt
;
249 switch (xbuf
->attachment
) {
250 case DRI2BufferFrontLeft
:
251 natt
= NATIVE_ATTACHMENT_FRONT_LEFT
;
252 desc
= "DRI2 Front Buffer";
254 case DRI2BufferFakeFrontLeft
:
255 natt
= NATIVE_ATTACHMENT_FRONT_LEFT
;
256 desc
= "DRI2 Fake Front Buffer";
257 dri2surf
->have_fake
= TRUE
;
259 case DRI2BufferBackLeft
:
260 natt
= NATIVE_ATTACHMENT_BACK_LEFT
;
261 desc
= "DRI2 Back Buffer";
262 dri2surf
->have_back
= TRUE
;
269 if (!desc
|| !native_attachment_mask_test(attachment_mask
, natt
) ||
270 (textures
&& textures
[natt
])) {
272 _eglLog(_EGL_WARNING
, "unknown buffer %d", xbuf
->attachment
);
273 else if (!native_attachment_mask_test(attachment_mask
, natt
))
274 _eglLog(_EGL_WARNING
, "unexpected buffer %d", xbuf
->attachment
);
276 _eglLog(_EGL_WARNING
, "both real and fake front buffers are listed");
281 struct pipe_texture
*ptex
=
282 dri2dpy
->api
->texture_from_shared_handle(dri2dpy
->api
,
283 dri2dpy
->base
.screen
, &templ
,
284 desc
, xbuf
->pitch
, xbuf
->name
);
286 /* the caller owns the textures */
287 textures
[natt
] = ptex
;
295 *seq_num
= dri2surf
->sequence_number
;
297 *width
= dri2surf
->width
;
299 *height
= dri2surf
->height
;
305 dri2_surface_wait(struct native_surface
*nsurf
)
307 struct dri2_surface
*dri2surf
= dri2_surface(nsurf
);
308 struct dri2_display
*dri2dpy
= dri2surf
->dri2dpy
;
310 if (dri2surf
->have_fake
) {
311 x11_drawable_copy_buffers(dri2dpy
->xscr
, dri2surf
->drawable
,
312 0, 0, dri2surf
->width
, dri2surf
->height
,
313 DRI2BufferFrontLeft
, DRI2BufferFakeFrontLeft
);
318 dri2_surface_destroy(struct native_surface
*nsurf
)
320 struct dri2_surface
*dri2surf
= dri2_surface(nsurf
);
323 for (i
= 0; i
< NUM_NATIVE_ATTACHMENTS
; i
++) {
324 struct pipe_texture
*ptex
= dri2surf
->pbuffer_textures
[i
];
325 pipe_texture_reference(&ptex
, NULL
);
328 if (dri2surf
->drawable
)
329 x11_drawable_enable_dri2(dri2surf
->dri2dpy
->xscr
,
330 dri2surf
->drawable
, FALSE
);
334 static struct dri2_surface
*
335 dri2_display_create_surface(struct native_display
*ndpy
,
336 enum dri2_surface_type type
,
338 const struct native_config
*nconf
)
340 struct dri2_display
*dri2dpy
= dri2_display(ndpy
);
341 struct dri2_config
*dri2conf
= dri2_config(nconf
);
342 struct dri2_surface
*dri2surf
;
344 dri2surf
= CALLOC_STRUCT(dri2_surface
);
349 x11_drawable_enable_dri2(dri2dpy
->xscr
, drawable
, TRUE
);
351 dri2surf
->dri2dpy
= dri2dpy
;
352 dri2surf
->type
= type
;
353 dri2surf
->drawable
= drawable
;
354 dri2surf
->color_format
= dri2conf
->base
.color_format
;
356 dri2surf
->base
.destroy
= dri2_surface_destroy
;
357 dri2surf
->base
.swap_buffers
= dri2_surface_swap_buffers
;
358 dri2surf
->base
.flush_frontbuffer
= dri2_surface_flush_frontbuffer
;
359 dri2surf
->base
.validate
= dri2_surface_validate
;
360 dri2surf
->base
.wait
= dri2_surface_wait
;
365 static struct native_surface
*
366 dri2_display_create_window_surface(struct native_display
*ndpy
,
367 EGLNativeWindowType win
,
368 const struct native_config
*nconf
)
370 struct dri2_surface
*dri2surf
;
372 dri2surf
= dri2_display_create_surface(ndpy
, DRI2_SURFACE_TYPE_WINDOW
,
373 (Drawable
) win
, nconf
);
374 return (dri2surf
) ? &dri2surf
->base
: NULL
;
377 static struct native_surface
*
378 dri2_display_create_pixmap_surface(struct native_display
*ndpy
,
379 EGLNativePixmapType pix
,
380 const struct native_config
*nconf
)
382 struct dri2_surface
*dri2surf
;
384 dri2surf
= dri2_display_create_surface(ndpy
, DRI2_SURFACE_TYPE_PIXMAP
,
385 (Drawable
) pix
, nconf
);
386 return (dri2surf
) ? &dri2surf
->base
: NULL
;
389 static struct native_surface
*
390 dri2_display_create_pbuffer_surface(struct native_display
*ndpy
,
391 const struct native_config
*nconf
,
392 uint width
, uint height
)
394 struct dri2_surface
*dri2surf
;
396 dri2surf
= dri2_display_create_surface(ndpy
, DRI2_SURFACE_TYPE_PBUFFER
,
397 (Drawable
) None
, nconf
);
399 dri2surf
->width
= width
;
400 dri2surf
->height
= height
;
402 return (dri2surf
) ? &dri2surf
->base
: NULL
;
406 choose_color_format(const __GLcontextModes
*mode
, enum pipe_format formats
[32])
410 switch (mode
->rgbBits
) {
412 formats
[count
++] = PIPE_FORMAT_A8R8G8B8_UNORM
;
413 formats
[count
++] = PIPE_FORMAT_B8G8R8A8_UNORM
;
416 formats
[count
++] = PIPE_FORMAT_X8R8G8B8_UNORM
;
417 formats
[count
++] = PIPE_FORMAT_B8G8R8X8_UNORM
;
418 formats
[count
++] = PIPE_FORMAT_A8R8G8B8_UNORM
;
419 formats
[count
++] = PIPE_FORMAT_B8G8R8A8_UNORM
;
422 formats
[count
++] = PIPE_FORMAT_R5G6B5_UNORM
;
432 choose_depth_stencil_format(const __GLcontextModes
*mode
,
433 enum pipe_format formats
[32])
437 switch (mode
->depthBits
) {
439 formats
[count
++] = PIPE_FORMAT_Z32_UNORM
;
442 if (mode
->stencilBits
) {
443 formats
[count
++] = PIPE_FORMAT_S8Z24_UNORM
;
444 formats
[count
++] = PIPE_FORMAT_Z24S8_UNORM
;
447 formats
[count
++] = PIPE_FORMAT_X8Z24_UNORM
;
448 formats
[count
++] = PIPE_FORMAT_Z24X8_UNORM
;
452 formats
[count
++] = PIPE_FORMAT_Z16_UNORM
;
462 is_format_supported(struct pipe_screen
*screen
,
463 enum pipe_format fmt
, boolean is_color
)
465 return screen
->is_format_supported(screen
, fmt
, PIPE_TEXTURE_2D
,
466 (is_color
) ? PIPE_TEXTURE_USAGE_RENDER_TARGET
:
467 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
, 0);
471 dri2_display_convert_config(struct native_display
*ndpy
,
472 const __GLcontextModes
*mode
,
473 struct native_config
*nconf
)
475 enum pipe_format formats
[32];
478 if (!(mode
->renderType
& GLX_RGBA_BIT
) || !mode
->rgbMode
)
481 /* skip single-buffered configs */
482 if (!mode
->doubleBufferMode
)
486 nconf
->mode
.renderType
= GLX_RGBA_BIT
;
487 nconf
->mode
.rgbMode
= TRUE
;
488 /* pbuffer is allocated locally and is always supported */
489 nconf
->mode
.drawableType
|= GLX_PBUFFER_BIT
;
490 /* the swap method is always copy */
491 nconf
->mode
.swapMethod
= GLX_SWAP_COPY_OML
;
494 nconf
->mode
.rgbBits
=
495 nconf
->mode
.redBits
+ nconf
->mode
.greenBits
+
496 nconf
->mode
.blueBits
+ nconf
->mode
.alphaBits
;
497 if (!(nconf
->mode
.drawableType
& GLX_WINDOW_BIT
)) {
498 nconf
->mode
.visualID
= 0;
499 nconf
->mode
.visualType
= GLX_NONE
;
501 if (!(nconf
->mode
.drawableType
& GLX_PBUFFER_BIT
)) {
502 nconf
->mode
.bindToTextureRgb
= FALSE
;
503 nconf
->mode
.bindToTextureRgba
= FALSE
;
506 nconf
->color_format
= PIPE_FORMAT_NONE
;
507 nconf
->depth_format
= PIPE_FORMAT_NONE
;
508 nconf
->stencil_format
= PIPE_FORMAT_NONE
;
510 /* choose color format */
511 num_formats
= choose_color_format(mode
, formats
);
512 for (i
= 0; i
< num_formats
; i
++) {
513 if (is_format_supported(ndpy
->screen
, formats
[i
], TRUE
)) {
514 nconf
->color_format
= formats
[i
];
518 if (nconf
->color_format
== PIPE_FORMAT_NONE
)
521 /* choose depth/stencil format */
522 num_formats
= choose_depth_stencil_format(mode
, formats
);
523 for (i
= 0; i
< num_formats
; i
++) {
524 if (is_format_supported(ndpy
->screen
, formats
[i
], FALSE
)) {
525 nconf
->depth_format
= formats
[i
];
526 nconf
->stencil_format
= formats
[i
];
530 if ((nconf
->mode
.depthBits
&& nconf
->depth_format
== PIPE_FORMAT_NONE
) ||
531 (nconf
->mode
.stencilBits
&& nconf
->stencil_format
== PIPE_FORMAT_NONE
))
537 static const struct native_config
**
538 dri2_display_get_configs(struct native_display
*ndpy
, int *num_configs
)
540 struct dri2_display
*dri2dpy
= dri2_display(ndpy
);
541 const struct native_config
**configs
;
545 if (!dri2dpy
->configs
) {
546 const __GLcontextModes
*modes
;
547 int num_modes
, count
;
549 modes
= x11_screen_get_glx_configs(dri2dpy
->xscr
);
552 num_modes
= x11_context_modes_count(modes
);
554 dri2dpy
->configs
= calloc(num_modes
, sizeof(*dri2dpy
->configs
));
555 if (!dri2dpy
->configs
)
559 for (i
= 0; i
< num_modes
; i
++) {
560 struct native_config
*nconf
= &dri2dpy
->configs
[count
].base
;
561 if (dri2_display_convert_config(&dri2dpy
->base
, modes
, nconf
))
566 dri2dpy
->num_configs
= count
;
569 configs
= malloc(dri2dpy
->num_configs
* sizeof(*configs
));
571 for (i
= 0; i
< dri2dpy
->num_configs
; i
++)
572 configs
[i
] = (const struct native_config
*) &dri2dpy
->configs
[i
];
574 *num_configs
= dri2dpy
->num_configs
;
581 dri2_display_is_pixmap_supported(struct native_display
*ndpy
,
582 EGLNativePixmapType pix
,
583 const struct native_config
*nconf
)
585 struct dri2_display
*dri2dpy
= dri2_display(ndpy
);
586 uint depth
, nconf_depth
;
588 depth
= x11_drawable_get_depth(dri2dpy
->xscr
, (Drawable
) pix
);
589 nconf_depth
= util_format_get_blocksizebits(nconf
->color_format
);
591 /* simple depth match for now */
592 return (depth
== nconf_depth
|| (depth
== 24 && depth
+ 8 == nconf_depth
));
596 dri2_display_destroy(struct native_display
*ndpy
)
598 struct dri2_display
*dri2dpy
= dri2_display(ndpy
);
600 if (dri2dpy
->configs
)
601 free(dri2dpy
->configs
);
603 if (dri2dpy
->base
.screen
)
604 dri2dpy
->base
.screen
->destroy(dri2dpy
->base
.screen
);
607 x11_screen_destroy(dri2dpy
->xscr
);
608 if (dri2dpy
->own_dpy
)
609 XCloseDisplay(dri2dpy
->dpy
);
610 if (dri2dpy
->api
&& dri2dpy
->api
->destroy
)
611 dri2dpy
->api
->destroy(dri2dpy
->api
);
616 * Initialize DRI2 and pipe screen.
619 dri2_display_init_screen(struct native_display
*ndpy
)
621 struct dri2_display
*dri2dpy
= dri2_display(ndpy
);
622 const char *driver
= dri2dpy
->api
->name
;
623 struct drm_create_screen_arg arg
;
626 if (!x11_screen_support(dri2dpy
->xscr
, X11_SCREEN_EXTENSION_DRI2
) ||
627 !x11_screen_support(dri2dpy
->xscr
, X11_SCREEN_EXTENSION_GLX
)) {
628 _eglLog(_EGL_WARNING
, "GLX/DRI2 is not supported");
632 fd
= x11_screen_enable_dri2(dri2dpy
->xscr
, driver
);
636 memset(&arg
, 0, sizeof(arg
));
637 arg
.mode
= DRM_CREATE_NORMAL
;
638 dri2dpy
->base
.screen
= dri2dpy
->api
->create_screen(dri2dpy
->api
, fd
, &arg
);
639 if (!dri2dpy
->base
.screen
) {
640 _eglLog(_EGL_WARNING
, "failed to create DRM screen");
647 struct native_display
*
648 x11_create_dri2_display(EGLNativeDisplayType dpy
, struct drm_api
*api
)
650 struct dri2_display
*dri2dpy
;
652 dri2dpy
= CALLOC_STRUCT(dri2_display
);
658 _eglLog(_EGL_WARNING
, "failed to create DRM API");
665 dri2dpy
->dpy
= XOpenDisplay(NULL
);
667 dri2_display_destroy(&dri2dpy
->base
);
670 dri2dpy
->own_dpy
= TRUE
;
673 dri2dpy
->xscr_number
= DefaultScreen(dri2dpy
->dpy
);
674 dri2dpy
->xscr
= x11_screen_create(dri2dpy
->dpy
, dri2dpy
->xscr_number
);
675 if (!dri2dpy
->xscr
) {
676 dri2_display_destroy(&dri2dpy
->base
);
680 if (!dri2_display_init_screen(&dri2dpy
->base
)) {
681 dri2_display_destroy(&dri2dpy
->base
);
685 dri2dpy
->base
.destroy
= dri2_display_destroy
;
686 dri2dpy
->base
.get_configs
= dri2_display_get_configs
;
687 dri2dpy
->base
.is_pixmap_supported
= dri2_display_is_pixmap_supported
;
688 dri2dpy
->base
.create_window_surface
= dri2_display_create_window_surface
;
689 dri2dpy
->base
.create_pixmap_surface
= dri2_display_create_pixmap_surface
;
690 dri2dpy
->base
.create_pbuffer_surface
= dri2_display_create_pbuffer_surface
;
692 return &dri2dpy
->base
;