Squashed commit of the following:
[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 struct glx_context base;
39 __DRIcontext *driContext;
40
41 };
42
43 struct drisw_screen
44 {
45 struct glx_screen base;
46
47 __DRIscreen *driScreen;
48 __GLXDRIscreen vtable;
49 const __DRIcoreExtension *core;
50 const __DRIswrastExtension *swrast;
51 const __DRIconfig **driver_configs;
52
53 void *driver;
54 };
55
56 struct drisw_drawable
57 {
58 __GLXDRIdrawable base;
59
60 GC gc;
61 GC swapgc;
62
63 __DRIdrawable *driDrawable;
64 XVisualInfo *visinfo;
65 XImage *ximage;
66 };
67
68 static Bool
69 XCreateDrawable(struct drisw_drawable * pdp,
70 Display * dpy, XID drawable, int visualid)
71 {
72 XGCValues gcvalues;
73 long visMask;
74 XVisualInfo visTemp;
75 int num_visuals;
76
77 /* create GC's */
78 pdp->gc = XCreateGC(dpy, drawable, 0, NULL);
79 pdp->swapgc = XCreateGC(dpy, drawable, 0, NULL);
80
81 gcvalues.function = GXcopy;
82 gcvalues.graphics_exposures = False;
83 XChangeGC(dpy, pdp->gc, GCFunction, &gcvalues);
84 XChangeGC(dpy, pdp->swapgc, GCFunction, &gcvalues);
85 XChangeGC(dpy, pdp->swapgc, GCGraphicsExposures, &gcvalues);
86
87 /* visual */
88 visTemp.screen = DefaultScreen(dpy);
89 visTemp.visualid = visualid;
90 visMask = (VisualScreenMask | VisualIDMask);
91 pdp->visinfo = XGetVisualInfo(dpy, visMask, &visTemp, &num_visuals);
92
93 /* create XImage */
94 pdp->ximage = XCreateImage(dpy,
95 pdp->visinfo->visual,
96 pdp->visinfo->depth,
97 ZPixmap, 0, /* format, offset */
98 NULL, /* data */
99 0, 0, /* width, height */
100 32, /* bitmap_pad */
101 0); /* bytes_per_line */
102
103 return True;
104 }
105
106 static void
107 XDestroyDrawable(struct drisw_drawable * pdp, Display * dpy, XID drawable)
108 {
109 XDestroyImage(pdp->ximage);
110 XFree(pdp->visinfo);
111
112 XFreeGC(dpy, pdp->gc);
113 XFreeGC(dpy, pdp->swapgc);
114 }
115
116 /**
117 * swrast loader functions
118 */
119
120 static void
121 swrastGetDrawableInfo(__DRIdrawable * draw,
122 int *x, int *y, int *w, int *h,
123 void *loaderPrivate)
124 {
125 struct drisw_drawable *pdp = loaderPrivate;
126 __GLXDRIdrawable *pdraw = &(pdp->base);
127 Display *dpy = pdraw->psc->dpy;
128 Drawable drawable;
129
130 Window root;
131 unsigned uw, uh, bw, depth;
132
133 drawable = pdraw->xDrawable;
134
135 XGetGeometry(dpy, drawable, &root, 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 drisw_destroy_context(struct glx_context *context)
241 {
242 struct drisw_context *pcp = (struct drisw_context *) context;
243 struct drisw_screen *psc = (struct drisw_screen *) context->psc;
244
245 driReleaseDrawables(&pcp->base);
246
247 if (context->xid)
248 glx_send_destroy_context(psc->base.dpy, context->xid);
249
250 if (context->extensions)
251 XFree((char *) context->extensions);
252
253 (*psc->core->destroyContext) (pcp->driContext);
254
255 Xfree(pcp);
256 }
257
258 static int
259 drisw_bind_context(struct glx_context *context, struct glx_context *old,
260 GLXDrawable draw, GLXDrawable read)
261 {
262 struct drisw_context *pcp = (struct drisw_context *) context;
263 struct drisw_screen *psc = (struct drisw_screen *) pcp->base.psc;
264 struct drisw_drawable *pdraw, *pread;
265
266 pdraw = (struct drisw_drawable *) driFetchDrawable(context, draw);
267 pread = (struct drisw_drawable *) driFetchDrawable(context, read);
268
269 driReleaseDrawables(&pcp->base);
270
271 if (pdraw == NULL || pread == NULL)
272 return GLXBadDrawable;
273
274 if ((*psc->core->bindContext) (pcp->driContext,
275 pdraw->driDrawable, pread->driDrawable))
276 return Success;
277
278 return GLXBadContext;
279 }
280
281 static void
282 drisw_unbind_context(struct glx_context *context, struct glx_context *new)
283 {
284 struct drisw_context *pcp = (struct drisw_context *) context;
285 struct drisw_screen *psc = (struct drisw_screen *) pcp->base.psc;
286
287 (*psc->core->unbindContext) (pcp->driContext);
288 }
289
290 static const struct glx_context_vtable drisw_context_vtable = {
291 drisw_destroy_context,
292 drisw_bind_context,
293 drisw_unbind_context,
294 NULL,
295 NULL,
296 DRI_glXUseXFont,
297 NULL,
298 NULL,
299 NULL, /* get_proc_address */
300 };
301
302 static struct glx_context *
303 drisw_create_context(struct glx_screen *base,
304 struct glx_config *config_base,
305 struct glx_context *shareList, int renderType)
306 {
307 struct drisw_context *pcp, *pcp_shared;
308 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
309 struct drisw_screen *psc = (struct drisw_screen *) base;
310 __DRIcontext *shared = NULL;
311
312 if (!psc->base.driScreen)
313 return NULL;
314
315 if (shareList) {
316 pcp_shared = (struct drisw_context *) shareList;
317 shared = pcp_shared->driContext;
318 }
319
320 pcp = Xmalloc(sizeof *pcp);
321 if (pcp == NULL)
322 return NULL;
323
324 memset(pcp, 0, sizeof *pcp);
325 if (!glx_context_init(&pcp->base, &psc->base, &config->base)) {
326 Xfree(pcp);
327 return NULL;
328 }
329
330 pcp->driContext =
331 (*psc->core->createNewContext) (psc->driScreen,
332 config->driConfig, shared, pcp);
333 if (pcp->driContext == NULL) {
334 Xfree(pcp);
335 return NULL;
336 }
337
338 pcp->base.vtable = &drisw_context_vtable;
339
340 return &pcp->base;
341 }
342
343 static void
344 driswDestroyDrawable(__GLXDRIdrawable * pdraw)
345 {
346 struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
347 struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
348
349 (*psc->core->destroyDrawable) (pdp->driDrawable);
350
351 XDestroyDrawable(pdp, pdraw->psc->dpy, pdraw->drawable);
352 Xfree(pdp);
353 }
354
355 static __GLXDRIdrawable *
356 driswCreateDrawable(struct glx_screen *base, XID xDrawable,
357 GLXDrawable drawable, struct glx_config *modes)
358 {
359 struct drisw_drawable *pdp;
360 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) modes;
361 struct drisw_screen *psc = (struct drisw_screen *) base;
362
363 const __DRIswrastExtension *swrast = psc->swrast;
364
365 pdp = Xmalloc(sizeof(*pdp));
366 if (!pdp)
367 return NULL;
368
369 memset(pdp, 0, sizeof *pdp);
370 pdp->base.xDrawable = xDrawable;
371 pdp->base.drawable = drawable;
372 pdp->base.psc = &psc->base;
373
374 XCreateDrawable(pdp, psc->base.dpy, xDrawable, modes->visualID);
375
376 /* Create a new drawable */
377 pdp->driDrawable =
378 (*swrast->createNewDrawable) (psc->driScreen, config->driConfig, pdp);
379
380 if (!pdp->driDrawable) {
381 XDestroyDrawable(pdp, psc->base.dpy, xDrawable);
382 Xfree(pdp);
383 return NULL;
384 }
385
386 pdp->base.destroyDrawable = driswDestroyDrawable;
387
388 return &pdp->base;
389 }
390
391 static int64_t
392 driswSwapBuffers(__GLXDRIdrawable * pdraw,
393 int64_t target_msc, int64_t divisor, int64_t remainder)
394 {
395 struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
396 struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
397
398 (void) target_msc;
399 (void) divisor;
400 (void) remainder;
401
402 (*psc->core->swapBuffers) (pdp->driDrawable);
403
404 return 0;
405 }
406
407 static void
408 driswDestroyScreen(struct glx_screen *base)
409 {
410 struct drisw_screen *psc = (struct drisw_screen *) base;
411
412 /* Free the direct rendering per screen data */
413 (*psc->core->destroyScreen) (psc->driScreen);
414 driDestroyConfigs(psc->driver_configs);
415 psc->driScreen = NULL;
416 if (psc->driver)
417 dlclose(psc->driver);
418 }
419
420 static void *
421 driOpenSwrast(void)
422 {
423 void *driver = NULL;
424
425 if (driver == NULL)
426 driver = driOpenDriver("swrast");
427
428 return driver;
429 }
430
431 static const struct glx_screen_vtable drisw_screen_vtable = {
432 drisw_create_context
433 };
434
435 static struct glx_screen *
436 driswCreateScreen(int screen, struct glx_display *priv)
437 {
438 __GLXDRIscreen *psp;
439 const __DRIconfig **driver_configs;
440 const __DRIextension **extensions;
441 struct drisw_screen *psc;
442 int i;
443
444 psc = Xcalloc(1, sizeof *psc);
445 if (psc == NULL)
446 return NULL;
447
448 memset(psc, 0, sizeof *psc);
449 if (!glx_screen_init(&psc->base, screen, priv)) {
450 Xfree(psc);
451 return NULL;
452 }
453
454 psc->driver = driOpenSwrast();
455 if (psc->driver == NULL)
456 goto handle_error;
457
458 extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
459 if (extensions == NULL) {
460 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
461 goto handle_error;
462 }
463
464 for (i = 0; extensions[i]; i++) {
465 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
466 psc->core = (__DRIcoreExtension *) extensions[i];
467 if (strcmp(extensions[i]->name, __DRI_SWRAST) == 0)
468 psc->swrast = (__DRIswrastExtension *) extensions[i];
469 }
470
471 if (psc->core == NULL || psc->swrast == NULL) {
472 ErrorMessageF("core dri extension not found\n");
473 goto handle_error;
474 }
475
476 psc->driScreen =
477 psc->swrast->createNewScreen(screen, loader_extensions,
478 &driver_configs, psc);
479 if (psc->driScreen == NULL) {
480 ErrorMessageF("failed to create dri screen\n");
481 goto handle_error;
482 }
483
484 psc->base.configs =
485 driConvertConfigs(psc->core, psc->base.configs, driver_configs);
486 psc->base.visuals =
487 driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
488
489 psc->driver_configs = driver_configs;
490
491 psc->base.vtable = &drisw_screen_vtable;
492 psp = &psc->vtable;
493 psc->base.driScreen = psp;
494 psp->destroyScreen = driswDestroyScreen;
495 psp->createDrawable = driswCreateDrawable;
496 psp->swapBuffers = driswSwapBuffers;
497
498 return &psc->base;
499
500 handle_error:
501 if (psc->driver)
502 dlclose(psc->driver);
503 glx_screen_cleanup(&psc->base);
504 Xfree(psc);
505
506 ErrorMessageF("reverting to indirect rendering\n");
507
508 return NULL;
509 }
510
511 /* Called from __glXFreeDisplayPrivate.
512 */
513 static void
514 driswDestroyDisplay(__GLXDRIdisplay * dpy)
515 {
516 Xfree(dpy);
517 }
518
519 /*
520 * Allocate, initialize and return a __DRIdisplayPrivate object.
521 * This is called from __glXInitialize() when we are given a new
522 * display pointer.
523 */
524 _X_HIDDEN __GLXDRIdisplay *
525 driswCreateDisplay(Display * dpy)
526 {
527 struct drisw_display *pdpyp;
528
529 pdpyp = Xmalloc(sizeof *pdpyp);
530 if (pdpyp == NULL)
531 return NULL;
532
533 pdpyp->base.destroyDisplay = driswDestroyDisplay;
534 pdpyp->base.createScreen = driswCreateScreen;
535
536 return &pdpyp->base;
537 }
538
539 #endif /* GLX_DIRECT_RENDERING */