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
);
176 _eglAddConfig(disp
, config
);
182 * Called via eglInitialize(), drv->API.Initialize().
185 xlib_eglInitialize(_EGLDriver
*drv
, EGLDisplay dpy
,
186 EGLint
*minor
, EGLint
*major
)
188 create_configs(drv
, dpy
);
190 drv
->Initialized
= EGL_TRUE
;
192 /* we're supporting EGL 1.4 */
201 * Called via eglTerminate(), drv->API.Terminate().
204 xlib_eglTerminate(_EGLDriver
*drv
, EGLDisplay dpy
)
211 xlib_eglGetProcAddress(const char *procname
)
213 return (_EGLProc
) st_get_proc_address(procname
);
218 get_drawable_visual_info(Display
*dpy
, Drawable d
, XVisualInfo
*visInfo
)
220 XWindowAttributes attr
;
221 XVisualInfo visTemp
, *vis
;
224 XGetWindowAttributes(dpy
, d
, &attr
);
226 visTemp
.screen
= DefaultScreen(dpy
);
227 visTemp
.visualid
= attr
.visual
->visualid
;
228 vis
= XGetVisualInfo(dpy
,
229 (VisualScreenMask
| VisualIDMask
),
230 &visTemp
, &num_visuals
);
239 /** Get size of given window */
241 get_drawable_size(Display
*dpy
, Drawable d
, uint
*width
, uint
*height
)
246 unsigned int w
, h
, bw
, depth
;
247 stat
= XGetGeometry(dpy
, d
, &root
, &xpos
, &ypos
, &w
, &h
, &bw
, &depth
);
255 check_and_update_buffer_size(struct xlib_egl_surface
*surface
)
258 get_drawable_size(surface
->Dpy
, surface
->Win
, &width
, &height
);
259 st_resize_framebuffer(surface
->Framebuffer
, width
, height
);
260 surface
->Base
.Width
= width
;
261 surface
->Base
.Height
= height
;
267 display_surface(struct pipe_winsys
*pws
,
268 struct pipe_surface
*psurf
,
269 struct xlib_egl_surface
*xsurf
)
274 ximage
= XCreateImage(xsurf
->Dpy
,
275 xsurf
->VisInfo
.visual
,
276 xsurf
->VisInfo
.depth
,
277 ZPixmap
, 0, /* format, offset */
281 0); /* bytes_per_line */
284 assert(ximage
->format
);
285 assert(ximage
->bitmap_unit
);
287 data
= pws
->buffer_map(pws
, psurf
->buffer
, 0);
289 /* update XImage's fields */
291 ximage
->width
= psurf
->width
;
292 ximage
->height
= psurf
->height
;
293 ximage
->bytes_per_line
= psurf
->pitch
* psurf
->cpp
;
295 XPutImage(xsurf
->Dpy
, xsurf
->Win
, xsurf
->Gc
,
296 ximage
, 0, 0, 0, 0, psurf
->width
, psurf
->height
);
298 XSync(xsurf
->Dpy
, 0);
301 XDestroyImage(ximage
);
303 pws
->buffer_unmap(pws
, psurf
->buffer
);
308 /** Display gallium surface in X window */
310 flush_frontbuffer(struct pipe_winsys
*pws
,
311 struct pipe_surface
*psurf
,
312 void *context_private
)
314 struct xlib_egl_surface
*xsurf
= (struct xlib_egl_surface
*) context_private
;
315 display_surface(pws
, psurf
, xsurf
);
321 * Called via eglCreateContext(), drv->API.CreateContext().
324 xlib_eglCreateContext(_EGLDriver
*drv
, EGLDisplay dpy
, EGLConfig config
,
325 EGLContext share_list
, const EGLint
*attrib_list
)
327 struct xlib_egl_driver
*xdrv
= xlib_egl_driver(drv
);
328 _EGLConfig
*conf
= _eglLookupConfig(drv
, dpy
, config
);
329 struct xlib_egl_context
*ctx
;
330 struct st_context
*share_ctx
= NULL
; /* XXX fix */
331 __GLcontextModes visual
;
333 ctx
= CALLOC_STRUCT(xlib_egl_context
);
335 return EGL_NO_CONTEXT
;
337 /* let EGL lib init the common stuff */
338 if (!_eglInitContext(drv
, dpy
, &ctx
->Base
, config
, attrib_list
)) {
340 return EGL_NO_CONTEXT
;
343 /* API-dependent context creation */
344 switch (ctx
->Base
.ClientAPI
) {
345 case EGL_OPENGL_ES_API
:
346 _eglLog(_EGL_DEBUG
, "Create Context for ES version %d\n",
347 ctx
->Base
.ClientVersion
);
350 /* create a softpipe context */
351 ctx
->pipe
= softpipe_create(xdrv
->screen
, xdrv
->winsys
, NULL
);
352 /* Now do xlib / state tracker inits here */
353 _eglConfigToContextModesRec(conf
, &visual
);
354 ctx
->Context
= st_create_context(ctx
->pipe
, &visual
, share_ctx
);
357 _eglError(EGL_BAD_MATCH
, "eglCreateContext(unsupported API)");
359 return EGL_NO_CONTEXT
;
362 _eglSaveContext(&ctx
->Base
);
364 return _eglGetContextHandle(&ctx
->Base
);
369 xlib_eglDestroyContext(_EGLDriver
*drv
, EGLDisplay dpy
, EGLContext ctx
)
371 struct xlib_egl_context
*context
= lookup_context(ctx
);
373 if (context
->Base
.IsBound
) {
374 context
->Base
.DeletePending
= EGL_TRUE
;
377 /* API-dependent clean-up */
378 switch (context
->Base
.ClientAPI
) {
379 case EGL_OPENGL_ES_API
:
382 st_destroy_context(context
->Context
);
392 _eglError(EGL_BAD_CONTEXT
, "eglDestroyContext");
399 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
402 xlib_eglMakeCurrent(_EGLDriver
*drv
, EGLDisplay dpy
,
403 EGLSurface draw
, EGLSurface read
, EGLContext ctx
)
405 struct xlib_egl_context
*context
= lookup_context(ctx
);
406 struct xlib_egl_surface
*draw_surf
= lookup_surface(draw
);
407 struct xlib_egl_surface
*read_surf
= lookup_surface(read
);
409 if (!_eglMakeCurrent(drv
, dpy
, draw
, read
, context
))
412 st_make_current((context
? context
->Context
: NULL
),
413 (draw_surf
? draw_surf
->Framebuffer
: NULL
),
414 (read_surf
? read_surf
->Framebuffer
: NULL
));
417 check_and_update_buffer_size(draw_surf
);
418 if (read_surf
&& read_surf
!= draw_surf
)
419 check_and_update_buffer_size(draw_surf
);
425 static enum pipe_format
426 choose_color_format(const __GLcontextModes
*visual
)
428 if (visual
->redBits
== 8 &&
429 visual
->greenBits
== 8 &&
430 visual
->blueBits
== 8 &&
431 visual
->alphaBits
== 8) {
432 /* XXX this really also depends on the ordering of R,G,B,A */
433 return PIPE_FORMAT_A8R8G8B8_UNORM
;
437 return PIPE_FORMAT_NONE
;
442 static enum pipe_format
443 choose_depth_format(const __GLcontextModes
*visual
)
445 if (visual
->depthBits
> 0)
446 return PIPE_FORMAT_S8Z24_UNORM
;
448 return PIPE_FORMAT_NONE
;
452 static enum pipe_format
453 choose_stencil_format(const __GLcontextModes
*visual
)
455 if (visual
->stencilBits
> 0)
456 return PIPE_FORMAT_S8Z24_UNORM
;
458 return PIPE_FORMAT_NONE
;
463 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
466 xlib_eglCreateWindowSurface(_EGLDriver
*drv
, EGLDisplay dpy
, EGLConfig config
,
467 NativeWindowType window
, const EGLint
*attrib_list
)
469 struct xlib_egl_driver
*xdrv
= xlib_egl_driver(drv
);
470 _EGLDisplay
*disp
= _eglLookupDisplay(dpy
);
471 _EGLConfig
*conf
= _eglLookupConfig(drv
, dpy
, config
);
473 struct xlib_egl_surface
*surf
;
474 __GLcontextModes visual
;
477 surf
= CALLOC_STRUCT(xlib_egl_surface
);
479 return EGL_NO_SURFACE
;
481 /* Let EGL lib init the common stuff */
482 if (!_eglInitSurface(drv
, dpy
, &surf
->Base
, EGL_WINDOW_BIT
,
483 config
, attrib_list
)) {
485 return EGL_NO_SURFACE
;
488 _eglSaveSurface(&surf
->Base
);
491 * Now init the Xlib and gallium stuff
493 surf
->Win
= (Window
) window
; /* The X window ID */
494 surf
->Dpy
= disp
->Xdpy
; /* The X display */
495 surf
->Gc
= XCreateGC(surf
->Dpy
, surf
->Win
, 0, NULL
);
497 surf
->winsys
= xdrv
->winsys
;
499 _eglConfigToContextModesRec(conf
, &visual
);
500 get_drawable_size(surf
->Dpy
, surf
->Win
, &width
, &height
);
501 get_drawable_visual_info(surf
->Dpy
, surf
->Win
, &surf
->VisInfo
);
503 surf
->Base
.Width
= width
;
504 surf
->Base
.Height
= height
;
506 /* Create GL statetracker framebuffer */
507 surf
->Framebuffer
= st_create_framebuffer(&visual
,
508 choose_color_format(&visual
),
509 choose_depth_format(&visual
),
510 choose_stencil_format(&visual
),
514 st_resize_framebuffer(surf
->Framebuffer
, width
, height
);
516 return _eglGetSurfaceHandle(&surf
->Base
);
521 xlib_eglDestroySurface(_EGLDriver
*drv
, EGLDisplay dpy
, EGLSurface surface
)
523 struct xlib_egl_surface
*surf
= lookup_surface(surface
);
525 _eglHashRemove(_eglGlobal
.Surfaces
, (EGLuint
) surface
);
526 if (surf
->Base
.IsBound
) {
527 surf
->Base
.DeletePending
= EGL_TRUE
;
530 st_unreference_framebuffer(&surf
->Framebuffer
);
536 _eglError(EGL_BAD_SURFACE
, "eglDestroySurface");
543 xlib_eglSwapBuffers(_EGLDriver
*drv
, EGLDisplay dpy
, EGLSurface draw
)
545 /* error checking step: */
546 if (!_eglSwapBuffers(drv
, dpy
, draw
))
550 struct xlib_egl_surface
*xsurf
= lookup_surface(draw
);
551 struct pipe_winsys
*pws
= xsurf
->winsys
;
552 struct pipe_surface
*psurf
=
553 st_get_framebuffer_surface(xsurf
->Framebuffer
, ST_SURFACE_BACK_LEFT
);
555 st_notify_swapbuffers(xsurf
->Framebuffer
);
557 display_surface(pws
, psurf
, xsurf
);
559 check_and_update_buffer_size(xsurf
);
567 * Determine which API(s) is(are) present by looking for some specific
571 find_supported_apis(void)
576 handle
= dlopen(NULL
, 0);
578 if (dlsym(handle
, "st_api_OpenGL_ES1"))
579 mask
|= EGL_OPENGL_ES_BIT
;
581 if (dlsym(handle
, "st_api_OpenGL_ES2"))
582 mask
|= EGL_OPENGL_ES2_BIT
;
584 if (dlsym(handle
, "st_api_OpenGL"))
585 mask
|= EGL_OPENGL_BIT
;
587 if (dlsym(handle
, "st_api_OpenVG"))
588 mask
|= EGL_OPENVG_BIT
;
597 * This is the main entrypoint into the driver.
598 * Called by libEGL to instantiate an _EGLDriver object.
601 _eglMain(_EGLDisplay
*dpy
, const char *args
)
603 struct xlib_egl_driver
*xdrv
;
605 _eglLog(_EGL_INFO
, "Entering EGL/Xlib _eglMain(%s)", args
);
607 xdrv
= CALLOC_STRUCT(xlib_egl_driver
);
612 dpy
->Xdpy
= XOpenDisplay(NULL
);
615 _eglInitDriverFallbacks(&xdrv
->Base
);
616 xdrv
->Base
.API
.Initialize
= xlib_eglInitialize
;
617 xdrv
->Base
.API
.Terminate
= xlib_eglTerminate
;
618 xdrv
->Base
.API
.GetProcAddress
= xlib_eglGetProcAddress
;
619 xdrv
->Base
.API
.CreateContext
= xlib_eglCreateContext
;
620 xdrv
->Base
.API
.DestroyContext
= xlib_eglDestroyContext
;
621 xdrv
->Base
.API
.CreateWindowSurface
= xlib_eglCreateWindowSurface
;
622 xdrv
->Base
.API
.DestroySurface
= xlib_eglDestroySurface
;
623 xdrv
->Base
.API
.MakeCurrent
= xlib_eglMakeCurrent
;
624 xdrv
->Base
.API
.SwapBuffers
= xlib_eglSwapBuffers
;
626 xdrv
->Base
.ClientAPIsMask
= find_supported_apis();
627 if (xdrv
->Base
.ClientAPIsMask
== 0x0) {
628 /* the app isn't directly linked with any EGL-supprted APIs
629 * (such as libGLESv2.so) so use an EGL utility to see what
630 * APIs might be loaded dynamically on this system.
632 xdrv
->Base
.ClientAPIsMask
= _eglFindAPIs();
635 xdrv
->Base
.Name
= "Xlib/softpipe";
637 /* create one winsys and use it for all contexts/surfaces */
638 xdrv
->winsys
= create_sw_winsys();
639 xdrv
->winsys
->flush_frontbuffer
= flush_frontbuffer
;
641 xdrv
->screen
= softpipe_create_screen(xdrv
->winsys
);