2 * Copyright 2008 George Sapountzis
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:
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
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
24 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
27 #include "glxclient.h"
29 #include "dri_common.h"
38 struct glx_context base
;
39 __DRIcontext
*driContext
;
45 struct glx_screen base
;
47 __DRIscreen
*driScreen
;
48 __GLXDRIscreen vtable
;
49 const __DRIcoreExtension
*core
;
50 const __DRIswrastExtension
*swrast
;
51 const __DRIconfig
**driver_configs
;
58 __GLXDRIdrawable base
;
63 __DRIdrawable
*driDrawable
;
69 XCreateDrawable(struct drisw_drawable
* pdp
,
70 Display
* dpy
, XID drawable
, int visualid
)
78 pdp
->gc
= XCreateGC(dpy
, drawable
, 0, NULL
);
79 pdp
->swapgc
= XCreateGC(dpy
, drawable
, 0, NULL
);
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
);
88 visTemp
.screen
= DefaultScreen(dpy
);
89 visTemp
.visualid
= visualid
;
90 visMask
= (VisualScreenMask
| VisualIDMask
);
91 pdp
->visinfo
= XGetVisualInfo(dpy
, visMask
, &visTemp
, &num_visuals
);
94 pdp
->ximage
= XCreateImage(dpy
,
97 ZPixmap
, 0, /* format, offset */
99 0, 0, /* width, height */
101 0); /* bytes_per_line */
107 XDestroyDrawable(struct drisw_drawable
* pdp
, Display
* dpy
, XID drawable
)
109 XDestroyImage(pdp
->ximage
);
112 XFreeGC(dpy
, pdp
->gc
);
113 XFreeGC(dpy
, pdp
->swapgc
);
117 * swrast loader functions
121 swrastGetDrawableInfo(__DRIdrawable
* draw
,
122 int *x
, int *y
, int *w
, int *h
,
125 struct drisw_drawable
*pdp
= loaderPrivate
;
126 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);
127 Display
*dpy
= pdraw
->psc
->dpy
;
131 unsigned uw
, uh
, bw
, depth
;
133 drawable
= pdraw
->xDrawable
;
135 XGetGeometry(dpy
, drawable
, &root
, x
, y
, &uw
, &uh
, &bw
, &depth
);
141 * Align renderbuffer pitch.
143 * This should be chosen by the driver and the loader (libGL, xserver/glx)
144 * should use the driver provided pitch.
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
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'.
155 bytes_per_line(unsigned pitch_bits
, unsigned mul
)
157 unsigned mask
= mul
- 1;
159 return ((pitch_bits
+ mask
) & ~mask
) / 8;
163 swrastPutImage(__DRIdrawable
* draw
, int op
,
164 int x
, int y
, int w
, int h
,
165 char *data
, void *loaderPrivate
)
167 struct drisw_drawable
*pdp
= loaderPrivate
;
168 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);
169 Display
*dpy
= pdraw
->psc
->dpy
;
175 case __DRI_SWRAST_IMAGE_OP_DRAW
:
178 case __DRI_SWRAST_IMAGE_OP_SWAP
:
185 drawable
= pdraw
->xDrawable
;
187 ximage
= pdp
->ximage
;
191 ximage
->bytes_per_line
= bytes_per_line(w
* ximage
->bits_per_pixel
, 32);
193 XPutImage(dpy
, drawable
, gc
, ximage
, 0, 0, x
, y
, w
, h
);
199 swrastGetImage(__DRIdrawable
* read
,
200 int x
, int y
, int w
, int h
,
201 char *data
, void *loaderPrivate
)
203 struct drisw_drawable
*prp
= loaderPrivate
;
204 __GLXDRIdrawable
*pread
= &(prp
->base
);
205 Display
*dpy
= pread
->psc
->dpy
;
209 readable
= pread
->xDrawable
;
211 ximage
= prp
->ximage
;
215 ximage
->bytes_per_line
= bytes_per_line(w
* ximage
->bits_per_pixel
, 32);
217 XGetSubImage(dpy
, readable
, x
, y
, w
, h
, ~0L, ZPixmap
, ximage
, 0, 0);
222 static const __DRIswrastLoaderExtension swrastLoaderExtension
= {
223 {__DRI_SWRAST_LOADER
, __DRI_SWRAST_LOADER_VERSION
},
224 swrastGetDrawableInfo
,
229 static const __DRIextension
*loader_extensions
[] = {
230 &systemTimeExtension
.base
,
231 &swrastLoaderExtension
.base
,
240 drisw_destroy_context(struct glx_context
*context
)
242 struct drisw_context
*pcp
= (struct drisw_context
*) context
;
243 struct drisw_screen
*psc
= (struct drisw_screen
*) context
->psc
;
246 glx_send_destroy_context(psc
->base
.dpy
, context
->xid
);
248 if (context
->extensions
)
249 XFree((char *) context
->extensions
);
251 (*psc
->core
->destroyContext
) (pcp
->driContext
);
257 drisw_bind_context(struct glx_context
*context
, struct glx_context
*old
,
258 GLXDrawable draw
, GLXDrawable read
)
260 struct drisw_context
*pcp
= (struct drisw_context
*) context
;
261 struct drisw_screen
*psc
= (struct drisw_screen
*) pcp
->base
.psc
;
262 struct drisw_drawable
*pdraw
, *pread
;
264 pdraw
= (struct drisw_drawable
*) driFetchDrawable(context
, draw
);
265 pread
= (struct drisw_drawable
*) driFetchDrawable(context
, read
);
267 if (pdraw
== NULL
|| pread
== NULL
)
268 return GLXBadDrawable
;
270 if ((*psc
->core
->bindContext
) (pcp
->driContext
,
271 pdraw
->driDrawable
, pread
->driDrawable
))
274 return GLXBadContext
;
278 drisw_unbind_context(struct glx_context
*context
, struct glx_context
*new)
280 struct drisw_context
*pcp
= (struct drisw_context
*) context
;
281 struct drisw_screen
*psc
= (struct drisw_screen
*) pcp
->base
.psc
;
283 (*psc
->core
->unbindContext
) (pcp
->driContext
);
285 driReleaseDrawables(&pcp
->base
);
288 static const struct glx_context_vtable drisw_context_vtable
= {
289 drisw_destroy_context
,
291 drisw_unbind_context
,
299 static struct glx_context
*
300 drisw_create_context(struct glx_screen
*base
,
301 struct glx_config
*config_base
,
302 struct glx_context
*shareList
, int renderType
)
304 struct drisw_context
*pcp
, *pcp_shared
;
305 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
306 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
307 __DRIcontext
*shared
= NULL
;
309 if (!psc
->base
.driScreen
)
313 pcp_shared
= (struct drisw_context
*) shareList
;
314 shared
= pcp_shared
->driContext
;
317 pcp
= Xmalloc(sizeof *pcp
);
321 memset(pcp
, 0, sizeof *pcp
);
322 if (!glx_context_init(&pcp
->base
, &psc
->base
, &config
->base
)) {
328 (*psc
->core
->createNewContext
) (psc
->driScreen
,
329 config
->driConfig
, shared
, pcp
);
330 if (pcp
->driContext
== NULL
) {
335 pcp
->base
.vtable
= &drisw_context_vtable
;
341 driswDestroyDrawable(__GLXDRIdrawable
* pdraw
)
343 struct drisw_drawable
*pdp
= (struct drisw_drawable
*) pdraw
;
344 struct drisw_screen
*psc
= (struct drisw_screen
*) pdp
->base
.psc
;
346 (*psc
->core
->destroyDrawable
) (pdp
->driDrawable
);
348 XDestroyDrawable(pdp
, pdraw
->psc
->dpy
, pdraw
->drawable
);
352 static __GLXDRIdrawable
*
353 driswCreateDrawable(struct glx_screen
*base
, XID xDrawable
,
354 GLXDrawable drawable
, struct glx_config
*modes
)
356 struct drisw_drawable
*pdp
;
357 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) modes
;
358 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
360 const __DRIswrastExtension
*swrast
= psc
->swrast
;
362 /* Old dri can't handle GLX 1.3+ drawable constructors. */
363 if (xDrawable
!= drawable
)
366 pdp
= Xmalloc(sizeof(*pdp
));
370 memset(pdp
, 0, sizeof *pdp
);
371 pdp
->base
.xDrawable
= xDrawable
;
372 pdp
->base
.drawable
= drawable
;
373 pdp
->base
.psc
= &psc
->base
;
375 XCreateDrawable(pdp
, psc
->base
.dpy
, xDrawable
, modes
->visualID
);
377 /* Create a new drawable */
379 (*swrast
->createNewDrawable
) (psc
->driScreen
, config
->driConfig
, pdp
);
381 if (!pdp
->driDrawable
) {
382 XDestroyDrawable(pdp
, psc
->base
.dpy
, xDrawable
);
387 pdp
->base
.destroyDrawable
= driswDestroyDrawable
;
393 driswSwapBuffers(__GLXDRIdrawable
* pdraw
,
394 int64_t target_msc
, int64_t divisor
, int64_t remainder
)
396 struct drisw_drawable
*pdp
= (struct drisw_drawable
*) pdraw
;
397 struct drisw_screen
*psc
= (struct drisw_screen
*) pdp
->base
.psc
;
403 (*psc
->core
->swapBuffers
) (pdp
->driDrawable
);
409 driswDestroyScreen(struct glx_screen
*base
)
411 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
413 /* Free the direct rendering per screen data */
414 (*psc
->core
->destroyScreen
) (psc
->driScreen
);
415 driDestroyConfigs(psc
->driver_configs
);
416 psc
->driScreen
= NULL
;
418 dlclose(psc
->driver
);
427 driver
= driOpenDriver("swrast");
430 driver
= driOpenDriver("swrastg");
435 static const struct glx_screen_vtable drisw_screen_vtable
= {
439 static struct glx_screen
*
440 driswCreateScreen(int screen
, struct glx_display
*priv
)
443 const __DRIconfig
**driver_configs
;
444 const __DRIextension
**extensions
;
445 struct drisw_screen
*psc
;
448 psc
= Xcalloc(1, sizeof *psc
);
452 memset(psc
, 0, sizeof *psc
);
453 if (!glx_screen_init(&psc
->base
, screen
, priv
)) {
458 psc
->driver
= driOpenSwrast();
459 if (psc
->driver
== NULL
)
462 extensions
= dlsym(psc
->driver
, __DRI_DRIVER_EXTENSIONS
);
463 if (extensions
== NULL
) {
464 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
468 for (i
= 0; extensions
[i
]; i
++) {
469 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
470 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
471 if (strcmp(extensions
[i
]->name
, __DRI_SWRAST
) == 0)
472 psc
->swrast
= (__DRIswrastExtension
*) extensions
[i
];
475 if (psc
->core
== NULL
|| psc
->swrast
== NULL
) {
476 ErrorMessageF("core dri extension not found\n");
481 psc
->swrast
->createNewScreen(screen
, loader_extensions
,
482 &driver_configs
, psc
);
483 if (psc
->driScreen
== NULL
) {
484 ErrorMessageF("failed to create dri screen\n");
489 driConvertConfigs(psc
->core
, psc
->base
.configs
, driver_configs
);
491 driConvertConfigs(psc
->core
, psc
->base
.visuals
, driver_configs
);
493 psc
->driver_configs
= driver_configs
;
495 psc
->base
.vtable
= &drisw_screen_vtable
;
497 psc
->base
.driScreen
= psp
;
498 psp
->destroyScreen
= driswDestroyScreen
;
499 psp
->createDrawable
= driswCreateDrawable
;
500 psp
->swapBuffers
= driswSwapBuffers
;
506 dlclose(psc
->driver
);
507 glx_screen_cleanup(&psc
->base
);
510 ErrorMessageF("reverting to indirect rendering\n");
515 /* Called from __glXFreeDisplayPrivate.
518 driswDestroyDisplay(__GLXDRIdisplay
* dpy
)
524 * Allocate, initialize and return a __DRIdisplayPrivate object.
525 * This is called from __glXInitialize() when we are given a new
528 _X_HIDDEN __GLXDRIdisplay
*
529 driswCreateDisplay(Display
* dpy
)
531 struct drisw_display
*pdpyp
;
533 pdpyp
= Xmalloc(sizeof *pdpyp
);
537 pdpyp
->base
.destroyDisplay
= driswDestroyDisplay
;
538 pdpyp
->base
.createScreen
= driswCreateScreen
;
543 #endif /* GLX_DIRECT_RENDERING */