Merge remote branch 'main/master' into radeon-rewrite
[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/internal/p_winsys_screen.h"
42 #include "util/u_memory.h"
43 #include "softpipe/sp_winsys.h"
44 #include "softpipe/sp_texture.h"
45
46 #include "eglconfig.h"
47 #include "eglconfigutil.h"
48 #include "eglcontext.h"
49 #include "egldisplay.h"
50 #include "egldriver.h"
51 #include "eglglobals.h"
52 #include "egllog.h"
53 #include "eglsurface.h"
54
55 #include "state_tracker/st_public.h"
56
57 #include "sw_winsys.h"
58
59
60 /** subclass of _EGLDriver */
61 struct xlib_egl_driver
62 {
63 _EGLDriver Base; /**< base class */
64
65 struct pipe_winsys *winsys;
66 struct pipe_screen *screen;
67 };
68
69
70 /** subclass of _EGLContext */
71 struct xlib_egl_context
72 {
73 _EGLContext Base; /**< base class */
74
75 struct pipe_context *pipe; /**< Gallium driver context */
76 struct st_context *Context; /**< Mesa/gallium state tracker context */
77 };
78
79
80 /** subclass of _EGLSurface */
81 struct xlib_egl_surface
82 {
83 _EGLSurface Base; /**< base class */
84
85 Display *Dpy; /**< The X Display of the window */
86 Window Win; /**< The user-created window ID */
87 GC Gc;
88 XVisualInfo VisInfo;
89
90 struct pipe_winsys *winsys;
91
92 struct st_framebuffer *Framebuffer;
93 };
94
95
96 /** cast wrapper */
97 static INLINE struct xlib_egl_driver *
98 xlib_egl_driver(_EGLDriver *drv)
99 {
100 return (struct xlib_egl_driver *) drv;
101 }
102
103
104 static struct xlib_egl_surface *
105 lookup_surface(EGLSurface surf)
106 {
107 _EGLSurface *surface = _eglLookupSurface(surf);
108 return (struct xlib_egl_surface *) surface;
109 }
110
111
112 static struct xlib_egl_context *
113 lookup_context(EGLContext surf)
114 {
115 _EGLContext *context = _eglLookupContext(surf);
116 return (struct xlib_egl_context *) context;
117 }
118
119
120 static unsigned int
121 bitcount(unsigned int n)
122 {
123 unsigned int bits;
124 for (bits = 0; n > 0; n = n >> 1) {
125 bits += (n & 1);
126 }
127 return bits;
128 }
129
130
131 /**
132 * Create the EGLConfigs. (one per X visual)
133 */
134 static void
135 create_configs(_EGLDriver *drv, EGLDisplay dpy)
136 {
137 static const EGLint all_apis = (EGL_OPENGL_ES_BIT |
138 EGL_OPENGL_ES2_BIT |
139 EGL_OPENVG_BIT |
140 EGL_OPENGL_BIT);
141 _EGLDisplay *disp = _eglLookupDisplay(dpy);
142 XVisualInfo *visInfo, visTemplate;
143 int num_visuals, i;
144
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);
149 if (!visInfo) {
150 printf("egl_xlib.c: couldn't get any X visuals\n");
151 abort();
152 }
153
154 for (i = 0; i < num_visuals; i++) {
155 _EGLConfig *config = calloc(1, sizeof(_EGLConfig));
156 int id = i + 1;
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;
161 int zbits = 24;
162 int sbits = 8;
163 int visid = visInfo[i].visualid;
164 #if defined(__cplusplus) || defined(c_plusplus)
165 int vistype = visInfo[i].c_class;
166 #else
167 int vistype = visInfo[i].class;
168 #endif
169
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);
184
185 _eglAddConfig(disp, config);
186 }
187 }
188
189
190 /**
191 * Called via eglInitialize(), drv->API.Initialize().
192 */
193 static EGLBoolean
194 xlib_eglInitialize(_EGLDriver *drv, EGLDisplay dpy,
195 EGLint *minor, EGLint *major)
196 {
197 create_configs(drv, dpy);
198
199 drv->Initialized = EGL_TRUE;
200
201 /* we're supporting EGL 1.4 */
202 *minor = 1;
203 *major = 4;
204
205 return EGL_TRUE;
206 }
207
208
209 /**
210 * Called via eglTerminate(), drv->API.Terminate().
211 */
212 static EGLBoolean
213 xlib_eglTerminate(_EGLDriver *drv, EGLDisplay dpy)
214 {
215 return EGL_TRUE;
216 }
217
218
219 static _EGLProc
220 xlib_eglGetProcAddress(const char *procname)
221 {
222 return (_EGLProc) st_get_proc_address(procname);
223 }
224
225
226 static void
227 get_drawable_visual_info(Display *dpy, Drawable d, XVisualInfo *visInfo)
228 {
229 XWindowAttributes attr;
230 XVisualInfo visTemp, *vis;
231 int num_visuals;
232
233 XGetWindowAttributes(dpy, d, &attr);
234
235 visTemp.screen = DefaultScreen(dpy);
236 visTemp.visualid = attr.visual->visualid;
237 vis = XGetVisualInfo(dpy,
238 (VisualScreenMask | VisualIDMask),
239 &visTemp, &num_visuals);
240 if (vis)
241 *visInfo = *vis;
242
243 XFree(vis);
244 }
245
246
247
248 /** Get size of given window */
249 static Status
250 get_drawable_size(Display *dpy, Drawable d, uint *width, uint *height)
251 {
252 Window root;
253 Status stat;
254 int xpos, ypos;
255 unsigned int w, h, bw, depth;
256 stat = XGetGeometry(dpy, d, &root, &xpos, &ypos, &w, &h, &bw, &depth);
257 *width = w;
258 *height = h;
259 return stat;
260 }
261
262
263 static void
264 check_and_update_buffer_size(struct xlib_egl_surface *surface)
265 {
266 uint width, height;
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;
271 }
272
273
274
275 static void
276 display_surface(struct pipe_winsys *pws,
277 struct pipe_surface *psurf,
278 struct xlib_egl_surface *xsurf)
279 {
280 struct softpipe_texture *spt = softpipe_texture(psurf->texture);
281 XImage *ximage;
282 void *data;
283
284 ximage = XCreateImage(xsurf->Dpy,
285 xsurf->VisInfo.visual,
286 xsurf->VisInfo.depth,
287 ZPixmap, 0, /* format, offset */
288 NULL, /* data */
289 0, 0, /* size */
290 32, /* bitmap_pad */
291 0); /* bytes_per_line */
292
293
294 assert(ximage->format);
295 assert(ximage->bitmap_unit);
296
297 data = pws->buffer_map(pws, spt->buffer, 0);
298
299 /* update XImage's fields */
300 ximage->data = data;
301 ximage->width = psurf->width;
302 ximage->height = psurf->height;
303 ximage->bytes_per_line = spt->stride[psurf->level];
304
305 XPutImage(xsurf->Dpy, xsurf->Win, xsurf->Gc,
306 ximage, 0, 0, 0, 0, psurf->width, psurf->height);
307
308 XSync(xsurf->Dpy, 0);
309
310 ximage->data = NULL;
311 XDestroyImage(ximage);
312
313 pws->buffer_unmap(pws, spt->buffer);
314 }
315
316
317
318 /** Display gallium surface in X window */
319 static void
320 flush_frontbuffer(struct pipe_winsys *pws,
321 struct pipe_surface *psurf,
322 void *context_private)
323 {
324 struct xlib_egl_surface *xsurf = (struct xlib_egl_surface *) context_private;
325 display_surface(pws, psurf, xsurf);
326 }
327
328
329
330 /**
331 * Called via eglCreateContext(), drv->API.CreateContext().
332 */
333 static EGLContext
334 xlib_eglCreateContext(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
335 EGLContext share_list, const EGLint *attrib_list)
336 {
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;
342
343 ctx = CALLOC_STRUCT(xlib_egl_context);
344 if (!ctx)
345 return EGL_NO_CONTEXT;
346
347 /* let EGL lib init the common stuff */
348 if (!_eglInitContext(drv, dpy, &ctx->Base, config, attrib_list)) {
349 free(ctx);
350 return EGL_NO_CONTEXT;
351 }
352
353 /* API-dependent context creation */
354 switch (ctx->Base.ClientAPI) {
355 case EGL_OPENVG_API:
356 case EGL_OPENGL_ES_API:
357 _eglLog(_EGL_DEBUG, "Create Context for ES version %d\n",
358 ctx->Base.ClientVersion);
359 /* fall-through */
360 case EGL_OPENGL_API:
361 /* create a softpipe context */
362 ctx->pipe = softpipe_create(xdrv->screen, xdrv->winsys, NULL);
363 /* Now do xlib / state tracker inits here */
364 _eglConfigToContextModesRec(conf, &visual);
365 ctx->Context = st_create_context(ctx->pipe, &visual, share_ctx);
366 break;
367 default:
368 _eglError(EGL_BAD_MATCH, "eglCreateContext(unsupported API)");
369 free(ctx);
370 return EGL_NO_CONTEXT;
371 }
372
373 _eglSaveContext(&ctx->Base);
374
375 return _eglGetContextHandle(&ctx->Base);
376 }
377
378
379 static EGLBoolean
380 xlib_eglDestroyContext(_EGLDriver *drv, EGLDisplay dpy, EGLContext ctx)
381 {
382 struct xlib_egl_context *context = lookup_context(ctx);
383 if (context) {
384 if (context->Base.IsBound) {
385 context->Base.DeletePending = EGL_TRUE;
386 }
387 else {
388 /* API-dependent clean-up */
389 switch (context->Base.ClientAPI) {
390 case EGL_OPENGL_ES_API:
391 /* fall-through */
392 case EGL_OPENGL_API:
393 st_destroy_context(context->Context);
394 break;
395 default:
396 assert(0);
397 }
398 free(context);
399 }
400 return EGL_TRUE;
401 }
402 else {
403 _eglError(EGL_BAD_CONTEXT, "eglDestroyContext");
404 return EGL_TRUE;
405 }
406 }
407
408
409 /**
410 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
411 */
412 static EGLBoolean
413 xlib_eglMakeCurrent(_EGLDriver *drv, EGLDisplay dpy,
414 EGLSurface draw, EGLSurface read, EGLContext ctx)
415 {
416 struct xlib_egl_context *context = lookup_context(ctx);
417 struct xlib_egl_surface *draw_surf = lookup_surface(draw);
418 struct xlib_egl_surface *read_surf = lookup_surface(read);
419
420 if (!_eglMakeCurrent(drv, dpy, draw, read, context))
421 return EGL_FALSE;
422
423 st_make_current((context ? context->Context : NULL),
424 (draw_surf ? draw_surf->Framebuffer : NULL),
425 (read_surf ? read_surf->Framebuffer : NULL));
426
427 if (draw_surf)
428 check_and_update_buffer_size(draw_surf);
429 if (read_surf && read_surf != draw_surf)
430 check_and_update_buffer_size(draw_surf);
431
432 return EGL_TRUE;
433 }
434
435
436 static enum pipe_format
437 choose_color_format(const __GLcontextModes *visual)
438 {
439 if (visual->redBits == 8 &&
440 visual->greenBits == 8 &&
441 visual->blueBits == 8 &&
442 visual->alphaBits == 8) {
443 /* XXX this really also depends on the ordering of R,G,B,A */
444 return PIPE_FORMAT_A8R8G8B8_UNORM;
445 }
446 else {
447 assert(0);
448 return PIPE_FORMAT_NONE;
449 }
450 }
451
452
453 static enum pipe_format
454 choose_depth_format(const __GLcontextModes *visual)
455 {
456 if (visual->depthBits > 0)
457 return PIPE_FORMAT_S8Z24_UNORM;
458 else
459 return PIPE_FORMAT_NONE;
460 }
461
462
463 static enum pipe_format
464 choose_stencil_format(const __GLcontextModes *visual)
465 {
466 if (visual->stencilBits > 0)
467 return PIPE_FORMAT_S8Z24_UNORM;
468 else
469 return PIPE_FORMAT_NONE;
470 }
471
472
473 /**
474 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
475 */
476 static EGLSurface
477 xlib_eglCreateWindowSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
478 NativeWindowType window, const EGLint *attrib_list)
479 {
480 struct xlib_egl_driver *xdrv = xlib_egl_driver(drv);
481 _EGLDisplay *disp = _eglLookupDisplay(dpy);
482 _EGLConfig *conf = _eglLookupConfig(drv, dpy, config);
483
484 struct xlib_egl_surface *surf;
485 __GLcontextModes visual;
486 uint width, height;
487
488 surf = CALLOC_STRUCT(xlib_egl_surface);
489 if (!surf)
490 return EGL_NO_SURFACE;
491
492 /* Let EGL lib init the common stuff */
493 if (!_eglInitSurface(drv, dpy, &surf->Base, EGL_WINDOW_BIT,
494 config, attrib_list)) {
495 free(surf);
496 return EGL_NO_SURFACE;
497 }
498
499 _eglSaveSurface(&surf->Base);
500
501 /*
502 * Now init the Xlib and gallium stuff
503 */
504 surf->Win = (Window) window; /* The X window ID */
505 surf->Dpy = disp->Xdpy; /* The X display */
506 surf->Gc = XCreateGC(surf->Dpy, surf->Win, 0, NULL);
507
508 surf->winsys = xdrv->winsys;
509
510 _eglConfigToContextModesRec(conf, &visual);
511 get_drawable_size(surf->Dpy, surf->Win, &width, &height);
512 get_drawable_visual_info(surf->Dpy, surf->Win, &surf->VisInfo);
513
514 surf->Base.Width = width;
515 surf->Base.Height = height;
516
517 /* Create GL statetracker framebuffer */
518 surf->Framebuffer = st_create_framebuffer(&visual,
519 choose_color_format(&visual),
520 choose_depth_format(&visual),
521 choose_stencil_format(&visual),
522 width, height,
523 (void *) surf);
524
525 st_resize_framebuffer(surf->Framebuffer, width, height);
526
527 return _eglGetSurfaceHandle(&surf->Base);
528 }
529
530
531 static EGLBoolean
532 xlib_eglDestroySurface(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface)
533 {
534 struct xlib_egl_surface *surf = lookup_surface(surface);
535 if (surf) {
536 _eglHashRemove(_eglGlobal.Surfaces, (EGLuint) surface);
537 if (surf->Base.IsBound) {
538 surf->Base.DeletePending = EGL_TRUE;
539 }
540 else {
541 XFreeGC(surf->Dpy, surf->Gc);
542 st_unreference_framebuffer(surf->Framebuffer);
543 free(surf);
544 }
545 return EGL_TRUE;
546 }
547 else {
548 _eglError(EGL_BAD_SURFACE, "eglDestroySurface");
549 return EGL_FALSE;
550 }
551 }
552
553
554 static EGLBoolean
555 xlib_eglSwapBuffers(_EGLDriver *drv, EGLDisplay dpy, EGLSurface draw)
556 {
557 /* error checking step: */
558 if (!_eglSwapBuffers(drv, dpy, draw))
559 return EGL_FALSE;
560
561 {
562 struct xlib_egl_surface *xsurf = lookup_surface(draw);
563 struct pipe_winsys *pws = xsurf->winsys;
564 struct pipe_surface *psurf;
565
566 st_get_framebuffer_surface(xsurf->Framebuffer, ST_SURFACE_BACK_LEFT,
567 &psurf);
568
569 st_notify_swapbuffers(xsurf->Framebuffer);
570
571 display_surface(pws, psurf, xsurf);
572
573 check_and_update_buffer_size(xsurf);
574 }
575
576 return EGL_TRUE;
577 }
578
579
580 /**
581 * Determine which API(s) is(are) present by looking for some specific
582 * global symbols.
583 */
584 static EGLint
585 find_supported_apis(void)
586 {
587 EGLint mask = 0;
588 void *handle;
589
590 handle = dlopen(NULL, 0);
591
592 if (dlsym(handle, "st_api_OpenGL_ES1"))
593 mask |= EGL_OPENGL_ES_BIT;
594
595 if (dlsym(handle, "st_api_OpenGL_ES2"))
596 mask |= EGL_OPENGL_ES2_BIT;
597
598 if (dlsym(handle, "st_api_OpenGL"))
599 mask |= EGL_OPENGL_BIT;
600
601 if (dlsym(handle, "st_api_OpenVG"))
602 mask |= EGL_OPENVG_BIT;
603
604 dlclose(handle);
605
606 return mask;
607 }
608
609
610 /**
611 * This is the main entrypoint into the driver.
612 * Called by libEGL to instantiate an _EGLDriver object.
613 */
614 _EGLDriver *
615 _eglMain(_EGLDisplay *dpy, const char *args)
616 {
617 struct xlib_egl_driver *xdrv;
618
619 _eglLog(_EGL_INFO, "Entering EGL/Xlib _eglMain(%s)", args);
620
621 xdrv = CALLOC_STRUCT(xlib_egl_driver);
622 if (!xdrv)
623 return NULL;
624
625 if (!dpy->Xdpy) {
626 dpy->Xdpy = XOpenDisplay(NULL);
627 }
628
629 _eglInitDriverFallbacks(&xdrv->Base);
630 xdrv->Base.API.Initialize = xlib_eglInitialize;
631 xdrv->Base.API.Terminate = xlib_eglTerminate;
632 xdrv->Base.API.GetProcAddress = xlib_eglGetProcAddress;
633 xdrv->Base.API.CreateContext = xlib_eglCreateContext;
634 xdrv->Base.API.DestroyContext = xlib_eglDestroyContext;
635 xdrv->Base.API.CreateWindowSurface = xlib_eglCreateWindowSurface;
636 xdrv->Base.API.DestroySurface = xlib_eglDestroySurface;
637 xdrv->Base.API.MakeCurrent = xlib_eglMakeCurrent;
638 xdrv->Base.API.SwapBuffers = xlib_eglSwapBuffers;
639
640 xdrv->Base.ClientAPIsMask = find_supported_apis();
641 if (xdrv->Base.ClientAPIsMask == 0x0) {
642 /* the app isn't directly linked with any EGL-supprted APIs
643 * (such as libGLESv2.so) so use an EGL utility to see what
644 * APIs might be loaded dynamically on this system.
645 */
646 xdrv->Base.ClientAPIsMask = _eglFindAPIs();
647 }
648
649 xdrv->Base.Name = "Xlib/softpipe";
650
651 /* create one winsys and use it for all contexts/surfaces */
652 xdrv->winsys = create_sw_winsys();
653 xdrv->winsys->flush_frontbuffer = flush_frontbuffer;
654
655 xdrv->screen = softpipe_create_screen(xdrv->winsys);
656
657 return &xdrv->Base;
658 }
659