2 * (C) Copyright IBM Corporation 2002, 2004
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * DRI utility functions.
29 * This module acts as glue between GLX and the actual hardware driver. A DRI
30 * driver doesn't really \e have to use any of this - it's optional. But, some
31 * useful stuff is done here that otherwise would have to be duplicated in most
34 * Basically, these utility functions take care of some of the dirty details of
35 * screen initialization, context creation, context binding, DRM setup, etc.
37 * These functions are compiled into each DRI driver so libGL.so knows nothing
43 #ifndef __NOT_HAVE_DRM_H
49 #include "../glsl/glsl_parser_extras.h"
50 #include "main/mtypes.h"
51 #include "main/version.h"
52 #include "main/macros.h"
54 PUBLIC
const char __dri2ConfigOptions
[] =
56 DRI_CONF_SECTION_PERFORMANCE
57 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_1
)
61 /*****************************************************************/
62 /** \name Screen handling functions */
63 /*****************************************************************/
67 setupLoaderExtensions(__DRIscreen
*psp
,
68 const __DRIextension
**extensions
)
72 for (i
= 0; extensions
[i
]; i
++) {
73 if (strcmp(extensions
[i
]->name
, __DRI_DRI2_LOADER
) == 0)
74 psp
->dri2
.loader
= (__DRIdri2LoaderExtension
*) extensions
[i
];
75 if (strcmp(extensions
[i
]->name
, __DRI_IMAGE_LOOKUP
) == 0)
76 psp
->dri2
.image
= (__DRIimageLookupExtension
*) extensions
[i
];
77 if (strcmp(extensions
[i
]->name
, __DRI_USE_INVALIDATE
) == 0)
78 psp
->dri2
.useInvalidate
= (__DRIuseInvalidateExtension
*) extensions
[i
];
79 if (strcmp(extensions
[i
]->name
, __DRI_SWRAST_LOADER
) == 0)
80 psp
->swrast_loader
= (__DRIswrastLoaderExtension
*) extensions
[i
];
85 * This pointer determines which driver API we'll use in the case of the
86 * loader not passing us an explicit driver extensions list (that would,
87 * itself, contain a pointer to a driver API.)
89 * A driver's driDriverGetExtensions_drivername() can update this pointer to
90 * what it's returning, and a loader that is ignorant of createNewScreen2()
91 * will get the correct driver screen created, as long as no other
92 * driDriverGetExtensions() happened in between the first one and the
95 * This allows the X Server to not require the significant dri_interface.h
96 * updates for doing createNewScreen2(), which would discourage backporting of
97 * the X Server patches to support the new loader interface.
99 const struct __DriverAPIRec
*globalDriverAPI
= &driDriverAPI
;
102 * This is the first entrypoint in the driver called by the DRI driver loader
103 * after dlopen()ing it.
105 * It's used to create global state for the driver across contexts on the same
109 dri2CreateNewScreen2(int scrn
, int fd
,
110 const __DRIextension
**extensions
,
111 const __DRIextension
**driver_extensions
,
112 const __DRIconfig
***driver_configs
, void *data
)
114 static const __DRIextension
*emptyExtensionList
[] = { NULL
};
117 psp
= calloc(1, sizeof(*psp
));
121 /* By default, use the global driDriverAPI symbol (non-megadrivers). */
122 psp
->driver
= globalDriverAPI
;
124 /* If the driver exposes its vtable through its extensions list
125 * (megadrivers), use that instead.
127 if (driver_extensions
) {
128 for (int i
= 0; driver_extensions
[i
]; i
++) {
129 if (strcmp(driver_extensions
[i
]->name
, __DRI_DRIVER_VTABLE
) == 0) {
131 ((__DRIDriverVtableExtension
*)driver_extensions
[i
])->vtable
;
136 setupLoaderExtensions(psp
, extensions
);
138 #ifndef __NOT_HAVE_DRM_H
140 drmVersionPtr version
= drmGetVersion(fd
);
142 psp
->drm_version
.major
= version
->version_major
;
143 psp
->drm_version
.minor
= version
->version_minor
;
144 psp
->drm_version
.patch
= version
->version_patchlevel
;
145 drmFreeVersion(version
);
150 psp
->loaderPrivate
= data
;
152 psp
->extensions
= emptyExtensionList
;
156 *driver_configs
= psp
->driver
->InitScreen(psp
);
157 if (*driver_configs
== NULL
) {
162 int gl_version_override
= _mesa_get_gl_version_override();
163 if (gl_version_override
>= 31) {
164 psp
->max_gl_core_version
= MAX2(psp
->max_gl_core_version
,
165 gl_version_override
);
167 psp
->max_gl_compat_version
= MAX2(psp
->max_gl_compat_version
,
168 gl_version_override
);
171 psp
->api_mask
= (1 << __DRI_API_OPENGL
);
172 if (psp
->max_gl_core_version
> 0)
173 psp
->api_mask
|= (1 << __DRI_API_OPENGL_CORE
);
174 if (psp
->max_gl_es1_version
> 0)
175 psp
->api_mask
|= (1 << __DRI_API_GLES
);
176 if (psp
->max_gl_es2_version
> 0)
177 psp
->api_mask
|= (1 << __DRI_API_GLES2
);
178 if (psp
->max_gl_es2_version
>= 30)
179 psp
->api_mask
|= (1 << __DRI_API_GLES3
);
181 driParseOptionInfo(&psp
->optionInfo
, __dri2ConfigOptions
);
182 driParseConfigFiles(&psp
->optionCache
, &psp
->optionInfo
, psp
->myNum
, "dri2");
189 dri2CreateNewScreen(int scrn
, int fd
,
190 const __DRIextension
**extensions
,
191 const __DRIconfig
***driver_configs
, void *data
)
193 return dri2CreateNewScreen2(scrn
, fd
, extensions
, NULL
,
194 driver_configs
, data
);
197 /** swrast driver createNewScreen entrypoint. */
199 driSWRastCreateNewScreen(int scrn
, const __DRIextension
**extensions
,
200 const __DRIconfig
***driver_configs
, void *data
)
202 return dri2CreateNewScreen2(scrn
, -1, extensions
, NULL
,
203 driver_configs
, data
);
207 driSWRastCreateNewScreen2(int scrn
, const __DRIextension
**extensions
,
208 const __DRIextension
**driver_extensions
,
209 const __DRIconfig
***driver_configs
, void *data
)
211 return dri2CreateNewScreen2(scrn
, -1, extensions
, driver_extensions
,
212 driver_configs
, data
);
216 * Destroy the per-screen private information.
219 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
220 * drmClose(), and finally frees \p screenPrivate.
222 static void driDestroyScreen(__DRIscreen
*psp
)
225 /* No interaction with the X-server is possible at this point. This
226 * routine is called after XCloseDisplay, so there is no protocol
227 * stream open to the X-server anymore.
230 _mesa_destroy_shader_compiler();
232 psp
->driver
->DestroyScreen(psp
);
234 driDestroyOptionCache(&psp
->optionCache
);
235 driDestroyOptionInfo(&psp
->optionInfo
);
241 static const __DRIextension
**driGetExtensions(__DRIscreen
*psp
)
243 return psp
->extensions
;
250 validate_context_version(__DRIscreen
*screen
,
252 unsigned major_version
,
253 unsigned minor_version
,
254 unsigned *dri_ctx_error
)
256 unsigned req_version
= 10 * major_version
+ minor_version
;
257 unsigned max_version
= 0;
260 case API_OPENGL_COMPAT
:
261 max_version
= screen
->max_gl_compat_version
;
263 case API_OPENGL_CORE
:
264 max_version
= screen
->max_gl_core_version
;
267 max_version
= screen
->max_gl_es1_version
;
270 max_version
= screen
->max_gl_es2_version
;
277 if (max_version
== 0) {
278 *dri_ctx_error
= __DRI_CTX_ERROR_BAD_API
;
280 } else if (req_version
> max_version
) {
281 *dri_ctx_error
= __DRI_CTX_ERROR_BAD_VERSION
;
288 /*****************************************************************/
289 /** \name Context handling functions */
290 /*****************************************************************/
293 static __DRIcontext
*
294 dri2CreateContextAttribs(__DRIscreen
*screen
, int api
,
295 const __DRIconfig
*config
,
296 __DRIcontext
*shared
,
297 unsigned num_attribs
,
298 const uint32_t *attribs
,
302 __DRIcontext
*context
;
303 const struct gl_config
*modes
= (config
!= NULL
) ? &config
->modes
: NULL
;
304 void *shareCtx
= (shared
!= NULL
) ? shared
->driverPrivate
: NULL
;
306 unsigned major_version
= 1;
307 unsigned minor_version
= 0;
310 assert((num_attribs
== 0) || (attribs
!= NULL
));
312 if (!(screen
->api_mask
& (1 << api
))) {
313 *error
= __DRI_CTX_ERROR_BAD_API
;
318 case __DRI_API_OPENGL
:
319 mesa_api
= API_OPENGL_COMPAT
;
322 mesa_api
= API_OPENGLES
;
324 case __DRI_API_GLES2
:
325 case __DRI_API_GLES3
:
326 mesa_api
= API_OPENGLES2
;
328 case __DRI_API_OPENGL_CORE
:
329 mesa_api
= API_OPENGL_CORE
;
332 *error
= __DRI_CTX_ERROR_BAD_API
;
336 for (unsigned i
= 0; i
< num_attribs
; i
++) {
337 switch (attribs
[i
* 2]) {
338 case __DRI_CTX_ATTRIB_MAJOR_VERSION
:
339 major_version
= attribs
[i
* 2 + 1];
341 case __DRI_CTX_ATTRIB_MINOR_VERSION
:
342 minor_version
= attribs
[i
* 2 + 1];
344 case __DRI_CTX_ATTRIB_FLAGS
:
345 flags
= attribs
[i
* 2 + 1];
348 /* We can't create a context that satisfies the requirements of an
349 * attribute that we don't understand. Return failure.
351 assert(!"Should not get here.");
352 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
357 /* Mesa does not support the GL_ARB_compatibilty extension or the
358 * compatibility profile. This means that we treat a API_OPENGL_COMPAT 3.1 as
359 * API_OPENGL_CORE and reject API_OPENGL_COMPAT 3.2+.
361 if (mesa_api
== API_OPENGL_COMPAT
&& major_version
== 3 && minor_version
== 1)
362 mesa_api
= API_OPENGL_CORE
;
364 if (mesa_api
== API_OPENGL_COMPAT
365 && ((major_version
> 3)
366 || (major_version
== 3 && minor_version
>= 2))) {
367 *error
= __DRI_CTX_ERROR_BAD_API
;
371 /* The EGL_KHR_create_context spec says:
373 * "Flags are only defined for OpenGL context creation, and specifying
374 * a flags value other than zero for other types of contexts,
375 * including OpenGL ES contexts, will generate an error."
377 * The GLX_EXT_create_context_es2_profile specification doesn't say
378 * anything specific about this case. However, none of the known flags
379 * have any meaning in an ES context, so this seems safe.
381 if (mesa_api
!= API_OPENGL_COMPAT
382 && mesa_api
!= API_OPENGL_CORE
384 *error
= __DRI_CTX_ERROR_BAD_FLAG
;
388 /* There are no forward-compatible contexts before OpenGL 3.0. The
389 * GLX_ARB_create_context spec says:
391 * "Forward-compatible contexts are defined only for OpenGL versions
394 * Forward-looking contexts are supported by silently converting the
395 * requested API to API_OPENGL_CORE.
397 * In Mesa, a debug context is the same as a regular context.
399 if ((flags
& __DRI_CTX_FLAG_FORWARD_COMPATIBLE
) != 0) {
400 mesa_api
= API_OPENGL_CORE
;
403 if ((flags
& ~(__DRI_CTX_FLAG_DEBUG
| __DRI_CTX_FLAG_FORWARD_COMPATIBLE
))
405 *error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
409 if (!validate_context_version(screen
, mesa_api
,
410 major_version
, minor_version
, error
))
413 context
= calloc(1, sizeof *context
);
415 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
419 context
->loaderPrivate
= data
;
421 context
->driScreenPriv
= screen
;
422 context
->driDrawablePriv
= NULL
;
423 context
->driReadablePriv
= NULL
;
425 if (!screen
->driver
->CreateContext(mesa_api
, modes
, context
,
426 major_version
, minor_version
,
427 flags
, error
, shareCtx
) ) {
432 struct gl_context
*ctx
= context
->driverPrivate
;
433 if ((flags
& __DRI_CTX_FLAG_FORWARD_COMPATIBLE
) != 0)
434 ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT
;
435 if ((flags
& __DRI_CTX_FLAG_DEBUG
) != 0) {
436 ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_DEBUG_BIT
;
437 ctx
->Debug
.DebugOutput
= GL_TRUE
;
440 *error
= __DRI_CTX_ERROR_SUCCESS
;
444 static __DRIcontext
*
445 dri2CreateNewContextForAPI(__DRIscreen
*screen
, int api
,
446 const __DRIconfig
*config
,
447 __DRIcontext
*shared
, void *data
)
451 return dri2CreateContextAttribs(screen
, api
, config
, shared
, 0, NULL
,
455 static __DRIcontext
*
456 dri2CreateNewContext(__DRIscreen
*screen
, const __DRIconfig
*config
,
457 __DRIcontext
*shared
, void *data
)
459 return dri2CreateNewContextForAPI(screen
, __DRI_API_OPENGL
,
460 config
, shared
, data
);
464 * Destroy the per-context private information.
467 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
468 * drmDestroyContext(), and finally frees \p contextPrivate.
471 driDestroyContext(__DRIcontext
*pcp
)
474 pcp
->driScreenPriv
->driver
->DestroyContext(pcp
);
480 driCopyContext(__DRIcontext
*dest
, __DRIcontext
*src
, unsigned long mask
)
491 /*****************************************************************/
492 /** \name Context (un)binding functions */
493 /*****************************************************************/
496 static void dri_get_drawable(__DRIdrawable
*pdp
);
497 static void dri_put_drawable(__DRIdrawable
*pdp
);
500 * This function takes both a read buffer and a draw buffer. This is needed
501 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
504 static int driBindContext(__DRIcontext
*pcp
,
509 ** Assume error checking is done properly in glXMakeCurrent before
510 ** calling driUnbindContext.
516 /* Bind the drawable to the context */
517 pcp
->driDrawablePriv
= pdp
;
518 pcp
->driReadablePriv
= prp
;
520 pdp
->driContextPriv
= pcp
;
521 dri_get_drawable(pdp
);
523 if (prp
&& pdp
!= prp
) {
524 dri_get_drawable(prp
);
527 return pcp
->driScreenPriv
->driver
->MakeCurrent(pcp
, pdp
, prp
);
533 * \param scrn the screen.
536 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
539 * This function calls __DriverAPIRec::UnbindContext, and then decrements
540 * __DRIdrawableRec::refcount which must be non-zero for a successful
543 * While casting the opaque private pointers associated with the parameters
544 * into their respective real types it also assures they are not \c NULL.
546 static int driUnbindContext(__DRIcontext
*pcp
)
552 ** Assume error checking is done properly in glXMakeCurrent before
553 ** calling driUnbindContext.
559 pdp
= pcp
->driDrawablePriv
;
560 prp
= pcp
->driReadablePriv
;
562 /* already unbound */
566 pcp
->driScreenPriv
->driver
->UnbindContext(pcp
);
569 if (pdp
->refcount
== 0) {
574 dri_put_drawable(pdp
);
577 if (prp
->refcount
== 0) {
582 dri_put_drawable(prp
);
585 pcp
->driDrawablePriv
= NULL
;
586 pcp
->driReadablePriv
= NULL
;
594 static void dri_get_drawable(__DRIdrawable
*pdp
)
599 static void dri_put_drawable(__DRIdrawable
*pdp
)
606 pdp
->driScreenPriv
->driver
->DestroyBuffer(pdp
);
611 static __DRIdrawable
*
612 dri2CreateNewDrawable(__DRIscreen
*screen
,
613 const __DRIconfig
*config
,
616 __DRIdrawable
*pdraw
;
618 pdraw
= malloc(sizeof *pdraw
);
622 pdraw
->loaderPrivate
= data
;
624 pdraw
->driScreenPriv
= screen
;
625 pdraw
->driContextPriv
= NULL
;
627 pdraw
->lastStamp
= 0;
631 dri_get_drawable(pdraw
);
633 if (!screen
->driver
->CreateBuffer(screen
, pdraw
, &config
->modes
,
639 pdraw
->dri2
.stamp
= pdraw
->lastStamp
+ 1;
645 driDestroyDrawable(__DRIdrawable
*pdp
)
647 dri_put_drawable(pdp
);
651 dri2AllocateBuffer(__DRIscreen
*screen
,
652 unsigned int attachment
, unsigned int format
,
653 int width
, int height
)
655 return screen
->driver
->AllocateBuffer(screen
, attachment
, format
,
660 dri2ReleaseBuffer(__DRIscreen
*screen
, __DRIbuffer
*buffer
)
662 screen
->driver
->ReleaseBuffer(screen
, buffer
);
667 dri2ConfigQueryb(__DRIscreen
*screen
, const char *var
, GLboolean
*val
)
669 if (!driCheckOption(&screen
->optionCache
, var
, DRI_BOOL
))
672 *val
= driQueryOptionb(&screen
->optionCache
, var
);
678 dri2ConfigQueryi(__DRIscreen
*screen
, const char *var
, GLint
*val
)
680 if (!driCheckOption(&screen
->optionCache
, var
, DRI_INT
) &&
681 !driCheckOption(&screen
->optionCache
, var
, DRI_ENUM
))
684 *val
= driQueryOptioni(&screen
->optionCache
, var
);
690 dri2ConfigQueryf(__DRIscreen
*screen
, const char *var
, GLfloat
*val
)
692 if (!driCheckOption(&screen
->optionCache
, var
, DRI_FLOAT
))
695 *val
= driQueryOptionf(&screen
->optionCache
, var
);
701 dri2GetAPIMask(__DRIscreen
*screen
)
703 return screen
->api_mask
;
707 * swrast swapbuffers entrypoint.
709 * DRI2 implements this inside the loader with only flushes handled by the
713 driSwapBuffers(__DRIdrawable
*pdp
)
715 assert(pdp
->driScreenPriv
->swrast_loader
);
717 pdp
->driScreenPriv
->driver
->SwapBuffers(pdp
);
720 /** Core interface */
721 const __DRIcoreExtension driCoreExtension
= {
722 .base
= { __DRI_CORE
, __DRI_CORE_VERSION
},
724 .createNewScreen
= NULL
,
725 .destroyScreen
= driDestroyScreen
,
726 .getExtensions
= driGetExtensions
,
727 .getConfigAttrib
= driGetConfigAttrib
,
728 .indexConfigAttrib
= driIndexConfigAttrib
,
729 .createNewDrawable
= NULL
,
730 .destroyDrawable
= driDestroyDrawable
,
731 .swapBuffers
= driSwapBuffers
, /* swrast */
732 .createNewContext
= dri2CreateNewContext
, /* swrast */
733 .copyContext
= driCopyContext
,
734 .destroyContext
= driDestroyContext
,
735 .bindContext
= driBindContext
,
736 .unbindContext
= driUnbindContext
739 /** DRI2 interface */
740 const __DRIdri2Extension driDRI2Extension
= {
741 .base
= { __DRI_DRI2
, 4 },
743 .createNewScreen
= dri2CreateNewScreen
,
744 .createNewDrawable
= dri2CreateNewDrawable
,
745 .createNewContext
= dri2CreateNewContext
,
746 .getAPIMask
= dri2GetAPIMask
,
747 .createNewContextForAPI
= dri2CreateNewContextForAPI
,
748 .allocateBuffer
= dri2AllocateBuffer
,
749 .releaseBuffer
= dri2ReleaseBuffer
,
750 .createContextAttribs
= dri2CreateContextAttribs
,
751 .createNewScreen2
= dri2CreateNewScreen2
,
754 const __DRIswrastExtension driSWRastExtension
= {
756 driSWRastCreateNewScreen
,
757 dri2CreateNewDrawable
,
758 dri2CreateNewContextForAPI
,
759 dri2CreateContextAttribs
,
760 driSWRastCreateNewScreen2
,
763 const __DRI2configQueryExtension dri2ConfigQueryExtension
= {
764 .base
= { __DRI2_CONFIG_QUERY
, __DRI2_CONFIG_QUERY_VERSION
},
766 .configQueryb
= dri2ConfigQueryb
,
767 .configQueryi
= dri2ConfigQueryi
,
768 .configQueryf
= dri2ConfigQueryf
,
772 dri2InvalidateDrawable(__DRIdrawable
*drawable
)
774 drawable
->dri2
.stamp
++;
778 * Check that the gl_framebuffer associated with dPriv is the right size.
779 * Resize the gl_framebuffer if needed.
780 * It's expected that the dPriv->driverPrivate member points to a
781 * gl_framebuffer object.
784 driUpdateFramebufferSize(struct gl_context
*ctx
, const __DRIdrawable
*dPriv
)
786 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) dPriv
->driverPrivate
;
787 if (fb
&& (dPriv
->w
!= fb
->Width
|| dPriv
->h
!= fb
->Height
)) {
788 ctx
->Driver
.ResizeBuffers(ctx
, fb
, dPriv
->w
, dPriv
->h
);
789 /* if the driver needs the hw lock for ResizeBuffers, the drawable
790 might have changed again by now */
791 assert(fb
->Width
== dPriv
->w
);
792 assert(fb
->Height
== dPriv
->h
);