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 dri2CreateNewScreen2(int scrn
, int fd
,
93 const __DRIextension
**extensions
,
94 const __DRIextension
**driver_extensions
,
95 const __DRIconfig
***driver_configs
, void *data
)
97 static const __DRIextension
*emptyExtensionList
[] = { NULL
};
100 psp
= calloc(1, sizeof(*psp
));
104 psp
->driver
= &driDriverAPI
;
106 setupLoaderExtensions(psp
, extensions
);
108 #ifndef __NOT_HAVE_DRM_H
110 drmVersionPtr version
= drmGetVersion(fd
);
112 psp
->drm_version
.major
= version
->version_major
;
113 psp
->drm_version
.minor
= version
->version_minor
;
114 psp
->drm_version
.patch
= version
->version_patchlevel
;
115 drmFreeVersion(version
);
120 psp
->loaderPrivate
= data
;
122 psp
->extensions
= emptyExtensionList
;
126 *driver_configs
= psp
->driver
->InitScreen(psp
);
127 if (*driver_configs
== NULL
) {
132 int gl_version_override
= _mesa_get_gl_version_override();
133 if (gl_version_override
>= 31) {
134 psp
->max_gl_core_version
= MAX2(psp
->max_gl_core_version
,
135 gl_version_override
);
137 psp
->max_gl_compat_version
= MAX2(psp
->max_gl_compat_version
,
138 gl_version_override
);
141 psp
->api_mask
= (1 << __DRI_API_OPENGL
);
142 if (psp
->max_gl_core_version
> 0)
143 psp
->api_mask
|= (1 << __DRI_API_OPENGL_CORE
);
144 if (psp
->max_gl_es1_version
> 0)
145 psp
->api_mask
|= (1 << __DRI_API_GLES
);
146 if (psp
->max_gl_es2_version
> 0)
147 psp
->api_mask
|= (1 << __DRI_API_GLES2
);
148 if (psp
->max_gl_es2_version
>= 30)
149 psp
->api_mask
|= (1 << __DRI_API_GLES3
);
151 driParseOptionInfo(&psp
->optionInfo
, __dri2ConfigOptions
);
152 driParseConfigFiles(&psp
->optionCache
, &psp
->optionInfo
, psp
->myNum
, "dri2");
159 dri2CreateNewScreen(int scrn
, int fd
,
160 const __DRIextension
**extensions
,
161 const __DRIconfig
***driver_configs
, void *data
)
163 return dri2CreateNewScreen2(scrn
, fd
, extensions
, NULL
,
164 driver_configs
, data
);
167 /** swrast driver createNewScreen entrypoint. */
169 driSWRastCreateNewScreen(int scrn
, const __DRIextension
**extensions
,
170 const __DRIconfig
***driver_configs
, void *data
)
172 return dri2CreateNewScreen2(scrn
, -1, extensions
, NULL
,
173 driver_configs
, data
);
177 driSWRastCreateNewScreen2(int scrn
, const __DRIextension
**extensions
,
178 const __DRIextension
**driver_extensions
,
179 const __DRIconfig
***driver_configs
, void *data
)
181 return dri2CreateNewScreen2(scrn
, -1, extensions
, driver_extensions
,
182 driver_configs
, data
);
186 * Destroy the per-screen private information.
189 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
190 * drmClose(), and finally frees \p screenPrivate.
192 static void driDestroyScreen(__DRIscreen
*psp
)
195 /* No interaction with the X-server is possible at this point. This
196 * routine is called after XCloseDisplay, so there is no protocol
197 * stream open to the X-server anymore.
200 _mesa_destroy_shader_compiler();
202 psp
->driver
->DestroyScreen(psp
);
204 driDestroyOptionCache(&psp
->optionCache
);
205 driDestroyOptionInfo(&psp
->optionInfo
);
211 static const __DRIextension
**driGetExtensions(__DRIscreen
*psp
)
213 return psp
->extensions
;
220 validate_context_version(__DRIscreen
*screen
,
222 unsigned major_version
,
223 unsigned minor_version
,
224 unsigned *dri_ctx_error
)
226 unsigned req_version
= 10 * major_version
+ minor_version
;
227 unsigned max_version
= 0;
230 case API_OPENGL_COMPAT
:
231 max_version
= screen
->max_gl_compat_version
;
233 case API_OPENGL_CORE
:
234 max_version
= screen
->max_gl_core_version
;
237 max_version
= screen
->max_gl_es1_version
;
240 max_version
= screen
->max_gl_es2_version
;
247 if (max_version
== 0) {
248 *dri_ctx_error
= __DRI_CTX_ERROR_BAD_API
;
250 } else if (req_version
> max_version
) {
251 *dri_ctx_error
= __DRI_CTX_ERROR_BAD_VERSION
;
258 /*****************************************************************/
259 /** \name Context handling functions */
260 /*****************************************************************/
263 static __DRIcontext
*
264 dri2CreateContextAttribs(__DRIscreen
*screen
, int api
,
265 const __DRIconfig
*config
,
266 __DRIcontext
*shared
,
267 unsigned num_attribs
,
268 const uint32_t *attribs
,
272 __DRIcontext
*context
;
273 const struct gl_config
*modes
= (config
!= NULL
) ? &config
->modes
: NULL
;
274 void *shareCtx
= (shared
!= NULL
) ? shared
->driverPrivate
: NULL
;
276 unsigned major_version
= 1;
277 unsigned minor_version
= 0;
280 assert((num_attribs
== 0) || (attribs
!= NULL
));
282 if (!(screen
->api_mask
& (1 << api
))) {
283 *error
= __DRI_CTX_ERROR_BAD_API
;
288 case __DRI_API_OPENGL
:
289 mesa_api
= API_OPENGL_COMPAT
;
292 mesa_api
= API_OPENGLES
;
294 case __DRI_API_GLES2
:
295 case __DRI_API_GLES3
:
296 mesa_api
= API_OPENGLES2
;
298 case __DRI_API_OPENGL_CORE
:
299 mesa_api
= API_OPENGL_CORE
;
302 *error
= __DRI_CTX_ERROR_BAD_API
;
306 for (unsigned i
= 0; i
< num_attribs
; i
++) {
307 switch (attribs
[i
* 2]) {
308 case __DRI_CTX_ATTRIB_MAJOR_VERSION
:
309 major_version
= attribs
[i
* 2 + 1];
311 case __DRI_CTX_ATTRIB_MINOR_VERSION
:
312 minor_version
= attribs
[i
* 2 + 1];
314 case __DRI_CTX_ATTRIB_FLAGS
:
315 flags
= attribs
[i
* 2 + 1];
318 /* We can't create a context that satisfies the requirements of an
319 * attribute that we don't understand. Return failure.
321 assert(!"Should not get here.");
322 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
327 /* Mesa does not support the GL_ARB_compatibilty extension or the
328 * compatibility profile. This means that we treat a API_OPENGL_COMPAT 3.1 as
329 * API_OPENGL_CORE and reject API_OPENGL_COMPAT 3.2+.
331 if (mesa_api
== API_OPENGL_COMPAT
&& major_version
== 3 && minor_version
== 1)
332 mesa_api
= API_OPENGL_CORE
;
334 if (mesa_api
== API_OPENGL_COMPAT
335 && ((major_version
> 3)
336 || (major_version
== 3 && minor_version
>= 2))) {
337 *error
= __DRI_CTX_ERROR_BAD_API
;
341 /* The EGL_KHR_create_context spec says:
343 * "Flags are only defined for OpenGL context creation, and specifying
344 * a flags value other than zero for other types of contexts,
345 * including OpenGL ES contexts, will generate an error."
347 * The GLX_EXT_create_context_es2_profile specification doesn't say
348 * anything specific about this case. However, none of the known flags
349 * have any meaning in an ES context, so this seems safe.
351 if (mesa_api
!= API_OPENGL_COMPAT
352 && mesa_api
!= API_OPENGL_CORE
354 *error
= __DRI_CTX_ERROR_BAD_FLAG
;
358 /* There are no forward-compatible contexts before OpenGL 3.0. The
359 * GLX_ARB_create_context spec says:
361 * "Forward-compatible contexts are defined only for OpenGL versions
364 * Forward-looking contexts are supported by silently converting the
365 * requested API to API_OPENGL_CORE.
367 * In Mesa, a debug context is the same as a regular context.
369 if ((flags
& __DRI_CTX_FLAG_FORWARD_COMPATIBLE
) != 0) {
370 mesa_api
= API_OPENGL_CORE
;
373 if ((flags
& ~(__DRI_CTX_FLAG_DEBUG
| __DRI_CTX_FLAG_FORWARD_COMPATIBLE
))
375 *error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
379 if (!validate_context_version(screen
, mesa_api
,
380 major_version
, minor_version
, error
))
383 context
= calloc(1, sizeof *context
);
385 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
389 context
->loaderPrivate
= data
;
391 context
->driScreenPriv
= screen
;
392 context
->driDrawablePriv
= NULL
;
393 context
->driReadablePriv
= NULL
;
395 if (!screen
->driver
->CreateContext(mesa_api
, modes
, context
,
396 major_version
, minor_version
,
397 flags
, error
, shareCtx
) ) {
402 struct gl_context
*ctx
= context
->driverPrivate
;
403 if ((flags
& __DRI_CTX_FLAG_FORWARD_COMPATIBLE
) != 0)
404 ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT
;
405 if ((flags
& __DRI_CTX_FLAG_DEBUG
) != 0) {
406 ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_DEBUG_BIT
;
407 ctx
->Debug
.DebugOutput
= GL_TRUE
;
410 *error
= __DRI_CTX_ERROR_SUCCESS
;
414 static __DRIcontext
*
415 dri2CreateNewContextForAPI(__DRIscreen
*screen
, int api
,
416 const __DRIconfig
*config
,
417 __DRIcontext
*shared
, void *data
)
421 return dri2CreateContextAttribs(screen
, api
, config
, shared
, 0, NULL
,
425 static __DRIcontext
*
426 dri2CreateNewContext(__DRIscreen
*screen
, const __DRIconfig
*config
,
427 __DRIcontext
*shared
, void *data
)
429 return dri2CreateNewContextForAPI(screen
, __DRI_API_OPENGL
,
430 config
, shared
, data
);
434 * Destroy the per-context private information.
437 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
438 * drmDestroyContext(), and finally frees \p contextPrivate.
441 driDestroyContext(__DRIcontext
*pcp
)
444 pcp
->driScreenPriv
->driver
->DestroyContext(pcp
);
450 driCopyContext(__DRIcontext
*dest
, __DRIcontext
*src
, unsigned long mask
)
461 /*****************************************************************/
462 /** \name Context (un)binding functions */
463 /*****************************************************************/
466 static void dri_get_drawable(__DRIdrawable
*pdp
);
467 static void dri_put_drawable(__DRIdrawable
*pdp
);
470 * This function takes both a read buffer and a draw buffer. This is needed
471 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
474 static int driBindContext(__DRIcontext
*pcp
,
479 ** Assume error checking is done properly in glXMakeCurrent before
480 ** calling driUnbindContext.
486 /* Bind the drawable to the context */
487 pcp
->driDrawablePriv
= pdp
;
488 pcp
->driReadablePriv
= prp
;
490 pdp
->driContextPriv
= pcp
;
491 dri_get_drawable(pdp
);
493 if (prp
&& pdp
!= prp
) {
494 dri_get_drawable(prp
);
497 return pcp
->driScreenPriv
->driver
->MakeCurrent(pcp
, pdp
, prp
);
503 * \param scrn the screen.
506 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
509 * This function calls __DriverAPIRec::UnbindContext, and then decrements
510 * __DRIdrawableRec::refcount which must be non-zero for a successful
513 * While casting the opaque private pointers associated with the parameters
514 * into their respective real types it also assures they are not \c NULL.
516 static int driUnbindContext(__DRIcontext
*pcp
)
522 ** Assume error checking is done properly in glXMakeCurrent before
523 ** calling driUnbindContext.
529 pdp
= pcp
->driDrawablePriv
;
530 prp
= pcp
->driReadablePriv
;
532 /* already unbound */
536 pcp
->driScreenPriv
->driver
->UnbindContext(pcp
);
539 if (pdp
->refcount
== 0) {
544 dri_put_drawable(pdp
);
547 if (prp
->refcount
== 0) {
552 dri_put_drawable(prp
);
555 pcp
->driDrawablePriv
= NULL
;
556 pcp
->driReadablePriv
= NULL
;
564 static void dri_get_drawable(__DRIdrawable
*pdp
)
569 static void dri_put_drawable(__DRIdrawable
*pdp
)
576 pdp
->driScreenPriv
->driver
->DestroyBuffer(pdp
);
581 static __DRIdrawable
*
582 dri2CreateNewDrawable(__DRIscreen
*screen
,
583 const __DRIconfig
*config
,
586 __DRIdrawable
*pdraw
;
588 pdraw
= malloc(sizeof *pdraw
);
592 pdraw
->loaderPrivate
= data
;
594 pdraw
->driScreenPriv
= screen
;
595 pdraw
->driContextPriv
= NULL
;
597 pdraw
->lastStamp
= 0;
601 dri_get_drawable(pdraw
);
603 if (!screen
->driver
->CreateBuffer(screen
, pdraw
, &config
->modes
,
609 pdraw
->dri2
.stamp
= pdraw
->lastStamp
+ 1;
615 driDestroyDrawable(__DRIdrawable
*pdp
)
617 dri_put_drawable(pdp
);
621 dri2AllocateBuffer(__DRIscreen
*screen
,
622 unsigned int attachment
, unsigned int format
,
623 int width
, int height
)
625 return screen
->driver
->AllocateBuffer(screen
, attachment
, format
,
630 dri2ReleaseBuffer(__DRIscreen
*screen
, __DRIbuffer
*buffer
)
632 screen
->driver
->ReleaseBuffer(screen
, buffer
);
637 dri2ConfigQueryb(__DRIscreen
*screen
, const char *var
, GLboolean
*val
)
639 if (!driCheckOption(&screen
->optionCache
, var
, DRI_BOOL
))
642 *val
= driQueryOptionb(&screen
->optionCache
, var
);
648 dri2ConfigQueryi(__DRIscreen
*screen
, const char *var
, GLint
*val
)
650 if (!driCheckOption(&screen
->optionCache
, var
, DRI_INT
) &&
651 !driCheckOption(&screen
->optionCache
, var
, DRI_ENUM
))
654 *val
= driQueryOptioni(&screen
->optionCache
, var
);
660 dri2ConfigQueryf(__DRIscreen
*screen
, const char *var
, GLfloat
*val
)
662 if (!driCheckOption(&screen
->optionCache
, var
, DRI_FLOAT
))
665 *val
= driQueryOptionf(&screen
->optionCache
, var
);
671 dri2GetAPIMask(__DRIscreen
*screen
)
673 return screen
->api_mask
;
677 * swrast swapbuffers entrypoint.
679 * DRI2 implements this inside the loader with only flushes handled by the
683 driSwapBuffers(__DRIdrawable
*pdp
)
685 assert(pdp
->driScreenPriv
->swrast_loader
);
687 pdp
->driScreenPriv
->driver
->SwapBuffers(pdp
);
690 /** Core interface */
691 const __DRIcoreExtension driCoreExtension
= {
692 .base
= { __DRI_CORE
, __DRI_CORE_VERSION
},
694 .createNewScreen
= NULL
,
695 .destroyScreen
= driDestroyScreen
,
696 .getExtensions
= driGetExtensions
,
697 .getConfigAttrib
= driGetConfigAttrib
,
698 .indexConfigAttrib
= driIndexConfigAttrib
,
699 .createNewDrawable
= NULL
,
700 .destroyDrawable
= driDestroyDrawable
,
701 .swapBuffers
= driSwapBuffers
, /* swrast */
702 .createNewContext
= dri2CreateNewContext
, /* swrast */
703 .copyContext
= driCopyContext
,
704 .destroyContext
= driDestroyContext
,
705 .bindContext
= driBindContext
,
706 .unbindContext
= driUnbindContext
709 /** DRI2 interface */
710 const __DRIdri2Extension driDRI2Extension
= {
711 .base
= { __DRI_DRI2
, 4 },
713 .createNewScreen
= dri2CreateNewScreen
,
714 .createNewDrawable
= dri2CreateNewDrawable
,
715 .createNewContext
= dri2CreateNewContext
,
716 .getAPIMask
= dri2GetAPIMask
,
717 .createNewContextForAPI
= dri2CreateNewContextForAPI
,
718 .allocateBuffer
= dri2AllocateBuffer
,
719 .releaseBuffer
= dri2ReleaseBuffer
,
720 .createContextAttribs
= dri2CreateContextAttribs
,
721 .createNewScreen2
= dri2CreateNewScreen2
,
724 const __DRIswrastExtension driSWRastExtension
= {
726 driSWRastCreateNewScreen
,
727 dri2CreateNewDrawable
,
728 dri2CreateNewContextForAPI
,
729 dri2CreateContextAttribs
,
730 driSWRastCreateNewScreen2
,
733 const __DRI2configQueryExtension dri2ConfigQueryExtension
= {
734 .base
= { __DRI2_CONFIG_QUERY
, __DRI2_CONFIG_QUERY_VERSION
},
736 .configQueryb
= dri2ConfigQueryb
,
737 .configQueryi
= dri2ConfigQueryi
,
738 .configQueryf
= dri2ConfigQueryf
,
742 dri2InvalidateDrawable(__DRIdrawable
*drawable
)
744 drawable
->dri2
.stamp
++;
748 * Check that the gl_framebuffer associated with dPriv is the right size.
749 * Resize the gl_framebuffer if needed.
750 * It's expected that the dPriv->driverPrivate member points to a
751 * gl_framebuffer object.
754 driUpdateFramebufferSize(struct gl_context
*ctx
, const __DRIdrawable
*dPriv
)
756 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) dPriv
->driverPrivate
;
757 if (fb
&& (dPriv
->w
!= fb
->Width
|| dPriv
->h
!= fb
->Height
)) {
758 ctx
->Driver
.ResizeBuffers(ctx
, fb
, dPriv
->w
, dPriv
->h
);
759 /* if the driver needs the hw lock for ResizeBuffers, the drawable
760 might have changed again by now */
761 assert(fb
->Width
== dPriv
->w
);
762 assert(fb
->Height
== dPriv
->h
);