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