1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
29 * EGL / softpipe / xlib winsys module
36 #include <X11/Xutil.h>
38 #include "pipe/p_compiler.h"
39 #include "pipe/p_format.h"
40 #include "pipe/p_state.h"
41 #include "pipe/p_util.h"
42 #include "pipe/p_winsys.h"
43 #include "softpipe/sp_winsys.h"
45 #include "eglconfig.h"
46 #include "eglconfigutil.h"
47 #include "eglcontext.h"
48 #include "egldisplay.h"
49 #include "egldriver.h"
50 #include "eglglobals.h"
52 #include "eglsurface.h"
54 #include "state_tracker/st_public.h"
56 #include "sw_winsys.h"
59 /** subclass of _EGLDriver */
60 struct xlib_egl_driver
62 _EGLDriver Base
; /**< base class */
64 struct pipe_winsys
*winsys
;
65 struct pipe_screen
*screen
;
69 /** subclass of _EGLContext */
70 struct xlib_egl_context
72 _EGLContext Base
; /**< base class */
74 struct pipe_context
*pipe
; /**< Gallium driver context */
75 struct st_context
*Context
; /**< Mesa/gallium state tracker context */
79 /** subclass of _EGLSurface */
80 struct xlib_egl_surface
82 _EGLSurface Base
; /**< base class */
84 Display
*Dpy
; /**< The X Display of the window */
85 Window Win
; /**< The user-created window ID */
89 struct pipe_winsys
*winsys
;
91 struct st_framebuffer
*Framebuffer
;
96 static INLINE
struct xlib_egl_driver
*
97 xlib_egl_driver(_EGLDriver
*drv
)
99 return (struct xlib_egl_driver
*) drv
;
103 static struct xlib_egl_surface
*
104 lookup_surface(EGLSurface surf
)
106 _EGLSurface
*surface
= _eglLookupSurface(surf
);
107 return (struct xlib_egl_surface
*) surface
;
111 static struct xlib_egl_context
*
112 lookup_context(EGLContext surf
)
114 _EGLContext
*context
= _eglLookupContext(surf
);
115 return (struct xlib_egl_context
*) context
;
120 bitcount(unsigned int n
)
123 for (bits
= 0; n
> 0; n
= n
>> 1) {
131 * Create the EGLConfigs. (one per X visual)
134 create_configs(_EGLDriver
*drv
, EGLDisplay dpy
)
136 _EGLDisplay
*disp
= _eglLookupDisplay(dpy
);
137 XVisualInfo
*visInfo
, visTemplate
;
140 /* get list of all X visuals, create an EGL config for each */
141 visTemplate
.screen
= DefaultScreen(disp
->Xdpy
);
142 visInfo
= XGetVisualInfo(disp
->Xdpy
, VisualScreenMask
,
143 &visTemplate
, &num_visuals
);
145 printf("egl_xlib.c: couldn't get any X visuals\n");
149 for (i
= 0; i
< num_visuals
; i
++) {
150 _EGLConfig
*config
= calloc(1, sizeof(_EGLConfig
));
152 int rbits
= bitcount(visInfo
[i
].red_mask
);
153 int gbits
= bitcount(visInfo
[i
].green_mask
);
154 int bbits
= bitcount(visInfo
[i
].blue_mask
);
155 int abits
= bbits
== 8 ? 8 : 0;
158 int visid
= visInfo
[i
].visualid
;
159 #if defined(__cplusplus) || defined(c_plusplus)
160 int vistype
= visInfo
[i
].c_class
;
162 int vistype
= visInfo
[i
].class;
165 _eglInitConfig(config
, id
);
166 SET_CONFIG_ATTRIB(config
, EGL_BUFFER_SIZE
, rbits
+ gbits
+ bbits
+ abits
);
167 SET_CONFIG_ATTRIB(config
, EGL_RED_SIZE
, rbits
);
168 SET_CONFIG_ATTRIB(config
, EGL_GREEN_SIZE
, gbits
);
169 SET_CONFIG_ATTRIB(config
, EGL_BLUE_SIZE
, bbits
);
170 SET_CONFIG_ATTRIB(config
, EGL_ALPHA_SIZE
, abits
);
171 SET_CONFIG_ATTRIB(config
, EGL_DEPTH_SIZE
, zbits
);
172 SET_CONFIG_ATTRIB(config
, EGL_STENCIL_SIZE
, sbits
);
173 SET_CONFIG_ATTRIB(config
, EGL_NATIVE_VISUAL_ID
, visid
);
174 SET_CONFIG_ATTRIB(config
, EGL_NATIVE_VISUAL_TYPE
, vistype
);
175 SET_CONFIG_ATTRIB(config
, EGL_NATIVE_RENDERABLE
, EGL_FALSE
);
177 _eglAddConfig(disp
, config
);
183 * Called via eglInitialize(), drv->API.Initialize().
186 xlib_eglInitialize(_EGLDriver
*drv
, EGLDisplay dpy
,
187 EGLint
*minor
, EGLint
*major
)
189 create_configs(drv
, dpy
);
191 drv
->Initialized
= EGL_TRUE
;
193 /* we're supporting EGL 1.4 */
202 * Called via eglTerminate(), drv->API.Terminate().
205 xlib_eglTerminate(_EGLDriver
*drv
, EGLDisplay dpy
)
212 xlib_eglGetProcAddress(const char *procname
)
214 return (_EGLProc
) st_get_proc_address(procname
);
219 get_drawable_visual_info(Display
*dpy
, Drawable d
, XVisualInfo
*visInfo
)
221 XWindowAttributes attr
;
222 XVisualInfo visTemp
, *vis
;
225 XGetWindowAttributes(dpy
, d
, &attr
);
227 visTemp
.screen
= DefaultScreen(dpy
);
228 visTemp
.visualid
= attr
.visual
->visualid
;
229 vis
= XGetVisualInfo(dpy
,
230 (VisualScreenMask
| VisualIDMask
),
231 &visTemp
, &num_visuals
);
240 /** Get size of given window */
242 get_drawable_size(Display
*dpy
, Drawable d
, uint
*width
, uint
*height
)
247 unsigned int w
, h
, bw
, depth
;
248 stat
= XGetGeometry(dpy
, d
, &root
, &xpos
, &ypos
, &w
, &h
, &bw
, &depth
);
256 check_and_update_buffer_size(struct xlib_egl_surface
*surface
)
259 get_drawable_size(surface
->Dpy
, surface
->Win
, &width
, &height
);
260 st_resize_framebuffer(surface
->Framebuffer
, width
, height
);
261 surface
->Base
.Width
= width
;
262 surface
->Base
.Height
= height
;
268 display_surface(struct pipe_winsys
*pws
,
269 struct pipe_surface
*psurf
,
270 struct xlib_egl_surface
*xsurf
)
275 ximage
= XCreateImage(xsurf
->Dpy
,
276 xsurf
->VisInfo
.visual
,
277 xsurf
->VisInfo
.depth
,
278 ZPixmap
, 0, /* format, offset */
282 0); /* bytes_per_line */
285 assert(ximage
->format
);
286 assert(ximage
->bitmap_unit
);
288 data
= pws
->buffer_map(pws
, psurf
->buffer
, 0);
290 /* update XImage's fields */
292 ximage
->width
= psurf
->width
;
293 ximage
->height
= psurf
->height
;
294 ximage
->bytes_per_line
= psurf
->stride
;
296 XPutImage(xsurf
->Dpy
, xsurf
->Win
, xsurf
->Gc
,
297 ximage
, 0, 0, 0, 0, psurf
->width
, psurf
->height
);
299 XSync(xsurf
->Dpy
, 0);
302 XDestroyImage(ximage
);
304 pws
->buffer_unmap(pws
, psurf
->buffer
);
309 /** Display gallium surface in X window */
311 flush_frontbuffer(struct pipe_winsys
*pws
,
312 struct pipe_surface
*psurf
,
313 void *context_private
)
315 struct xlib_egl_surface
*xsurf
= (struct xlib_egl_surface
*) context_private
;
316 display_surface(pws
, psurf
, xsurf
);
322 * Called via eglCreateContext(), drv->API.CreateContext().
325 xlib_eglCreateContext(_EGLDriver
*drv
, EGLDisplay dpy
, EGLConfig config
,
326 EGLContext share_list
, const EGLint
*attrib_list
)
328 struct xlib_egl_driver
*xdrv
= xlib_egl_driver(drv
);
329 _EGLConfig
*conf
= _eglLookupConfig(drv
, dpy
, config
);
330 struct xlib_egl_context
*ctx
;
331 struct st_context
*share_ctx
= NULL
; /* XXX fix */
332 __GLcontextModes visual
;
334 ctx
= CALLOC_STRUCT(xlib_egl_context
);
336 return EGL_NO_CONTEXT
;
338 /* let EGL lib init the common stuff */
339 if (!_eglInitContext(drv
, dpy
, &ctx
->Base
, config
, attrib_list
)) {
341 return EGL_NO_CONTEXT
;
344 /* API-dependent context creation */
345 switch (ctx
->Base
.ClientAPI
) {
346 case EGL_OPENGL_ES_API
:
347 _eglLog(_EGL_DEBUG
, "Create Context for ES version %d\n",
348 ctx
->Base
.ClientVersion
);
351 /* create a softpipe context */
352 ctx
->pipe
= softpipe_create(xdrv
->screen
, xdrv
->winsys
, NULL
);
353 /* Now do xlib / state tracker inits here */
354 _eglConfigToContextModesRec(conf
, &visual
);
355 ctx
->Context
= st_create_context(ctx
->pipe
, &visual
, share_ctx
);
358 _eglError(EGL_BAD_MATCH
, "eglCreateContext(unsupported API)");
360 return EGL_NO_CONTEXT
;
363 _eglSaveContext(&ctx
->Base
);
365 return _eglGetContextHandle(&ctx
->Base
);
370 xlib_eglDestroyContext(_EGLDriver
*drv
, EGLDisplay dpy
, EGLContext ctx
)
372 struct xlib_egl_context
*context
= lookup_context(ctx
);
374 if (context
->Base
.IsBound
) {
375 context
->Base
.DeletePending
= EGL_TRUE
;
378 /* API-dependent clean-up */
379 switch (context
->Base
.ClientAPI
) {
380 case EGL_OPENGL_ES_API
:
383 st_destroy_context(context
->Context
);
393 _eglError(EGL_BAD_CONTEXT
, "eglDestroyContext");
400 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
403 xlib_eglMakeCurrent(_EGLDriver
*drv
, EGLDisplay dpy
,
404 EGLSurface draw
, EGLSurface read
, EGLContext ctx
)
406 struct xlib_egl_context
*context
= lookup_context(ctx
);
407 struct xlib_egl_surface
*draw_surf
= lookup_surface(draw
);
408 struct xlib_egl_surface
*read_surf
= lookup_surface(read
);
410 if (!_eglMakeCurrent(drv
, dpy
, draw
, read
, context
))
413 st_make_current((context
? context
->Context
: NULL
),
414 (draw_surf
? draw_surf
->Framebuffer
: NULL
),
415 (read_surf
? read_surf
->Framebuffer
: NULL
));
418 check_and_update_buffer_size(draw_surf
);
419 if (read_surf
&& read_surf
!= draw_surf
)
420 check_and_update_buffer_size(draw_surf
);
426 static enum pipe_format
427 choose_color_format(const __GLcontextModes
*visual
)
429 if (visual
->redBits
== 8 &&
430 visual
->greenBits
== 8 &&
431 visual
->blueBits
== 8 &&
432 visual
->alphaBits
== 8) {
433 /* XXX this really also depends on the ordering of R,G,B,A */
434 return PIPE_FORMAT_A8R8G8B8_UNORM
;
438 return PIPE_FORMAT_NONE
;
443 static enum pipe_format
444 choose_depth_format(const __GLcontextModes
*visual
)
446 if (visual
->depthBits
> 0)
447 return PIPE_FORMAT_S8Z24_UNORM
;
449 return PIPE_FORMAT_NONE
;
453 static enum pipe_format
454 choose_stencil_format(const __GLcontextModes
*visual
)
456 if (visual
->stencilBits
> 0)
457 return PIPE_FORMAT_S8Z24_UNORM
;
459 return PIPE_FORMAT_NONE
;
464 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
467 xlib_eglCreateWindowSurface(_EGLDriver
*drv
, EGLDisplay dpy
, EGLConfig config
,
468 NativeWindowType window
, const EGLint
*attrib_list
)
470 struct xlib_egl_driver
*xdrv
= xlib_egl_driver(drv
);
471 _EGLDisplay
*disp
= _eglLookupDisplay(dpy
);
472 _EGLConfig
*conf
= _eglLookupConfig(drv
, dpy
, config
);
474 struct xlib_egl_surface
*surf
;
475 __GLcontextModes visual
;
478 surf
= CALLOC_STRUCT(xlib_egl_surface
);
480 return EGL_NO_SURFACE
;
482 /* Let EGL lib init the common stuff */
483 if (!_eglInitSurface(drv
, dpy
, &surf
->Base
, EGL_WINDOW_BIT
,
484 config
, attrib_list
)) {
486 return EGL_NO_SURFACE
;
489 _eglSaveSurface(&surf
->Base
);
492 * Now init the Xlib and gallium stuff
494 surf
->Win
= (Window
) window
; /* The X window ID */
495 surf
->Dpy
= disp
->Xdpy
; /* The X display */
496 surf
->Gc
= XCreateGC(surf
->Dpy
, surf
->Win
, 0, NULL
);
498 surf
->winsys
= xdrv
->winsys
;
500 _eglConfigToContextModesRec(conf
, &visual
);
501 get_drawable_size(surf
->Dpy
, surf
->Win
, &width
, &height
);
502 get_drawable_visual_info(surf
->Dpy
, surf
->Win
, &surf
->VisInfo
);
504 surf
->Base
.Width
= width
;
505 surf
->Base
.Height
= height
;
507 /* Create GL statetracker framebuffer */
508 surf
->Framebuffer
= st_create_framebuffer(&visual
,
509 choose_color_format(&visual
),
510 choose_depth_format(&visual
),
511 choose_stencil_format(&visual
),
515 st_resize_framebuffer(surf
->Framebuffer
, width
, height
);
517 return _eglGetSurfaceHandle(&surf
->Base
);
522 xlib_eglDestroySurface(_EGLDriver
*drv
, EGLDisplay dpy
, EGLSurface surface
)
524 struct xlib_egl_surface
*surf
= lookup_surface(surface
);
526 _eglHashRemove(_eglGlobal
.Surfaces
, (EGLuint
) surface
);
527 if (surf
->Base
.IsBound
) {
528 surf
->Base
.DeletePending
= EGL_TRUE
;
531 st_unreference_framebuffer(&surf
->Framebuffer
);
537 _eglError(EGL_BAD_SURFACE
, "eglDestroySurface");
544 xlib_eglSwapBuffers(_EGLDriver
*drv
, EGLDisplay dpy
, EGLSurface draw
)
546 /* error checking step: */
547 if (!_eglSwapBuffers(drv
, dpy
, draw
))
551 struct xlib_egl_surface
*xsurf
= lookup_surface(draw
);
552 struct pipe_winsys
*pws
= xsurf
->winsys
;
553 struct pipe_surface
*psurf
=
554 st_get_framebuffer_surface(xsurf
->Framebuffer
, ST_SURFACE_BACK_LEFT
);
556 st_notify_swapbuffers(xsurf
->Framebuffer
);
558 display_surface(pws
, psurf
, xsurf
);
560 check_and_update_buffer_size(xsurf
);
568 * Determine which API(s) is(are) present by looking for some specific
572 find_supported_apis(void)
577 handle
= dlopen(NULL
, 0);
579 if (dlsym(handle
, "st_api_OpenGL_ES1"))
580 mask
|= EGL_OPENGL_ES_BIT
;
582 if (dlsym(handle
, "st_api_OpenGL_ES2"))
583 mask
|= EGL_OPENGL_ES2_BIT
;
585 if (dlsym(handle
, "st_api_OpenGL"))
586 mask
|= EGL_OPENGL_BIT
;
588 if (dlsym(handle
, "st_api_OpenVG"))
589 mask
|= EGL_OPENVG_BIT
;
598 * This is the main entrypoint into the driver.
599 * Called by libEGL to instantiate an _EGLDriver object.
602 _eglMain(_EGLDisplay
*dpy
, const char *args
)
604 struct xlib_egl_driver
*xdrv
;
606 _eglLog(_EGL_INFO
, "Entering EGL/Xlib _eglMain(%s)", args
);
608 xdrv
= CALLOC_STRUCT(xlib_egl_driver
);
613 dpy
->Xdpy
= XOpenDisplay(NULL
);
616 _eglInitDriverFallbacks(&xdrv
->Base
);
617 xdrv
->Base
.API
.Initialize
= xlib_eglInitialize
;
618 xdrv
->Base
.API
.Terminate
= xlib_eglTerminate
;
619 xdrv
->Base
.API
.GetProcAddress
= xlib_eglGetProcAddress
;
620 xdrv
->Base
.API
.CreateContext
= xlib_eglCreateContext
;
621 xdrv
->Base
.API
.DestroyContext
= xlib_eglDestroyContext
;
622 xdrv
->Base
.API
.CreateWindowSurface
= xlib_eglCreateWindowSurface
;
623 xdrv
->Base
.API
.DestroySurface
= xlib_eglDestroySurface
;
624 xdrv
->Base
.API
.MakeCurrent
= xlib_eglMakeCurrent
;
625 xdrv
->Base
.API
.SwapBuffers
= xlib_eglSwapBuffers
;
627 xdrv
->Base
.ClientAPIsMask
= find_supported_apis();
628 if (xdrv
->Base
.ClientAPIsMask
== 0x0) {
629 /* the app isn't directly linked with any EGL-supprted APIs
630 * (such as libGLESv2.so) so use an EGL utility to see what
631 * APIs might be loaded dynamically on this system.
633 xdrv
->Base
.ClientAPIsMask
= _eglFindAPIs();
636 xdrv
->Base
.Name
= "Xlib/softpipe";
638 /* create one winsys and use it for all contexts/surfaces */
639 xdrv
->winsys
= create_sw_winsys();
640 xdrv
->winsys
->flush_frontbuffer
= flush_frontbuffer
;
642 xdrv
->screen
= softpipe_create_screen(xdrv
->winsys
);