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 is the first entrypoint in the driver called by the DRI driver loader
86 * after dlopen()ing it.
88 * It's used to create global state for the driver across contexts on the same
92 dri2CreateNewScreen(int scrn
, int fd
,
93 const __DRIextension
**extensions
,
94 const __DRIconfig
***driver_configs
, void *data
)
96 static const __DRIextension
*emptyExtensionList
[] = { NULL
};
99 psp
= calloc(1, sizeof(*psp
));
103 psp
->driver
= &driDriverAPI
;
105 setupLoaderExtensions(psp
, extensions
);
107 #ifndef __NOT_HAVE_DRM_H
109 drmVersionPtr version
= drmGetVersion(fd
);
111 psp
->drm_version
.major
= version
->version_major
;
112 psp
->drm_version
.minor
= version
->version_minor
;
113 psp
->drm_version
.patch
= version
->version_patchlevel
;
114 drmFreeVersion(version
);
119 psp
->loaderPrivate
= data
;
121 psp
->extensions
= emptyExtensionList
;
125 *driver_configs
= psp
->driver
->InitScreen(psp
);
126 if (*driver_configs
== NULL
) {
131 int gl_version_override
= _mesa_get_gl_version_override();
132 if (gl_version_override
>= 31) {
133 psp
->max_gl_core_version
= MAX2(psp
->max_gl_core_version
,
134 gl_version_override
);
136 psp
->max_gl_compat_version
= MAX2(psp
->max_gl_compat_version
,
137 gl_version_override
);
140 psp
->api_mask
= (1 << __DRI_API_OPENGL
);
141 if (psp
->max_gl_core_version
> 0)
142 psp
->api_mask
|= (1 << __DRI_API_OPENGL_CORE
);
143 if (psp
->max_gl_es1_version
> 0)
144 psp
->api_mask
|= (1 << __DRI_API_GLES
);
145 if (psp
->max_gl_es2_version
> 0)
146 psp
->api_mask
|= (1 << __DRI_API_GLES2
);
147 if (psp
->max_gl_es2_version
>= 30)
148 psp
->api_mask
|= (1 << __DRI_API_GLES3
);
150 driParseOptionInfo(&psp
->optionInfo
, __dri2ConfigOptions
);
151 driParseConfigFiles(&psp
->optionCache
, &psp
->optionInfo
, psp
->myNum
, "dri2");
157 /** swrast driver createNewScreen entrypoint. */
159 driCreateNewScreen(int scrn
, const __DRIextension
**extensions
,
160 const __DRIconfig
***driver_configs
, void *data
)
162 return dri2CreateNewScreen(scrn
, -1, extensions
, driver_configs
, data
);
166 * Destroy the per-screen private information.
169 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
170 * drmClose(), and finally frees \p screenPrivate.
172 static void driDestroyScreen(__DRIscreen
*psp
)
175 /* No interaction with the X-server is possible at this point. This
176 * routine is called after XCloseDisplay, so there is no protocol
177 * stream open to the X-server anymore.
180 _mesa_destroy_shader_compiler();
182 psp
->driver
->DestroyScreen(psp
);
184 driDestroyOptionCache(&psp
->optionCache
);
185 driDestroyOptionInfo(&psp
->optionInfo
);
191 static const __DRIextension
**driGetExtensions(__DRIscreen
*psp
)
193 return psp
->extensions
;
200 validate_context_version(__DRIscreen
*screen
,
202 unsigned major_version
,
203 unsigned minor_version
,
204 unsigned *dri_ctx_error
)
206 unsigned req_version
= 10 * major_version
+ minor_version
;
207 unsigned max_version
= 0;
210 case API_OPENGL_COMPAT
:
211 max_version
= screen
->max_gl_compat_version
;
213 case API_OPENGL_CORE
:
214 max_version
= screen
->max_gl_core_version
;
217 max_version
= screen
->max_gl_es1_version
;
220 max_version
= screen
->max_gl_es2_version
;
227 if (max_version
== 0) {
228 *dri_ctx_error
= __DRI_CTX_ERROR_BAD_API
;
230 } else if (req_version
> max_version
) {
231 *dri_ctx_error
= __DRI_CTX_ERROR_BAD_VERSION
;
238 /*****************************************************************/
239 /** \name Context handling functions */
240 /*****************************************************************/
243 static __DRIcontext
*
244 dri2CreateContextAttribs(__DRIscreen
*screen
, int api
,
245 const __DRIconfig
*config
,
246 __DRIcontext
*shared
,
247 unsigned num_attribs
,
248 const uint32_t *attribs
,
252 __DRIcontext
*context
;
253 const struct gl_config
*modes
= (config
!= NULL
) ? &config
->modes
: NULL
;
254 void *shareCtx
= (shared
!= NULL
) ? shared
->driverPrivate
: NULL
;
256 unsigned major_version
= 1;
257 unsigned minor_version
= 0;
260 assert((num_attribs
== 0) || (attribs
!= NULL
));
262 if (!(screen
->api_mask
& (1 << api
))) {
263 *error
= __DRI_CTX_ERROR_BAD_API
;
268 case __DRI_API_OPENGL
:
269 mesa_api
= API_OPENGL_COMPAT
;
272 mesa_api
= API_OPENGLES
;
274 case __DRI_API_GLES2
:
275 case __DRI_API_GLES3
:
276 mesa_api
= API_OPENGLES2
;
278 case __DRI_API_OPENGL_CORE
:
279 mesa_api
= API_OPENGL_CORE
;
282 *error
= __DRI_CTX_ERROR_BAD_API
;
286 for (unsigned i
= 0; i
< num_attribs
; i
++) {
287 switch (attribs
[i
* 2]) {
288 case __DRI_CTX_ATTRIB_MAJOR_VERSION
:
289 major_version
= attribs
[i
* 2 + 1];
291 case __DRI_CTX_ATTRIB_MINOR_VERSION
:
292 minor_version
= attribs
[i
* 2 + 1];
294 case __DRI_CTX_ATTRIB_FLAGS
:
295 flags
= attribs
[i
* 2 + 1];
298 /* We can't create a context that satisfies the requirements of an
299 * attribute that we don't understand. Return failure.
301 assert(!"Should not get here.");
302 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
307 /* Mesa does not support the GL_ARB_compatibilty extension or the
308 * compatibility profile. This means that we treat a API_OPENGL_COMPAT 3.1 as
309 * API_OPENGL_CORE and reject API_OPENGL_COMPAT 3.2+.
311 if (mesa_api
== API_OPENGL_COMPAT
&& major_version
== 3 && minor_version
== 1)
312 mesa_api
= API_OPENGL_CORE
;
314 if (mesa_api
== API_OPENGL_COMPAT
315 && ((major_version
> 3)
316 || (major_version
== 3 && minor_version
>= 2))) {
317 *error
= __DRI_CTX_ERROR_BAD_API
;
321 /* The EGL_KHR_create_context spec says:
323 * "Flags are only defined for OpenGL context creation, and specifying
324 * a flags value other than zero for other types of contexts,
325 * including OpenGL ES contexts, will generate an error."
327 * The GLX_EXT_create_context_es2_profile specification doesn't say
328 * anything specific about this case. However, none of the known flags
329 * have any meaning in an ES context, so this seems safe.
331 if (mesa_api
!= API_OPENGL_COMPAT
332 && mesa_api
!= API_OPENGL_CORE
334 *error
= __DRI_CTX_ERROR_BAD_FLAG
;
338 /* There are no forward-compatible contexts before OpenGL 3.0. The
339 * GLX_ARB_create_context spec says:
341 * "Forward-compatible contexts are defined only for OpenGL versions
344 * Forward-looking contexts are supported by silently converting the
345 * requested API to API_OPENGL_CORE.
347 * In Mesa, a debug context is the same as a regular context.
349 if ((flags
& __DRI_CTX_FLAG_FORWARD_COMPATIBLE
) != 0) {
350 mesa_api
= API_OPENGL_CORE
;
353 if ((flags
& ~(__DRI_CTX_FLAG_DEBUG
| __DRI_CTX_FLAG_FORWARD_COMPATIBLE
))
355 *error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
359 if (!validate_context_version(screen
, mesa_api
,
360 major_version
, minor_version
, error
))
363 context
= calloc(1, sizeof *context
);
365 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
369 context
->loaderPrivate
= data
;
371 context
->driScreenPriv
= screen
;
372 context
->driDrawablePriv
= NULL
;
373 context
->driReadablePriv
= NULL
;
375 if (!screen
->driver
->CreateContext(mesa_api
, modes
, context
,
376 major_version
, minor_version
,
377 flags
, error
, shareCtx
) ) {
382 struct gl_context
*ctx
= context
->driverPrivate
;
383 if ((flags
& __DRI_CTX_FLAG_FORWARD_COMPATIBLE
) != 0)
384 ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT
;
385 if ((flags
& __DRI_CTX_FLAG_DEBUG
) != 0) {
386 ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_DEBUG_BIT
;
387 ctx
->Debug
.DebugOutput
= GL_TRUE
;
390 *error
= __DRI_CTX_ERROR_SUCCESS
;
394 static __DRIcontext
*
395 dri2CreateNewContextForAPI(__DRIscreen
*screen
, int api
,
396 const __DRIconfig
*config
,
397 __DRIcontext
*shared
, void *data
)
401 return dri2CreateContextAttribs(screen
, api
, config
, shared
, 0, NULL
,
405 static __DRIcontext
*
406 dri2CreateNewContext(__DRIscreen
*screen
, const __DRIconfig
*config
,
407 __DRIcontext
*shared
, void *data
)
409 return dri2CreateNewContextForAPI(screen
, __DRI_API_OPENGL
,
410 config
, shared
, data
);
414 * Destroy the per-context private information.
417 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
418 * drmDestroyContext(), and finally frees \p contextPrivate.
421 driDestroyContext(__DRIcontext
*pcp
)
424 pcp
->driScreenPriv
->driver
->DestroyContext(pcp
);
430 driCopyContext(__DRIcontext
*dest
, __DRIcontext
*src
, unsigned long mask
)
441 /*****************************************************************/
442 /** \name Context (un)binding functions */
443 /*****************************************************************/
446 static void dri_get_drawable(__DRIdrawable
*pdp
);
447 static void dri_put_drawable(__DRIdrawable
*pdp
);
450 * This function takes both a read buffer and a draw buffer. This is needed
451 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
454 static int driBindContext(__DRIcontext
*pcp
,
459 ** Assume error checking is done properly in glXMakeCurrent before
460 ** calling driUnbindContext.
466 /* Bind the drawable to the context */
467 pcp
->driDrawablePriv
= pdp
;
468 pcp
->driReadablePriv
= prp
;
470 pdp
->driContextPriv
= pcp
;
471 dri_get_drawable(pdp
);
473 if (prp
&& pdp
!= prp
) {
474 dri_get_drawable(prp
);
477 return pcp
->driScreenPriv
->driver
->MakeCurrent(pcp
, pdp
, prp
);
483 * \param scrn the screen.
486 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
489 * This function calls __DriverAPIRec::UnbindContext, and then decrements
490 * __DRIdrawableRec::refcount which must be non-zero for a successful
493 * While casting the opaque private pointers associated with the parameters
494 * into their respective real types it also assures they are not \c NULL.
496 static int driUnbindContext(__DRIcontext
*pcp
)
502 ** Assume error checking is done properly in glXMakeCurrent before
503 ** calling driUnbindContext.
509 pdp
= pcp
->driDrawablePriv
;
510 prp
= pcp
->driReadablePriv
;
512 /* already unbound */
516 pcp
->driScreenPriv
->driver
->UnbindContext(pcp
);
519 if (pdp
->refcount
== 0) {
524 dri_put_drawable(pdp
);
527 if (prp
->refcount
== 0) {
532 dri_put_drawable(prp
);
535 pcp
->driDrawablePriv
= NULL
;
536 pcp
->driReadablePriv
= NULL
;
544 static void dri_get_drawable(__DRIdrawable
*pdp
)
549 static void dri_put_drawable(__DRIdrawable
*pdp
)
556 pdp
->driScreenPriv
->driver
->DestroyBuffer(pdp
);
561 static __DRIdrawable
*
562 dri2CreateNewDrawable(__DRIscreen
*screen
,
563 const __DRIconfig
*config
,
566 __DRIdrawable
*pdraw
;
568 pdraw
= malloc(sizeof *pdraw
);
572 pdraw
->loaderPrivate
= data
;
574 pdraw
->driScreenPriv
= screen
;
575 pdraw
->driContextPriv
= NULL
;
577 pdraw
->lastStamp
= 0;
581 dri_get_drawable(pdraw
);
583 if (!screen
->driver
->CreateBuffer(screen
, pdraw
, &config
->modes
,
589 pdraw
->dri2
.stamp
= pdraw
->lastStamp
+ 1;
595 driDestroyDrawable(__DRIdrawable
*pdp
)
597 dri_put_drawable(pdp
);
601 dri2AllocateBuffer(__DRIscreen
*screen
,
602 unsigned int attachment
, unsigned int format
,
603 int width
, int height
)
605 return screen
->driver
->AllocateBuffer(screen
, attachment
, format
,
610 dri2ReleaseBuffer(__DRIscreen
*screen
, __DRIbuffer
*buffer
)
612 screen
->driver
->ReleaseBuffer(screen
, buffer
);
617 dri2ConfigQueryb(__DRIscreen
*screen
, const char *var
, GLboolean
*val
)
619 if (!driCheckOption(&screen
->optionCache
, var
, DRI_BOOL
))
622 *val
= driQueryOptionb(&screen
->optionCache
, var
);
628 dri2ConfigQueryi(__DRIscreen
*screen
, const char *var
, GLint
*val
)
630 if (!driCheckOption(&screen
->optionCache
, var
, DRI_INT
) &&
631 !driCheckOption(&screen
->optionCache
, var
, DRI_ENUM
))
634 *val
= driQueryOptioni(&screen
->optionCache
, var
);
640 dri2ConfigQueryf(__DRIscreen
*screen
, const char *var
, GLfloat
*val
)
642 if (!driCheckOption(&screen
->optionCache
, var
, DRI_FLOAT
))
645 *val
= driQueryOptionf(&screen
->optionCache
, var
);
651 dri2GetAPIMask(__DRIscreen
*screen
)
653 return screen
->api_mask
;
657 * swrast swapbuffers entrypoint.
659 * DRI2 implements this inside the loader with only flushes handled by the
663 driSwapBuffers(__DRIdrawable
*pdp
)
665 assert(pdp
->driScreenPriv
->swrast_loader
);
667 pdp
->driScreenPriv
->driver
->SwapBuffers(pdp
);
670 /** Core interface */
671 const __DRIcoreExtension driCoreExtension
= {
672 .base
= { __DRI_CORE
, __DRI_CORE_VERSION
},
674 .createNewScreen
= NULL
,
675 .destroyScreen
= driDestroyScreen
,
676 .getExtensions
= driGetExtensions
,
677 .getConfigAttrib
= driGetConfigAttrib
,
678 .indexConfigAttrib
= driIndexConfigAttrib
,
679 .createNewDrawable
= NULL
,
680 .destroyDrawable
= driDestroyDrawable
,
681 .swapBuffers
= driSwapBuffers
, /* swrast */
682 .createNewContext
= dri2CreateNewContext
, /* swrast */
683 .copyContext
= driCopyContext
,
684 .destroyContext
= driDestroyContext
,
685 .bindContext
= driBindContext
,
686 .unbindContext
= driUnbindContext
689 /** DRI2 interface */
690 const __DRIdri2Extension driDRI2Extension
= {
691 .base
= { __DRI_DRI2
, 3 },
693 .createNewScreen
= dri2CreateNewScreen
,
694 .createNewDrawable
= dri2CreateNewDrawable
,
695 .createNewContext
= dri2CreateNewContext
,
696 .getAPIMask
= dri2GetAPIMask
,
697 .createNewContextForAPI
= dri2CreateNewContextForAPI
,
698 .allocateBuffer
= dri2AllocateBuffer
,
699 .releaseBuffer
= dri2ReleaseBuffer
,
700 .createContextAttribs
= dri2CreateContextAttribs
703 const __DRIswrastExtension driSWRastExtension
= {
704 { __DRI_SWRAST
, __DRI_SWRAST_VERSION
},
706 dri2CreateNewDrawable
,
707 dri2CreateNewContextForAPI
,
708 dri2CreateContextAttribs
711 const __DRI2configQueryExtension dri2ConfigQueryExtension
= {
712 .base
= { __DRI2_CONFIG_QUERY
, __DRI2_CONFIG_QUERY_VERSION
},
714 .configQueryb
= dri2ConfigQueryb
,
715 .configQueryi
= dri2ConfigQueryi
,
716 .configQueryf
= dri2ConfigQueryf
,
720 dri2InvalidateDrawable(__DRIdrawable
*drawable
)
722 drawable
->dri2
.stamp
++;
726 * Check that the gl_framebuffer associated with dPriv is the right size.
727 * Resize the gl_framebuffer if needed.
728 * It's expected that the dPriv->driverPrivate member points to a
729 * gl_framebuffer object.
732 driUpdateFramebufferSize(struct gl_context
*ctx
, const __DRIdrawable
*dPriv
)
734 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) dPriv
->driverPrivate
;
735 if (fb
&& (dPriv
->w
!= fb
->Width
|| dPriv
->h
!= fb
->Height
)) {
736 ctx
->Driver
.ResizeBuffers(ctx
, fb
, dPriv
->w
, dPriv
->h
);
737 /* if the driver needs the hw lock for ResizeBuffers, the drawable
738 might have changed again by now */
739 assert(fb
->Width
== dPriv
->w
);
740 assert(fb
->Height
== dPriv
->h
);