2 * Mesa 3-D graphics library
5 * Copyright (C) 2009-2010 Chia-I Wu <olv@0xlab.org>
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
29 #include "pipe/p_screen.h"
30 #include "util/u_memory.h"
31 #include "util/u_rect.h"
32 #include "util/u_inlines.h"
33 #include "egldriver.h"
34 #include "eglcurrent.h"
35 #include "eglconfigutil.h"
40 #include "egl_g3d_st.h"
41 #include "egl_g3d_image.h"
44 * Return the state tracker for the given context.
46 static struct st_api *
47 egl_g3d_choose_st(_EGLDriver *drv, _EGLContext *ctx)
49 struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
53 switch (ctx->ClientAPI) {
54 case EGL_OPENGL_ES_API:
55 switch (ctx->ClientVersion) {
57 idx = ST_API_OPENGL_ES1;
60 idx = ST_API_OPENGL_ES2;
63 _eglLog(_EGL_WARNING, "unknown client version %d",
75 _eglLog(_EGL_WARNING, "unknown client API 0x%04x", ctx->ClientAPI);
79 stapi = (idx >= 0) ? gdrv->stapis[idx] : NULL;
84 * Initialize the state trackers.
87 egl_g3d_init_st(_EGLDriver *drv)
89 struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
92 /* already initialized */
96 for (i = 0; i < ST_API_COUNT; i++) {
97 gdrv->stapis[i] = egl_g3d_create_st_api(i);
99 gdrv->api_mask |= egl_g3d_st_api_bit(i);
103 _eglLog(_EGL_DEBUG, "Driver API mask: 0x%x", gdrv->api_mask);
105 _eglLog(_EGL_WARNING, "No supported client API");
109 * Get the probe object of the display.
111 * Note that this function may be called before the display is initialized.
113 static struct native_probe *
114 egl_g3d_get_probe(_EGLDriver *drv, _EGLDisplay *dpy)
116 struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
117 struct native_probe *nprobe;
119 nprobe = (struct native_probe *) _eglGetProbeCache(gdrv->probe_key);
120 if (!nprobe || nprobe->display != dpy->NativeDisplay) {
122 nprobe->destroy(nprobe);
123 nprobe = native_create_probe(dpy->NativeDisplay);
124 _eglSetProbeCache(gdrv->probe_key, (void *) nprobe);
131 * Destroy the probe object of the display. The display may be NULL.
133 * Note that this function may be called before the display is initialized.
136 egl_g3d_destroy_probe(_EGLDriver *drv, _EGLDisplay *dpy)
138 struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
139 struct native_probe *nprobe;
141 nprobe = (struct native_probe *) _eglGetProbeCache(gdrv->probe_key);
142 if (nprobe && (!dpy || nprobe->display == dpy->NativeDisplay)) {
143 nprobe->destroy(nprobe);
144 _eglSetProbeCache(gdrv->probe_key, NULL);
148 #ifdef EGL_MESA_screen_surface
151 egl_g3d_add_screens(_EGLDriver *drv, _EGLDisplay *dpy)
153 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
154 const struct native_connector **native_connectors;
155 EGLint num_connectors, i;
158 gdpy->native->modeset->get_connectors(gdpy->native, &num_connectors, NULL);
159 if (!num_connectors) {
160 if (native_connectors)
161 free(native_connectors);
165 for (i = 0; i < num_connectors; i++) {
166 const struct native_connector *nconn = native_connectors[i];
167 struct egl_g3d_screen *gscr;
168 const struct native_mode **native_modes;
171 /* TODO support for hotplug */
173 gdpy->native->modeset->get_modes(gdpy->native, nconn, &num_modes);
180 gscr = CALLOC_STRUCT(egl_g3d_screen);
186 _eglInitScreen(&gscr->base);
188 for (j = 0; j < num_modes; j++) {
189 const struct native_mode *nmode = native_modes[j];
192 mode = _eglAddNewMode(&gscr->base, nmode->width, nmode->height,
193 nmode->refresh_rate, nmode->desc);
196 /* gscr->native_modes and gscr->base.Modes should be consistent */
197 assert(mode == &gscr->base.Modes[j]);
200 gscr->native = nconn;
201 gscr->native_modes = native_modes;
203 _eglAddScreen(dpy, &gscr->base);
206 free(native_connectors);
209 #endif /* EGL_MESA_screen_surface */
212 * Initialize an EGL config from the native config.
215 egl_g3d_init_config(_EGLDriver *drv, _EGLDisplay *dpy,
216 _EGLConfig *conf, const struct native_config *nconf)
218 struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
219 struct egl_g3d_config *gconf = egl_g3d_config(conf);
220 const __GLcontextModes *mode = &nconf->mode;
221 EGLint buffer_mask, api_mask;
225 buffer_mask = ST_ATTACHMENT_FRONT_LEFT_MASK;
226 if (mode->doubleBufferMode)
227 buffer_mask |= ST_ATTACHMENT_BACK_LEFT_MASK;
228 if (mode->stereoMode) {
229 buffer_mask |= ST_ATTACHMENT_FRONT_RIGHT_MASK;
230 if (mode->doubleBufferMode)
231 buffer_mask |= ST_ATTACHMENT_BACK_RIGHT_MASK;
234 gconf->stvis.buffer_mask = buffer_mask;
235 gconf->stvis.color_format = nconf->color_format;
236 gconf->stvis.depth_stencil_format = nconf->depth_format;
237 gconf->stvis.accum_format = PIPE_FORMAT_NONE;
238 gconf->stvis.samples = 0;
240 gconf->stvis.render_buffer = (buffer_mask & ST_ATTACHMENT_BACK_LEFT_MASK) ?
241 ST_ATTACHMENT_BACK_LEFT : ST_ATTACHMENT_FRONT_LEFT;
244 for (i = 0; i < ST_API_COUNT; i++) {
245 struct st_api *stapi = gdrv->stapis[i];
247 if (stapi->is_visual_supported(stapi, &gconf->stvis))
248 api_mask |= egl_g3d_st_api_bit(i);
251 /* this is required by EGL, not by OpenGL ES */
252 if ((mode->drawableType & GLX_WINDOW_BIT) && !mode->doubleBufferMode)
253 api_mask &= ~(EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT);
256 _eglLog(_EGL_DEBUG, "no state tracker supports config 0x%x",
260 valid = _eglConfigFromContextModesRec(&gconf->base,
261 mode, api_mask, api_mask);
263 #ifdef EGL_MESA_screen_surface
264 /* check if scanout surface bit is set */
265 if (nconf->scanout_bit) {
266 EGLint val = GET_CONFIG_ATTRIB(&gconf->base, EGL_SURFACE_TYPE);
267 val |= EGL_SCREEN_BIT_MESA;
268 SET_CONFIG_ATTRIB(&gconf->base, EGL_SURFACE_TYPE, val);
271 valid = _eglValidateConfig(&gconf->base, EGL_FALSE);
274 _eglLog(_EGL_DEBUG, "skip invalid config 0x%x", mode->visualID);
278 gconf->native = nconf;
284 * Add configs to display and return the next config ID.
287 egl_g3d_add_configs(_EGLDriver *drv, _EGLDisplay *dpy, EGLint id)
289 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
290 const struct native_config **native_configs;
293 native_configs = gdpy->native->get_configs(gdpy->native, &num_configs);
296 free(native_configs);
300 for (i = 0; i < num_configs; i++) {
301 struct egl_g3d_config *gconf;
303 gconf = CALLOC_STRUCT(egl_g3d_config);
305 _eglInitConfig(&gconf->base, dpy, id);
306 if (!egl_g3d_init_config(drv, dpy, &gconf->base, native_configs[i])) {
311 _eglAddConfig(dpy, &gconf->base);
316 free(native_configs);
321 egl_g3d_invalid_surface(struct native_display *ndpy,
322 struct native_surface *nsurf,
323 unsigned int seq_num)
325 /* XXX not thread safe? */
326 struct egl_g3d_surface *gsurf = egl_g3d_surface(nsurf->user_data);
327 struct egl_g3d_context *gctx;
330 * Some functions such as egl_g3d_copy_buffers create a temporary native
331 * surface. There is no gsurf associated with it.
333 gctx = (gsurf) ? egl_g3d_context(gsurf->base.CurrentContext) : NULL;
335 gctx->stctxi->notify_invalid_framebuffer(gctx->stctxi, gsurf->stfbi);
338 static struct native_event_handler egl_g3d_native_event_handler = {
339 .invalid_surface = egl_g3d_invalid_surface
343 egl_g3d_terminate(_EGLDriver *drv, _EGLDisplay *dpy)
345 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
348 _eglReleaseDisplayResources(drv, dpy);
349 _eglCleanupDisplay(dpy);
352 gdpy->pipe->destroy(gdpy->pipe);
355 for (i = 0; i < dpy->NumScreens; i++) {
356 struct egl_g3d_screen *gscr = egl_g3d_screen(dpy->Screens[i]);
357 free(gscr->native_modes);
364 egl_g3d_destroy_st_manager(gdpy->smapi);
367 gdpy->native->destroy(gdpy->native);
370 dpy->DriverData = NULL;
376 egl_g3d_initialize(_EGLDriver *drv, _EGLDisplay *dpy,
377 EGLint *major, EGLint *minor)
379 struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
380 struct egl_g3d_display *gdpy;
382 /* the probe object is unlikely to be needed again */
383 egl_g3d_destroy_probe(drv, dpy);
385 gdpy = CALLOC_STRUCT(egl_g3d_display);
387 _eglError(EGL_BAD_ALLOC, "eglInitialize");
390 dpy->DriverData = gdpy;
392 gdpy->native = native_create_display(dpy->NativeDisplay,
393 &egl_g3d_native_event_handler);
395 _eglError(EGL_NOT_INITIALIZED, "eglInitialize(no usable display)");
399 gdpy->native->user_data = (void *) dpy;
401 egl_g3d_init_st(&gdrv->base);
402 dpy->ClientAPIsMask = gdrv->api_mask;
404 gdpy->smapi = egl_g3d_create_st_manager(dpy);
406 _eglError(EGL_NOT_INITIALIZED,
407 "eglInitialize(failed to create st manager)");
411 #ifdef EGL_MESA_screen_surface
412 /* enable MESA_screen_surface before adding (and validating) configs */
413 if (gdpy->native->modeset) {
414 dpy->Extensions.MESA_screen_surface = EGL_TRUE;
415 egl_g3d_add_screens(drv, dpy);
419 dpy->Extensions.KHR_image_base = EGL_TRUE;
420 if (gdpy->native->get_param(gdpy->native, NATIVE_PARAM_USE_NATIVE_BUFFER))
421 dpy->Extensions.KHR_image_pixmap = EGL_TRUE;
423 if (egl_g3d_add_configs(drv, dpy, 1) == 1) {
424 _eglError(EGL_NOT_INITIALIZED, "eglInitialize(unable to add configs)");
435 egl_g3d_terminate(drv, dpy);
440 egl_g3d_create_context(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf,
441 _EGLContext *share, const EGLint *attribs)
443 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
444 struct egl_g3d_context *gshare = egl_g3d_context(share);
445 struct egl_g3d_config *gconf = egl_g3d_config(conf);
446 struct egl_g3d_context *gctx;
448 gctx = CALLOC_STRUCT(egl_g3d_context);
450 _eglError(EGL_BAD_ALLOC, "eglCreateContext");
454 if (!_eglInitContext(&gctx->base, dpy, conf, attribs)) {
459 gctx->stapi = egl_g3d_choose_st(drv, &gctx->base);
465 gctx->stctxi = gctx->stapi->create_context(gctx->stapi, gdpy->smapi,
466 &gconf->stvis, (gshare) ? gshare->stctxi : NULL);
472 gctx->stctxi->st_manager_private = (void *) &gctx->base;
481 destroy_context(_EGLDisplay *dpy, _EGLContext *ctx)
483 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
485 /* FIXME a context might live longer than its display */
486 if (!dpy->Initialized)
487 _eglLog(_EGL_FATAL, "destroy a context with an unitialized display");
489 gctx->stctxi->destroy(gctx->stctxi);
495 egl_g3d_destroy_context(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx)
497 if (!_eglIsContextBound(ctx))
498 destroy_context(dpy, ctx);
502 struct egl_g3d_create_surface_arg {
505 EGLNativeWindowType win;
506 EGLNativePixmapType pix;
511 egl_g3d_create_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf,
512 struct egl_g3d_create_surface_arg *arg,
513 const EGLint *attribs)
515 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
516 struct egl_g3d_config *gconf = egl_g3d_config(conf);
517 struct egl_g3d_surface *gsurf;
518 struct native_surface *nsurf;
523 err = "eglCreateWindowSurface";
526 err = "eglCreatePixmapSurface";
528 #ifdef EGL_MESA_screen_surface
529 case EGL_SCREEN_BIT_MESA:
530 err = "eglCreateScreenSurface";
534 err = "eglCreateUnknownSurface";
538 gsurf = CALLOC_STRUCT(egl_g3d_surface);
540 _eglError(EGL_BAD_ALLOC, err);
544 if (!_eglInitSurface(&gsurf->base, dpy, arg->type, conf, attribs)) {
549 /* create the native surface */
552 nsurf = gdpy->native->create_window_surface(gdpy->native,
553 arg->u.win, gconf->native);
556 nsurf = gdpy->native->create_pixmap_surface(gdpy->native,
557 arg->u.pix, gconf->native);
559 #ifdef EGL_MESA_screen_surface
560 case EGL_SCREEN_BIT_MESA:
561 /* prefer back buffer (move to _eglInitSurface?) */
562 gsurf->base.RenderBuffer = EGL_BACK_BUFFER;
563 nsurf = gdpy->native->modeset->create_scanout_surface(gdpy->native,
564 gconf->native, gsurf->base.Width, gsurf->base.Height);
576 /* initialize the geometry */
577 if (!nsurf->validate(nsurf, 0x0, &gsurf->sequence_number, NULL,
578 &gsurf->base.Width, &gsurf->base.Height)) {
579 nsurf->destroy(nsurf);
584 gsurf->stvis = gconf->stvis;
585 if (gsurf->base.RenderBuffer == EGL_SINGLE_BUFFER)
586 gsurf->stvis.render_buffer = ST_ATTACHMENT_FRONT_LEFT;
588 gsurf->stfbi = egl_g3d_create_st_framebuffer(&gsurf->base);
590 nsurf->destroy(nsurf);
595 nsurf->user_data = &gsurf->base;
596 gsurf->native = nsurf;
602 egl_g3d_create_window_surface(_EGLDriver *drv, _EGLDisplay *dpy,
603 _EGLConfig *conf, EGLNativeWindowType win,
604 const EGLint *attribs)
606 struct egl_g3d_create_surface_arg arg;
608 memset(&arg, 0, sizeof(arg));
609 arg.type = EGL_WINDOW_BIT;
612 return egl_g3d_create_surface(drv, dpy, conf, &arg, attribs);
616 egl_g3d_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *dpy,
617 _EGLConfig *conf, EGLNativePixmapType pix,
618 const EGLint *attribs)
620 struct egl_g3d_create_surface_arg arg;
622 memset(&arg, 0, sizeof(arg));
623 arg.type = EGL_PIXMAP_BIT;
626 return egl_g3d_create_surface(drv, dpy, conf, &arg, attribs);
630 egl_g3d_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *dpy,
631 _EGLConfig *conf, const EGLint *attribs)
633 struct egl_g3d_config *gconf = egl_g3d_config(conf);
634 struct egl_g3d_surface *gsurf;
636 gsurf = CALLOC_STRUCT(egl_g3d_surface);
638 _eglError(EGL_BAD_ALLOC, "eglCreatePbufferSurface");
642 if (!_eglInitSurface(&gsurf->base, dpy, EGL_PBUFFER_BIT, conf, attribs)) {
647 gsurf->stvis = gconf->stvis;
649 gsurf->stfbi = egl_g3d_create_st_framebuffer(&gsurf->base);
662 destroy_surface(_EGLDisplay *dpy, _EGLSurface *surf)
664 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
666 /* FIXME a surface might live longer than its display */
667 if (!dpy->Initialized)
668 _eglLog(_EGL_FATAL, "destroy a surface with an unitialized display");
670 pipe_texture_reference(&gsurf->render_texture, NULL);
671 egl_g3d_destroy_st_framebuffer(gsurf->stfbi);
673 gsurf->native->destroy(gsurf->native);
678 egl_g3d_destroy_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
680 if (!_eglIsSurfaceBound(surf))
681 destroy_surface(dpy, surf);
686 egl_g3d_make_current(_EGLDriver *drv, _EGLDisplay *dpy,
687 _EGLSurface *draw, _EGLSurface *read, _EGLContext *ctx)
689 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
690 struct egl_g3d_surface *gdraw = egl_g3d_surface(draw);
691 struct egl_g3d_surface *gread = egl_g3d_surface(read);
692 struct egl_g3d_context *old_gctx;
693 EGLBoolean ok = EGL_TRUE;
695 /* bind the new context and return the "orphaned" one */
696 if (!_eglBindContext(&ctx, &draw, &read))
698 old_gctx = egl_g3d_context(ctx);
701 /* flush old context */
702 old_gctx->stctxi->flush(old_gctx->stctxi,
703 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
707 ok = gctx->stapi->make_current(gctx->stapi, gctx->stctxi,
708 (gdraw) ? gdraw->stfbi : NULL, (gread) ? gread->stfbi : NULL);
710 gctx->stctxi->notify_invalid_framebuffer(gctx->stctxi, gdraw->stfbi);
711 if (gread != gdraw) {
712 gctx->stctxi->notify_invalid_framebuffer(gctx->stctxi,
716 if (gdraw->base.Type == EGL_WINDOW_BIT) {
717 gctx->base.WindowRenderBuffer =
718 (gdraw->stvis.render_buffer == ST_ATTACHMENT_FRONT_LEFT) ?
719 EGL_SINGLE_BUFFER : EGL_BACK_BUFFER;
724 ok = old_gctx->stapi->make_current(old_gctx->stapi, NULL, NULL, NULL);
725 old_gctx->base.WindowRenderBuffer = EGL_NONE;
728 if (ctx && !_eglIsContextLinked(ctx))
729 destroy_context(dpy, ctx);
730 if (draw && !_eglIsSurfaceLinked(draw))
731 destroy_surface(dpy, draw);
732 if (read && read != draw && !_eglIsSurfaceLinked(read))
733 destroy_surface(dpy, read);
739 egl_g3d_swap_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
741 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
742 _EGLContext *ctx = _eglGetCurrentContext();
743 struct egl_g3d_context *gctx = NULL;
745 /* no-op for pixmap or pbuffer surface */
746 if (gsurf->base.Type == EGL_PIXMAP_BIT ||
747 gsurf->base.Type == EGL_PBUFFER_BIT)
750 /* or when the surface is single-buffered */
751 if (gsurf->stvis.render_buffer == ST_ATTACHMENT_FRONT_LEFT)
754 if (ctx && ctx->DrawSurface == surf)
755 gctx = egl_g3d_context(ctx);
757 /* flush if the surface is current */
759 gctx->stctxi->flush(gctx->stctxi,
760 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
763 return gsurf->native->swap_buffers(gsurf->native);
767 * Find a config that supports the pixmap.
770 egl_g3d_find_pixmap_config(_EGLDisplay *dpy, EGLNativePixmapType pix)
772 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
773 struct egl_g3d_config *gconf;
776 for (i = 0; i < dpy->NumConfigs; i++) {
777 gconf = egl_g3d_config(dpy->Configs[i]);
778 if (gdpy->native->is_pixmap_supported(gdpy->native, pix, gconf->native))
782 return (i < dpy->NumConfigs) ? &gconf->base : NULL;
786 * Get the pipe surface of the given attachment of the native surface.
788 static struct pipe_surface *
789 get_pipe_surface(struct native_display *ndpy, struct native_surface *nsurf,
790 enum native_attachment natt)
792 struct pipe_texture *textures[NUM_NATIVE_ATTACHMENTS];
793 struct pipe_surface *psurf;
795 textures[natt] = NULL;
796 nsurf->validate(nsurf, 1 << natt, NULL, textures, NULL, NULL);
800 psurf = ndpy->screen->get_tex_surface(ndpy->screen, textures[natt],
801 0, 0, 0, PIPE_BUFFER_USAGE_GPU_WRITE);
802 pipe_texture_reference(&textures[natt], NULL);
808 egl_g3d_copy_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
809 EGLNativePixmapType target)
811 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
812 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
813 _EGLContext *ctx = _eglGetCurrentContext();
814 struct egl_g3d_config *gconf;
815 struct native_surface *nsurf;
816 struct pipe_screen *screen = gdpy->native->screen;
817 struct pipe_surface *psurf;
819 if (!gsurf->render_texture)
822 gconf = egl_g3d_config(egl_g3d_find_pixmap_config(dpy, target));
824 return _eglError(EGL_BAD_NATIVE_PIXMAP, "eglCopyBuffers");
826 nsurf = gdpy->native->create_pixmap_surface(gdpy->native,
827 target, gconf->native);
829 return _eglError(EGL_BAD_NATIVE_PIXMAP, "eglCopyBuffers");
831 /* flush if the surface is current */
832 if (ctx && ctx->DrawSurface == &gsurf->base) {
833 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
834 gctx->stctxi->flush(gctx->stctxi,
835 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
838 /* create a pipe context to copy surfaces */
841 gdpy->native->screen->context_create(gdpy->native->screen, NULL);
846 psurf = get_pipe_surface(gdpy->native, nsurf, NATIVE_ATTACHMENT_FRONT_LEFT);
848 struct pipe_surface *psrc;
850 psrc = screen->get_tex_surface(screen, gsurf->render_texture,
851 0, 0, 0, PIPE_BUFFER_USAGE_GPU_READ);
853 gdpy->pipe->surface_copy(gdpy->pipe, psurf, 0, 0,
854 psrc, 0, 0, psurf->width, psurf->height);
855 pipe_surface_reference(&psrc, NULL);
857 nsurf->flush_frontbuffer(nsurf);
860 pipe_surface_reference(&psurf, NULL);
863 nsurf->destroy(nsurf);
869 egl_g3d_wait_client(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx)
871 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
872 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
873 struct pipe_screen *screen = gdpy->native->screen;
874 struct pipe_fence_handle *fence = NULL;
876 gctx->stctxi->flush(gctx->stctxi,
877 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, &fence);
878 screen->fence_finish(screen, fence, 0);
879 screen->fence_reference(screen, &fence, NULL);
885 egl_g3d_wait_native(_EGLDriver *drv, _EGLDisplay *dpy, EGLint engine)
887 _EGLContext *ctx = _eglGetCurrentContext();
889 if (engine != EGL_CORE_NATIVE_ENGINE)
890 return _eglError(EGL_BAD_PARAMETER, "eglWaitNative");
892 if (ctx && ctx->DrawSurface) {
893 struct egl_g3d_surface *gsurf = egl_g3d_surface(ctx->DrawSurface);
896 gsurf->native->wait(gsurf->native);
903 egl_g3d_get_proc_address(_EGLDriver *drv, const char *procname)
905 struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
909 /* in case this is called before a display is initialized */
910 egl_g3d_init_st(&gdrv->base);
912 for (i = 0; i < ST_API_COUNT; i++) {
913 struct st_api *stapi = gdrv->stapis[i];
915 proc = (_EGLProc) stapi->get_proc_address(stapi, procname);
921 return (_EGLProc) NULL;
925 egl_g3d_bind_tex_image(_EGLDriver *drv, _EGLDisplay *dpy,
926 _EGLSurface *surf, EGLint buffer)
928 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
929 _EGLContext *es1 = _eglGetAPIContext(EGL_OPENGL_ES_API);
930 struct egl_g3d_context *gctx;
931 enum pipe_format internal_format;
932 enum st_texture_type target;
934 if (!gsurf || gsurf->base.Type != EGL_PBUFFER_BIT)
935 return _eglError(EGL_BAD_SURFACE, "eglBindTexImage");
936 if (buffer != EGL_BACK_BUFFER)
937 return _eglError(EGL_BAD_PARAMETER, "eglBindTexImage");
938 if (gsurf->base.BoundToTexture)
939 return _eglError(EGL_BAD_ACCESS, "eglBindTexImage");
941 switch (gsurf->base.TextureFormat) {
942 case EGL_TEXTURE_RGB:
943 internal_format = PIPE_FORMAT_R8G8B8_UNORM;
945 case EGL_TEXTURE_RGBA:
946 internal_format = PIPE_FORMAT_B8G8R8A8_UNORM;
949 return _eglError(EGL_BAD_MATCH, "eglBindTexImage");
952 switch (gsurf->base.TextureTarget) {
954 target = ST_TEXTURE_2D;
957 return _eglError(EGL_BAD_MATCH, "eglBindTexImage");
962 if (!gsurf->render_texture)
965 /* flush properly if the surface is bound */
966 if (gsurf->base.CurrentContext) {
967 gctx = egl_g3d_context(gsurf->base.CurrentContext);
968 gctx->stctxi->flush(gctx->stctxi,
969 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
972 gctx = egl_g3d_context(es1);
973 if (gctx->stctxi->teximage) {
974 if (!gctx->stctxi->teximage(gctx->stctxi, target,
975 gsurf->base.MipmapLevel, internal_format,
976 gsurf->render_texture, gsurf->base.MipmapTexture))
978 gsurf->base.BoundToTexture = EGL_TRUE;
985 egl_g3d_release_tex_image(_EGLDriver *drv, _EGLDisplay *dpy,
986 _EGLSurface *surf, EGLint buffer)
988 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
990 if (!gsurf || gsurf->base.Type != EGL_PBUFFER_BIT ||
991 !gsurf->base.BoundToTexture)
992 return _eglError(EGL_BAD_SURFACE, "eglReleaseTexImage");
993 if (buffer != EGL_BACK_BUFFER)
994 return _eglError(EGL_BAD_PARAMETER, "eglReleaseTexImage");
996 if (gsurf->render_texture) {
997 _EGLContext *ctx = _eglGetAPIContext(EGL_OPENGL_ES_API);
998 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
1000 /* what if the context the surface binds to is no longer current? */
1002 gctx->stctxi->teximage(gctx->stctxi, ST_TEXTURE_2D,
1003 gsurf->base.MipmapLevel, PIPE_FORMAT_NONE, NULL, FALSE);
1007 gsurf->base.BoundToTexture = EGL_FALSE;
1012 #ifdef EGL_MESA_screen_surface
1014 static _EGLSurface *
1015 egl_g3d_create_screen_surface(_EGLDriver *drv, _EGLDisplay *dpy,
1016 _EGLConfig *conf, const EGLint *attribs)
1018 struct egl_g3d_create_surface_arg arg;
1020 memset(&arg, 0, sizeof(arg));
1021 arg.type = EGL_SCREEN_BIT_MESA;
1023 return egl_g3d_create_surface(drv, dpy, conf, &arg, attribs);
1027 egl_g3d_show_screen_surface(_EGLDriver *drv, _EGLDisplay *dpy,
1028 _EGLScreen *scr, _EGLSurface *surf,
1031 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
1032 struct egl_g3d_screen *gscr = egl_g3d_screen(scr);
1033 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
1034 struct native_surface *nsurf;
1035 const struct native_mode *nmode;
1042 return _eglError(EGL_BAD_MATCH, "eglShowSurfaceMESA");
1043 if (gsurf->base.Type != EGL_SCREEN_BIT_MESA)
1044 return _eglError(EGL_BAD_SURFACE, "eglShowScreenSurfaceMESA");
1045 if (gsurf->base.Width < mode->Width || gsurf->base.Height < mode->Height)
1046 return _eglError(EGL_BAD_MATCH,
1047 "eglShowSurfaceMESA(surface smaller than mode size)");
1049 /* find the index of the mode */
1050 for (idx = 0; idx < gscr->base.NumModes; idx++)
1051 if (mode == &gscr->base.Modes[idx])
1053 if (idx >= gscr->base.NumModes) {
1054 return _eglError(EGL_BAD_MODE_MESA,
1055 "eglShowSurfaceMESA(unknown mode)");
1058 nsurf = gsurf->native;
1059 nmode = gscr->native_modes[idx];
1063 return _eglError(EGL_BAD_MATCH, "eglShowSurfaceMESA");
1065 /* disable the screen */
1070 /* TODO surface panning by CRTC choosing */
1071 changed = gdpy->native->modeset->program(gdpy->native, 0, nsurf,
1072 gscr->base.OriginX, gscr->base.OriginY, &gscr->native, 1, nmode);
1074 gscr->base.CurrentSurface = &gsurf->base;
1075 gscr->base.CurrentMode = mode;
1081 #endif /* EGL_MESA_screen_surface */
1084 egl_g3d_probe(_EGLDriver *drv, _EGLDisplay *dpy)
1086 struct native_probe *nprobe;
1087 enum native_probe_result res;
1090 nprobe = egl_g3d_get_probe(drv, dpy);
1091 res = native_get_probe_result(nprobe);
1094 case NATIVE_PROBE_UNKNOWN:
1098 case NATIVE_PROBE_FALLBACK:
1101 case NATIVE_PROBE_SUPPORTED:
1104 case NATIVE_PROBE_EXACT:
1113 egl_g3d_unload(_EGLDriver *drv)
1115 struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
1118 for (i = 0; i < ST_API_COUNT; i++) {
1119 if (gdrv->stapis[i])
1120 gdrv->stapis[i]->destroy(gdrv->stapis[i]);
1123 egl_g3d_destroy_probe(drv, NULL);
1128 _eglMain(const char *args)
1130 static char driver_name[64];
1131 struct egl_g3d_driver *gdrv;
1133 snprintf(driver_name, sizeof(driver_name),
1134 "Gallium/%s", native_get_name());
1136 gdrv = CALLOC_STRUCT(egl_g3d_driver);
1140 _eglInitDriverFallbacks(&gdrv->base);
1142 gdrv->base.API.Initialize = egl_g3d_initialize;
1143 gdrv->base.API.Terminate = egl_g3d_terminate;
1144 gdrv->base.API.CreateContext = egl_g3d_create_context;
1145 gdrv->base.API.DestroyContext = egl_g3d_destroy_context;
1146 gdrv->base.API.CreateWindowSurface = egl_g3d_create_window_surface;
1147 gdrv->base.API.CreatePixmapSurface = egl_g3d_create_pixmap_surface;
1148 gdrv->base.API.CreatePbufferSurface = egl_g3d_create_pbuffer_surface;
1149 gdrv->base.API.DestroySurface = egl_g3d_destroy_surface;
1150 gdrv->base.API.MakeCurrent = egl_g3d_make_current;
1151 gdrv->base.API.SwapBuffers = egl_g3d_swap_buffers;
1152 gdrv->base.API.CopyBuffers = egl_g3d_copy_buffers;
1153 gdrv->base.API.WaitClient = egl_g3d_wait_client;
1154 gdrv->base.API.WaitNative = egl_g3d_wait_native;
1155 gdrv->base.API.GetProcAddress = egl_g3d_get_proc_address;
1157 gdrv->base.API.BindTexImage = egl_g3d_bind_tex_image;
1158 gdrv->base.API.ReleaseTexImage = egl_g3d_release_tex_image;
1160 gdrv->base.API.CreateImageKHR = egl_g3d_create_image;
1161 gdrv->base.API.DestroyImageKHR = egl_g3d_destroy_image;
1163 #ifdef EGL_MESA_screen_surface
1164 gdrv->base.API.CreateScreenSurfaceMESA = egl_g3d_create_screen_surface;
1165 gdrv->base.API.ShowScreenSurfaceMESA = egl_g3d_show_screen_surface;
1168 gdrv->base.Name = driver_name;
1169 gdrv->base.Probe = egl_g3d_probe;
1170 gdrv->base.Unload = egl_g3d_unload;
1172 /* the key is " EGL G3D" */
1173 gdrv->probe_key = 0x0E61063D;