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 static const EGLint all_apis
= (EGL_OPENGL_ES_BIT
|
140 _EGLDisplay
*disp
= _eglLookupDisplay(dpy
);
141 XVisualInfo
*visInfo
, visTemplate
;
144 /* get list of all X visuals, create an EGL config for each */
145 visTemplate
.screen
= DefaultScreen(disp
->Xdpy
);
146 visInfo
= XGetVisualInfo(disp
->Xdpy
, VisualScreenMask
,
147 &visTemplate
, &num_visuals
);
149 printf("egl_xlib.c: couldn't get any X visuals\n");
153 for (i
= 0; i
< num_visuals
; i
++) {
154 _EGLConfig
*config
= calloc(1, sizeof(_EGLConfig
));
156 int rbits
= bitcount(visInfo
[i
].red_mask
);
157 int gbits
= bitcount(visInfo
[i
].green_mask
);
158 int bbits
= bitcount(visInfo
[i
].blue_mask
);
159 int abits
= bbits
== 8 ? 8 : 0;
162 int visid
= visInfo
[i
].visualid
;
163 #if defined(__cplusplus) || defined(c_plusplus)
164 int vistype
= visInfo
[i
].c_class
;
166 int vistype
= visInfo
[i
].class;
169 _eglInitConfig(config
, id
);
170 SET_CONFIG_ATTRIB(config
, EGL_BUFFER_SIZE
, rbits
+ gbits
+ bbits
+ abits
);
171 SET_CONFIG_ATTRIB(config
, EGL_RED_SIZE
, rbits
);
172 SET_CONFIG_ATTRIB(config
, EGL_GREEN_SIZE
, gbits
);
173 SET_CONFIG_ATTRIB(config
, EGL_BLUE_SIZE
, bbits
);
174 SET_CONFIG_ATTRIB(config
, EGL_ALPHA_SIZE
, abits
);
175 SET_CONFIG_ATTRIB(config
, EGL_DEPTH_SIZE
, zbits
);
176 SET_CONFIG_ATTRIB(config
, EGL_STENCIL_SIZE
, sbits
);
177 SET_CONFIG_ATTRIB(config
, EGL_NATIVE_VISUAL_ID
, visid
);
178 SET_CONFIG_ATTRIB(config
, EGL_NATIVE_VISUAL_TYPE
, vistype
);
179 SET_CONFIG_ATTRIB(config
, EGL_NATIVE_RENDERABLE
, EGL_FALSE
);
180 SET_CONFIG_ATTRIB(config
, EGL_CONFORMANT
, all_apis
);
181 SET_CONFIG_ATTRIB(config
, EGL_RENDERABLE_TYPE
, all_apis
);
182 SET_CONFIG_ATTRIB(config
, EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
);
184 _eglAddConfig(disp
, config
);
190 * Called via eglInitialize(), drv->API.Initialize().
193 xlib_eglInitialize(_EGLDriver
*drv
, EGLDisplay dpy
,
194 EGLint
*minor
, EGLint
*major
)
196 create_configs(drv
, dpy
);
198 drv
->Initialized
= EGL_TRUE
;
200 /* we're supporting EGL 1.4 */
209 * Called via eglTerminate(), drv->API.Terminate().
212 xlib_eglTerminate(_EGLDriver
*drv
, EGLDisplay dpy
)
219 xlib_eglGetProcAddress(const char *procname
)
221 return (_EGLProc
) st_get_proc_address(procname
);
226 get_drawable_visual_info(Display
*dpy
, Drawable d
, XVisualInfo
*visInfo
)
228 XWindowAttributes attr
;
229 XVisualInfo visTemp
, *vis
;
232 XGetWindowAttributes(dpy
, d
, &attr
);
234 visTemp
.screen
= DefaultScreen(dpy
);
235 visTemp
.visualid
= attr
.visual
->visualid
;
236 vis
= XGetVisualInfo(dpy
,
237 (VisualScreenMask
| VisualIDMask
),
238 &visTemp
, &num_visuals
);
247 /** Get size of given window */
249 get_drawable_size(Display
*dpy
, Drawable d
, uint
*width
, uint
*height
)
254 unsigned int w
, h
, bw
, depth
;
255 stat
= XGetGeometry(dpy
, d
, &root
, &xpos
, &ypos
, &w
, &h
, &bw
, &depth
);
263 check_and_update_buffer_size(struct xlib_egl_surface
*surface
)
266 get_drawable_size(surface
->Dpy
, surface
->Win
, &width
, &height
);
267 st_resize_framebuffer(surface
->Framebuffer
, width
, height
);
268 surface
->Base
.Width
= width
;
269 surface
->Base
.Height
= height
;
275 display_surface(struct pipe_winsys
*pws
,
276 struct pipe_surface
*psurf
,
277 struct xlib_egl_surface
*xsurf
)
282 ximage
= XCreateImage(xsurf
->Dpy
,
283 xsurf
->VisInfo
.visual
,
284 xsurf
->VisInfo
.depth
,
285 ZPixmap
, 0, /* format, offset */
289 0); /* bytes_per_line */
292 assert(ximage
->format
);
293 assert(ximage
->bitmap_unit
);
295 data
= pws
->buffer_map(pws
, psurf
->buffer
, 0);
297 /* update XImage's fields */
299 ximage
->width
= psurf
->width
;
300 ximage
->height
= psurf
->height
;
301 ximage
->bytes_per_line
= psurf
->stride
;
303 XPutImage(xsurf
->Dpy
, xsurf
->Win
, xsurf
->Gc
,
304 ximage
, 0, 0, 0, 0, psurf
->width
, psurf
->height
);
306 XSync(xsurf
->Dpy
, 0);
309 XDestroyImage(ximage
);
311 pws
->buffer_unmap(pws
, psurf
->buffer
);
316 /** Display gallium surface in X window */
318 flush_frontbuffer(struct pipe_winsys
*pws
,
319 struct pipe_surface
*psurf
,
320 void *context_private
)
322 struct xlib_egl_surface
*xsurf
= (struct xlib_egl_surface
*) context_private
;
323 display_surface(pws
, psurf
, xsurf
);
329 * Called via eglCreateContext(), drv->API.CreateContext().
332 xlib_eglCreateContext(_EGLDriver
*drv
, EGLDisplay dpy
, EGLConfig config
,
333 EGLContext share_list
, const EGLint
*attrib_list
)
335 struct xlib_egl_driver
*xdrv
= xlib_egl_driver(drv
);
336 _EGLConfig
*conf
= _eglLookupConfig(drv
, dpy
, config
);
337 struct xlib_egl_context
*ctx
;
338 struct st_context
*share_ctx
= NULL
; /* XXX fix */
339 __GLcontextModes visual
;
341 ctx
= CALLOC_STRUCT(xlib_egl_context
);
343 return EGL_NO_CONTEXT
;
345 /* let EGL lib init the common stuff */
346 if (!_eglInitContext(drv
, dpy
, &ctx
->Base
, config
, attrib_list
)) {
348 return EGL_NO_CONTEXT
;
351 /* API-dependent context creation */
352 switch (ctx
->Base
.ClientAPI
) {
354 case EGL_OPENGL_ES_API
:
355 _eglLog(_EGL_DEBUG
, "Create Context for ES version %d\n",
356 ctx
->Base
.ClientVersion
);
359 /* create a softpipe context */
360 ctx
->pipe
= softpipe_create(xdrv
->screen
, xdrv
->winsys
, NULL
);
361 /* Now do xlib / state tracker inits here */
362 _eglConfigToContextModesRec(conf
, &visual
);
363 ctx
->Context
= st_create_context(ctx
->pipe
, &visual
, share_ctx
);
366 _eglError(EGL_BAD_MATCH
, "eglCreateContext(unsupported API)");
368 return EGL_NO_CONTEXT
;
371 _eglSaveContext(&ctx
->Base
);
373 return _eglGetContextHandle(&ctx
->Base
);
378 xlib_eglDestroyContext(_EGLDriver
*drv
, EGLDisplay dpy
, EGLContext ctx
)
380 struct xlib_egl_context
*context
= lookup_context(ctx
);
382 if (context
->Base
.IsBound
) {
383 context
->Base
.DeletePending
= EGL_TRUE
;
386 /* API-dependent clean-up */
387 switch (context
->Base
.ClientAPI
) {
388 case EGL_OPENGL_ES_API
:
391 st_destroy_context(context
->Context
);
401 _eglError(EGL_BAD_CONTEXT
, "eglDestroyContext");
408 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
411 xlib_eglMakeCurrent(_EGLDriver
*drv
, EGLDisplay dpy
,
412 EGLSurface draw
, EGLSurface read
, EGLContext ctx
)
414 struct xlib_egl_context
*context
= lookup_context(ctx
);
415 struct xlib_egl_surface
*draw_surf
= lookup_surface(draw
);
416 struct xlib_egl_surface
*read_surf
= lookup_surface(read
);
418 if (!_eglMakeCurrent(drv
, dpy
, draw
, read
, context
))
421 st_make_current((context
? context
->Context
: NULL
),
422 (draw_surf
? draw_surf
->Framebuffer
: NULL
),
423 (read_surf
? read_surf
->Framebuffer
: NULL
));
426 check_and_update_buffer_size(draw_surf
);
427 if (read_surf
&& read_surf
!= draw_surf
)
428 check_and_update_buffer_size(draw_surf
);
434 static enum pipe_format
435 choose_color_format(const __GLcontextModes
*visual
)
437 if (visual
->redBits
== 8 &&
438 visual
->greenBits
== 8 &&
439 visual
->blueBits
== 8 &&
440 visual
->alphaBits
== 8) {
441 /* XXX this really also depends on the ordering of R,G,B,A */
442 return PIPE_FORMAT_A8R8G8B8_UNORM
;
446 return PIPE_FORMAT_NONE
;
451 static enum pipe_format
452 choose_depth_format(const __GLcontextModes
*visual
)
454 if (visual
->depthBits
> 0)
455 return PIPE_FORMAT_S8Z24_UNORM
;
457 return PIPE_FORMAT_NONE
;
461 static enum pipe_format
462 choose_stencil_format(const __GLcontextModes
*visual
)
464 if (visual
->stencilBits
> 0)
465 return PIPE_FORMAT_S8Z24_UNORM
;
467 return PIPE_FORMAT_NONE
;
472 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
475 xlib_eglCreateWindowSurface(_EGLDriver
*drv
, EGLDisplay dpy
, EGLConfig config
,
476 NativeWindowType window
, const EGLint
*attrib_list
)
478 struct xlib_egl_driver
*xdrv
= xlib_egl_driver(drv
);
479 _EGLDisplay
*disp
= _eglLookupDisplay(dpy
);
480 _EGLConfig
*conf
= _eglLookupConfig(drv
, dpy
, config
);
482 struct xlib_egl_surface
*surf
;
483 __GLcontextModes visual
;
486 surf
= CALLOC_STRUCT(xlib_egl_surface
);
488 return EGL_NO_SURFACE
;
490 /* Let EGL lib init the common stuff */
491 if (!_eglInitSurface(drv
, dpy
, &surf
->Base
, EGL_WINDOW_BIT
,
492 config
, attrib_list
)) {
494 return EGL_NO_SURFACE
;
497 _eglSaveSurface(&surf
->Base
);
500 * Now init the Xlib and gallium stuff
502 surf
->Win
= (Window
) window
; /* The X window ID */
503 surf
->Dpy
= disp
->Xdpy
; /* The X display */
504 surf
->Gc
= XCreateGC(surf
->Dpy
, surf
->Win
, 0, NULL
);
506 surf
->winsys
= xdrv
->winsys
;
508 _eglConfigToContextModesRec(conf
, &visual
);
509 get_drawable_size(surf
->Dpy
, surf
->Win
, &width
, &height
);
510 get_drawable_visual_info(surf
->Dpy
, surf
->Win
, &surf
->VisInfo
);
512 surf
->Base
.Width
= width
;
513 surf
->Base
.Height
= height
;
515 /* Create GL statetracker framebuffer */
516 surf
->Framebuffer
= st_create_framebuffer(&visual
,
517 choose_color_format(&visual
),
518 choose_depth_format(&visual
),
519 choose_stencil_format(&visual
),
523 st_resize_framebuffer(surf
->Framebuffer
, width
, height
);
525 return _eglGetSurfaceHandle(&surf
->Base
);
530 xlib_eglDestroySurface(_EGLDriver
*drv
, EGLDisplay dpy
, EGLSurface surface
)
532 struct xlib_egl_surface
*surf
= lookup_surface(surface
);
534 _eglHashRemove(_eglGlobal
.Surfaces
, (EGLuint
) surface
);
535 if (surf
->Base
.IsBound
) {
536 surf
->Base
.DeletePending
= EGL_TRUE
;
539 XFreeGC(surf
->Dpy
, surf
->Gc
);
540 st_unreference_framebuffer(&surf
->Framebuffer
);
546 _eglError(EGL_BAD_SURFACE
, "eglDestroySurface");
553 xlib_eglSwapBuffers(_EGLDriver
*drv
, EGLDisplay dpy
, EGLSurface draw
)
555 /* error checking step: */
556 if (!_eglSwapBuffers(drv
, dpy
, draw
))
560 struct xlib_egl_surface
*xsurf
= lookup_surface(draw
);
561 struct pipe_winsys
*pws
= xsurf
->winsys
;
562 struct pipe_surface
*psurf
=
563 st_get_framebuffer_surface(xsurf
->Framebuffer
, ST_SURFACE_BACK_LEFT
);
565 st_notify_swapbuffers(xsurf
->Framebuffer
);
567 display_surface(pws
, psurf
, xsurf
);
569 check_and_update_buffer_size(xsurf
);
577 * Determine which API(s) is(are) present by looking for some specific
581 find_supported_apis(void)
586 handle
= dlopen(NULL
, 0);
588 if (dlsym(handle
, "st_api_OpenGL_ES1"))
589 mask
|= EGL_OPENGL_ES_BIT
;
591 if (dlsym(handle
, "st_api_OpenGL_ES2"))
592 mask
|= EGL_OPENGL_ES2_BIT
;
594 if (dlsym(handle
, "st_api_OpenGL"))
595 mask
|= EGL_OPENGL_BIT
;
597 if (dlsym(handle
, "st_api_OpenVG"))
598 mask
|= EGL_OPENVG_BIT
;
607 * This is the main entrypoint into the driver.
608 * Called by libEGL to instantiate an _EGLDriver object.
611 _eglMain(_EGLDisplay
*dpy
, const char *args
)
613 struct xlib_egl_driver
*xdrv
;
615 _eglLog(_EGL_INFO
, "Entering EGL/Xlib _eglMain(%s)", args
);
617 xdrv
= CALLOC_STRUCT(xlib_egl_driver
);
622 dpy
->Xdpy
= XOpenDisplay(NULL
);
625 _eglInitDriverFallbacks(&xdrv
->Base
);
626 xdrv
->Base
.API
.Initialize
= xlib_eglInitialize
;
627 xdrv
->Base
.API
.Terminate
= xlib_eglTerminate
;
628 xdrv
->Base
.API
.GetProcAddress
= xlib_eglGetProcAddress
;
629 xdrv
->Base
.API
.CreateContext
= xlib_eglCreateContext
;
630 xdrv
->Base
.API
.DestroyContext
= xlib_eglDestroyContext
;
631 xdrv
->Base
.API
.CreateWindowSurface
= xlib_eglCreateWindowSurface
;
632 xdrv
->Base
.API
.DestroySurface
= xlib_eglDestroySurface
;
633 xdrv
->Base
.API
.MakeCurrent
= xlib_eglMakeCurrent
;
634 xdrv
->Base
.API
.SwapBuffers
= xlib_eglSwapBuffers
;
636 xdrv
->Base
.ClientAPIsMask
= find_supported_apis();
637 if (xdrv
->Base
.ClientAPIsMask
== 0x0) {
638 /* the app isn't directly linked with any EGL-supprted APIs
639 * (such as libGLESv2.so) so use an EGL utility to see what
640 * APIs might be loaded dynamically on this system.
642 xdrv
->Base
.ClientAPIsMask
= _eglFindAPIs();
645 xdrv
->Base
.Name
= "Xlib/softpipe";
647 /* create one winsys and use it for all contexts/surfaces */
648 xdrv
->winsys
= create_sw_winsys();
649 xdrv
->winsys
->flush_frontbuffer
= flush_frontbuffer
;
651 xdrv
->screen
= softpipe_create_screen(xdrv
->winsys
);