Merge branch '7.8'
[mesa.git] / src / glx / 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 <dlfcn.h>
29 #include "dri_common.h"
30
31 typedef struct __GLXDRIdisplayPrivateRec __GLXDRIdisplayPrivate;
32 typedef struct __GLXDRIcontextPrivateRec __GLXDRIcontextPrivate;
33 typedef struct __GLXDRIdrawablePrivateRec __GLXDRIdrawablePrivate;
34
35 struct __GLXDRIdisplayPrivateRec
36 {
37 __GLXDRIdisplay base;
38 };
39
40 struct __GLXDRIcontextPrivateRec
41 {
42 __GLXDRIcontext base;
43 __DRIcontext *driContext;
44 __GLXscreenConfigs *psc;
45 };
46
47 struct __GLXDRIdrawablePrivateRec
48 {
49 __GLXDRIdrawable base;
50
51 GC gc;
52 GC swapgc;
53
54 XVisualInfo *visinfo;
55 XImage *ximage;
56 };
57
58 static Bool
59 XCreateDrawable(__GLXDRIdrawablePrivate * pdp,
60 Display * dpy, XID drawable, int visualid)
61 {
62 XGCValues gcvalues;
63 long visMask;
64 XVisualInfo visTemp;
65 int num_visuals;
66
67 /* create GC's */
68 pdp->gc = XCreateGC(dpy, drawable, 0, NULL);
69 pdp->swapgc = XCreateGC(dpy, drawable, 0, NULL);
70
71 gcvalues.function = GXcopy;
72 gcvalues.graphics_exposures = False;
73 XChangeGC(dpy, pdp->gc, GCFunction, &gcvalues);
74 XChangeGC(dpy, pdp->swapgc, GCFunction, &gcvalues);
75 XChangeGC(dpy, pdp->swapgc, GCGraphicsExposures, &gcvalues);
76
77 /* visual */
78 visTemp.screen = DefaultScreen(dpy);
79 visTemp.visualid = visualid;
80 visMask = (VisualScreenMask | VisualIDMask);
81 pdp->visinfo = XGetVisualInfo(dpy, visMask, &visTemp, &num_visuals);
82
83 /* create XImage */
84 pdp->ximage = XCreateImage(dpy,
85 pdp->visinfo->visual,
86 pdp->visinfo->depth,
87 ZPixmap, 0, /* format, offset */
88 NULL, /* data */
89 0, 0, /* width, height */
90 32, /* bitmap_pad */
91 0); /* bytes_per_line */
92
93 return True;
94 }
95
96 static void
97 XDestroyDrawable(__GLXDRIdrawablePrivate * pdp, Display * dpy, XID drawable)
98 {
99 XDestroyImage(pdp->ximage);
100 XFree(pdp->visinfo);
101
102 XFreeGC(dpy, pdp->gc);
103 XFreeGC(dpy, pdp->swapgc);
104 }
105
106 /**
107 * swrast loader functions
108 */
109
110 static void
111 swrastGetDrawableInfo(__DRIdrawable * draw,
112 int *x, int *y, int *w, int *h,
113 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 uw, uh, bw, depth;
123
124 drawable = pdraw->xDrawable;
125
126 stat = XGetGeometry(dpy, drawable, &root,
127 x, y, &uw, &uh, &bw, &depth);
128 *w = uw;
129 *h = uh;
130 }
131
132 /**
133 * Align renderbuffer pitch.
134 *
135 * This should be chosen by the driver and the loader (libGL, xserver/glx)
136 * should use the driver provided pitch.
137 *
138 * It seems that the xorg loader (that is the xserver loading swrast_dri for
139 * indirect rendering, not client-side libGL) requires that the pitch is
140 * exactly the image width padded to 32 bits. XXX
141 *
142 * The above restriction can probably be overcome by using ScratchPixmap and
143 * CopyArea in the xserver, similar to ShmPutImage, and setting the width of
144 * the scratch pixmap to 'pitch / cpp'.
145 */
146 static inline int
147 bytes_per_line(unsigned pitch_bits, unsigned mul)
148 {
149 unsigned mask = mul - 1;
150
151 return ((pitch_bits + mask) & ~mask) / 8;
152 }
153
154 static void
155 swrastPutImage(__DRIdrawable * draw, int op,
156 int x, int y, int w, int h,
157 char *data, void *loaderPrivate)
158 {
159 __GLXDRIdrawablePrivate *pdp = loaderPrivate;
160 __GLXDRIdrawable *pdraw = &(pdp->base);
161 Display *dpy = pdraw->psc->dpy;
162 Drawable drawable;
163 XImage *ximage;
164 GC gc;
165
166 switch (op) {
167 case __DRI_SWRAST_IMAGE_OP_DRAW:
168 gc = pdp->gc;
169 break;
170 case __DRI_SWRAST_IMAGE_OP_SWAP:
171 gc = pdp->swapgc;
172 break;
173 default:
174 return;
175 }
176
177 drawable = pdraw->xDrawable;
178
179 ximage = pdp->ximage;
180 ximage->data = data;
181 ximage->width = w;
182 ximage->height = h;
183 ximage->bytes_per_line = bytes_per_line(w * ximage->bits_per_pixel, 32);
184
185 XPutImage(dpy, drawable, gc, ximage, 0, 0, x, y, w, h);
186
187 ximage->data = NULL;
188 }
189
190 static void
191 swrastGetImage(__DRIdrawable * read,
192 int x, int y, int w, int h,
193 char *data, void *loaderPrivate)
194 {
195 __GLXDRIdrawablePrivate *prp = loaderPrivate;
196 __GLXDRIdrawable *pread = &(prp->base);
197 Display *dpy = pread->psc->dpy;
198 Drawable readable;
199 XImage *ximage;
200
201 readable = pread->xDrawable;
202
203 ximage = prp->ximage;
204 ximage->data = data;
205 ximage->width = w;
206 ximage->height = h;
207 ximage->bytes_per_line = bytes_per_line(w * ximage->bits_per_pixel, 32);
208
209 XGetSubImage(dpy, readable, x, y, w, h, ~0L, ZPixmap, ximage, 0, 0);
210
211 ximage->data = NULL;
212 }
213
214 static const __DRIswrastLoaderExtension swrastLoaderExtension = {
215 {__DRI_SWRAST_LOADER, __DRI_SWRAST_LOADER_VERSION},
216 swrastGetDrawableInfo,
217 swrastPutImage,
218 swrastGetImage
219 };
220
221 static const __DRIextension *loader_extensions[] = {
222 &systemTimeExtension.base,
223 &swrastLoaderExtension.base,
224 NULL
225 };
226
227 /**
228 * GLXDRI functions
229 */
230
231 static void
232 driDestroyContext(__GLXDRIcontext * context,
233 __GLXscreenConfigs * psc, Display * dpy)
234 {
235 __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
236 const __DRIcoreExtension *core = pcp->psc->core;
237
238 (*core->destroyContext) (pcp->driContext);
239
240 Xfree(pcp);
241 }
242
243 static Bool
244 driBindContext(__GLXDRIcontext * context,
245 __GLXDRIdrawable * draw, __GLXDRIdrawable * read)
246 {
247 __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
248 const __DRIcoreExtension *core = pcp->psc->core;
249
250 return (*core->bindContext) (pcp->driContext,
251 draw->driDrawable, read->driDrawable);
252 }
253
254 static void
255 driUnbindContext(__GLXDRIcontext * context)
256 {
257 __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
258 const __DRIcoreExtension *core = pcp->psc->core;
259
260 (*core->unbindContext) (pcp->driContext);
261 }
262
263 static __GLXDRIcontext *
264 driCreateContext(__GLXscreenConfigs * psc,
265 const __GLcontextModes * mode,
266 GLXContext gc, GLXContext shareList, int renderType)
267 {
268 __GLXDRIcontextPrivate *pcp, *pcp_shared;
269 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) mode;
270 const __DRIcoreExtension *core;
271 __DRIcontext *shared = NULL;
272
273 if (!psc || !psc->driScreen)
274 return NULL;
275
276 core = psc->core;
277
278 if (shareList) {
279 pcp_shared = (__GLXDRIcontextPrivate *) shareList->driContext;
280 shared = pcp_shared->driContext;
281 }
282
283 pcp = Xmalloc(sizeof *pcp);
284 if (pcp == NULL)
285 return NULL;
286
287 pcp->psc = psc;
288 pcp->driContext =
289 (*core->createNewContext) (psc->__driScreen,
290 config->driConfig, shared, pcp);
291 if (pcp->driContext == NULL) {
292 Xfree(pcp);
293 return NULL;
294 }
295
296 pcp->base.destroyContext = driDestroyContext;
297 pcp->base.bindContext = driBindContext;
298 pcp->base.unbindContext = driUnbindContext;
299
300 return &pcp->base;
301 }
302
303 static void
304 driDestroyDrawable(__GLXDRIdrawable * pdraw)
305 {
306 __GLXDRIdrawablePrivate *pdp = (__GLXDRIdrawablePrivate *) pdraw;
307 const __DRIcoreExtension *core = pdraw->psc->core;
308
309 (*core->destroyDrawable) (pdraw->driDrawable);
310
311 XDestroyDrawable(pdp, pdraw->psc->dpy, pdraw->drawable);
312 Xfree(pdp);
313 }
314
315 static __GLXDRIdrawable *
316 driCreateDrawable(__GLXscreenConfigs * psc,
317 XID xDrawable,
318 GLXDrawable drawable, const __GLcontextModes * modes)
319 {
320 __GLXDRIdrawable *pdraw;
321 __GLXDRIdrawablePrivate *pdp;
322 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) modes;
323 const __DRIswrastExtension *swrast = psc->swrast;
324
325 /* Old dri can't handle GLX 1.3+ drawable constructors. */
326 if (xDrawable != drawable)
327 return NULL;
328
329 pdp = Xmalloc(sizeof(*pdp));
330 if (!pdp)
331 return NULL;
332
333 pdraw = &(pdp->base);
334 pdraw->xDrawable = xDrawable;
335 pdraw->drawable = drawable;
336 pdraw->psc = psc;
337
338 XCreateDrawable(pdp, psc->dpy, xDrawable, modes->visualID);
339
340 /* Create a new drawable */
341 pdraw->driDrawable =
342 (*swrast->createNewDrawable) (psc->__driScreen, config->driConfig, pdp);
343
344 if (!pdraw->driDrawable) {
345 XDestroyDrawable(pdp, psc->dpy, xDrawable);
346 Xfree(pdp);
347 return NULL;
348 }
349
350 pdraw->destroyDrawable = driDestroyDrawable;
351
352 return pdraw;
353 }
354
355 static int64_t
356 driSwapBuffers(__GLXDRIdrawable * pdraw,
357 int64_t target_msc, int64_t divisor, int64_t remainder)
358 {
359 (void) target_msc;
360 (void) divisor;
361 (void) remainder;
362
363 (*pdraw->psc->core->swapBuffers) (pdraw->driDrawable);
364
365 return 0;
366 }
367
368 static void
369 driDestroyScreen(__GLXscreenConfigs * psc)
370 {
371 /* Free the direct rendering per screen data */
372 (*psc->core->destroyScreen) (psc->__driScreen);
373 psc->__driScreen = NULL;
374 if (psc->driver)
375 dlclose(psc->driver);
376 }
377
378 static void *
379 driOpenSwrast(void)
380 {
381 void *driver = NULL;
382
383 if (driver == NULL)
384 driver = driOpenDriver("swrast");
385
386 if (driver == NULL)
387 driver = driOpenDriver("swrastg");
388
389 return driver;
390 }
391
392 static __GLXDRIscreen *
393 driCreateScreen(__GLXscreenConfigs * psc, int screen,
394 __GLXdisplayPrivate * priv)
395 {
396 __GLXDRIscreen *psp;
397 const __DRIconfig **driver_configs;
398 const __DRIextension **extensions;
399 int i;
400
401 psp = Xcalloc(1, sizeof *psp);
402 if (psp == NULL)
403 return NULL;
404
405 psc->driver = driOpenSwrast();
406 if (psc->driver == NULL)
407 goto handle_error;
408
409 extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
410 if (extensions == NULL) {
411 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
412 goto handle_error;
413 }
414
415 for (i = 0; extensions[i]; i++) {
416 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
417 psc->core = (__DRIcoreExtension *) extensions[i];
418 if (strcmp(extensions[i]->name, __DRI_SWRAST) == 0)
419 psc->swrast = (__DRIswrastExtension *) extensions[i];
420 }
421
422 if (psc->core == NULL || psc->swrast == NULL) {
423 ErrorMessageF("core dri extension not found\n");
424 goto handle_error;
425 }
426
427 psc->__driScreen =
428 psc->swrast->createNewScreen(screen,
429 loader_extensions, &driver_configs, psc);
430 if (psc->__driScreen == NULL) {
431 ErrorMessageF("failed to create dri screen\n");
432 goto handle_error;
433 }
434
435 driBindExtensions(psc);
436 driBindCommonExtensions(psc);
437
438 psc->configs = driConvertConfigs(psc->core, psc->configs, driver_configs);
439 psc->visuals = driConvertConfigs(psc->core, psc->visuals, driver_configs);
440
441 psc->driver_configs = driver_configs;
442
443 psp->destroyScreen = driDestroyScreen;
444 psp->createContext = driCreateContext;
445 psp->createDrawable = driCreateDrawable;
446 psp->swapBuffers = driSwapBuffers;
447 psp->waitX = NULL;
448 psp->waitGL = NULL;
449
450 return psp;
451
452 handle_error:
453 Xfree(psp);
454
455 if (psc->driver)
456 dlclose(psc->driver);
457
458 ErrorMessageF("reverting to indirect rendering\n");
459
460 return NULL;
461 }
462
463 /* Called from __glXFreeDisplayPrivate.
464 */
465 static void
466 driDestroyDisplay(__GLXDRIdisplay * dpy)
467 {
468 Xfree(dpy);
469 }
470
471 /*
472 * Allocate, initialize and return a __DRIdisplayPrivate object.
473 * This is called from __glXInitialize() when we are given a new
474 * display pointer.
475 */
476 _X_HIDDEN __GLXDRIdisplay *
477 driswCreateDisplay(Display * dpy)
478 {
479 __GLXDRIdisplayPrivate *pdpyp;
480
481 pdpyp = Xmalloc(sizeof *pdpyp);
482 if (pdpyp == NULL)
483 return NULL;
484
485 pdpyp->base.destroyDisplay = driDestroyDisplay;
486 pdpyp->base.createScreen = driCreateScreen;
487
488 return &pdpyp->base;
489 }
490
491 #endif /* GLX_DIRECT_RENDERING */