Merge branch 'gallium-tex-surfaces' into gallium-0.1
[mesa.git] / src / gallium / winsys / egl_xlib / egl_xlib.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * EGL / softpipe / xlib winsys module
30 *
31 * Authors: Brian Paul
32 */
33
34
35 #include <X11/Xutil.h>
36
37 #include "pipe/p_compiler.h"
38 #include "pipe/p_format.h"
39 #include "pipe/p_state.h"
40 #include "pipe/p_util.h"
41 #include "pipe/p_winsys.h"
42 #include "softpipe/sp_winsys.h"
43
44 #include "eglconfig.h"
45 #include "eglconfigutil.h"
46 #include "eglcontext.h"
47 #include "egldisplay.h"
48 #include "egldriver.h"
49 #include "eglglobals.h"
50 #include "egllog.h"
51 #include "eglsurface.h"
52
53 #include "state_tracker/st_public.h"
54
55 #include "sw_winsys.h"
56
57
58 /** subclass of _EGLDriver */
59 struct xlib_egl_driver
60 {
61 _EGLDriver Base; /**< base class */
62
63 struct pipe_winsys *winsys;
64 struct pipe_screen *screen;
65 };
66
67
68 /** subclass of _EGLContext */
69 struct xlib_egl_context
70 {
71 _EGLContext Base; /**< base class */
72
73 struct pipe_context *pipe; /**< Gallium driver context */
74 struct st_context *Context; /**< Mesa/gallium state tracker context */
75 };
76
77
78 /** subclass of _EGLSurface */
79 struct xlib_egl_surface
80 {
81 _EGLSurface Base; /**< base class */
82
83 Display *Dpy; /**< The X Display of the window */
84 Window Win; /**< The user-created window ID */
85 GC Gc;
86 XVisualInfo VisInfo;
87
88 struct pipe_winsys *winsys;
89
90 struct st_framebuffer *Framebuffer;
91 };
92
93
94 /** cast wrapper */
95 static INLINE struct xlib_egl_driver *
96 xlib_egl_driver(_EGLDriver *drv)
97 {
98 return (struct xlib_egl_driver *) drv;
99 }
100
101
102 static struct xlib_egl_surface *
103 lookup_surface(EGLSurface surf)
104 {
105 _EGLSurface *surface = _eglLookupSurface(surf);
106 return (struct xlib_egl_surface *) surface;
107 }
108
109
110 static struct xlib_egl_context *
111 lookup_context(EGLContext surf)
112 {
113 _EGLContext *context = _eglLookupContext(surf);
114 return (struct xlib_egl_context *) context;
115 }
116
117
118 /**
119 * XXX temporary
120 * Need to query X server's GLX visuals.
121 */
122 static void
123 init_configs(_EGLDriver *drv, EGLDisplay dpy)
124 {
125 _EGLDisplay *disp = _eglLookupDisplay(dpy);
126 int i;
127
128 for (i = 0; i < 2; i++) {
129 _EGLConfig config;
130 int id = i + 1;
131 _eglInitConfig(&config, id);
132 SET_CONFIG_ATTRIB(&config, EGL_RED_SIZE, 8);
133 SET_CONFIG_ATTRIB(&config, EGL_GREEN_SIZE, 8);
134 SET_CONFIG_ATTRIB(&config, EGL_BLUE_SIZE, 8);
135 SET_CONFIG_ATTRIB(&config, EGL_ALPHA_SIZE, 8);
136 if (i > 0) {
137 SET_CONFIG_ATTRIB(&config, EGL_DEPTH_SIZE, 24);
138 SET_CONFIG_ATTRIB(&config, EGL_STENCIL_SIZE, 8);
139 }
140 _eglAddConfig(disp, &config);
141 }
142 }
143
144
145
146 /**
147 * Called via eglInitialize(), drv->API.Initialize().
148 */
149 static EGLBoolean
150 xlib_eglInitialize(_EGLDriver *drv, EGLDisplay dpy,
151 EGLint *minor, EGLint *major)
152 {
153 /* visual configs */
154
155 init_configs(drv, dpy);
156
157
158 drv->Initialized = EGL_TRUE;
159
160 /* we're supporting EGL 1.4 */
161 *minor = 1;
162 *major = 4;
163
164 return EGL_TRUE;
165 }
166
167
168 /**
169 * Called via eglTerminate(), drv->API.Terminate().
170 */
171 static EGLBoolean
172 xlib_eglTerminate(_EGLDriver *drv, EGLDisplay dpy)
173 {
174
175 return EGL_TRUE;
176 }
177
178
179 /** Get size of given window */
180 static Status
181 get_drawable_size(Display *dpy, Drawable d, uint *width, uint *height)
182 {
183 Window root;
184 Status stat;
185 int xpos, ypos;
186 unsigned int w, h, bw, depth;
187 stat = XGetGeometry(dpy, d, &root, &xpos, &ypos, &w, &h, &bw, &depth);
188 *width = w;
189 *height = h;
190 return stat;
191 }
192
193
194 static void
195 display_surface(struct pipe_winsys *pws,
196 struct pipe_surface *psurf,
197 struct xlib_egl_surface *xsurf)
198 {
199 XImage *ximage;
200 void *data;
201
202 ximage = XCreateImage(xsurf->Dpy,
203 xsurf->VisInfo.visual,
204 xsurf->VisInfo.depth,
205 ZPixmap, 0, /* format, offset */
206 NULL, /* data */
207 0, 0, /* size */
208 32, /* bitmap_pad */
209 0); /* bytes_per_line */
210
211
212 assert(ximage->format);
213 assert(ximage->bitmap_unit);
214
215 data = pws->buffer_map(pws, psurf->buffer, 0);
216
217 /* update XImage's fields */
218 ximage->data = data;
219 ximage->width = psurf->width;
220 ximage->height = psurf->height;
221 ximage->bytes_per_line = psurf->pitch * psurf->cpp;
222
223 XPutImage(xsurf->Dpy, xsurf->Win, xsurf->Gc,
224 ximage, 0, 0, 0, 0, psurf->width, psurf->height);
225
226 ximage->data = NULL;
227 XDestroyImage(ximage);
228
229 pws->buffer_unmap(pws, psurf->buffer);
230 }
231
232
233
234 /** Display gallium surface in X window */
235 static void
236 flush_frontbuffer(struct pipe_winsys *pws,
237 struct pipe_surface *psurf,
238 void *context_private)
239 {
240 struct xlib_egl_surface *xsurf = (struct xlib_egl_surface *) context_private;
241 display_surface(pws, psurf, xsurf);
242 }
243
244
245
246 /**
247 * Called via eglCreateContext(), drv->API.CreateContext().
248 */
249 static EGLContext
250 xlib_eglCreateContext(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
251 EGLContext share_list, const EGLint *attrib_list)
252 {
253 struct xlib_egl_driver *xdrv = xlib_egl_driver(drv);
254 _EGLConfig *conf = _eglLookupConfig(drv, dpy, config);
255 struct xlib_egl_context *ctx;
256 struct st_context *share_ctx = NULL; /* XXX fix */
257 __GLcontextModes visual;
258
259 ctx = CALLOC_STRUCT(xlib_egl_context);
260 if (!ctx)
261 return EGL_NO_CONTEXT;
262
263 /* let EGL lib init the common stuff */
264 if (!_eglInitContext(drv, dpy, &ctx->Base, config, attrib_list)) {
265 free(ctx);
266 return EGL_NO_CONTEXT;
267 }
268
269 /* create a softpipe context */
270 ctx->pipe = softpipe_create(xdrv->screen, xdrv->winsys, NULL);
271
272 /* Now do xlib / state tracker inits here */
273 _eglConfigToContextModesRec(conf, &visual);
274 ctx->Context = st_create_context(ctx->pipe, &visual, share_ctx);
275
276
277 return _eglGetContextHandle(&ctx->Base);
278 }
279
280
281 static EGLBoolean
282 xlib_eglDestroyContext(_EGLDriver *drv, EGLDisplay dpy, EGLContext ctx)
283 {
284 struct xlib_egl_context *context = lookup_context(ctx);
285 if (context) {
286 if (context->Base.IsBound) {
287 context->Base.DeletePending = EGL_TRUE;
288 }
289 else {
290 st_destroy_context(context->Context);
291 free(context);
292 }
293 return EGL_TRUE;
294 }
295 else {
296 _eglError(EGL_BAD_CONTEXT, "eglDestroyContext");
297 return EGL_TRUE;
298 }
299 }
300
301
302 /**
303 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
304 */
305 static EGLBoolean
306 xlib_eglMakeCurrent(_EGLDriver *drv, EGLDisplay dpy, EGLSurface d,
307 EGLSurface r, EGLContext context)
308 {
309 if (!_eglMakeCurrent(drv, dpy, d, r, context))
310 return EGL_FALSE;
311
312 /* XXX anything todo? */
313
314 return EGL_TRUE;
315 }
316
317
318 static enum pipe_format
319 choose_color_format(const __GLcontextModes *visual)
320 {
321 if (visual->redBits == 8 &&
322 visual->greenBits == 8 &&
323 visual->blueBits == 8 &&
324 visual->alphaBits == 8) {
325 /* XXX this really also depends on the ordering of R,G,B,A */
326 return PIPE_FORMAT_A8R8G8B8_UNORM;
327 }
328 else {
329 assert(0);
330 return PIPE_FORMAT_NONE;
331 }
332 }
333
334
335 static enum pipe_format
336 choose_depth_format(const __GLcontextModes *visual)
337 {
338 if (visual->depthBits > 0)
339 return PIPE_FORMAT_Z24S8_UNORM;
340 else
341 return PIPE_FORMAT_NONE;
342 }
343
344
345 static enum pipe_format
346 choose_stencil_format(const __GLcontextModes *visual)
347 {
348 if (visual->stencilBits > 0)
349 return PIPE_FORMAT_Z24S8_UNORM;
350 else
351 return PIPE_FORMAT_NONE;
352 }
353
354
355 /**
356 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
357 */
358 static EGLSurface
359 xlib_eglCreateWindowSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
360 NativeWindowType window, const EGLint *attrib_list)
361 {
362 struct xlib_egl_driver *xdrv = xlib_egl_driver(drv);
363 _EGLDisplay *disp = _eglLookupDisplay(dpy);
364 _EGLConfig *conf = _eglLookupConfig(drv, dpy, config);
365
366 struct xlib_egl_surface *surf;
367 __GLcontextModes visual;
368 uint width, height;
369
370 surf = CALLOC_STRUCT(xlib_egl_surface);
371 if (!surf)
372 return EGL_NO_SURFACE;
373
374 /* Let EGL lib init the common stuff */
375 if (!_eglInitSurface(drv, dpy, &surf->Base, EGL_WINDOW_BIT,
376 config, attrib_list)) {
377 free(surf);
378 return EGL_NO_SURFACE;
379 }
380
381 /*
382 * Now init the Xlib and gallium stuff
383 */
384 surf->Win = (Window) window; /* The X window ID */
385 surf->Dpy = disp->Xdpy; /* The X display */
386 surf->Gc = XCreateGC(surf->Dpy, surf->Win, 0, NULL);
387
388 surf->winsys = xdrv->winsys;
389
390 _eglConfigToContextModesRec(conf, &visual);
391 get_drawable_size(surf->Dpy, surf->Win, &width, &height);
392
393 /* Create GL statetracker framebuffer */
394 surf->Framebuffer = st_create_framebuffer(&visual,
395 choose_color_format(&visual),
396 choose_depth_format(&visual),
397 choose_stencil_format(&visual),
398 width, height,
399 (void *) surf);
400 return surf;
401 }
402
403
404 static EGLBoolean
405 xlib_eglSwapBuffers(_EGLDriver *drv, EGLDisplay dpy, EGLSurface draw)
406 {
407 /* error checking step: */
408 if (!_eglSwapBuffers(drv, dpy, draw))
409 return EGL_FALSE;
410
411 {
412 struct xlib_egl_surface *xsurf = lookup_surface(draw);
413 struct pipe_winsys *pws = xsurf->winsys;
414 struct pipe_surface *psurf =
415 st_get_framebuffer_surface(xsurf->Framebuffer, ST_SURFACE_BACK_LEFT);
416
417 display_surface(pws, psurf, xsurf);
418 }
419
420 return EGL_TRUE;
421 }
422
423
424 /**
425 * This is the main entrypoint into the driver.
426 * Called by libEGL to instantiate an _EGLDriver object.
427 */
428 _EGLDriver *
429 _eglMain(_EGLDisplay *dpy, const char *args)
430 {
431 struct xlib_egl_driver *xdrv;
432
433 _eglLog(_EGL_INFO, "Entering EGL/Xlib _eglMain(%s)", args);
434
435 xdrv = CALLOC_STRUCT(xlib_egl_driver);
436 if (!xdrv)
437 return NULL;
438
439 _eglInitDriverFallbacks(&xdrv->Base);
440 xdrv->Base.API.Initialize = xlib_eglInitialize;
441 xdrv->Base.API.Terminate = xlib_eglTerminate;
442 xdrv->Base.API.CreateContext = xlib_eglCreateContext;
443 xdrv->Base.API.DestroyContext = xlib_eglDestroyContext;
444 xdrv->Base.API.CreateWindowSurface = xlib_eglCreateWindowSurface;
445 xdrv->Base.API.MakeCurrent = xlib_eglMakeCurrent;
446 xdrv->Base.API.SwapBuffers = xlib_eglSwapBuffers;
447
448
449 xdrv->Base.ClientAPIs = "OpenGL"; /* "OpenGL_ES" */
450 xdrv->Base.Name = "Xlib/softpipe";
451
452 /* create one winsys and use it for all contexts/surfaces */
453 xdrv->winsys = create_sw_winsys();
454 xdrv->winsys->flush_frontbuffer = flush_frontbuffer;
455
456 xdrv->screen = softpipe_create_screen(xdrv->winsys);
457
458
459 return &xdrv->Base;
460 }
461