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