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 #ifdef EGL_MESA_screen_surface
528 case EGL_SCREEN_BIT_MESA:
529 err = "eglCreateScreenSurface";
530 break;
531 #endif
532 default:
533 err = "eglCreateUnknownSurface";
534 break;
535 }
536
537 gsurf = CALLOC_STRUCT(egl_g3d_surface);
538 if (!gsurf) {
539 _eglError(EGL_BAD_ALLOC, err);
540 return NULL;
541 }
542
543 if (!_eglInitSurface(&gsurf->base, dpy, arg->type, conf, attribs)) {
544 free(gsurf);
545 return NULL;
546 }
547
548 /* create the native surface */
549 switch (arg->type) {
550 case EGL_WINDOW_BIT:
551 nsurf = gdpy->native->create_window_surface(gdpy->native,
552 arg->u.win, gconf->native);
553 break;
554 case EGL_PIXMAP_BIT:
555 nsurf = gdpy->native->create_pixmap_surface(gdpy->native,
556 arg->u.pix, gconf->native);
557 break;
558 #ifdef EGL_MESA_screen_surface
559 case EGL_SCREEN_BIT_MESA:
560 /* prefer back buffer (move to _eglInitSurface?) */
561 gsurf->base.RenderBuffer = EGL_BACK_BUFFER;
562 nsurf = gdpy->native->modeset->create_scanout_surface(gdpy->native,
563 gconf->native, gsurf->base.Width, gsurf->base.Height);
564 break;
565 #endif
566 default:
567 nsurf = NULL;
568 break;
569 }
570
571 if (!nsurf) {
572 free(gsurf);
573 return NULL;
574 }
575 /* initialize the geometry */
576 if (!nsurf->validate(nsurf, 0x0, &gsurf->sequence_number, NULL,
577 &gsurf->base.Width, &gsurf->base.Height)) {
578 nsurf->destroy(nsurf);
579 free(gsurf);
580 return NULL;
581 }
582
583 gsurf->stvis = gconf->stvis;
584 if (gsurf->base.RenderBuffer == EGL_SINGLE_BUFFER)
585 gsurf->stvis.render_buffer = ST_ATTACHMENT_FRONT_LEFT;
586
587 gsurf->stfbi = egl_g3d_create_st_framebuffer(&gsurf->base);
588 if (!gsurf->stfbi) {
589 nsurf->destroy(nsurf);
590 free(gsurf);
591 return NULL;
592 }
593
594 nsurf->user_data = &gsurf->base;
595 gsurf->native = nsurf;
596
597 return &gsurf->base;
598 }
599
600 static _EGLSurface *
601 egl_g3d_create_window_surface(_EGLDriver *drv, _EGLDisplay *dpy,
602 _EGLConfig *conf, EGLNativeWindowType win,
603 const EGLint *attribs)
604 {
605 struct egl_g3d_create_surface_arg arg;
606
607 memset(&arg, 0, sizeof(arg));
608 arg.type = EGL_WINDOW_BIT;
609 arg.u.win = win;
610
611 return egl_g3d_create_surface(drv, dpy, conf, &arg, attribs);
612 }
613
614 static _EGLSurface *
615 egl_g3d_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *dpy,
616 _EGLConfig *conf, EGLNativePixmapType pix,
617 const EGLint *attribs)
618 {
619 struct egl_g3d_create_surface_arg arg;
620
621 memset(&arg, 0, sizeof(arg));
622 arg.type = EGL_PIXMAP_BIT;
623 arg.u.pix = pix;
624
625 return egl_g3d_create_surface(drv, dpy, conf, &arg, attribs);
626 }
627
628 static _EGLSurface *
629 egl_g3d_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *dpy,
630 _EGLConfig *conf, const EGLint *attribs)
631 {
632 struct egl_g3d_config *gconf = egl_g3d_config(conf);
633 struct egl_g3d_surface *gsurf;
634
635 gsurf = CALLOC_STRUCT(egl_g3d_surface);
636 if (!gsurf) {
637 _eglError(EGL_BAD_ALLOC, "eglCreatePbufferSurface");
638 return NULL;
639 }
640
641 if (!_eglInitSurface(&gsurf->base, dpy, EGL_PBUFFER_BIT, conf, attribs)) {
642 free(gsurf);
643 return NULL;
644 }
645
646 gsurf->stvis = gconf->stvis;
647
648 gsurf->stfbi = egl_g3d_create_st_framebuffer(&gsurf->base);
649 if (!gsurf->stfbi) {
650 free(gsurf);
651 return NULL;
652 }
653
654 return &gsurf->base;
655 }
656
657 /**
658 * Destroy a surface.
659 */
660 static void
661 destroy_surface(_EGLDisplay *dpy, _EGLSurface *surf)
662 {
663 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
664
665 /* FIXME a surface might live longer than its display */
666 if (!dpy->Initialized)
667 _eglLog(_EGL_FATAL, "destroy a surface with an unitialized display");
668
669 pipe_texture_reference(&gsurf->render_texture, NULL);
670 egl_g3d_destroy_st_framebuffer(gsurf->stfbi);
671 if (gsurf->native)
672 gsurf->native->destroy(gsurf->native);
673 free(gsurf);
674 }
675
676 static EGLBoolean
677 egl_g3d_destroy_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
678 {
679 if (!_eglIsSurfaceBound(surf))
680 destroy_surface(dpy, surf);
681 return EGL_TRUE;
682 }
683
684 static EGLBoolean
685 egl_g3d_make_current(_EGLDriver *drv, _EGLDisplay *dpy,
686 _EGLSurface *draw, _EGLSurface *read, _EGLContext *ctx)
687 {
688 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
689 struct egl_g3d_surface *gdraw = egl_g3d_surface(draw);
690 struct egl_g3d_surface *gread = egl_g3d_surface(read);
691 struct egl_g3d_context *old_gctx;
692 EGLBoolean ok = EGL_TRUE;
693
694 /* bind the new context and return the "orphaned" one */
695 if (!_eglBindContext(&ctx, &draw, &read))
696 return EGL_FALSE;
697 old_gctx = egl_g3d_context(ctx);
698
699 if (old_gctx) {
700 /* flush old context */
701 old_gctx->stctxi->flush(old_gctx->stctxi,
702 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
703 }
704
705 if (gctx) {
706 ok = gctx->stapi->make_current(gctx->stapi, gctx->stctxi,
707 (gdraw) ? gdraw->stfbi : NULL, (gread) ? gread->stfbi : NULL);
708 if (ok) {
709 gctx->stctxi->notify_invalid_framebuffer(gctx->stctxi, gdraw->stfbi);
710 if (gread != gdraw) {
711 gctx->stctxi->notify_invalid_framebuffer(gctx->stctxi,
712 gread->stfbi);
713 }
714
715 if (gdraw->base.Type == EGL_WINDOW_BIT) {
716 gctx->base.WindowRenderBuffer =
717 (gdraw->stvis.render_buffer == ST_ATTACHMENT_FRONT_LEFT) ?
718 EGL_SINGLE_BUFFER : EGL_BACK_BUFFER;
719 }
720 }
721 }
722 else if (old_gctx) {
723 ok = old_gctx->stapi->make_current(old_gctx->stapi, NULL, NULL, NULL);
724 old_gctx->base.WindowRenderBuffer = EGL_NONE;
725 }
726
727 if (ctx && !_eglIsContextLinked(ctx))
728 destroy_context(dpy, ctx);
729 if (draw && !_eglIsSurfaceLinked(draw))
730 destroy_surface(dpy, draw);
731 if (read && read != draw && !_eglIsSurfaceLinked(read))
732 destroy_surface(dpy, read);
733
734 return ok;
735 }
736
737 static EGLBoolean
738 egl_g3d_swap_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
739 {
740 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
741 _EGLContext *ctx = _eglGetCurrentContext();
742 struct egl_g3d_context *gctx = NULL;
743
744 /* no-op for pixmap or pbuffer surface */
745 if (gsurf->base.Type == EGL_PIXMAP_BIT ||
746 gsurf->base.Type == EGL_PBUFFER_BIT)
747 return EGL_TRUE;
748
749 /* or when the surface is single-buffered */
750 if (gsurf->stvis.render_buffer == ST_ATTACHMENT_FRONT_LEFT)
751 return EGL_TRUE;
752
753 if (ctx && ctx->DrawSurface == surf)
754 gctx = egl_g3d_context(ctx);
755
756 /* flush if the surface is current */
757 if (gctx) {
758 gctx->stctxi->flush(gctx->stctxi,
759 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
760 }
761
762 return gsurf->native->swap_buffers(gsurf->native);
763 }
764
765 /**
766 * Find a config that supports the pixmap.
767 */
768 _EGLConfig *
769 egl_g3d_find_pixmap_config(_EGLDisplay *dpy, EGLNativePixmapType pix)
770 {
771 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
772 struct egl_g3d_config *gconf;
773 EGLint i;
774
775 for (i = 0; i < dpy->NumConfigs; i++) {
776 gconf = egl_g3d_config(dpy->Configs[i]);
777 if (gdpy->native->is_pixmap_supported(gdpy->native, pix, gconf->native))
778 break;
779 }
780
781 return (i < dpy->NumConfigs) ? &gconf->base : NULL;
782 }
783
784 /**
785 * Get the pipe surface of the given attachment of the native surface.
786 */
787 static struct pipe_surface *
788 get_pipe_surface(struct native_display *ndpy, struct native_surface *nsurf,
789 enum native_attachment natt)
790 {
791 struct pipe_texture *textures[NUM_NATIVE_ATTACHMENTS];
792 struct pipe_surface *psurf;
793
794 textures[natt] = NULL;
795 nsurf->validate(nsurf, 1 << natt, NULL, textures, NULL, NULL);
796 if (!textures[natt])
797 return NULL;
798
799 psurf = ndpy->screen->get_tex_surface(ndpy->screen, textures[natt],
800 0, 0, 0, PIPE_BUFFER_USAGE_GPU_WRITE);
801 pipe_texture_reference(&textures[natt], NULL);
802
803 return psurf;
804 }
805
806 static EGLBoolean
807 egl_g3d_copy_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
808 EGLNativePixmapType target)
809 {
810 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
811 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
812 _EGLContext *ctx = _eglGetCurrentContext();
813 struct egl_g3d_config *gconf;
814 struct native_surface *nsurf;
815 struct pipe_screen *screen = gdpy->native->screen;
816 struct pipe_surface *psurf;
817
818 if (!gsurf->render_texture)
819 return EGL_TRUE;
820
821 gconf = egl_g3d_config(egl_g3d_find_pixmap_config(dpy, target));
822 if (!gconf)
823 return _eglError(EGL_BAD_NATIVE_PIXMAP, "eglCopyBuffers");
824
825 nsurf = gdpy->native->create_pixmap_surface(gdpy->native,
826 target, gconf->native);
827 if (!nsurf)
828 return _eglError(EGL_BAD_NATIVE_PIXMAP, "eglCopyBuffers");
829
830 /* flush if the surface is current */
831 if (ctx && ctx->DrawSurface == &gsurf->base) {
832 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
833 gctx->stctxi->flush(gctx->stctxi,
834 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
835 }
836
837 /* create a pipe context to copy surfaces */
838 if (!gdpy->pipe) {
839 gdpy->pipe =
840 gdpy->native->screen->context_create(gdpy->native->screen, NULL);
841 if (!gdpy->pipe)
842 return EGL_FALSE;
843 }
844
845 psurf = get_pipe_surface(gdpy->native, nsurf, NATIVE_ATTACHMENT_FRONT_LEFT);
846 if (psurf) {
847 struct pipe_surface *psrc;
848
849 psrc = screen->get_tex_surface(screen, gsurf->render_texture,
850 0, 0, 0, PIPE_BUFFER_USAGE_GPU_READ);
851 if (psrc) {
852 gdpy->pipe->surface_copy(gdpy->pipe, psurf, 0, 0,
853 psrc, 0, 0, psurf->width, psurf->height);
854 pipe_surface_reference(&psrc, NULL);
855
856 nsurf->flush_frontbuffer(nsurf);
857 }
858
859 pipe_surface_reference(&psurf, NULL);
860 }
861
862 nsurf->destroy(nsurf);
863
864 return EGL_TRUE;
865 }
866
867 static EGLBoolean
868 egl_g3d_wait_client(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx)
869 {
870 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
871 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
872 struct pipe_screen *screen = gdpy->native->screen;
873 struct pipe_fence_handle *fence = NULL;
874
875 gctx->stctxi->flush(gctx->stctxi,
876 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, &fence);
877 screen->fence_finish(screen, fence, 0);
878 screen->fence_reference(screen, &fence, NULL);
879
880 return EGL_TRUE;
881 }
882
883 static EGLBoolean
884 egl_g3d_wait_native(_EGLDriver *drv, _EGLDisplay *dpy, EGLint engine)
885 {
886 _EGLContext *ctx = _eglGetCurrentContext();
887
888 if (engine != EGL_CORE_NATIVE_ENGINE)
889 return _eglError(EGL_BAD_PARAMETER, "eglWaitNative");
890
891 if (ctx && ctx->DrawSurface) {
892 struct egl_g3d_surface *gsurf = egl_g3d_surface(ctx->DrawSurface);
893
894 if (gsurf->native)
895 gsurf->native->wait(gsurf->native);
896 }
897
898 return EGL_TRUE;
899 }
900
901 static _EGLProc
902 egl_g3d_get_proc_address(_EGLDriver *drv, const char *procname)
903 {
904 struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
905 _EGLProc proc;
906 EGLint i;
907
908 /* in case this is called before a display is initialized */
909 egl_g3d_init_st(&gdrv->base);
910
911 for (i = 0; i < ST_API_COUNT; i++) {
912 struct st_api *stapi = gdrv->stapis[i];
913 if (stapi) {
914 proc = (_EGLProc) stapi->get_proc_address(stapi, procname);
915 if (proc)
916 return proc;
917 }
918 }
919
920 return (_EGLProc) NULL;
921 }
922
923 static EGLBoolean
924 egl_g3d_bind_tex_image(_EGLDriver *drv, _EGLDisplay *dpy,
925 _EGLSurface *surf, EGLint buffer)
926 {
927 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
928 _EGLContext *es1 = _eglGetAPIContext(EGL_OPENGL_ES_API);
929 struct egl_g3d_context *gctx;
930 enum pipe_format internal_format;
931 enum st_texture_type target;
932
933 if (!gsurf || gsurf->base.Type != EGL_PBUFFER_BIT)
934 return _eglError(EGL_BAD_SURFACE, "eglBindTexImage");
935 if (buffer != EGL_BACK_BUFFER)
936 return _eglError(EGL_BAD_PARAMETER, "eglBindTexImage");
937 if (gsurf->base.BoundToTexture)
938 return _eglError(EGL_BAD_ACCESS, "eglBindTexImage");
939
940 switch (gsurf->base.TextureFormat) {
941 case EGL_TEXTURE_RGB:
942 internal_format = PIPE_FORMAT_R8G8B8_UNORM;
943 break;
944 case EGL_TEXTURE_RGBA:
945 internal_format = PIPE_FORMAT_B8G8R8A8_UNORM;
946 break;
947 default:
948 return _eglError(EGL_BAD_MATCH, "eglBindTexImage");
949 }
950
951 switch (gsurf->base.TextureTarget) {
952 case EGL_TEXTURE_2D:
953 target = ST_TEXTURE_2D;
954 break;
955 default:
956 return _eglError(EGL_BAD_MATCH, "eglBindTexImage");
957 }
958
959 if (!es1)
960 return EGL_TRUE;
961 if (!gsurf->render_texture)
962 return EGL_FALSE;
963
964 /* flush properly if the surface is bound */
965 if (gsurf->base.CurrentContext) {
966 gctx = egl_g3d_context(gsurf->base.CurrentContext);
967 gctx->stctxi->flush(gctx->stctxi,
968 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
969 }
970
971 gctx = egl_g3d_context(es1);
972 if (gctx->stctxi->teximage) {
973 if (!gctx->stctxi->teximage(gctx->stctxi, target,
974 gsurf->base.MipmapLevel, internal_format,
975 gsurf->render_texture, gsurf->base.MipmapTexture))
976 return EGL_FALSE;
977 gsurf->base.BoundToTexture = EGL_TRUE;
978 }
979
980 return EGL_TRUE;
981 }
982
983 static EGLBoolean
984 egl_g3d_release_tex_image(_EGLDriver *drv, _EGLDisplay *dpy,
985 _EGLSurface *surf, EGLint buffer)
986 {
987 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
988
989 if (!gsurf || gsurf->base.Type != EGL_PBUFFER_BIT ||
990 !gsurf->base.BoundToTexture)
991 return _eglError(EGL_BAD_SURFACE, "eglReleaseTexImage");
992 if (buffer != EGL_BACK_BUFFER)
993 return _eglError(EGL_BAD_PARAMETER, "eglReleaseTexImage");
994
995 if (gsurf->render_texture) {
996 _EGLContext *ctx = _eglGetAPIContext(EGL_OPENGL_ES_API);
997 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
998
999 /* what if the context the surface binds to is no longer current? */
1000 if (gctx) {
1001 gctx->stctxi->teximage(gctx->stctxi, ST_TEXTURE_2D,
1002 gsurf->base.MipmapLevel, PIPE_FORMAT_NONE, NULL, FALSE);
1003 }
1004 }
1005
1006 gsurf->base.BoundToTexture = EGL_FALSE;
1007
1008 return EGL_TRUE;
1009 }
1010
1011 #ifdef EGL_MESA_screen_surface
1012
1013 static _EGLSurface *
1014 egl_g3d_create_screen_surface(_EGLDriver *drv, _EGLDisplay *dpy,
1015 _EGLConfig *conf, const EGLint *attribs)
1016 {
1017 struct egl_g3d_create_surface_arg arg;
1018
1019 memset(&arg, 0, sizeof(arg));
1020 arg.type = EGL_SCREEN_BIT_MESA;
1021
1022 return egl_g3d_create_surface(drv, dpy, conf, &arg, attribs);
1023 }
1024
1025 static EGLBoolean
1026 egl_g3d_show_screen_surface(_EGLDriver *drv, _EGLDisplay *dpy,
1027 _EGLScreen *scr, _EGLSurface *surf,
1028 _EGLMode *mode)
1029 {
1030 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
1031 struct egl_g3d_screen *gscr = egl_g3d_screen(scr);
1032 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
1033 struct native_surface *nsurf;
1034 const struct native_mode *nmode;
1035 EGLBoolean changed;
1036
1037 if (gsurf) {
1038 EGLint idx;
1039
1040 if (!mode)
1041 return _eglError(EGL_BAD_MATCH, "eglShowSurfaceMESA");
1042 if (gsurf->base.Type != EGL_SCREEN_BIT_MESA)
1043 return _eglError(EGL_BAD_SURFACE, "eglShowScreenSurfaceMESA");
1044 if (gsurf->base.Width < mode->Width || gsurf->base.Height < mode->Height)
1045 return _eglError(EGL_BAD_MATCH,
1046 "eglShowSurfaceMESA(surface smaller than mode size)");
1047
1048 /* find the index of the mode */
1049 for (idx = 0; idx < gscr->base.NumModes; idx++)
1050 if (mode == &gscr->base.Modes[idx])
1051 break;
1052 if (idx >= gscr->base.NumModes) {
1053 return _eglError(EGL_BAD_MODE_MESA,
1054 "eglShowSurfaceMESA(unknown mode)");
1055 }
1056
1057 nsurf = gsurf->native;
1058 nmode = gscr->native_modes[idx];
1059 }
1060 else {
1061 if (mode)
1062 return _eglError(EGL_BAD_MATCH, "eglShowSurfaceMESA");
1063
1064 /* disable the screen */
1065 nsurf = NULL;
1066 nmode = NULL;
1067 }
1068
1069 /* TODO surface panning by CRTC choosing */
1070 changed = gdpy->native->modeset->program(gdpy->native, 0, nsurf,
1071 gscr->base.OriginX, gscr->base.OriginY, &gscr->native, 1, nmode);
1072 if (changed) {
1073 gscr->base.CurrentSurface = &gsurf->base;
1074 gscr->base.CurrentMode = mode;
1075 }
1076
1077 return changed;
1078 }
1079
1080 #endif /* EGL_MESA_screen_surface */
1081
1082 static EGLint
1083 egl_g3d_probe(_EGLDriver *drv, _EGLDisplay *dpy)
1084 {
1085 struct native_probe *nprobe;
1086 enum native_probe_result res;
1087 EGLint score;
1088
1089 nprobe = egl_g3d_get_probe(drv, dpy);
1090 res = native_get_probe_result(nprobe);
1091
1092 switch (res) {
1093 case NATIVE_PROBE_UNKNOWN:
1094 default:
1095 score = 0;
1096 break;
1097 case NATIVE_PROBE_FALLBACK:
1098 score = 40;
1099 break;
1100 case NATIVE_PROBE_SUPPORTED:
1101 score = 50;
1102 break;
1103 case NATIVE_PROBE_EXACT:
1104 score = 100;
1105 break;
1106 }
1107
1108 return score;
1109 }
1110
1111 static void
1112 egl_g3d_unload(_EGLDriver *drv)
1113 {
1114 struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
1115 EGLint i;
1116
1117 for (i = 0; i < ST_API_COUNT; i++) {
1118 if (gdrv->stapis[i])
1119 gdrv->stapis[i]->destroy(gdrv->stapis[i]);
1120 }
1121
1122 egl_g3d_destroy_probe(drv, NULL);
1123 free(gdrv);
1124 }
1125
1126 _EGLDriver *
1127 _eglMain(const char *args)
1128 {
1129 static char driver_name[64];
1130 struct egl_g3d_driver *gdrv;
1131
1132 snprintf(driver_name, sizeof(driver_name),
1133 "Gallium/%s", native_get_name());
1134
1135 gdrv = CALLOC_STRUCT(egl_g3d_driver);
1136 if (!gdrv)
1137 return NULL;
1138
1139 _eglInitDriverFallbacks(&gdrv->base);
1140
1141 gdrv->base.API.Initialize = egl_g3d_initialize;
1142 gdrv->base.API.Terminate = egl_g3d_terminate;
1143 gdrv->base.API.CreateContext = egl_g3d_create_context;
1144 gdrv->base.API.DestroyContext = egl_g3d_destroy_context;
1145 gdrv->base.API.CreateWindowSurface = egl_g3d_create_window_surface;
1146 gdrv->base.API.CreatePixmapSurface = egl_g3d_create_pixmap_surface;
1147 gdrv->base.API.CreatePbufferSurface = egl_g3d_create_pbuffer_surface;
1148 gdrv->base.API.DestroySurface = egl_g3d_destroy_surface;
1149 gdrv->base.API.MakeCurrent = egl_g3d_make_current;
1150 gdrv->base.API.SwapBuffers = egl_g3d_swap_buffers;
1151 gdrv->base.API.CopyBuffers = egl_g3d_copy_buffers;
1152 gdrv->base.API.WaitClient = egl_g3d_wait_client;
1153 gdrv->base.API.WaitNative = egl_g3d_wait_native;
1154 gdrv->base.API.GetProcAddress = egl_g3d_get_proc_address;
1155
1156 gdrv->base.API.BindTexImage = egl_g3d_bind_tex_image;
1157 gdrv->base.API.ReleaseTexImage = egl_g3d_release_tex_image;
1158
1159 gdrv->base.API.CreateImageKHR = egl_g3d_create_image;
1160 gdrv->base.API.DestroyImageKHR = egl_g3d_destroy_image;
1161
1162 #ifdef EGL_MESA_screen_surface
1163 gdrv->base.API.CreateScreenSurfaceMESA = egl_g3d_create_screen_surface;
1164 gdrv->base.API.ShowScreenSurfaceMESA = egl_g3d_show_screen_surface;
1165 #endif
1166
1167 gdrv->base.Name = driver_name;
1168 gdrv->base.Probe = egl_g3d_probe;
1169 gdrv->base.Unload = egl_g3d_unload;
1170
1171 /* the key is " EGL G3D" */
1172 gdrv->probe_key = 0x0E61063D;
1173
1174 return &gdrv->base;
1175 }