i965/gen6+: Add support for edge flags.
[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 <xf86drm.h>
43 #include "dri_util.h"
44 #include "utils.h"
45 #include "xmlpool.h"
46 #include "../glsl/glsl_parser_extras.h"
47
48 PUBLIC const char __dri2ConfigOptions[] =
49 DRI_CONF_BEGIN
50 DRI_CONF_SECTION_PERFORMANCE
51 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_1)
52 DRI_CONF_SECTION_END
53 DRI_CONF_END;
54
55 static const uint __dri2NConfigOptions = 1;
56
57 /*****************************************************************/
58 /** \name Screen handling functions */
59 /*****************************************************************/
60 /*@{*/
61
62 static void
63 setupLoaderExtensions(__DRIscreen *psp,
64 const __DRIextension **extensions)
65 {
66 int i;
67
68 for (i = 0; extensions[i]; i++) {
69 if (strcmp(extensions[i]->name, __DRI_DRI2_LOADER) == 0)
70 psp->dri2.loader = (__DRIdri2LoaderExtension *) extensions[i];
71 if (strcmp(extensions[i]->name, __DRI_IMAGE_LOOKUP) == 0)
72 psp->dri2.image = (__DRIimageLookupExtension *) extensions[i];
73 if (strcmp(extensions[i]->name, __DRI_USE_INVALIDATE) == 0)
74 psp->dri2.useInvalidate = (__DRIuseInvalidateExtension *) extensions[i];
75 }
76 }
77
78 static __DRIscreen *
79 dri2CreateNewScreen(int scrn, int fd,
80 const __DRIextension **extensions,
81 const __DRIconfig ***driver_configs, void *data)
82 {
83 static const __DRIextension *emptyExtensionList[] = { NULL };
84 __DRIscreen *psp;
85 drmVersionPtr version;
86
87 psp = calloc(1, sizeof(*psp));
88 if (!psp)
89 return NULL;
90
91 setupLoaderExtensions(psp, extensions);
92
93 version = drmGetVersion(fd);
94 if (version) {
95 psp->drm_version.major = version->version_major;
96 psp->drm_version.minor = version->version_minor;
97 psp->drm_version.patch = version->version_patchlevel;
98 drmFreeVersion(version);
99 }
100
101 psp->loaderPrivate = data;
102
103 psp->extensions = emptyExtensionList;
104 psp->fd = fd;
105 psp->myNum = scrn;
106
107 psp->api_mask = (1 << __DRI_API_OPENGL);
108
109 *driver_configs = driDriverAPI.InitScreen(psp);
110 if (*driver_configs == NULL) {
111 free(psp);
112 return NULL;
113 }
114
115 driParseOptionInfo(&psp->optionInfo, __dri2ConfigOptions, __dri2NConfigOptions);
116 driParseConfigFiles(&psp->optionCache, &psp->optionInfo, psp->myNum, "dri2");
117
118 return psp;
119 }
120
121 /**
122 * Destroy the per-screen private information.
123 *
124 * \internal
125 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
126 * drmClose(), and finally frees \p screenPrivate.
127 */
128 static void driDestroyScreen(__DRIscreen *psp)
129 {
130 if (psp) {
131 /* No interaction with the X-server is possible at this point. This
132 * routine is called after XCloseDisplay, so there is no protocol
133 * stream open to the X-server anymore.
134 */
135
136 _mesa_destroy_shader_compiler();
137
138 driDriverAPI.DestroyScreen(psp);
139
140 driDestroyOptionCache(&psp->optionCache);
141 driDestroyOptionInfo(&psp->optionInfo);
142
143 free(psp);
144 }
145 }
146
147 static const __DRIextension **driGetExtensions(__DRIscreen *psp)
148 {
149 return psp->extensions;
150 }
151
152 /*@}*/
153
154
155 /*****************************************************************/
156 /** \name Context handling functions */
157 /*****************************************************************/
158 /*@{*/
159
160 static __DRIcontext *
161 dri2CreateContextAttribs(__DRIscreen *screen, int api,
162 const __DRIconfig *config,
163 __DRIcontext *shared,
164 unsigned num_attribs,
165 const uint32_t *attribs,
166 unsigned *error,
167 void *data)
168 {
169 __DRIcontext *context;
170 const struct gl_config *modes = (config != NULL) ? &config->modes : NULL;
171 void *shareCtx = (shared != NULL) ? shared->driverPrivate : NULL;
172 gl_api mesa_api;
173 unsigned major_version = 1;
174 unsigned minor_version = 0;
175 uint32_t flags = 0;
176
177 assert((num_attribs == 0) || (attribs != NULL));
178
179 if (!(screen->api_mask & (1 << api))) {
180 *error = __DRI_CTX_ERROR_BAD_API;
181 return NULL;
182 }
183
184 switch (api) {
185 case __DRI_API_OPENGL:
186 mesa_api = API_OPENGL;
187 break;
188 case __DRI_API_GLES:
189 mesa_api = API_OPENGLES;
190 break;
191 case __DRI_API_GLES2:
192 mesa_api = API_OPENGLES2;
193 break;
194 case __DRI_API_OPENGL_CORE:
195 default:
196 *error = __DRI_CTX_ERROR_BAD_API;
197 return NULL;
198 }
199
200 for (unsigned i = 0; i < num_attribs; i++) {
201 switch (attribs[i * 2]) {
202 case __DRI_CTX_ATTRIB_MAJOR_VERSION:
203 major_version = attribs[i * 2 + 1];
204 break;
205 case __DRI_CTX_ATTRIB_MINOR_VERSION:
206 minor_version = attribs[i * 2 + 1];
207 break;
208 case __DRI_CTX_ATTRIB_FLAGS:
209 flags = attribs[i * 2 + 1];
210 break;
211 default:
212 /* We can't create a context that satisfies the requirements of an
213 * attribute that we don't understand. Return failure.
214 */
215 assert(!"Should not get here.");
216 *error = __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE;
217 return NULL;
218 }
219 }
220
221 /* The EGL_KHR_create_context spec says:
222 *
223 * "Flags are only defined for OpenGL context creation, and specifying
224 * a flags value other than zero for other types of contexts,
225 * including OpenGL ES contexts, will generate an error."
226 *
227 * The GLX_EXT_create_context_es2_profile specification doesn't say
228 * anything specific about this case. However, none of the known flags
229 * have any meaning in an ES context, so this seems safe.
230 */
231 if (mesa_api != __DRI_API_OPENGL
232 && mesa_api != __DRI_API_OPENGL_CORE
233 && flags != 0) {
234 *error = __DRI_CTX_ERROR_BAD_FLAG;
235 return NULL;
236 }
237
238 /* There are no forward-compatible contexts before OpenGL 3.0. The
239 * GLX_ARB_create_context spec says:
240 *
241 * "Forward-compatible contexts are defined only for OpenGL versions
242 * 3.0 and later."
243 *
244 * Moreover, Mesa can't fulfill the requirements of a forward-looking
245 * context. Return failure if a forward-looking context is requested.
246 *
247 * In Mesa, a debug context is the same as a regular context.
248 */
249 if ((flags & __DRI_CTX_FLAG_FORWARD_COMPATIBLE) != 0) {
250 *error = __DRI_CTX_ERROR_BAD_FLAG;
251 return NULL;
252 }
253
254 if ((flags & ~__DRI_CTX_FLAG_DEBUG) != 0) {
255 *error = __DRI_CTX_ERROR_UNKNOWN_FLAG;
256 return NULL;
257 }
258
259 context = malloc(sizeof *context);
260 if (!context) {
261 *error = __DRI_CTX_ERROR_NO_MEMORY;
262 return NULL;
263 }
264
265 context->loaderPrivate = data;
266
267 context->driScreenPriv = screen;
268 context->driDrawablePriv = NULL;
269 context->driReadablePriv = NULL;
270
271 if (!driDriverAPI.CreateContext(mesa_api, modes, context,
272 major_version, minor_version,
273 flags, error, shareCtx) ) {
274 free(context);
275 return NULL;
276 }
277
278 *error = __DRI_CTX_ERROR_SUCCESS;
279 return context;
280 }
281
282 static __DRIcontext *
283 dri2CreateNewContextForAPI(__DRIscreen *screen, int api,
284 const __DRIconfig *config,
285 __DRIcontext *shared, void *data)
286 {
287 unsigned error;
288
289 return dri2CreateContextAttribs(screen, api, config, shared, 0, NULL,
290 &error, data);
291 }
292
293 static __DRIcontext *
294 dri2CreateNewContext(__DRIscreen *screen, const __DRIconfig *config,
295 __DRIcontext *shared, void *data)
296 {
297 return dri2CreateNewContextForAPI(screen, __DRI_API_OPENGL,
298 config, shared, data);
299 }
300
301 /**
302 * Destroy the per-context private information.
303 *
304 * \internal
305 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
306 * drmDestroyContext(), and finally frees \p contextPrivate.
307 */
308 static void
309 driDestroyContext(__DRIcontext *pcp)
310 {
311 if (pcp) {
312 driDriverAPI.DestroyContext(pcp);
313 free(pcp);
314 }
315 }
316
317 static int
318 driCopyContext(__DRIcontext *dest, __DRIcontext *src, unsigned long mask)
319 {
320 (void) dest;
321 (void) src;
322 (void) mask;
323 return GL_FALSE;
324 }
325
326 /*@}*/
327
328
329 /*****************************************************************/
330 /** \name Context (un)binding functions */
331 /*****************************************************************/
332 /*@{*/
333
334 static void dri_get_drawable(__DRIdrawable *pdp);
335 static void dri_put_drawable(__DRIdrawable *pdp);
336
337 /**
338 * This function takes both a read buffer and a draw buffer. This is needed
339 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
340 * function.
341 */
342 static int driBindContext(__DRIcontext *pcp,
343 __DRIdrawable *pdp,
344 __DRIdrawable *prp)
345 {
346 /*
347 ** Assume error checking is done properly in glXMakeCurrent before
348 ** calling driUnbindContext.
349 */
350
351 if (!pcp)
352 return GL_FALSE;
353
354 /* Bind the drawable to the context */
355 pcp->driDrawablePriv = pdp;
356 pcp->driReadablePriv = prp;
357 if (pdp) {
358 pdp->driContextPriv = pcp;
359 dri_get_drawable(pdp);
360 }
361 if (prp && pdp != prp) {
362 dri_get_drawable(prp);
363 }
364
365 return driDriverAPI.MakeCurrent(pcp, pdp, prp);
366 }
367
368 /**
369 * Unbind context.
370 *
371 * \param scrn the screen.
372 * \param gc context.
373 *
374 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
375 *
376 * \internal
377 * This function calls __DriverAPIRec::UnbindContext, and then decrements
378 * __DRIdrawableRec::refcount which must be non-zero for a successful
379 * return.
380 *
381 * While casting the opaque private pointers associated with the parameters
382 * into their respective real types it also assures they are not \c NULL.
383 */
384 static int driUnbindContext(__DRIcontext *pcp)
385 {
386 __DRIdrawable *pdp;
387 __DRIdrawable *prp;
388
389 /*
390 ** Assume error checking is done properly in glXMakeCurrent before
391 ** calling driUnbindContext.
392 */
393
394 if (pcp == NULL)
395 return GL_FALSE;
396
397 pdp = pcp->driDrawablePriv;
398 prp = pcp->driReadablePriv;
399
400 /* already unbound */
401 if (!pdp && !prp)
402 return GL_TRUE;
403
404 driDriverAPI.UnbindContext(pcp);
405
406 assert(pdp);
407 if (pdp->refcount == 0) {
408 /* ERROR!!! */
409 return GL_FALSE;
410 }
411
412 dri_put_drawable(pdp);
413
414 if (prp != pdp) {
415 if (prp->refcount == 0) {
416 /* ERROR!!! */
417 return GL_FALSE;
418 }
419
420 dri_put_drawable(prp);
421 }
422
423 /* XXX this is disabled so that if we call SwapBuffers on an unbound
424 * window we can determine the last context bound to the window and
425 * use that context's lock. (BrianP, 2-Dec-2000)
426 */
427 pcp->driDrawablePriv = NULL;
428 pcp->driReadablePriv = NULL;
429
430 return GL_TRUE;
431 }
432
433 /*@}*/
434
435
436 static void dri_get_drawable(__DRIdrawable *pdp)
437 {
438 pdp->refcount++;
439 }
440
441 static void dri_put_drawable(__DRIdrawable *pdp)
442 {
443 if (pdp) {
444 pdp->refcount--;
445 if (pdp->refcount)
446 return;
447
448 driDriverAPI.DestroyBuffer(pdp);
449 free(pdp);
450 }
451 }
452
453 static __DRIdrawable *
454 dri2CreateNewDrawable(__DRIscreen *screen,
455 const __DRIconfig *config,
456 void *data)
457 {
458 __DRIdrawable *pdraw;
459
460 pdraw = malloc(sizeof *pdraw);
461 if (!pdraw)
462 return NULL;
463
464 pdraw->loaderPrivate = data;
465
466 pdraw->driScreenPriv = screen;
467 pdraw->driContextPriv = NULL;
468 pdraw->refcount = 0;
469 pdraw->lastStamp = 0;
470 pdraw->w = 0;
471 pdraw->h = 0;
472
473 dri_get_drawable(pdraw);
474
475 if (!driDriverAPI.CreateBuffer(screen, pdraw, &config->modes, GL_FALSE)) {
476 free(pdraw);
477 return NULL;
478 }
479
480 pdraw->dri2.stamp = pdraw->lastStamp + 1;
481
482 return pdraw;
483 }
484
485 static void
486 driDestroyDrawable(__DRIdrawable *pdp)
487 {
488 dri_put_drawable(pdp);
489 }
490
491 static __DRIbuffer *
492 dri2AllocateBuffer(__DRIscreen *screen,
493 unsigned int attachment, unsigned int format,
494 int width, int height)
495 {
496 return driDriverAPI.AllocateBuffer(screen, attachment, format,
497 width, height);
498 }
499
500 static void
501 dri2ReleaseBuffer(__DRIscreen *screen, __DRIbuffer *buffer)
502 {
503 driDriverAPI.ReleaseBuffer(screen, buffer);
504 }
505
506
507 static int
508 dri2ConfigQueryb(__DRIscreen *screen, const char *var, GLboolean *val)
509 {
510 if (!driCheckOption(&screen->optionCache, var, DRI_BOOL))
511 return -1;
512
513 *val = driQueryOptionb(&screen->optionCache, var);
514
515 return 0;
516 }
517
518 static int
519 dri2ConfigQueryi(__DRIscreen *screen, const char *var, GLint *val)
520 {
521 if (!driCheckOption(&screen->optionCache, var, DRI_INT) &&
522 !driCheckOption(&screen->optionCache, var, DRI_ENUM))
523 return -1;
524
525 *val = driQueryOptioni(&screen->optionCache, var);
526
527 return 0;
528 }
529
530 static int
531 dri2ConfigQueryf(__DRIscreen *screen, const char *var, GLfloat *val)
532 {
533 if (!driCheckOption(&screen->optionCache, var, DRI_FLOAT))
534 return -1;
535
536 *val = driQueryOptionf(&screen->optionCache, var);
537
538 return 0;
539 }
540
541 static unsigned int
542 dri2GetAPIMask(__DRIscreen *screen)
543 {
544 return screen->api_mask;
545 }
546
547
548 /** Core interface */
549 const __DRIcoreExtension driCoreExtension = {
550 { __DRI_CORE, __DRI_CORE_VERSION },
551 NULL,
552 driDestroyScreen,
553 driGetExtensions,
554 driGetConfigAttrib,
555 driIndexConfigAttrib,
556 NULL,
557 driDestroyDrawable,
558 NULL,
559 NULL,
560 driCopyContext,
561 driDestroyContext,
562 driBindContext,
563 driUnbindContext
564 };
565
566 /** DRI2 interface */
567 const __DRIdri2Extension driDRI2Extension = {
568 { __DRI_DRI2, 3 },
569 dri2CreateNewScreen,
570 dri2CreateNewDrawable,
571 dri2CreateNewContext,
572 dri2GetAPIMask,
573 dri2CreateNewContextForAPI,
574 dri2AllocateBuffer,
575 dri2ReleaseBuffer,
576 dri2CreateContextAttribs
577 };
578
579 const __DRI2configQueryExtension dri2ConfigQueryExtension = {
580 { __DRI2_CONFIG_QUERY, __DRI2_CONFIG_QUERY_VERSION },
581 dri2ConfigQueryb,
582 dri2ConfigQueryi,
583 dri2ConfigQueryf,
584 };
585
586 void
587 dri2InvalidateDrawable(__DRIdrawable *drawable)
588 {
589 drawable->dri2.stamp++;
590 }
591
592 /**
593 * Check that the gl_framebuffer associated with dPriv is the right size.
594 * Resize the gl_framebuffer if needed.
595 * It's expected that the dPriv->driverPrivate member points to a
596 * gl_framebuffer object.
597 */
598 void
599 driUpdateFramebufferSize(struct gl_context *ctx, const __DRIdrawable *dPriv)
600 {
601 struct gl_framebuffer *fb = (struct gl_framebuffer *) dPriv->driverPrivate;
602 if (fb && (dPriv->w != fb->Width || dPriv->h != fb->Height)) {
603 ctx->Driver.ResizeBuffers(ctx, fb, dPriv->w, dPriv->h);
604 /* if the driver needs the hw lock for ResizeBuffers, the drawable
605 might have changed again by now */
606 assert(fb->Width == dPriv->w);
607 assert(fb->Height == dPriv->h);
608 }
609 }