c58650491dfbb15bea9383562728bcea3463c64f
[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 /* The latest version of EGL_KHR_create_context spec says:
393 *
394 * "If the EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR flag bit is set in
395 * EGL_CONTEXT_FLAGS_KHR, then a <debug context> will be created.
396 * [...] This bit is supported for OpenGL and OpenGL ES contexts.
397 *
398 * No other EGL_CONTEXT_OPENGL_*_BIT is legal for an ES context.
399 *
400 * However, Mesa's EGL layer translates the context attribute
401 * EGL_CONTEXT_OPENGL_ROBUST_ACCESS into the context flag
402 * __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS. That attribute is legal for ES
403 * (with EGL 1.5 or EGL_EXT_create_context_robustness) and GL (only with
404 * EGL 1.5).
405 *
406 * From the EGL_EXT_create_context_robustness spec:
407 *
408 * This extension is written against the OpenGL ES 2.0 Specification
409 * but can apply to OpenGL ES 1.1 and up.
410 *
411 * From the EGL 1.5 (2014.08.27) spec, p55:
412 *
413 * If the EGL_CONTEXT_OPENGL_ROBUST_ACCESS attribute is set to
414 * EGL_TRUE, a context supporting robust buffer access will be created.
415 * OpenGL contexts must support the GL_ARB_robustness extension, or
416 * equivalent core API functional- ity. OpenGL ES contexts must support
417 * the GL_EXT_robustness extension, or equivalent core API
418 * functionality.
419 */
420 if (mesa_api != API_OPENGL_COMPAT
421 && mesa_api != API_OPENGL_CORE
422 && (ctx_config.flags & ~(__DRI_CTX_FLAG_DEBUG |
423 __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS |
424 __DRI_CTX_FLAG_NO_ERROR))) {
425 *error = __DRI_CTX_ERROR_BAD_FLAG;
426 return NULL;
427 }
428
429 /* There are no forward-compatible contexts before OpenGL 3.0. The
430 * GLX_ARB_create_context spec says:
431 *
432 * "Forward-compatible contexts are defined only for OpenGL versions
433 * 3.0 and later."
434 *
435 * Forward-looking contexts are supported by silently converting the
436 * requested API to API_OPENGL_CORE.
437 *
438 * In Mesa, a debug context is the same as a regular context.
439 */
440 if ((ctx_config.flags & __DRI_CTX_FLAG_FORWARD_COMPATIBLE) != 0) {
441 mesa_api = API_OPENGL_CORE;
442 }
443
444 const uint32_t allowed_flags = (__DRI_CTX_FLAG_DEBUG
445 | __DRI_CTX_FLAG_FORWARD_COMPATIBLE
446 | __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS
447 | __DRI_CTX_FLAG_NO_ERROR);
448 if (ctx_config.flags & ~allowed_flags) {
449 *error = __DRI_CTX_ERROR_UNKNOWN_FLAG;
450 return NULL;
451 }
452
453 if (!validate_context_version(screen, mesa_api,
454 ctx_config.major_version,
455 ctx_config.minor_version,
456 error))
457 return NULL;
458
459 context = calloc(1, sizeof *context);
460 if (!context) {
461 *error = __DRI_CTX_ERROR_NO_MEMORY;
462 return NULL;
463 }
464
465 context->loaderPrivate = data;
466
467 context->driScreenPriv = screen;
468 context->driDrawablePriv = NULL;
469 context->driReadablePriv = NULL;
470
471 if (!screen->driver->CreateContext(mesa_api, modes, context,
472 &ctx_config, error, shareCtx)) {
473 free(context);
474 return NULL;
475 }
476
477 *error = __DRI_CTX_ERROR_SUCCESS;
478 return context;
479 }
480
481 void
482 driContextSetFlags(struct gl_context *ctx, uint32_t flags)
483 {
484 if ((flags & __DRI_CTX_FLAG_FORWARD_COMPATIBLE) != 0)
485 ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT;
486 if ((flags & __DRI_CTX_FLAG_DEBUG) != 0) {
487 _mesa_set_debug_state_int(ctx, GL_DEBUG_OUTPUT, GL_TRUE);
488 ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_DEBUG_BIT;
489 }
490 if ((flags & __DRI_CTX_FLAG_NO_ERROR) != 0)
491 ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_NO_ERROR_BIT_KHR;
492 }
493
494 static __DRIcontext *
495 driCreateNewContextForAPI(__DRIscreen *screen, int api,
496 const __DRIconfig *config,
497 __DRIcontext *shared, void *data)
498 {
499 unsigned error;
500
501 return driCreateContextAttribs(screen, api, config, shared, 0, NULL,
502 &error, data);
503 }
504
505 static __DRIcontext *
506 driCreateNewContext(__DRIscreen *screen, const __DRIconfig *config,
507 __DRIcontext *shared, void *data)
508 {
509 return driCreateNewContextForAPI(screen, __DRI_API_OPENGL,
510 config, shared, data);
511 }
512
513 /**
514 * Destroy the per-context private information.
515 *
516 * \internal
517 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
518 * drmDestroyContext(), and finally frees \p contextPrivate.
519 */
520 static void
521 driDestroyContext(__DRIcontext *pcp)
522 {
523 if (pcp) {
524 pcp->driScreenPriv->driver->DestroyContext(pcp);
525 free(pcp);
526 }
527 }
528
529 static int
530 driCopyContext(__DRIcontext *dest, __DRIcontext *src, unsigned long mask)
531 {
532 (void) dest;
533 (void) src;
534 (void) mask;
535 return GL_FALSE;
536 }
537
538 /*@}*/
539
540
541 /*****************************************************************/
542 /** \name Context (un)binding functions */
543 /*****************************************************************/
544 /*@{*/
545
546 static void dri_get_drawable(__DRIdrawable *pdp);
547 static void dri_put_drawable(__DRIdrawable *pdp);
548
549 /**
550 * This function takes both a read buffer and a draw buffer. This is needed
551 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
552 * function.
553 */
554 static int driBindContext(__DRIcontext *pcp,
555 __DRIdrawable *pdp,
556 __DRIdrawable *prp)
557 {
558 /*
559 ** Assume error checking is done properly in glXMakeCurrent before
560 ** calling driUnbindContext.
561 */
562
563 if (!pcp)
564 return GL_FALSE;
565
566 /* Bind the drawable to the context */
567 pcp->driDrawablePriv = pdp;
568 pcp->driReadablePriv = prp;
569 if (pdp) {
570 pdp->driContextPriv = pcp;
571 dri_get_drawable(pdp);
572 }
573 if (prp && pdp != prp) {
574 dri_get_drawable(prp);
575 }
576
577 return pcp->driScreenPriv->driver->MakeCurrent(pcp, pdp, prp);
578 }
579
580 /**
581 * Unbind context.
582 *
583 * \param scrn the screen.
584 * \param gc context.
585 *
586 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
587 *
588 * \internal
589 * This function calls __DriverAPIRec::UnbindContext, and then decrements
590 * __DRIdrawableRec::refcount which must be non-zero for a successful
591 * return.
592 *
593 * While casting the opaque private pointers associated with the parameters
594 * into their respective real types it also assures they are not \c NULL.
595 */
596 static int driUnbindContext(__DRIcontext *pcp)
597 {
598 __DRIdrawable *pdp;
599 __DRIdrawable *prp;
600
601 /*
602 ** Assume error checking is done properly in glXMakeCurrent before
603 ** calling driUnbindContext.
604 */
605
606 if (pcp == NULL)
607 return GL_FALSE;
608
609 /*
610 ** Call driUnbindContext before checking for valid drawables
611 ** to handle surfaceless contexts properly.
612 */
613 pcp->driScreenPriv->driver->UnbindContext(pcp);
614
615 pdp = pcp->driDrawablePriv;
616 prp = pcp->driReadablePriv;
617
618 /* already unbound */
619 if (!pdp && !prp)
620 return GL_TRUE;
621
622 assert(pdp);
623 if (pdp->refcount == 0) {
624 /* ERROR!!! */
625 return GL_FALSE;
626 }
627
628 dri_put_drawable(pdp);
629
630 if (prp != pdp) {
631 if (prp->refcount == 0) {
632 /* ERROR!!! */
633 return GL_FALSE;
634 }
635
636 dri_put_drawable(prp);
637 }
638
639 pcp->driDrawablePriv = NULL;
640 pcp->driReadablePriv = NULL;
641
642 return GL_TRUE;
643 }
644
645 /*@}*/
646
647
648 static void dri_get_drawable(__DRIdrawable *pdp)
649 {
650 pdp->refcount++;
651 }
652
653 static void dri_put_drawable(__DRIdrawable *pdp)
654 {
655 if (pdp) {
656 pdp->refcount--;
657 if (pdp->refcount)
658 return;
659
660 pdp->driScreenPriv->driver->DestroyBuffer(pdp);
661 free(pdp);
662 }
663 }
664
665 static __DRIdrawable *
666 driCreateNewDrawable(__DRIscreen *screen,
667 const __DRIconfig *config,
668 void *data)
669 {
670 __DRIdrawable *pdraw;
671
672 assert(data != NULL);
673
674 pdraw = malloc(sizeof *pdraw);
675 if (!pdraw)
676 return NULL;
677
678 pdraw->loaderPrivate = data;
679
680 pdraw->driScreenPriv = screen;
681 pdraw->driContextPriv = NULL;
682 pdraw->refcount = 0;
683 pdraw->lastStamp = 0;
684 pdraw->w = 0;
685 pdraw->h = 0;
686
687 dri_get_drawable(pdraw);
688
689 if (!screen->driver->CreateBuffer(screen, pdraw, &config->modes,
690 GL_FALSE)) {
691 free(pdraw);
692 return NULL;
693 }
694
695 pdraw->dri2.stamp = pdraw->lastStamp + 1;
696
697 return pdraw;
698 }
699
700 static void
701 driDestroyDrawable(__DRIdrawable *pdp)
702 {
703 /*
704 * The loader's data structures are going away, even if pdp itself stays
705 * around for the time being because it is currently bound. This happens
706 * when a currently bound GLX pixmap is destroyed.
707 *
708 * Clear out the pointer back into the loader's data structures to avoid
709 * accessing an outdated pointer.
710 */
711 pdp->loaderPrivate = NULL;
712
713 dri_put_drawable(pdp);
714 }
715
716 static __DRIbuffer *
717 dri2AllocateBuffer(__DRIscreen *screen,
718 unsigned int attachment, unsigned int format,
719 int width, int height)
720 {
721 return screen->driver->AllocateBuffer(screen, attachment, format,
722 width, height);
723 }
724
725 static void
726 dri2ReleaseBuffer(__DRIscreen *screen, __DRIbuffer *buffer)
727 {
728 screen->driver->ReleaseBuffer(screen, buffer);
729 }
730
731
732 static int
733 dri2ConfigQueryb(__DRIscreen *screen, const char *var, unsigned char *val)
734 {
735 if (!driCheckOption(&screen->optionCache, var, DRI_BOOL))
736 return -1;
737
738 *val = driQueryOptionb(&screen->optionCache, var);
739
740 return 0;
741 }
742
743 static int
744 dri2ConfigQueryi(__DRIscreen *screen, const char *var, int *val)
745 {
746 if (!driCheckOption(&screen->optionCache, var, DRI_INT) &&
747 !driCheckOption(&screen->optionCache, var, DRI_ENUM))
748 return -1;
749
750 *val = driQueryOptioni(&screen->optionCache, var);
751
752 return 0;
753 }
754
755 static int
756 dri2ConfigQueryf(__DRIscreen *screen, const char *var, float *val)
757 {
758 if (!driCheckOption(&screen->optionCache, var, DRI_FLOAT))
759 return -1;
760
761 *val = driQueryOptionf(&screen->optionCache, var);
762
763 return 0;
764 }
765
766 static unsigned int
767 driGetAPIMask(__DRIscreen *screen)
768 {
769 return screen->api_mask;
770 }
771
772 /**
773 * swrast swapbuffers entrypoint.
774 *
775 * DRI2 implements this inside the loader with only flushes handled by the
776 * driver.
777 */
778 static void
779 driSwapBuffers(__DRIdrawable *pdp)
780 {
781 assert(pdp->driScreenPriv->swrast_loader);
782
783 pdp->driScreenPriv->driver->SwapBuffers(pdp);
784 }
785
786 /** Core interface */
787 const __DRIcoreExtension driCoreExtension = {
788 .base = { __DRI_CORE, 2 },
789
790 .createNewScreen = NULL,
791 .destroyScreen = driDestroyScreen,
792 .getExtensions = driGetExtensions,
793 .getConfigAttrib = driGetConfigAttrib,
794 .indexConfigAttrib = driIndexConfigAttrib,
795 .createNewDrawable = NULL,
796 .destroyDrawable = driDestroyDrawable,
797 .swapBuffers = driSwapBuffers, /* swrast */
798 .createNewContext = driCreateNewContext, /* swrast */
799 .copyContext = driCopyContext,
800 .destroyContext = driDestroyContext,
801 .bindContext = driBindContext,
802 .unbindContext = driUnbindContext
803 };
804
805 /** DRI2 interface */
806 const __DRIdri2Extension driDRI2Extension = {
807 .base = { __DRI_DRI2, 4 },
808
809 .createNewScreen = dri2CreateNewScreen,
810 .createNewDrawable = driCreateNewDrawable,
811 .createNewContext = driCreateNewContext,
812 .getAPIMask = driGetAPIMask,
813 .createNewContextForAPI = driCreateNewContextForAPI,
814 .allocateBuffer = dri2AllocateBuffer,
815 .releaseBuffer = dri2ReleaseBuffer,
816 .createContextAttribs = driCreateContextAttribs,
817 .createNewScreen2 = driCreateNewScreen2,
818 };
819
820 const __DRIswrastExtension driSWRastExtension = {
821 .base = { __DRI_SWRAST, 4 },
822
823 .createNewScreen = driSWRastCreateNewScreen,
824 .createNewDrawable = driCreateNewDrawable,
825 .createNewContextForAPI = driCreateNewContextForAPI,
826 .createContextAttribs = driCreateContextAttribs,
827 .createNewScreen2 = driSWRastCreateNewScreen2,
828 };
829
830 const __DRI2configQueryExtension dri2ConfigQueryExtension = {
831 .base = { __DRI2_CONFIG_QUERY, 1 },
832
833 .configQueryb = dri2ConfigQueryb,
834 .configQueryi = dri2ConfigQueryi,
835 .configQueryf = dri2ConfigQueryf,
836 };
837
838 const __DRI2flushControlExtension dri2FlushControlExtension = {
839 .base = { __DRI2_FLUSH_CONTROL, 1 }
840 };
841
842 void
843 dri2InvalidateDrawable(__DRIdrawable *drawable)
844 {
845 drawable->dri2.stamp++;
846 }
847
848 /**
849 * Check that the gl_framebuffer associated with dPriv is the right size.
850 * Resize the gl_framebuffer if needed.
851 * It's expected that the dPriv->driverPrivate member points to a
852 * gl_framebuffer object.
853 */
854 void
855 driUpdateFramebufferSize(struct gl_context *ctx, const __DRIdrawable *dPriv)
856 {
857 struct gl_framebuffer *fb = (struct gl_framebuffer *) dPriv->driverPrivate;
858 if (fb && (dPriv->w != fb->Width || dPriv->h != fb->Height)) {
859 _mesa_resize_framebuffer(ctx, fb, dPriv->w, dPriv->h);
860 /* if the driver needs the hw lock for ResizeBuffers, the drawable
861 might have changed again by now */
862 assert(fb->Width == dPriv->w);
863 assert(fb->Height == dPriv->h);
864 }
865 }
866
867 uint32_t
868 driGLFormatToImageFormat(mesa_format format)
869 {
870 switch (format) {
871 case MESA_FORMAT_B5G6R5_UNORM:
872 return __DRI_IMAGE_FORMAT_RGB565;
873 case MESA_FORMAT_B5G5R5A1_UNORM:
874 return __DRI_IMAGE_FORMAT_ARGB1555;
875 case MESA_FORMAT_B8G8R8X8_UNORM:
876 return __DRI_IMAGE_FORMAT_XRGB8888;
877 case MESA_FORMAT_B10G10R10A2_UNORM:
878 return __DRI_IMAGE_FORMAT_ARGB2101010;
879 case MESA_FORMAT_B10G10R10X2_UNORM:
880 return __DRI_IMAGE_FORMAT_XRGB2101010;
881 case MESA_FORMAT_R10G10B10A2_UNORM:
882 return __DRI_IMAGE_FORMAT_ABGR2101010;
883 case MESA_FORMAT_R10G10B10X2_UNORM:
884 return __DRI_IMAGE_FORMAT_XBGR2101010;
885 case MESA_FORMAT_B8G8R8A8_UNORM:
886 return __DRI_IMAGE_FORMAT_ARGB8888;
887 case MESA_FORMAT_R8G8B8A8_UNORM:
888 return __DRI_IMAGE_FORMAT_ABGR8888;
889 case MESA_FORMAT_R8G8B8X8_UNORM:
890 return __DRI_IMAGE_FORMAT_XBGR8888;
891 case MESA_FORMAT_L_UNORM8:
892 case MESA_FORMAT_R_UNORM8:
893 return __DRI_IMAGE_FORMAT_R8;
894 case MESA_FORMAT_L8A8_UNORM:
895 case MESA_FORMAT_R8G8_UNORM:
896 return __DRI_IMAGE_FORMAT_GR88;
897 case MESA_FORMAT_NONE:
898 return __DRI_IMAGE_FORMAT_NONE;
899 case MESA_FORMAT_R8G8B8A8_SRGB:
900 return __DRI_IMAGE_FORMAT_SABGR8;
901 case MESA_FORMAT_B8G8R8A8_SRGB:
902 return __DRI_IMAGE_FORMAT_SARGB8;
903 default:
904 return 0;
905 }
906 }
907
908 mesa_format
909 driImageFormatToGLFormat(uint32_t image_format)
910 {
911 switch (image_format) {
912 case __DRI_IMAGE_FORMAT_RGB565:
913 return MESA_FORMAT_B5G6R5_UNORM;
914 case __DRI_IMAGE_FORMAT_ARGB1555:
915 return MESA_FORMAT_B5G5R5A1_UNORM;
916 case __DRI_IMAGE_FORMAT_XRGB8888:
917 return MESA_FORMAT_B8G8R8X8_UNORM;
918 case __DRI_IMAGE_FORMAT_ARGB2101010:
919 return MESA_FORMAT_B10G10R10A2_UNORM;
920 case __DRI_IMAGE_FORMAT_XRGB2101010:
921 return MESA_FORMAT_B10G10R10X2_UNORM;
922 case __DRI_IMAGE_FORMAT_ABGR2101010:
923 return MESA_FORMAT_R10G10B10A2_UNORM;
924 case __DRI_IMAGE_FORMAT_XBGR2101010:
925 return MESA_FORMAT_R10G10B10X2_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 };