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