2 #include <cogl/cogl-xlib.h>
3 #include <cogl/winsys/cogl-texture-pixmap-x11.h>
10 #include <X11/extensions/Xcomposite.h>
12 #define X11_FOREIGN_EVENT_MASK \
19 #define TFP_XWIN_WIDTH 200
20 #define TFP_XWIN_HEIGHT 200
23 update_cogl_x11_event_mask (CoglOnscreen *onscreen,
27 Display *xdpy = user_data;
28 XSetWindowAttributes attrs;
31 attrs.event_mask = event_mask | X11_FOREIGN_EVENT_MASK;
32 xwin = cogl_x11_onscreen_get_window_xid (onscreen);
34 XChangeWindowAttributes (xdpy,
41 main (int argc, char **argv)
44 int composite_error = 0, composite_event = 0;
45 CoglRenderer *renderer;
47 CoglOnscreenTemplate *onscreen_template;
50 CoglOnscreen *onscreen;
54 XVisualInfo template, *xvisinfo;
56 XSetWindowAttributes xattr;
62 CoglTexturePixmapX11 *tfp;
65 g_print ("NB: Don't use this example as a benchmark since there is "
66 "no synchonization between X window updates and onscreen "
67 "framebuffer updates!\n");
69 /* Since we want to test external ownership of the X display,
70 * connect to X manually... */
71 xdpy = XOpenDisplay (NULL);
74 fprintf (stderr, "Failed to open X Display\n");
78 XSynchronize (xdpy, True);
80 if (XCompositeQueryExtension (xdpy, &composite_event, &composite_error))
82 int major = 0, minor = 0;
83 if (XCompositeQueryVersion (xdpy, &major, &minor))
85 if (major != 0 || minor < 3)
86 g_error ("Missing XComposite extension >= 0.3");
90 /* Conceptually choose a GPU... */
91 renderer = cogl_renderer_new ();
92 /* FIXME: This should conceptually be part of the configuration of
94 cogl_xlib_renderer_set_foreign_display (renderer, xdpy);
95 if (!cogl_renderer_connect (renderer, &error))
97 fprintf (stderr, "Failed to connect to a renderer: %s\n",
101 chain = cogl_swap_chain_new ();
102 cogl_swap_chain_set_has_alpha (chain, TRUE);
104 /* Conceptually declare upfront the kinds of windows we anticipate
105 * creating so that when we configure the display pipeline we can avoid
106 * having an impedance miss-match between the format of windows and the
107 * format the display pipeline expects. */
108 onscreen_template = cogl_onscreen_template_new (chain);
109 cogl_object_unref (chain);
111 /* Conceptually setup a display pipeline */
112 display = cogl_display_new (renderer, onscreen_template);
113 cogl_object_unref (renderer);
114 if (!cogl_display_setup (display, &error))
116 fprintf (stderr, "Failed to setup a display pipeline: %s\n",
121 ctx = cogl_context_new (display, &error);
124 fprintf (stderr, "Failed to create context: %s\n", error->message);
128 onscreen = cogl_onscreen_new (ctx, 640, 480);
130 /* We want to test that Cogl can handle foreign X windows... */
132 visual = cogl_x11_onscreen_get_visual_xid (onscreen);
135 fprintf (stderr, "Failed to query an X visual suitable for the "
136 "configured CoglOnscreen framebuffer\n");
140 template.visualid = visual;
141 xvisinfo = XGetVisualInfo (xdpy, VisualIDMask, &template, &visinfos_count);
143 /* window attributes */
144 xattr.background_pixel = WhitePixel (xdpy, DefaultScreen (xdpy));
145 xattr.border_pixel = 0;
146 xattr.colormap = XCreateColormap (xdpy,
147 DefaultRootWindow (xdpy),
150 mask = CWBorderPixel | CWColormap;
152 xwin = XCreateWindow (xdpy,
153 DefaultRootWindow (xdpy),
164 cogl_x11_onscreen_set_foreign_window_xid (onscreen, xwin,
165 update_cogl_x11_event_mask,
168 XMapWindow (xdpy, xwin);
170 XCompositeRedirectSubwindows (xdpy, xwin, CompositeRedirectManual);
172 screen = DefaultScreen (xdpy);
173 tfp_xwin = XCreateSimpleWindow (xdpy, xwin,
174 0, 0, TFP_XWIN_WIDTH, TFP_XWIN_HEIGHT,
176 WhitePixel (xdpy, screen),
177 WhitePixel (xdpy, screen));
178 XMapWindow (xdpy, tfp_xwin);
180 gc = XCreateGC (xdpy, tfp_xwin, 0, NULL);
182 pixmap = XCompositeNameWindowPixmap (xdpy, tfp_xwin);
184 tfp = cogl_texture_pixmap_x11_new (ctx, pixmap, TRUE, &error);
187 fprintf (stderr, "Failed to create CoglTexturePixmapX11: %s",
192 fb = COGL_FRAMEBUFFER (onscreen);
193 cogl_push_framebuffer (fb);
199 while (XPending (xdpy))
203 XNextEvent (xdpy, &event);
207 keysym = XLookupKeysym (&event.xkey, 0);
208 if (keysym == XK_q || keysym == XK_Q || keysym == XK_Escape)
211 cogl_xlib_renderer_handle_event (renderer, &event);
215 g_random_int_range (0, 255) << 24 |
216 g_random_int_range (0, 255) << 16 |
217 g_random_int_range (0, 255) << 8;
218 g_random_int_range (0, 255);
219 XSetForeground (xdpy, gc, pixel);
220 XFillRectangle (xdpy, tfp_xwin, gc, 0, 0, TFP_XWIN_WIDTH, TFP_XWIN_HEIGHT);
223 cogl_framebuffer_clear4f (fb, COGL_BUFFER_BIT_COLOR, 0, 0, 0, 1);
224 cogl_set_source_texture (COGL_TEXTURE (tfp));
225 cogl_rectangle (-0.8, 0.8, 0.8, -0.8);
226 cogl_onscreen_swap_buffers (onscreen);