dri: Choose a decent global driNConfigOptions.
[mesa.git] / src / mesa / drivers / dri / common / dri_util.c
1 /*
2 * (C) Copyright IBM Corporation 2002, 2004
3 * All Rights Reserved.
4 *
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:
11 *
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
14 * Software.
15 *
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.
23 */
24
25 /**
26 * \file dri_util.c
27 * DRI utility functions.
28 *
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
32 * drivers.
33 *
34 * Basically, these utility functions take care of some of the dirty details of
35 * screen initialization, context creation, context binding, DRM setup, etc.
36 *
37 * These functions are compiled into each DRI driver so libGL.so knows nothing
38 * about them.
39 */
40
41
42 #include <xf86drm.h>
43 #include "dri_util.h"
44 #include "utils.h"
45 #include "xmlpool.h"
46 #include "../glsl/glsl_parser_extras.h"
47
48 PUBLIC const char __dri2ConfigOptions[] =
49 DRI_CONF_BEGIN
50 DRI_CONF_SECTION_PERFORMANCE
51 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_1)
52 DRI_CONF_SECTION_END
53 DRI_CONF_END;
54
55 /*****************************************************************/
56 /** \name Screen handling functions */
57 /*****************************************************************/
58 /*@{*/
59
60 static void
61 setupLoaderExtensions(__DRIscreen *psp,
62 const __DRIextension **extensions)
63 {
64 int i;
65
66 for (i = 0; extensions[i]; i++) {
67 if (strcmp(extensions[i]->name, __DRI_DRI2_LOADER) == 0)
68 psp->dri2.loader = (__DRIdri2LoaderExtension *) extensions[i];
69 if (strcmp(extensions[i]->name, __DRI_IMAGE_LOOKUP) == 0)
70 psp->dri2.image = (__DRIimageLookupExtension *) extensions[i];
71 if (strcmp(extensions[i]->name, __DRI_USE_INVALIDATE) == 0)
72 psp->dri2.useInvalidate = (__DRIuseInvalidateExtension *) extensions[i];
73 }
74 }
75
76 static __DRIscreen *
77 dri2CreateNewScreen(int scrn, int fd,
78 const __DRIextension **extensions,
79 const __DRIconfig ***driver_configs, void *data)
80 {
81 static const __DRIextension *emptyExtensionList[] = { NULL };
82 __DRIscreen *psp;
83 drmVersionPtr version;
84
85 psp = calloc(1, sizeof(*psp));
86 if (!psp)
87 return NULL;
88
89 setupLoaderExtensions(psp, extensions);
90
91 version = drmGetVersion(fd);
92 if (version) {
93 psp->drm_version.major = version->version_major;
94 psp->drm_version.minor = version->version_minor;
95 psp->drm_version.patch = version->version_patchlevel;
96 drmFreeVersion(version);
97 }
98
99 psp->loaderPrivate = data;
100
101 psp->extensions = emptyExtensionList;
102 psp->fd = fd;
103 psp->myNum = scrn;
104
105 psp->api_mask = (1 << __DRI_API_OPENGL);
106
107 *driver_configs = driDriverAPI.InitScreen(psp);
108 if (*driver_configs == NULL) {
109 free(psp);
110 return NULL;
111 }
112
113 driParseOptionInfo(&psp->optionInfo, __dri2ConfigOptions);
114 driParseConfigFiles(&psp->optionCache, &psp->optionInfo, psp->myNum, "dri2");
115
116 return psp;
117 }
118
119 /**
120 * Destroy the per-screen private information.
121 *
122 * \internal
123 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
124 * drmClose(), and finally frees \p screenPrivate.
125 */
126 static void driDestroyScreen(__DRIscreen *psp)
127 {
128 if (psp) {
129 /* No interaction with the X-server is possible at this point. This
130 * routine is called after XCloseDisplay, so there is no protocol
131 * stream open to the X-server anymore.
132 */
133
134 _mesa_destroy_shader_compiler();
135
136 driDriverAPI.DestroyScreen(psp);
137
138 driDestroyOptionCache(&psp->optionCache);
139 driDestroyOptionInfo(&psp->optionInfo);
140
141 free(psp);
142 }
143 }
144
145 static const __DRIextension **driGetExtensions(__DRIscreen *psp)
146 {
147 return psp->extensions;
148 }
149
150 /*@}*/
151
152
153 /*****************************************************************/
154 /** \name Context handling functions */
155 /*****************************************************************/
156 /*@{*/
157
158 static __DRIcontext *
159 dri2CreateContextAttribs(__DRIscreen *screen, int api,
160 const __DRIconfig *config,
161 __DRIcontext *shared,
162 unsigned num_attribs,
163 const uint32_t *attribs,
164 unsigned *error,
165 void *data)
166 {
167 __DRIcontext *context;
168 const struct gl_config *modes = (config != NULL) ? &config->modes : NULL;
169 void *shareCtx = (shared != NULL) ? shared->driverPrivate : NULL;
170 gl_api mesa_api;
171 unsigned major_version = 1;
172 unsigned minor_version = 0;
173 uint32_t flags = 0;
174
175 assert((num_attribs == 0) || (attribs != NULL));
176
177 if (!(screen->api_mask & (1 << api))) {
178 *error = __DRI_CTX_ERROR_BAD_API;
179 return NULL;
180 }
181
182 switch (api) {
183 case __DRI_API_OPENGL:
184 mesa_api = API_OPENGL_COMPAT;
185 break;
186 case __DRI_API_GLES:
187 mesa_api = API_OPENGLES;
188 break;
189 case __DRI_API_GLES2:
190 case __DRI_API_GLES3:
191 mesa_api = API_OPENGLES2;
192 break;
193 case __DRI_API_OPENGL_CORE:
194 mesa_api = API_OPENGL_CORE;
195 break;
196 default:
197 *error = __DRI_CTX_ERROR_BAD_API;
198 return NULL;
199 }
200
201 for (unsigned i = 0; i < num_attribs; i++) {
202 switch (attribs[i * 2]) {
203 case __DRI_CTX_ATTRIB_MAJOR_VERSION:
204 major_version = attribs[i * 2 + 1];
205 break;
206 case __DRI_CTX_ATTRIB_MINOR_VERSION:
207 minor_version = attribs[i * 2 + 1];
208 break;
209 case __DRI_CTX_ATTRIB_FLAGS:
210 flags = attribs[i * 2 + 1];
211 break;
212 default:
213 /* We can't create a context that satisfies the requirements of an
214 * attribute that we don't understand. Return failure.
215 */
216 assert(!"Should not get here.");
217 *error = __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE;
218 return NULL;
219 }
220 }
221
222 /* Mesa does not support the GL_ARB_compatibilty extension or the
223 * compatibility profile. This means that we treat a API_OPENGL_COMPAT 3.1 as
224 * API_OPENGL_CORE and reject API_OPENGL_COMPAT 3.2+.
225 */
226 if (mesa_api == API_OPENGL_COMPAT && major_version == 3 && minor_version == 1)
227 mesa_api = API_OPENGL_CORE;
228
229 if (mesa_api == API_OPENGL_COMPAT
230 && ((major_version > 3)
231 || (major_version == 3 && minor_version >= 2))) {
232 *error = __DRI_CTX_ERROR_BAD_API;
233 return NULL;
234 }
235
236 /* The EGL_KHR_create_context spec says:
237 *
238 * "Flags are only defined for OpenGL context creation, and specifying
239 * a flags value other than zero for other types of contexts,
240 * including OpenGL ES contexts, will generate an error."
241 *
242 * The GLX_EXT_create_context_es2_profile specification doesn't say
243 * anything specific about this case. However, none of the known flags
244 * have any meaning in an ES context, so this seems safe.
245 */
246 if (mesa_api != API_OPENGL_COMPAT
247 && mesa_api != API_OPENGL_CORE
248 && flags != 0) {
249 *error = __DRI_CTX_ERROR_BAD_FLAG;
250 return NULL;
251 }
252
253 /* There are no forward-compatible contexts before OpenGL 3.0. The
254 * GLX_ARB_create_context spec says:
255 *
256 * "Forward-compatible contexts are defined only for OpenGL versions
257 * 3.0 and later."
258 *
259 * Forward-looking contexts are supported by silently converting the
260 * requested API to API_OPENGL_CORE.
261 *
262 * In Mesa, a debug context is the same as a regular context.
263 */
264 if ((flags & __DRI_CTX_FLAG_FORWARD_COMPATIBLE) != 0) {
265 mesa_api = API_OPENGL_CORE;
266 }
267
268 if ((flags & ~(__DRI_CTX_FLAG_DEBUG | __DRI_CTX_FLAG_FORWARD_COMPATIBLE))
269 != 0) {
270 *error = __DRI_CTX_ERROR_UNKNOWN_FLAG;
271 return NULL;
272 }
273
274 context = calloc(1, sizeof *context);
275 if (!context) {
276 *error = __DRI_CTX_ERROR_NO_MEMORY;
277 return NULL;
278 }
279
280 context->loaderPrivate = data;
281
282 context->driScreenPriv = screen;
283 context->driDrawablePriv = NULL;
284 context->driReadablePriv = NULL;
285
286 if (!driDriverAPI.CreateContext(mesa_api, modes, context,
287 major_version, minor_version,
288 flags, error, shareCtx) ) {
289 free(context);
290 return NULL;
291 }
292
293 *error = __DRI_CTX_ERROR_SUCCESS;
294 return context;
295 }
296
297 static __DRIcontext *
298 dri2CreateNewContextForAPI(__DRIscreen *screen, int api,
299 const __DRIconfig *config,
300 __DRIcontext *shared, void *data)
301 {
302 unsigned error;
303
304 return dri2CreateContextAttribs(screen, api, config, shared, 0, NULL,
305 &error, data);
306 }
307
308 static __DRIcontext *
309 dri2CreateNewContext(__DRIscreen *screen, const __DRIconfig *config,
310 __DRIcontext *shared, void *data)
311 {
312 return dri2CreateNewContextForAPI(screen, __DRI_API_OPENGL,
313 config, shared, data);
314 }
315
316 /**
317 * Destroy the per-context private information.
318 *
319 * \internal
320 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
321 * drmDestroyContext(), and finally frees \p contextPrivate.
322 */
323 static void
324 driDestroyContext(__DRIcontext *pcp)
325 {
326 if (pcp) {
327 driDriverAPI.DestroyContext(pcp);
328 free(pcp);
329 }
330 }
331
332 static int
333 driCopyContext(__DRIcontext *dest, __DRIcontext *src, unsigned long mask)
334 {
335 (void) dest;
336 (void) src;
337 (void) mask;
338 return GL_FALSE;
339 }
340
341 /*@}*/
342
343
344 /*****************************************************************/
345 /** \name Context (un)binding functions */
346 /*****************************************************************/
347 /*@{*/
348
349 static void dri_get_drawable(__DRIdrawable *pdp);
350 static void dri_put_drawable(__DRIdrawable *pdp);
351
352 /**
353 * This function takes both a read buffer and a draw buffer. This is needed
354 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
355 * function.
356 */
357 static int driBindContext(__DRIcontext *pcp,
358 __DRIdrawable *pdp,
359 __DRIdrawable *prp)
360 {
361 /*
362 ** Assume error checking is done properly in glXMakeCurrent before
363 ** calling driUnbindContext.
364 */
365
366 if (!pcp)
367 return GL_FALSE;
368
369 /* Bind the drawable to the context */
370 pcp->driDrawablePriv = pdp;
371 pcp->driReadablePriv = prp;
372 if (pdp) {
373 pdp->driContextPriv = pcp;
374 dri_get_drawable(pdp);
375 }
376 if (prp && pdp != prp) {
377 dri_get_drawable(prp);
378 }
379
380 return driDriverAPI.MakeCurrent(pcp, pdp, prp);
381 }
382
383 /**
384 * Unbind context.
385 *
386 * \param scrn the screen.
387 * \param gc context.
388 *
389 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
390 *
391 * \internal
392 * This function calls __DriverAPIRec::UnbindContext, and then decrements
393 * __DRIdrawableRec::refcount which must be non-zero for a successful
394 * return.
395 *
396 * While casting the opaque private pointers associated with the parameters
397 * into their respective real types it also assures they are not \c NULL.
398 */
399 static int driUnbindContext(__DRIcontext *pcp)
400 {
401 __DRIdrawable *pdp;
402 __DRIdrawable *prp;
403
404 /*
405 ** Assume error checking is done properly in glXMakeCurrent before
406 ** calling driUnbindContext.
407 */
408
409 if (pcp == NULL)
410 return GL_FALSE;
411
412 pdp = pcp->driDrawablePriv;
413 prp = pcp->driReadablePriv;
414
415 /* already unbound */
416 if (!pdp && !prp)
417 return GL_TRUE;
418
419 driDriverAPI.UnbindContext(pcp);
420
421 assert(pdp);
422 if (pdp->refcount == 0) {
423 /* ERROR!!! */
424 return GL_FALSE;
425 }
426
427 dri_put_drawable(pdp);
428
429 if (prp != pdp) {
430 if (prp->refcount == 0) {
431 /* ERROR!!! */
432 return GL_FALSE;
433 }
434
435 dri_put_drawable(prp);
436 }
437
438 /* XXX this is disabled so that if we call SwapBuffers on an unbound
439 * window we can determine the last context bound to the window and
440 * use that context's lock. (BrianP, 2-Dec-2000)
441 */
442 pcp->driDrawablePriv = NULL;
443 pcp->driReadablePriv = NULL;
444
445 return GL_TRUE;
446 }
447
448 /*@}*/
449
450
451 static void dri_get_drawable(__DRIdrawable *pdp)
452 {
453 pdp->refcount++;
454 }
455
456 static void dri_put_drawable(__DRIdrawable *pdp)
457 {
458 if (pdp) {
459 pdp->refcount--;
460 if (pdp->refcount)
461 return;
462
463 driDriverAPI.DestroyBuffer(pdp);
464 free(pdp);
465 }
466 }
467
468 static __DRIdrawable *
469 dri2CreateNewDrawable(__DRIscreen *screen,
470 const __DRIconfig *config,
471 void *data)
472 {
473 __DRIdrawable *pdraw;
474
475 pdraw = malloc(sizeof *pdraw);
476 if (!pdraw)
477 return NULL;
478
479 pdraw->loaderPrivate = data;
480
481 pdraw->driScreenPriv = screen;
482 pdraw->driContextPriv = NULL;
483 pdraw->refcount = 0;
484 pdraw->lastStamp = 0;
485 pdraw->w = 0;
486 pdraw->h = 0;
487
488 dri_get_drawable(pdraw);
489
490 if (!driDriverAPI.CreateBuffer(screen, pdraw, &config->modes, GL_FALSE)) {
491 free(pdraw);
492 return NULL;
493 }
494
495 pdraw->dri2.stamp = pdraw->lastStamp + 1;
496
497 return pdraw;
498 }
499
500 static void
501 driDestroyDrawable(__DRIdrawable *pdp)
502 {
503 dri_put_drawable(pdp);
504 }
505
506 static __DRIbuffer *
507 dri2AllocateBuffer(__DRIscreen *screen,
508 unsigned int attachment, unsigned int format,
509 int width, int height)
510 {
511 return driDriverAPI.AllocateBuffer(screen, attachment, format,
512 width, height);
513 }
514
515 static void
516 dri2ReleaseBuffer(__DRIscreen *screen, __DRIbuffer *buffer)
517 {
518 driDriverAPI.ReleaseBuffer(screen, buffer);
519 }
520
521
522 static int
523 dri2ConfigQueryb(__DRIscreen *screen, const char *var, GLboolean *val)
524 {
525 if (!driCheckOption(&screen->optionCache, var, DRI_BOOL))
526 return -1;
527
528 *val = driQueryOptionb(&screen->optionCache, var);
529
530 return 0;
531 }
532
533 static int
534 dri2ConfigQueryi(__DRIscreen *screen, const char *var, GLint *val)
535 {
536 if (!driCheckOption(&screen->optionCache, var, DRI_INT) &&
537 !driCheckOption(&screen->optionCache, var, DRI_ENUM))
538 return -1;
539
540 *val = driQueryOptioni(&screen->optionCache, var);
541
542 return 0;
543 }
544
545 static int
546 dri2ConfigQueryf(__DRIscreen *screen, const char *var, GLfloat *val)
547 {
548 if (!driCheckOption(&screen->optionCache, var, DRI_FLOAT))
549 return -1;
550
551 *val = driQueryOptionf(&screen->optionCache, var);
552
553 return 0;
554 }
555
556 static unsigned int
557 dri2GetAPIMask(__DRIscreen *screen)
558 {
559 return screen->api_mask;
560 }
561
562
563 /** Core interface */
564 const __DRIcoreExtension driCoreExtension = {
565 .base = { __DRI_CORE, __DRI_CORE_VERSION },
566
567 .createNewScreen = NULL,
568 .destroyScreen = driDestroyScreen,
569 .getExtensions = driGetExtensions,
570 .getConfigAttrib = driGetConfigAttrib,
571 .indexConfigAttrib = driIndexConfigAttrib,
572 .createNewDrawable = NULL,
573 .destroyDrawable = driDestroyDrawable,
574 .swapBuffers = NULL,
575 .createNewContext = NULL,
576 .copyContext = driCopyContext,
577 .destroyContext = driDestroyContext,
578 .bindContext = driBindContext,
579 .unbindContext = driUnbindContext
580 };
581
582 /** DRI2 interface */
583 const __DRIdri2Extension driDRI2Extension = {
584 .base = { __DRI_DRI2, 3 },
585
586 .createNewScreen = dri2CreateNewScreen,
587 .createNewDrawable = dri2CreateNewDrawable,
588 .createNewContext = dri2CreateNewContext,
589 .getAPIMask = dri2GetAPIMask,
590 .createNewContextForAPI = dri2CreateNewContextForAPI,
591 .allocateBuffer = dri2AllocateBuffer,
592 .releaseBuffer = dri2ReleaseBuffer,
593 .createContextAttribs = dri2CreateContextAttribs
594 };
595
596 const __DRI2configQueryExtension dri2ConfigQueryExtension = {
597 .base = { __DRI2_CONFIG_QUERY, __DRI2_CONFIG_QUERY_VERSION },
598
599 .configQueryb = dri2ConfigQueryb,
600 .configQueryi = dri2ConfigQueryi,
601 .configQueryf = dri2ConfigQueryf,
602 };
603
604 void
605 dri2InvalidateDrawable(__DRIdrawable *drawable)
606 {
607 drawable->dri2.stamp++;
608 }
609
610 /**
611 * Check that the gl_framebuffer associated with dPriv is the right size.
612 * Resize the gl_framebuffer if needed.
613 * It's expected that the dPriv->driverPrivate member points to a
614 * gl_framebuffer object.
615 */
616 void
617 driUpdateFramebufferSize(struct gl_context *ctx, const __DRIdrawable *dPriv)
618 {
619 struct gl_framebuffer *fb = (struct gl_framebuffer *) dPriv->driverPrivate;
620 if (fb && (dPriv->w != fb->Width || dPriv->h != fb->Height)) {
621 ctx->Driver.ResizeBuffers(ctx, fb, dPriv->w, dPriv->h);
622 /* if the driver needs the hw lock for ResizeBuffers, the drawable
623 might have changed again by now */
624 assert(fb->Width == dPriv->w);
625 assert(fb->Height == dPriv->h);
626 }
627 }