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/version.h"
51 #include "main/macros.h"
53 PUBLIC
const char __dri2ConfigOptions
[] =
55 DRI_CONF_SECTION_PERFORMANCE
56 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_1
)
60 /*****************************************************************/
61 /** \name Screen handling functions */
62 /*****************************************************************/
66 setupLoaderExtensions(__DRIscreen
*psp
,
67 const __DRIextension
**extensions
)
71 for (i
= 0; extensions
[i
]; i
++) {
72 if (strcmp(extensions
[i
]->name
, __DRI_DRI2_LOADER
) == 0)
73 psp
->dri2
.loader
= (__DRIdri2LoaderExtension
*) extensions
[i
];
74 if (strcmp(extensions
[i
]->name
, __DRI_IMAGE_LOOKUP
) == 0)
75 psp
->dri2
.image
= (__DRIimageLookupExtension
*) extensions
[i
];
76 if (strcmp(extensions
[i
]->name
, __DRI_USE_INVALIDATE
) == 0)
77 psp
->dri2
.useInvalidate
= (__DRIuseInvalidateExtension
*) extensions
[i
];
78 if (strcmp(extensions
[i
]->name
, __DRI_SWRAST_LOADER
) == 0)
79 psp
->swrast_loader
= (__DRIswrastLoaderExtension
*) extensions
[i
];
84 * This is the first entrypoint in the driver called by the DRI driver loader
85 * after dlopen()ing it.
87 * It's used to create global state for the driver across contexts on the same
91 dri2CreateNewScreen(int scrn
, int fd
,
92 const __DRIextension
**extensions
,
93 const __DRIconfig
***driver_configs
, void *data
)
95 static const __DRIextension
*emptyExtensionList
[] = { NULL
};
98 psp
= calloc(1, sizeof(*psp
));
102 psp
->driver
= &driDriverAPI
;
104 setupLoaderExtensions(psp
, extensions
);
106 #ifndef __NOT_HAVE_DRM_H
108 drmVersionPtr version
= drmGetVersion(fd
);
110 psp
->drm_version
.major
= version
->version_major
;
111 psp
->drm_version
.minor
= version
->version_minor
;
112 psp
->drm_version
.patch
= version
->version_patchlevel
;
113 drmFreeVersion(version
);
118 psp
->loaderPrivate
= data
;
120 psp
->extensions
= emptyExtensionList
;
124 *driver_configs
= psp
->driver
->InitScreen(psp
);
125 if (*driver_configs
== NULL
) {
130 int gl_version_override
= _mesa_get_gl_version_override();
131 if (gl_version_override
>= 31) {
132 psp
->max_gl_core_version
= MAX2(psp
->max_gl_core_version
,
133 gl_version_override
);
135 psp
->max_gl_compat_version
= MAX2(psp
->max_gl_compat_version
,
136 gl_version_override
);
139 psp
->api_mask
= (1 << __DRI_API_OPENGL
);
140 if (psp
->max_gl_core_version
> 0)
141 psp
->api_mask
|= (1 << __DRI_API_OPENGL_CORE
);
142 if (psp
->max_gl_es1_version
> 0)
143 psp
->api_mask
|= (1 << __DRI_API_GLES
);
144 if (psp
->max_gl_es2_version
> 0)
145 psp
->api_mask
|= (1 << __DRI_API_GLES2
);
146 if (psp
->max_gl_es2_version
>= 30)
147 psp
->api_mask
|= (1 << __DRI_API_GLES3
);
149 driParseOptionInfo(&psp
->optionInfo
, __dri2ConfigOptions
);
150 driParseConfigFiles(&psp
->optionCache
, &psp
->optionInfo
, psp
->myNum
, "dri2");
156 /** swrast driver createNewScreen entrypoint. */
158 driCreateNewScreen(int scrn
, const __DRIextension
**extensions
,
159 const __DRIconfig
***driver_configs
, void *data
)
161 return dri2CreateNewScreen(scrn
, -1, extensions
, driver_configs
, data
);
165 * Destroy the per-screen private information.
168 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
169 * drmClose(), and finally frees \p screenPrivate.
171 static void driDestroyScreen(__DRIscreen
*psp
)
174 /* No interaction with the X-server is possible at this point. This
175 * routine is called after XCloseDisplay, so there is no protocol
176 * stream open to the X-server anymore.
179 _mesa_destroy_shader_compiler();
181 psp
->driver
->DestroyScreen(psp
);
183 driDestroyOptionCache(&psp
->optionCache
);
184 driDestroyOptionInfo(&psp
->optionInfo
);
190 static const __DRIextension
**driGetExtensions(__DRIscreen
*psp
)
192 return psp
->extensions
;
199 validate_context_version(__DRIscreen
*screen
,
201 unsigned major_version
,
202 unsigned minor_version
,
203 unsigned *dri_ctx_error
)
205 unsigned req_version
= 10 * major_version
+ minor_version
;
206 unsigned max_version
= 0;
209 case API_OPENGL_COMPAT
:
210 max_version
= screen
->max_gl_compat_version
;
212 case API_OPENGL_CORE
:
213 max_version
= screen
->max_gl_core_version
;
216 max_version
= screen
->max_gl_es1_version
;
219 max_version
= screen
->max_gl_es2_version
;
226 if (max_version
== 0) {
227 *dri_ctx_error
= __DRI_CTX_ERROR_BAD_API
;
229 } else if (req_version
> max_version
) {
230 *dri_ctx_error
= __DRI_CTX_ERROR_BAD_VERSION
;
237 /*****************************************************************/
238 /** \name Context handling functions */
239 /*****************************************************************/
242 static __DRIcontext
*
243 dri2CreateContextAttribs(__DRIscreen
*screen
, int api
,
244 const __DRIconfig
*config
,
245 __DRIcontext
*shared
,
246 unsigned num_attribs
,
247 const uint32_t *attribs
,
251 __DRIcontext
*context
;
252 const struct gl_config
*modes
= (config
!= NULL
) ? &config
->modes
: NULL
;
253 void *shareCtx
= (shared
!= NULL
) ? shared
->driverPrivate
: NULL
;
255 unsigned major_version
= 1;
256 unsigned minor_version
= 0;
259 assert((num_attribs
== 0) || (attribs
!= NULL
));
261 if (!(screen
->api_mask
& (1 << api
))) {
262 *error
= __DRI_CTX_ERROR_BAD_API
;
267 case __DRI_API_OPENGL
:
268 mesa_api
= API_OPENGL_COMPAT
;
271 mesa_api
= API_OPENGLES
;
273 case __DRI_API_GLES2
:
274 case __DRI_API_GLES3
:
275 mesa_api
= API_OPENGLES2
;
277 case __DRI_API_OPENGL_CORE
:
278 mesa_api
= API_OPENGL_CORE
;
281 *error
= __DRI_CTX_ERROR_BAD_API
;
285 for (unsigned i
= 0; i
< num_attribs
; i
++) {
286 switch (attribs
[i
* 2]) {
287 case __DRI_CTX_ATTRIB_MAJOR_VERSION
:
288 major_version
= attribs
[i
* 2 + 1];
290 case __DRI_CTX_ATTRIB_MINOR_VERSION
:
291 minor_version
= attribs
[i
* 2 + 1];
293 case __DRI_CTX_ATTRIB_FLAGS
:
294 flags
= attribs
[i
* 2 + 1];
297 /* We can't create a context that satisfies the requirements of an
298 * attribute that we don't understand. Return failure.
300 assert(!"Should not get here.");
301 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
306 /* Mesa does not support the GL_ARB_compatibilty extension or the
307 * compatibility profile. This means that we treat a API_OPENGL_COMPAT 3.1 as
308 * API_OPENGL_CORE and reject API_OPENGL_COMPAT 3.2+.
310 if (mesa_api
== API_OPENGL_COMPAT
&& major_version
== 3 && minor_version
== 1)
311 mesa_api
= API_OPENGL_CORE
;
313 if (mesa_api
== API_OPENGL_COMPAT
314 && ((major_version
> 3)
315 || (major_version
== 3 && minor_version
>= 2))) {
316 *error
= __DRI_CTX_ERROR_BAD_API
;
320 /* The EGL_KHR_create_context spec says:
322 * "Flags are only defined for OpenGL context creation, and specifying
323 * a flags value other than zero for other types of contexts,
324 * including OpenGL ES contexts, will generate an error."
326 * The GLX_EXT_create_context_es2_profile specification doesn't say
327 * anything specific about this case. However, none of the known flags
328 * have any meaning in an ES context, so this seems safe.
330 if (mesa_api
!= API_OPENGL_COMPAT
331 && mesa_api
!= API_OPENGL_CORE
333 *error
= __DRI_CTX_ERROR_BAD_FLAG
;
337 /* There are no forward-compatible contexts before OpenGL 3.0. The
338 * GLX_ARB_create_context spec says:
340 * "Forward-compatible contexts are defined only for OpenGL versions
343 * Forward-looking contexts are supported by silently converting the
344 * requested API to API_OPENGL_CORE.
346 * In Mesa, a debug context is the same as a regular context.
348 if ((flags
& __DRI_CTX_FLAG_FORWARD_COMPATIBLE
) != 0) {
349 mesa_api
= API_OPENGL_CORE
;
352 if ((flags
& ~(__DRI_CTX_FLAG_DEBUG
| __DRI_CTX_FLAG_FORWARD_COMPATIBLE
))
354 *error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
358 if (!validate_context_version(screen
, mesa_api
,
359 major_version
, minor_version
, error
))
362 context
= calloc(1, sizeof *context
);
364 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
368 context
->loaderPrivate
= data
;
370 context
->driScreenPriv
= screen
;
371 context
->driDrawablePriv
= NULL
;
372 context
->driReadablePriv
= NULL
;
374 if (!screen
->driver
->CreateContext(mesa_api
, modes
, context
,
375 major_version
, minor_version
,
376 flags
, error
, shareCtx
) ) {
381 *error
= __DRI_CTX_ERROR_SUCCESS
;
385 static __DRIcontext
*
386 dri2CreateNewContextForAPI(__DRIscreen
*screen
, int api
,
387 const __DRIconfig
*config
,
388 __DRIcontext
*shared
, void *data
)
392 return dri2CreateContextAttribs(screen
, api
, config
, shared
, 0, NULL
,
396 static __DRIcontext
*
397 dri2CreateNewContext(__DRIscreen
*screen
, const __DRIconfig
*config
,
398 __DRIcontext
*shared
, void *data
)
400 return dri2CreateNewContextForAPI(screen
, __DRI_API_OPENGL
,
401 config
, shared
, data
);
405 * Destroy the per-context private information.
408 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
409 * drmDestroyContext(), and finally frees \p contextPrivate.
412 driDestroyContext(__DRIcontext
*pcp
)
415 pcp
->driScreenPriv
->driver
->DestroyContext(pcp
);
421 driCopyContext(__DRIcontext
*dest
, __DRIcontext
*src
, unsigned long mask
)
432 /*****************************************************************/
433 /** \name Context (un)binding functions */
434 /*****************************************************************/
437 static void dri_get_drawable(__DRIdrawable
*pdp
);
438 static void dri_put_drawable(__DRIdrawable
*pdp
);
441 * This function takes both a read buffer and a draw buffer. This is needed
442 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
445 static int driBindContext(__DRIcontext
*pcp
,
450 ** Assume error checking is done properly in glXMakeCurrent before
451 ** calling driUnbindContext.
457 /* Bind the drawable to the context */
458 pcp
->driDrawablePriv
= pdp
;
459 pcp
->driReadablePriv
= prp
;
461 pdp
->driContextPriv
= pcp
;
462 dri_get_drawable(pdp
);
464 if (prp
&& pdp
!= prp
) {
465 dri_get_drawable(prp
);
468 return pcp
->driScreenPriv
->driver
->MakeCurrent(pcp
, pdp
, prp
);
474 * \param scrn the screen.
477 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
480 * This function calls __DriverAPIRec::UnbindContext, and then decrements
481 * __DRIdrawableRec::refcount which must be non-zero for a successful
484 * While casting the opaque private pointers associated with the parameters
485 * into their respective real types it also assures they are not \c NULL.
487 static int driUnbindContext(__DRIcontext
*pcp
)
493 ** Assume error checking is done properly in glXMakeCurrent before
494 ** calling driUnbindContext.
500 pdp
= pcp
->driDrawablePriv
;
501 prp
= pcp
->driReadablePriv
;
503 /* already unbound */
507 pcp
->driScreenPriv
->driver
->UnbindContext(pcp
);
510 if (pdp
->refcount
== 0) {
515 dri_put_drawable(pdp
);
518 if (prp
->refcount
== 0) {
523 dri_put_drawable(prp
);
526 pcp
->driDrawablePriv
= NULL
;
527 pcp
->driReadablePriv
= NULL
;
535 static void dri_get_drawable(__DRIdrawable
*pdp
)
540 static void dri_put_drawable(__DRIdrawable
*pdp
)
547 pdp
->driScreenPriv
->driver
->DestroyBuffer(pdp
);
552 static __DRIdrawable
*
553 dri2CreateNewDrawable(__DRIscreen
*screen
,
554 const __DRIconfig
*config
,
557 __DRIdrawable
*pdraw
;
559 pdraw
= malloc(sizeof *pdraw
);
563 pdraw
->loaderPrivate
= data
;
565 pdraw
->driScreenPriv
= screen
;
566 pdraw
->driContextPriv
= NULL
;
568 pdraw
->lastStamp
= 0;
572 dri_get_drawable(pdraw
);
574 if (!screen
->driver
->CreateBuffer(screen
, pdraw
, &config
->modes
,
580 pdraw
->dri2
.stamp
= pdraw
->lastStamp
+ 1;
586 driDestroyDrawable(__DRIdrawable
*pdp
)
588 dri_put_drawable(pdp
);
592 dri2AllocateBuffer(__DRIscreen
*screen
,
593 unsigned int attachment
, unsigned int format
,
594 int width
, int height
)
596 return screen
->driver
->AllocateBuffer(screen
, attachment
, format
,
601 dri2ReleaseBuffer(__DRIscreen
*screen
, __DRIbuffer
*buffer
)
603 screen
->driver
->ReleaseBuffer(screen
, buffer
);
608 dri2ConfigQueryb(__DRIscreen
*screen
, const char *var
, GLboolean
*val
)
610 if (!driCheckOption(&screen
->optionCache
, var
, DRI_BOOL
))
613 *val
= driQueryOptionb(&screen
->optionCache
, var
);
619 dri2ConfigQueryi(__DRIscreen
*screen
, const char *var
, GLint
*val
)
621 if (!driCheckOption(&screen
->optionCache
, var
, DRI_INT
) &&
622 !driCheckOption(&screen
->optionCache
, var
, DRI_ENUM
))
625 *val
= driQueryOptioni(&screen
->optionCache
, var
);
631 dri2ConfigQueryf(__DRIscreen
*screen
, const char *var
, GLfloat
*val
)
633 if (!driCheckOption(&screen
->optionCache
, var
, DRI_FLOAT
))
636 *val
= driQueryOptionf(&screen
->optionCache
, var
);
642 dri2GetAPIMask(__DRIscreen
*screen
)
644 return screen
->api_mask
;
648 * swrast swapbuffers entrypoint.
650 * DRI2 implements this inside the loader with only flushes handled by the
654 driSwapBuffers(__DRIdrawable
*pdp
)
656 assert(pdp
->driScreenPriv
->swrast_loader
);
658 pdp
->driScreenPriv
->driver
->SwapBuffers(pdp
);
661 /** Core interface */
662 const __DRIcoreExtension driCoreExtension
= {
663 .base
= { __DRI_CORE
, __DRI_CORE_VERSION
},
665 .createNewScreen
= NULL
,
666 .destroyScreen
= driDestroyScreen
,
667 .getExtensions
= driGetExtensions
,
668 .getConfigAttrib
= driGetConfigAttrib
,
669 .indexConfigAttrib
= driIndexConfigAttrib
,
670 .createNewDrawable
= NULL
,
671 .destroyDrawable
= driDestroyDrawable
,
672 .swapBuffers
= driSwapBuffers
, /* swrast */
673 .createNewContext
= dri2CreateNewContext
, /* swrast */
674 .copyContext
= driCopyContext
,
675 .destroyContext
= driDestroyContext
,
676 .bindContext
= driBindContext
,
677 .unbindContext
= driUnbindContext
680 /** DRI2 interface */
681 const __DRIdri2Extension driDRI2Extension
= {
682 .base
= { __DRI_DRI2
, 3 },
684 .createNewScreen
= dri2CreateNewScreen
,
685 .createNewDrawable
= dri2CreateNewDrawable
,
686 .createNewContext
= dri2CreateNewContext
,
687 .getAPIMask
= dri2GetAPIMask
,
688 .createNewContextForAPI
= dri2CreateNewContextForAPI
,
689 .allocateBuffer
= dri2AllocateBuffer
,
690 .releaseBuffer
= dri2ReleaseBuffer
,
691 .createContextAttribs
= dri2CreateContextAttribs
694 const __DRIswrastExtension driSWRastExtension
= {
695 { __DRI_SWRAST
, __DRI_SWRAST_VERSION
},
697 dri2CreateNewDrawable
,
698 dri2CreateNewContextForAPI
,
699 dri2CreateContextAttribs
702 const __DRI2configQueryExtension dri2ConfigQueryExtension
= {
703 .base
= { __DRI2_CONFIG_QUERY
, __DRI2_CONFIG_QUERY_VERSION
},
705 .configQueryb
= dri2ConfigQueryb
,
706 .configQueryi
= dri2ConfigQueryi
,
707 .configQueryf
= dri2ConfigQueryf
,
711 dri2InvalidateDrawable(__DRIdrawable
*drawable
)
713 drawable
->dri2
.stamp
++;
717 * Check that the gl_framebuffer associated with dPriv is the right size.
718 * Resize the gl_framebuffer if needed.
719 * It's expected that the dPriv->driverPrivate member points to a
720 * gl_framebuffer object.
723 driUpdateFramebufferSize(struct gl_context
*ctx
, const __DRIdrawable
*dPriv
)
725 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) dPriv
->driverPrivate
;
726 if (fb
&& (dPriv
->w
!= fb
->Width
|| dPriv
->h
!= fb
->Height
)) {
727 ctx
->Driver
.ResizeBuffers(ctx
, fb
, dPriv
->w
, dPriv
->h
);
728 /* if the driver needs the hw lock for ResizeBuffers, the drawable
729 might have changed again by now */
730 assert(fb
->Width
== dPriv
->w
);
731 assert(fb
->Height
== dPriv
->h
);