Merge branch '7.8'
[mesa.git] / src / gallium / state_trackers / egl / common / egl_g3d.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.8
4 *
5 * Copyright (C) 2009-2010 Chia-I Wu <olv@0xlab.org>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include <assert.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include "pipe/p_screen.h"
29 #include "util/u_memory.h"
30 #include "util/u_rect.h"
31 #include "util/u_inlines.h"
32 #include "egldriver.h"
33 #include "eglcurrent.h"
34 #include "eglconfigutil.h"
35 #include "egllog.h"
36
37 #include "native.h"
38 #include "egl_g3d.h"
39 #include "egl_g3d_st.h"
40 #include "egl_g3d_image.h"
41
42 /**
43 * Return the state tracker for the given context.
44 */
45 static struct st_api *
46 egl_g3d_choose_st(_EGLDriver *drv, _EGLContext *ctx)
47 {
48 struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
49 struct st_api *stapi;
50 EGLint idx = -1;
51
52 switch (ctx->ClientAPI) {
53 case EGL_OPENGL_ES_API:
54 switch (ctx->ClientVersion) {
55 case 1:
56 idx = ST_API_OPENGL_ES1;
57 break;
58 case 2:
59 idx = ST_API_OPENGL_ES2;
60 break;
61 default:
62 _eglLog(_EGL_WARNING, "unknown client version %d",
63 ctx->ClientVersion);
64 break;
65 }
66 break;
67 case EGL_OPENVG_API:
68 idx = ST_API_OPENVG;
69 break;
70 case EGL_OPENGL_API:
71 idx = ST_API_OPENGL;
72 break;
73 default:
74 _eglLog(_EGL_WARNING, "unknown client API 0x%04x", ctx->ClientAPI);
75 break;
76 }
77
78 stapi = (idx >= 0) ? gdrv->stapis[idx] : NULL;
79 return stapi;
80 }
81
82 /**
83 * Initialize the state trackers.
84 */
85 static void
86 egl_g3d_init_st(_EGLDriver *drv)
87 {
88 struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
89 EGLint i;
90
91 /* already initialized */
92 if (gdrv->api_mask)
93 return;
94
95 for (i = 0; i < ST_API_COUNT; i++) {
96 gdrv->stapis[i] = egl_g3d_create_st_api(i);
97 if (gdrv->stapis[i])
98 gdrv->api_mask |= egl_g3d_st_api_bit(i);
99 }
100
101 if (gdrv->api_mask)
102 _eglLog(_EGL_DEBUG, "Driver API mask: 0x%x", gdrv->api_mask);
103 else
104 _eglLog(_EGL_WARNING, "No supported client API");
105 }
106
107 /**
108 * Get the probe object of the display.
109 *
110 * Note that this function may be called before the display is initialized.
111 */
112 static struct native_probe *
113 egl_g3d_get_probe(_EGLDriver *drv, _EGLDisplay *dpy)
114 {
115 struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
116 struct native_probe *nprobe;
117
118 nprobe = (struct native_probe *) _eglGetProbeCache(gdrv->probe_key);
119 if (!nprobe || nprobe->display != dpy->NativeDisplay) {
120 if (nprobe)
121 nprobe->destroy(nprobe);
122 nprobe = native_create_probe(dpy->NativeDisplay);
123 _eglSetProbeCache(gdrv->probe_key, (void *) nprobe);
124 }
125
126 return nprobe;
127 }
128
129 /**
130 * Destroy the probe object of the display. The display may be NULL.
131 *
132 * Note that this function may be called before the display is initialized.
133 */
134 static void
135 egl_g3d_destroy_probe(_EGLDriver *drv, _EGLDisplay *dpy)
136 {
137 struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
138 struct native_probe *nprobe;
139
140 nprobe = (struct native_probe *) _eglGetProbeCache(gdrv->probe_key);
141 if (nprobe && (!dpy || nprobe->display == dpy->NativeDisplay)) {
142 nprobe->destroy(nprobe);
143 _eglSetProbeCache(gdrv->probe_key, NULL);
144 }
145 }
146
147 #ifdef EGL_MESA_screen_surface
148
149 static void
150 egl_g3d_add_screens(_EGLDriver *drv, _EGLDisplay *dpy)
151 {
152 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
153 const struct native_connector **native_connectors;
154 EGLint num_connectors, i;
155
156 native_connectors =
157 gdpy->native->modeset->get_connectors(gdpy->native, &num_connectors, NULL);
158 if (!num_connectors) {
159 if (native_connectors)
160 free(native_connectors);
161 return;
162 }
163
164 for (i = 0; i < num_connectors; i++) {
165 const struct native_connector *nconn = native_connectors[i];
166 struct egl_g3d_screen *gscr;
167 const struct native_mode **native_modes;
168 EGLint num_modes, j;
169
170 /* TODO support for hotplug */
171 native_modes =
172 gdpy->native->modeset->get_modes(gdpy->native, nconn, &num_modes);
173 if (!num_modes) {
174 if (native_modes)
175 free(native_modes);
176 continue;
177 }
178
179 gscr = CALLOC_STRUCT(egl_g3d_screen);
180 if (!gscr) {
181 free(native_modes);
182 continue;
183 }
184
185 _eglInitScreen(&gscr->base);
186
187 for (j = 0; j < num_modes; j++) {
188 const struct native_mode *nmode = native_modes[j];
189 _EGLMode *mode;
190
191 mode = _eglAddNewMode(&gscr->base, nmode->width, nmode->height,
192 nmode->refresh_rate, nmode->desc);
193 if (!mode)
194 break;
195 /* gscr->native_modes and gscr->base.Modes should be consistent */
196 assert(mode == &gscr->base.Modes[j]);
197 }
198
199 gscr->native = nconn;
200 gscr->native_modes = native_modes;
201
202 _eglAddScreen(dpy, &gscr->base);
203 }
204
205 free(native_connectors);
206 }
207
208 #endif /* EGL_MESA_screen_surface */
209
210 /**
211 * Initialize an EGL config from the native config.
212 */
213 static EGLBoolean
214 egl_g3d_init_config(_EGLDriver *drv, _EGLDisplay *dpy,
215 _EGLConfig *conf, const struct native_config *nconf)
216 {
217 struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
218 struct egl_g3d_config *gconf = egl_g3d_config(conf);
219 const __GLcontextModes *mode = &nconf->mode;
220 EGLint buffer_mask, api_mask;
221 EGLBoolean valid;
222 EGLint i;
223
224 buffer_mask = ST_ATTACHMENT_FRONT_LEFT_MASK;
225 if (mode->doubleBufferMode)
226 buffer_mask |= ST_ATTACHMENT_BACK_LEFT_MASK;
227 if (mode->stereoMode) {
228 buffer_mask |= ST_ATTACHMENT_FRONT_RIGHT_MASK;
229 if (mode->doubleBufferMode)
230 buffer_mask |= ST_ATTACHMENT_BACK_RIGHT_MASK;
231 }
232
233 gconf->stvis.buffer_mask = buffer_mask;
234 gconf->stvis.color_format = nconf->color_format;
235 gconf->stvis.depth_stencil_format = nconf->depth_format;
236 gconf->stvis.accum_format = PIPE_FORMAT_NONE;
237 gconf->stvis.samples = 0;
238
239 gconf->stvis.render_buffer = (buffer_mask & ST_ATTACHMENT_BACK_LEFT) ?
240 ST_ATTACHMENT_BACK_LEFT : ST_ATTACHMENT_FRONT_LEFT;
241
242 api_mask = 0;
243 for (i = 0; i < ST_API_COUNT; i++) {
244 struct st_api *stapi = gdrv->stapis[i];
245 if (stapi) {
246 if (stapi->is_visual_supported(stapi, &gconf->stvis))
247 api_mask |= egl_g3d_st_api_bit(i);
248 }
249 }
250 /* this is required by EGL, not by OpenGL ES */
251 if ((mode->drawableType & GLX_WINDOW_BIT) && !mode->doubleBufferMode)
252 api_mask &= ~(EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT);
253
254 if (!api_mask) {
255 _eglLog(_EGL_DEBUG, "no state tracker supports config 0x%x",
256 mode->visualID);
257 }
258
259 valid = _eglConfigFromContextModesRec(&gconf->base,
260 mode, api_mask, api_mask);
261 if (valid) {
262 #ifdef EGL_MESA_screen_surface
263 /* check if scanout surface bit is set */
264 if (nconf->scanout_bit) {
265 EGLint val = GET_CONFIG_ATTRIB(&gconf->base, EGL_SURFACE_TYPE);
266 val |= EGL_SCREEN_BIT_MESA;
267 SET_CONFIG_ATTRIB(&gconf->base, EGL_SURFACE_TYPE, val);
268 }
269 #endif
270 valid = _eglValidateConfig(&gconf->base, EGL_FALSE);
271 }
272 if (!valid) {
273 _eglLog(_EGL_DEBUG, "skip invalid config 0x%x", mode->visualID);
274 return EGL_FALSE;
275 }
276
277 gconf->native = nconf;
278
279 return EGL_TRUE;
280 }
281
282 /**
283 * Add configs to display and return the next config ID.
284 */
285 static EGLint
286 egl_g3d_add_configs(_EGLDriver *drv, _EGLDisplay *dpy, EGLint id)
287 {
288 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
289 const struct native_config **native_configs;
290 int num_configs, i;
291
292 native_configs = gdpy->native->get_configs(gdpy->native, &num_configs);
293 if (!num_configs) {
294 if (native_configs)
295 free(native_configs);
296 return id;
297 }
298
299 for (i = 0; i < num_configs; i++) {
300 struct egl_g3d_config *gconf;
301
302 gconf = CALLOC_STRUCT(egl_g3d_config);
303 if (gconf) {
304 _eglInitConfig(&gconf->base, dpy, id);
305 if (!egl_g3d_init_config(drv, dpy, &gconf->base, native_configs[i])) {
306 free(gconf);
307 continue;
308 }
309
310 _eglAddConfig(dpy, &gconf->base);
311 id++;
312 }
313 }
314
315 free(native_configs);
316 return id;
317 }
318
319 static void
320 egl_g3d_invalid_surface(struct native_display *ndpy,
321 struct native_surface *nsurf,
322 unsigned int seq_num)
323 {
324 /* XXX not thread safe? */
325 struct egl_g3d_surface *gsurf = egl_g3d_surface(nsurf->user_data);
326 struct egl_g3d_context *gctx;
327
328 /*
329 * Some functions such as egl_g3d_copy_buffers create a temporary native
330 * surface. There is no gsurf associated with it.
331 */
332 gctx = (gsurf) ? egl_g3d_context(gsurf->base.CurrentContext) : NULL;
333 if (gctx)
334 gctx->stctxi->notify_invalid_framebuffer(gctx->stctxi, gsurf->stfbi);
335 }
336
337 static struct native_event_handler egl_g3d_native_event_handler = {
338 .invalid_surface = egl_g3d_invalid_surface
339 };
340
341 static EGLBoolean
342 egl_g3d_terminate(_EGLDriver *drv, _EGLDisplay *dpy)
343 {
344 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
345 EGLint i;
346
347 _eglReleaseDisplayResources(drv, dpy);
348 _eglCleanupDisplay(dpy);
349
350 if (gdpy->pipe)
351 gdpy->pipe->destroy(gdpy->pipe);
352
353 if (dpy->Screens) {
354 for (i = 0; i < dpy->NumScreens; i++) {
355 struct egl_g3d_screen *gscr = egl_g3d_screen(dpy->Screens[i]);
356 free(gscr->native_modes);
357 free(gscr);
358 }
359 free(dpy->Screens);
360 }
361
362 if (gdpy->smapi)
363 egl_g3d_destroy_st_manager(gdpy->smapi);
364
365 if (gdpy->native)
366 gdpy->native->destroy(gdpy->native);
367
368 free(gdpy);
369 dpy->DriverData = NULL;
370
371 return EGL_TRUE;
372 }
373
374 static EGLBoolean
375 egl_g3d_initialize(_EGLDriver *drv, _EGLDisplay *dpy,
376 EGLint *major, EGLint *minor)
377 {
378 struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
379 struct egl_g3d_display *gdpy;
380
381 /* the probe object is unlikely to be needed again */
382 egl_g3d_destroy_probe(drv, dpy);
383
384 gdpy = CALLOC_STRUCT(egl_g3d_display);
385 if (!gdpy) {
386 _eglError(EGL_BAD_ALLOC, "eglInitialize");
387 goto fail;
388 }
389 dpy->DriverData = gdpy;
390
391 gdpy->native = native_create_display(dpy->NativeDisplay,
392 &egl_g3d_native_event_handler);
393 if (!gdpy->native) {
394 _eglError(EGL_NOT_INITIALIZED, "eglInitialize(no usable display)");
395 goto fail;
396 }
397
398 gdpy->native->user_data = (void *) dpy;
399
400 egl_g3d_init_st(&gdrv->base);
401 dpy->ClientAPIsMask = gdrv->api_mask;
402
403 gdpy->smapi = egl_g3d_create_st_manager(dpy);
404 if (!gdpy->smapi) {
405 _eglError(EGL_NOT_INITIALIZED,
406 "eglInitialize(failed to create st manager)");
407 goto fail;
408 }
409
410 #ifdef EGL_MESA_screen_surface
411 /* enable MESA_screen_surface before adding (and validating) configs */
412 if (gdpy->native->modeset) {
413 dpy->Extensions.MESA_screen_surface = EGL_TRUE;
414 egl_g3d_add_screens(drv, dpy);
415 }
416 #endif
417
418 dpy->Extensions.KHR_image_base = EGL_TRUE;
419 if (gdpy->native->get_param(gdpy->native, NATIVE_PARAM_USE_NATIVE_BUFFER))
420 dpy->Extensions.KHR_image_pixmap = EGL_TRUE;
421
422 if (egl_g3d_add_configs(drv, dpy, 1) == 1) {
423 _eglError(EGL_NOT_INITIALIZED, "eglInitialize(unable to add configs)");
424 goto fail;
425 }
426
427 *major = 1;
428 *minor = 4;
429
430 return EGL_TRUE;
431
432 fail:
433 if (gdpy)
434 egl_g3d_terminate(drv, dpy);
435 return EGL_FALSE;
436 }
437
438 static _EGLContext *
439 egl_g3d_create_context(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf,
440 _EGLContext *share, const EGLint *attribs)
441 {
442 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
443 struct egl_g3d_context *gshare = egl_g3d_context(share);
444 struct egl_g3d_config *gconf = egl_g3d_config(conf);
445 struct egl_g3d_context *gctx;
446
447 gctx = CALLOC_STRUCT(egl_g3d_context);
448 if (!gctx) {
449 _eglError(EGL_BAD_ALLOC, "eglCreateContext");
450 return NULL;
451 }
452
453 if (!_eglInitContext(&gctx->base, dpy, conf, attribs)) {
454 free(gctx);
455 return NULL;
456 }
457
458 gctx->stapi = egl_g3d_choose_st(drv, &gctx->base);
459 if (!gctx->stapi) {
460 free(gctx);
461 return NULL;
462 }
463
464 gctx->stctxi = gctx->stapi->create_context(gctx->stapi, gdpy->smapi,
465 &gconf->stvis, (gshare) ? gshare->stctxi : NULL);
466 if (!gctx->stctxi) {
467 free(gctx);
468 return NULL;
469 }
470
471 gctx->stctxi->st_manager_private = (void *) &gctx->base;
472
473 return &gctx->base;
474 }
475
476 /**
477 * Destroy a context.
478 */
479 static void
480 destroy_context(_EGLDisplay *dpy, _EGLContext *ctx)
481 {
482 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
483
484 /* FIXME a context might live longer than its display */
485 if (!dpy->Initialized)
486 _eglLog(_EGL_FATAL, "destroy a context with an unitialized display");
487
488 gctx->stctxi->destroy(gctx->stctxi);
489
490 free(gctx);
491 }
492
493 static EGLBoolean
494 egl_g3d_destroy_context(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx)
495 {
496 if (!_eglIsContextBound(ctx))
497 destroy_context(dpy, ctx);
498 return EGL_TRUE;
499 }
500
501 struct egl_g3d_create_surface_arg {
502 EGLint type;
503 union {
504 EGLNativeWindowType win;
505 EGLNativePixmapType pix;
506 } u;
507 };
508
509 static _EGLSurface *
510 egl_g3d_create_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf,
511 struct egl_g3d_create_surface_arg *arg,
512 const EGLint *attribs)
513 {
514 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
515 struct egl_g3d_config *gconf = egl_g3d_config(conf);
516 struct egl_g3d_surface *gsurf;
517 struct native_surface *nsurf;
518 const char *err;
519
520 switch (arg->type) {
521 case EGL_WINDOW_BIT:
522 err = "eglCreateWindowSurface";
523 break;
524 case EGL_PIXMAP_BIT:
525 err = "eglCreatePixmapSurface";
526 break;
527 case EGL_PBUFFER_BIT:
528 err = "eglCreatePBufferSurface";
529 break;
530 #ifdef EGL_MESA_screen_surface
531 case EGL_SCREEN_BIT_MESA:
532 err = "eglCreateScreenSurface";
533 break;
534 #endif
535 default:
536 err = "eglCreateUnknownSurface";
537 break;
538 }
539
540 gsurf = CALLOC_STRUCT(egl_g3d_surface);
541 if (!gsurf) {
542 _eglError(EGL_BAD_ALLOC, err);
543 return NULL;
544 }
545
546 if (!_eglInitSurface(&gsurf->base, dpy, arg->type, conf, attribs)) {
547 free(gsurf);
548 return NULL;
549 }
550
551 /* create the native surface */
552 switch (arg->type) {
553 case EGL_WINDOW_BIT:
554 nsurf = gdpy->native->create_window_surface(gdpy->native,
555 arg->u.win, gconf->native);
556 break;
557 case EGL_PIXMAP_BIT:
558 nsurf = gdpy->native->create_pixmap_surface(gdpy->native,
559 arg->u.pix, gconf->native);
560 break;
561 case EGL_PBUFFER_BIT:
562 nsurf = gdpy->native->create_pbuffer_surface(gdpy->native,
563 gconf->native, gsurf->base.Width, gsurf->base.Height);
564 break;
565 #ifdef EGL_MESA_screen_surface
566 case EGL_SCREEN_BIT_MESA:
567 /* prefer back buffer (move to _eglInitSurface?) */
568 gsurf->base.RenderBuffer = EGL_BACK_BUFFER;
569 nsurf = gdpy->native->modeset->create_scanout_surface(gdpy->native,
570 gconf->native, gsurf->base.Width, gsurf->base.Height);
571 break;
572 #endif
573 default:
574 nsurf = NULL;
575 break;
576 }
577
578 if (!nsurf) {
579 free(gsurf);
580 return NULL;
581 }
582 /* initialize the geometry */
583 if (!nsurf->validate(nsurf, 0x0, &gsurf->sequence_number, NULL,
584 &gsurf->base.Width, &gsurf->base.Height)) {
585 nsurf->destroy(nsurf);
586 free(gsurf);
587 return NULL;
588 }
589
590 gsurf->stvis = gconf->stvis;
591 if (gsurf->base.RenderBuffer == EGL_SINGLE_BUFFER)
592 gsurf->stvis.render_buffer = ST_ATTACHMENT_FRONT_LEFT;
593
594 gsurf->stfbi = egl_g3d_create_st_framebuffer(&gsurf->base);
595 if (!gsurf->stfbi) {
596 gsurf->native->destroy(gsurf->native);
597 free(gsurf);
598 return NULL;
599 }
600
601 nsurf->user_data = &gsurf->base;
602 gsurf->native = nsurf;
603
604 return &gsurf->base;
605 }
606
607 static _EGLSurface *
608 egl_g3d_create_window_surface(_EGLDriver *drv, _EGLDisplay *dpy,
609 _EGLConfig *conf, EGLNativeWindowType win,
610 const EGLint *attribs)
611 {
612 struct egl_g3d_create_surface_arg arg;
613
614 memset(&arg, 0, sizeof(arg));
615 arg.type = EGL_WINDOW_BIT;
616 arg.u.win = win;
617
618 return egl_g3d_create_surface(drv, dpy, conf, &arg, attribs);
619 }
620
621 static _EGLSurface *
622 egl_g3d_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *dpy,
623 _EGLConfig *conf, EGLNativePixmapType pix,
624 const EGLint *attribs)
625 {
626 struct egl_g3d_create_surface_arg arg;
627
628 memset(&arg, 0, sizeof(arg));
629 arg.type = EGL_PIXMAP_BIT;
630 arg.u.pix = pix;
631
632 return egl_g3d_create_surface(drv, dpy, conf, &arg, attribs);
633 }
634
635 static _EGLSurface *
636 egl_g3d_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *dpy,
637 _EGLConfig *conf, const EGLint *attribs)
638 {
639 struct egl_g3d_create_surface_arg arg;
640
641 memset(&arg, 0, sizeof(arg));
642 arg.type = EGL_PBUFFER_BIT;
643
644 return egl_g3d_create_surface(drv, dpy, conf, &arg, attribs);
645 }
646
647 /**
648 * Destroy a surface.
649 */
650 static void
651 destroy_surface(_EGLDisplay *dpy, _EGLSurface *surf)
652 {
653 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
654
655 /* FIXME a surface might live longer than its display */
656 if (!dpy->Initialized)
657 _eglLog(_EGL_FATAL, "destroy a surface with an unitialized display");
658
659 pipe_texture_reference(&gsurf->render_texture, NULL);
660 egl_g3d_destroy_st_framebuffer(gsurf->stfbi);
661 gsurf->native->destroy(gsurf->native);
662 free(gsurf);
663 }
664
665 static EGLBoolean
666 egl_g3d_destroy_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
667 {
668 if (!_eglIsSurfaceBound(surf))
669 destroy_surface(dpy, surf);
670 return EGL_TRUE;
671 }
672
673 static EGLBoolean
674 egl_g3d_make_current(_EGLDriver *drv, _EGLDisplay *dpy,
675 _EGLSurface *draw, _EGLSurface *read, _EGLContext *ctx)
676 {
677 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
678 struct egl_g3d_surface *gdraw = egl_g3d_surface(draw);
679 struct egl_g3d_surface *gread = egl_g3d_surface(read);
680 struct egl_g3d_context *old_gctx;
681 EGLBoolean ok = EGL_TRUE;
682
683 /* bind the new context and return the "orphaned" one */
684 if (!_eglBindContext(&ctx, &draw, &read))
685 return EGL_FALSE;
686 old_gctx = egl_g3d_context(ctx);
687
688 if (old_gctx) {
689 /* flush old context */
690 old_gctx->stctxi->flush(old_gctx->stctxi,
691 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
692 }
693
694 if (gctx) {
695 ok = gctx->stapi->make_current(gctx->stapi, gctx->stctxi,
696 (gdraw) ? gdraw->stfbi : NULL, (gread) ? gread->stfbi : NULL);
697 if (ok) {
698 gctx->stctxi->notify_invalid_framebuffer(gctx->stctxi, gdraw->stfbi);
699 if (gread != gdraw) {
700 gctx->stctxi->notify_invalid_framebuffer(gctx->stctxi,
701 gread->stfbi);
702 }
703
704 if (gdraw->base.Type == EGL_WINDOW_BIT) {
705 gctx->base.WindowRenderBuffer =
706 (gdraw->stvis.render_buffer == ST_ATTACHMENT_FRONT_LEFT) ?
707 EGL_SINGLE_BUFFER : EGL_BACK_BUFFER;
708 }
709 }
710 }
711 else if (old_gctx) {
712 ok = old_gctx->stapi->make_current(old_gctx->stapi, NULL, NULL, NULL);
713 old_gctx->base.WindowRenderBuffer = EGL_NONE;
714 }
715
716 if (ctx && !_eglIsContextLinked(ctx))
717 destroy_context(dpy, ctx);
718 if (draw && !_eglIsSurfaceLinked(draw))
719 destroy_surface(dpy, draw);
720 if (read && read != draw && !_eglIsSurfaceLinked(read))
721 destroy_surface(dpy, read);
722
723 return ok;
724 }
725
726 static EGLBoolean
727 egl_g3d_swap_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
728 {
729 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
730 _EGLContext *ctx = _eglGetCurrentContext();
731 struct egl_g3d_context *gctx = NULL;
732
733 /* no-op for pixmap or pbuffer surface */
734 if (gsurf->base.Type == EGL_PIXMAP_BIT ||
735 gsurf->base.Type == EGL_PBUFFER_BIT)
736 return EGL_TRUE;
737
738 /* or when the surface is single-buffered */
739 if (gsurf->stvis.render_buffer == ST_ATTACHMENT_FRONT_LEFT)
740 return EGL_TRUE;
741
742 if (ctx && ctx->DrawSurface == surf)
743 gctx = egl_g3d_context(ctx);
744
745 /* flush if the surface is current */
746 if (gctx) {
747 gctx->stctxi->flush(gctx->stctxi,
748 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
749 }
750
751 return gsurf->native->swap_buffers(gsurf->native);
752 }
753
754 /**
755 * Find a config that supports the pixmap.
756 */
757 _EGLConfig *
758 egl_g3d_find_pixmap_config(_EGLDisplay *dpy, EGLNativePixmapType pix)
759 {
760 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
761 struct egl_g3d_config *gconf;
762 EGLint i;
763
764 for (i = 0; i < dpy->NumConfigs; i++) {
765 gconf = egl_g3d_config(dpy->Configs[i]);
766 if (gdpy->native->is_pixmap_supported(gdpy->native, pix, gconf->native))
767 break;
768 }
769
770 return (i < dpy->NumConfigs) ? &gconf->base : NULL;
771 }
772
773 /**
774 * Get the pipe surface of the given attachment of the native surface.
775 */
776 static struct pipe_surface *
777 get_pipe_surface(struct native_display *ndpy, struct native_surface *nsurf,
778 enum native_attachment natt)
779 {
780 struct pipe_texture *textures[NUM_NATIVE_ATTACHMENTS];
781 struct pipe_surface *psurf;
782
783 textures[natt] = NULL;
784 nsurf->validate(nsurf, 1 << natt, NULL, textures, NULL, NULL);
785 if (!textures[natt])
786 return NULL;
787
788 psurf = ndpy->screen->get_tex_surface(ndpy->screen, textures[natt],
789 0, 0, 0, PIPE_BUFFER_USAGE_GPU_WRITE);
790 pipe_texture_reference(&textures[natt], NULL);
791
792 return psurf;
793 }
794
795 static EGLBoolean
796 egl_g3d_copy_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
797 EGLNativePixmapType target)
798 {
799 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
800 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
801 _EGLContext *ctx = _eglGetCurrentContext();
802 struct egl_g3d_config *gconf;
803 struct native_surface *nsurf;
804 struct pipe_screen *screen = gdpy->native->screen;
805 struct pipe_surface *psurf;
806
807 if (!gsurf->render_texture)
808 return EGL_TRUE;
809
810 gconf = egl_g3d_config(egl_g3d_find_pixmap_config(dpy, target));
811 if (!gconf)
812 return _eglError(EGL_BAD_NATIVE_PIXMAP, "eglCopyBuffers");
813
814 nsurf = gdpy->native->create_pixmap_surface(gdpy->native,
815 target, gconf->native);
816 if (!nsurf)
817 return _eglError(EGL_BAD_NATIVE_PIXMAP, "eglCopyBuffers");
818
819 /* flush if the surface is current */
820 if (ctx && ctx->DrawSurface == &gsurf->base) {
821 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
822 gctx->stctxi->flush(gctx->stctxi,
823 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
824 }
825
826 /* create a pipe context to copy surfaces */
827 if (!gdpy->pipe) {
828 gdpy->pipe =
829 gdpy->native->screen->context_create(gdpy->native->screen, NULL);
830 if (!gdpy->pipe)
831 return EGL_FALSE;
832 }
833
834 psurf = get_pipe_surface(gdpy->native, nsurf, NATIVE_ATTACHMENT_FRONT_LEFT);
835 if (psurf) {
836 struct pipe_surface *psrc;
837
838 psrc = screen->get_tex_surface(screen, gsurf->render_texture,
839 0, 0, 0, PIPE_BUFFER_USAGE_GPU_READ);
840 if (psrc) {
841 gdpy->pipe->surface_copy(gdpy->pipe, psurf, 0, 0,
842 psrc, 0, 0, psurf->width, psurf->height);
843 pipe_surface_reference(&psrc, NULL);
844
845 nsurf->flush_frontbuffer(nsurf);
846 }
847
848 pipe_surface_reference(&psurf, NULL);
849 }
850
851 nsurf->destroy(nsurf);
852
853 return EGL_TRUE;
854 }
855
856 static EGLBoolean
857 egl_g3d_wait_client(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx)
858 {
859 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
860 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
861 struct pipe_screen *screen = gdpy->native->screen;
862 struct pipe_fence_handle *fence = NULL;
863
864 gctx->stctxi->flush(gctx->stctxi,
865 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, &fence);
866 screen->fence_finish(screen, fence, 0);
867 screen->fence_reference(screen, &fence, NULL);
868
869 return EGL_TRUE;
870 }
871
872 static EGLBoolean
873 egl_g3d_wait_native(_EGLDriver *drv, _EGLDisplay *dpy, EGLint engine)
874 {
875 _EGLContext *ctx = _eglGetCurrentContext();
876
877 if (engine != EGL_CORE_NATIVE_ENGINE)
878 return _eglError(EGL_BAD_PARAMETER, "eglWaitNative");
879
880 if (ctx && ctx->DrawSurface) {
881 struct egl_g3d_surface *gsurf = egl_g3d_surface(ctx->DrawSurface);
882 gsurf->native->wait(gsurf->native);
883 }
884
885 return EGL_TRUE;
886 }
887
888 static _EGLProc
889 egl_g3d_get_proc_address(_EGLDriver *drv, const char *procname)
890 {
891 struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
892 _EGLProc proc;
893 EGLint i;
894
895 /* in case this is called before a display is initialized */
896 egl_g3d_init_st(&gdrv->base);
897
898 for (i = 0; i < ST_API_COUNT; i++) {
899 struct st_api *stapi = gdrv->stapis[i];
900 if (stapi) {
901 proc = (_EGLProc) stapi->get_proc_address(stapi, procname);
902 if (proc)
903 return proc;
904 }
905 }
906
907 return (_EGLProc) NULL;
908 }
909
910 static EGLBoolean
911 egl_g3d_bind_tex_image(_EGLDriver *drv, _EGLDisplay *dpy,
912 _EGLSurface *surf, EGLint buffer)
913 {
914 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
915 _EGLContext *es1 = _eglGetAPIContext(EGL_OPENGL_ES_API);
916 struct egl_g3d_context *gctx;
917 enum pipe_format internal_format;
918 enum st_texture_type target;
919
920 if (!gsurf || gsurf->base.Type != EGL_PBUFFER_BIT)
921 return _eglError(EGL_BAD_SURFACE, "eglBindTexImage");
922 if (buffer != EGL_BACK_BUFFER)
923 return _eglError(EGL_BAD_PARAMETER, "eglBindTexImage");
924 if (gsurf->base.BoundToTexture)
925 return _eglError(EGL_BAD_ACCESS, "eglBindTexImage");
926
927 switch (gsurf->base.TextureFormat) {
928 case EGL_TEXTURE_RGB:
929 internal_format = PIPE_FORMAT_R8G8B8_UNORM;
930 break;
931 case EGL_TEXTURE_RGBA:
932 internal_format = PIPE_FORMAT_B8G8R8A8_UNORM;
933 break;
934 default:
935 return _eglError(EGL_BAD_MATCH, "eglBindTexImage");
936 }
937
938 switch (gsurf->base.TextureTarget) {
939 case EGL_TEXTURE_2D:
940 target = ST_TEXTURE_2D;
941 break;
942 default:
943 return _eglError(EGL_BAD_MATCH, "eglBindTexImage");
944 }
945
946 if (!es1)
947 return EGL_TRUE;
948 if (!gsurf->render_texture)
949 return EGL_FALSE;
950
951 /* flush properly if the surface is bound */
952 if (gsurf->base.CurrentContext) {
953 gctx = egl_g3d_context(gsurf->base.CurrentContext);
954 gctx->stctxi->flush(gctx->stctxi,
955 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
956 }
957
958 gctx = egl_g3d_context(es1);
959 if (gctx->stctxi->teximage) {
960 if (!gctx->stctxi->teximage(gctx->stctxi, target,
961 gsurf->base.MipmapLevel, internal_format,
962 gsurf->render_texture, gsurf->base.MipmapTexture))
963 return EGL_FALSE;
964 gsurf->base.BoundToTexture = EGL_TRUE;
965 }
966
967 return EGL_TRUE;
968 }
969
970 static EGLBoolean
971 egl_g3d_release_tex_image(_EGLDriver *drv, _EGLDisplay *dpy,
972 _EGLSurface *surf, EGLint buffer)
973 {
974 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
975
976 if (!gsurf || gsurf->base.Type != EGL_PBUFFER_BIT ||
977 !gsurf->base.BoundToTexture)
978 return _eglError(EGL_BAD_SURFACE, "eglReleaseTexImage");
979 if (buffer != EGL_BACK_BUFFER)
980 return _eglError(EGL_BAD_PARAMETER, "eglReleaseTexImage");
981
982 if (gsurf->render_texture) {
983 _EGLContext *ctx = _eglGetAPIContext(EGL_OPENGL_ES_API);
984 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
985
986 /* what if the context the surface binds to is no longer current? */
987 if (gctx) {
988 gctx->stctxi->teximage(gctx->stctxi, ST_TEXTURE_2D,
989 gsurf->base.MipmapLevel, PIPE_FORMAT_NONE, NULL, FALSE);
990 }
991 }
992
993 gsurf->base.BoundToTexture = EGL_FALSE;
994
995 return EGL_TRUE;
996 }
997
998 #ifdef EGL_MESA_screen_surface
999
1000 static _EGLSurface *
1001 egl_g3d_create_screen_surface(_EGLDriver *drv, _EGLDisplay *dpy,
1002 _EGLConfig *conf, const EGLint *attribs)
1003 {
1004 struct egl_g3d_create_surface_arg arg;
1005
1006 memset(&arg, 0, sizeof(arg));
1007 arg.type = EGL_SCREEN_BIT_MESA;
1008
1009 return egl_g3d_create_surface(drv, dpy, conf, &arg, attribs);
1010 }
1011
1012 static EGLBoolean
1013 egl_g3d_show_screen_surface(_EGLDriver *drv, _EGLDisplay *dpy,
1014 _EGLScreen *scr, _EGLSurface *surf,
1015 _EGLMode *mode)
1016 {
1017 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
1018 struct egl_g3d_screen *gscr = egl_g3d_screen(scr);
1019 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
1020 struct native_surface *nsurf;
1021 const struct native_mode *nmode;
1022 EGLBoolean changed;
1023
1024 if (gsurf) {
1025 EGLint idx;
1026
1027 if (!mode)
1028 return _eglError(EGL_BAD_MATCH, "eglShowSurfaceMESA");
1029 if (gsurf->base.Type != EGL_SCREEN_BIT_MESA)
1030 return _eglError(EGL_BAD_SURFACE, "eglShowScreenSurfaceMESA");
1031 if (gsurf->base.Width < mode->Width || gsurf->base.Height < mode->Height)
1032 return _eglError(EGL_BAD_MATCH,
1033 "eglShowSurfaceMESA(surface smaller than mode size)");
1034
1035 /* find the index of the mode */
1036 for (idx = 0; idx < gscr->base.NumModes; idx++)
1037 if (mode == &gscr->base.Modes[idx])
1038 break;
1039 if (idx >= gscr->base.NumModes) {
1040 return _eglError(EGL_BAD_MODE_MESA,
1041 "eglShowSurfaceMESA(unknown mode)");
1042 }
1043
1044 nsurf = gsurf->native;
1045 nmode = gscr->native_modes[idx];
1046 }
1047 else {
1048 if (mode)
1049 return _eglError(EGL_BAD_MATCH, "eglShowSurfaceMESA");
1050
1051 /* disable the screen */
1052 nsurf = NULL;
1053 nmode = NULL;
1054 }
1055
1056 /* TODO surface panning by CRTC choosing */
1057 changed = gdpy->native->modeset->program(gdpy->native, 0, nsurf,
1058 gscr->base.OriginX, gscr->base.OriginY, &gscr->native, 1, nmode);
1059 if (changed) {
1060 gscr->base.CurrentSurface = &gsurf->base;
1061 gscr->base.CurrentMode = mode;
1062 }
1063
1064 return changed;
1065 }
1066
1067 #endif /* EGL_MESA_screen_surface */
1068
1069 static EGLint
1070 egl_g3d_probe(_EGLDriver *drv, _EGLDisplay *dpy)
1071 {
1072 struct native_probe *nprobe;
1073 enum native_probe_result res;
1074 EGLint score;
1075
1076 nprobe = egl_g3d_get_probe(drv, dpy);
1077 res = native_get_probe_result(nprobe);
1078
1079 switch (res) {
1080 case NATIVE_PROBE_UNKNOWN:
1081 default:
1082 score = 0;
1083 break;
1084 case NATIVE_PROBE_FALLBACK:
1085 score = 40;
1086 break;
1087 case NATIVE_PROBE_SUPPORTED:
1088 score = 50;
1089 break;
1090 case NATIVE_PROBE_EXACT:
1091 score = 100;
1092 break;
1093 }
1094
1095 return score;
1096 }
1097
1098 static void
1099 egl_g3d_unload(_EGLDriver *drv)
1100 {
1101 struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
1102 EGLint i;
1103
1104 for (i = 0; i < ST_API_COUNT; i++) {
1105 if (gdrv->stapis[i])
1106 gdrv->stapis[i]->destroy(gdrv->stapis[i]);
1107 }
1108
1109 egl_g3d_destroy_probe(drv, NULL);
1110 free(gdrv);
1111 }
1112
1113 _EGLDriver *
1114 _eglMain(const char *args)
1115 {
1116 static char driver_name[64];
1117 struct egl_g3d_driver *gdrv;
1118
1119 snprintf(driver_name, sizeof(driver_name),
1120 "Gallium/%s", native_get_name());
1121
1122 gdrv = CALLOC_STRUCT(egl_g3d_driver);
1123 if (!gdrv)
1124 return NULL;
1125
1126 _eglInitDriverFallbacks(&gdrv->base);
1127
1128 gdrv->base.API.Initialize = egl_g3d_initialize;
1129 gdrv->base.API.Terminate = egl_g3d_terminate;
1130 gdrv->base.API.CreateContext = egl_g3d_create_context;
1131 gdrv->base.API.DestroyContext = egl_g3d_destroy_context;
1132 gdrv->base.API.CreateWindowSurface = egl_g3d_create_window_surface;
1133 gdrv->base.API.CreatePixmapSurface = egl_g3d_create_pixmap_surface;
1134 gdrv->base.API.CreatePbufferSurface = egl_g3d_create_pbuffer_surface;
1135 gdrv->base.API.DestroySurface = egl_g3d_destroy_surface;
1136 gdrv->base.API.MakeCurrent = egl_g3d_make_current;
1137 gdrv->base.API.SwapBuffers = egl_g3d_swap_buffers;
1138 gdrv->base.API.CopyBuffers = egl_g3d_copy_buffers;
1139 gdrv->base.API.WaitClient = egl_g3d_wait_client;
1140 gdrv->base.API.WaitNative = egl_g3d_wait_native;
1141 gdrv->base.API.GetProcAddress = egl_g3d_get_proc_address;
1142
1143 gdrv->base.API.BindTexImage = egl_g3d_bind_tex_image;
1144 gdrv->base.API.ReleaseTexImage = egl_g3d_release_tex_image;
1145
1146 gdrv->base.API.CreateImageKHR = egl_g3d_create_image;
1147 gdrv->base.API.DestroyImageKHR = egl_g3d_destroy_image;
1148
1149 #ifdef EGL_MESA_screen_surface
1150 gdrv->base.API.CreateScreenSurfaceMESA = egl_g3d_create_screen_surface;
1151 gdrv->base.API.ShowScreenSurfaceMESA = egl_g3d_show_screen_surface;
1152 #endif
1153
1154 gdrv->base.Name = driver_name;
1155 gdrv->base.Probe = egl_g3d_probe;
1156 gdrv->base.Unload = egl_g3d_unload;
1157
1158 /* the key is " EGL G3D" */
1159 gdrv->probe_key = 0x0E61063D;
1160
1161 return &gdrv->base;
1162 }