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 static const uint __dri2NConfigOptions
= 1;
57 /*****************************************************************/
58 /** \name Screen handling functions */
59 /*****************************************************************/
63 setupLoaderExtensions(__DRIscreen
*psp
,
64 const __DRIextension
**extensions
)
68 for (i
= 0; extensions
[i
]; i
++) {
69 if (strcmp(extensions
[i
]->name
, __DRI_DRI2_LOADER
) == 0)
70 psp
->dri2
.loader
= (__DRIdri2LoaderExtension
*) extensions
[i
];
71 if (strcmp(extensions
[i
]->name
, __DRI_IMAGE_LOOKUP
) == 0)
72 psp
->dri2
.image
= (__DRIimageLookupExtension
*) extensions
[i
];
73 if (strcmp(extensions
[i
]->name
, __DRI_USE_INVALIDATE
) == 0)
74 psp
->dri2
.useInvalidate
= (__DRIuseInvalidateExtension
*) extensions
[i
];
79 dri2CreateNewScreen(int scrn
, int fd
,
80 const __DRIextension
**extensions
,
81 const __DRIconfig
***driver_configs
, void *data
)
83 static const __DRIextension
*emptyExtensionList
[] = { NULL
};
85 drmVersionPtr version
;
87 psp
= calloc(1, sizeof(*psp
));
91 setupLoaderExtensions(psp
, extensions
);
93 version
= drmGetVersion(fd
);
95 psp
->drm_version
.major
= version
->version_major
;
96 psp
->drm_version
.minor
= version
->version_minor
;
97 psp
->drm_version
.patch
= version
->version_patchlevel
;
98 drmFreeVersion(version
);
101 psp
->loaderPrivate
= data
;
103 psp
->extensions
= emptyExtensionList
;
107 psp
->api_mask
= (1 << __DRI_API_OPENGL
);
109 *driver_configs
= driDriverAPI
.InitScreen(psp
);
110 if (*driver_configs
== NULL
) {
115 driParseOptionInfo(&psp
->optionInfo
, __dri2ConfigOptions
, __dri2NConfigOptions
);
116 driParseConfigFiles(&psp
->optionCache
, &psp
->optionInfo
, psp
->myNum
, "dri2");
122 * Destroy the per-screen private information.
125 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
126 * drmClose(), and finally frees \p screenPrivate.
128 static void driDestroyScreen(__DRIscreen
*psp
)
131 /* No interaction with the X-server is possible at this point. This
132 * routine is called after XCloseDisplay, so there is no protocol
133 * stream open to the X-server anymore.
136 _mesa_destroy_shader_compiler();
138 driDriverAPI
.DestroyScreen(psp
);
140 driDestroyOptionCache(&psp
->optionCache
);
141 driDestroyOptionInfo(&psp
->optionInfo
);
147 static const __DRIextension
**driGetExtensions(__DRIscreen
*psp
)
149 return psp
->extensions
;
155 /*****************************************************************/
156 /** \name Context handling functions */
157 /*****************************************************************/
160 static __DRIcontext
*
161 dri2CreateContextAttribs(__DRIscreen
*screen
, int api
,
162 const __DRIconfig
*config
,
163 __DRIcontext
*shared
,
164 unsigned num_attribs
,
165 const uint32_t *attribs
,
169 __DRIcontext
*context
;
170 const struct gl_config
*modes
= (config
!= NULL
) ? &config
->modes
: NULL
;
171 void *shareCtx
= (shared
!= NULL
) ? shared
->driverPrivate
: NULL
;
173 unsigned major_version
= 1;
174 unsigned minor_version
= 0;
177 assert((num_attribs
== 0) || (attribs
!= NULL
));
179 if (!(screen
->api_mask
& (1 << api
))) {
180 *error
= __DRI_CTX_ERROR_BAD_API
;
185 case __DRI_API_OPENGL
:
186 mesa_api
= API_OPENGL_COMPAT
;
189 mesa_api
= API_OPENGLES
;
191 case __DRI_API_GLES2
:
192 mesa_api
= API_OPENGLES2
;
194 case __DRI_API_OPENGL_CORE
:
195 mesa_api
= API_OPENGL_CORE
;
198 *error
= __DRI_CTX_ERROR_BAD_API
;
202 for (unsigned i
= 0; i
< num_attribs
; i
++) {
203 switch (attribs
[i
* 2]) {
204 case __DRI_CTX_ATTRIB_MAJOR_VERSION
:
205 major_version
= attribs
[i
* 2 + 1];
207 case __DRI_CTX_ATTRIB_MINOR_VERSION
:
208 minor_version
= attribs
[i
* 2 + 1];
210 case __DRI_CTX_ATTRIB_FLAGS
:
211 flags
= attribs
[i
* 2 + 1];
214 /* We can't create a context that satisfies the requirements of an
215 * attribute that we don't understand. Return failure.
217 assert(!"Should not get here.");
218 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
223 /* Mesa does not support the GL_ARB_compatibilty extension or the
224 * compatibility profile. This means that we treat a API_OPENGL_COMPAT 3.1 as
225 * API_OPENGL_CORE and reject API_OPENGL_COMPAT 3.2+.
227 if (mesa_api
== API_OPENGL_COMPAT
&& major_version
== 3 && minor_version
== 1)
228 mesa_api
= API_OPENGL_CORE
;
230 if (mesa_api
== API_OPENGL_COMPAT
231 && ((major_version
> 3)
232 || (major_version
== 3 && minor_version
>= 2))) {
233 *error
= __DRI_CTX_ERROR_BAD_API
;
237 /* The EGL_KHR_create_context spec says:
239 * "Flags are only defined for OpenGL context creation, and specifying
240 * a flags value other than zero for other types of contexts,
241 * including OpenGL ES contexts, will generate an error."
243 * The GLX_EXT_create_context_es2_profile specification doesn't say
244 * anything specific about this case. However, none of the known flags
245 * have any meaning in an ES context, so this seems safe.
247 if (mesa_api
!= API_OPENGL_COMPAT
248 && mesa_api
!= API_OPENGL_CORE
250 *error
= __DRI_CTX_ERROR_BAD_FLAG
;
254 /* There are no forward-compatible contexts before OpenGL 3.0. The
255 * GLX_ARB_create_context spec says:
257 * "Forward-compatible contexts are defined only for OpenGL versions
260 * Forward-looking contexts are supported by silently converting the
261 * requested API to API_OPENGL_CORE.
263 * In Mesa, a debug context is the same as a regular context.
265 if ((flags
& __DRI_CTX_FLAG_FORWARD_COMPATIBLE
) != 0) {
266 mesa_api
= API_OPENGL_CORE
;
269 if ((flags
& ~(__DRI_CTX_FLAG_DEBUG
| __DRI_CTX_FLAG_FORWARD_COMPATIBLE
))
271 *error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
275 context
= calloc(1, sizeof *context
);
277 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
281 context
->loaderPrivate
= data
;
283 context
->driScreenPriv
= screen
;
284 context
->driDrawablePriv
= NULL
;
285 context
->driReadablePriv
= NULL
;
287 if (!driDriverAPI
.CreateContext(mesa_api
, modes
, context
,
288 major_version
, minor_version
,
289 flags
, error
, shareCtx
) ) {
294 *error
= __DRI_CTX_ERROR_SUCCESS
;
298 static __DRIcontext
*
299 dri2CreateNewContextForAPI(__DRIscreen
*screen
, int api
,
300 const __DRIconfig
*config
,
301 __DRIcontext
*shared
, void *data
)
305 return dri2CreateContextAttribs(screen
, api
, config
, shared
, 0, NULL
,
309 static __DRIcontext
*
310 dri2CreateNewContext(__DRIscreen
*screen
, const __DRIconfig
*config
,
311 __DRIcontext
*shared
, void *data
)
313 return dri2CreateNewContextForAPI(screen
, __DRI_API_OPENGL
,
314 config
, shared
, data
);
318 * Destroy the per-context private information.
321 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
322 * drmDestroyContext(), and finally frees \p contextPrivate.
325 driDestroyContext(__DRIcontext
*pcp
)
328 driDriverAPI
.DestroyContext(pcp
);
334 driCopyContext(__DRIcontext
*dest
, __DRIcontext
*src
, unsigned long mask
)
345 /*****************************************************************/
346 /** \name Context (un)binding functions */
347 /*****************************************************************/
350 static void dri_get_drawable(__DRIdrawable
*pdp
);
351 static void dri_put_drawable(__DRIdrawable
*pdp
);
354 * This function takes both a read buffer and a draw buffer. This is needed
355 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
358 static int driBindContext(__DRIcontext
*pcp
,
363 ** Assume error checking is done properly in glXMakeCurrent before
364 ** calling driUnbindContext.
370 /* Bind the drawable to the context */
371 pcp
->driDrawablePriv
= pdp
;
372 pcp
->driReadablePriv
= prp
;
374 pdp
->driContextPriv
= pcp
;
375 dri_get_drawable(pdp
);
377 if (prp
&& pdp
!= prp
) {
378 dri_get_drawable(prp
);
381 return driDriverAPI
.MakeCurrent(pcp
, pdp
, prp
);
387 * \param scrn the screen.
390 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
393 * This function calls __DriverAPIRec::UnbindContext, and then decrements
394 * __DRIdrawableRec::refcount which must be non-zero for a successful
397 * While casting the opaque private pointers associated with the parameters
398 * into their respective real types it also assures they are not \c NULL.
400 static int driUnbindContext(__DRIcontext
*pcp
)
406 ** Assume error checking is done properly in glXMakeCurrent before
407 ** calling driUnbindContext.
413 pdp
= pcp
->driDrawablePriv
;
414 prp
= pcp
->driReadablePriv
;
416 /* already unbound */
420 driDriverAPI
.UnbindContext(pcp
);
423 if (pdp
->refcount
== 0) {
428 dri_put_drawable(pdp
);
431 if (prp
->refcount
== 0) {
436 dri_put_drawable(prp
);
439 /* XXX this is disabled so that if we call SwapBuffers on an unbound
440 * window we can determine the last context bound to the window and
441 * use that context's lock. (BrianP, 2-Dec-2000)
443 pcp
->driDrawablePriv
= NULL
;
444 pcp
->driReadablePriv
= NULL
;
452 static void dri_get_drawable(__DRIdrawable
*pdp
)
457 static void dri_put_drawable(__DRIdrawable
*pdp
)
464 driDriverAPI
.DestroyBuffer(pdp
);
469 static __DRIdrawable
*
470 dri2CreateNewDrawable(__DRIscreen
*screen
,
471 const __DRIconfig
*config
,
474 __DRIdrawable
*pdraw
;
476 pdraw
= malloc(sizeof *pdraw
);
480 pdraw
->loaderPrivate
= data
;
482 pdraw
->driScreenPriv
= screen
;
483 pdraw
->driContextPriv
= NULL
;
485 pdraw
->lastStamp
= 0;
489 dri_get_drawable(pdraw
);
491 if (!driDriverAPI
.CreateBuffer(screen
, pdraw
, &config
->modes
, GL_FALSE
)) {
496 pdraw
->dri2
.stamp
= pdraw
->lastStamp
+ 1;
502 driDestroyDrawable(__DRIdrawable
*pdp
)
504 dri_put_drawable(pdp
);
508 dri2AllocateBuffer(__DRIscreen
*screen
,
509 unsigned int attachment
, unsigned int format
,
510 int width
, int height
)
512 return driDriverAPI
.AllocateBuffer(screen
, attachment
, format
,
517 dri2ReleaseBuffer(__DRIscreen
*screen
, __DRIbuffer
*buffer
)
519 driDriverAPI
.ReleaseBuffer(screen
, buffer
);
524 dri2ConfigQueryb(__DRIscreen
*screen
, const char *var
, GLboolean
*val
)
526 if (!driCheckOption(&screen
->optionCache
, var
, DRI_BOOL
))
529 *val
= driQueryOptionb(&screen
->optionCache
, var
);
535 dri2ConfigQueryi(__DRIscreen
*screen
, const char *var
, GLint
*val
)
537 if (!driCheckOption(&screen
->optionCache
, var
, DRI_INT
) &&
538 !driCheckOption(&screen
->optionCache
, var
, DRI_ENUM
))
541 *val
= driQueryOptioni(&screen
->optionCache
, var
);
547 dri2ConfigQueryf(__DRIscreen
*screen
, const char *var
, GLfloat
*val
)
549 if (!driCheckOption(&screen
->optionCache
, var
, DRI_FLOAT
))
552 *val
= driQueryOptionf(&screen
->optionCache
, var
);
558 dri2GetAPIMask(__DRIscreen
*screen
)
560 return screen
->api_mask
;
564 /** Core interface */
565 const __DRIcoreExtension driCoreExtension
= {
566 .base
= { __DRI_CORE
, __DRI_CORE_VERSION
},
568 .createNewScreen
= NULL
,
569 .destroyScreen
= driDestroyScreen
,
570 .getExtensions
= driGetExtensions
,
571 .getConfigAttrib
= driGetConfigAttrib
,
572 .indexConfigAttrib
= driIndexConfigAttrib
,
573 .createNewDrawable
= NULL
,
574 .destroyDrawable
= driDestroyDrawable
,
576 .createNewContext
= NULL
,
577 .copyContext
= driCopyContext
,
578 .destroyContext
= driDestroyContext
,
579 .bindContext
= driBindContext
,
580 .unbindContext
= driUnbindContext
583 /** DRI2 interface */
584 const __DRIdri2Extension driDRI2Extension
= {
585 .base
= { __DRI_DRI2
, 3 },
587 .createNewScreen
= dri2CreateNewScreen
,
588 .createNewDrawable
= dri2CreateNewDrawable
,
589 .createNewContext
= dri2CreateNewContext
,
590 .getAPIMask
= dri2GetAPIMask
,
591 .createNewContextForAPI
= dri2CreateNewContextForAPI
,
592 .allocateBuffer
= dri2AllocateBuffer
,
593 .releaseBuffer
= dri2ReleaseBuffer
,
594 .createContextAttribs
= dri2CreateContextAttribs
597 const __DRI2configQueryExtension dri2ConfigQueryExtension
= {
598 .base
= { __DRI2_CONFIG_QUERY
, __DRI2_CONFIG_QUERY_VERSION
},
600 .configQueryb
= dri2ConfigQueryb
,
601 .configQueryi
= dri2ConfigQueryi
,
602 .configQueryf
= dri2ConfigQueryf
,
606 dri2InvalidateDrawable(__DRIdrawable
*drawable
)
608 drawable
->dri2
.stamp
++;
612 * Check that the gl_framebuffer associated with dPriv is the right size.
613 * Resize the gl_framebuffer if needed.
614 * It's expected that the dPriv->driverPrivate member points to a
615 * gl_framebuffer object.
618 driUpdateFramebufferSize(struct gl_context
*ctx
, const __DRIdrawable
*dPriv
)
620 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) dPriv
->driverPrivate
;
621 if (fb
&& (dPriv
->w
!= fb
->Width
|| dPriv
->h
!= fb
->Height
)) {
622 ctx
->Driver
.ResizeBuffers(ctx
, fb
, dPriv
->w
, dPriv
->h
);
623 /* if the driver needs the hw lock for ResizeBuffers, the drawable
624 might have changed again by now */
625 assert(fb
->Width
== dPriv
->w
);
626 assert(fb
->Height
== dPriv
->h
);