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 dri2CreateNewContextForAPI(__DRIscreen
*screen
, int api
,
138 const __DRIconfig
*config
,
139 __DRIcontext
*shared
, void *data
)
141 __DRIcontext
*context
;
142 const struct gl_config
*modes
= (config
!= NULL
) ? &config
->modes
: NULL
;
143 void *shareCtx
= (shared
!= NULL
) ? shared
->driverPrivate
: NULL
;
146 if (!(screen
->api_mask
& (1 << api
)))
150 case __DRI_API_OPENGL
:
151 mesa_api
= API_OPENGL
;
154 mesa_api
= API_OPENGLES
;
156 case __DRI_API_GLES2
:
157 mesa_api
= API_OPENGLES2
;
163 context
= malloc(sizeof *context
);
167 context
->loaderPrivate
= data
;
169 context
->driScreenPriv
= screen
;
170 context
->driDrawablePriv
= NULL
;
171 context
->driReadablePriv
= NULL
;
173 if (!driDriverAPI
.CreateContext(mesa_api
, modes
, context
, shareCtx
) ) {
182 static __DRIcontext
*
183 dri2CreateNewContext(__DRIscreen
*screen
, const __DRIconfig
*config
,
184 __DRIcontext
*shared
, void *data
)
186 return dri2CreateNewContextForAPI(screen
, __DRI_API_OPENGL
,
187 config
, shared
, data
);
191 * Destroy the per-context private information.
194 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
195 * drmDestroyContext(), and finally frees \p contextPrivate.
198 driDestroyContext(__DRIcontext
*pcp
)
201 driDriverAPI
.DestroyContext(pcp
);
207 driCopyContext(__DRIcontext
*dest
, __DRIcontext
*src
, unsigned long mask
)
218 /*****************************************************************/
219 /** \name Context (un)binding functions */
220 /*****************************************************************/
223 static void dri_get_drawable(__DRIdrawable
*pdp
);
224 static void dri_put_drawable(__DRIdrawable
*pdp
);
227 * This function takes both a read buffer and a draw buffer. This is needed
228 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
231 static int driBindContext(__DRIcontext
*pcp
,
236 ** Assume error checking is done properly in glXMakeCurrent before
237 ** calling driUnbindContext.
243 /* Bind the drawable to the context */
244 pcp
->driDrawablePriv
= pdp
;
245 pcp
->driReadablePriv
= prp
;
247 pdp
->driContextPriv
= pcp
;
248 dri_get_drawable(pdp
);
250 if (prp
&& pdp
!= prp
) {
251 dri_get_drawable(prp
);
254 return driDriverAPI
.MakeCurrent(pcp
, pdp
, prp
);
260 * \param scrn the screen.
263 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
266 * This function calls __DriverAPIRec::UnbindContext, and then decrements
267 * __DRIdrawableRec::refcount which must be non-zero for a successful
270 * While casting the opaque private pointers associated with the parameters
271 * into their respective real types it also assures they are not \c NULL.
273 static int driUnbindContext(__DRIcontext
*pcp
)
279 ** Assume error checking is done properly in glXMakeCurrent before
280 ** calling driUnbindContext.
286 pdp
= pcp
->driDrawablePriv
;
287 prp
= pcp
->driReadablePriv
;
289 /* already unbound */
293 driDriverAPI
.UnbindContext(pcp
);
296 if (pdp
->refcount
== 0) {
301 dri_put_drawable(pdp
);
304 if (prp
->refcount
== 0) {
309 dri_put_drawable(prp
);
312 /* XXX this is disabled so that if we call SwapBuffers on an unbound
313 * window we can determine the last context bound to the window and
314 * use that context's lock. (BrianP, 2-Dec-2000)
316 pcp
->driDrawablePriv
= NULL
;
317 pcp
->driReadablePriv
= NULL
;
325 static void dri_get_drawable(__DRIdrawable
*pdp
)
330 static void dri_put_drawable(__DRIdrawable
*pdp
)
337 driDriverAPI
.DestroyBuffer(pdp
);
342 static __DRIdrawable
*
343 dri2CreateNewDrawable(__DRIscreen
*screen
,
344 const __DRIconfig
*config
,
347 __DRIdrawable
*pdraw
;
349 pdraw
= malloc(sizeof *pdraw
);
353 pdraw
->loaderPrivate
= data
;
355 pdraw
->driScreenPriv
= screen
;
356 pdraw
->driContextPriv
= NULL
;
358 pdraw
->lastStamp
= 0;
362 dri_get_drawable(pdraw
);
364 if (!driDriverAPI
.CreateBuffer(screen
, pdraw
, &config
->modes
, GL_FALSE
)) {
369 pdraw
->dri2
.stamp
= pdraw
->lastStamp
+ 1;
375 driDestroyDrawable(__DRIdrawable
*pdp
)
377 dri_put_drawable(pdp
);
381 dri2AllocateBuffer(__DRIscreen
*screen
,
382 unsigned int attachment
, unsigned int format
,
383 int width
, int height
)
385 return driDriverAPI
.AllocateBuffer(screen
, attachment
, format
,
390 dri2ReleaseBuffer(__DRIscreen
*screen
, __DRIbuffer
*buffer
)
392 driDriverAPI
.ReleaseBuffer(screen
, buffer
);
397 dri2ConfigQueryb(__DRIscreen
*screen
, const char *var
, GLboolean
*val
)
399 if (!driCheckOption(&screen
->optionCache
, var
, DRI_BOOL
))
402 *val
= driQueryOptionb(&screen
->optionCache
, var
);
408 dri2ConfigQueryi(__DRIscreen
*screen
, const char *var
, GLint
*val
)
410 if (!driCheckOption(&screen
->optionCache
, var
, DRI_INT
) &&
411 !driCheckOption(&screen
->optionCache
, var
, DRI_ENUM
))
414 *val
= driQueryOptioni(&screen
->optionCache
, var
);
420 dri2ConfigQueryf(__DRIscreen
*screen
, const char *var
, GLfloat
*val
)
422 if (!driCheckOption(&screen
->optionCache
, var
, DRI_FLOAT
))
425 *val
= driQueryOptionf(&screen
->optionCache
, var
);
431 dri2GetAPIMask(__DRIscreen
*screen
)
433 return screen
->api_mask
;
437 /** Core interface */
438 const __DRIcoreExtension driCoreExtension
= {
439 { __DRI_CORE
, __DRI_CORE_VERSION
},
444 driIndexConfigAttrib
,
455 /** DRI2 interface */
456 const __DRIdri2Extension driDRI2Extension
= {
457 { __DRI_DRI2
, __DRI_DRI2_VERSION
},
459 dri2CreateNewDrawable
,
460 dri2CreateNewContext
,
462 dri2CreateNewContextForAPI
,
467 const __DRI2configQueryExtension dri2ConfigQueryExtension
= {
468 { __DRI2_CONFIG_QUERY
, __DRI2_CONFIG_QUERY_VERSION
},
475 dri2InvalidateDrawable(__DRIdrawable
*drawable
)
477 drawable
->dri2
.stamp
++;
481 * Check that the gl_framebuffer associated with dPriv is the right size.
482 * Resize the gl_framebuffer if needed.
483 * It's expected that the dPriv->driverPrivate member points to a
484 * gl_framebuffer object.
487 driUpdateFramebufferSize(struct gl_context
*ctx
, const __DRIdrawable
*dPriv
)
489 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) dPriv
->driverPrivate
;
490 if (fb
&& (dPriv
->w
!= fb
->Width
|| dPriv
->h
!= fb
->Height
)) {
491 ctx
->Driver
.ResizeBuffers(ctx
, fb
, dPriv
->w
, dPriv
->h
);
492 /* if the driver needs the hw lock for ResizeBuffers, the drawable
493 might have changed again by now */
494 assert(fb
->Width
== dPriv
->w
);
495 assert(fb
->Height
== dPriv
->h
);