g3dvl: Use sobel filter for chroma interpolation
[mesa/nouveau-pmpeg.git] / src / gallium / targets / graw-xlib / graw_xlib.c
blobb6d798e577c262176b6fa8e6cd8c6e19dcfed253
1 #include "pipe/p_compiler.h"
2 #include "pipe/p_context.h"
3 #include "pipe/p_screen.h"
4 #include "util/u_debug.h"
5 #include "util/u_memory.h"
6 #include "target-helpers/inline_sw_helper.h"
7 #include "target-helpers/inline_debug_helper.h"
8 #include "state_tracker/xlib_sw_winsys.h"
9 #include "state_tracker/graw.h"
11 #include <X11/Xlib.h>
12 #include <X11/Xlibint.h>
13 #include <X11/Xutil.h>
14 #include <stdio.h>
16 static struct {
17 Display *display;
18 void (*draw)(void);
19 } graw;
22 static struct pipe_screen *
23 graw_create_screen( void )
25 struct pipe_screen *screen = NULL;
26 struct sw_winsys *winsys = NULL;
28 /* Create the underlying winsys, which performs presents to Xlib
29 * drawables:
31 winsys = xlib_create_sw_winsys( graw.display );
32 if (winsys == NULL)
33 return NULL;
35 screen = sw_screen_create( winsys );
37 /* Inject any wrapping layers we want to here:
39 return debug_screen_wrap( screen );
43 struct pipe_screen *
44 graw_create_window_and_screen( int x,
45 int y,
46 unsigned width,
47 unsigned height,
48 enum pipe_format format,
49 void **handle)
51 struct pipe_screen *screen = NULL;
52 struct xlib_drawable *xlib_handle = NULL;
53 XSetWindowAttributes attr;
54 Window root;
55 Window win = 0;
56 XVisualInfo templat, *visinfo = NULL;
57 unsigned mask;
58 int n;
59 int scrnum;
61 graw.display = XOpenDisplay(NULL);
62 if (graw.display == NULL)
63 return NULL;
65 scrnum = DefaultScreen( graw.display );
66 root = RootWindow( graw.display, scrnum );
69 if (graw.display == NULL)
70 goto fail;
72 xlib_handle = CALLOC_STRUCT(xlib_drawable);
73 if (xlib_handle == NULL)
74 goto fail;
77 mask = VisualScreenMask | VisualDepthMask | VisualClassMask;
78 templat.screen = DefaultScreen(graw.display);
79 templat.depth = 32;
80 templat.class = TrueColor;
82 visinfo = XGetVisualInfo(graw.display, mask, &templat, &n);
83 if (!visinfo) {
84 printf("Error: couldn't get an RGB, Double-buffered visual\n");
85 exit(1);
88 /* See if the requirested pixel format matches the visual */
89 if (visinfo->red_mask == 0xff0000 &&
90 visinfo->green_mask == 0xff00 &&
91 visinfo->blue_mask == 0xff) {
92 if (format != PIPE_FORMAT_B8G8R8A8_UNORM)
93 goto fail;
95 else if (visinfo->red_mask == 0xff &&
96 visinfo->green_mask == 0xff00 &&
97 visinfo->blue_mask == 0xff0000) {
98 if (format != PIPE_FORMAT_R8G8B8A8_UNORM)
99 goto fail;
101 else {
102 goto fail;
105 /* window attributes */
106 attr.background_pixel = 0;
107 attr.border_pixel = 0;
108 attr.colormap = XCreateColormap( graw.display, root, visinfo->visual, AllocNone);
109 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
110 /* XXX this is a bad way to get a borderless window! */
111 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
113 win = XCreateWindow( graw.display, root, x, y, width, height,
114 0, visinfo->depth, InputOutput,
115 visinfo->visual, mask, &attr );
118 /* set hints and properties */
120 char *name = NULL;
121 XSizeHints sizehints;
122 sizehints.x = x;
123 sizehints.y = y;
124 sizehints.width = width;
125 sizehints.height = height;
126 sizehints.flags = USSize | USPosition;
127 XSetNormalHints(graw.display, win, &sizehints);
128 XSetStandardProperties(graw.display, win, name, name,
129 None, (char **)NULL, 0, &sizehints);
132 XMapWindow(graw.display, win);
133 while (1) {
134 XEvent e;
135 XNextEvent( graw.display, &e );
136 if (e.type == MapNotify && e.xmap.window == win) {
137 break;
141 xlib_handle->visual = visinfo->visual;
142 xlib_handle->drawable = (Drawable)win;
143 xlib_handle->depth = visinfo->depth;
144 *handle = (void *)xlib_handle;
146 screen = graw_create_screen();
147 if (screen == NULL)
148 goto fail;
150 XFree(visinfo);
151 return screen;
153 fail:
154 if (screen)
155 screen->destroy(screen);
157 if (xlib_handle)
158 FREE(xlib_handle);
160 if (visinfo)
161 XFree(visinfo);
163 if (win)
164 XDestroyWindow(graw.display, win);
166 return NULL;
170 void
171 graw_set_display_func( void (*draw)( void ) )
173 graw.draw = draw;
176 void
177 graw_main_loop( void )
179 int i;
180 for (i = 0; i < 10; i++) {
181 graw.draw();
182 sleep(1);