i965: fix bugs in projective texture coordinates
[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);
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 case EGL_OPENVG_API:
392 /* fall-through */
393 case EGL_OPENGL_API:
394 st_destroy_context(context->Context);
395 break;
396 default:
397 assert(0);
398 }
399 free(context);
400 }
401 return EGL_TRUE;
402 }
403 else {
404 _eglError(EGL_BAD_CONTEXT, "eglDestroyContext");
405 return EGL_TRUE;
406 }
407 }
408
409
410 /**
411 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
412 */
413 static EGLBoolean
414 xlib_eglMakeCurrent(_EGLDriver *drv, EGLDisplay dpy,
415 EGLSurface draw, EGLSurface read, EGLContext ctx)
416 {
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);
420
421 if (!_eglMakeCurrent(drv, dpy, draw, read, context))
422 return EGL_FALSE;
423
424 st_make_current((context ? context->Context : NULL),
425 (draw_surf ? draw_surf->Framebuffer : NULL),
426 (read_surf ? read_surf->Framebuffer : NULL));
427
428 if (draw_surf)
429 check_and_update_buffer_size(draw_surf);
430 if (read_surf && read_surf != draw_surf)
431 check_and_update_buffer_size(draw_surf);
432
433 return EGL_TRUE;
434 }
435
436
437 static enum pipe_format
438 choose_color_format(const __GLcontextModes *visual)
439 {
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;
446 }
447 else {
448 assert(0);
449 return PIPE_FORMAT_NONE;
450 }
451 }
452
453
454 static enum pipe_format
455 choose_depth_format(const __GLcontextModes *visual)
456 {
457 if (visual->depthBits > 0)
458 return PIPE_FORMAT_S8Z24_UNORM;
459 else
460 return PIPE_FORMAT_NONE;
461 }
462
463
464 static enum pipe_format
465 choose_stencil_format(const __GLcontextModes *visual)
466 {
467 if (visual->stencilBits > 0)
468 return PIPE_FORMAT_S8Z24_UNORM;
469 else
470 return PIPE_FORMAT_NONE;
471 }
472
473
474 /**
475 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
476 */
477 static EGLSurface
478 xlib_eglCreateWindowSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
479 NativeWindowType window, const EGLint *attrib_list)
480 {
481 struct xlib_egl_driver *xdrv = xlib_egl_driver(drv);
482 _EGLDisplay *disp = _eglLookupDisplay(dpy);
483 _EGLConfig *conf = _eglLookupConfig(drv, dpy, config);
484
485 struct xlib_egl_surface *surf;
486 __GLcontextModes visual;
487 uint width, height;
488
489 surf = CALLOC_STRUCT(xlib_egl_surface);
490 if (!surf)
491 return EGL_NO_SURFACE;
492
493 /* Let EGL lib init the common stuff */
494 if (!_eglInitSurface(drv, dpy, &surf->Base, EGL_WINDOW_BIT,
495 config, attrib_list)) {
496 free(surf);
497 return EGL_NO_SURFACE;
498 }
499
500 _eglSaveSurface(&surf->Base);
501
502 /*
503 * Now init the Xlib and gallium stuff
504 */
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);
508
509 surf->winsys = xdrv->winsys;
510
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);
514
515 surf->Base.Width = width;
516 surf->Base.Height = height;
517
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),
523 width, height,
524 (void *) surf);
525
526 st_resize_framebuffer(surf->Framebuffer, width, height);
527
528 return _eglGetSurfaceHandle(&surf->Base);
529 }
530
531
532 static EGLBoolean
533 xlib_eglDestroySurface(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface)
534 {
535 struct xlib_egl_surface *surf = lookup_surface(surface);
536 if (surf) {
537 _eglHashRemove(_eglGlobal.Surfaces, (EGLuint) surface);
538 if (surf->Base.IsBound) {
539 surf->Base.DeletePending = EGL_TRUE;
540 }
541 else {
542 XFreeGC(surf->Dpy, surf->Gc);
543 st_unreference_framebuffer(surf->Framebuffer);
544 free(surf);
545 }
546 return EGL_TRUE;
547 }
548 else {
549 _eglError(EGL_BAD_SURFACE, "eglDestroySurface");
550 return EGL_FALSE;
551 }
552 }
553
554
555 static EGLBoolean
556 xlib_eglSwapBuffers(_EGLDriver *drv, EGLDisplay dpy, EGLSurface draw)
557 {
558 /* error checking step: */
559 if (!_eglSwapBuffers(drv, dpy, draw))
560 return EGL_FALSE;
561
562 {
563 struct xlib_egl_surface *xsurf = lookup_surface(draw);
564 struct pipe_winsys *pws = xsurf->winsys;
565 struct pipe_surface *psurf;
566
567 st_get_framebuffer_surface(xsurf->Framebuffer, ST_SURFACE_BACK_LEFT,
568 &psurf);
569
570 st_notify_swapbuffers(xsurf->Framebuffer);
571
572 display_surface(pws, psurf, xsurf);
573
574 check_and_update_buffer_size(xsurf);
575 }
576
577 return EGL_TRUE;
578 }
579
580
581 /**
582 * Determine which API(s) is(are) present by looking for some specific
583 * global symbols.
584 */
585 static EGLint
586 find_supported_apis(void)
587 {
588 EGLint mask = 0;
589 void *handle;
590
591 handle = dlopen(NULL, 0);
592
593 if (dlsym(handle, "st_api_OpenGL_ES1"))
594 mask |= EGL_OPENGL_ES_BIT;
595
596 if (dlsym(handle, "st_api_OpenGL_ES2"))
597 mask |= EGL_OPENGL_ES2_BIT;
598
599 if (dlsym(handle, "st_api_OpenGL"))
600 mask |= EGL_OPENGL_BIT;
601
602 if (dlsym(handle, "st_api_OpenVG"))
603 mask |= EGL_OPENVG_BIT;
604
605 dlclose(handle);
606
607 return mask;
608 }
609
610
611 /**
612 * This is the main entrypoint into the driver.
613 * Called by libEGL to instantiate an _EGLDriver object.
614 */
615 _EGLDriver *
616 _eglMain(_EGLDisplay *dpy, const char *args)
617 {
618 struct xlib_egl_driver *xdrv;
619
620 _eglLog(_EGL_INFO, "Entering EGL/Xlib _eglMain(%s)", args);
621
622 xdrv = CALLOC_STRUCT(xlib_egl_driver);
623 if (!xdrv)
624 return NULL;
625
626 if (!dpy->Xdpy) {
627 dpy->Xdpy = XOpenDisplay(NULL);
628 }
629
630 _eglInitDriverFallbacks(&xdrv->Base);
631 xdrv->Base.API.Initialize = xlib_eglInitialize;
632 xdrv->Base.API.Terminate = xlib_eglTerminate;
633 xdrv->Base.API.GetProcAddress = xlib_eglGetProcAddress;
634 xdrv->Base.API.CreateContext = xlib_eglCreateContext;
635 xdrv->Base.API.DestroyContext = xlib_eglDestroyContext;
636 xdrv->Base.API.CreateWindowSurface = xlib_eglCreateWindowSurface;
637 xdrv->Base.API.DestroySurface = xlib_eglDestroySurface;
638 xdrv->Base.API.MakeCurrent = xlib_eglMakeCurrent;
639 xdrv->Base.API.SwapBuffers = xlib_eglSwapBuffers;
640
641 xdrv->Base.ClientAPIsMask = find_supported_apis();
642 if (xdrv->Base.ClientAPIsMask == 0x0) {
643 /* the app isn't directly linked with any EGL-supprted APIs
644 * (such as libGLESv2.so) so use an EGL utility to see what
645 * APIs might be loaded dynamically on this system.
646 */
647 xdrv->Base.ClientAPIsMask = _eglFindAPIs();
648 }
649
650 xdrv->Base.Name = "Xlib/softpipe";
651
652 /* create one winsys and use it for all contexts/surfaces */
653 xdrv->winsys = create_sw_winsys();
654 xdrv->winsys->flush_frontbuffer = flush_frontbuffer;
655
656 xdrv->screen = softpipe_create_screen(xdrv->winsys);
657
658 return &xdrv->Base;
659 }
660