Fix copyright headers.
[mesa.git] / src / gallium / state_trackers / egl / x11 / native_ximage.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.8
4 *
5 * Copyright (C) 2009-2010 Chia-I Wu <olv@0xlab.org>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 */
25
26 #include <assert.h>
27 #include <sys/ipc.h>
28 #include <sys/types.h>
29 #include <sys/shm.h>
30 #include <X11/Xlib.h>
31 #include <X11/Xutil.h>
32 #include "util/u_memory.h"
33 #include "util/u_math.h"
34 #include "util/u_format.h"
35 #include "pipe/p_compiler.h"
36 #include "util/u_inlines.h"
37 #include "state_tracker/xlib_sw_winsys.h"
38 #include "target-helpers/wrap_screen.h"
39 #include "util/u_debug.h"
40 #include "softpipe/sp_public.h"
41 #include "llvmpipe/lp_public.h"
42 #include "cell/ppu/cell_public.h"
43 #include "egllog.h"
44
45 #include "native_x11.h"
46 #include "x11_screen.h"
47
48 enum ximage_surface_type {
49 XIMAGE_SURFACE_TYPE_WINDOW,
50 XIMAGE_SURFACE_TYPE_PIXMAP,
51 };
52
53 struct ximage_display {
54 struct native_display base;
55 Display *dpy;
56 boolean own_dpy;
57
58 struct native_event_handler *event_handler;
59
60 struct x11_screen *xscr;
61 int xscr_number;
62
63 struct ximage_config *configs;
64 int num_configs;
65 };
66
67 struct ximage_buffer {
68 struct pipe_texture *texture;
69 struct xlib_drawable xdraw;
70 };
71
72 struct ximage_surface {
73 struct native_surface base;
74 Drawable drawable;
75 enum ximage_surface_type type;
76 enum pipe_format color_format;
77 XVisualInfo visual;
78 struct ximage_display *xdpy;
79
80 unsigned int server_stamp;
81 unsigned int client_stamp;
82 int width, height;
83 struct ximage_buffer buffers[NUM_NATIVE_ATTACHMENTS];
84 uint valid_mask;
85
86 struct pipe_surface *draw_surface;
87 };
88
89 struct ximage_config {
90 struct native_config base;
91 const XVisualInfo *visual;
92 };
93
94 static INLINE struct ximage_display *
95 ximage_display(const struct native_display *ndpy)
96 {
97 return (struct ximage_display *) ndpy;
98 }
99
100 static INLINE struct ximage_surface *
101 ximage_surface(const struct native_surface *nsurf)
102 {
103 return (struct ximage_surface *) nsurf;
104 }
105
106 static INLINE struct ximage_config *
107 ximage_config(const struct native_config *nconf)
108 {
109 return (struct ximage_config *) nconf;
110 }
111
112 static void
113 ximage_surface_free_buffer(struct native_surface *nsurf,
114 enum native_attachment which)
115 {
116 struct ximage_surface *xsurf = ximage_surface(nsurf);
117 struct ximage_buffer *xbuf = &xsurf->buffers[which];
118
119 pipe_texture_reference(&xbuf->texture, NULL);
120 }
121
122 static boolean
123 ximage_surface_alloc_buffer(struct native_surface *nsurf,
124 enum native_attachment which)
125 {
126 struct ximage_surface *xsurf = ximage_surface(nsurf);
127 struct ximage_buffer *xbuf = &xsurf->buffers[which];
128 struct pipe_screen *screen = xsurf->xdpy->base.screen;
129 struct pipe_texture templ;
130
131 /* free old data */
132 if (xbuf->texture)
133 ximage_surface_free_buffer(&xsurf->base, which);
134
135 memset(&templ, 0, sizeof(templ));
136 templ.target = PIPE_TEXTURE_2D;
137 templ.format = xsurf->color_format;
138 templ.width0 = xsurf->width;
139 templ.height0 = xsurf->height;
140 templ.depth0 = 1;
141 templ.tex_usage = PIPE_TEXTURE_USAGE_RENDER_TARGET;
142
143 switch (which) {
144 case NATIVE_ATTACHMENT_FRONT_LEFT:
145 case NATIVE_ATTACHMENT_FRONT_RIGHT:
146 templ.tex_usage |= PIPE_TEXTURE_USAGE_SCANOUT;
147 break;
148 case NATIVE_ATTACHMENT_BACK_LEFT:
149 case NATIVE_ATTACHMENT_BACK_RIGHT:
150 templ.tex_usage |= PIPE_TEXTURE_USAGE_DISPLAY_TARGET;
151 break;
152 default:
153 break;
154 }
155
156 xbuf->texture = screen->texture_create(screen, &templ);
157 if (xbuf->texture) {
158 xbuf->xdraw.visual = xsurf->visual.visual;
159 xbuf->xdraw.depth = xsurf->visual.depth;
160 xbuf->xdraw.drawable = xsurf->drawable;
161 }
162
163 /* clean up the buffer if allocation failed */
164 if (!xbuf->texture)
165 ximage_surface_free_buffer(&xsurf->base, which);
166
167 return (xbuf->texture != NULL);
168 }
169
170 /**
171 * Update the geometry of the surface. Return TRUE if the geometry has changed
172 * since last call.
173 */
174 static boolean
175 ximage_surface_update_geometry(struct native_surface *nsurf)
176 {
177 struct ximage_surface *xsurf = ximage_surface(nsurf);
178 Status ok;
179 Window root;
180 int x, y;
181 unsigned int w, h, border, depth;
182 boolean updated = FALSE;
183
184 ok = XGetGeometry(xsurf->xdpy->dpy, xsurf->drawable,
185 &root, &x, &y, &w, &h, &border, &depth);
186 if (ok && (xsurf->width != w || xsurf->height != h)) {
187 xsurf->width = w;
188 xsurf->height = h;
189
190 xsurf->server_stamp++;
191 updated = TRUE;
192 }
193
194 return updated;
195 }
196
197 static void
198 ximage_surface_notify_invalid(struct native_surface *nsurf)
199 {
200 struct ximage_surface *xsurf = ximage_surface(nsurf);
201 struct ximage_display *xdpy = xsurf->xdpy;
202
203 xdpy->event_handler->invalid_surface(&xdpy->base,
204 &xsurf->base, xsurf->server_stamp);
205 }
206
207 /**
208 * Update the buffers of the surface. It is a slow function due to the
209 * round-trip to the server.
210 */
211 static boolean
212 ximage_surface_update_buffers(struct native_surface *nsurf, uint buffer_mask)
213 {
214 struct ximage_surface *xsurf = ximage_surface(nsurf);
215 boolean updated;
216 uint new_valid;
217 int att;
218
219 updated = ximage_surface_update_geometry(&xsurf->base);
220 if (updated) {
221 /* all buffers become invalid */
222 xsurf->valid_mask = 0x0;
223 }
224 else {
225 buffer_mask &= ~xsurf->valid_mask;
226 /* all requested buffers are valid */
227 if (!buffer_mask) {
228 xsurf->client_stamp = xsurf->server_stamp;
229 return TRUE;
230 }
231 }
232
233 new_valid = 0x0;
234 for (att = 0; att < NUM_NATIVE_ATTACHMENTS; att++) {
235 if (native_attachment_mask_test(buffer_mask, att)) {
236 /* reallocate the texture */
237 if (!ximage_surface_alloc_buffer(&xsurf->base, att))
238 break;
239
240 new_valid |= (1 << att);
241 if (buffer_mask == new_valid)
242 break;
243 }
244 }
245
246 xsurf->valid_mask |= new_valid;
247 xsurf->client_stamp = xsurf->server_stamp;
248
249 return (new_valid == buffer_mask);
250 }
251
252 static boolean
253 ximage_surface_draw_buffer(struct native_surface *nsurf,
254 enum native_attachment which)
255 {
256 struct ximage_surface *xsurf = ximage_surface(nsurf);
257 struct ximage_buffer *xbuf = &xsurf->buffers[which];
258 struct pipe_screen *screen = xsurf->xdpy->base.screen;
259 struct pipe_surface *psurf;
260
261 assert(xsurf->drawable && xbuf->texture);
262
263 psurf = xsurf->draw_surface;
264 if (!psurf || psurf->texture != xbuf->texture) {
265 pipe_surface_reference(&xsurf->draw_surface, NULL);
266
267 psurf = screen->get_tex_surface(screen,
268 xbuf->texture, 0, 0, 0, PIPE_BUFFER_USAGE_CPU_READ);
269 if (!psurf)
270 return FALSE;
271
272 xsurf->draw_surface = psurf;
273 }
274
275 screen->flush_frontbuffer(screen, psurf, &xbuf->xdraw);
276
277 return TRUE;
278 }
279
280 static boolean
281 ximage_surface_flush_frontbuffer(struct native_surface *nsurf)
282 {
283 struct ximage_surface *xsurf = ximage_surface(nsurf);
284 boolean ret;
285
286 ret = ximage_surface_draw_buffer(&xsurf->base,
287 NATIVE_ATTACHMENT_FRONT_LEFT);
288 /* force buffers to be updated in next validation call */
289 xsurf->server_stamp++;
290 ximage_surface_notify_invalid(&xsurf->base);
291
292 return ret;
293 }
294
295 static boolean
296 ximage_surface_swap_buffers(struct native_surface *nsurf)
297 {
298 struct ximage_surface *xsurf = ximage_surface(nsurf);
299 struct ximage_buffer *xfront, *xback, xtmp;
300 boolean ret;
301
302 /* display the back buffer first */
303 ret = ximage_surface_draw_buffer(&xsurf->base,
304 NATIVE_ATTACHMENT_BACK_LEFT);
305 /* force buffers to be updated in next validation call */
306 xsurf->server_stamp++;
307 ximage_surface_notify_invalid(&xsurf->base);
308
309 xfront = &xsurf->buffers[NATIVE_ATTACHMENT_FRONT_LEFT];
310 xback = &xsurf->buffers[NATIVE_ATTACHMENT_BACK_LEFT];
311
312 /* skip swapping unless there is a front buffer */
313 if (xfront->texture) {
314 xtmp = *xfront;
315 *xfront = *xback;
316 *xback = xtmp;
317 }
318
319 return ret;
320 }
321
322 static boolean
323 ximage_surface_validate(struct native_surface *nsurf, uint attachment_mask,
324 unsigned int *seq_num, struct pipe_texture **textures,
325 int *width, int *height)
326 {
327 struct ximage_surface *xsurf = ximage_surface(nsurf);
328
329 if (xsurf->client_stamp != xsurf->server_stamp ||
330 (xsurf->valid_mask & attachment_mask) != attachment_mask) {
331 if (!ximage_surface_update_buffers(&xsurf->base, attachment_mask))
332 return FALSE;
333 }
334
335 if (seq_num)
336 *seq_num = xsurf->client_stamp;
337
338 if (textures) {
339 int att;
340 for (att = 0; att < NUM_NATIVE_ATTACHMENTS; att++) {
341 if (native_attachment_mask_test(attachment_mask, att)) {
342 struct ximage_buffer *xbuf = &xsurf->buffers[att];
343
344 textures[att] = NULL;
345 pipe_texture_reference(&textures[att], xbuf->texture);
346 }
347 }
348 }
349
350 if (width)
351 *width = xsurf->width;
352 if (height)
353 *height = xsurf->height;
354
355 return TRUE;
356 }
357
358 static void
359 ximage_surface_wait(struct native_surface *nsurf)
360 {
361 struct ximage_surface *xsurf = ximage_surface(nsurf);
362 XSync(xsurf->xdpy->dpy, FALSE);
363 /* TODO XGetImage and update the front texture */
364 }
365
366 static void
367 ximage_surface_destroy(struct native_surface *nsurf)
368 {
369 struct ximage_surface *xsurf = ximage_surface(nsurf);
370 int i;
371
372 pipe_surface_reference(&xsurf->draw_surface, NULL);
373
374 for (i = 0; i < NUM_NATIVE_ATTACHMENTS; i++)
375 ximage_surface_free_buffer(&xsurf->base, i);
376
377 free(xsurf);
378 }
379
380 static struct ximage_surface *
381 ximage_display_create_surface(struct native_display *ndpy,
382 enum ximage_surface_type type,
383 Drawable drawable,
384 const struct native_config *nconf)
385 {
386 struct ximage_display *xdpy = ximage_display(ndpy);
387 struct ximage_config *xconf = ximage_config(nconf);
388 struct ximage_surface *xsurf;
389
390 xsurf = CALLOC_STRUCT(ximage_surface);
391 if (!xsurf)
392 return NULL;
393
394 xsurf->xdpy = xdpy;
395 xsurf->type = type;
396 xsurf->color_format = xconf->base.color_format;
397 xsurf->drawable = drawable;
398
399 xsurf->drawable = drawable;
400 xsurf->visual = *xconf->visual;
401 /* initialize the geometry */
402 ximage_surface_update_buffers(&xsurf->base, 0x0);
403
404 xsurf->base.destroy = ximage_surface_destroy;
405 xsurf->base.swap_buffers = ximage_surface_swap_buffers;
406 xsurf->base.flush_frontbuffer = ximage_surface_flush_frontbuffer;
407 xsurf->base.validate = ximage_surface_validate;
408 xsurf->base.wait = ximage_surface_wait;
409
410 return xsurf;
411 }
412
413 static struct native_surface *
414 ximage_display_create_window_surface(struct native_display *ndpy,
415 EGLNativeWindowType win,
416 const struct native_config *nconf)
417 {
418 struct ximage_surface *xsurf;
419
420 xsurf = ximage_display_create_surface(ndpy, XIMAGE_SURFACE_TYPE_WINDOW,
421 (Drawable) win, nconf);
422 return (xsurf) ? &xsurf->base : NULL;
423 }
424
425 static struct native_surface *
426 ximage_display_create_pixmap_surface(struct native_display *ndpy,
427 EGLNativePixmapType pix,
428 const struct native_config *nconf)
429 {
430 struct ximage_surface *xsurf;
431
432 xsurf = ximage_display_create_surface(ndpy, XIMAGE_SURFACE_TYPE_PIXMAP,
433 (Drawable) pix, nconf);
434 return (xsurf) ? &xsurf->base : NULL;
435 }
436
437 static enum pipe_format
438 choose_format(const XVisualInfo *vinfo)
439 {
440 enum pipe_format fmt;
441 /* TODO elaborate the formats */
442 switch (vinfo->depth) {
443 case 32:
444 fmt = PIPE_FORMAT_B8G8R8A8_UNORM;
445 break;
446 case 24:
447 fmt = PIPE_FORMAT_B8G8R8X8_UNORM;
448 break;
449 case 16:
450 fmt = PIPE_FORMAT_B5G6R5_UNORM;
451 break;
452 default:
453 fmt = PIPE_FORMAT_NONE;
454 break;
455 }
456
457 return fmt;
458 }
459
460 static const struct native_config **
461 ximage_display_get_configs(struct native_display *ndpy, int *num_configs)
462 {
463 struct ximage_display *xdpy = ximage_display(ndpy);
464 const struct native_config **configs;
465 int i;
466
467 /* first time */
468 if (!xdpy->configs) {
469 const XVisualInfo *visuals;
470 int num_visuals, count, j;
471
472 visuals = x11_screen_get_visuals(xdpy->xscr, &num_visuals);
473 if (!visuals)
474 return NULL;
475
476 /*
477 * Create two configs for each visual.
478 * One with depth/stencil buffer; one without
479 */
480 xdpy->configs = calloc(num_visuals * 2, sizeof(*xdpy->configs));
481 if (!xdpy->configs)
482 return NULL;
483
484 count = 0;
485 for (i = 0; i < num_visuals; i++) {
486 for (j = 0; j < 2; j++) {
487 struct ximage_config *xconf = &xdpy->configs[count];
488 __GLcontextModes *mode = &xconf->base.mode;
489
490 xconf->visual = &visuals[i];
491 xconf->base.color_format = choose_format(xconf->visual);
492 if (xconf->base.color_format == PIPE_FORMAT_NONE)
493 continue;
494
495 x11_screen_convert_visual(xdpy->xscr, xconf->visual, mode);
496 /* support double buffer mode */
497 mode->doubleBufferMode = TRUE;
498
499 xconf->base.depth_format = PIPE_FORMAT_NONE;
500 xconf->base.stencil_format = PIPE_FORMAT_NONE;
501 /* create the second config with depth/stencil buffer */
502 if (j == 1) {
503 xconf->base.depth_format = PIPE_FORMAT_Z24_UNORM_S8_USCALED;
504 xconf->base.stencil_format = PIPE_FORMAT_Z24_UNORM_S8_USCALED;
505 mode->depthBits = 24;
506 mode->stencilBits = 8;
507 mode->haveDepthBuffer = TRUE;
508 mode->haveStencilBuffer = TRUE;
509 }
510
511 mode->maxPbufferWidth = 4096;
512 mode->maxPbufferHeight = 4096;
513 mode->maxPbufferPixels = 4096 * 4096;
514 mode->drawableType =
515 GLX_WINDOW_BIT | GLX_PIXMAP_BIT | GLX_PBUFFER_BIT;
516 mode->swapMethod = GLX_SWAP_EXCHANGE_OML;
517
518 if (mode->alphaBits)
519 mode->bindToTextureRgba = TRUE;
520 else
521 mode->bindToTextureRgb = TRUE;
522
523 count++;
524 }
525 }
526
527 xdpy->num_configs = count;
528 }
529
530 configs = malloc(xdpy->num_configs * sizeof(*configs));
531 if (configs) {
532 for (i = 0; i < xdpy->num_configs; i++)
533 configs[i] = (const struct native_config *) &xdpy->configs[i];
534 if (num_configs)
535 *num_configs = xdpy->num_configs;
536 }
537 return configs;
538 }
539
540 static boolean
541 ximage_display_is_pixmap_supported(struct native_display *ndpy,
542 EGLNativePixmapType pix,
543 const struct native_config *nconf)
544 {
545 struct ximage_display *xdpy = ximage_display(ndpy);
546 enum pipe_format fmt;
547 uint depth;
548
549 depth = x11_drawable_get_depth(xdpy->xscr, (Drawable) pix);
550 switch (depth) {
551 case 32:
552 fmt = PIPE_FORMAT_B8G8R8A8_UNORM;
553 break;
554 case 24:
555 fmt = PIPE_FORMAT_B8G8R8X8_UNORM;
556 break;
557 case 16:
558 fmt = PIPE_FORMAT_B5G6R5_UNORM;
559 break;
560 default:
561 fmt = PIPE_FORMAT_NONE;
562 break;
563 }
564
565 return (fmt == nconf->color_format);
566 }
567
568 static int
569 ximage_display_get_param(struct native_display *ndpy,
570 enum native_param_type param)
571 {
572 int val;
573
574 switch (param) {
575 case NATIVE_PARAM_USE_NATIVE_BUFFER:
576 /* private buffers are allocated */
577 val = FALSE;
578 break;
579 default:
580 val = 0;
581 break;
582 }
583
584 return val;
585 }
586
587 static void
588 ximage_display_destroy(struct native_display *ndpy)
589 {
590 struct ximage_display *xdpy = ximage_display(ndpy);
591
592 if (xdpy->configs)
593 free(xdpy->configs);
594
595 xdpy->base.screen->destroy(xdpy->base.screen);
596
597 x11_screen_destroy(xdpy->xscr);
598 if (xdpy->own_dpy)
599 XCloseDisplay(xdpy->dpy);
600 free(xdpy);
601 }
602
603
604 /* Helper function to build a subset of a driver stack consisting of
605 * one of the software rasterizers (cell, llvmpipe, softpipe) and the
606 * xlib winsys.
607 *
608 * This function could be shared, but currently causes headaches for
609 * the build systems, particularly scons if we try.
610 *
611 * Long term, want to avoid having global #defines for things like
612 * GALLIUM_LLVMPIPE, GALLIUM_CELL, etc. Scons already eliminates
613 * those #defines, so things that are painful for it now are likely to
614 * be painful for other build systems in the future.
615 */
616 static struct pipe_screen *
617 swrast_xlib_create_screen( Display *display )
618 {
619 struct sw_winsys *winsys;
620 struct pipe_screen *screen = NULL;
621
622 /* Create the underlying winsys, which performs presents to Xlib
623 * drawables:
624 */
625 winsys = xlib_create_sw_winsys( display );
626 if (winsys == NULL)
627 return NULL;
628
629 /* Create a software rasterizer on top of that winsys. Use
630 * llvmpipe if it is available.
631 */
632 #if defined(GALLIUM_LLVMPIPE)
633 if (screen == NULL &&
634 !debug_get_bool_option("GALLIUM_NO_LLVM", FALSE))
635 screen = llvmpipe_create_screen( winsys );
636 #endif
637
638 if (screen == NULL)
639 screen = softpipe_create_screen( winsys );
640
641 if (screen == NULL)
642 goto fail;
643
644 /* Inject any wrapping layers we want to here:
645 */
646 return gallium_wrap_screen( screen );
647
648 fail:
649 if (winsys)
650 winsys->destroy( winsys );
651
652 return NULL;
653 }
654
655
656
657 struct native_display *
658 x11_create_ximage_display(EGLNativeDisplayType dpy,
659 struct native_event_handler *event_handler)
660 {
661 struct ximage_display *xdpy;
662
663 xdpy = CALLOC_STRUCT(ximage_display);
664 if (!xdpy)
665 return NULL;
666
667 xdpy->dpy = dpy;
668 if (!xdpy->dpy) {
669 xdpy->dpy = XOpenDisplay(NULL);
670 if (!xdpy->dpy) {
671 free(xdpy);
672 return NULL;
673 }
674 xdpy->own_dpy = TRUE;
675 }
676
677 xdpy->event_handler = event_handler;
678
679 xdpy->xscr_number = DefaultScreen(xdpy->dpy);
680 xdpy->xscr = x11_screen_create(xdpy->dpy, xdpy->xscr_number);
681 if (!xdpy->xscr) {
682 free(xdpy);
683 return NULL;
684 }
685
686 xdpy->base.screen = swrast_xlib_create_screen(xdpy->dpy);
687
688 xdpy->base.destroy = ximage_display_destroy;
689 xdpy->base.get_param = ximage_display_get_param;
690
691 xdpy->base.get_configs = ximage_display_get_configs;
692 xdpy->base.is_pixmap_supported = ximage_display_is_pixmap_supported;
693 xdpy->base.create_window_surface = ximage_display_create_window_surface;
694 xdpy->base.create_pixmap_surface = ximage_display_create_pixmap_surface;
695
696 return &xdpy->base;
697 }