glx: Move __driScreen into the dri screen 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 XVisualInfo *visinfo;
62 XImage *ximage;
63 };
64
65 static Bool
66 XCreateDrawable(struct drisw_drawable * pdp,
67 Display * dpy, XID drawable, int visualid)
68 {
69 XGCValues gcvalues;
70 long visMask;
71 XVisualInfo visTemp;
72 int num_visuals;
73
74 /* create GC's */
75 pdp->gc = XCreateGC(dpy, drawable, 0, NULL);
76 pdp->swapgc = XCreateGC(dpy, drawable, 0, NULL);
77
78 gcvalues.function = GXcopy;
79 gcvalues.graphics_exposures = False;
80 XChangeGC(dpy, pdp->gc, GCFunction, &gcvalues);
81 XChangeGC(dpy, pdp->swapgc, GCFunction, &gcvalues);
82 XChangeGC(dpy, pdp->swapgc, GCGraphicsExposures, &gcvalues);
83
84 /* visual */
85 visTemp.screen = DefaultScreen(dpy);
86 visTemp.visualid = visualid;
87 visMask = (VisualScreenMask | VisualIDMask);
88 pdp->visinfo = XGetVisualInfo(dpy, visMask, &visTemp, &num_visuals);
89
90 /* create XImage */
91 pdp->ximage = XCreateImage(dpy,
92 pdp->visinfo->visual,
93 pdp->visinfo->depth,
94 ZPixmap, 0, /* format, offset */
95 NULL, /* data */
96 0, 0, /* width, height */
97 32, /* bitmap_pad */
98 0); /* bytes_per_line */
99
100 return True;
101 }
102
103 static void
104 XDestroyDrawable(struct drisw_drawable * pdp, Display * dpy, XID drawable)
105 {
106 XDestroyImage(pdp->ximage);
107 XFree(pdp->visinfo);
108
109 XFreeGC(dpy, pdp->gc);
110 XFreeGC(dpy, pdp->swapgc);
111 }
112
113 /**
114 * swrast loader functions
115 */
116
117 static void
118 swrastGetDrawableInfo(__DRIdrawable * draw,
119 int *x, int *y, int *w, int *h,
120 void *loaderPrivate)
121 {
122 struct drisw_drawable *pdp = loaderPrivate;
123 __GLXDRIdrawable *pdraw = &(pdp->base);
124 Display *dpy = pdraw->psc->dpy;
125 Drawable drawable;
126
127 Window root;
128 Status stat;
129 unsigned uw, uh, bw, depth;
130
131 drawable = pdraw->xDrawable;
132
133 stat = XGetGeometry(dpy, drawable, &root,
134 x, y, &uw, &uh, &bw, &depth);
135 *w = uw;
136 *h = uh;
137 }
138
139 /**
140 * Align renderbuffer pitch.
141 *
142 * This should be chosen by the driver and the loader (libGL, xserver/glx)
143 * should use the driver provided pitch.
144 *
145 * It seems that the xorg loader (that is the xserver loading swrast_dri for
146 * indirect rendering, not client-side libGL) requires that the pitch is
147 * exactly the image width padded to 32 bits. XXX
148 *
149 * The above restriction can probably be overcome by using ScratchPixmap and
150 * CopyArea in the xserver, similar to ShmPutImage, and setting the width of
151 * the scratch pixmap to 'pitch / cpp'.
152 */
153 static inline int
154 bytes_per_line(unsigned pitch_bits, unsigned mul)
155 {
156 unsigned mask = mul - 1;
157
158 return ((pitch_bits + mask) & ~mask) / 8;
159 }
160
161 static void
162 swrastPutImage(__DRIdrawable * draw, int op,
163 int x, int y, int w, int h,
164 char *data, void *loaderPrivate)
165 {
166 struct drisw_drawable *pdp = loaderPrivate;
167 __GLXDRIdrawable *pdraw = &(pdp->base);
168 Display *dpy = pdraw->psc->dpy;
169 Drawable drawable;
170 XImage *ximage;
171 GC gc;
172
173 switch (op) {
174 case __DRI_SWRAST_IMAGE_OP_DRAW:
175 gc = pdp->gc;
176 break;
177 case __DRI_SWRAST_IMAGE_OP_SWAP:
178 gc = pdp->swapgc;
179 break;
180 default:
181 return;
182 }
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 * ximage->bits_per_pixel, 32);
191
192 XPutImage(dpy, drawable, gc, ximage, 0, 0, x, y, w, h);
193
194 ximage->data = NULL;
195 }
196
197 static void
198 swrastGetImage(__DRIdrawable * read,
199 int x, int y, int w, int h,
200 char *data, void *loaderPrivate)
201 {
202 struct drisw_drawable *prp = loaderPrivate;
203 __GLXDRIdrawable *pread = &(prp->base);
204 Display *dpy = pread->psc->dpy;
205 Drawable readable;
206 XImage *ximage;
207
208 readable = pread->xDrawable;
209
210 ximage = prp->ximage;
211 ximage->data = data;
212 ximage->width = w;
213 ximage->height = h;
214 ximage->bytes_per_line = bytes_per_line(w * ximage->bits_per_pixel, 32);
215
216 XGetSubImage(dpy, readable, x, y, w, h, ~0L, ZPixmap, ximage, 0, 0);
217
218 ximage->data = NULL;
219 }
220
221 static const __DRIswrastLoaderExtension swrastLoaderExtension = {
222 {__DRI_SWRAST_LOADER, __DRI_SWRAST_LOADER_VERSION},
223 swrastGetDrawableInfo,
224 swrastPutImage,
225 swrastGetImage
226 };
227
228 static const __DRIextension *loader_extensions[] = {
229 &systemTimeExtension.base,
230 &swrastLoaderExtension.base,
231 NULL
232 };
233
234 /**
235 * GLXDRI functions
236 */
237
238 static void
239 driDestroyContext(__GLXDRIcontext *context,
240 __GLXscreenConfigs *base, Display *dpy)
241 {
242 struct drisw_context *pcp = (struct drisw_context *) context;
243 struct drisw_screen *psc = (struct drisw_screen *) base;
244
245 (*psc->core->destroyContext) (pcp->driContext);
246
247 Xfree(pcp);
248 }
249
250 static Bool
251 driBindContext(__GLXDRIcontext * context,
252 __GLXDRIdrawable * draw, __GLXDRIdrawable * read)
253 {
254 struct drisw_context *pcp = (struct drisw_context *) context;
255 struct drisw_screen *psc = (struct drisw_screen *) pcp->psc;
256
257 return (*psc->core->bindContext) (pcp->driContext,
258 draw->driDrawable, read->driDrawable);
259 }
260
261 static void
262 driUnbindContext(__GLXDRIcontext * context)
263 {
264 struct drisw_context *pcp = (struct drisw_context *) context;
265 struct drisw_screen *psc = (struct drisw_screen *) pcp->psc;
266
267 (*psc->core->unbindContext) (pcp->driContext);
268 }
269
270 static __GLXDRIcontext *
271 driCreateContext(__GLXscreenConfigs *base,
272 const __GLcontextModes *mode,
273 GLXContext gc, GLXContext shareList, int renderType)
274 {
275 struct drisw_context *pcp, *pcp_shared;
276 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) mode;
277 struct drisw_screen *psc = (struct drisw_screen *) base;
278 __DRIcontext *shared = NULL;
279
280 if (!psc->base.driScreen)
281 return NULL;
282
283 if (shareList) {
284 pcp_shared = (struct drisw_context *) shareList->driContext;
285 shared = pcp_shared->driContext;
286 }
287
288 pcp = Xmalloc(sizeof *pcp);
289 if (pcp == NULL)
290 return NULL;
291
292 pcp->psc = &psc->base;
293 pcp->driContext =
294 (*psc->core->createNewContext) (psc->driScreen,
295 config->driConfig, shared, pcp);
296 if (pcp->driContext == NULL) {
297 Xfree(pcp);
298 return NULL;
299 }
300
301 pcp->base.destroyContext = driDestroyContext;
302 pcp->base.bindContext = driBindContext;
303 pcp->base.unbindContext = driUnbindContext;
304
305 return &pcp->base;
306 }
307
308 static void
309 driDestroyDrawable(__GLXDRIdrawable * pdraw)
310 {
311 struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
312 struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
313
314 (*psc->core->destroyDrawable) (pdraw->driDrawable);
315
316 XDestroyDrawable(pdp, pdraw->psc->dpy, pdraw->drawable);
317 Xfree(pdp);
318 }
319
320 static __GLXDRIdrawable *
321 driCreateDrawable(__GLXscreenConfigs *base, XID xDrawable,
322 GLXDrawable drawable, const __GLcontextModes * modes)
323 {
324 __GLXDRIdrawable *pdraw;
325 struct drisw_drawable *pdp;
326 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) modes;
327 struct drisw_screen *psc = (struct drisw_screen *) base;
328
329 const __DRIswrastExtension *swrast = psc->swrast;
330
331 /* Old dri can't handle GLX 1.3+ drawable constructors. */
332 if (xDrawable != drawable)
333 return NULL;
334
335 pdp = Xmalloc(sizeof(*pdp));
336 if (!pdp)
337 return NULL;
338
339 pdraw = &(pdp->base);
340 pdraw->xDrawable = xDrawable;
341 pdraw->drawable = drawable;
342 pdraw->psc = &psc->base;
343
344 XCreateDrawable(pdp, psc->base.dpy, xDrawable, modes->visualID);
345
346 /* Create a new drawable */
347 pdraw->driDrawable =
348 (*swrast->createNewDrawable) (psc->driScreen,
349 config->driConfig, pdp);
350
351 if (!pdraw->driDrawable) {
352 XDestroyDrawable(pdp, psc->base.dpy, xDrawable);
353 Xfree(pdp);
354 return NULL;
355 }
356
357 pdraw->destroyDrawable = driDestroyDrawable;
358
359 return pdraw;
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) (pdraw->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 */