042d7a54571001fb5c83add608c6bb08f1993416
[mesa.git] / src / gallium / winsys / egl_xlib / egl_xlib.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /**
29 * EGL / softpipe / xlib winsys module
30 *
31 * Authors: Brian Paul
32 */
33
34
35 #include <dlfcn.h>
36 #include <X11/Xutil.h>
37
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"
44
45 #include "eglconfig.h"
46 #include "eglconfigutil.h"
47 #include "eglcontext.h"
48 #include "egldisplay.h"
49 #include "egldriver.h"
50 #include "eglglobals.h"
51 #include "egllog.h"
52 #include "eglsurface.h"
53
54 #include "state_tracker/st_public.h"
55
56 #include "sw_winsys.h"
57
58
59 /** subclass of _EGLDriver */
60 struct xlib_egl_driver
61 {
62 _EGLDriver Base; /**< base class */
63
64 struct pipe_winsys *winsys;
65 struct pipe_screen *screen;
66 };
67
68
69 /** subclass of _EGLContext */
70 struct xlib_egl_context
71 {
72 _EGLContext Base; /**< base class */
73
74 struct pipe_context *pipe; /**< Gallium driver context */
75 struct st_context *Context; /**< Mesa/gallium state tracker context */
76 };
77
78
79 /** subclass of _EGLSurface */
80 struct xlib_egl_surface
81 {
82 _EGLSurface Base; /**< base class */
83
84 Display *Dpy; /**< The X Display of the window */
85 Window Win; /**< The user-created window ID */
86 GC Gc;
87 XVisualInfo VisInfo;
88
89 struct pipe_winsys *winsys;
90
91 struct st_framebuffer *Framebuffer;
92 };
93
94
95 /** cast wrapper */
96 static INLINE struct xlib_egl_driver *
97 xlib_egl_driver(_EGLDriver *drv)
98 {
99 return (struct xlib_egl_driver *) drv;
100 }
101
102
103 static struct xlib_egl_surface *
104 lookup_surface(EGLSurface surf)
105 {
106 _EGLSurface *surface = _eglLookupSurface(surf);
107 return (struct xlib_egl_surface *) surface;
108 }
109
110
111 static struct xlib_egl_context *
112 lookup_context(EGLContext surf)
113 {
114 _EGLContext *context = _eglLookupContext(surf);
115 return (struct xlib_egl_context *) context;
116 }
117
118
119 static unsigned int
120 bitcount(unsigned int n)
121 {
122 unsigned int bits;
123 for (bits = 0; n > 0; n = n >> 1) {
124 bits += (n & 1);
125 }
126 return bits;
127 }
128
129
130 /**
131 * Create the EGLConfigs. (one per X visual)
132 */
133 static void
134 create_configs(_EGLDriver *drv, EGLDisplay dpy)
135 {
136 _EGLDisplay *disp = _eglLookupDisplay(dpy);
137 XVisualInfo *visInfo, visTemplate;
138 int num_visuals, i;
139
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);
144 if (!visInfo) {
145 printf("egl_xlib.c: couldn't get any X visuals\n");
146 abort();
147 }
148
149 for (i = 0; i < num_visuals; i++) {
150 _EGLConfig *config = calloc(1, sizeof(_EGLConfig));
151 int id = i + 1;
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;
156 int zbits = 24;
157 int sbits = 8;
158 int visid = visInfo[i].visualid;
159 #if defined(__cplusplus) || defined(c_plusplus)
160 int vistype = visInfo[i].c_class;
161 #else
162 int vistype = visInfo[i].class;
163 #endif
164
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);
176
177 _eglAddConfig(disp, config);
178 }
179 }
180
181
182 /**
183 * Called via eglInitialize(), drv->API.Initialize().
184 */
185 static EGLBoolean
186 xlib_eglInitialize(_EGLDriver *drv, EGLDisplay dpy,
187 EGLint *minor, EGLint *major)
188 {
189 create_configs(drv, dpy);
190
191 drv->Initialized = EGL_TRUE;
192
193 /* we're supporting EGL 1.4 */
194 *minor = 1;
195 *major = 4;
196
197 return EGL_TRUE;
198 }
199
200
201 /**
202 * Called via eglTerminate(), drv->API.Terminate().
203 */
204 static EGLBoolean
205 xlib_eglTerminate(_EGLDriver *drv, EGLDisplay dpy)
206 {
207 return EGL_TRUE;
208 }
209
210
211 static _EGLProc
212 xlib_eglGetProcAddress(const char *procname)
213 {
214 return (_EGLProc) st_get_proc_address(procname);
215 }
216
217
218 static void
219 get_drawable_visual_info(Display *dpy, Drawable d, XVisualInfo *visInfo)
220 {
221 XWindowAttributes attr;
222 XVisualInfo visTemp, *vis;
223 int num_visuals;
224
225 XGetWindowAttributes(dpy, d, &attr);
226
227 visTemp.screen = DefaultScreen(dpy);
228 visTemp.visualid = attr.visual->visualid;
229 vis = XGetVisualInfo(dpy,
230 (VisualScreenMask | VisualIDMask),
231 &visTemp, &num_visuals);
232 if (vis)
233 *visInfo = *vis;
234
235 XFree(vis);
236 }
237
238
239
240 /** Get size of given window */
241 static Status
242 get_drawable_size(Display *dpy, Drawable d, uint *width, uint *height)
243 {
244 Window root;
245 Status stat;
246 int xpos, ypos;
247 unsigned int w, h, bw, depth;
248 stat = XGetGeometry(dpy, d, &root, &xpos, &ypos, &w, &h, &bw, &depth);
249 *width = w;
250 *height = h;
251 return stat;
252 }
253
254
255 static void
256 check_and_update_buffer_size(struct xlib_egl_surface *surface)
257 {
258 uint width, height;
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;
263 }
264
265
266
267 static void
268 display_surface(struct pipe_winsys *pws,
269 struct pipe_surface *psurf,
270 struct xlib_egl_surface *xsurf)
271 {
272 XImage *ximage;
273 void *data;
274
275 ximage = XCreateImage(xsurf->Dpy,
276 xsurf->VisInfo.visual,
277 xsurf->VisInfo.depth,
278 ZPixmap, 0, /* format, offset */
279 NULL, /* data */
280 0, 0, /* size */
281 32, /* bitmap_pad */
282 0); /* bytes_per_line */
283
284
285 assert(ximage->format);
286 assert(ximage->bitmap_unit);
287
288 data = pws->buffer_map(pws, psurf->buffer, 0);
289
290 /* update XImage's fields */
291 ximage->data = data;
292 ximage->width = psurf->width;
293 ximage->height = psurf->height;
294 ximage->bytes_per_line = psurf->stride;
295
296 XPutImage(xsurf->Dpy, xsurf->Win, xsurf->Gc,
297 ximage, 0, 0, 0, 0, psurf->width, psurf->height);
298
299 XSync(xsurf->Dpy, 0);
300
301 ximage->data = NULL;
302 XDestroyImage(ximage);
303
304 pws->buffer_unmap(pws, psurf->buffer);
305 }
306
307
308
309 /** Display gallium surface in X window */
310 static void
311 flush_frontbuffer(struct pipe_winsys *pws,
312 struct pipe_surface *psurf,
313 void *context_private)
314 {
315 struct xlib_egl_surface *xsurf = (struct xlib_egl_surface *) context_private;
316 display_surface(pws, psurf, xsurf);
317 }
318
319
320
321 /**
322 * Called via eglCreateContext(), drv->API.CreateContext().
323 */
324 static EGLContext
325 xlib_eglCreateContext(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
326 EGLContext share_list, const EGLint *attrib_list)
327 {
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;
333
334 ctx = CALLOC_STRUCT(xlib_egl_context);
335 if (!ctx)
336 return EGL_NO_CONTEXT;
337
338 /* let EGL lib init the common stuff */
339 if (!_eglInitContext(drv, dpy, &ctx->Base, config, attrib_list)) {
340 free(ctx);
341 return EGL_NO_CONTEXT;
342 }
343
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);
349 /* fall-through */
350 case EGL_OPENGL_API:
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);
356 break;
357 default:
358 _eglError(EGL_BAD_MATCH, "eglCreateContext(unsupported API)");
359 free(ctx);
360 return EGL_NO_CONTEXT;
361 }
362
363 _eglSaveContext(&ctx->Base);
364
365 return _eglGetContextHandle(&ctx->Base);
366 }
367
368
369 static EGLBoolean
370 xlib_eglDestroyContext(_EGLDriver *drv, EGLDisplay dpy, EGLContext ctx)
371 {
372 struct xlib_egl_context *context = lookup_context(ctx);
373 if (context) {
374 if (context->Base.IsBound) {
375 context->Base.DeletePending = EGL_TRUE;
376 }
377 else {
378 /* API-dependent clean-up */
379 switch (context->Base.ClientAPI) {
380 case EGL_OPENGL_ES_API:
381 /* fall-through */
382 case EGL_OPENGL_API:
383 st_destroy_context(context->Context);
384 break;
385 default:
386 assert(0);
387 }
388 free(context);
389 }
390 return EGL_TRUE;
391 }
392 else {
393 _eglError(EGL_BAD_CONTEXT, "eglDestroyContext");
394 return EGL_TRUE;
395 }
396 }
397
398
399 /**
400 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
401 */
402 static EGLBoolean
403 xlib_eglMakeCurrent(_EGLDriver *drv, EGLDisplay dpy,
404 EGLSurface draw, EGLSurface read, EGLContext ctx)
405 {
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);
409
410 if (!_eglMakeCurrent(drv, dpy, draw, read, context))
411 return EGL_FALSE;
412
413 st_make_current((context ? context->Context : NULL),
414 (draw_surf ? draw_surf->Framebuffer : NULL),
415 (read_surf ? read_surf->Framebuffer : NULL));
416
417 if (draw_surf)
418 check_and_update_buffer_size(draw_surf);
419 if (read_surf && read_surf != draw_surf)
420 check_and_update_buffer_size(draw_surf);
421
422 return EGL_TRUE;
423 }
424
425
426 static enum pipe_format
427 choose_color_format(const __GLcontextModes *visual)
428 {
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;
435 }
436 else {
437 assert(0);
438 return PIPE_FORMAT_NONE;
439 }
440 }
441
442
443 static enum pipe_format
444 choose_depth_format(const __GLcontextModes *visual)
445 {
446 if (visual->depthBits > 0)
447 return PIPE_FORMAT_S8Z24_UNORM;
448 else
449 return PIPE_FORMAT_NONE;
450 }
451
452
453 static enum pipe_format
454 choose_stencil_format(const __GLcontextModes *visual)
455 {
456 if (visual->stencilBits > 0)
457 return PIPE_FORMAT_S8Z24_UNORM;
458 else
459 return PIPE_FORMAT_NONE;
460 }
461
462
463 /**
464 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
465 */
466 static EGLSurface
467 xlib_eglCreateWindowSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
468 NativeWindowType window, const EGLint *attrib_list)
469 {
470 struct xlib_egl_driver *xdrv = xlib_egl_driver(drv);
471 _EGLDisplay *disp = _eglLookupDisplay(dpy);
472 _EGLConfig *conf = _eglLookupConfig(drv, dpy, config);
473
474 struct xlib_egl_surface *surf;
475 __GLcontextModes visual;
476 uint width, height;
477
478 surf = CALLOC_STRUCT(xlib_egl_surface);
479 if (!surf)
480 return EGL_NO_SURFACE;
481
482 /* Let EGL lib init the common stuff */
483 if (!_eglInitSurface(drv, dpy, &surf->Base, EGL_WINDOW_BIT,
484 config, attrib_list)) {
485 free(surf);
486 return EGL_NO_SURFACE;
487 }
488
489 _eglSaveSurface(&surf->Base);
490
491 /*
492 * Now init the Xlib and gallium stuff
493 */
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);
497
498 surf->winsys = xdrv->winsys;
499
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);
503
504 surf->Base.Width = width;
505 surf->Base.Height = height;
506
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),
512 width, height,
513 (void *) surf);
514
515 st_resize_framebuffer(surf->Framebuffer, width, height);
516
517 return _eglGetSurfaceHandle(&surf->Base);
518 }
519
520
521 static EGLBoolean
522 xlib_eglDestroySurface(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface)
523 {
524 struct xlib_egl_surface *surf = lookup_surface(surface);
525 if (surf) {
526 _eglHashRemove(_eglGlobal.Surfaces, (EGLuint) surface);
527 if (surf->Base.IsBound) {
528 surf->Base.DeletePending = EGL_TRUE;
529 }
530 else {
531 st_unreference_framebuffer(&surf->Framebuffer);
532 free(surf);
533 }
534 return EGL_TRUE;
535 }
536 else {
537 _eglError(EGL_BAD_SURFACE, "eglDestroySurface");
538 return EGL_FALSE;
539 }
540 }
541
542
543 static EGLBoolean
544 xlib_eglSwapBuffers(_EGLDriver *drv, EGLDisplay dpy, EGLSurface draw)
545 {
546 /* error checking step: */
547 if (!_eglSwapBuffers(drv, dpy, draw))
548 return EGL_FALSE;
549
550 {
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);
555
556 st_notify_swapbuffers(xsurf->Framebuffer);
557
558 display_surface(pws, psurf, xsurf);
559
560 check_and_update_buffer_size(xsurf);
561 }
562
563 return EGL_TRUE;
564 }
565
566
567 /**
568 * Determine which API(s) is(are) present by looking for some specific
569 * global symbols.
570 */
571 static EGLint
572 find_supported_apis(void)
573 {
574 EGLint mask = 0;
575 void *handle;
576
577 handle = dlopen(NULL, 0);
578
579 if (dlsym(handle, "st_api_OpenGL_ES1"))
580 mask |= EGL_OPENGL_ES_BIT;
581
582 if (dlsym(handle, "st_api_OpenGL_ES2"))
583 mask |= EGL_OPENGL_ES2_BIT;
584
585 if (dlsym(handle, "st_api_OpenGL"))
586 mask |= EGL_OPENGL_BIT;
587
588 if (dlsym(handle, "st_api_OpenVG"))
589 mask |= EGL_OPENVG_BIT;
590
591 dlclose(handle);
592
593 return mask;
594 }
595
596
597 /**
598 * This is the main entrypoint into the driver.
599 * Called by libEGL to instantiate an _EGLDriver object.
600 */
601 _EGLDriver *
602 _eglMain(_EGLDisplay *dpy, const char *args)
603 {
604 struct xlib_egl_driver *xdrv;
605
606 _eglLog(_EGL_INFO, "Entering EGL/Xlib _eglMain(%s)", args);
607
608 xdrv = CALLOC_STRUCT(xlib_egl_driver);
609 if (!xdrv)
610 return NULL;
611
612 if (!dpy->Xdpy) {
613 dpy->Xdpy = XOpenDisplay(NULL);
614 }
615
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;
626
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.
632 */
633 xdrv->Base.ClientAPIsMask = _eglFindAPIs();
634 }
635
636 xdrv->Base.Name = "Xlib/softpipe";
637
638 /* create one winsys and use it for all contexts/surfaces */
639 xdrv->winsys = create_sw_winsys();
640 xdrv->winsys->flush_frontbuffer = flush_frontbuffer;
641
642 xdrv->screen = softpipe_create_screen(xdrv->winsys);
643
644 return &xdrv->Base;
645 }
646