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