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