203e06e17777525ab2bca58a9925f39444dcb02c
[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_resource *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_resource_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_resource 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.bind = PIPE_BIND_RENDER_TARGET;
142
143 switch (which) {
144 case NATIVE_ATTACHMENT_FRONT_LEFT:
145 case NATIVE_ATTACHMENT_FRONT_RIGHT:
146 templ.bind |= PIPE_BIND_SCANOUT;
147 break;
148 case NATIVE_ATTACHMENT_BACK_LEFT:
149 case NATIVE_ATTACHMENT_BACK_RIGHT:
150 templ.bind |= PIPE_BIND_DISPLAY_TARGET;
151 break;
152 default:
153 break;
154 }
155 xbuf->texture = screen->resource_create(screen, &templ);
156 if (xbuf->texture) {
157 xbuf->xdraw.visual = xsurf->visual.visual;
158 xbuf->xdraw.depth = xsurf->visual.depth;
159 xbuf->xdraw.drawable = xsurf->drawable;
160 }
161
162 /* clean up the buffer if allocation failed */
163 if (!xbuf->texture)
164 ximage_surface_free_buffer(&xsurf->base, which);
165
166 return (xbuf->texture != NULL);
167 }
168
169 /**
170 * Update the geometry of the surface. Return TRUE if the geometry has changed
171 * since last call.
172 */
173 static boolean
174 ximage_surface_update_geometry(struct native_surface *nsurf)
175 {
176 struct ximage_surface *xsurf = ximage_surface(nsurf);
177 Status ok;
178 Window root;
179 int x, y;
180 unsigned int w, h, border, depth;
181 boolean updated = FALSE;
182
183 ok = XGetGeometry(xsurf->xdpy->dpy, xsurf->drawable,
184 &root, &x, &y, &w, &h, &border, &depth);
185 if (ok && (xsurf->width != w || xsurf->height != h)) {
186 xsurf->width = w;
187 xsurf->height = h;
188
189 xsurf->server_stamp++;
190 updated = TRUE;
191 }
192
193 return updated;
194 }
195
196 static void
197 ximage_surface_notify_invalid(struct native_surface *nsurf)
198 {
199 struct ximage_surface *xsurf = ximage_surface(nsurf);
200 struct ximage_display *xdpy = xsurf->xdpy;
201
202 xdpy->event_handler->invalid_surface(&xdpy->base,
203 &xsurf->base, xsurf->server_stamp);
204 }
205
206 /**
207 * Update the buffers of the surface. It is a slow function due to the
208 * round-trip to the server.
209 */
210 static boolean
211 ximage_surface_update_buffers(struct native_surface *nsurf, uint buffer_mask)
212 {
213 struct ximage_surface *xsurf = ximage_surface(nsurf);
214 boolean updated;
215 uint new_valid;
216 int att;
217
218 updated = ximage_surface_update_geometry(&xsurf->base);
219 if (updated) {
220 /* all buffers become invalid */
221 xsurf->valid_mask = 0x0;
222 }
223 else {
224 buffer_mask &= ~xsurf->valid_mask;
225 /* all requested buffers are valid */
226 if (!buffer_mask) {
227 xsurf->client_stamp = xsurf->server_stamp;
228 return TRUE;
229 }
230 }
231
232 new_valid = 0x0;
233 for (att = 0; att < NUM_NATIVE_ATTACHMENTS; att++) {
234 if (native_attachment_mask_test(buffer_mask, att)) {
235 /* reallocate the texture */
236 if (!ximage_surface_alloc_buffer(&xsurf->base, att))
237 break;
238
239 new_valid |= (1 << att);
240 if (buffer_mask == new_valid)
241 break;
242 }
243 }
244
245 xsurf->valid_mask |= new_valid;
246 xsurf->client_stamp = xsurf->server_stamp;
247
248 return (new_valid == buffer_mask);
249 }
250
251 static boolean
252 ximage_surface_draw_buffer(struct native_surface *nsurf,
253 enum native_attachment which)
254 {
255 struct ximage_surface *xsurf = ximage_surface(nsurf);
256 struct ximage_buffer *xbuf = &xsurf->buffers[which];
257 struct pipe_screen *screen = xsurf->xdpy->base.screen;
258 struct pipe_surface *psurf;
259
260 assert(xsurf->drawable && xbuf->texture);
261
262 psurf = xsurf->draw_surface;
263 if (!psurf || psurf->texture != xbuf->texture) {
264 pipe_surface_reference(&xsurf->draw_surface, NULL);
265
266 psurf = screen->get_tex_surface(screen,
267 xbuf->texture, 0, 0, 0, PIPE_BIND_DISPLAY_TARGET);
268 if (!psurf)
269 return FALSE;
270
271 xsurf->draw_surface = psurf;
272 }
273
274 screen->flush_frontbuffer(screen, psurf, &xbuf->xdraw);
275
276 return TRUE;
277 }
278
279 static boolean
280 ximage_surface_flush_frontbuffer(struct native_surface *nsurf)
281 {
282 struct ximage_surface *xsurf = ximage_surface(nsurf);
283 boolean ret;
284
285 ret = ximage_surface_draw_buffer(&xsurf->base,
286 NATIVE_ATTACHMENT_FRONT_LEFT);
287 /* force buffers to be updated in next validation call */
288 xsurf->server_stamp++;
289 ximage_surface_notify_invalid(&xsurf->base);
290
291 return ret;
292 }
293
294 static boolean
295 ximage_surface_swap_buffers(struct native_surface *nsurf)
296 {
297 struct ximage_surface *xsurf = ximage_surface(nsurf);
298 struct ximage_buffer *xfront, *xback, xtmp;
299 boolean ret;
300
301 /* display the back buffer first */
302 ret = ximage_surface_draw_buffer(&xsurf->base,
303 NATIVE_ATTACHMENT_BACK_LEFT);
304 /* force buffers to be updated in next validation call */
305 xsurf->server_stamp++;
306 ximage_surface_notify_invalid(&xsurf->base);
307
308 xfront = &xsurf->buffers[NATIVE_ATTACHMENT_FRONT_LEFT];
309 xback = &xsurf->buffers[NATIVE_ATTACHMENT_BACK_LEFT];
310
311 /* skip swapping unless there is a front buffer */
312 if (xfront->texture) {
313 xtmp = *xfront;
314 *xfront = *xback;
315 *xback = xtmp;
316 }
317
318 return ret;
319 }
320
321 static boolean
322 ximage_surface_validate(struct native_surface *nsurf, uint attachment_mask,
323 unsigned int *seq_num, struct pipe_resource **textures,
324 int *width, int *height)
325 {
326 struct ximage_surface *xsurf = ximage_surface(nsurf);
327
328 if (xsurf->client_stamp != xsurf->server_stamp ||
329 (xsurf->valid_mask & attachment_mask) != attachment_mask) {
330 if (!ximage_surface_update_buffers(&xsurf->base, attachment_mask))
331 return FALSE;
332 }
333
334 if (seq_num)
335 *seq_num = xsurf->client_stamp;
336
337 if (textures) {
338 int att;
339 for (att = 0; att < NUM_NATIVE_ATTACHMENTS; att++) {
340 if (native_attachment_mask_test(attachment_mask, att)) {
341 struct ximage_buffer *xbuf = &xsurf->buffers[att];
342
343 textures[att] = NULL;
344 pipe_resource_reference(&textures[att], xbuf->texture);
345 }
346 }
347 }
348
349 if (width)
350 *width = xsurf->width;
351 if (height)
352 *height = xsurf->height;
353
354 return TRUE;
355 }
356
357 static void
358 ximage_surface_wait(struct native_surface *nsurf)
359 {
360 struct ximage_surface *xsurf = ximage_surface(nsurf);
361 XSync(xsurf->xdpy->dpy, FALSE);
362 /* TODO XGetImage and update the front texture */
363 }
364
365 static void
366 ximage_surface_destroy(struct native_surface *nsurf)
367 {
368 struct ximage_surface *xsurf = ximage_surface(nsurf);
369 int i;
370
371 pipe_surface_reference(&xsurf->draw_surface, NULL);
372
373 for (i = 0; i < NUM_NATIVE_ATTACHMENTS; i++)
374 ximage_surface_free_buffer(&xsurf->base, i);
375
376 free(xsurf);
377 }
378
379 static struct ximage_surface *
380 ximage_display_create_surface(struct native_display *ndpy,
381 enum ximage_surface_type type,
382 Drawable drawable,
383 const struct native_config *nconf)
384 {
385 struct ximage_display *xdpy = ximage_display(ndpy);
386 struct ximage_config *xconf = ximage_config(nconf);
387 struct ximage_surface *xsurf;
388
389 xsurf = CALLOC_STRUCT(ximage_surface);
390 if (!xsurf)
391 return NULL;
392
393 xsurf->xdpy = xdpy;
394 xsurf->type = type;
395 xsurf->color_format = xconf->base.color_format;
396 xsurf->drawable = drawable;
397
398 xsurf->drawable = drawable;
399 xsurf->visual = *xconf->visual;
400 /* initialize the geometry */
401 ximage_surface_update_buffers(&xsurf->base, 0x0);
402
403 xsurf->base.destroy = ximage_surface_destroy;
404 xsurf->base.swap_buffers = ximage_surface_swap_buffers;
405 xsurf->base.flush_frontbuffer = ximage_surface_flush_frontbuffer;
406 xsurf->base.validate = ximage_surface_validate;
407 xsurf->base.wait = ximage_surface_wait;
408
409 return xsurf;
410 }
411
412 static struct native_surface *
413 ximage_display_create_window_surface(struct native_display *ndpy,
414 EGLNativeWindowType win,
415 const struct native_config *nconf)
416 {
417 struct ximage_surface *xsurf;
418
419 xsurf = ximage_display_create_surface(ndpy, XIMAGE_SURFACE_TYPE_WINDOW,
420 (Drawable) win, nconf);
421 return (xsurf) ? &xsurf->base : NULL;
422 }
423
424 static struct native_surface *
425 ximage_display_create_pixmap_surface(struct native_display *ndpy,
426 EGLNativePixmapType pix,
427 const struct native_config *nconf)
428 {
429 struct ximage_surface *xsurf;
430
431 xsurf = ximage_display_create_surface(ndpy, XIMAGE_SURFACE_TYPE_PIXMAP,
432 (Drawable) pix, nconf);
433 return (xsurf) ? &xsurf->base : NULL;
434 }
435
436 static enum pipe_format
437 choose_format(const XVisualInfo *vinfo)
438 {
439 enum pipe_format fmt;
440 /* TODO elaborate the formats */
441 switch (vinfo->depth) {
442 case 32:
443 fmt = PIPE_FORMAT_B8G8R8A8_UNORM;
444 break;
445 case 24:
446 fmt = PIPE_FORMAT_B8G8R8X8_UNORM;
447 break;
448 case 16:
449 fmt = PIPE_FORMAT_B5G6R5_UNORM;
450 break;
451 default:
452 fmt = PIPE_FORMAT_NONE;
453 break;
454 }
455
456 return fmt;
457 }
458
459 static const struct native_config **
460 ximage_display_get_configs(struct native_display *ndpy, int *num_configs)
461 {
462 struct ximage_display *xdpy = ximage_display(ndpy);
463 const struct native_config **configs;
464 int i;
465
466 /* first time */
467 if (!xdpy->configs) {
468 const XVisualInfo *visuals;
469 int num_visuals, count;
470
471 visuals = x11_screen_get_visuals(xdpy->xscr, &num_visuals);
472 if (!visuals)
473 return NULL;
474
475 /*
476 * Create two configs for each visual.
477 * One with depth/stencil buffer; one without
478 */
479 xdpy->configs = calloc(num_visuals * 2, sizeof(*xdpy->configs));
480 if (!xdpy->configs)
481 return NULL;
482
483 count = 0;
484 for (i = 0; i < num_visuals; i++) {
485 struct ximage_config *xconf = &xdpy->configs[count];
486
487 xconf->visual = &visuals[i];
488 xconf->base.color_format = choose_format(xconf->visual);
489 if (xconf->base.color_format == PIPE_FORMAT_NONE)
490 continue;
491
492 xconf->base.buffer_mask =
493 (1 << NATIVE_ATTACHMENT_FRONT_LEFT) |
494 (1 << NATIVE_ATTACHMENT_BACK_LEFT);
495
496 xconf->base.window_bit = TRUE;
497 xconf->base.pixmap_bit = TRUE;
498
499 xconf->base.native_visual_id = xconf->visual->visualid;
500 #if defined(__cplusplus) || defined(c_plusplus)
501 xconf->base.native_visual_type = xconf->visual->c_class;
502 #else
503 xconf->base.native_visual_type = xconf->visual->class;
504 #endif
505
506 xconf->base.slow_config = TRUE;
507
508 count++;
509 }
510
511 xdpy->num_configs = count;
512 }
513
514 configs = malloc(xdpy->num_configs * sizeof(*configs));
515 if (configs) {
516 for (i = 0; i < xdpy->num_configs; i++)
517 configs[i] = (const struct native_config *) &xdpy->configs[i];
518 if (num_configs)
519 *num_configs = xdpy->num_configs;
520 }
521 return configs;
522 }
523
524 static boolean
525 ximage_display_is_pixmap_supported(struct native_display *ndpy,
526 EGLNativePixmapType pix,
527 const struct native_config *nconf)
528 {
529 struct ximage_display *xdpy = ximage_display(ndpy);
530 enum pipe_format fmt;
531 uint depth;
532
533 depth = x11_drawable_get_depth(xdpy->xscr, (Drawable) pix);
534 switch (depth) {
535 case 32:
536 fmt = PIPE_FORMAT_B8G8R8A8_UNORM;
537 break;
538 case 24:
539 fmt = PIPE_FORMAT_B8G8R8X8_UNORM;
540 break;
541 case 16:
542 fmt = PIPE_FORMAT_B5G6R5_UNORM;
543 break;
544 default:
545 fmt = PIPE_FORMAT_NONE;
546 break;
547 }
548
549 return (fmt == nconf->color_format);
550 }
551
552 static int
553 ximage_display_get_param(struct native_display *ndpy,
554 enum native_param_type param)
555 {
556 int val;
557
558 switch (param) {
559 case NATIVE_PARAM_USE_NATIVE_BUFFER:
560 /* private buffers are allocated */
561 val = FALSE;
562 break;
563 default:
564 val = 0;
565 break;
566 }
567
568 return val;
569 }
570
571 static void
572 ximage_display_destroy(struct native_display *ndpy)
573 {
574 struct ximage_display *xdpy = ximage_display(ndpy);
575
576 if (xdpy->configs)
577 free(xdpy->configs);
578
579 xdpy->base.screen->destroy(xdpy->base.screen);
580
581 x11_screen_destroy(xdpy->xscr);
582 if (xdpy->own_dpy)
583 XCloseDisplay(xdpy->dpy);
584 free(xdpy);
585 }
586
587
588 /* Helper function to build a subset of a driver stack consisting of
589 * one of the software rasterizers (cell, llvmpipe, softpipe) and the
590 * xlib winsys.
591 *
592 * This function could be shared, but currently causes headaches for
593 * the build systems, particularly scons if we try.
594 *
595 * Long term, want to avoid having global #defines for things like
596 * GALLIUM_LLVMPIPE, GALLIUM_CELL, etc. Scons already eliminates
597 * those #defines, so things that are painful for it now are likely to
598 * be painful for other build systems in the future.
599 */
600 static struct pipe_screen *
601 swrast_xlib_create_screen( Display *display )
602 {
603 struct sw_winsys *winsys;
604 struct pipe_screen *screen = NULL;
605
606 /* Create the underlying winsys, which performs presents to Xlib
607 * drawables:
608 */
609 winsys = xlib_create_sw_winsys( display );
610 if (winsys == NULL)
611 return NULL;
612
613 /* Create a software rasterizer on top of that winsys. Use
614 * llvmpipe if it is available.
615 */
616 #if defined(GALLIUM_LLVMPIPE)
617 if (screen == NULL &&
618 !debug_get_bool_option("GALLIUM_NO_LLVM", FALSE))
619 screen = llvmpipe_create_screen( winsys );
620 #endif
621
622 if (screen == NULL)
623 screen = softpipe_create_screen( winsys );
624
625 if (screen == NULL)
626 goto fail;
627
628 /* Inject any wrapping layers we want to here:
629 */
630 return gallium_wrap_screen( screen );
631
632 fail:
633 if (winsys)
634 winsys->destroy( winsys );
635
636 return NULL;
637 }
638
639
640
641 struct native_display *
642 x11_create_ximage_display(EGLNativeDisplayType dpy,
643 struct native_event_handler *event_handler)
644 {
645 struct ximage_display *xdpy;
646
647 xdpy = CALLOC_STRUCT(ximage_display);
648 if (!xdpy)
649 return NULL;
650
651 xdpy->dpy = dpy;
652 if (!xdpy->dpy) {
653 xdpy->dpy = XOpenDisplay(NULL);
654 if (!xdpy->dpy) {
655 free(xdpy);
656 return NULL;
657 }
658 xdpy->own_dpy = TRUE;
659 }
660
661 xdpy->event_handler = event_handler;
662
663 xdpy->xscr_number = DefaultScreen(xdpy->dpy);
664 xdpy->xscr = x11_screen_create(xdpy->dpy, xdpy->xscr_number);
665 if (!xdpy->xscr) {
666 free(xdpy);
667 return NULL;
668 }
669
670 xdpy->base.screen = swrast_xlib_create_screen(xdpy->dpy);
671
672 xdpy->base.destroy = ximage_display_destroy;
673 xdpy->base.get_param = ximage_display_get_param;
674
675 xdpy->base.get_configs = ximage_display_get_configs;
676 xdpy->base.is_pixmap_supported = ximage_display_is_pixmap_supported;
677 xdpy->base.create_window_surface = ximage_display_create_window_surface;
678 xdpy->base.create_pixmap_surface = ximage_display_create_pixmap_surface;
679
680 return &xdpy->base;
681 }