82653f1625b82cb51aa7b9b34ce8dbcd71821b5e
[mesa.git] / src / glx / x11 / dri_glx.c
1 /**************************************************************************
2
3 Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
4 All Rights Reserved.
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sub license, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice (including the
15 next paragraph) shall be included in all copies or substantial portions
16 of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
22 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Kevin E. Martin <kevin@precisioninsight.com>
31 * Brian Paul <brian@precisioninsight.com>
32 *
33 */
34
35 #ifdef GLX_DIRECT_RENDERING
36
37 #include <X11/Xlib.h>
38 #include <X11/extensions/Xfixes.h>
39 #include <X11/extensions/Xdamage.h>
40 #include "glheader.h"
41 #include "glxclient.h"
42 #include "glcontextmodes.h"
43 #include "xf86dri.h"
44 #include "sarea.h"
45 #include <dlfcn.h>
46 #include <sys/types.h>
47 #include <sys/mman.h>
48 #include "xf86drm.h"
49 #include "dri_common.h"
50
51 typedef struct __GLXDRIdisplayPrivateRec __GLXDRIdisplayPrivate;
52 typedef struct __GLXDRIcontextPrivateRec __GLXDRIcontextPrivate;
53
54 struct __GLXDRIdisplayPrivateRec {
55 __GLXDRIdisplay base;
56
57 /*
58 ** XFree86-DRI version information
59 */
60 int driMajor;
61 int driMinor;
62 int driPatch;
63 };
64
65 struct __GLXDRIcontextPrivateRec {
66 __GLXDRIcontext base;
67 __DRIcontext *driContext;
68 XID hwContextID;
69 __GLXscreenConfigs *psc;
70 };
71
72 /*
73 * Given a display pointer and screen number, determine the name of
74 * the DRI driver for the screen. (I.e. "r128", "tdfx", etc).
75 * Return True for success, False for failure.
76 */
77 static Bool driGetDriverName(Display *dpy, int scrNum, char **driverName)
78 {
79 int directCapable;
80 Bool b;
81 int driverMajor, driverMinor, driverPatch;
82
83 *driverName = NULL;
84
85 if (!XF86DRIQueryDirectRenderingCapable(dpy, scrNum, &directCapable)) {
86 ErrorMessageF("XF86DRIQueryDirectRenderingCapable failed\n");
87 return False;
88 }
89 if (!directCapable) {
90 ErrorMessageF("XF86DRIQueryDirectRenderingCapable returned false\n");
91 return False;
92 }
93
94 b = XF86DRIGetClientDriverName(dpy, scrNum, &driverMajor, &driverMinor,
95 &driverPatch, driverName);
96 if (!b) {
97 ErrorMessageF("Cannot determine driver name for screen %d\n", scrNum);
98 return False;
99 }
100
101 InfoMessageF("XF86DRIGetClientDriverName: %d.%d.%d %s (screen %d)\n",
102 driverMajor, driverMinor, driverPatch, *driverName, scrNum);
103
104 return True;
105 }
106
107 /*
108 * Exported function for querying the DRI driver for a given screen.
109 *
110 * The returned char pointer points to a static array that will be
111 * overwritten by subsequent calls.
112 */
113 PUBLIC const char *glXGetScreenDriver (Display *dpy, int scrNum) {
114 static char ret[32];
115 char *driverName;
116 if (driGetDriverName(dpy, scrNum, &driverName)) {
117 int len;
118 if (!driverName)
119 return NULL;
120 len = strlen (driverName);
121 if (len >= 31)
122 return NULL;
123 memcpy (ret, driverName, len+1);
124 Xfree(driverName);
125 return ret;
126 }
127 return NULL;
128 }
129
130 /*
131 * Exported function for obtaining a driver's option list (UTF-8 encoded XML).
132 *
133 * The returned char pointer points directly into the driver. Therefore
134 * it should be treated as a constant.
135 *
136 * If the driver was not found or does not support configuration NULL is
137 * returned.
138 *
139 * Note: The driver remains opened after this function returns.
140 */
141 PUBLIC const char *glXGetDriverConfig (const char *driverName)
142 {
143 void *handle = driOpenDriver (driverName);
144 if (handle)
145 return dlsym (handle, "__driConfigOptions");
146 else
147 return NULL;
148 }
149
150 #ifdef XDAMAGE_1_1_INTERFACE
151
152 static GLboolean has_damage_post(Display *dpy)
153 {
154 static GLboolean inited = GL_FALSE;
155 static GLboolean has_damage;
156
157 if (!inited) {
158 int major, minor;
159
160 if (XDamageQueryVersion(dpy, &major, &minor) &&
161 major == 1 && minor >= 1)
162 {
163 has_damage = GL_TRUE;
164 } else {
165 has_damage = GL_FALSE;
166 }
167 inited = GL_TRUE;
168 }
169
170 return has_damage;
171 }
172
173 static void __glXReportDamage(__DRIdrawable *driDraw,
174 int x, int y,
175 drm_clip_rect_t *rects, int num_rects,
176 GLboolean front_buffer,
177 void *loaderPrivate)
178 {
179 XRectangle *xrects;
180 XserverRegion region;
181 int i;
182 int x_off, y_off;
183 __GLXDRIdrawable *glxDraw = loaderPrivate;
184 __GLXscreenConfigs *psc = glxDraw->psc;
185 Display *dpy = psc->dpy;
186 Drawable drawable;
187
188 if (!has_damage_post(dpy))
189 return;
190
191 if (front_buffer) {
192 x_off = x;
193 y_off = y;
194 drawable = RootWindow(dpy, psc->scr);
195 } else{
196 x_off = 0;
197 y_off = 0;
198 drawable = glxDraw->xDrawable;
199 }
200
201 xrects = malloc(sizeof(XRectangle) * num_rects);
202 if (xrects == NULL)
203 return;
204
205 for (i = 0; i < num_rects; i++) {
206 xrects[i].x = rects[i].x1 + x_off;
207 xrects[i].y = rects[i].y1 + y_off;
208 xrects[i].width = rects[i].x2 - rects[i].x1;
209 xrects[i].height = rects[i].y2 - rects[i].y1;
210 }
211 region = XFixesCreateRegion(dpy, xrects, num_rects);
212 free(xrects);
213 XDamageAdd(dpy, drawable, region);
214 XFixesDestroyRegion(dpy, region);
215 }
216
217 static const __DRIdamageExtension damageExtension = {
218 { __DRI_DAMAGE, __DRI_DAMAGE_VERSION },
219 __glXReportDamage,
220 };
221
222 #endif
223
224 static GLboolean
225 __glXDRIGetDrawableInfo(__DRIdrawable *drawable,
226 unsigned int *index, unsigned int *stamp,
227 int *X, int *Y, int *W, int *H,
228 int *numClipRects, drm_clip_rect_t ** pClipRects,
229 int *backX, int *backY,
230 int *numBackClipRects, drm_clip_rect_t **pBackClipRects,
231 void *loaderPrivate)
232 {
233 __GLXDRIdrawable *glxDraw = loaderPrivate;
234 __GLXscreenConfigs *psc = glxDraw->psc;
235 Display *dpy = psc->dpy;
236
237 return XF86DRIGetDrawableInfo(dpy, psc->scr, glxDraw->drawable,
238 index, stamp, X, Y, W, H,
239 numClipRects, pClipRects,
240 backX, backY,
241 numBackClipRects, pBackClipRects);
242 }
243
244 static const __DRIgetDrawableInfoExtension getDrawableInfoExtension = {
245 { __DRI_GET_DRAWABLE_INFO, __DRI_GET_DRAWABLE_INFO_VERSION },
246 __glXDRIGetDrawableInfo
247 };
248
249 static const __DRIextension *loader_extensions[] = {
250 &systemTimeExtension.base,
251 &getDrawableInfoExtension.base,
252 #ifdef XDAMAGE_1_1_INTERFACE
253 &damageExtension.base,
254 #endif
255 NULL
256 };
257
258 #ifndef GLX_USE_APPLEGL
259
260 /**
261 * Perform the required libGL-side initialization and call the client-side
262 * driver's \c __driCreateNewScreen function.
263 *
264 * \param dpy Display pointer.
265 * \param scrn Screen number on the display.
266 * \param psc DRI screen information.
267 * \param driDpy DRI display information.
268 * \param createNewScreen Pointer to the client-side driver's
269 * \c __driCreateNewScreen function.
270 * \returns A pointer to the \c __DRIscreenPrivate structure returned by
271 * the client-side driver on success, or \c NULL on failure.
272 */
273 static void *
274 CallCreateNewScreen(Display *dpy, int scrn, __GLXscreenConfigs *psc,
275 __GLXDRIdisplayPrivate * driDpy)
276 {
277 void *psp = NULL;
278 drm_handle_t hSAREA;
279 drmAddress pSAREA = MAP_FAILED;
280 char *BusID;
281 __DRIversion ddx_version;
282 __DRIversion dri_version;
283 __DRIversion drm_version;
284 __DRIframebuffer framebuffer;
285 int fd = -1;
286 int status;
287
288 drm_magic_t magic;
289 drmVersionPtr version;
290 int newlyopened;
291 char *driverName;
292 drm_handle_t hFB;
293 int junk;
294 const __DRIconfig **driver_configs;
295
296 /* DRI protocol version. */
297 dri_version.major = driDpy->driMajor;
298 dri_version.minor = driDpy->driMinor;
299 dri_version.patch = driDpy->driPatch;
300
301 framebuffer.base = MAP_FAILED;
302 framebuffer.dev_priv = NULL;
303
304 if (!XF86DRIOpenConnection(dpy, scrn, &hSAREA, &BusID)) {
305 ErrorMessageF("XF86DRIOpenConnection failed\n");
306 goto handle_error;
307 }
308
309 fd = drmOpenOnce(NULL, BusID, &newlyopened);
310
311 Xfree(BusID); /* No longer needed */
312
313 if (fd < 0) {
314 ErrorMessageF("drmOpenOnce failed (%s)\n", strerror(-fd));
315 goto handle_error;
316 }
317
318 if (drmGetMagic(fd, &magic)) {
319 ErrorMessageF("drmGetMagic failed\n");
320 goto handle_error;
321 }
322
323 version = drmGetVersion(fd);
324 if (version) {
325 drm_version.major = version->version_major;
326 drm_version.minor = version->version_minor;
327 drm_version.patch = version->version_patchlevel;
328 drmFreeVersion(version);
329 }
330 else {
331 drm_version.major = -1;
332 drm_version.minor = -1;
333 drm_version.patch = -1;
334 }
335
336 if (newlyopened && !XF86DRIAuthConnection(dpy, scrn, magic)) {
337 ErrorMessageF("XF86DRIAuthConnection failed\n");
338 goto handle_error;
339 }
340
341 /* Get device name (like "tdfx") and the ddx version numbers.
342 * We'll check the version in each DRI driver's "createNewScreen"
343 * function. */
344 if (!XF86DRIGetClientDriverName(dpy, scrn,
345 &ddx_version.major,
346 &ddx_version.minor,
347 &ddx_version.patch,
348 &driverName)) {
349 ErrorMessageF("XF86DRIGetClientDriverName failed\n");
350 goto handle_error;
351 }
352
353 Xfree(driverName); /* No longer needed. */
354
355 /*
356 * Get device-specific info. pDevPriv will point to a struct
357 * (such as DRIRADEONRec in xfree86/driver/ati/radeon_dri.h) that
358 * has information about the screen size, depth, pitch, ancilliary
359 * buffers, DRM mmap handles, etc.
360 */
361 if (!XF86DRIGetDeviceInfo(dpy, scrn, &hFB, &junk,
362 &framebuffer.size, &framebuffer.stride,
363 &framebuffer.dev_priv_size, &framebuffer.dev_priv)) {
364 ErrorMessageF("XF86DRIGetDeviceInfo failed");
365 goto handle_error;
366 }
367
368 framebuffer.width = DisplayWidth(dpy, scrn);
369 framebuffer.height = DisplayHeight(dpy, scrn);
370
371 /* Map the framebuffer region. */
372 status = drmMap(fd, hFB, framebuffer.size,
373 (drmAddressPtr)&framebuffer.base);
374 if (status != 0) {
375 ErrorMessageF("drmMap of framebuffer failed (%s)", strerror(-status));
376 goto handle_error;
377 }
378
379 /* Map the SAREA region. Further mmap regions may be setup in
380 * each DRI driver's "createNewScreen" function.
381 */
382 status = drmMap(fd, hSAREA, SAREA_MAX, &pSAREA);
383 if (status != 0) {
384 ErrorMessageF("drmMap of SAREA failed (%s)", strerror(-status));
385 goto handle_error;
386 }
387
388 psp = (*psc->legacy->createNewScreen)(scrn,
389 &ddx_version,
390 &dri_version,
391 &drm_version,
392 &framebuffer,
393 pSAREA,
394 fd,
395 loader_extensions,
396 &driver_configs,
397 psc);
398
399 if (psp == NULL) {
400 ErrorMessageF("Calling driver entry point failed");
401 goto handle_error;
402 }
403
404 psc->configs = driConvertConfigs(psc->core, psc->configs, driver_configs);
405 psc->visuals = driConvertConfigs(psc->core, psc->visuals, driver_configs);
406
407 return psp;
408
409 handle_error:
410 if (pSAREA != MAP_FAILED)
411 drmUnmap(pSAREA, SAREA_MAX);
412
413 if (framebuffer.base != MAP_FAILED)
414 drmUnmap((drmAddress)framebuffer.base, framebuffer.size);
415
416 if (framebuffer.dev_priv != NULL)
417 Xfree(framebuffer.dev_priv);
418
419 if (fd >= 0)
420 drmCloseOnce(fd);
421
422 XF86DRICloseConnection(dpy, scrn);
423
424 ErrorMessageF("reverting to software direct rendering\n");
425
426 return NULL;
427 }
428
429 #else /* !GLX_USE_APPLEGL */
430
431 static void *
432 CallCreateNewScreen(Display *dpy, int scrn, __GLXscreenConfigs *psc,
433 __GLXDRIdisplayPrivate * driDpy)
434 {
435 return NULL;
436 }
437
438 #endif /* !GLX_USE_APPLEGL */
439
440 static void driDestroyContext(__GLXDRIcontext *context,
441 __GLXscreenConfigs *psc, Display *dpy)
442 {
443 __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
444
445 (*psc->core->destroyContext)(pcp->driContext);
446
447 XF86DRIDestroyContext(psc->dpy, psc->scr, pcp->hwContextID);
448 }
449
450 static Bool driBindContext(__GLXDRIcontext *context,
451 __GLXDRIdrawable *draw, __GLXDRIdrawable *read)
452 {
453 __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
454 const __DRIcoreExtension *core = pcp->psc->core;
455
456 return (*core->bindContext)(pcp->driContext,
457 draw->driDrawable,
458 read->driDrawable);
459 }
460
461 static void driUnbindContext(__GLXDRIcontext *context)
462 {
463 __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
464 const __DRIcoreExtension *core = pcp->psc->core;
465
466 (*core->unbindContext)(pcp->driContext);
467 }
468
469 static __GLXDRIcontext *driCreateContext(__GLXscreenConfigs *psc,
470 const __GLcontextModes *mode,
471 GLXContext gc,
472 GLXContext shareList, int renderType)
473 {
474 __GLXDRIcontextPrivate *pcp, *pcp_shared;
475 drm_context_t hwContext;
476 __DRIcontext *shared = NULL;
477 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) mode;
478
479 if (!psc || !psc->driScreen)
480 return NULL;
481
482 if (shareList) {
483 pcp_shared = (__GLXDRIcontextPrivate *) shareList->driContext;
484 shared = pcp_shared->driContext;
485 }
486
487 pcp = Xmalloc(sizeof *pcp);
488 if (pcp == NULL)
489 return NULL;
490
491 pcp->psc = psc;
492 if (!XF86DRICreateContextWithConfig(psc->dpy, psc->scr,
493 mode->visualID,
494 &pcp->hwContextID, &hwContext)) {
495 Xfree(pcp);
496 return NULL;
497 }
498
499 pcp->driContext =
500 (*psc->legacy->createNewContext)(psc->__driScreen,
501 config->driConfig,
502 renderType,
503 shared,
504 hwContext,
505 pcp);
506 if (pcp->driContext == NULL) {
507 XF86DRIDestroyContext(psc->dpy, psc->scr, pcp->hwContextID);
508 Xfree(pcp);
509 return NULL;
510 }
511
512 pcp->base.destroyContext = driDestroyContext;
513 pcp->base.bindContext = driBindContext;
514 pcp->base.unbindContext = driUnbindContext;
515
516 return &pcp->base;
517 }
518
519 static void driDestroyDrawable(__GLXDRIdrawable *pdraw)
520 {
521 __GLXscreenConfigs *psc = pdraw->psc;
522
523 (*psc->core->destroyDrawable)(pdraw->driDrawable);
524 XF86DRIDestroyDrawable(psc->dpy, psc->scr, pdraw->drawable);
525 Xfree(pdraw);
526 }
527
528 static __GLXDRIdrawable *driCreateDrawable(__GLXscreenConfigs *psc,
529 XID xDrawable,
530 GLXDrawable drawable,
531 const __GLcontextModes *modes)
532 {
533 __GLXDRIdrawable *pdraw;
534 drm_drawable_t hwDrawable;
535 void *empty_attribute_list = NULL;
536 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) modes;
537
538 /* Old dri can't handle GLX 1.3+ drawable constructors. */
539 if (xDrawable != drawable)
540 return NULL;
541
542 pdraw = Xmalloc(sizeof(*pdraw));
543 if (!pdraw)
544 return NULL;
545
546 pdraw->drawable = drawable;
547 pdraw->psc = psc;
548
549 if (!XF86DRICreateDrawable(psc->dpy, psc->scr, drawable, &hwDrawable))
550 return NULL;
551
552 /* Create a new drawable */
553 pdraw->driDrawable =
554 (*psc->legacy->createNewDrawable)(psc->__driScreen,
555 config->driConfig,
556 hwDrawable,
557 GLX_WINDOW_BIT,
558 empty_attribute_list,
559 pdraw);
560
561 if (!pdraw->driDrawable) {
562 XF86DRIDestroyDrawable(psc->dpy, psc->scr, drawable);
563 Xfree(pdraw);
564 return NULL;
565 }
566
567 pdraw->destroyDrawable = driDestroyDrawable;
568
569 return pdraw;
570 }
571
572 static void driDestroyScreen(__GLXscreenConfigs *psc)
573 {
574 /* Free the direct rendering per screen data */
575 if (psc->__driScreen)
576 (*psc->core->destroyScreen)(psc->__driScreen);
577 psc->__driScreen = NULL;
578 if (psc->driver)
579 dlclose(psc->driver);
580 }
581
582 static __GLXDRIscreen *driCreateScreen(__GLXscreenConfigs *psc, int screen,
583 __GLXdisplayPrivate *priv)
584 {
585 __GLXDRIdisplayPrivate *pdp;
586 __GLXDRIscreen *psp;
587 const __DRIextension **extensions;
588 char *driverName;
589 int i;
590
591 psp = Xmalloc(sizeof *psp);
592 if (psp == NULL)
593 return NULL;
594
595 /* Initialize per screen dynamic client GLX extensions */
596 psc->ext_list_first_time = GL_TRUE;
597
598 if (!driGetDriverName(priv->dpy, screen, &driverName)) {
599 Xfree(psp);
600 return NULL;
601 }
602
603 psc->driver = driOpenDriver(driverName);
604 Xfree(driverName);
605 if (psc->driver == NULL) {
606 Xfree(psp);
607 return NULL;
608 }
609
610 extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
611 if (extensions == NULL) {
612 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
613 Xfree(psp);
614 return NULL;
615 }
616
617 for (i = 0; extensions[i]; i++) {
618 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
619 psc->core = (__DRIcoreExtension *) extensions[i];
620 if (strcmp(extensions[i]->name, __DRI_LEGACY) == 0)
621 psc->legacy = (__DRIlegacyExtension *) extensions[i];
622 }
623
624 if (psc->core == NULL || psc->legacy == NULL) {
625 Xfree(psp);
626 return NULL;
627 }
628
629 pdp = (__GLXDRIdisplayPrivate *) priv->driDisplay;
630 psc->__driScreen =
631 CallCreateNewScreen(psc->dpy, screen, psc, pdp);
632 if (psc->__driScreen == NULL) {
633 dlclose(psc->driver);
634 Xfree(psp);
635 return NULL;
636 }
637
638 driBindExtensions(psc, 0);
639
640 psp->destroyScreen = driDestroyScreen;
641 psp->createContext = driCreateContext;
642 psp->createDrawable = driCreateDrawable;
643
644 return psp;
645 }
646
647 /* Called from __glXFreeDisplayPrivate.
648 */
649 static void driDestroyDisplay(__GLXDRIdisplay *dpy)
650 {
651 Xfree(dpy);
652 }
653
654 /*
655 * Allocate, initialize and return a __DRIdisplayPrivate object.
656 * This is called from __glXInitialize() when we are given a new
657 * display pointer.
658 */
659 _X_HIDDEN __GLXDRIdisplay *driCreateDisplay(Display *dpy)
660 {
661 __GLXDRIdisplayPrivate *pdpyp;
662 int eventBase, errorBase;
663 int major, minor, patch;
664
665 if (!XF86DRIQueryExtension(dpy, &eventBase, &errorBase)) {
666 return NULL;
667 }
668
669 if (!XF86DRIQueryVersion(dpy, &major, &minor, &patch)) {
670 return NULL;
671 }
672
673 pdpyp = Xmalloc(sizeof *pdpyp);
674 if (!pdpyp) {
675 return NULL;
676 }
677
678 pdpyp->driMajor = major;
679 pdpyp->driMinor = minor;
680 pdpyp->driPatch = patch;
681
682 pdpyp->base.destroyDisplay = driDestroyDisplay;
683 pdpyp->base.createScreen = driCreateScreen;
684
685 return &pdpyp->base;
686 }
687
688 #endif /* GLX_DIRECT_RENDERING */