bfae0209d1862d9d54bf8f3f836f0ff726cc6067
[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 <stdbool.h>
43 #include "dri_util.h"
44 #include "utils.h"
45 #include "xmlpool.h"
46 #include "main/mtypes.h"
47 #include "main/framebuffer.h"
48 #include "main/version.h"
49 #include "main/debug_output.h"
50 #include "main/errors.h"
51 #include "main/macros.h"
52
53 const char __dri2ConfigOptions[] =
54 DRI_CONF_BEGIN
55 DRI_CONF_SECTION_PERFORMANCE
56 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_1)
57 DRI_CONF_SECTION_END
58 DRI_CONF_END;
59
60 /*****************************************************************/
61 /** \name Screen handling functions */
62 /*****************************************************************/
63 /*@{*/
64
65 static void
66 setupLoaderExtensions(__DRIscreen *psp,
67 const __DRIextension **extensions)
68 {
69 int i;
70
71 for (i = 0; extensions[i]; i++) {
72 if (strcmp(extensions[i]->name, __DRI_DRI2_LOADER) == 0)
73 psp->dri2.loader = (__DRIdri2LoaderExtension *) extensions[i];
74 if (strcmp(extensions[i]->name, __DRI_IMAGE_LOOKUP) == 0)
75 psp->dri2.image = (__DRIimageLookupExtension *) extensions[i];
76 if (strcmp(extensions[i]->name, __DRI_USE_INVALIDATE) == 0)
77 psp->dri2.useInvalidate = (__DRIuseInvalidateExtension *) extensions[i];
78 if (strcmp(extensions[i]->name, __DRI_BACKGROUND_CALLABLE) == 0)
79 psp->dri2.backgroundCallable = (__DRIbackgroundCallableExtension *) extensions[i];
80 if (strcmp(extensions[i]->name, __DRI_SWRAST_LOADER) == 0)
81 psp->swrast_loader = (__DRIswrastLoaderExtension *) extensions[i];
82 if (strcmp(extensions[i]->name, __DRI_IMAGE_LOADER) == 0)
83 psp->image.loader = (__DRIimageLoaderExtension *) extensions[i];
84 }
85 }
86
87 /**
88 * This pointer determines which driver API we'll use in the case of the
89 * loader not passing us an explicit driver extensions list (that would,
90 * itself, contain a pointer to a driver API.)
91 *
92 * A driver's driDriverGetExtensions_drivername() can update this pointer to
93 * what it's returning, and a loader that is ignorant of createNewScreen2()
94 * will get the correct driver screen created, as long as no other
95 * driDriverGetExtensions() happened in between the first one and the
96 * createNewScreen().
97 *
98 * This allows the X Server to not require the significant dri_interface.h
99 * updates for doing createNewScreen2(), which would discourage backporting of
100 * the X Server patches to support the new loader interface.
101 */
102 const struct __DriverAPIRec *globalDriverAPI = &driDriverAPI;
103
104 /**
105 * This is the first entrypoint in the driver called by the DRI driver loader
106 * after dlopen()ing it.
107 *
108 * It's used to create global state for the driver across contexts on the same
109 * Display.
110 */
111 static __DRIscreen *
112 driCreateNewScreen2(int scrn, int fd,
113 const __DRIextension **extensions,
114 const __DRIextension **driver_extensions,
115 const __DRIconfig ***driver_configs, void *data)
116 {
117 static const __DRIextension *emptyExtensionList[] = { NULL };
118 __DRIscreen *psp;
119
120 psp = calloc(1, sizeof(*psp));
121 if (!psp)
122 return NULL;
123
124 /* By default, use the global driDriverAPI symbol (non-megadrivers). */
125 psp->driver = globalDriverAPI;
126
127 /* If the driver exposes its vtable through its extensions list
128 * (megadrivers), use that instead.
129 */
130 if (driver_extensions) {
131 for (int i = 0; driver_extensions[i]; i++) {
132 if (strcmp(driver_extensions[i]->name, __DRI_DRIVER_VTABLE) == 0) {
133 psp->driver =
134 ((__DRIDriverVtableExtension *)driver_extensions[i])->vtable;
135 }
136 }
137 }
138
139 setupLoaderExtensions(psp, extensions);
140
141 psp->loaderPrivate = data;
142
143 psp->extensions = emptyExtensionList;
144 psp->fd = fd;
145 psp->myNum = scrn;
146
147 *driver_configs = psp->driver->InitScreen(psp);
148 if (*driver_configs == NULL) {
149 free(psp);
150 return NULL;
151 }
152
153 struct gl_constants consts = { 0 };
154 gl_api api;
155 unsigned version;
156
157 api = API_OPENGLES2;
158 if (_mesa_override_gl_version_contextless(&consts, &api, &version))
159 psp->max_gl_es2_version = version;
160
161 api = API_OPENGL_COMPAT;
162 if (_mesa_override_gl_version_contextless(&consts, &api, &version)) {
163 if (api == API_OPENGL_CORE) {
164 psp->max_gl_core_version = version;
165 } else {
166 psp->max_gl_compat_version = version;
167 }
168 }
169
170 psp->api_mask = 0;
171 if (psp->max_gl_compat_version > 0)
172 psp->api_mask |= (1 << __DRI_API_OPENGL);
173 if (psp->max_gl_core_version > 0)
174 psp->api_mask |= (1 << __DRI_API_OPENGL_CORE);
175 if (psp->max_gl_es1_version > 0)
176 psp->api_mask |= (1 << __DRI_API_GLES);
177 if (psp->max_gl_es2_version > 0)
178 psp->api_mask |= (1 << __DRI_API_GLES2);
179 if (psp->max_gl_es2_version >= 30)
180 psp->api_mask |= (1 << __DRI_API_GLES3);
181
182 driParseOptionInfo(&psp->optionInfo, __dri2ConfigOptions);
183 driParseConfigFiles(&psp->optionCache, &psp->optionInfo, psp->myNum, "dri2");
184
185
186 return psp;
187 }
188
189 static __DRIscreen *
190 dri2CreateNewScreen(int scrn, int fd,
191 const __DRIextension **extensions,
192 const __DRIconfig ***driver_configs, void *data)
193 {
194 return driCreateNewScreen2(scrn, fd, extensions, NULL,
195 driver_configs, data);
196 }
197
198 /** swrast driver createNewScreen entrypoint. */
199 static __DRIscreen *
200 driSWRastCreateNewScreen(int scrn, const __DRIextension **extensions,
201 const __DRIconfig ***driver_configs, void *data)
202 {
203 return driCreateNewScreen2(scrn, -1, extensions, NULL,
204 driver_configs, data);
205 }
206
207 static __DRIscreen *
208 driSWRastCreateNewScreen2(int scrn, const __DRIextension **extensions,
209 const __DRIextension **driver_extensions,
210 const __DRIconfig ***driver_configs, void *data)
211 {
212 return driCreateNewScreen2(scrn, -1, extensions, driver_extensions,
213 driver_configs, data);
214 }
215
216 /**
217 * Destroy the per-screen private information.
218 *
219 * \internal
220 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
221 * drmClose(), and finally frees \p screenPrivate.
222 */
223 static void driDestroyScreen(__DRIscreen *psp)
224 {
225 if (psp) {
226 /* No interaction with the X-server is possible at this point. This
227 * routine is called after XCloseDisplay, so there is no protocol
228 * stream open to the X-server anymore.
229 */
230
231 psp->driver->DestroyScreen(psp);
232
233 driDestroyOptionCache(&psp->optionCache);
234 driDestroyOptionInfo(&psp->optionInfo);
235
236 free(psp);
237 }
238 }
239
240 static const __DRIextension **driGetExtensions(__DRIscreen *psp)
241 {
242 return psp->extensions;
243 }
244
245 /*@}*/
246
247
248 static bool
249 validate_context_version(__DRIscreen *screen,
250 int mesa_api,
251 unsigned major_version,
252 unsigned minor_version,
253 unsigned *dri_ctx_error)
254 {
255 unsigned req_version = 10 * major_version + minor_version;
256 unsigned max_version = 0;
257
258 switch (mesa_api) {
259 case API_OPENGL_COMPAT:
260 max_version = screen->max_gl_compat_version;
261 break;
262 case API_OPENGL_CORE:
263 max_version = screen->max_gl_core_version;
264 break;
265 case API_OPENGLES:
266 max_version = screen->max_gl_es1_version;
267 break;
268 case API_OPENGLES2:
269 max_version = screen->max_gl_es2_version;
270 break;
271 default:
272 max_version = 0;
273 break;
274 }
275
276 if (max_version == 0) {
277 *dri_ctx_error = __DRI_CTX_ERROR_BAD_API;
278 return false;
279 } else if (req_version > max_version) {
280 *dri_ctx_error = __DRI_CTX_ERROR_BAD_VERSION;
281 return false;
282 }
283
284 return true;
285 }
286
287 /*****************************************************************/
288 /** \name Context handling functions */
289 /*****************************************************************/
290 /*@{*/
291
292 static __DRIcontext *
293 driCreateContextAttribs(__DRIscreen *screen, int api,
294 const __DRIconfig *config,
295 __DRIcontext *shared,
296 unsigned num_attribs,
297 const uint32_t *attribs,
298 unsigned *error,
299 void *data)
300 {
301 __DRIcontext *context;
302 const struct gl_config *modes = (config != NULL) ? &config->modes : NULL;
303 void *shareCtx = (shared != NULL) ? shared->driverPrivate : NULL;
304 gl_api mesa_api;
305 unsigned major_version = 1;
306 unsigned minor_version = 0;
307 uint32_t flags = 0;
308 bool notify_reset = false;
309
310 assert((num_attribs == 0) || (attribs != NULL));
311
312 if (!(screen->api_mask & (1 << api))) {
313 *error = __DRI_CTX_ERROR_BAD_API;
314 return NULL;
315 }
316
317 switch (api) {
318 case __DRI_API_OPENGL:
319 mesa_api = API_OPENGL_COMPAT;
320 break;
321 case __DRI_API_GLES:
322 mesa_api = API_OPENGLES;
323 break;
324 case __DRI_API_GLES2:
325 case __DRI_API_GLES3:
326 mesa_api = API_OPENGLES2;
327 break;
328 case __DRI_API_OPENGL_CORE:
329 mesa_api = API_OPENGL_CORE;
330 break;
331 default:
332 *error = __DRI_CTX_ERROR_BAD_API;
333 return NULL;
334 }
335
336 for (unsigned i = 0; i < num_attribs; i++) {
337 switch (attribs[i * 2]) {
338 case __DRI_CTX_ATTRIB_MAJOR_VERSION:
339 major_version = attribs[i * 2 + 1];
340 break;
341 case __DRI_CTX_ATTRIB_MINOR_VERSION:
342 minor_version = attribs[i * 2 + 1];
343 break;
344 case __DRI_CTX_ATTRIB_FLAGS:
345 flags = attribs[i * 2 + 1];
346 break;
347 case __DRI_CTX_ATTRIB_RESET_STRATEGY:
348 notify_reset = (attribs[i * 2 + 1]
349 != __DRI_CTX_RESET_NO_NOTIFICATION);
350 break;
351 default:
352 /* We can't create a context that satisfies the requirements of an
353 * attribute that we don't understand. Return failure.
354 */
355 assert(!"Should not get here.");
356 *error = __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE;
357 return NULL;
358 }
359 }
360
361 /* Mesa does not support the GL_ARB_compatibilty extension or the
362 * compatibility profile. This means that we treat a API_OPENGL_COMPAT 3.1 as
363 * API_OPENGL_CORE and reject API_OPENGL_COMPAT 3.2+.
364 */
365 if (mesa_api == API_OPENGL_COMPAT && major_version == 3 && minor_version == 1)
366 mesa_api = API_OPENGL_CORE;
367
368 if (mesa_api == API_OPENGL_COMPAT
369 && ((major_version > 3)
370 || (major_version == 3 && minor_version >= 2))) {
371 *error = __DRI_CTX_ERROR_BAD_API;
372 return NULL;
373 }
374
375 /* The latest version of EGL_KHR_create_context spec says:
376 *
377 * "If the EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR flag bit is set in
378 * EGL_CONTEXT_FLAGS_KHR, then a <debug context> will be created.
379 * [...] This bit is supported for OpenGL and OpenGL ES contexts.
380 *
381 * No other EGL_CONTEXT_OPENGL_*_BIT is legal for an ES context.
382 *
383 * However, Mesa's EGL layer translates the context attribute
384 * EGL_CONTEXT_OPENGL_ROBUST_ACCESS into the context flag
385 * __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS. That attribute is legal for ES
386 * (with EGL 1.5 or EGL_EXT_create_context_robustness) and GL (only with
387 * EGL 1.5).
388 *
389 * From the EGL_EXT_create_context_robustness spec:
390 *
391 * This extension is written against the OpenGL ES 2.0 Specification
392 * but can apply to OpenGL ES 1.1 and up.
393 *
394 * From the EGL 1.5 (2014.08.27) spec, p55:
395 *
396 * If the EGL_CONTEXT_OPENGL_ROBUST_ACCESS attribute is set to
397 * EGL_TRUE, a context supporting robust buffer access will be created.
398 * OpenGL contexts must support the GL_ARB_robustness extension, or
399 * equivalent core API functional- ity. OpenGL ES contexts must support
400 * the GL_EXT_robustness extension, or equivalent core API
401 * functionality.
402 */
403 if (mesa_api != API_OPENGL_COMPAT
404 && mesa_api != API_OPENGL_CORE
405 && (flags & ~(__DRI_CTX_FLAG_DEBUG |
406 __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS |
407 __DRI_CTX_FLAG_NO_ERROR))) {
408 *error = __DRI_CTX_ERROR_BAD_FLAG;
409 return NULL;
410 }
411
412 /* There are no forward-compatible contexts before OpenGL 3.0. The
413 * GLX_ARB_create_context spec says:
414 *
415 * "Forward-compatible contexts are defined only for OpenGL versions
416 * 3.0 and later."
417 *
418 * Forward-looking contexts are supported by silently converting the
419 * requested API to API_OPENGL_CORE.
420 *
421 * In Mesa, a debug context is the same as a regular context.
422 */
423 if ((flags & __DRI_CTX_FLAG_FORWARD_COMPATIBLE) != 0) {
424 mesa_api = API_OPENGL_CORE;
425 }
426
427 const uint32_t allowed_flags = (__DRI_CTX_FLAG_DEBUG
428 | __DRI_CTX_FLAG_FORWARD_COMPATIBLE
429 | __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS
430 | __DRI_CTX_FLAG_NO_ERROR);
431 if (flags & ~allowed_flags) {
432 *error = __DRI_CTX_ERROR_UNKNOWN_FLAG;
433 return NULL;
434 }
435
436 if (!validate_context_version(screen, mesa_api,
437 major_version, minor_version, error))
438 return NULL;
439
440 context = calloc(1, sizeof *context);
441 if (!context) {
442 *error = __DRI_CTX_ERROR_NO_MEMORY;
443 return NULL;
444 }
445
446 context->loaderPrivate = data;
447
448 context->driScreenPriv = screen;
449 context->driDrawablePriv = NULL;
450 context->driReadablePriv = NULL;
451
452 if (!screen->driver->CreateContext(mesa_api, modes, context,
453 major_version, minor_version,
454 flags, notify_reset, error, shareCtx)) {
455 free(context);
456 return NULL;
457 }
458
459 *error = __DRI_CTX_ERROR_SUCCESS;
460 return context;
461 }
462
463 void
464 driContextSetFlags(struct gl_context *ctx, uint32_t flags)
465 {
466 if ((flags & __DRI_CTX_FLAG_FORWARD_COMPATIBLE) != 0)
467 ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT;
468 if ((flags & __DRI_CTX_FLAG_DEBUG) != 0) {
469 _mesa_set_debug_state_int(ctx, GL_DEBUG_OUTPUT, GL_TRUE);
470 ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_DEBUG_BIT;
471 }
472 if ((flags & __DRI_CTX_FLAG_NO_ERROR) != 0)
473 ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_NO_ERROR_BIT_KHR;
474 }
475
476 static __DRIcontext *
477 driCreateNewContextForAPI(__DRIscreen *screen, int api,
478 const __DRIconfig *config,
479 __DRIcontext *shared, void *data)
480 {
481 unsigned error;
482
483 return driCreateContextAttribs(screen, api, config, shared, 0, NULL,
484 &error, data);
485 }
486
487 static __DRIcontext *
488 driCreateNewContext(__DRIscreen *screen, const __DRIconfig *config,
489 __DRIcontext *shared, void *data)
490 {
491 return driCreateNewContextForAPI(screen, __DRI_API_OPENGL,
492 config, shared, data);
493 }
494
495 /**
496 * Destroy the per-context private information.
497 *
498 * \internal
499 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
500 * drmDestroyContext(), and finally frees \p contextPrivate.
501 */
502 static void
503 driDestroyContext(__DRIcontext *pcp)
504 {
505 if (pcp) {
506 pcp->driScreenPriv->driver->DestroyContext(pcp);
507 free(pcp);
508 }
509 }
510
511 static int
512 driCopyContext(__DRIcontext *dest, __DRIcontext *src, unsigned long mask)
513 {
514 (void) dest;
515 (void) src;
516 (void) mask;
517 return GL_FALSE;
518 }
519
520 /*@}*/
521
522
523 /*****************************************************************/
524 /** \name Context (un)binding functions */
525 /*****************************************************************/
526 /*@{*/
527
528 static void dri_get_drawable(__DRIdrawable *pdp);
529 static void dri_put_drawable(__DRIdrawable *pdp);
530
531 /**
532 * This function takes both a read buffer and a draw buffer. This is needed
533 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
534 * function.
535 */
536 static int driBindContext(__DRIcontext *pcp,
537 __DRIdrawable *pdp,
538 __DRIdrawable *prp)
539 {
540 /*
541 ** Assume error checking is done properly in glXMakeCurrent before
542 ** calling driUnbindContext.
543 */
544
545 if (!pcp)
546 return GL_FALSE;
547
548 /* Bind the drawable to the context */
549 pcp->driDrawablePriv = pdp;
550 pcp->driReadablePriv = prp;
551 if (pdp) {
552 pdp->driContextPriv = pcp;
553 dri_get_drawable(pdp);
554 }
555 if (prp && pdp != prp) {
556 dri_get_drawable(prp);
557 }
558
559 return pcp->driScreenPriv->driver->MakeCurrent(pcp, pdp, prp);
560 }
561
562 /**
563 * Unbind context.
564 *
565 * \param scrn the screen.
566 * \param gc context.
567 *
568 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
569 *
570 * \internal
571 * This function calls __DriverAPIRec::UnbindContext, and then decrements
572 * __DRIdrawableRec::refcount which must be non-zero for a successful
573 * return.
574 *
575 * While casting the opaque private pointers associated with the parameters
576 * into their respective real types it also assures they are not \c NULL.
577 */
578 static int driUnbindContext(__DRIcontext *pcp)
579 {
580 __DRIdrawable *pdp;
581 __DRIdrawable *prp;
582
583 /*
584 ** Assume error checking is done properly in glXMakeCurrent before
585 ** calling driUnbindContext.
586 */
587
588 if (pcp == NULL)
589 return GL_FALSE;
590
591 /*
592 ** Call driUnbindContext before checking for valid drawables
593 ** to handle surfaceless contexts properly.
594 */
595 pcp->driScreenPriv->driver->UnbindContext(pcp);
596
597 pdp = pcp->driDrawablePriv;
598 prp = pcp->driReadablePriv;
599
600 /* already unbound */
601 if (!pdp && !prp)
602 return GL_TRUE;
603
604 assert(pdp);
605 if (pdp->refcount == 0) {
606 /* ERROR!!! */
607 return GL_FALSE;
608 }
609
610 dri_put_drawable(pdp);
611
612 if (prp != pdp) {
613 if (prp->refcount == 0) {
614 /* ERROR!!! */
615 return GL_FALSE;
616 }
617
618 dri_put_drawable(prp);
619 }
620
621 pcp->driDrawablePriv = NULL;
622 pcp->driReadablePriv = NULL;
623
624 return GL_TRUE;
625 }
626
627 /*@}*/
628
629
630 static void dri_get_drawable(__DRIdrawable *pdp)
631 {
632 pdp->refcount++;
633 }
634
635 static void dri_put_drawable(__DRIdrawable *pdp)
636 {
637 if (pdp) {
638 pdp->refcount--;
639 if (pdp->refcount)
640 return;
641
642 pdp->driScreenPriv->driver->DestroyBuffer(pdp);
643 free(pdp);
644 }
645 }
646
647 static __DRIdrawable *
648 driCreateNewDrawable(__DRIscreen *screen,
649 const __DRIconfig *config,
650 void *data)
651 {
652 __DRIdrawable *pdraw;
653
654 assert(data != NULL);
655
656 pdraw = malloc(sizeof *pdraw);
657 if (!pdraw)
658 return NULL;
659
660 pdraw->loaderPrivate = data;
661
662 pdraw->driScreenPriv = screen;
663 pdraw->driContextPriv = NULL;
664 pdraw->refcount = 0;
665 pdraw->lastStamp = 0;
666 pdraw->w = 0;
667 pdraw->h = 0;
668
669 dri_get_drawable(pdraw);
670
671 if (!screen->driver->CreateBuffer(screen, pdraw, &config->modes,
672 GL_FALSE)) {
673 free(pdraw);
674 return NULL;
675 }
676
677 pdraw->dri2.stamp = pdraw->lastStamp + 1;
678
679 return pdraw;
680 }
681
682 static void
683 driDestroyDrawable(__DRIdrawable *pdp)
684 {
685 /*
686 * The loader's data structures are going away, even if pdp itself stays
687 * around for the time being because it is currently bound. This happens
688 * when a currently bound GLX pixmap is destroyed.
689 *
690 * Clear out the pointer back into the loader's data structures to avoid
691 * accessing an outdated pointer.
692 */
693 pdp->loaderPrivate = NULL;
694
695 dri_put_drawable(pdp);
696 }
697
698 static __DRIbuffer *
699 dri2AllocateBuffer(__DRIscreen *screen,
700 unsigned int attachment, unsigned int format,
701 int width, int height)
702 {
703 return screen->driver->AllocateBuffer(screen, attachment, format,
704 width, height);
705 }
706
707 static void
708 dri2ReleaseBuffer(__DRIscreen *screen, __DRIbuffer *buffer)
709 {
710 screen->driver->ReleaseBuffer(screen, buffer);
711 }
712
713
714 static int
715 dri2ConfigQueryb(__DRIscreen *screen, const char *var, unsigned char *val)
716 {
717 if (!driCheckOption(&screen->optionCache, var, DRI_BOOL))
718 return -1;
719
720 *val = driQueryOptionb(&screen->optionCache, var);
721
722 return 0;
723 }
724
725 static int
726 dri2ConfigQueryi(__DRIscreen *screen, const char *var, int *val)
727 {
728 if (!driCheckOption(&screen->optionCache, var, DRI_INT) &&
729 !driCheckOption(&screen->optionCache, var, DRI_ENUM))
730 return -1;
731
732 *val = driQueryOptioni(&screen->optionCache, var);
733
734 return 0;
735 }
736
737 static int
738 dri2ConfigQueryf(__DRIscreen *screen, const char *var, float *val)
739 {
740 if (!driCheckOption(&screen->optionCache, var, DRI_FLOAT))
741 return -1;
742
743 *val = driQueryOptionf(&screen->optionCache, var);
744
745 return 0;
746 }
747
748 static unsigned int
749 driGetAPIMask(__DRIscreen *screen)
750 {
751 return screen->api_mask;
752 }
753
754 /**
755 * swrast swapbuffers entrypoint.
756 *
757 * DRI2 implements this inside the loader with only flushes handled by the
758 * driver.
759 */
760 static void
761 driSwapBuffers(__DRIdrawable *pdp)
762 {
763 assert(pdp->driScreenPriv->swrast_loader);
764
765 pdp->driScreenPriv->driver->SwapBuffers(pdp);
766 }
767
768 /** Core interface */
769 const __DRIcoreExtension driCoreExtension = {
770 .base = { __DRI_CORE, 1 },
771
772 .createNewScreen = NULL,
773 .destroyScreen = driDestroyScreen,
774 .getExtensions = driGetExtensions,
775 .getConfigAttrib = driGetConfigAttrib,
776 .indexConfigAttrib = driIndexConfigAttrib,
777 .createNewDrawable = NULL,
778 .destroyDrawable = driDestroyDrawable,
779 .swapBuffers = driSwapBuffers, /* swrast */
780 .createNewContext = driCreateNewContext, /* swrast */
781 .copyContext = driCopyContext,
782 .destroyContext = driDestroyContext,
783 .bindContext = driBindContext,
784 .unbindContext = driUnbindContext
785 };
786
787 /** DRI2 interface */
788 const __DRIdri2Extension driDRI2Extension = {
789 .base = { __DRI_DRI2, 4 },
790
791 .createNewScreen = dri2CreateNewScreen,
792 .createNewDrawable = driCreateNewDrawable,
793 .createNewContext = driCreateNewContext,
794 .getAPIMask = driGetAPIMask,
795 .createNewContextForAPI = driCreateNewContextForAPI,
796 .allocateBuffer = dri2AllocateBuffer,
797 .releaseBuffer = dri2ReleaseBuffer,
798 .createContextAttribs = driCreateContextAttribs,
799 .createNewScreen2 = driCreateNewScreen2,
800 };
801
802 const __DRIswrastExtension driSWRastExtension = {
803 .base = { __DRI_SWRAST, 4 },
804
805 .createNewScreen = driSWRastCreateNewScreen,
806 .createNewDrawable = driCreateNewDrawable,
807 .createNewContextForAPI = driCreateNewContextForAPI,
808 .createContextAttribs = driCreateContextAttribs,
809 .createNewScreen2 = driSWRastCreateNewScreen2,
810 };
811
812 const __DRI2configQueryExtension dri2ConfigQueryExtension = {
813 .base = { __DRI2_CONFIG_QUERY, 1 },
814
815 .configQueryb = dri2ConfigQueryb,
816 .configQueryi = dri2ConfigQueryi,
817 .configQueryf = dri2ConfigQueryf,
818 };
819
820 void
821 dri2InvalidateDrawable(__DRIdrawable *drawable)
822 {
823 drawable->dri2.stamp++;
824 }
825
826 /**
827 * Check that the gl_framebuffer associated with dPriv is the right size.
828 * Resize the gl_framebuffer if needed.
829 * It's expected that the dPriv->driverPrivate member points to a
830 * gl_framebuffer object.
831 */
832 void
833 driUpdateFramebufferSize(struct gl_context *ctx, const __DRIdrawable *dPriv)
834 {
835 struct gl_framebuffer *fb = (struct gl_framebuffer *) dPriv->driverPrivate;
836 if (fb && (dPriv->w != fb->Width || dPriv->h != fb->Height)) {
837 _mesa_resize_framebuffer(ctx, fb, dPriv->w, dPriv->h);
838 /* if the driver needs the hw lock for ResizeBuffers, the drawable
839 might have changed again by now */
840 assert(fb->Width == dPriv->w);
841 assert(fb->Height == dPriv->h);
842 }
843 }
844
845 uint32_t
846 driGLFormatToImageFormat(mesa_format format)
847 {
848 switch (format) {
849 case MESA_FORMAT_B5G6R5_UNORM:
850 return __DRI_IMAGE_FORMAT_RGB565;
851 case MESA_FORMAT_B5G5R5A1_UNORM:
852 return __DRI_IMAGE_FORMAT_ARGB1555;
853 case MESA_FORMAT_B8G8R8X8_UNORM:
854 return __DRI_IMAGE_FORMAT_XRGB8888;
855 case MESA_FORMAT_B10G10R10A2_UNORM:
856 return __DRI_IMAGE_FORMAT_ARGB2101010;
857 case MESA_FORMAT_B10G10R10X2_UNORM:
858 return __DRI_IMAGE_FORMAT_XRGB2101010;
859 case MESA_FORMAT_B8G8R8A8_UNORM:
860 return __DRI_IMAGE_FORMAT_ARGB8888;
861 case MESA_FORMAT_R8G8B8A8_UNORM:
862 return __DRI_IMAGE_FORMAT_ABGR8888;
863 case MESA_FORMAT_R8G8B8X8_UNORM:
864 return __DRI_IMAGE_FORMAT_XBGR8888;
865 case MESA_FORMAT_L_UNORM8:
866 case MESA_FORMAT_R_UNORM8:
867 return __DRI_IMAGE_FORMAT_R8;
868 case MESA_FORMAT_L8A8_UNORM:
869 case MESA_FORMAT_R8G8_UNORM:
870 return __DRI_IMAGE_FORMAT_GR88;
871 case MESA_FORMAT_NONE:
872 return __DRI_IMAGE_FORMAT_NONE;
873 case MESA_FORMAT_B8G8R8A8_SRGB:
874 return __DRI_IMAGE_FORMAT_SARGB8;
875 default:
876 return 0;
877 }
878 }
879
880 mesa_format
881 driImageFormatToGLFormat(uint32_t image_format)
882 {
883 switch (image_format) {
884 case __DRI_IMAGE_FORMAT_RGB565:
885 return MESA_FORMAT_B5G6R5_UNORM;
886 case __DRI_IMAGE_FORMAT_ARGB1555:
887 return MESA_FORMAT_B5G5R5A1_UNORM;
888 case __DRI_IMAGE_FORMAT_XRGB8888:
889 return MESA_FORMAT_B8G8R8X8_UNORM;
890 case __DRI_IMAGE_FORMAT_ARGB2101010:
891 return MESA_FORMAT_B10G10R10A2_UNORM;
892 case __DRI_IMAGE_FORMAT_XRGB2101010:
893 return MESA_FORMAT_B10G10R10X2_UNORM;
894 case __DRI_IMAGE_FORMAT_ARGB8888:
895 return MESA_FORMAT_B8G8R8A8_UNORM;
896 case __DRI_IMAGE_FORMAT_ABGR8888:
897 return MESA_FORMAT_R8G8B8A8_UNORM;
898 case __DRI_IMAGE_FORMAT_XBGR8888:
899 return MESA_FORMAT_R8G8B8X8_UNORM;
900 case __DRI_IMAGE_FORMAT_R8:
901 return MESA_FORMAT_R_UNORM8;
902 case __DRI_IMAGE_FORMAT_R16:
903 return MESA_FORMAT_R_UNORM16;
904 case __DRI_IMAGE_FORMAT_GR88:
905 return MESA_FORMAT_R8G8_UNORM;
906 case __DRI_IMAGE_FORMAT_GR1616:
907 return MESA_FORMAT_R16G16_UNORM;
908 case __DRI_IMAGE_FORMAT_SARGB8:
909 return MESA_FORMAT_B8G8R8A8_SRGB;
910 case __DRI_IMAGE_FORMAT_NONE:
911 return MESA_FORMAT_NONE;
912 default:
913 return MESA_FORMAT_NONE;
914 }
915 }
916
917 /** Image driver interface */
918 const __DRIimageDriverExtension driImageDriverExtension = {
919 .base = { __DRI_IMAGE_DRIVER, 1 },
920
921 .createNewScreen2 = driCreateNewScreen2,
922 .createNewDrawable = driCreateNewDrawable,
923 .getAPIMask = driGetAPIMask,
924 .createContextAttribs = driCreateContextAttribs,
925 };
926
927 /* swrast copy sub buffer entrypoint. */
928 static void driCopySubBuffer(__DRIdrawable *pdp, int x, int y,
929 int w, int h)
930 {
931 assert(pdp->driScreenPriv->swrast_loader);
932
933 pdp->driScreenPriv->driver->CopySubBuffer(pdp, x, y, w, h);
934 }
935
936 /* for swrast only */
937 const __DRIcopySubBufferExtension driCopySubBufferExtension = {
938 .base = { __DRI_COPY_SUB_BUFFER, 1 },
939
940 .copySubBuffer = driCopySubBuffer,
941 };
942
943 const __DRInoErrorExtension dri2NoErrorExtension = {
944 .base = { __DRI2_NO_ERROR, 1 },
945 };