egl: added null ptr checks
[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
176 _eglAddConfig(disp, config);
177 }
178 }
179
180
181 /**
182 * Called via eglInitialize(), drv->API.Initialize().
183 */
184 static EGLBoolean
185 xlib_eglInitialize(_EGLDriver *drv, EGLDisplay dpy,
186 EGLint *minor, EGLint *major)
187 {
188 create_configs(drv, dpy);
189
190 drv->Initialized = EGL_TRUE;
191
192 /* we're supporting EGL 1.4 */
193 *minor = 1;
194 *major = 4;
195
196 return EGL_TRUE;
197 }
198
199
200 /**
201 * Called via eglTerminate(), drv->API.Terminate().
202 */
203 static EGLBoolean
204 xlib_eglTerminate(_EGLDriver *drv, EGLDisplay dpy)
205 {
206 return EGL_TRUE;
207 }
208
209
210 static _EGLProc
211 xlib_eglGetProcAddress(const char *procname)
212 {
213 return (_EGLProc) st_get_proc_address(procname);
214 }
215
216
217 static void
218 get_drawable_visual_info(Display *dpy, Drawable d, XVisualInfo *visInfo)
219 {
220 XWindowAttributes attr;
221 XVisualInfo visTemp, *vis;
222 int num_visuals;
223
224 XGetWindowAttributes(dpy, d, &attr);
225
226 visTemp.screen = DefaultScreen(dpy);
227 visTemp.visualid = attr.visual->visualid;
228 vis = XGetVisualInfo(dpy,
229 (VisualScreenMask | VisualIDMask),
230 &visTemp, &num_visuals);
231 if (vis)
232 *visInfo = *vis;
233
234 XFree(vis);
235 }
236
237
238
239 /** Get size of given window */
240 static Status
241 get_drawable_size(Display *dpy, Drawable d, uint *width, uint *height)
242 {
243 Window root;
244 Status stat;
245 int xpos, ypos;
246 unsigned int w, h, bw, depth;
247 stat = XGetGeometry(dpy, d, &root, &xpos, &ypos, &w, &h, &bw, &depth);
248 *width = w;
249 *height = h;
250 return stat;
251 }
252
253
254 static void
255 check_and_update_buffer_size(struct xlib_egl_surface *surface)
256 {
257 uint width, height;
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;
262 }
263
264
265
266 static void
267 display_surface(struct pipe_winsys *pws,
268 struct pipe_surface *psurf,
269 struct xlib_egl_surface *xsurf)
270 {
271 XImage *ximage;
272 void *data;
273
274 ximage = XCreateImage(xsurf->Dpy,
275 xsurf->VisInfo.visual,
276 xsurf->VisInfo.depth,
277 ZPixmap, 0, /* format, offset */
278 NULL, /* data */
279 0, 0, /* size */
280 32, /* bitmap_pad */
281 0); /* bytes_per_line */
282
283
284 assert(ximage->format);
285 assert(ximage->bitmap_unit);
286
287 data = pws->buffer_map(pws, psurf->buffer, 0);
288
289 /* update XImage's fields */
290 ximage->data = data;
291 ximage->width = psurf->width;
292 ximage->height = psurf->height;
293 ximage->bytes_per_line = psurf->pitch * psurf->cpp;
294
295 XPutImage(xsurf->Dpy, xsurf->Win, xsurf->Gc,
296 ximage, 0, 0, 0, 0, psurf->width, psurf->height);
297
298 XSync(xsurf->Dpy, 0);
299
300 ximage->data = NULL;
301 XDestroyImage(ximage);
302
303 pws->buffer_unmap(pws, psurf->buffer);
304 }
305
306
307
308 /** Display gallium surface in X window */
309 static void
310 flush_frontbuffer(struct pipe_winsys *pws,
311 struct pipe_surface *psurf,
312 void *context_private)
313 {
314 struct xlib_egl_surface *xsurf = (struct xlib_egl_surface *) context_private;
315 display_surface(pws, psurf, xsurf);
316 }
317
318
319
320 /**
321 * Called via eglCreateContext(), drv->API.CreateContext().
322 */
323 static EGLContext
324 xlib_eglCreateContext(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
325 EGLContext share_list, const EGLint *attrib_list)
326 {
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;
332
333 ctx = CALLOC_STRUCT(xlib_egl_context);
334 if (!ctx)
335 return EGL_NO_CONTEXT;
336
337 /* let EGL lib init the common stuff */
338 if (!_eglInitContext(drv, dpy, &ctx->Base, config, attrib_list)) {
339 free(ctx);
340 return EGL_NO_CONTEXT;
341 }
342
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);
348 /* fall-through */
349 case EGL_OPENGL_API:
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);
355 break;
356 default:
357 _eglError(EGL_BAD_MATCH, "eglCreateContext(unsupported API)");
358 free(ctx);
359 return EGL_NO_CONTEXT;
360 }
361
362 _eglSaveContext(&ctx->Base);
363
364 return _eglGetContextHandle(&ctx->Base);
365 }
366
367
368 static EGLBoolean
369 xlib_eglDestroyContext(_EGLDriver *drv, EGLDisplay dpy, EGLContext ctx)
370 {
371 struct xlib_egl_context *context = lookup_context(ctx);
372 if (context) {
373 if (context->Base.IsBound) {
374 context->Base.DeletePending = EGL_TRUE;
375 }
376 else {
377 /* API-dependent clean-up */
378 switch (context->Base.ClientAPI) {
379 case EGL_OPENGL_ES_API:
380 /* fall-through */
381 case EGL_OPENGL_API:
382 st_destroy_context(context->Context);
383 break;
384 default:
385 assert(0);
386 }
387 free(context);
388 }
389 return EGL_TRUE;
390 }
391 else {
392 _eglError(EGL_BAD_CONTEXT, "eglDestroyContext");
393 return EGL_TRUE;
394 }
395 }
396
397
398 /**
399 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
400 */
401 static EGLBoolean
402 xlib_eglMakeCurrent(_EGLDriver *drv, EGLDisplay dpy,
403 EGLSurface draw, EGLSurface read, EGLContext ctx)
404 {
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);
408
409 if (!_eglMakeCurrent(drv, dpy, draw, read, context))
410 return EGL_FALSE;
411
412 st_make_current((context ? context->Context : NULL),
413 (draw_surf ? draw_surf->Framebuffer : NULL),
414 (read_surf ? read_surf->Framebuffer : NULL));
415
416 if (draw_surf)
417 check_and_update_buffer_size(draw_surf);
418 if (read_surf && read_surf != draw_surf)
419 check_and_update_buffer_size(draw_surf);
420
421 return EGL_TRUE;
422 }
423
424
425 static enum pipe_format
426 choose_color_format(const __GLcontextModes *visual)
427 {
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;
434 }
435 else {
436 assert(0);
437 return PIPE_FORMAT_NONE;
438 }
439 }
440
441
442 static enum pipe_format
443 choose_depth_format(const __GLcontextModes *visual)
444 {
445 if (visual->depthBits > 0)
446 return PIPE_FORMAT_S8Z24_UNORM;
447 else
448 return PIPE_FORMAT_NONE;
449 }
450
451
452 static enum pipe_format
453 choose_stencil_format(const __GLcontextModes *visual)
454 {
455 if (visual->stencilBits > 0)
456 return PIPE_FORMAT_S8Z24_UNORM;
457 else
458 return PIPE_FORMAT_NONE;
459 }
460
461
462 /**
463 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
464 */
465 static EGLSurface
466 xlib_eglCreateWindowSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
467 NativeWindowType window, const EGLint *attrib_list)
468 {
469 struct xlib_egl_driver *xdrv = xlib_egl_driver(drv);
470 _EGLDisplay *disp = _eglLookupDisplay(dpy);
471 _EGLConfig *conf = _eglLookupConfig(drv, dpy, config);
472
473 struct xlib_egl_surface *surf;
474 __GLcontextModes visual;
475 uint width, height;
476
477 surf = CALLOC_STRUCT(xlib_egl_surface);
478 if (!surf)
479 return EGL_NO_SURFACE;
480
481 /* Let EGL lib init the common stuff */
482 if (!_eglInitSurface(drv, dpy, &surf->Base, EGL_WINDOW_BIT,
483 config, attrib_list)) {
484 free(surf);
485 return EGL_NO_SURFACE;
486 }
487
488 _eglSaveSurface(&surf->Base);
489
490 /*
491 * Now init the Xlib and gallium stuff
492 */
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);
496
497 surf->winsys = xdrv->winsys;
498
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);
502
503 surf->Base.Width = width;
504 surf->Base.Height = height;
505
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),
511 width, height,
512 (void *) surf);
513
514 st_resize_framebuffer(surf->Framebuffer, width, height);
515
516 return _eglGetSurfaceHandle(&surf->Base);
517 }
518
519
520 static EGLBoolean
521 xlib_eglDestroySurface(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface)
522 {
523 struct xlib_egl_surface *surf = lookup_surface(surface);
524 if (surf) {
525 _eglHashRemove(_eglGlobal.Surfaces, (EGLuint) surface);
526 if (surf->Base.IsBound) {
527 surf->Base.DeletePending = EGL_TRUE;
528 }
529 else {
530 st_unreference_framebuffer(&surf->Framebuffer);
531 free(surf);
532 }
533 return EGL_TRUE;
534 }
535 else {
536 _eglError(EGL_BAD_SURFACE, "eglDestroySurface");
537 return EGL_FALSE;
538 }
539 }
540
541
542 static EGLBoolean
543 xlib_eglSwapBuffers(_EGLDriver *drv, EGLDisplay dpy, EGLSurface draw)
544 {
545 /* error checking step: */
546 if (!_eglSwapBuffers(drv, dpy, draw))
547 return EGL_FALSE;
548
549 {
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);
554
555 st_notify_swapbuffers(xsurf->Framebuffer);
556
557 display_surface(pws, psurf, xsurf);
558
559 check_and_update_buffer_size(xsurf);
560 }
561
562 return EGL_TRUE;
563 }
564
565
566 /**
567 * Determine which API(s) is(are) present by looking for some specific
568 * global symbols.
569 */
570 static EGLint
571 find_supported_apis(void)
572 {
573 EGLint mask = 0;
574 void *handle;
575
576 handle = dlopen(NULL, 0);
577
578 if (dlsym(handle, "st_api_OpenGL_ES1"))
579 mask |= EGL_OPENGL_ES_BIT;
580
581 if (dlsym(handle, "st_api_OpenGL_ES2"))
582 mask |= EGL_OPENGL_ES2_BIT;
583
584 if (dlsym(handle, "st_api_OpenGL"))
585 mask |= EGL_OPENGL_BIT;
586
587 if (dlsym(handle, "st_api_OpenVG"))
588 mask |= EGL_OPENVG_BIT;
589
590 dlclose(handle);
591
592 return mask;
593 }
594
595
596 /**
597 * This is the main entrypoint into the driver.
598 * Called by libEGL to instantiate an _EGLDriver object.
599 */
600 _EGLDriver *
601 _eglMain(_EGLDisplay *dpy, const char *args)
602 {
603 struct xlib_egl_driver *xdrv;
604
605 _eglLog(_EGL_INFO, "Entering EGL/Xlib _eglMain(%s)", args);
606
607 xdrv = CALLOC_STRUCT(xlib_egl_driver);
608 if (!xdrv)
609 return NULL;
610
611 if (!dpy->Xdpy) {
612 dpy->Xdpy = XOpenDisplay(NULL);
613 }
614
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;
625
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.
631 */
632 xdrv->Base.ClientAPIsMask = _eglFindAPIs();
633 }
634
635 xdrv->Base.Name = "Xlib/softpipe";
636
637 /* create one winsys and use it for all contexts/surfaces */
638 xdrv->winsys = create_sw_winsys();
639 xdrv->winsys->flush_frontbuffer = flush_frontbuffer;
640
641 xdrv->screen = softpipe_create_screen(xdrv->winsys);
642
643 return &xdrv->Base;
644 }
645