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
->extensions
= emptyExtensionList
;
81 psp
->api_mask
= (1 << __DRI_API_OPENGL
);
82 *driver_configs
= driDriverAPI
.InitScreen(psp
);
83 if (*driver_configs
== NULL
) {
88 psp
->loaderPrivate
= data
;
90 driParseOptionInfo(&psp
->optionInfo
, __dri2ConfigOptions
,
91 __dri2NConfigOptions
);
92 driParseConfigFiles(&psp
->optionCache
, &psp
->optionInfo
, psp
->myNum
,
99 * Destroy the per-screen private information.
102 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
103 * drmClose(), and finally frees \p screenPrivate.
105 static void driDestroyScreen(__DRIscreen
*psp
)
108 /* No interaction with the X-server is possible at this point. This
109 * routine is called after XCloseDisplay, so there is no protocol
110 * stream open to the X-server anymore.
113 _mesa_destroy_shader_compiler();
115 if (driDriverAPI
.DestroyScreen
)
116 driDriverAPI
.DestroyScreen(psp
);
118 driDestroyOptionCache(&psp
->optionCache
);
119 driDestroyOptionInfo(&psp
->optionInfo
);
125 static const __DRIextension
**driGetExtensions(__DRIscreen
*psp
)
127 return psp
->extensions
;
133 /*****************************************************************/
134 /** \name Context handling functions */
135 /*****************************************************************/
138 static __DRIcontext
*
139 dri2CreateNewContextForAPI(__DRIscreen
*screen
, int api
,
140 const __DRIconfig
*config
,
141 __DRIcontext
*shared
, void *data
)
143 __DRIcontext
*context
;
144 const struct gl_config
*modes
= (config
!= NULL
) ? &config
->modes
: NULL
;
145 void *shareCtx
= (shared
!= NULL
) ? shared
->driverPrivate
: NULL
;
148 if (!(screen
->api_mask
& (1 << api
)))
152 case __DRI_API_OPENGL
:
153 mesa_api
= API_OPENGL
;
156 mesa_api
= API_OPENGLES
;
158 case __DRI_API_GLES2
:
159 mesa_api
= API_OPENGLES2
;
165 context
= malloc(sizeof *context
);
169 context
->driScreenPriv
= screen
;
170 context
->driDrawablePriv
= NULL
;
171 context
->loaderPrivate
= data
;
173 if (!driDriverAPI
.CreateContext(mesa_api
, modes
,
174 context
, shareCtx
) ) {
183 static __DRIcontext
*
184 dri2CreateNewContext(__DRIscreen
*screen
, const __DRIconfig
*config
,
185 __DRIcontext
*shared
, void *data
)
187 return dri2CreateNewContextForAPI(screen
, __DRI_API_OPENGL
,
188 config
, shared
, data
);
192 * Destroy the per-context private information.
195 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
196 * drmDestroyContext(), and finally frees \p contextPrivate.
199 driDestroyContext(__DRIcontext
*pcp
)
202 driDriverAPI
.DestroyContext(pcp
);
208 driCopyContext(__DRIcontext
*dest
, __DRIcontext
*src
, unsigned long mask
)
219 /*****************************************************************/
220 /** \name Context (un)binding functions */
221 /*****************************************************************/
224 static void dri_get_drawable(__DRIdrawable
*pdp
);
225 static void dri_put_drawable(__DRIdrawable
*pdp
);
228 * This function takes both a read buffer and a draw buffer. This is needed
229 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
232 static int driBindContext(__DRIcontext
*pcp
,
237 ** Assume error checking is done properly in glXMakeCurrent before
238 ** calling driUnbindContext.
244 /* Bind the drawable to the context */
245 pcp
->driDrawablePriv
= pdp
;
246 pcp
->driReadablePriv
= prp
;
248 pdp
->driContextPriv
= pcp
;
249 dri_get_drawable(pdp
);
251 if (prp
&& pdp
!= prp
) {
252 dri_get_drawable(prp
);
255 /* Call device-specific MakeCurrent */
256 return driDriverAPI
.MakeCurrent(pcp
, pdp
, prp
);
262 * \param scrn the screen.
265 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
268 * This function calls __DriverAPIRec::UnbindContext, and then decrements
269 * __DRIdrawableRec::refcount which must be non-zero for a successful
272 * While casting the opaque private pointers associated with the parameters
273 * into their respective real types it also assures they are not \c NULL.
275 static int driUnbindContext(__DRIcontext
*pcp
)
281 ** Assume error checking is done properly in glXMakeCurrent before
282 ** calling driUnbindContext.
288 pdp
= pcp
->driDrawablePriv
;
289 prp
= pcp
->driReadablePriv
;
291 /* already unbound */
294 /* Let driver unbind drawable from context */
295 driDriverAPI
.UnbindContext(pcp
);
298 if (pdp
->refcount
== 0) {
303 dri_put_drawable(pdp
);
306 if (prp
->refcount
== 0) {
311 dri_put_drawable(prp
);
315 /* XXX this is disabled so that if we call SwapBuffers on an unbound
316 * window we can determine the last context bound to the window and
317 * use that context's lock. (BrianP, 2-Dec-2000)
319 pcp
->driDrawablePriv
= pcp
->driReadablePriv
= NULL
;
327 static __DRIdrawable
*
328 dri2CreateNewDrawable(__DRIscreen
*screen
,
329 const __DRIconfig
*config
,
332 __DRIdrawable
*pdraw
;
334 pdraw
= malloc(sizeof *pdraw
);
338 pdraw
->driContextPriv
= NULL
;
339 pdraw
->loaderPrivate
= loaderPrivate
;
341 pdraw
->lastStamp
= 0;
344 pdraw
->driScreenPriv
= screen
;
346 if (!driDriverAPI
.CreateBuffer(screen
, pdraw
, &config
->modes
, 0)) {
351 pdraw
->dri2
.stamp
= pdraw
->lastStamp
+ 1;
357 dri2AllocateBuffer(__DRIscreen
*screen
,
358 unsigned int attachment
, unsigned int format
,
359 int width
, int height
)
361 return driDriverAPI
.AllocateBuffer(screen
, attachment
, format
,
366 dri2ReleaseBuffer(__DRIscreen
*screen
, __DRIbuffer
*buffer
)
368 driDriverAPI
.ReleaseBuffer(screen
, buffer
);
373 dri2ConfigQueryb(__DRIscreen
*screen
, const char *var
, GLboolean
*val
)
375 if (!driCheckOption(&screen
->optionCache
, var
, DRI_BOOL
))
378 *val
= driQueryOptionb(&screen
->optionCache
, var
);
384 dri2ConfigQueryi(__DRIscreen
*screen
, const char *var
, GLint
*val
)
386 if (!driCheckOption(&screen
->optionCache
, var
, DRI_INT
) &&
387 !driCheckOption(&screen
->optionCache
, var
, DRI_ENUM
))
390 *val
= driQueryOptioni(&screen
->optionCache
, var
);
396 dri2ConfigQueryf(__DRIscreen
*screen
, const char *var
, GLfloat
*val
)
398 if (!driCheckOption(&screen
->optionCache
, var
, DRI_FLOAT
))
401 *val
= driQueryOptionf(&screen
->optionCache
, var
);
407 static void dri_get_drawable(__DRIdrawable
*pdp
)
412 static void dri_put_drawable(__DRIdrawable
*pdp
)
421 psp
= pdp
->driScreenPriv
;
422 driDriverAPI
.DestroyBuffer(pdp
);
428 driDestroyDrawable(__DRIdrawable
*pdp
)
430 dri_put_drawable(pdp
);
434 dri2GetAPIMask(__DRIscreen
*screen
)
436 return screen
->api_mask
;
440 /** Core interface */
441 const __DRIcoreExtension driCoreExtension
= {
442 { __DRI_CORE
, __DRI_CORE_VERSION
},
447 driIndexConfigAttrib
,
458 /** DRI2 interface */
459 const __DRIdri2Extension driDRI2Extension
= {
460 { __DRI_DRI2
, __DRI_DRI2_VERSION
},
462 dri2CreateNewDrawable
,
463 dri2CreateNewContext
,
465 dri2CreateNewContextForAPI
,
470 const __DRI2configQueryExtension dri2ConfigQueryExtension
= {
471 { __DRI2_CONFIG_QUERY
, __DRI2_CONFIG_QUERY_VERSION
},
478 dri2InvalidateDrawable(__DRIdrawable
*drawable
)
480 drawable
->dri2
.stamp
++;
484 * Check that the gl_framebuffer associated with dPriv is the right size.
485 * Resize the gl_framebuffer if needed.
486 * It's expected that the dPriv->driverPrivate member points to a
487 * gl_framebuffer object.
490 driUpdateFramebufferSize(struct gl_context
*ctx
, const __DRIdrawable
*dPriv
)
492 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) dPriv
->driverPrivate
;
493 if (fb
&& (dPriv
->w
!= fb
->Width
|| dPriv
->h
!= fb
->Height
)) {
494 ctx
->Driver
.ResizeBuffers(ctx
, fb
, dPriv
->w
, dPriv
->h
);
495 /* if the driver needs the hw lock for ResizeBuffers, the drawable
496 might have changed again by now */
497 assert(fb
->Width
== dPriv
->w
);
498 assert(fb
->Height
== dPriv
->h
);