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
;
189 mesa_api
= API_OPENGLES
;
191 case __DRI_API_GLES2
:
192 mesa_api
= API_OPENGLES2
;
194 case __DRI_API_OPENGL_CORE
:
196 *error
= __DRI_CTX_ERROR_BAD_API
;
200 if (mesa_api
!= API_OPENGL
&& num_attribs
!= 0) {
201 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
202 assert(!"Should not get here.");
206 for (unsigned i
= 0; i
< num_attribs
; i
++) {
207 switch (attribs
[i
* 2]) {
208 case __DRI_CTX_ATTRIB_MAJOR_VERSION
:
209 major_version
= attribs
[i
* 2 + 1];
211 case __DRI_CTX_ATTRIB_MINOR_VERSION
:
212 minor_version
= attribs
[i
* 2 + 1];
214 case __DRI_CTX_ATTRIB_FLAGS
:
215 flags
= attribs
[i
* 2 + 1];
218 /* We can't create a context that satisfies the requirements of an
219 * attribute that we don't understand. Return failure.
221 assert(!"Should not get here.");
222 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
227 /* There are no forward-compatible contexts before OpenGL 3.0. The
228 * GLX_ARB_create_context spec says:
230 * "Forward-compatible contexts are defined only for OpenGL versions
233 * Moreover, Mesa can't fulfill the requirements of a forward-looking
234 * context. Return failure if a forward-looking context is requested.
236 * In Mesa, a debug context is the same as a regular context.
238 if ((flags
& __DRI_CTX_FLAG_FORWARD_COMPATIBLE
) != 0) {
239 *error
= __DRI_CTX_ERROR_BAD_FLAG
;
243 if ((flags
& ~__DRI_CTX_FLAG_DEBUG
) != 0) {
244 *error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
248 context
= malloc(sizeof *context
);
250 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
254 context
->loaderPrivate
= data
;
256 context
->driScreenPriv
= screen
;
257 context
->driDrawablePriv
= NULL
;
258 context
->driReadablePriv
= NULL
;
260 if (!driDriverAPI
.CreateContext(mesa_api
, modes
, context
,
261 major_version
, minor_version
,
262 flags
, error
, shareCtx
) ) {
267 *error
= __DRI_CTX_ERROR_SUCCESS
;
271 static __DRIcontext
*
272 dri2CreateNewContextForAPI(__DRIscreen
*screen
, int api
,
273 const __DRIconfig
*config
,
274 __DRIcontext
*shared
, void *data
)
278 return dri2CreateContextAttribs(screen
, api
, config
, shared
, 0, NULL
,
282 static __DRIcontext
*
283 dri2CreateNewContext(__DRIscreen
*screen
, const __DRIconfig
*config
,
284 __DRIcontext
*shared
, void *data
)
286 return dri2CreateNewContextForAPI(screen
, __DRI_API_OPENGL
,
287 config
, shared
, data
);
291 * Destroy the per-context private information.
294 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
295 * drmDestroyContext(), and finally frees \p contextPrivate.
298 driDestroyContext(__DRIcontext
*pcp
)
301 driDriverAPI
.DestroyContext(pcp
);
307 driCopyContext(__DRIcontext
*dest
, __DRIcontext
*src
, unsigned long mask
)
318 /*****************************************************************/
319 /** \name Context (un)binding functions */
320 /*****************************************************************/
323 static void dri_get_drawable(__DRIdrawable
*pdp
);
324 static void dri_put_drawable(__DRIdrawable
*pdp
);
327 * This function takes both a read buffer and a draw buffer. This is needed
328 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
331 static int driBindContext(__DRIcontext
*pcp
,
336 ** Assume error checking is done properly in glXMakeCurrent before
337 ** calling driUnbindContext.
343 /* Bind the drawable to the context */
344 pcp
->driDrawablePriv
= pdp
;
345 pcp
->driReadablePriv
= prp
;
347 pdp
->driContextPriv
= pcp
;
348 dri_get_drawable(pdp
);
350 if (prp
&& pdp
!= prp
) {
351 dri_get_drawable(prp
);
354 return driDriverAPI
.MakeCurrent(pcp
, pdp
, prp
);
360 * \param scrn the screen.
363 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
366 * This function calls __DriverAPIRec::UnbindContext, and then decrements
367 * __DRIdrawableRec::refcount which must be non-zero for a successful
370 * While casting the opaque private pointers associated with the parameters
371 * into their respective real types it also assures they are not \c NULL.
373 static int driUnbindContext(__DRIcontext
*pcp
)
379 ** Assume error checking is done properly in glXMakeCurrent before
380 ** calling driUnbindContext.
386 pdp
= pcp
->driDrawablePriv
;
387 prp
= pcp
->driReadablePriv
;
389 /* already unbound */
393 driDriverAPI
.UnbindContext(pcp
);
396 if (pdp
->refcount
== 0) {
401 dri_put_drawable(pdp
);
404 if (prp
->refcount
== 0) {
409 dri_put_drawable(prp
);
412 /* XXX this is disabled so that if we call SwapBuffers on an unbound
413 * window we can determine the last context bound to the window and
414 * use that context's lock. (BrianP, 2-Dec-2000)
416 pcp
->driDrawablePriv
= NULL
;
417 pcp
->driReadablePriv
= NULL
;
425 static void dri_get_drawable(__DRIdrawable
*pdp
)
430 static void dri_put_drawable(__DRIdrawable
*pdp
)
437 driDriverAPI
.DestroyBuffer(pdp
);
442 static __DRIdrawable
*
443 dri2CreateNewDrawable(__DRIscreen
*screen
,
444 const __DRIconfig
*config
,
447 __DRIdrawable
*pdraw
;
449 pdraw
= malloc(sizeof *pdraw
);
453 pdraw
->loaderPrivate
= data
;
455 pdraw
->driScreenPriv
= screen
;
456 pdraw
->driContextPriv
= NULL
;
458 pdraw
->lastStamp
= 0;
462 dri_get_drawable(pdraw
);
464 if (!driDriverAPI
.CreateBuffer(screen
, pdraw
, &config
->modes
, GL_FALSE
)) {
469 pdraw
->dri2
.stamp
= pdraw
->lastStamp
+ 1;
475 driDestroyDrawable(__DRIdrawable
*pdp
)
477 dri_put_drawable(pdp
);
481 dri2AllocateBuffer(__DRIscreen
*screen
,
482 unsigned int attachment
, unsigned int format
,
483 int width
, int height
)
485 return driDriverAPI
.AllocateBuffer(screen
, attachment
, format
,
490 dri2ReleaseBuffer(__DRIscreen
*screen
, __DRIbuffer
*buffer
)
492 driDriverAPI
.ReleaseBuffer(screen
, buffer
);
497 dri2ConfigQueryb(__DRIscreen
*screen
, const char *var
, GLboolean
*val
)
499 if (!driCheckOption(&screen
->optionCache
, var
, DRI_BOOL
))
502 *val
= driQueryOptionb(&screen
->optionCache
, var
);
508 dri2ConfigQueryi(__DRIscreen
*screen
, const char *var
, GLint
*val
)
510 if (!driCheckOption(&screen
->optionCache
, var
, DRI_INT
) &&
511 !driCheckOption(&screen
->optionCache
, var
, DRI_ENUM
))
514 *val
= driQueryOptioni(&screen
->optionCache
, var
);
520 dri2ConfigQueryf(__DRIscreen
*screen
, const char *var
, GLfloat
*val
)
522 if (!driCheckOption(&screen
->optionCache
, var
, DRI_FLOAT
))
525 *val
= driQueryOptionf(&screen
->optionCache
, var
);
531 dri2GetAPIMask(__DRIscreen
*screen
)
533 return screen
->api_mask
;
537 /** Core interface */
538 const __DRIcoreExtension driCoreExtension
= {
539 { __DRI_CORE
, __DRI_CORE_VERSION
},
544 driIndexConfigAttrib
,
555 /** DRI2 interface */
556 const __DRIdri2Extension driDRI2Extension
= {
559 dri2CreateNewDrawable
,
560 dri2CreateNewContext
,
562 dri2CreateNewContextForAPI
,
565 dri2CreateContextAttribs
568 const __DRI2configQueryExtension dri2ConfigQueryExtension
= {
569 { __DRI2_CONFIG_QUERY
, __DRI2_CONFIG_QUERY_VERSION
},
576 dri2InvalidateDrawable(__DRIdrawable
*drawable
)
578 drawable
->dri2
.stamp
++;
582 * Check that the gl_framebuffer associated with dPriv is the right size.
583 * Resize the gl_framebuffer if needed.
584 * It's expected that the dPriv->driverPrivate member points to a
585 * gl_framebuffer object.
588 driUpdateFramebufferSize(struct gl_context
*ctx
, const __DRIdrawable
*dPriv
)
590 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) dPriv
->driverPrivate
;
591 if (fb
&& (dPriv
->w
!= fb
->Width
|| dPriv
->h
!= fb
->Height
)) {
592 ctx
->Driver
.ResizeBuffers(ctx
, fb
, dPriv
->w
, dPriv
->h
);
593 /* if the driver needs the hw lock for ResizeBuffers, the drawable
594 might have changed again by now */
595 assert(fb
->Width
== dPriv
->w
);
596 assert(fb
->Height
== dPriv
->h
);