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