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