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