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 dri2CreateNewScreen(int scrn
, int fd
,
78 const __DRIextension
**extensions
,
79 const __DRIconfig
***driver_configs
, void *data
)
81 static const __DRIextension
*emptyExtensionList
[] = { NULL
};
83 drmVersionPtr version
;
85 psp
= calloc(1, sizeof(*psp
));
89 setupLoaderExtensions(psp
, extensions
);
91 version
= drmGetVersion(fd
);
93 psp
->drm_version
.major
= version
->version_major
;
94 psp
->drm_version
.minor
= version
->version_minor
;
95 psp
->drm_version
.patch
= version
->version_patchlevel
;
96 drmFreeVersion(version
);
99 psp
->loaderPrivate
= data
;
101 psp
->extensions
= emptyExtensionList
;
105 psp
->api_mask
= (1 << __DRI_API_OPENGL
);
107 *driver_configs
= driDriverAPI
.InitScreen(psp
);
108 if (*driver_configs
== NULL
) {
113 driParseOptionInfo(&psp
->optionInfo
, __dri2ConfigOptions
);
114 driParseConfigFiles(&psp
->optionCache
, &psp
->optionInfo
, psp
->myNum
, "dri2");
120 * Destroy the per-screen private information.
123 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
124 * drmClose(), and finally frees \p screenPrivate.
126 static void driDestroyScreen(__DRIscreen
*psp
)
129 /* No interaction with the X-server is possible at this point. This
130 * routine is called after XCloseDisplay, so there is no protocol
131 * stream open to the X-server anymore.
134 _mesa_destroy_shader_compiler();
136 driDriverAPI
.DestroyScreen(psp
);
138 driDestroyOptionCache(&psp
->optionCache
);
139 driDestroyOptionInfo(&psp
->optionInfo
);
145 static const __DRIextension
**driGetExtensions(__DRIscreen
*psp
)
147 return psp
->extensions
;
153 /*****************************************************************/
154 /** \name Context handling functions */
155 /*****************************************************************/
158 static __DRIcontext
*
159 dri2CreateContextAttribs(__DRIscreen
*screen
, int api
,
160 const __DRIconfig
*config
,
161 __DRIcontext
*shared
,
162 unsigned num_attribs
,
163 const uint32_t *attribs
,
167 __DRIcontext
*context
;
168 const struct gl_config
*modes
= (config
!= NULL
) ? &config
->modes
: NULL
;
169 void *shareCtx
= (shared
!= NULL
) ? shared
->driverPrivate
: NULL
;
171 unsigned major_version
= 1;
172 unsigned minor_version
= 0;
175 assert((num_attribs
== 0) || (attribs
!= NULL
));
177 if (!(screen
->api_mask
& (1 << api
))) {
178 *error
= __DRI_CTX_ERROR_BAD_API
;
183 case __DRI_API_OPENGL
:
184 mesa_api
= API_OPENGL_COMPAT
;
187 mesa_api
= API_OPENGLES
;
189 case __DRI_API_GLES2
:
190 case __DRI_API_GLES3
:
191 mesa_api
= API_OPENGLES2
;
193 case __DRI_API_OPENGL_CORE
:
194 mesa_api
= API_OPENGL_CORE
;
197 *error
= __DRI_CTX_ERROR_BAD_API
;
201 for (unsigned i
= 0; i
< num_attribs
; i
++) {
202 switch (attribs
[i
* 2]) {
203 case __DRI_CTX_ATTRIB_MAJOR_VERSION
:
204 major_version
= attribs
[i
* 2 + 1];
206 case __DRI_CTX_ATTRIB_MINOR_VERSION
:
207 minor_version
= attribs
[i
* 2 + 1];
209 case __DRI_CTX_ATTRIB_FLAGS
:
210 flags
= attribs
[i
* 2 + 1];
213 /* We can't create a context that satisfies the requirements of an
214 * attribute that we don't understand. Return failure.
216 assert(!"Should not get here.");
217 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
222 /* Mesa does not support the GL_ARB_compatibilty extension or the
223 * compatibility profile. This means that we treat a API_OPENGL_COMPAT 3.1 as
224 * API_OPENGL_CORE and reject API_OPENGL_COMPAT 3.2+.
226 if (mesa_api
== API_OPENGL_COMPAT
&& major_version
== 3 && minor_version
== 1)
227 mesa_api
= API_OPENGL_CORE
;
229 if (mesa_api
== API_OPENGL_COMPAT
230 && ((major_version
> 3)
231 || (major_version
== 3 && minor_version
>= 2))) {
232 *error
= __DRI_CTX_ERROR_BAD_API
;
236 /* The EGL_KHR_create_context spec says:
238 * "Flags are only defined for OpenGL context creation, and specifying
239 * a flags value other than zero for other types of contexts,
240 * including OpenGL ES contexts, will generate an error."
242 * The GLX_EXT_create_context_es2_profile specification doesn't say
243 * anything specific about this case. However, none of the known flags
244 * have any meaning in an ES context, so this seems safe.
246 if (mesa_api
!= API_OPENGL_COMPAT
247 && mesa_api
!= API_OPENGL_CORE
249 *error
= __DRI_CTX_ERROR_BAD_FLAG
;
253 /* There are no forward-compatible contexts before OpenGL 3.0. The
254 * GLX_ARB_create_context spec says:
256 * "Forward-compatible contexts are defined only for OpenGL versions
259 * Forward-looking contexts are supported by silently converting the
260 * requested API to API_OPENGL_CORE.
262 * In Mesa, a debug context is the same as a regular context.
264 if ((flags
& __DRI_CTX_FLAG_FORWARD_COMPATIBLE
) != 0) {
265 mesa_api
= API_OPENGL_CORE
;
268 if ((flags
& ~(__DRI_CTX_FLAG_DEBUG
| __DRI_CTX_FLAG_FORWARD_COMPATIBLE
))
270 *error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
274 context
= calloc(1, sizeof *context
);
276 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
280 context
->loaderPrivate
= data
;
282 context
->driScreenPriv
= screen
;
283 context
->driDrawablePriv
= NULL
;
284 context
->driReadablePriv
= NULL
;
286 if (!driDriverAPI
.CreateContext(mesa_api
, modes
, context
,
287 major_version
, minor_version
,
288 flags
, error
, shareCtx
) ) {
293 *error
= __DRI_CTX_ERROR_SUCCESS
;
297 static __DRIcontext
*
298 dri2CreateNewContextForAPI(__DRIscreen
*screen
, int api
,
299 const __DRIconfig
*config
,
300 __DRIcontext
*shared
, void *data
)
304 return dri2CreateContextAttribs(screen
, api
, config
, shared
, 0, NULL
,
308 static __DRIcontext
*
309 dri2CreateNewContext(__DRIscreen
*screen
, const __DRIconfig
*config
,
310 __DRIcontext
*shared
, void *data
)
312 return dri2CreateNewContextForAPI(screen
, __DRI_API_OPENGL
,
313 config
, shared
, data
);
317 * Destroy the per-context private information.
320 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
321 * drmDestroyContext(), and finally frees \p contextPrivate.
324 driDestroyContext(__DRIcontext
*pcp
)
327 driDriverAPI
.DestroyContext(pcp
);
333 driCopyContext(__DRIcontext
*dest
, __DRIcontext
*src
, unsigned long mask
)
344 /*****************************************************************/
345 /** \name Context (un)binding functions */
346 /*****************************************************************/
349 static void dri_get_drawable(__DRIdrawable
*pdp
);
350 static void dri_put_drawable(__DRIdrawable
*pdp
);
353 * This function takes both a read buffer and a draw buffer. This is needed
354 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
357 static int driBindContext(__DRIcontext
*pcp
,
362 ** Assume error checking is done properly in glXMakeCurrent before
363 ** calling driUnbindContext.
369 /* Bind the drawable to the context */
370 pcp
->driDrawablePriv
= pdp
;
371 pcp
->driReadablePriv
= prp
;
373 pdp
->driContextPriv
= pcp
;
374 dri_get_drawable(pdp
);
376 if (prp
&& pdp
!= prp
) {
377 dri_get_drawable(prp
);
380 return driDriverAPI
.MakeCurrent(pcp
, pdp
, prp
);
386 * \param scrn the screen.
389 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
392 * This function calls __DriverAPIRec::UnbindContext, and then decrements
393 * __DRIdrawableRec::refcount which must be non-zero for a successful
396 * While casting the opaque private pointers associated with the parameters
397 * into their respective real types it also assures they are not \c NULL.
399 static int driUnbindContext(__DRIcontext
*pcp
)
405 ** Assume error checking is done properly in glXMakeCurrent before
406 ** calling driUnbindContext.
412 pdp
= pcp
->driDrawablePriv
;
413 prp
= pcp
->driReadablePriv
;
415 /* already unbound */
419 driDriverAPI
.UnbindContext(pcp
);
422 if (pdp
->refcount
== 0) {
427 dri_put_drawable(pdp
);
430 if (prp
->refcount
== 0) {
435 dri_put_drawable(prp
);
438 pcp
->driDrawablePriv
= NULL
;
439 pcp
->driReadablePriv
= NULL
;
447 static void dri_get_drawable(__DRIdrawable
*pdp
)
452 static void dri_put_drawable(__DRIdrawable
*pdp
)
459 driDriverAPI
.DestroyBuffer(pdp
);
464 static __DRIdrawable
*
465 dri2CreateNewDrawable(__DRIscreen
*screen
,
466 const __DRIconfig
*config
,
469 __DRIdrawable
*pdraw
;
471 pdraw
= malloc(sizeof *pdraw
);
475 pdraw
->loaderPrivate
= data
;
477 pdraw
->driScreenPriv
= screen
;
478 pdraw
->driContextPriv
= NULL
;
480 pdraw
->lastStamp
= 0;
484 dri_get_drawable(pdraw
);
486 if (!driDriverAPI
.CreateBuffer(screen
, pdraw
, &config
->modes
, GL_FALSE
)) {
491 pdraw
->dri2
.stamp
= pdraw
->lastStamp
+ 1;
497 driDestroyDrawable(__DRIdrawable
*pdp
)
499 dri_put_drawable(pdp
);
503 dri2AllocateBuffer(__DRIscreen
*screen
,
504 unsigned int attachment
, unsigned int format
,
505 int width
, int height
)
507 return driDriverAPI
.AllocateBuffer(screen
, attachment
, format
,
512 dri2ReleaseBuffer(__DRIscreen
*screen
, __DRIbuffer
*buffer
)
514 driDriverAPI
.ReleaseBuffer(screen
, buffer
);
519 dri2ConfigQueryb(__DRIscreen
*screen
, const char *var
, GLboolean
*val
)
521 if (!driCheckOption(&screen
->optionCache
, var
, DRI_BOOL
))
524 *val
= driQueryOptionb(&screen
->optionCache
, var
);
530 dri2ConfigQueryi(__DRIscreen
*screen
, const char *var
, GLint
*val
)
532 if (!driCheckOption(&screen
->optionCache
, var
, DRI_INT
) &&
533 !driCheckOption(&screen
->optionCache
, var
, DRI_ENUM
))
536 *val
= driQueryOptioni(&screen
->optionCache
, var
);
542 dri2ConfigQueryf(__DRIscreen
*screen
, const char *var
, GLfloat
*val
)
544 if (!driCheckOption(&screen
->optionCache
, var
, DRI_FLOAT
))
547 *val
= driQueryOptionf(&screen
->optionCache
, var
);
553 dri2GetAPIMask(__DRIscreen
*screen
)
555 return screen
->api_mask
;
559 /** Core interface */
560 const __DRIcoreExtension driCoreExtension
= {
561 .base
= { __DRI_CORE
, __DRI_CORE_VERSION
},
563 .createNewScreen
= NULL
,
564 .destroyScreen
= driDestroyScreen
,
565 .getExtensions
= driGetExtensions
,
566 .getConfigAttrib
= driGetConfigAttrib
,
567 .indexConfigAttrib
= driIndexConfigAttrib
,
568 .createNewDrawable
= NULL
,
569 .destroyDrawable
= driDestroyDrawable
,
571 .createNewContext
= NULL
,
572 .copyContext
= driCopyContext
,
573 .destroyContext
= driDestroyContext
,
574 .bindContext
= driBindContext
,
575 .unbindContext
= driUnbindContext
578 /** DRI2 interface */
579 const __DRIdri2Extension driDRI2Extension
= {
580 .base
= { __DRI_DRI2
, 3 },
582 .createNewScreen
= dri2CreateNewScreen
,
583 .createNewDrawable
= dri2CreateNewDrawable
,
584 .createNewContext
= dri2CreateNewContext
,
585 .getAPIMask
= dri2GetAPIMask
,
586 .createNewContextForAPI
= dri2CreateNewContextForAPI
,
587 .allocateBuffer
= dri2AllocateBuffer
,
588 .releaseBuffer
= dri2ReleaseBuffer
,
589 .createContextAttribs
= dri2CreateContextAttribs
592 const __DRI2configQueryExtension dri2ConfigQueryExtension
= {
593 .base
= { __DRI2_CONFIG_QUERY
, __DRI2_CONFIG_QUERY_VERSION
},
595 .configQueryb
= dri2ConfigQueryb
,
596 .configQueryi
= dri2ConfigQueryi
,
597 .configQueryf
= dri2ConfigQueryf
,
601 dri2InvalidateDrawable(__DRIdrawable
*drawable
)
603 drawable
->dri2
.stamp
++;
607 * Check that the gl_framebuffer associated with dPriv is the right size.
608 * Resize the gl_framebuffer if needed.
609 * It's expected that the dPriv->driverPrivate member points to a
610 * gl_framebuffer object.
613 driUpdateFramebufferSize(struct gl_context
*ctx
, const __DRIdrawable
*dPriv
)
615 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) dPriv
->driverPrivate
;
616 if (fb
&& (dPriv
->w
!= fb
->Width
|| dPriv
->h
!= fb
->Height
)) {
617 ctx
->Driver
.ResizeBuffers(ctx
, fb
, dPriv
->w
, dPriv
->h
);
618 /* if the driver needs the hw lock for ResizeBuffers, the drawable
619 might have changed again by now */
620 assert(fb
->Width
== dPriv
->w
);
621 assert(fb
->Height
== dPriv
->h
);