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
46 #include "../glsl/glsl_parser_extras.h"
48 PUBLIC
const char __dri2ConfigOptions
[] =
50 DRI_CONF_SECTION_PERFORMANCE
51 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_1
)
55 /*****************************************************************/
56 /** \name Screen handling functions */
57 /*****************************************************************/
61 setupLoaderExtensions(__DRIscreen
*psp
,
62 const __DRIextension
**extensions
)
66 for (i
= 0; extensions
[i
]; i
++) {
67 if (strcmp(extensions
[i
]->name
, __DRI_DRI2_LOADER
) == 0)
68 psp
->dri2
.loader
= (__DRIdri2LoaderExtension
*) extensions
[i
];
69 if (strcmp(extensions
[i
]->name
, __DRI_IMAGE_LOOKUP
) == 0)
70 psp
->dri2
.image
= (__DRIimageLookupExtension
*) extensions
[i
];
71 if (strcmp(extensions
[i
]->name
, __DRI_USE_INVALIDATE
) == 0)
72 psp
->dri2
.useInvalidate
= (__DRIuseInvalidateExtension
*) extensions
[i
];
77 * This is the first entrypoint in the driver called by the DRI driver loader
78 * after dlopen()ing it.
80 * It's used to create global state for the driver across contexts on the same
84 dri2CreateNewScreen(int scrn
, int fd
,
85 const __DRIextension
**extensions
,
86 const __DRIconfig
***driver_configs
, void *data
)
88 static const __DRIextension
*emptyExtensionList
[] = { NULL
};
90 drmVersionPtr version
;
92 psp
= calloc(1, sizeof(*psp
));
96 setupLoaderExtensions(psp
, extensions
);
98 version
= drmGetVersion(fd
);
100 psp
->drm_version
.major
= version
->version_major
;
101 psp
->drm_version
.minor
= version
->version_minor
;
102 psp
->drm_version
.patch
= version
->version_patchlevel
;
103 drmFreeVersion(version
);
106 psp
->loaderPrivate
= data
;
108 psp
->extensions
= emptyExtensionList
;
112 psp
->api_mask
= (1 << __DRI_API_OPENGL
);
114 *driver_configs
= driDriverAPI
.InitScreen(psp
);
115 if (*driver_configs
== NULL
) {
120 driParseOptionInfo(&psp
->optionInfo
, __dri2ConfigOptions
);
121 driParseConfigFiles(&psp
->optionCache
, &psp
->optionInfo
, psp
->myNum
, "dri2");
127 * Destroy the per-screen private information.
130 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
131 * drmClose(), and finally frees \p screenPrivate.
133 static void driDestroyScreen(__DRIscreen
*psp
)
136 /* No interaction with the X-server is possible at this point. This
137 * routine is called after XCloseDisplay, so there is no protocol
138 * stream open to the X-server anymore.
141 _mesa_destroy_shader_compiler();
143 driDriverAPI
.DestroyScreen(psp
);
145 driDestroyOptionCache(&psp
->optionCache
);
146 driDestroyOptionInfo(&psp
->optionInfo
);
152 static const __DRIextension
**driGetExtensions(__DRIscreen
*psp
)
154 return psp
->extensions
;
160 /*****************************************************************/
161 /** \name Context handling functions */
162 /*****************************************************************/
165 static __DRIcontext
*
166 dri2CreateContextAttribs(__DRIscreen
*screen
, int api
,
167 const __DRIconfig
*config
,
168 __DRIcontext
*shared
,
169 unsigned num_attribs
,
170 const uint32_t *attribs
,
174 __DRIcontext
*context
;
175 const struct gl_config
*modes
= (config
!= NULL
) ? &config
->modes
: NULL
;
176 void *shareCtx
= (shared
!= NULL
) ? shared
->driverPrivate
: NULL
;
178 unsigned major_version
= 1;
179 unsigned minor_version
= 0;
182 assert((num_attribs
== 0) || (attribs
!= NULL
));
184 if (!(screen
->api_mask
& (1 << api
))) {
185 *error
= __DRI_CTX_ERROR_BAD_API
;
190 case __DRI_API_OPENGL
:
191 mesa_api
= API_OPENGL_COMPAT
;
194 mesa_api
= API_OPENGLES
;
196 case __DRI_API_GLES2
:
197 case __DRI_API_GLES3
:
198 mesa_api
= API_OPENGLES2
;
200 case __DRI_API_OPENGL_CORE
:
201 mesa_api
= API_OPENGL_CORE
;
204 *error
= __DRI_CTX_ERROR_BAD_API
;
208 for (unsigned i
= 0; i
< num_attribs
; i
++) {
209 switch (attribs
[i
* 2]) {
210 case __DRI_CTX_ATTRIB_MAJOR_VERSION
:
211 major_version
= attribs
[i
* 2 + 1];
213 case __DRI_CTX_ATTRIB_MINOR_VERSION
:
214 minor_version
= attribs
[i
* 2 + 1];
216 case __DRI_CTX_ATTRIB_FLAGS
:
217 flags
= attribs
[i
* 2 + 1];
220 /* We can't create a context that satisfies the requirements of an
221 * attribute that we don't understand. Return failure.
223 assert(!"Should not get here.");
224 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
229 /* Mesa does not support the GL_ARB_compatibilty extension or the
230 * compatibility profile. This means that we treat a API_OPENGL_COMPAT 3.1 as
231 * API_OPENGL_CORE and reject API_OPENGL_COMPAT 3.2+.
233 if (mesa_api
== API_OPENGL_COMPAT
&& major_version
== 3 && minor_version
== 1)
234 mesa_api
= API_OPENGL_CORE
;
236 if (mesa_api
== API_OPENGL_COMPAT
237 && ((major_version
> 3)
238 || (major_version
== 3 && minor_version
>= 2))) {
239 *error
= __DRI_CTX_ERROR_BAD_API
;
243 /* The EGL_KHR_create_context spec says:
245 * "Flags are only defined for OpenGL context creation, and specifying
246 * a flags value other than zero for other types of contexts,
247 * including OpenGL ES contexts, will generate an error."
249 * The GLX_EXT_create_context_es2_profile specification doesn't say
250 * anything specific about this case. However, none of the known flags
251 * have any meaning in an ES context, so this seems safe.
253 if (mesa_api
!= API_OPENGL_COMPAT
254 && mesa_api
!= API_OPENGL_CORE
256 *error
= __DRI_CTX_ERROR_BAD_FLAG
;
260 /* There are no forward-compatible contexts before OpenGL 3.0. The
261 * GLX_ARB_create_context spec says:
263 * "Forward-compatible contexts are defined only for OpenGL versions
266 * Forward-looking contexts are supported by silently converting the
267 * requested API to API_OPENGL_CORE.
269 * In Mesa, a debug context is the same as a regular context.
271 if ((flags
& __DRI_CTX_FLAG_FORWARD_COMPATIBLE
) != 0) {
272 mesa_api
= API_OPENGL_CORE
;
275 if ((flags
& ~(__DRI_CTX_FLAG_DEBUG
| __DRI_CTX_FLAG_FORWARD_COMPATIBLE
))
277 *error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
281 context
= calloc(1, sizeof *context
);
283 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
287 context
->loaderPrivate
= data
;
289 context
->driScreenPriv
= screen
;
290 context
->driDrawablePriv
= NULL
;
291 context
->driReadablePriv
= NULL
;
293 if (!driDriverAPI
.CreateContext(mesa_api
, modes
, context
,
294 major_version
, minor_version
,
295 flags
, error
, shareCtx
) ) {
300 *error
= __DRI_CTX_ERROR_SUCCESS
;
304 static __DRIcontext
*
305 dri2CreateNewContextForAPI(__DRIscreen
*screen
, int api
,
306 const __DRIconfig
*config
,
307 __DRIcontext
*shared
, void *data
)
311 return dri2CreateContextAttribs(screen
, api
, config
, shared
, 0, NULL
,
315 static __DRIcontext
*
316 dri2CreateNewContext(__DRIscreen
*screen
, const __DRIconfig
*config
,
317 __DRIcontext
*shared
, void *data
)
319 return dri2CreateNewContextForAPI(screen
, __DRI_API_OPENGL
,
320 config
, shared
, data
);
324 * Destroy the per-context private information.
327 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
328 * drmDestroyContext(), and finally frees \p contextPrivate.
331 driDestroyContext(__DRIcontext
*pcp
)
334 driDriverAPI
.DestroyContext(pcp
);
340 driCopyContext(__DRIcontext
*dest
, __DRIcontext
*src
, unsigned long mask
)
351 /*****************************************************************/
352 /** \name Context (un)binding functions */
353 /*****************************************************************/
356 static void dri_get_drawable(__DRIdrawable
*pdp
);
357 static void dri_put_drawable(__DRIdrawable
*pdp
);
360 * This function takes both a read buffer and a draw buffer. This is needed
361 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
364 static int driBindContext(__DRIcontext
*pcp
,
369 ** Assume error checking is done properly in glXMakeCurrent before
370 ** calling driUnbindContext.
376 /* Bind the drawable to the context */
377 pcp
->driDrawablePriv
= pdp
;
378 pcp
->driReadablePriv
= prp
;
380 pdp
->driContextPriv
= pcp
;
381 dri_get_drawable(pdp
);
383 if (prp
&& pdp
!= prp
) {
384 dri_get_drawable(prp
);
387 return driDriverAPI
.MakeCurrent(pcp
, pdp
, prp
);
393 * \param scrn the screen.
396 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
399 * This function calls __DriverAPIRec::UnbindContext, and then decrements
400 * __DRIdrawableRec::refcount which must be non-zero for a successful
403 * While casting the opaque private pointers associated with the parameters
404 * into their respective real types it also assures they are not \c NULL.
406 static int driUnbindContext(__DRIcontext
*pcp
)
412 ** Assume error checking is done properly in glXMakeCurrent before
413 ** calling driUnbindContext.
419 pdp
= pcp
->driDrawablePriv
;
420 prp
= pcp
->driReadablePriv
;
422 /* already unbound */
426 driDriverAPI
.UnbindContext(pcp
);
429 if (pdp
->refcount
== 0) {
434 dri_put_drawable(pdp
);
437 if (prp
->refcount
== 0) {
442 dri_put_drawable(prp
);
445 pcp
->driDrawablePriv
= NULL
;
446 pcp
->driReadablePriv
= NULL
;
454 static void dri_get_drawable(__DRIdrawable
*pdp
)
459 static void dri_put_drawable(__DRIdrawable
*pdp
)
466 driDriverAPI
.DestroyBuffer(pdp
);
471 static __DRIdrawable
*
472 dri2CreateNewDrawable(__DRIscreen
*screen
,
473 const __DRIconfig
*config
,
476 __DRIdrawable
*pdraw
;
478 pdraw
= malloc(sizeof *pdraw
);
482 pdraw
->loaderPrivate
= data
;
484 pdraw
->driScreenPriv
= screen
;
485 pdraw
->driContextPriv
= NULL
;
487 pdraw
->lastStamp
= 0;
491 dri_get_drawable(pdraw
);
493 if (!driDriverAPI
.CreateBuffer(screen
, pdraw
, &config
->modes
, GL_FALSE
)) {
498 pdraw
->dri2
.stamp
= pdraw
->lastStamp
+ 1;
504 driDestroyDrawable(__DRIdrawable
*pdp
)
506 dri_put_drawable(pdp
);
510 dri2AllocateBuffer(__DRIscreen
*screen
,
511 unsigned int attachment
, unsigned int format
,
512 int width
, int height
)
514 return driDriverAPI
.AllocateBuffer(screen
, attachment
, format
,
519 dri2ReleaseBuffer(__DRIscreen
*screen
, __DRIbuffer
*buffer
)
521 driDriverAPI
.ReleaseBuffer(screen
, buffer
);
526 dri2ConfigQueryb(__DRIscreen
*screen
, const char *var
, GLboolean
*val
)
528 if (!driCheckOption(&screen
->optionCache
, var
, DRI_BOOL
))
531 *val
= driQueryOptionb(&screen
->optionCache
, var
);
537 dri2ConfigQueryi(__DRIscreen
*screen
, const char *var
, GLint
*val
)
539 if (!driCheckOption(&screen
->optionCache
, var
, DRI_INT
) &&
540 !driCheckOption(&screen
->optionCache
, var
, DRI_ENUM
))
543 *val
= driQueryOptioni(&screen
->optionCache
, var
);
549 dri2ConfigQueryf(__DRIscreen
*screen
, const char *var
, GLfloat
*val
)
551 if (!driCheckOption(&screen
->optionCache
, var
, DRI_FLOAT
))
554 *val
= driQueryOptionf(&screen
->optionCache
, var
);
560 dri2GetAPIMask(__DRIscreen
*screen
)
562 return screen
->api_mask
;
566 /** Core interface */
567 const __DRIcoreExtension driCoreExtension
= {
568 .base
= { __DRI_CORE
, __DRI_CORE_VERSION
},
570 .createNewScreen
= NULL
,
571 .destroyScreen
= driDestroyScreen
,
572 .getExtensions
= driGetExtensions
,
573 .getConfigAttrib
= driGetConfigAttrib
,
574 .indexConfigAttrib
= driIndexConfigAttrib
,
575 .createNewDrawable
= NULL
,
576 .destroyDrawable
= driDestroyDrawable
,
578 .createNewContext
= NULL
,
579 .copyContext
= driCopyContext
,
580 .destroyContext
= driDestroyContext
,
581 .bindContext
= driBindContext
,
582 .unbindContext
= driUnbindContext
585 /** DRI2 interface */
586 const __DRIdri2Extension driDRI2Extension
= {
587 .base
= { __DRI_DRI2
, 3 },
589 .createNewScreen
= dri2CreateNewScreen
,
590 .createNewDrawable
= dri2CreateNewDrawable
,
591 .createNewContext
= dri2CreateNewContext
,
592 .getAPIMask
= dri2GetAPIMask
,
593 .createNewContextForAPI
= dri2CreateNewContextForAPI
,
594 .allocateBuffer
= dri2AllocateBuffer
,
595 .releaseBuffer
= dri2ReleaseBuffer
,
596 .createContextAttribs
= dri2CreateContextAttribs
599 const __DRI2configQueryExtension dri2ConfigQueryExtension
= {
600 .base
= { __DRI2_CONFIG_QUERY
, __DRI2_CONFIG_QUERY_VERSION
},
602 .configQueryb
= dri2ConfigQueryb
,
603 .configQueryi
= dri2ConfigQueryi
,
604 .configQueryf
= dri2ConfigQueryf
,
608 dri2InvalidateDrawable(__DRIdrawable
*drawable
)
610 drawable
->dri2
.stamp
++;
614 * Check that the gl_framebuffer associated with dPriv is the right size.
615 * Resize the gl_framebuffer if needed.
616 * It's expected that the dPriv->driverPrivate member points to a
617 * gl_framebuffer object.
620 driUpdateFramebufferSize(struct gl_context
*ctx
, const __DRIdrawable
*dPriv
)
622 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) dPriv
->driverPrivate
;
623 if (fb
&& (dPriv
->w
!= fb
->Width
|| dPriv
->h
!= fb
->Height
)) {
624 ctx
->Driver
.ResizeBuffers(ctx
, fb
, dPriv
->w
, dPriv
->h
);
625 /* if the driver needs the hw lock for ResizeBuffers, the drawable
626 might have changed again by now */
627 assert(fb
->Width
== dPriv
->w
);
628 assert(fb
->Height
== dPriv
->h
);