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