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 OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
28 #include <sys/types.h>
31 #include <X11/Xlibint.h>
32 #include <X11/extensions/XShm.h>
34 #include "util/u_memory.h"
37 #include "x11_screen.h"
42 #ifdef GLX_DIRECT_RENDERING
43 /* dummy base class */
44 struct __GLXDRIdisplayRec base
;
51 * This is used to fetch GLX visuals/fbconfigs. It steals code from GLX.
52 * It might be better to rewrite the part in Xlib or XCB.
54 __GLXdisplayPrivate
*glx_dpy
;
56 int dri_major
, dri_minor
;
61 x11_drawable_invalidate_buffers dri_invalidate_buffers
;
67 /* cached values for x11_drawable_get_depth */
68 Drawable last_drawable
;
69 unsigned int last_depth
;
74 * Create a X11 screen.
77 x11_screen_create(Display
*dpy
, int screen
)
79 struct x11_screen
*xscr
;
81 if (screen
>= ScreenCount(dpy
))
84 xscr
= CALLOC_STRUCT(x11_screen
);
87 xscr
->number
= screen
;
96 * Destroy a X11 screen.
99 x11_screen_destroy(struct x11_screen
*xscr
)
101 if (xscr
->dri_fd
>= 0)
103 if (xscr
->dri_driver
)
104 Xfree(xscr
->dri_driver
);
105 if (xscr
->dri_device
)
106 Xfree(xscr
->dri_device
);
108 #ifdef GLX_DIRECT_RENDERING
109 /* xscr->glx_dpy will be destroyed with the X display */
111 xscr
->glx_dpy
->dri2Display
= NULL
;
115 XFree(xscr
->visuals
);
119 #ifdef GLX_DIRECT_RENDERING
122 x11_screen_init_dri2(struct x11_screen
*xscr
)
124 if (xscr
->dri_major
< 0) {
125 int eventBase
, errorBase
;
127 if (!DRI2QueryExtension(xscr
->dpy
, &eventBase
, &errorBase
) ||
128 !DRI2QueryVersion(xscr
->dpy
, &xscr
->dri_major
, &xscr
->dri_minor
))
129 xscr
->dri_major
= -1;
131 return (xscr
->dri_major
>= 0);
135 x11_screen_init_glx(struct x11_screen
*xscr
)
138 xscr
->glx_dpy
= __glXInitialize(xscr
->dpy
);
139 return (xscr
->glx_dpy
!= NULL
);
142 #endif /* GLX_DIRECT_RENDERING */
145 * Return true if the screen supports the extension.
148 x11_screen_support(struct x11_screen
*xscr
, enum x11_screen_extension ext
)
150 boolean supported
= FALSE
;
153 case X11_SCREEN_EXTENSION_XSHM
:
154 supported
= XShmQueryExtension(xscr
->dpy
);
156 #ifdef GLX_DIRECT_RENDERING
157 case X11_SCREEN_EXTENSION_GLX
:
158 supported
= x11_screen_init_glx(xscr
);
160 case X11_SCREEN_EXTENSION_DRI2
:
161 supported
= x11_screen_init_dri2(xscr
);
172 * Return the X visuals.
175 x11_screen_get_visuals(struct x11_screen
*xscr
, int *num_visuals
)
177 if (!xscr
->visuals
) {
178 XVisualInfo vinfo_template
;
179 vinfo_template
.screen
= xscr
->number
;
180 xscr
->visuals
= XGetVisualInfo(xscr
->dpy
, VisualScreenMask
,
181 &vinfo_template
, &xscr
->num_visuals
);
185 *num_visuals
= xscr
->num_visuals
;
186 return xscr
->visuals
;
190 * Return the depth of a drawable.
192 * Unlike other drawable functions, the drawable needs not be a DRI2 drawable.
195 x11_drawable_get_depth(struct x11_screen
*xscr
, Drawable drawable
)
199 if (drawable
!= xscr
->last_drawable
) {
202 unsigned int w
, h
, border
;
205 ok
= XGetGeometry(xscr
->dpy
, drawable
, &root
,
206 &x
, &y
, &w
, &h
, &border
, &depth
);
210 xscr
->last_drawable
= drawable
;
211 xscr
->last_depth
= depth
;
214 depth
= xscr
->last_depth
;
220 #ifdef GLX_DIRECT_RENDERING
223 * Return the GLX fbconfigs.
225 const __GLcontextModes
*
226 x11_screen_get_glx_configs(struct x11_screen
*xscr
)
228 return (x11_screen_init_glx(xscr
))
229 ? xscr
->glx_dpy
->screenConfigs
[xscr
->number
]->configs
234 * Return the GLX visuals.
236 const __GLcontextModes
*
237 x11_screen_get_glx_visuals(struct x11_screen
*xscr
)
239 return (x11_screen_init_glx(xscr
))
240 ? xscr
->glx_dpy
->screenConfigs
[xscr
->number
]->visuals
245 * Probe the screen for the DRI2 driver name.
248 x11_screen_probe_dri2(struct x11_screen
*xscr
, int *major
, int *minor
)
250 if (!x11_screen_init_dri2(xscr
))
253 /* get the driver name and the device name */
254 if (!xscr
->dri_driver
) {
255 if (!DRI2Connect(xscr
->dpy
, RootWindow(xscr
->dpy
, xscr
->number
),
256 &xscr
->dri_driver
, &xscr
->dri_device
))
257 xscr
->dri_driver
= xscr
->dri_device
= NULL
;
260 *major
= xscr
->dri_major
;
262 *minor
= xscr
->dri_minor
;
264 return xscr
->dri_driver
;
268 * Enable DRI2 and returns the file descriptor of the DRM device. The file
269 * descriptor will be closed automatically when the screen is destoryed.
272 x11_screen_enable_dri2(struct x11_screen
*xscr
,
273 x11_drawable_invalidate_buffers invalidate_buffers
,
276 if (xscr
->dri_fd
< 0) {
280 /* get the driver name and the device name first */
281 if (!x11_screen_probe_dri2(xscr
, NULL
, NULL
))
284 fd
= open(xscr
->dri_device
, O_RDWR
);
286 _eglLog(_EGL_WARNING
, "failed to open %s", xscr
->dri_device
);
290 memset(&magic
, 0, sizeof(magic
));
291 if (drmGetMagic(fd
, &magic
)) {
292 _eglLog(_EGL_WARNING
, "failed to get magic");
297 if (!DRI2Authenticate(xscr
->dpy
,
298 RootWindow(xscr
->dpy
, xscr
->number
), magic
)) {
299 _eglLog(_EGL_WARNING
, "failed to authenticate magic");
304 if (!x11_screen_init_glx(xscr
)) {
305 _eglLog(_EGL_WARNING
, "failed to initialize GLX");
309 if (xscr
->glx_dpy
->dri2Display
) {
310 _eglLog(_EGL_WARNING
,
311 "display is already managed by another x11 screen");
316 xscr
->glx_dpy
->dri2Display
= (__GLXDRIdisplay
*) xscr
;
317 xscr
->dri_invalidate_buffers
= invalidate_buffers
;
318 xscr
->dri_user_data
= user_data
;
327 * Create/Destroy the DRI drawable.
330 x11_drawable_enable_dri2(struct x11_screen
*xscr
,
331 Drawable drawable
, boolean on
)
334 DRI2CreateDrawable(xscr
->dpy
, drawable
);
336 DRI2DestroyDrawable(xscr
->dpy
, drawable
);
340 * Copy between buffers of the DRI2 drawable.
343 x11_drawable_copy_buffers(struct x11_screen
*xscr
, Drawable drawable
,
344 int x
, int y
, int width
, int height
,
345 int src_buf
, int dst_buf
)
348 XserverRegion region
;
353 rect
.height
= height
;
355 region
= XFixesCreateRegion(xscr
->dpy
, &rect
, 1);
356 DRI2CopyRegion(xscr
->dpy
, drawable
, region
, dst_buf
, src_buf
);
357 XFixesDestroyRegion(xscr
->dpy
, region
);
361 * Get the buffers of the DRI2 drawable. The returned array should be freed.
363 struct x11_drawable_buffer
*
364 x11_drawable_get_buffers(struct x11_screen
*xscr
, Drawable drawable
,
365 int *width
, int *height
, unsigned int *attachments
,
366 boolean with_format
, int num_ins
, int *num_outs
)
368 DRI2Buffer
*dri2bufs
;
371 dri2bufs
= DRI2GetBuffersWithFormat(xscr
->dpy
, drawable
, width
, height
,
372 attachments
, num_ins
, num_outs
);
374 dri2bufs
= DRI2GetBuffers(xscr
->dpy
, drawable
, width
, height
,
375 attachments
, num_ins
, num_outs
);
377 return (struct x11_drawable_buffer
*) dri2bufs
;
381 * Create a mode list of the given size.
384 x11_context_modes_create(unsigned count
)
386 const size_t size
= sizeof(__GLcontextModes
);
387 __GLcontextModes
*base
= NULL
;
388 __GLcontextModes
**next
;
392 for (i
= 0; i
< count
; i
++) {
393 *next
= (__GLcontextModes
*) CALLOC(1, size
);
395 x11_context_modes_destroy(base
);
399 next
= &((*next
)->next
);
406 * Destroy a mode list.
409 x11_context_modes_destroy(__GLcontextModes
*modes
)
411 while (modes
!= NULL
) {
412 __GLcontextModes
*next
= modes
->next
;
419 * Return the number of the modes in the mode list.
422 x11_context_modes_count(const __GLcontextModes
*modes
)
424 const __GLcontextModes
*mode
;
426 for (mode
= modes
; mode
; mode
= mode
->next
)
432 * This is called from src/glx/dri2.c.
435 dri2InvalidateBuffers(Display
*dpy
, XID drawable
)
437 __GLXdisplayPrivate
*priv
= __glXInitialize(dpy
);
438 struct x11_screen
*xscr
= NULL
;
440 if (priv
&& priv
->dri2Display
)
441 xscr
= (struct x11_screen
*) priv
->dri2Display
;
442 if (!xscr
|| !xscr
->dri_invalidate_buffers
)
445 xscr
->dri_invalidate_buffers(xscr
, drawable
, xscr
->dri_user_data
);
448 #endif /* GLX_DIRECT_RENDERING */