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