2 * Mesa 3-D graphics library
5 * Copyright (C) 2009-2010 Chia-I Wu <olv@0xlab.org>
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
27 #include <X11/Xutil.h>
28 #include "util/u_memory.h"
29 #include "util/u_math.h"
30 #include "util/u_format.h"
31 #include "pipe/p_compiler.h"
32 #include "util/u_inlines.h"
33 #include "state_tracker/xlib_sw_winsys.h"
34 #include "util/u_debug.h"
37 #include "common/native_helper.h"
38 #include "native_x11.h"
39 #include "x11_screen.h"
41 struct ximage_display {
42 struct native_display base;
46 const struct native_event_handler *event_handler;
48 struct x11_screen *xscr;
51 struct ximage_config *configs;
55 struct ximage_surface {
56 struct native_surface base;
58 enum pipe_format color_format;
60 struct ximage_display *xdpy;
62 unsigned int server_stamp;
63 unsigned int client_stamp;
65 struct resource_surface *rsurf;
66 struct xlib_drawable xdraw;
69 struct ximage_config {
70 struct native_config base;
71 const XVisualInfo *visual;
74 static INLINE struct ximage_display *
75 ximage_display(const struct native_display *ndpy)
77 return (struct ximage_display *) ndpy;
80 static INLINE struct ximage_surface *
81 ximage_surface(const struct native_surface *nsurf)
83 return (struct ximage_surface *) nsurf;
86 static INLINE struct ximage_config *
87 ximage_config(const struct native_config *nconf)
89 return (struct ximage_config *) nconf;
93 * Update the geometry of the surface. This is a slow functions.
96 ximage_surface_update_geometry(struct native_surface *nsurf)
98 struct ximage_surface *xsurf = ximage_surface(nsurf);
102 unsigned int w, h, border, depth;
104 ok = XGetGeometry(xsurf->xdpy->dpy, xsurf->drawable,
105 &root, &x, &y, &w, &h, &border, &depth);
106 if (ok && resource_surface_set_size(xsurf->rsurf, w, h))
107 xsurf->server_stamp++;
111 * Update the buffers of the surface.
114 ximage_surface_update_buffers(struct native_surface *nsurf, uint buffer_mask)
116 struct ximage_surface *xsurf = ximage_surface(nsurf);
118 if (xsurf->client_stamp != xsurf->server_stamp) {
119 ximage_surface_update_geometry(&xsurf->base);
120 xsurf->client_stamp = xsurf->server_stamp;
123 return resource_surface_add_resources(xsurf->rsurf, buffer_mask);
127 * Emulate an invalidate event.
130 ximage_surface_invalidate(struct native_surface *nsurf)
132 struct ximage_surface *xsurf = ximage_surface(nsurf);
133 struct ximage_display *xdpy = xsurf->xdpy;
135 xsurf->server_stamp++;
136 xdpy->event_handler->invalid_surface(&xdpy->base,
137 &xsurf->base, xsurf->server_stamp);
141 ximage_surface_flush_frontbuffer(struct native_surface *nsurf)
143 struct ximage_surface *xsurf = ximage_surface(nsurf);
146 ret = resource_surface_present(xsurf->rsurf,
147 NATIVE_ATTACHMENT_FRONT_LEFT, (void *) &xsurf->xdraw);
148 /* force buffers to be updated in next validation call */
149 ximage_surface_invalidate(&xsurf->base);
155 ximage_surface_swap_buffers(struct native_surface *nsurf)
157 struct ximage_surface *xsurf = ximage_surface(nsurf);
160 ret = resource_surface_present(xsurf->rsurf,
161 NATIVE_ATTACHMENT_BACK_LEFT, (void *) &xsurf->xdraw);
163 resource_surface_swap_buffers(xsurf->rsurf,
164 NATIVE_ATTACHMENT_FRONT_LEFT, NATIVE_ATTACHMENT_BACK_LEFT, TRUE);
165 /* the front/back buffers have been swapped */
166 ximage_surface_invalidate(&xsurf->base);
172 ximage_surface_present(struct native_surface *nsurf,
173 enum native_attachment natt,
179 if (preserve || swap_interval)
183 case NATIVE_ATTACHMENT_FRONT_LEFT:
184 ret = ximage_surface_flush_frontbuffer(nsurf);
186 case NATIVE_ATTACHMENT_BACK_LEFT:
187 ret = ximage_surface_swap_buffers(nsurf);
198 ximage_surface_validate(struct native_surface *nsurf, uint attachment_mask,
199 unsigned int *seq_num, struct pipe_resource **textures,
200 int *width, int *height)
202 struct ximage_surface *xsurf = ximage_surface(nsurf);
205 if (!ximage_surface_update_buffers(&xsurf->base, attachment_mask))
209 *seq_num = xsurf->client_stamp;
212 resource_surface_get_resources(xsurf->rsurf, textures, attachment_mask);
214 resource_surface_get_size(xsurf->rsurf, &w, &h);
224 ximage_surface_wait(struct native_surface *nsurf)
226 struct ximage_surface *xsurf = ximage_surface(nsurf);
227 XSync(xsurf->xdpy->dpy, FALSE);
228 /* TODO XGetImage and update the front texture */
232 ximage_surface_destroy(struct native_surface *nsurf)
234 struct ximage_surface *xsurf = ximage_surface(nsurf);
236 resource_surface_destroy(xsurf->rsurf);
240 static struct ximage_surface *
241 ximage_display_create_surface(struct native_display *ndpy,
243 const struct native_config *nconf)
245 struct ximage_display *xdpy = ximage_display(ndpy);
246 struct ximage_config *xconf = ximage_config(nconf);
247 struct ximage_surface *xsurf;
249 xsurf = CALLOC_STRUCT(ximage_surface);
254 xsurf->color_format = xconf->base.color_format;
255 xsurf->drawable = drawable;
257 xsurf->rsurf = resource_surface_create(xdpy->base.screen,
259 PIPE_BIND_RENDER_TARGET |
260 PIPE_BIND_SAMPLER_VIEW |
261 PIPE_BIND_DISPLAY_TARGET |
268 xsurf->drawable = drawable;
269 xsurf->visual = *xconf->visual;
270 /* initialize the geometry */
271 ximage_surface_update_geometry(&xsurf->base);
273 xsurf->xdraw.visual = xsurf->visual.visual;
274 xsurf->xdraw.depth = xsurf->visual.depth;
275 xsurf->xdraw.drawable = xsurf->drawable;
277 xsurf->base.destroy = ximage_surface_destroy;
278 xsurf->base.present = ximage_surface_present;
279 xsurf->base.validate = ximage_surface_validate;
280 xsurf->base.wait = ximage_surface_wait;
285 static struct native_surface *
286 ximage_display_create_window_surface(struct native_display *ndpy,
287 EGLNativeWindowType win,
288 const struct native_config *nconf)
290 struct ximage_surface *xsurf;
292 xsurf = ximage_display_create_surface(ndpy, (Drawable) win, nconf);
293 return (xsurf) ? &xsurf->base : NULL;
296 static enum pipe_format
297 get_pixmap_format(struct native_display *ndpy, EGLNativePixmapType pix)
299 struct ximage_display *xdpy = ximage_display(ndpy);
300 enum pipe_format fmt;
303 depth = x11_drawable_get_depth(xdpy->xscr, (Drawable) pix);
307 fmt = PIPE_FORMAT_B8G8R8A8_UNORM;
310 fmt = PIPE_FORMAT_B8G8R8X8_UNORM;
313 fmt = PIPE_FORMAT_B5G6R5_UNORM;
316 fmt = PIPE_FORMAT_NONE;
323 static struct native_surface *
324 ximage_display_create_pixmap_surface(struct native_display *ndpy,
325 EGLNativePixmapType pix,
326 const struct native_config *nconf)
328 struct ximage_surface *xsurf;
330 /* find the config */
332 struct ximage_display *xdpy = ximage_display(ndpy);
333 enum pipe_format fmt = get_pixmap_format(&xdpy->base, pix);
336 if (fmt != PIPE_FORMAT_NONE) {
337 for (i = 0; i < xdpy->num_configs; i++) {
338 if (xdpy->configs[i].base.color_format == fmt) {
339 nconf = &xdpy->configs[i].base;
349 xsurf = ximage_display_create_surface(ndpy, (Drawable) pix, nconf);
350 return (xsurf) ? &xsurf->base : NULL;
353 static enum pipe_format
354 choose_format(const XVisualInfo *vinfo)
356 enum pipe_format fmt;
357 /* TODO elaborate the formats */
358 switch (vinfo->depth) {
360 fmt = PIPE_FORMAT_B8G8R8A8_UNORM;
363 fmt = PIPE_FORMAT_B8G8R8X8_UNORM;
366 fmt = PIPE_FORMAT_B5G6R5_UNORM;
369 fmt = PIPE_FORMAT_NONE;
376 static const struct native_config **
377 ximage_display_get_configs(struct native_display *ndpy, int *num_configs)
379 struct ximage_display *xdpy = ximage_display(ndpy);
380 const struct native_config **configs;
384 if (!xdpy->configs) {
385 const XVisualInfo *visuals;
386 int num_visuals, count;
388 visuals = x11_screen_get_visuals(xdpy->xscr, &num_visuals);
393 * Create two configs for each visual.
394 * One with depth/stencil buffer; one without
396 xdpy->configs = CALLOC(num_visuals * 2, sizeof(*xdpy->configs));
401 for (i = 0; i < num_visuals; i++) {
402 struct ximage_config *xconf = &xdpy->configs[count];
404 xconf->visual = &visuals[i];
405 xconf->base.color_format = choose_format(xconf->visual);
406 if (xconf->base.color_format == PIPE_FORMAT_NONE)
409 xconf->base.buffer_mask =
410 (1 << NATIVE_ATTACHMENT_FRONT_LEFT) |
411 (1 << NATIVE_ATTACHMENT_BACK_LEFT);
413 xconf->base.window_bit = TRUE;
414 xconf->base.pixmap_bit = TRUE;
416 xconf->base.native_visual_id = xconf->visual->visualid;
417 #if defined(__cplusplus) || defined(c_plusplus)
418 xconf->base.native_visual_type = xconf->visual->c_class;
420 xconf->base.native_visual_type = xconf->visual->class;
426 xdpy->num_configs = count;
429 configs = MALLOC(xdpy->num_configs * sizeof(*configs));
431 for (i = 0; i < xdpy->num_configs; i++)
432 configs[i] = (const struct native_config *) &xdpy->configs[i];
434 *num_configs = xdpy->num_configs;
440 ximage_display_is_pixmap_supported(struct native_display *ndpy,
441 EGLNativePixmapType pix,
442 const struct native_config *nconf)
444 struct ximage_display *xdpy = ximage_display(ndpy);
445 enum pipe_format fmt = get_pixmap_format(&xdpy->base, pix);
447 return (fmt == nconf->color_format);
451 ximage_display_get_param(struct native_display *ndpy,
452 enum native_param_type param)
457 case NATIVE_PARAM_USE_NATIVE_BUFFER:
458 /* private buffers are allocated */
461 case NATIVE_PARAM_PRESERVE_BUFFER:
462 case NATIVE_PARAM_MAX_SWAP_INTERVAL:
472 ximage_display_destroy(struct native_display *ndpy)
474 struct ximage_display *xdpy = ximage_display(ndpy);
481 x11_screen_destroy(xdpy->xscr);
483 XCloseDisplay(xdpy->dpy);
488 ximage_display_init_screen(struct native_display *ndpy)
490 struct ximage_display *xdpy = ximage_display(ndpy);
491 struct sw_winsys *winsys;
493 winsys = xlib_create_sw_winsys(xdpy->dpy);
498 xdpy->event_handler->new_sw_screen(&xdpy->base, winsys);
499 if (!xdpy->base.screen) {
501 winsys->destroy(winsys);
508 struct native_display *
509 x11_create_ximage_display(Display *dpy,
510 const struct native_event_handler *event_handler)
512 struct ximage_display *xdpy;
514 xdpy = CALLOC_STRUCT(ximage_display);
520 xdpy->dpy = XOpenDisplay(NULL);
525 xdpy->own_dpy = TRUE;
528 xdpy->event_handler = event_handler;
530 xdpy->xscr_number = DefaultScreen(xdpy->dpy);
531 xdpy->xscr = x11_screen_create(xdpy->dpy, xdpy->xscr_number);
534 XCloseDisplay(xdpy->dpy);
539 xdpy->base.init_screen = ximage_display_init_screen;
540 xdpy->base.destroy = ximage_display_destroy;
541 xdpy->base.get_param = ximage_display_get_param;
543 xdpy->base.get_configs = ximage_display_get_configs;
544 xdpy->base.is_pixmap_supported = ximage_display_is_pixmap_supported;
545 xdpy->base.create_window_surface = ximage_display_create_window_surface;
546 xdpy->base.create_pixmap_surface = ximage_display_create_pixmap_surface;