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