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