dri/common: include debug_output.h to silence warning
[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_SWRAST_LOADER) == 0)
79 psp->swrast_loader = (__DRIswrastLoaderExtension *) extensions[i];
80 if (strcmp(extensions[i]->name, __DRI_IMAGE_LOADER) == 0)
81 psp->image.loader = (__DRIimageLoaderExtension *) extensions[i];
82 }
83 }
84
85 /**
86 * This pointer determines which driver API we'll use in the case of the
87 * loader not passing us an explicit driver extensions list (that would,
88 * itself, contain a pointer to a driver API.)
89 *
90 * A driver's driDriverGetExtensions_drivername() can update this pointer to
91 * what it's returning, and a loader that is ignorant of createNewScreen2()
92 * will get the correct driver screen created, as long as no other
93 * driDriverGetExtensions() happened in between the first one and the
94 * createNewScreen().
95 *
96 * This allows the X Server to not require the significant dri_interface.h
97 * updates for doing createNewScreen2(), which would discourage backporting of
98 * the X Server patches to support the new loader interface.
99 */
100 const struct __DriverAPIRec *globalDriverAPI = &driDriverAPI;
101
102 /**
103 * This is the first entrypoint in the driver called by the DRI driver loader
104 * after dlopen()ing it.
105 *
106 * It's used to create global state for the driver across contexts on the same
107 * Display.
108 */
109 static __DRIscreen *
110 driCreateNewScreen2(int scrn, int fd,
111 const __DRIextension **extensions,
112 const __DRIextension **driver_extensions,
113 const __DRIconfig ***driver_configs, void *data)
114 {
115 static const __DRIextension *emptyExtensionList[] = { NULL };
116 __DRIscreen *psp;
117
118 psp = calloc(1, sizeof(*psp));
119 if (!psp)
120 return NULL;
121
122 /* By default, use the global driDriverAPI symbol (non-megadrivers). */
123 psp->driver = globalDriverAPI;
124
125 /* If the driver exposes its vtable through its extensions list
126 * (megadrivers), use that instead.
127 */
128 if (driver_extensions) {
129 for (int i = 0; driver_extensions[i]; i++) {
130 if (strcmp(driver_extensions[i]->name, __DRI_DRIVER_VTABLE) == 0) {
131 psp->driver =
132 ((__DRIDriverVtableExtension *)driver_extensions[i])->vtable;
133 }
134 }
135 }
136
137 setupLoaderExtensions(psp, extensions);
138
139 psp->loaderPrivate = data;
140
141 psp->extensions = emptyExtensionList;
142 psp->fd = fd;
143 psp->myNum = scrn;
144
145 *driver_configs = psp->driver->InitScreen(psp);
146 if (*driver_configs == NULL) {
147 free(psp);
148 return NULL;
149 }
150
151 struct gl_constants consts = { 0 };
152 gl_api api;
153 unsigned version;
154
155 api = API_OPENGLES2;
156 if (_mesa_override_gl_version_contextless(&consts, &api, &version))
157 psp->max_gl_es2_version = version;
158
159 api = API_OPENGL_COMPAT;
160 if (_mesa_override_gl_version_contextless(&consts, &api, &version)) {
161 if (api == API_OPENGL_CORE) {
162 psp->max_gl_core_version = version;
163 } else {
164 psp->max_gl_compat_version = version;
165 }
166 }
167
168 psp->api_mask = 0;
169 if (psp->max_gl_compat_version > 0)
170 psp->api_mask |= (1 << __DRI_API_OPENGL);
171 if (psp->max_gl_core_version > 0)
172 psp->api_mask |= (1 << __DRI_API_OPENGL_CORE);
173 if (psp->max_gl_es1_version > 0)
174 psp->api_mask |= (1 << __DRI_API_GLES);
175 if (psp->max_gl_es2_version > 0)
176 psp->api_mask |= (1 << __DRI_API_GLES2);
177 if (psp->max_gl_es2_version >= 30)
178 psp->api_mask |= (1 << __DRI_API_GLES3);
179
180 driParseOptionInfo(&psp->optionInfo, __dri2ConfigOptions);
181 driParseConfigFiles(&psp->optionCache, &psp->optionInfo, psp->myNum, "dri2");
182
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 unsigned major_version = 1;
304 unsigned minor_version = 0;
305 uint32_t flags = 0;
306 bool notify_reset = false;
307
308 assert((num_attribs == 0) || (attribs != NULL));
309
310 if (!(screen->api_mask & (1 << api))) {
311 *error = __DRI_CTX_ERROR_BAD_API;
312 return NULL;
313 }
314
315 switch (api) {
316 case __DRI_API_OPENGL:
317 mesa_api = API_OPENGL_COMPAT;
318 break;
319 case __DRI_API_GLES:
320 mesa_api = API_OPENGLES;
321 break;
322 case __DRI_API_GLES2:
323 case __DRI_API_GLES3:
324 mesa_api = API_OPENGLES2;
325 break;
326 case __DRI_API_OPENGL_CORE:
327 mesa_api = API_OPENGL_CORE;
328 break;
329 default:
330 *error = __DRI_CTX_ERROR_BAD_API;
331 return NULL;
332 }
333
334 for (unsigned i = 0; i < num_attribs; i++) {
335 switch (attribs[i * 2]) {
336 case __DRI_CTX_ATTRIB_MAJOR_VERSION:
337 major_version = attribs[i * 2 + 1];
338 break;
339 case __DRI_CTX_ATTRIB_MINOR_VERSION:
340 minor_version = attribs[i * 2 + 1];
341 break;
342 case __DRI_CTX_ATTRIB_FLAGS:
343 flags = attribs[i * 2 + 1];
344 break;
345 case __DRI_CTX_ATTRIB_RESET_STRATEGY:
346 notify_reset = (attribs[i * 2 + 1]
347 != __DRI_CTX_RESET_NO_NOTIFICATION);
348 break;
349 default:
350 /* We can't create a context that satisfies the requirements of an
351 * attribute that we don't understand. Return failure.
352 */
353 assert(!"Should not get here.");
354 *error = __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE;
355 return NULL;
356 }
357 }
358
359 /* Mesa does not support the GL_ARB_compatibilty extension or the
360 * compatibility profile. This means that we treat a API_OPENGL_COMPAT 3.1 as
361 * API_OPENGL_CORE and reject API_OPENGL_COMPAT 3.2+.
362 */
363 if (mesa_api == API_OPENGL_COMPAT && major_version == 3 && minor_version == 1)
364 mesa_api = API_OPENGL_CORE;
365
366 if (mesa_api == API_OPENGL_COMPAT
367 && ((major_version > 3)
368 || (major_version == 3 && minor_version >= 2))) {
369 *error = __DRI_CTX_ERROR_BAD_API;
370 return NULL;
371 }
372
373 /* The latest version of EGL_KHR_create_context spec says:
374 *
375 * "If the EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR flag bit is set in
376 * EGL_CONTEXT_FLAGS_KHR, then a <debug context> will be created.
377 * [...] This bit is supported for OpenGL and OpenGL ES contexts.
378 *
379 * None of the other flags have any meaning in an ES context, so this seems safe.
380 */
381 if (mesa_api != API_OPENGL_COMPAT
382 && mesa_api != API_OPENGL_CORE
383 && (flags & ~__DRI_CTX_FLAG_DEBUG)) {
384 *error = __DRI_CTX_ERROR_BAD_FLAG;
385 return NULL;
386 }
387
388 /* There are no forward-compatible contexts before OpenGL 3.0. The
389 * GLX_ARB_create_context spec says:
390 *
391 * "Forward-compatible contexts are defined only for OpenGL versions
392 * 3.0 and later."
393 *
394 * Forward-looking contexts are supported by silently converting the
395 * requested API to API_OPENGL_CORE.
396 *
397 * In Mesa, a debug context is the same as a regular context.
398 */
399 if ((flags & __DRI_CTX_FLAG_FORWARD_COMPATIBLE) != 0) {
400 mesa_api = API_OPENGL_CORE;
401 }
402
403 const uint32_t allowed_flags = (__DRI_CTX_FLAG_DEBUG
404 | __DRI_CTX_FLAG_FORWARD_COMPATIBLE
405 | __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS);
406 if (flags & ~allowed_flags) {
407 *error = __DRI_CTX_ERROR_UNKNOWN_FLAG;
408 return NULL;
409 }
410
411 if (!validate_context_version(screen, mesa_api,
412 major_version, minor_version, error))
413 return NULL;
414
415 context = calloc(1, sizeof *context);
416 if (!context) {
417 *error = __DRI_CTX_ERROR_NO_MEMORY;
418 return NULL;
419 }
420
421 context->loaderPrivate = data;
422
423 context->driScreenPriv = screen;
424 context->driDrawablePriv = NULL;
425 context->driReadablePriv = NULL;
426
427 if (!screen->driver->CreateContext(mesa_api, modes, context,
428 major_version, minor_version,
429 flags, notify_reset, error, shareCtx)) {
430 free(context);
431 return NULL;
432 }
433
434 *error = __DRI_CTX_ERROR_SUCCESS;
435 return context;
436 }
437
438 void
439 driContextSetFlags(struct gl_context *ctx, uint32_t flags)
440 {
441 if ((flags & __DRI_CTX_FLAG_FORWARD_COMPATIBLE) != 0)
442 ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT;
443 if ((flags & __DRI_CTX_FLAG_DEBUG) != 0) {
444 _mesa_set_debug_state_int(ctx, GL_DEBUG_OUTPUT, GL_TRUE);
445 ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_DEBUG_BIT;
446 }
447 }
448
449 static __DRIcontext *
450 driCreateNewContextForAPI(__DRIscreen *screen, int api,
451 const __DRIconfig *config,
452 __DRIcontext *shared, void *data)
453 {
454 unsigned error;
455
456 return driCreateContextAttribs(screen, api, config, shared, 0, NULL,
457 &error, data);
458 }
459
460 static __DRIcontext *
461 driCreateNewContext(__DRIscreen *screen, const __DRIconfig *config,
462 __DRIcontext *shared, void *data)
463 {
464 return driCreateNewContextForAPI(screen, __DRI_API_OPENGL,
465 config, shared, data);
466 }
467
468 /**
469 * Destroy the per-context private information.
470 *
471 * \internal
472 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
473 * drmDestroyContext(), and finally frees \p contextPrivate.
474 */
475 static void
476 driDestroyContext(__DRIcontext *pcp)
477 {
478 if (pcp) {
479 pcp->driScreenPriv->driver->DestroyContext(pcp);
480 free(pcp);
481 }
482 }
483
484 static int
485 driCopyContext(__DRIcontext *dest, __DRIcontext *src, unsigned long mask)
486 {
487 (void) dest;
488 (void) src;
489 (void) mask;
490 return GL_FALSE;
491 }
492
493 /*@}*/
494
495
496 /*****************************************************************/
497 /** \name Context (un)binding functions */
498 /*****************************************************************/
499 /*@{*/
500
501 static void dri_get_drawable(__DRIdrawable *pdp);
502 static void dri_put_drawable(__DRIdrawable *pdp);
503
504 /**
505 * This function takes both a read buffer and a draw buffer. This is needed
506 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
507 * function.
508 */
509 static int driBindContext(__DRIcontext *pcp,
510 __DRIdrawable *pdp,
511 __DRIdrawable *prp)
512 {
513 /*
514 ** Assume error checking is done properly in glXMakeCurrent before
515 ** calling driUnbindContext.
516 */
517
518 if (!pcp)
519 return GL_FALSE;
520
521 /* Bind the drawable to the context */
522 pcp->driDrawablePriv = pdp;
523 pcp->driReadablePriv = prp;
524 if (pdp) {
525 pdp->driContextPriv = pcp;
526 dri_get_drawable(pdp);
527 }
528 if (prp && pdp != prp) {
529 dri_get_drawable(prp);
530 }
531
532 return pcp->driScreenPriv->driver->MakeCurrent(pcp, pdp, prp);
533 }
534
535 /**
536 * Unbind context.
537 *
538 * \param scrn the screen.
539 * \param gc context.
540 *
541 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
542 *
543 * \internal
544 * This function calls __DriverAPIRec::UnbindContext, and then decrements
545 * __DRIdrawableRec::refcount which must be non-zero for a successful
546 * return.
547 *
548 * While casting the opaque private pointers associated with the parameters
549 * into their respective real types it also assures they are not \c NULL.
550 */
551 static int driUnbindContext(__DRIcontext *pcp)
552 {
553 __DRIdrawable *pdp;
554 __DRIdrawable *prp;
555
556 /*
557 ** Assume error checking is done properly in glXMakeCurrent before
558 ** calling driUnbindContext.
559 */
560
561 if (pcp == NULL)
562 return GL_FALSE;
563
564 /*
565 ** Call driUnbindContext before checking for valid drawables
566 ** to handle surfaceless contexts properly.
567 */
568 pcp->driScreenPriv->driver->UnbindContext(pcp);
569
570 pdp = pcp->driDrawablePriv;
571 prp = pcp->driReadablePriv;
572
573 /* already unbound */
574 if (!pdp && !prp)
575 return GL_TRUE;
576
577 assert(pdp);
578 if (pdp->refcount == 0) {
579 /* ERROR!!! */
580 return GL_FALSE;
581 }
582
583 dri_put_drawable(pdp);
584
585 if (prp != pdp) {
586 if (prp->refcount == 0) {
587 /* ERROR!!! */
588 return GL_FALSE;
589 }
590
591 dri_put_drawable(prp);
592 }
593
594 pcp->driDrawablePriv = NULL;
595 pcp->driReadablePriv = NULL;
596
597 return GL_TRUE;
598 }
599
600 /*@}*/
601
602
603 static void dri_get_drawable(__DRIdrawable *pdp)
604 {
605 pdp->refcount++;
606 }
607
608 static void dri_put_drawable(__DRIdrawable *pdp)
609 {
610 if (pdp) {
611 pdp->refcount--;
612 if (pdp->refcount)
613 return;
614
615 pdp->driScreenPriv->driver->DestroyBuffer(pdp);
616 free(pdp);
617 }
618 }
619
620 static __DRIdrawable *
621 driCreateNewDrawable(__DRIscreen *screen,
622 const __DRIconfig *config,
623 void *data)
624 {
625 __DRIdrawable *pdraw;
626
627 pdraw = malloc(sizeof *pdraw);
628 if (!pdraw)
629 return NULL;
630
631 pdraw->loaderPrivate = data;
632
633 pdraw->driScreenPriv = screen;
634 pdraw->driContextPriv = NULL;
635 pdraw->refcount = 0;
636 pdraw->lastStamp = 0;
637 pdraw->w = 0;
638 pdraw->h = 0;
639
640 dri_get_drawable(pdraw);
641
642 if (!screen->driver->CreateBuffer(screen, pdraw, &config->modes,
643 GL_FALSE)) {
644 free(pdraw);
645 return NULL;
646 }
647
648 pdraw->dri2.stamp = pdraw->lastStamp + 1;
649
650 return pdraw;
651 }
652
653 static void
654 driDestroyDrawable(__DRIdrawable *pdp)
655 {
656 dri_put_drawable(pdp);
657 }
658
659 static __DRIbuffer *
660 dri2AllocateBuffer(__DRIscreen *screen,
661 unsigned int attachment, unsigned int format,
662 int width, int height)
663 {
664 return screen->driver->AllocateBuffer(screen, attachment, format,
665 width, height);
666 }
667
668 static void
669 dri2ReleaseBuffer(__DRIscreen *screen, __DRIbuffer *buffer)
670 {
671 screen->driver->ReleaseBuffer(screen, buffer);
672 }
673
674
675 static int
676 dri2ConfigQueryb(__DRIscreen *screen, const char *var, unsigned char *val)
677 {
678 if (!driCheckOption(&screen->optionCache, var, DRI_BOOL))
679 return -1;
680
681 *val = driQueryOptionb(&screen->optionCache, var);
682
683 return 0;
684 }
685
686 static int
687 dri2ConfigQueryi(__DRIscreen *screen, const char *var, int *val)
688 {
689 if (!driCheckOption(&screen->optionCache, var, DRI_INT) &&
690 !driCheckOption(&screen->optionCache, var, DRI_ENUM))
691 return -1;
692
693 *val = driQueryOptioni(&screen->optionCache, var);
694
695 return 0;
696 }
697
698 static int
699 dri2ConfigQueryf(__DRIscreen *screen, const char *var, float *val)
700 {
701 if (!driCheckOption(&screen->optionCache, var, DRI_FLOAT))
702 return -1;
703
704 *val = driQueryOptionf(&screen->optionCache, var);
705
706 return 0;
707 }
708
709 static unsigned int
710 driGetAPIMask(__DRIscreen *screen)
711 {
712 return screen->api_mask;
713 }
714
715 /**
716 * swrast swapbuffers entrypoint.
717 *
718 * DRI2 implements this inside the loader with only flushes handled by the
719 * driver.
720 */
721 static void
722 driSwapBuffers(__DRIdrawable *pdp)
723 {
724 assert(pdp->driScreenPriv->swrast_loader);
725
726 pdp->driScreenPriv->driver->SwapBuffers(pdp);
727 }
728
729 /** Core interface */
730 const __DRIcoreExtension driCoreExtension = {
731 .base = { __DRI_CORE, 1 },
732
733 .createNewScreen = NULL,
734 .destroyScreen = driDestroyScreen,
735 .getExtensions = driGetExtensions,
736 .getConfigAttrib = driGetConfigAttrib,
737 .indexConfigAttrib = driIndexConfigAttrib,
738 .createNewDrawable = NULL,
739 .destroyDrawable = driDestroyDrawable,
740 .swapBuffers = driSwapBuffers, /* swrast */
741 .createNewContext = driCreateNewContext, /* swrast */
742 .copyContext = driCopyContext,
743 .destroyContext = driDestroyContext,
744 .bindContext = driBindContext,
745 .unbindContext = driUnbindContext
746 };
747
748 /** DRI2 interface */
749 const __DRIdri2Extension driDRI2Extension = {
750 .base = { __DRI_DRI2, 4 },
751
752 .createNewScreen = dri2CreateNewScreen,
753 .createNewDrawable = driCreateNewDrawable,
754 .createNewContext = driCreateNewContext,
755 .getAPIMask = driGetAPIMask,
756 .createNewContextForAPI = driCreateNewContextForAPI,
757 .allocateBuffer = dri2AllocateBuffer,
758 .releaseBuffer = dri2ReleaseBuffer,
759 .createContextAttribs = driCreateContextAttribs,
760 .createNewScreen2 = driCreateNewScreen2,
761 };
762
763 const __DRIswrastExtension driSWRastExtension = {
764 .base = { __DRI_SWRAST, 4 },
765
766 .createNewScreen = driSWRastCreateNewScreen,
767 .createNewDrawable = driCreateNewDrawable,
768 .createNewContextForAPI = driCreateNewContextForAPI,
769 .createContextAttribs = driCreateContextAttribs,
770 .createNewScreen2 = driSWRastCreateNewScreen2,
771 };
772
773 const __DRI2configQueryExtension dri2ConfigQueryExtension = {
774 .base = { __DRI2_CONFIG_QUERY, 1 },
775
776 .configQueryb = dri2ConfigQueryb,
777 .configQueryi = dri2ConfigQueryi,
778 .configQueryf = dri2ConfigQueryf,
779 };
780
781 void
782 dri2InvalidateDrawable(__DRIdrawable *drawable)
783 {
784 drawable->dri2.stamp++;
785 }
786
787 /**
788 * Check that the gl_framebuffer associated with dPriv is the right size.
789 * Resize the gl_framebuffer if needed.
790 * It's expected that the dPriv->driverPrivate member points to a
791 * gl_framebuffer object.
792 */
793 void
794 driUpdateFramebufferSize(struct gl_context *ctx, const __DRIdrawable *dPriv)
795 {
796 struct gl_framebuffer *fb = (struct gl_framebuffer *) dPriv->driverPrivate;
797 if (fb && (dPriv->w != fb->Width || dPriv->h != fb->Height)) {
798 _mesa_resize_framebuffer(ctx, fb, dPriv->w, dPriv->h);
799 /* if the driver needs the hw lock for ResizeBuffers, the drawable
800 might have changed again by now */
801 assert(fb->Width == dPriv->w);
802 assert(fb->Height == dPriv->h);
803 }
804 }
805
806 uint32_t
807 driGLFormatToImageFormat(mesa_format format)
808 {
809 switch (format) {
810 case MESA_FORMAT_B5G6R5_UNORM:
811 return __DRI_IMAGE_FORMAT_RGB565;
812 case MESA_FORMAT_B8G8R8X8_UNORM:
813 return __DRI_IMAGE_FORMAT_XRGB8888;
814 case MESA_FORMAT_B10G10R10A2_UNORM:
815 return __DRI_IMAGE_FORMAT_ARGB2101010;
816 case MESA_FORMAT_B10G10R10X2_UNORM:
817 return __DRI_IMAGE_FORMAT_XRGB2101010;
818 case MESA_FORMAT_B8G8R8A8_UNORM:
819 return __DRI_IMAGE_FORMAT_ARGB8888;
820 case MESA_FORMAT_R8G8B8A8_UNORM:
821 return __DRI_IMAGE_FORMAT_ABGR8888;
822 case MESA_FORMAT_R8G8B8X8_UNORM:
823 return __DRI_IMAGE_FORMAT_XBGR8888;
824 case MESA_FORMAT_R_UNORM8:
825 return __DRI_IMAGE_FORMAT_R8;
826 case MESA_FORMAT_R8G8_UNORM:
827 return __DRI_IMAGE_FORMAT_GR88;
828 case MESA_FORMAT_NONE:
829 return __DRI_IMAGE_FORMAT_NONE;
830 case MESA_FORMAT_B8G8R8A8_SRGB:
831 return __DRI_IMAGE_FORMAT_SARGB8;
832 default:
833 return 0;
834 }
835 }
836
837 mesa_format
838 driImageFormatToGLFormat(uint32_t image_format)
839 {
840 switch (image_format) {
841 case __DRI_IMAGE_FORMAT_RGB565:
842 return MESA_FORMAT_B5G6R5_UNORM;
843 case __DRI_IMAGE_FORMAT_XRGB8888:
844 return MESA_FORMAT_B8G8R8X8_UNORM;
845 case __DRI_IMAGE_FORMAT_ARGB2101010:
846 return MESA_FORMAT_B10G10R10A2_UNORM;
847 case __DRI_IMAGE_FORMAT_XRGB2101010:
848 return MESA_FORMAT_B10G10R10X2_UNORM;
849 case __DRI_IMAGE_FORMAT_ARGB8888:
850 return MESA_FORMAT_B8G8R8A8_UNORM;
851 case __DRI_IMAGE_FORMAT_ABGR8888:
852 return MESA_FORMAT_R8G8B8A8_UNORM;
853 case __DRI_IMAGE_FORMAT_XBGR8888:
854 return MESA_FORMAT_R8G8B8X8_UNORM;
855 case __DRI_IMAGE_FORMAT_R8:
856 return MESA_FORMAT_R_UNORM8;
857 case __DRI_IMAGE_FORMAT_GR88:
858 return MESA_FORMAT_R8G8_UNORM;
859 case __DRI_IMAGE_FORMAT_SARGB8:
860 return MESA_FORMAT_B8G8R8A8_SRGB;
861 case __DRI_IMAGE_FORMAT_NONE:
862 return MESA_FORMAT_NONE;
863 default:
864 return MESA_FORMAT_NONE;
865 }
866 }
867
868 /** Image driver interface */
869 const __DRIimageDriverExtension driImageDriverExtension = {
870 .base = { __DRI_IMAGE_DRIVER, 1 },
871
872 .createNewScreen2 = driCreateNewScreen2,
873 .createNewDrawable = driCreateNewDrawable,
874 .getAPIMask = driGetAPIMask,
875 .createContextAttribs = driCreateContextAttribs,
876 };
877
878 /* swrast copy sub buffer entrypoint. */
879 static void driCopySubBuffer(__DRIdrawable *pdp, int x, int y,
880 int w, int h)
881 {
882 assert(pdp->driScreenPriv->swrast_loader);
883
884 pdp->driScreenPriv->driver->CopySubBuffer(pdp, x, y, w, h);
885 }
886
887 /* for swrast only */
888 const __DRIcopySubBufferExtension driCopySubBufferExtension = {
889 .base = { __DRI_COPY_SUB_BUFFER, 1 },
890
891 .copySubBuffer = driCopySubBuffer,
892 };