dri: cosmetic
[mesa.git] / src / mesa / drivers / dri / common / dri_util.c
1 /**
2 * \file dri_util.c
3 * DRI utility functions.
4 *
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
8 * drivers.
9 *
10 * Basically, these utility functions take care of some of the dirty details of
11 * screen initialization, context creation, context binding, DRM setup, etc.
12 *
13 * These functions are compiled into each DRI driver so libGL.so knows nothing
14 * about them.
15 */
16
17
18 #include <xf86drm.h>
19 #include "dri_util.h"
20 #include "utils.h"
21 #include "xmlpool.h"
22 #include "../glsl/glsl_parser_extras.h"
23
24 PUBLIC const char __dri2ConfigOptions[] =
25 DRI_CONF_BEGIN
26 DRI_CONF_SECTION_PERFORMANCE
27 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_1)
28 DRI_CONF_SECTION_END
29 DRI_CONF_END;
30
31 static const uint __dri2NConfigOptions = 1;
32
33 /*****************************************************************/
34 /** \name Screen handling functions */
35 /*****************************************************************/
36 /*@{*/
37
38 static void
39 setupLoaderExtensions(__DRIscreen *psp,
40 const __DRIextension **extensions)
41 {
42 int i;
43
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];
51 }
52 }
53
54 static __DRIscreen *
55 dri2CreateNewScreen(int scrn, int fd,
56 const __DRIextension **extensions,
57 const __DRIconfig ***driver_configs, void *data)
58 {
59 static const __DRIextension *emptyExtensionList[] = { NULL };
60 __DRIscreen *psp;
61 drmVersionPtr version;
62
63 psp = calloc(1, sizeof(*psp));
64 if (!psp)
65 return NULL;
66
67 setupLoaderExtensions(psp, extensions);
68
69 version = drmGetVersion(fd);
70 if (version) {
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);
75 }
76
77 psp->loaderPrivate = data;
78
79 psp->extensions = emptyExtensionList;
80 psp->fd = fd;
81 psp->myNum = scrn;
82
83 psp->api_mask = (1 << __DRI_API_OPENGL);
84
85 *driver_configs = driDriverAPI.InitScreen(psp);
86 if (*driver_configs == NULL) {
87 free(psp);
88 return NULL;
89 }
90
91 driParseOptionInfo(&psp->optionInfo, __dri2ConfigOptions, __dri2NConfigOptions);
92 driParseConfigFiles(&psp->optionCache, &psp->optionInfo, psp->myNum, "dri2");
93
94 return psp;
95 }
96
97 /**
98 * Destroy the per-screen private information.
99 *
100 * \internal
101 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
102 * drmClose(), and finally frees \p screenPrivate.
103 */
104 static void driDestroyScreen(__DRIscreen *psp)
105 {
106 if (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.
110 */
111
112 _mesa_destroy_shader_compiler();
113
114 driDriverAPI.DestroyScreen(psp);
115
116 driDestroyOptionCache(&psp->optionCache);
117 driDestroyOptionInfo(&psp->optionInfo);
118
119 free(psp);
120 }
121 }
122
123 static const __DRIextension **driGetExtensions(__DRIscreen *psp)
124 {
125 return psp->extensions;
126 }
127
128 /*@}*/
129
130
131 /*****************************************************************/
132 /** \name Context handling functions */
133 /*****************************************************************/
134 /*@{*/
135
136 static __DRIcontext *
137 dri2CreateNewContextForAPI(__DRIscreen *screen, int api,
138 const __DRIconfig *config,
139 __DRIcontext *shared, void *data)
140 {
141 __DRIcontext *context;
142 const struct gl_config *modes = (config != NULL) ? &config->modes : NULL;
143 void *shareCtx = (shared != NULL) ? shared->driverPrivate : NULL;
144 gl_api mesa_api;
145
146 if (!(screen->api_mask & (1 << api)))
147 return NULL;
148
149 switch (api) {
150 case __DRI_API_OPENGL:
151 mesa_api = API_OPENGL;
152 break;
153 case __DRI_API_GLES:
154 mesa_api = API_OPENGLES;
155 break;
156 case __DRI_API_GLES2:
157 mesa_api = API_OPENGLES2;
158 break;
159 default:
160 return NULL;
161 }
162
163 context = malloc(sizeof *context);
164 if (!context)
165 return NULL;
166
167 context->loaderPrivate = data;
168
169 context->driScreenPriv = screen;
170 context->driDrawablePriv = NULL;
171 context->driReadablePriv = NULL;
172
173 if (!driDriverAPI.CreateContext(mesa_api, modes, context, shareCtx) ) {
174 free(context);
175 return NULL;
176 }
177
178 return context;
179 }
180
181
182 static __DRIcontext *
183 dri2CreateNewContext(__DRIscreen *screen, const __DRIconfig *config,
184 __DRIcontext *shared, void *data)
185 {
186 return dri2CreateNewContextForAPI(screen, __DRI_API_OPENGL,
187 config, shared, data);
188 }
189
190 /**
191 * Destroy the per-context private information.
192 *
193 * \internal
194 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
195 * drmDestroyContext(), and finally frees \p contextPrivate.
196 */
197 static void
198 driDestroyContext(__DRIcontext *pcp)
199 {
200 if (pcp) {
201 driDriverAPI.DestroyContext(pcp);
202 free(pcp);
203 }
204 }
205
206 static int
207 driCopyContext(__DRIcontext *dest, __DRIcontext *src, unsigned long mask)
208 {
209 (void) dest;
210 (void) src;
211 (void) mask;
212 return GL_FALSE;
213 }
214
215 /*@}*/
216
217
218 /*****************************************************************/
219 /** \name Context (un)binding functions */
220 /*****************************************************************/
221 /*@{*/
222
223 static void dri_get_drawable(__DRIdrawable *pdp);
224 static void dri_put_drawable(__DRIdrawable *pdp);
225
226 /**
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
229 * function.
230 */
231 static int driBindContext(__DRIcontext *pcp,
232 __DRIdrawable *pdp,
233 __DRIdrawable *prp)
234 {
235 /*
236 ** Assume error checking is done properly in glXMakeCurrent before
237 ** calling driUnbindContext.
238 */
239
240 if (!pcp)
241 return GL_FALSE;
242
243 /* Bind the drawable to the context */
244 pcp->driDrawablePriv = pdp;
245 pcp->driReadablePriv = prp;
246 if (pdp) {
247 pdp->driContextPriv = pcp;
248 dri_get_drawable(pdp);
249 }
250 if (prp && pdp != prp) {
251 dri_get_drawable(prp);
252 }
253
254 return driDriverAPI.MakeCurrent(pcp, pdp, prp);
255 }
256
257 /**
258 * Unbind context.
259 *
260 * \param scrn the screen.
261 * \param gc context.
262 *
263 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
264 *
265 * \internal
266 * This function calls __DriverAPIRec::UnbindContext, and then decrements
267 * __DRIdrawableRec::refcount which must be non-zero for a successful
268 * return.
269 *
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.
272 */
273 static int driUnbindContext(__DRIcontext *pcp)
274 {
275 __DRIdrawable *pdp;
276 __DRIdrawable *prp;
277
278 /*
279 ** Assume error checking is done properly in glXMakeCurrent before
280 ** calling driUnbindContext.
281 */
282
283 if (pcp == NULL)
284 return GL_FALSE;
285
286 pdp = pcp->driDrawablePriv;
287 prp = pcp->driReadablePriv;
288
289 /* already unbound */
290 if (!pdp && !prp)
291 return GL_TRUE;
292
293 driDriverAPI.UnbindContext(pcp);
294
295 assert(pdp);
296 if (pdp->refcount == 0) {
297 /* ERROR!!! */
298 return GL_FALSE;
299 }
300
301 dri_put_drawable(pdp);
302
303 if (prp != pdp) {
304 if (prp->refcount == 0) {
305 /* ERROR!!! */
306 return GL_FALSE;
307 }
308
309 dri_put_drawable(prp);
310 }
311
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)
315 */
316 pcp->driDrawablePriv = NULL;
317 pcp->driReadablePriv = NULL;
318
319 return GL_TRUE;
320 }
321
322 /*@}*/
323
324
325 static void dri_get_drawable(__DRIdrawable *pdp)
326 {
327 pdp->refcount++;
328 }
329
330 static void dri_put_drawable(__DRIdrawable *pdp)
331 {
332 if (pdp) {
333 pdp->refcount--;
334 if (pdp->refcount)
335 return;
336
337 driDriverAPI.DestroyBuffer(pdp);
338 free(pdp);
339 }
340 }
341
342 static __DRIdrawable *
343 dri2CreateNewDrawable(__DRIscreen *screen,
344 const __DRIconfig *config,
345 void *data)
346 {
347 __DRIdrawable *pdraw;
348
349 pdraw = malloc(sizeof *pdraw);
350 if (!pdraw)
351 return NULL;
352
353 pdraw->loaderPrivate = data;
354
355 pdraw->driScreenPriv = screen;
356 pdraw->driContextPriv = NULL;
357 pdraw->refcount = 0;
358 pdraw->lastStamp = 0;
359 pdraw->w = 0;
360 pdraw->h = 0;
361
362 dri_get_drawable(pdraw);
363
364 if (!driDriverAPI.CreateBuffer(screen, pdraw, &config->modes, GL_FALSE)) {
365 free(pdraw);
366 return NULL;
367 }
368
369 pdraw->dri2.stamp = pdraw->lastStamp + 1;
370
371 return pdraw;
372 }
373
374 static void
375 driDestroyDrawable(__DRIdrawable *pdp)
376 {
377 dri_put_drawable(pdp);
378 }
379
380 static __DRIbuffer *
381 dri2AllocateBuffer(__DRIscreen *screen,
382 unsigned int attachment, unsigned int format,
383 int width, int height)
384 {
385 return driDriverAPI.AllocateBuffer(screen, attachment, format,
386 width, height);
387 }
388
389 static void
390 dri2ReleaseBuffer(__DRIscreen *screen, __DRIbuffer *buffer)
391 {
392 driDriverAPI.ReleaseBuffer(screen, buffer);
393 }
394
395
396 static int
397 dri2ConfigQueryb(__DRIscreen *screen, const char *var, GLboolean *val)
398 {
399 if (!driCheckOption(&screen->optionCache, var, DRI_BOOL))
400 return -1;
401
402 *val = driQueryOptionb(&screen->optionCache, var);
403
404 return 0;
405 }
406
407 static int
408 dri2ConfigQueryi(__DRIscreen *screen, const char *var, GLint *val)
409 {
410 if (!driCheckOption(&screen->optionCache, var, DRI_INT) &&
411 !driCheckOption(&screen->optionCache, var, DRI_ENUM))
412 return -1;
413
414 *val = driQueryOptioni(&screen->optionCache, var);
415
416 return 0;
417 }
418
419 static int
420 dri2ConfigQueryf(__DRIscreen *screen, const char *var, GLfloat *val)
421 {
422 if (!driCheckOption(&screen->optionCache, var, DRI_FLOAT))
423 return -1;
424
425 *val = driQueryOptionf(&screen->optionCache, var);
426
427 return 0;
428 }
429
430 static unsigned int
431 dri2GetAPIMask(__DRIscreen *screen)
432 {
433 return screen->api_mask;
434 }
435
436
437 /** Core interface */
438 const __DRIcoreExtension driCoreExtension = {
439 { __DRI_CORE, __DRI_CORE_VERSION },
440 NULL,
441 driDestroyScreen,
442 driGetExtensions,
443 driGetConfigAttrib,
444 driIndexConfigAttrib,
445 NULL,
446 driDestroyDrawable,
447 NULL,
448 NULL,
449 driCopyContext,
450 driDestroyContext,
451 driBindContext,
452 driUnbindContext
453 };
454
455 /** DRI2 interface */
456 const __DRIdri2Extension driDRI2Extension = {
457 { __DRI_DRI2, __DRI_DRI2_VERSION },
458 dri2CreateNewScreen,
459 dri2CreateNewDrawable,
460 dri2CreateNewContext,
461 dri2GetAPIMask,
462 dri2CreateNewContextForAPI,
463 dri2AllocateBuffer,
464 dri2ReleaseBuffer
465 };
466
467 const __DRI2configQueryExtension dri2ConfigQueryExtension = {
468 { __DRI2_CONFIG_QUERY, __DRI2_CONFIG_QUERY_VERSION },
469 dri2ConfigQueryb,
470 dri2ConfigQueryi,
471 dri2ConfigQueryf,
472 };
473
474 void
475 dri2InvalidateDrawable(__DRIdrawable *drawable)
476 {
477 drawable->dri2.stamp++;
478 }
479
480 /**
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.
485 */
486 void
487 driUpdateFramebufferSize(struct gl_context *ctx, const __DRIdrawable *dPriv)
488 {
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);
496 }
497 }