dri: Remove dead comment.
[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 pcp->driDrawablePriv = NULL;
439 pcp->driReadablePriv = NULL;
440
441 return GL_TRUE;
442 }
443
444 /*@}*/
445
446
447 static void dri_get_drawable(__DRIdrawable *pdp)
448 {
449 pdp->refcount++;
450 }
451
452 static void dri_put_drawable(__DRIdrawable *pdp)
453 {
454 if (pdp) {
455 pdp->refcount--;
456 if (pdp->refcount)
457 return;
458
459 driDriverAPI.DestroyBuffer(pdp);
460 free(pdp);
461 }
462 }
463
464 static __DRIdrawable *
465 dri2CreateNewDrawable(__DRIscreen *screen,
466 const __DRIconfig *config,
467 void *data)
468 {
469 __DRIdrawable *pdraw;
470
471 pdraw = malloc(sizeof *pdraw);
472 if (!pdraw)
473 return NULL;
474
475 pdraw->loaderPrivate = data;
476
477 pdraw->driScreenPriv = screen;
478 pdraw->driContextPriv = NULL;
479 pdraw->refcount = 0;
480 pdraw->lastStamp = 0;
481 pdraw->w = 0;
482 pdraw->h = 0;
483
484 dri_get_drawable(pdraw);
485
486 if (!driDriverAPI.CreateBuffer(screen, pdraw, &config->modes, GL_FALSE)) {
487 free(pdraw);
488 return NULL;
489 }
490
491 pdraw->dri2.stamp = pdraw->lastStamp + 1;
492
493 return pdraw;
494 }
495
496 static void
497 driDestroyDrawable(__DRIdrawable *pdp)
498 {
499 dri_put_drawable(pdp);
500 }
501
502 static __DRIbuffer *
503 dri2AllocateBuffer(__DRIscreen *screen,
504 unsigned int attachment, unsigned int format,
505 int width, int height)
506 {
507 return driDriverAPI.AllocateBuffer(screen, attachment, format,
508 width, height);
509 }
510
511 static void
512 dri2ReleaseBuffer(__DRIscreen *screen, __DRIbuffer *buffer)
513 {
514 driDriverAPI.ReleaseBuffer(screen, buffer);
515 }
516
517
518 static int
519 dri2ConfigQueryb(__DRIscreen *screen, const char *var, GLboolean *val)
520 {
521 if (!driCheckOption(&screen->optionCache, var, DRI_BOOL))
522 return -1;
523
524 *val = driQueryOptionb(&screen->optionCache, var);
525
526 return 0;
527 }
528
529 static int
530 dri2ConfigQueryi(__DRIscreen *screen, const char *var, GLint *val)
531 {
532 if (!driCheckOption(&screen->optionCache, var, DRI_INT) &&
533 !driCheckOption(&screen->optionCache, var, DRI_ENUM))
534 return -1;
535
536 *val = driQueryOptioni(&screen->optionCache, var);
537
538 return 0;
539 }
540
541 static int
542 dri2ConfigQueryf(__DRIscreen *screen, const char *var, GLfloat *val)
543 {
544 if (!driCheckOption(&screen->optionCache, var, DRI_FLOAT))
545 return -1;
546
547 *val = driQueryOptionf(&screen->optionCache, var);
548
549 return 0;
550 }
551
552 static unsigned int
553 dri2GetAPIMask(__DRIscreen *screen)
554 {
555 return screen->api_mask;
556 }
557
558
559 /** Core interface */
560 const __DRIcoreExtension driCoreExtension = {
561 .base = { __DRI_CORE, __DRI_CORE_VERSION },
562
563 .createNewScreen = NULL,
564 .destroyScreen = driDestroyScreen,
565 .getExtensions = driGetExtensions,
566 .getConfigAttrib = driGetConfigAttrib,
567 .indexConfigAttrib = driIndexConfigAttrib,
568 .createNewDrawable = NULL,
569 .destroyDrawable = driDestroyDrawable,
570 .swapBuffers = NULL,
571 .createNewContext = NULL,
572 .copyContext = driCopyContext,
573 .destroyContext = driDestroyContext,
574 .bindContext = driBindContext,
575 .unbindContext = driUnbindContext
576 };
577
578 /** DRI2 interface */
579 const __DRIdri2Extension driDRI2Extension = {
580 .base = { __DRI_DRI2, 3 },
581
582 .createNewScreen = dri2CreateNewScreen,
583 .createNewDrawable = dri2CreateNewDrawable,
584 .createNewContext = dri2CreateNewContext,
585 .getAPIMask = dri2GetAPIMask,
586 .createNewContextForAPI = dri2CreateNewContextForAPI,
587 .allocateBuffer = dri2AllocateBuffer,
588 .releaseBuffer = dri2ReleaseBuffer,
589 .createContextAttribs = dri2CreateContextAttribs
590 };
591
592 const __DRI2configQueryExtension dri2ConfigQueryExtension = {
593 .base = { __DRI2_CONFIG_QUERY, __DRI2_CONFIG_QUERY_VERSION },
594
595 .configQueryb = dri2ConfigQueryb,
596 .configQueryi = dri2ConfigQueryi,
597 .configQueryf = dri2ConfigQueryf,
598 };
599
600 void
601 dri2InvalidateDrawable(__DRIdrawable *drawable)
602 {
603 drawable->dri2.stamp++;
604 }
605
606 /**
607 * Check that the gl_framebuffer associated with dPriv is the right size.
608 * Resize the gl_framebuffer if needed.
609 * It's expected that the dPriv->driverPrivate member points to a
610 * gl_framebuffer object.
611 */
612 void
613 driUpdateFramebufferSize(struct gl_context *ctx, const __DRIdrawable *dPriv)
614 {
615 struct gl_framebuffer *fb = (struct gl_framebuffer *) dPriv->driverPrivate;
616 if (fb && (dPriv->w != fb->Width || dPriv->h != fb->Height)) {
617 ctx->Driver.ResizeBuffers(ctx, fb, dPriv->w, dPriv->h);
618 /* if the driver needs the hw lock for ResizeBuffers, the drawable
619 might have changed again by now */
620 assert(fb->Width == dPriv->w);
621 assert(fb->Height == dPriv->h);
622 }
623 }