Merge branch 'mesa_7_7_branch'
[mesa.git] / src / glx / x11 / drisw_glx.c
1 /*
2 * Copyright 2008 George Sapountzis
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #ifdef GLX_DIRECT_RENDERING
25
26 #include <X11/Xlib.h>
27 #include "glxclient.h"
28 #include "glcontextmodes.h"
29 #include <dlfcn.h>
30 #include "dri_common.h"
31
32 typedef struct __GLXDRIdisplayPrivateRec __GLXDRIdisplayPrivate;
33 typedef struct __GLXDRIcontextPrivateRec __GLXDRIcontextPrivate;
34 typedef struct __GLXDRIdrawablePrivateRec __GLXDRIdrawablePrivate;
35
36 struct __GLXDRIdisplayPrivateRec
37 {
38 __GLXDRIdisplay base;
39 };
40
41 struct __GLXDRIcontextPrivateRec
42 {
43 __GLXDRIcontext base;
44 __DRIcontext *driContext;
45 __GLXscreenConfigs *psc;
46 };
47
48 struct __GLXDRIdrawablePrivateRec
49 {
50 __GLXDRIdrawable base;
51
52 GC gc;
53 GC swapgc;
54
55 XVisualInfo *visinfo;
56 XImage *ximage;
57 int bpp;
58 };
59
60 /**
61 * swrast loader functions
62 */
63
64 static Bool
65 XCreateDrawable(__GLXDRIdrawablePrivate * pdp,
66 Display * dpy, XID drawable, int visualid)
67 {
68 XGCValues gcvalues;
69 long visMask;
70 XVisualInfo visTemp;
71 int num_visuals;
72
73 /* create GC's */
74 pdp->gc = XCreateGC(dpy, drawable, 0, NULL);
75 pdp->swapgc = XCreateGC(dpy, drawable, 0, NULL);
76
77 gcvalues.function = GXcopy;
78 gcvalues.graphics_exposures = False;
79 XChangeGC(dpy, pdp->gc, GCFunction, &gcvalues);
80 XChangeGC(dpy, pdp->swapgc, GCFunction, &gcvalues);
81 XChangeGC(dpy, pdp->swapgc, GCGraphicsExposures, &gcvalues);
82
83 /* create XImage */
84 visTemp.screen = DefaultScreen(dpy);
85 visTemp.visualid = visualid;
86 visMask = (VisualScreenMask | VisualIDMask);
87 pdp->visinfo = XGetVisualInfo(dpy, visMask, &visTemp, &num_visuals);
88
89 pdp->ximage = XCreateImage(dpy, pdp->visinfo->visual, pdp->visinfo->depth, ZPixmap, 0, /* format, offset */
90 NULL, /* data */
91 0, 0, /* size */
92 32, /* bitmap_pad */
93 0); /* bytes_per_line */
94
95 /* get the true number of bits per pixel */
96 pdp->bpp = pdp->ximage->bits_per_pixel;
97
98 return True;
99 }
100
101 static void
102 XDestroyDrawable(__GLXDRIdrawablePrivate * pdp, Display * dpy, XID drawable)
103 {
104 XDestroyImage(pdp->ximage);
105 XFree(pdp->visinfo);
106
107 XFreeGC(dpy, pdp->gc);
108 XFreeGC(dpy, pdp->swapgc);
109 }
110
111 static void
112 swrastGetDrawableInfo(__DRIdrawable * draw,
113 int *x, int *y, int *w, int *h, void *loaderPrivate)
114 {
115 __GLXDRIdrawablePrivate *pdp = loaderPrivate;
116 __GLXDRIdrawable *pdraw = &(pdp->base);
117 Display *dpy = pdraw->psc->dpy;
118 Drawable drawable;
119
120 Window root;
121 Status stat;
122 unsigned int bw, depth;
123
124 drawable = pdraw->xDrawable;
125
126 stat = XGetGeometry(dpy, drawable, &root,
127 x, y, (unsigned int *) w, (unsigned int *) h,
128 &bw, &depth);
129 }
130
131 static inline int
132 bytes_per_line(int w, int bpp, unsigned mul)
133 {
134 unsigned mask = mul - 1;
135
136 return ((w * bpp + mask) & ~mask) / 8;
137 }
138
139 static void
140 swrastPutImage(__DRIdrawable * draw, int op,
141 int x, int y, int w, int h, char *data, void *loaderPrivate)
142 {
143 __GLXDRIdrawablePrivate *pdp = loaderPrivate;
144 __GLXDRIdrawable *pdraw = &(pdp->base);
145 Display *dpy = pdraw->psc->dpy;
146 Drawable drawable;
147 XImage *ximage;
148 GC gc;
149
150 switch (op) {
151 case __DRI_SWRAST_IMAGE_OP_DRAW:
152 gc = pdp->gc;
153 break;
154 case __DRI_SWRAST_IMAGE_OP_SWAP:
155 gc = pdp->swapgc;
156 break;
157 default:
158 return;
159 }
160
161 drawable = pdraw->xDrawable;
162
163 ximage = pdp->ximage;
164 ximage->data = data;
165 ximage->width = w;
166 ximage->height = h;
167 ximage->bytes_per_line = bytes_per_line(w, pdp->bpp, 32);
168
169 XPutImage(dpy, drawable, gc, ximage, 0, 0, x, y, w, h);
170
171 ximage->data = NULL;
172 }
173
174 static void
175 swrastGetImage(__DRIdrawable * draw,
176 int x, int y, int w, int h, char *data, void *loaderPrivate)
177 {
178 __GLXDRIdrawablePrivate *pdp = loaderPrivate;
179 __GLXDRIdrawable *pdraw = &(pdp->base);
180 Display *dpy = pdraw->psc->dpy;
181 Drawable drawable;
182 XImage *ximage;
183
184 drawable = pdraw->xDrawable;
185
186 ximage = pdp->ximage;
187 ximage->data = data;
188 ximage->width = w;
189 ximage->height = h;
190 ximage->bytes_per_line = bytes_per_line(w, pdp->bpp, 32);
191
192 XGetSubImage(dpy, drawable, x, y, w, h, ~0L, ZPixmap, ximage, 0, 0);
193
194 ximage->data = NULL;
195 }
196
197 static const __DRIswrastLoaderExtension swrastLoaderExtension = {
198 {__DRI_SWRAST_LOADER, __DRI_SWRAST_LOADER_VERSION},
199 swrastGetDrawableInfo,
200 swrastPutImage,
201 swrastGetImage
202 };
203
204 static const __DRIextension *loader_extensions[] = {
205 &systemTimeExtension.base,
206 &swrastLoaderExtension.base,
207 NULL
208 };
209
210 /**
211 * GLXDRI functions
212 */
213
214 static void
215 driDestroyContext(__GLXDRIcontext * context,
216 __GLXscreenConfigs * psc, Display * dpy)
217 {
218 __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
219 const __DRIcoreExtension *core = pcp->psc->core;
220
221 (*core->destroyContext) (pcp->driContext);
222
223 Xfree(pcp);
224 }
225
226 static Bool
227 driBindContext(__GLXDRIcontext * context,
228 __GLXDRIdrawable * draw, __GLXDRIdrawable * read)
229 {
230 __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
231 const __DRIcoreExtension *core = pcp->psc->core;
232
233 return (*core->bindContext) (pcp->driContext,
234 draw->driDrawable, read->driDrawable);
235 }
236
237 static void
238 driUnbindContext(__GLXDRIcontext * context)
239 {
240 __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
241 const __DRIcoreExtension *core = pcp->psc->core;
242
243 (*core->unbindContext) (pcp->driContext);
244 }
245
246 static __GLXDRIcontext *
247 driCreateContext(__GLXscreenConfigs * psc,
248 const __GLcontextModes * mode,
249 GLXContext gc, GLXContext shareList, int renderType)
250 {
251 __GLXDRIcontextPrivate *pcp, *pcp_shared;
252 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) mode;
253 const __DRIcoreExtension *core;
254 __DRIcontext *shared = NULL;
255
256 if (!psc || !psc->driScreen)
257 return NULL;
258
259 core = psc->core;
260
261 if (shareList) {
262 pcp_shared = (__GLXDRIcontextPrivate *) shareList->driContext;
263 shared = pcp_shared->driContext;
264 }
265
266 pcp = Xmalloc(sizeof *pcp);
267 if (pcp == NULL)
268 return NULL;
269
270 pcp->psc = psc;
271 pcp->driContext =
272 (*core->createNewContext) (psc->__driScreen,
273 config->driConfig, shared, pcp);
274 if (pcp->driContext == NULL) {
275 Xfree(pcp);
276 return NULL;
277 }
278
279 pcp->base.destroyContext = driDestroyContext;
280 pcp->base.bindContext = driBindContext;
281 pcp->base.unbindContext = driUnbindContext;
282
283 return &pcp->base;
284 }
285
286 static void
287 driDestroyDrawable(__GLXDRIdrawable * pdraw)
288 {
289 __GLXDRIdrawablePrivate *pdp = (__GLXDRIdrawablePrivate *) pdraw;
290 const __DRIcoreExtension *core = pdraw->psc->core;
291
292 (*core->destroyDrawable) (pdraw->driDrawable);
293
294 XDestroyDrawable(pdp, pdraw->psc->dpy, pdraw->drawable);
295 Xfree(pdp);
296 }
297
298 static __GLXDRIdrawable *
299 driCreateDrawable(__GLXscreenConfigs * psc,
300 XID xDrawable,
301 GLXDrawable drawable, const __GLcontextModes * modes)
302 {
303 __GLXDRIdrawable *pdraw;
304 __GLXDRIdrawablePrivate *pdp;
305 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) modes;
306 const __DRIswrastExtension *swrast = psc->swrast;
307
308 /* Old dri can't handle GLX 1.3+ drawable constructors. */
309 if (xDrawable != drawable)
310 return NULL;
311
312 pdp = Xmalloc(sizeof(*pdp));
313 if (!pdp)
314 return NULL;
315
316 pdraw = &(pdp->base);
317 pdraw->xDrawable = xDrawable;
318 pdraw->drawable = drawable;
319 pdraw->psc = psc;
320
321 XCreateDrawable(pdp, psc->dpy, xDrawable, modes->visualID);
322
323 /* Create a new drawable */
324 pdraw->driDrawable =
325 (*swrast->createNewDrawable) (psc->__driScreen, config->driConfig, pdp);
326
327 if (!pdraw->driDrawable) {
328 XDestroyDrawable(pdp, psc->dpy, xDrawable);
329 Xfree(pdp);
330 return NULL;
331 }
332
333 pdraw->destroyDrawable = driDestroyDrawable;
334
335 return pdraw;
336 }
337
338 static void
339 driSwapBuffers(__GLXDRIdrawable * pdraw)
340 {
341 (*pdraw->psc->core->swapBuffers) (pdraw->driDrawable);
342 }
343
344 static void
345 driDestroyScreen(__GLXscreenConfigs * psc)
346 {
347 /* Free the direct rendering per screen data */
348 (*psc->core->destroyScreen) (psc->__driScreen);
349 psc->__driScreen = NULL;
350 if (psc->driver)
351 dlclose(psc->driver);
352 }
353
354 static __GLXDRIscreen *
355 driCreateScreen(__GLXscreenConfigs * psc, int screen,
356 __GLXdisplayPrivate * priv)
357 {
358 __GLXDRIscreen *psp;
359 const __DRIconfig **driver_configs;
360 const __DRIextension **extensions;
361 const char *driverName = "swrast";
362 int i;
363
364 psp = Xcalloc(1, sizeof *psp);
365 if (psp == NULL)
366 return NULL;
367
368 /* Initialize per screen dynamic client GLX extensions */
369 psc->ext_list_first_time = GL_TRUE;
370
371 psc->driver = driOpenDriver(driverName);
372 if (psc->driver == NULL)
373 goto handle_error;
374
375 extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
376 if (extensions == NULL) {
377 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
378 goto handle_error;
379 }
380
381 for (i = 0; extensions[i]; i++) {
382 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
383 psc->core = (__DRIcoreExtension *) extensions[i];
384 if (strcmp(extensions[i]->name, __DRI_SWRAST) == 0)
385 psc->swrast = (__DRIswrastExtension *) extensions[i];
386 }
387
388 if (psc->core == NULL || psc->swrast == NULL) {
389 ErrorMessageF("core dri extension not found\n");
390 goto handle_error;
391 }
392
393 psc->__driScreen =
394 psc->swrast->createNewScreen(screen,
395 loader_extensions, &driver_configs, psc);
396 if (psc->__driScreen == NULL) {
397 ErrorMessageF("failed to create dri screen\n");
398 goto handle_error;
399 }
400
401 driBindExtensions(psc);
402 driBindCommonExtensions(psc);
403
404 psc->configs = driConvertConfigs(psc->core, psc->configs, driver_configs);
405 psc->visuals = driConvertConfigs(psc->core, psc->visuals, driver_configs);
406
407 psc->driver_configs = driver_configs;
408
409 psp->destroyScreen = driDestroyScreen;
410 psp->createContext = driCreateContext;
411 psp->createDrawable = driCreateDrawable;
412 psp->swapBuffers = driSwapBuffers;
413 psp->waitX = NULL;
414 psp->waitGL = NULL;
415
416 return psp;
417
418 handle_error:
419 Xfree(psp);
420
421 if (psc->driver)
422 dlclose(psc->driver);
423
424 ErrorMessageF("reverting to indirect rendering\n");
425
426 return NULL;
427 }
428
429 /* Called from __glXFreeDisplayPrivate.
430 */
431 static void
432 driDestroyDisplay(__GLXDRIdisplay * dpy)
433 {
434 Xfree(dpy);
435 }
436
437 /*
438 * Allocate, initialize and return a __DRIdisplayPrivate object.
439 * This is called from __glXInitialize() when we are given a new
440 * display pointer.
441 */
442 _X_HIDDEN __GLXDRIdisplay *
443 driswCreateDisplay(Display * dpy)
444 {
445 __GLXDRIdisplayPrivate *pdpyp;
446
447 pdpyp = Xmalloc(sizeof *pdpyp);
448 if (pdpyp == NULL)
449 return NULL;
450
451 pdpyp->base.destroyDisplay = driDestroyDisplay;
452 pdpyp->base.createScreen = driCreateScreen;
453
454 return &pdpyp->base;
455 }
456
457 #endif /* GLX_DIRECT_RENDERING */