3 * DRI utility functions.
5 * This module acts as glue between GLX and the actual hardware driver. A DRI
6 * driver doesn't really \e have to use any of this - it's optional. But, some
7 * useful stuff is done here that otherwise would have to be duplicated in most
10 * Basically, these utility functions take care of some of the dirty details of
11 * screen initialization, context creation, context binding, DRM setup, etc.
13 * These functions are compiled into each DRI driver so libGL.so knows nothing
22 #include "../glsl/glsl_parser_extras.h"
24 PUBLIC
const char __dri2ConfigOptions
[] =
26 DRI_CONF_SECTION_PERFORMANCE
27 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_1
)
31 static const uint __dri2NConfigOptions
= 1;
33 /*****************************************************************/
34 /** \name Screen handling functions */
35 /*****************************************************************/
39 setupLoaderExtensions(__DRIscreen
*psp
,
40 const __DRIextension
**extensions
)
44 for (i
= 0; extensions
[i
]; i
++) {
45 if (strcmp(extensions
[i
]->name
, __DRI_DRI2_LOADER
) == 0)
46 psp
->dri2
.loader
= (__DRIdri2LoaderExtension
*) extensions
[i
];
47 if (strcmp(extensions
[i
]->name
, __DRI_IMAGE_LOOKUP
) == 0)
48 psp
->dri2
.image
= (__DRIimageLookupExtension
*) extensions
[i
];
49 if (strcmp(extensions
[i
]->name
, __DRI_USE_INVALIDATE
) == 0)
50 psp
->dri2
.useInvalidate
= (__DRIuseInvalidateExtension
*) extensions
[i
];
55 dri2CreateNewScreen(int scrn
, int fd
,
56 const __DRIextension
**extensions
,
57 const __DRIconfig
***driver_configs
, void *data
)
59 static const __DRIextension
*emptyExtensionList
[] = { NULL
};
61 drmVersionPtr version
;
63 psp
= calloc(1, sizeof(*psp
));
67 setupLoaderExtensions(psp
, extensions
);
69 version
= drmGetVersion(fd
);
71 psp
->drm_version
.major
= version
->version_major
;
72 psp
->drm_version
.minor
= version
->version_minor
;
73 psp
->drm_version
.patch
= version
->version_patchlevel
;
74 drmFreeVersion(version
);
77 psp
->loaderPrivate
= data
;
79 psp
->extensions
= emptyExtensionList
;
83 psp
->api_mask
= (1 << __DRI_API_OPENGL
);
85 *driver_configs
= driDriverAPI
.InitScreen(psp
);
86 if (*driver_configs
== NULL
) {
91 driParseOptionInfo(&psp
->optionInfo
, __dri2ConfigOptions
, __dri2NConfigOptions
);
92 driParseConfigFiles(&psp
->optionCache
, &psp
->optionInfo
, psp
->myNum
, "dri2");
98 * Destroy the per-screen private information.
101 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
102 * drmClose(), and finally frees \p screenPrivate.
104 static void driDestroyScreen(__DRIscreen
*psp
)
107 /* No interaction with the X-server is possible at this point. This
108 * routine is called after XCloseDisplay, so there is no protocol
109 * stream open to the X-server anymore.
112 _mesa_destroy_shader_compiler();
114 driDriverAPI
.DestroyScreen(psp
);
116 driDestroyOptionCache(&psp
->optionCache
);
117 driDestroyOptionInfo(&psp
->optionInfo
);
123 static const __DRIextension
**driGetExtensions(__DRIscreen
*psp
)
125 return psp
->extensions
;
131 /*****************************************************************/
132 /** \name Context handling functions */
133 /*****************************************************************/
136 static __DRIcontext
*
137 dri2CreateContextAttribs(__DRIscreen
*screen
, int api
,
138 const __DRIconfig
*config
,
139 __DRIcontext
*shared
,
140 unsigned num_attribs
,
141 const uint32_t *attribs
,
145 __DRIcontext
*context
;
146 const struct gl_config
*modes
= (config
!= NULL
) ? &config
->modes
: NULL
;
147 void *shareCtx
= (shared
!= NULL
) ? shared
->driverPrivate
: NULL
;
149 unsigned major_version
= 1;
150 unsigned minor_version
= 0;
153 assert((num_attribs
== 0) || (attribs
!= NULL
));
155 if (!(screen
->api_mask
& (1 << api
))) {
156 *error
= __DRI_CTX_ERROR_BAD_API
;
161 case __DRI_API_OPENGL
:
162 mesa_api
= API_OPENGL
;
165 mesa_api
= API_OPENGLES
;
167 case __DRI_API_GLES2
:
168 mesa_api
= API_OPENGLES2
;
170 case __DRI_API_OPENGL_CORE
:
172 *error
= __DRI_CTX_ERROR_BAD_API
;
176 if (mesa_api
!= API_OPENGL
&& num_attribs
!= 0) {
177 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
178 assert(!"Should not get here.");
182 for (unsigned i
= 0; i
< num_attribs
; i
++) {
183 switch (attribs
[i
* 2]) {
184 case __DRI_CTX_ATTRIB_MAJOR_VERSION
:
185 major_version
= attribs
[i
* 2 + 1];
187 case __DRI_CTX_ATTRIB_MINOR_VERSION
:
188 minor_version
= attribs
[i
* 2 + 1];
190 case __DRI_CTX_ATTRIB_FLAGS
:
191 flags
= attribs
[i
* 2 + 1];
194 /* We can't create a context that satisfies the requirements of an
195 * attribute that we don't understand. Return failure.
201 /* There are no forward-compatible contexts before OpenGL 3.0. The
202 * GLX_ARB_create_context spec says:
204 * "Forward-compatible contexts are defined only for OpenGL versions
207 * Moreover, Mesa can't fulfill the requirements of a forward-looking
208 * context. Return failure if a forward-looking context is requested.
210 * In Mesa, a debug context is the same as a regular context.
212 if (major_version
>= 3) {
213 if ((flags
& ~__DRI_CTX_FLAG_DEBUG
) != 0)
217 context
= malloc(sizeof *context
);
219 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
223 context
->loaderPrivate
= data
;
225 context
->driScreenPriv
= screen
;
226 context
->driDrawablePriv
= NULL
;
227 context
->driReadablePriv
= NULL
;
229 if (!driDriverAPI
.CreateContext(mesa_api
, modes
, context
, shareCtx
) ) {
234 *error
= __DRI_CTX_ERROR_SUCCESS
;
238 static __DRIcontext
*
239 dri2CreateNewContextForAPI(__DRIscreen
*screen
, int api
,
240 const __DRIconfig
*config
,
241 __DRIcontext
*shared
, void *data
)
245 return dri2CreateContextAttribs(screen
, api
, config
, shared
, 0, NULL
,
249 static __DRIcontext
*
250 dri2CreateNewContext(__DRIscreen
*screen
, const __DRIconfig
*config
,
251 __DRIcontext
*shared
, void *data
)
253 return dri2CreateNewContextForAPI(screen
, __DRI_API_OPENGL
,
254 config
, shared
, data
);
258 * Destroy the per-context private information.
261 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
262 * drmDestroyContext(), and finally frees \p contextPrivate.
265 driDestroyContext(__DRIcontext
*pcp
)
268 driDriverAPI
.DestroyContext(pcp
);
274 driCopyContext(__DRIcontext
*dest
, __DRIcontext
*src
, unsigned long mask
)
285 /*****************************************************************/
286 /** \name Context (un)binding functions */
287 /*****************************************************************/
290 static void dri_get_drawable(__DRIdrawable
*pdp
);
291 static void dri_put_drawable(__DRIdrawable
*pdp
);
294 * This function takes both a read buffer and a draw buffer. This is needed
295 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
298 static int driBindContext(__DRIcontext
*pcp
,
303 ** Assume error checking is done properly in glXMakeCurrent before
304 ** calling driUnbindContext.
310 /* Bind the drawable to the context */
311 pcp
->driDrawablePriv
= pdp
;
312 pcp
->driReadablePriv
= prp
;
314 pdp
->driContextPriv
= pcp
;
315 dri_get_drawable(pdp
);
317 if (prp
&& pdp
!= prp
) {
318 dri_get_drawable(prp
);
321 return driDriverAPI
.MakeCurrent(pcp
, pdp
, prp
);
327 * \param scrn the screen.
330 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
333 * This function calls __DriverAPIRec::UnbindContext, and then decrements
334 * __DRIdrawableRec::refcount which must be non-zero for a successful
337 * While casting the opaque private pointers associated with the parameters
338 * into their respective real types it also assures they are not \c NULL.
340 static int driUnbindContext(__DRIcontext
*pcp
)
346 ** Assume error checking is done properly in glXMakeCurrent before
347 ** calling driUnbindContext.
353 pdp
= pcp
->driDrawablePriv
;
354 prp
= pcp
->driReadablePriv
;
356 /* already unbound */
360 driDriverAPI
.UnbindContext(pcp
);
363 if (pdp
->refcount
== 0) {
368 dri_put_drawable(pdp
);
371 if (prp
->refcount
== 0) {
376 dri_put_drawable(prp
);
379 /* XXX this is disabled so that if we call SwapBuffers on an unbound
380 * window we can determine the last context bound to the window and
381 * use that context's lock. (BrianP, 2-Dec-2000)
383 pcp
->driDrawablePriv
= NULL
;
384 pcp
->driReadablePriv
= NULL
;
392 static void dri_get_drawable(__DRIdrawable
*pdp
)
397 static void dri_put_drawable(__DRIdrawable
*pdp
)
404 driDriverAPI
.DestroyBuffer(pdp
);
409 static __DRIdrawable
*
410 dri2CreateNewDrawable(__DRIscreen
*screen
,
411 const __DRIconfig
*config
,
414 __DRIdrawable
*pdraw
;
416 pdraw
= malloc(sizeof *pdraw
);
420 pdraw
->loaderPrivate
= data
;
422 pdraw
->driScreenPriv
= screen
;
423 pdraw
->driContextPriv
= NULL
;
425 pdraw
->lastStamp
= 0;
429 dri_get_drawable(pdraw
);
431 if (!driDriverAPI
.CreateBuffer(screen
, pdraw
, &config
->modes
, GL_FALSE
)) {
436 pdraw
->dri2
.stamp
= pdraw
->lastStamp
+ 1;
442 driDestroyDrawable(__DRIdrawable
*pdp
)
444 dri_put_drawable(pdp
);
448 dri2AllocateBuffer(__DRIscreen
*screen
,
449 unsigned int attachment
, unsigned int format
,
450 int width
, int height
)
452 return driDriverAPI
.AllocateBuffer(screen
, attachment
, format
,
457 dri2ReleaseBuffer(__DRIscreen
*screen
, __DRIbuffer
*buffer
)
459 driDriverAPI
.ReleaseBuffer(screen
, buffer
);
464 dri2ConfigQueryb(__DRIscreen
*screen
, const char *var
, GLboolean
*val
)
466 if (!driCheckOption(&screen
->optionCache
, var
, DRI_BOOL
))
469 *val
= driQueryOptionb(&screen
->optionCache
, var
);
475 dri2ConfigQueryi(__DRIscreen
*screen
, const char *var
, GLint
*val
)
477 if (!driCheckOption(&screen
->optionCache
, var
, DRI_INT
) &&
478 !driCheckOption(&screen
->optionCache
, var
, DRI_ENUM
))
481 *val
= driQueryOptioni(&screen
->optionCache
, var
);
487 dri2ConfigQueryf(__DRIscreen
*screen
, const char *var
, GLfloat
*val
)
489 if (!driCheckOption(&screen
->optionCache
, var
, DRI_FLOAT
))
492 *val
= driQueryOptionf(&screen
->optionCache
, var
);
498 dri2GetAPIMask(__DRIscreen
*screen
)
500 return screen
->api_mask
;
504 /** Core interface */
505 const __DRIcoreExtension driCoreExtension
= {
506 { __DRI_CORE
, __DRI_CORE_VERSION
},
511 driIndexConfigAttrib
,
522 /** DRI2 interface */
523 const __DRIdri2Extension driDRI2Extension
= {
524 /* Force the version to 2 because the underlying drivers don't (can't!)
525 * support the extra requirements of CreateContextAttribs.
529 dri2CreateNewDrawable
,
530 dri2CreateNewContext
,
532 dri2CreateNewContextForAPI
,
535 dri2CreateContextAttribs
538 const __DRI2configQueryExtension dri2ConfigQueryExtension
= {
539 { __DRI2_CONFIG_QUERY
, __DRI2_CONFIG_QUERY_VERSION
},
546 dri2InvalidateDrawable(__DRIdrawable
*drawable
)
548 drawable
->dri2
.stamp
++;
552 * Check that the gl_framebuffer associated with dPriv is the right size.
553 * Resize the gl_framebuffer if needed.
554 * It's expected that the dPriv->driverPrivate member points to a
555 * gl_framebuffer object.
558 driUpdateFramebufferSize(struct gl_context
*ctx
, const __DRIdrawable
*dPriv
)
560 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) dPriv
->driverPrivate
;
561 if (fb
&& (dPriv
->w
!= fb
->Width
|| dPriv
->h
!= fb
->Height
)) {
562 ctx
->Driver
.ResizeBuffers(ctx
, fb
, dPriv
->w
, dPriv
->h
);
563 /* if the driver needs the hw lock for ResizeBuffers, the drawable
564 might have changed again by now */
565 assert(fb
->Width
== dPriv
->w
);
566 assert(fb
->Height
== dPriv
->h
);