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/internal/p_winsys_screen.h"
42 #include "util/u_memory.h"
43 #include "softpipe/sp_winsys.h"
44 #include "softpipe/sp_texture.h"
46 #include "eglconfig.h"
47 #include "eglconfigutil.h"
48 #include "eglcontext.h"
49 #include "egldisplay.h"
50 #include "egldriver.h"
51 #include "eglglobals.h"
53 #include "eglsurface.h"
55 #include "state_tracker/st_public.h"
57 #include "sw_winsys.h"
60 /** subclass of _EGLDriver */
61 struct xlib_egl_driver
63 _EGLDriver Base
; /**< base class */
65 struct pipe_winsys
*winsys
;
66 struct pipe_screen
*screen
;
70 /** subclass of _EGLContext */
71 struct xlib_egl_context
73 _EGLContext Base
; /**< base class */
75 struct pipe_context
*pipe
; /**< Gallium driver context */
76 struct st_context
*Context
; /**< Mesa/gallium state tracker context */
80 /** subclass of _EGLSurface */
81 struct xlib_egl_surface
83 _EGLSurface Base
; /**< base class */
85 Display
*Dpy
; /**< The X Display of the window */
86 Window Win
; /**< The user-created window ID */
90 struct pipe_winsys
*winsys
;
92 struct st_framebuffer
*Framebuffer
;
97 static INLINE
struct xlib_egl_driver
*
98 xlib_egl_driver(_EGLDriver
*drv
)
100 return (struct xlib_egl_driver
*) drv
;
104 static struct xlib_egl_surface
*
105 lookup_surface(EGLSurface surf
)
107 _EGLSurface
*surface
= _eglLookupSurface(surf
);
108 return (struct xlib_egl_surface
*) surface
;
112 static struct xlib_egl_context
*
113 lookup_context(EGLContext surf
)
115 _EGLContext
*context
= _eglLookupContext(surf
);
116 return (struct xlib_egl_context
*) context
;
121 bitcount(unsigned int n
)
124 for (bits
= 0; n
> 0; n
= n
>> 1) {
132 * Create the EGLConfigs. (one per X visual)
135 create_configs(_EGLDriver
*drv
, EGLDisplay dpy
)
137 static const EGLint all_apis
= (EGL_OPENGL_ES_BIT
|
141 _EGLDisplay
*disp
= _eglLookupDisplay(dpy
);
142 XVisualInfo
*visInfo
, visTemplate
;
145 /* get list of all X visuals, create an EGL config for each */
146 visTemplate
.screen
= DefaultScreen(disp
->Xdpy
);
147 visInfo
= XGetVisualInfo(disp
->Xdpy
, VisualScreenMask
,
148 &visTemplate
, &num_visuals
);
150 printf("egl_xlib.c: couldn't get any X visuals\n");
154 for (i
= 0; i
< num_visuals
; i
++) {
155 _EGLConfig
*config
= calloc(1, sizeof(_EGLConfig
));
157 int rbits
= bitcount(visInfo
[i
].red_mask
);
158 int gbits
= bitcount(visInfo
[i
].green_mask
);
159 int bbits
= bitcount(visInfo
[i
].blue_mask
);
160 int abits
= bbits
== 8 ? 8 : 0;
163 int visid
= visInfo
[i
].visualid
;
164 #if defined(__cplusplus) || defined(c_plusplus)
165 int vistype
= visInfo
[i
].c_class
;
167 int vistype
= visInfo
[i
].class;
170 _eglInitConfig(config
, id
);
171 SET_CONFIG_ATTRIB(config
, EGL_BUFFER_SIZE
, rbits
+ gbits
+ bbits
+ abits
);
172 SET_CONFIG_ATTRIB(config
, EGL_RED_SIZE
, rbits
);
173 SET_CONFIG_ATTRIB(config
, EGL_GREEN_SIZE
, gbits
);
174 SET_CONFIG_ATTRIB(config
, EGL_BLUE_SIZE
, bbits
);
175 SET_CONFIG_ATTRIB(config
, EGL_ALPHA_SIZE
, abits
);
176 SET_CONFIG_ATTRIB(config
, EGL_DEPTH_SIZE
, zbits
);
177 SET_CONFIG_ATTRIB(config
, EGL_STENCIL_SIZE
, sbits
);
178 SET_CONFIG_ATTRIB(config
, EGL_NATIVE_VISUAL_ID
, visid
);
179 SET_CONFIG_ATTRIB(config
, EGL_NATIVE_VISUAL_TYPE
, vistype
);
180 SET_CONFIG_ATTRIB(config
, EGL_NATIVE_RENDERABLE
, EGL_FALSE
);
181 SET_CONFIG_ATTRIB(config
, EGL_CONFORMANT
, all_apis
);
182 SET_CONFIG_ATTRIB(config
, EGL_RENDERABLE_TYPE
, all_apis
);
183 SET_CONFIG_ATTRIB(config
, EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
);
185 _eglAddConfig(disp
, config
);
191 * Called via eglInitialize(), drv->API.Initialize().
194 xlib_eglInitialize(_EGLDriver
*drv
, EGLDisplay dpy
,
195 EGLint
*minor
, EGLint
*major
)
197 create_configs(drv
, dpy
);
199 drv
->Initialized
= EGL_TRUE
;
201 /* we're supporting EGL 1.4 */
210 * Called via eglTerminate(), drv->API.Terminate().
213 xlib_eglTerminate(_EGLDriver
*drv
, EGLDisplay dpy
)
220 xlib_eglGetProcAddress(const char *procname
)
222 return (_EGLProc
) st_get_proc_address(procname
);
227 get_drawable_visual_info(Display
*dpy
, Drawable d
, XVisualInfo
*visInfo
)
229 XWindowAttributes attr
;
230 XVisualInfo visTemp
, *vis
;
233 XGetWindowAttributes(dpy
, d
, &attr
);
235 visTemp
.screen
= DefaultScreen(dpy
);
236 visTemp
.visualid
= attr
.visual
->visualid
;
237 vis
= XGetVisualInfo(dpy
,
238 (VisualScreenMask
| VisualIDMask
),
239 &visTemp
, &num_visuals
);
248 /** Get size of given window */
250 get_drawable_size(Display
*dpy
, Drawable d
, uint
*width
, uint
*height
)
255 unsigned int w
, h
, bw
, depth
;
256 stat
= XGetGeometry(dpy
, d
, &root
, &xpos
, &ypos
, &w
, &h
, &bw
, &depth
);
264 check_and_update_buffer_size(struct xlib_egl_surface
*surface
)
267 get_drawable_size(surface
->Dpy
, surface
->Win
, &width
, &height
);
268 st_resize_framebuffer(surface
->Framebuffer
, width
, height
);
269 surface
->Base
.Width
= width
;
270 surface
->Base
.Height
= height
;
276 display_surface(struct pipe_winsys
*pws
,
277 struct pipe_surface
*psurf
,
278 struct xlib_egl_surface
*xsurf
)
280 struct softpipe_texture
*spt
= softpipe_texture(psurf
->texture
);
284 ximage
= XCreateImage(xsurf
->Dpy
,
285 xsurf
->VisInfo
.visual
,
286 xsurf
->VisInfo
.depth
,
287 ZPixmap
, 0, /* format, offset */
291 0); /* bytes_per_line */
294 assert(ximage
->format
);
295 assert(ximage
->bitmap_unit
);
297 data
= pws
->buffer_map(pws
, spt
->buffer
, 0);
299 /* update XImage's fields */
301 ximage
->width
= psurf
->width
;
302 ximage
->height
= psurf
->height
;
303 ximage
->bytes_per_line
= spt
->stride
[psurf
->level
];
305 XPutImage(xsurf
->Dpy
, xsurf
->Win
, xsurf
->Gc
,
306 ximage
, 0, 0, 0, 0, psurf
->width
, psurf
->height
);
308 XSync(xsurf
->Dpy
, 0);
311 XDestroyImage(ximage
);
313 pws
->buffer_unmap(pws
, spt
->buffer
);
318 /** Display gallium surface in X window */
320 flush_frontbuffer(struct pipe_winsys
*pws
,
321 struct pipe_surface
*psurf
,
322 void *context_private
)
324 struct xlib_egl_surface
*xsurf
= (struct xlib_egl_surface
*) context_private
;
325 display_surface(pws
, psurf
, xsurf
);
331 * Called via eglCreateContext(), drv->API.CreateContext().
334 xlib_eglCreateContext(_EGLDriver
*drv
, EGLDisplay dpy
, EGLConfig config
,
335 EGLContext share_list
, const EGLint
*attrib_list
)
337 struct xlib_egl_driver
*xdrv
= xlib_egl_driver(drv
);
338 _EGLConfig
*conf
= _eglLookupConfig(drv
, dpy
, config
);
339 struct xlib_egl_context
*ctx
;
340 struct st_context
*share_ctx
= NULL
; /* XXX fix */
341 __GLcontextModes visual
;
343 ctx
= CALLOC_STRUCT(xlib_egl_context
);
345 return EGL_NO_CONTEXT
;
347 /* let EGL lib init the common stuff */
348 if (!_eglInitContext(drv
, dpy
, &ctx
->Base
, config
, attrib_list
)) {
350 return EGL_NO_CONTEXT
;
353 /* API-dependent context creation */
354 switch (ctx
->Base
.ClientAPI
) {
356 case EGL_OPENGL_ES_API
:
357 _eglLog(_EGL_DEBUG
, "Create Context for ES version %d\n",
358 ctx
->Base
.ClientVersion
);
361 /* create a softpipe context */
362 ctx
->pipe
= softpipe_create(xdrv
->screen
);
363 /* Now do xlib / state tracker inits here */
364 _eglConfigToContextModesRec(conf
, &visual
);
365 ctx
->Context
= st_create_context(ctx
->pipe
, &visual
, share_ctx
);
368 _eglError(EGL_BAD_MATCH
, "eglCreateContext(unsupported API)");
370 return EGL_NO_CONTEXT
;
373 _eglSaveContext(&ctx
->Base
);
375 return _eglGetContextHandle(&ctx
->Base
);
380 xlib_eglDestroyContext(_EGLDriver
*drv
, EGLDisplay dpy
, EGLContext ctx
)
382 struct xlib_egl_context
*context
= lookup_context(ctx
);
384 if (context
->Base
.IsBound
) {
385 context
->Base
.DeletePending
= EGL_TRUE
;
388 /* API-dependent clean-up */
389 switch (context
->Base
.ClientAPI
) {
390 case EGL_OPENGL_ES_API
:
394 st_destroy_context(context
->Context
);
404 _eglError(EGL_BAD_CONTEXT
, "eglDestroyContext");
411 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
414 xlib_eglMakeCurrent(_EGLDriver
*drv
, EGLDisplay dpy
,
415 EGLSurface draw
, EGLSurface read
, EGLContext ctx
)
417 struct xlib_egl_context
*context
= lookup_context(ctx
);
418 struct xlib_egl_surface
*draw_surf
= lookup_surface(draw
);
419 struct xlib_egl_surface
*read_surf
= lookup_surface(read
);
421 if (!_eglMakeCurrent(drv
, dpy
, draw
, read
, context
))
424 st_make_current((context
? context
->Context
: NULL
),
425 (draw_surf
? draw_surf
->Framebuffer
: NULL
),
426 (read_surf
? read_surf
->Framebuffer
: NULL
));
429 check_and_update_buffer_size(draw_surf
);
430 if (read_surf
&& read_surf
!= draw_surf
)
431 check_and_update_buffer_size(draw_surf
);
437 static enum pipe_format
438 choose_color_format(const __GLcontextModes
*visual
)
440 if (visual
->redBits
== 8 &&
441 visual
->greenBits
== 8 &&
442 visual
->blueBits
== 8 &&
443 visual
->alphaBits
== 8) {
444 /* XXX this really also depends on the ordering of R,G,B,A */
445 return PIPE_FORMAT_A8R8G8B8_UNORM
;
449 return PIPE_FORMAT_NONE
;
454 static enum pipe_format
455 choose_depth_format(const __GLcontextModes
*visual
)
457 if (visual
->depthBits
> 0)
458 return PIPE_FORMAT_S8Z24_UNORM
;
460 return PIPE_FORMAT_NONE
;
464 static enum pipe_format
465 choose_stencil_format(const __GLcontextModes
*visual
)
467 if (visual
->stencilBits
> 0)
468 return PIPE_FORMAT_S8Z24_UNORM
;
470 return PIPE_FORMAT_NONE
;
475 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
478 xlib_eglCreateWindowSurface(_EGLDriver
*drv
, EGLDisplay dpy
, EGLConfig config
,
479 NativeWindowType window
, const EGLint
*attrib_list
)
481 struct xlib_egl_driver
*xdrv
= xlib_egl_driver(drv
);
482 _EGLDisplay
*disp
= _eglLookupDisplay(dpy
);
483 _EGLConfig
*conf
= _eglLookupConfig(drv
, dpy
, config
);
485 struct xlib_egl_surface
*surf
;
486 __GLcontextModes visual
;
489 surf
= CALLOC_STRUCT(xlib_egl_surface
);
491 return EGL_NO_SURFACE
;
493 /* Let EGL lib init the common stuff */
494 if (!_eglInitSurface(drv
, dpy
, &surf
->Base
, EGL_WINDOW_BIT
,
495 config
, attrib_list
)) {
497 return EGL_NO_SURFACE
;
500 _eglSaveSurface(&surf
->Base
);
503 * Now init the Xlib and gallium stuff
505 surf
->Win
= (Window
) window
; /* The X window ID */
506 surf
->Dpy
= disp
->Xdpy
; /* The X display */
507 surf
->Gc
= XCreateGC(surf
->Dpy
, surf
->Win
, 0, NULL
);
509 surf
->winsys
= xdrv
->winsys
;
511 _eglConfigToContextModesRec(conf
, &visual
);
512 get_drawable_size(surf
->Dpy
, surf
->Win
, &width
, &height
);
513 get_drawable_visual_info(surf
->Dpy
, surf
->Win
, &surf
->VisInfo
);
515 surf
->Base
.Width
= width
;
516 surf
->Base
.Height
= height
;
518 /* Create GL statetracker framebuffer */
519 surf
->Framebuffer
= st_create_framebuffer(&visual
,
520 choose_color_format(&visual
),
521 choose_depth_format(&visual
),
522 choose_stencil_format(&visual
),
526 st_resize_framebuffer(surf
->Framebuffer
, width
, height
);
528 return _eglGetSurfaceHandle(&surf
->Base
);
533 xlib_eglDestroySurface(_EGLDriver
*drv
, EGLDisplay dpy
, EGLSurface surface
)
535 struct xlib_egl_surface
*surf
= lookup_surface(surface
);
537 _eglHashRemove(_eglGlobal
.Surfaces
, (EGLuint
) surface
);
538 if (surf
->Base
.IsBound
) {
539 surf
->Base
.DeletePending
= EGL_TRUE
;
542 XFreeGC(surf
->Dpy
, surf
->Gc
);
543 st_unreference_framebuffer(surf
->Framebuffer
);
549 _eglError(EGL_BAD_SURFACE
, "eglDestroySurface");
556 xlib_eglSwapBuffers(_EGLDriver
*drv
, EGLDisplay dpy
, EGLSurface draw
)
558 /* error checking step: */
559 if (!_eglSwapBuffers(drv
, dpy
, draw
))
563 struct xlib_egl_surface
*xsurf
= lookup_surface(draw
);
564 struct pipe_winsys
*pws
= xsurf
->winsys
;
565 struct pipe_surface
*psurf
;
567 st_get_framebuffer_surface(xsurf
->Framebuffer
, ST_SURFACE_BACK_LEFT
,
570 st_notify_swapbuffers(xsurf
->Framebuffer
);
572 display_surface(pws
, psurf
, xsurf
);
574 check_and_update_buffer_size(xsurf
);
582 * Determine which API(s) is(are) present by looking for some specific
586 find_supported_apis(void)
591 handle
= dlopen(NULL
, RTLD_LAZY
| RTLD_LOCAL
);
595 if (dlsym(handle
, "st_api_OpenGL_ES1"))
596 mask
|= EGL_OPENGL_ES_BIT
;
598 if (dlsym(handle
, "st_api_OpenGL_ES2"))
599 mask
|= EGL_OPENGL_ES2_BIT
;
601 if (dlsym(handle
, "st_api_OpenGL"))
602 mask
|= EGL_OPENGL_BIT
;
604 if (dlsym(handle
, "st_api_OpenVG"))
605 mask
|= EGL_OPENVG_BIT
;
614 * This is the main entrypoint into the driver.
615 * Called by libEGL to instantiate an _EGLDriver object.
618 _eglMain(_EGLDisplay
*dpy
, const char *args
)
620 struct xlib_egl_driver
*xdrv
;
622 _eglLog(_EGL_INFO
, "Entering EGL/Xlib _eglMain(%s)", args
);
624 xdrv
= CALLOC_STRUCT(xlib_egl_driver
);
629 dpy
->Xdpy
= XOpenDisplay(NULL
);
632 _eglInitDriverFallbacks(&xdrv
->Base
);
633 xdrv
->Base
.API
.Initialize
= xlib_eglInitialize
;
634 xdrv
->Base
.API
.Terminate
= xlib_eglTerminate
;
635 xdrv
->Base
.API
.GetProcAddress
= xlib_eglGetProcAddress
;
636 xdrv
->Base
.API
.CreateContext
= xlib_eglCreateContext
;
637 xdrv
->Base
.API
.DestroyContext
= xlib_eglDestroyContext
;
638 xdrv
->Base
.API
.CreateWindowSurface
= xlib_eglCreateWindowSurface
;
639 xdrv
->Base
.API
.DestroySurface
= xlib_eglDestroySurface
;
640 xdrv
->Base
.API
.MakeCurrent
= xlib_eglMakeCurrent
;
641 xdrv
->Base
.API
.SwapBuffers
= xlib_eglSwapBuffers
;
643 xdrv
->Base
.ClientAPIsMask
= find_supported_apis();
644 if (xdrv
->Base
.ClientAPIsMask
== 0x0) {
645 /* the app isn't directly linked with any EGL-supprted APIs
646 * (such as libGLESv2.so) so use an EGL utility to see what
647 * APIs might be loaded dynamically on this system.
649 xdrv
->Base
.ClientAPIsMask
= _eglFindAPIs();
652 xdrv
->Base
.Name
= "Xlib/softpipe";
654 /* create one winsys and use it for all contexts/surfaces */
655 xdrv
->winsys
= create_sw_winsys();
656 xdrv
->winsys
->flush_frontbuffer
= flush_frontbuffer
;
658 xdrv
->screen
= softpipe_create_screen(xdrv
->winsys
);