2 * Mesa 3-D graphics library
5 * Copyright (C) 2010 LunarG Inc.
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.
26 * Chia-I Wu <olv@lunarg.com>
29 #include "util/u_memory.h"
30 #include "util/u_inlines.h"
31 #include "util/u_dl.h"
36 #include "egl_g3d_st.h"
38 struct egl_g3d_st_manager {
39 struct st_manager base;
43 static INLINE struct egl_g3d_st_manager *
44 egl_g3d_st_manager(struct st_manager *smapi)
46 return (struct egl_g3d_st_manager *) smapi;
50 egl_g3d_create_st_api(enum st_api_type api)
52 const char *stmod_name;
53 struct util_dl_library *lib;
54 const struct st_module *mod;
58 stmod_name = ST_MODULE_OPENGL_SYMBOL;
60 case ST_API_OPENGL_ES1:
61 stmod_name = ST_MODULE_OPENGL_ES1_SYMBOL;
63 case ST_API_OPENGL_ES2:
64 stmod_name = ST_MODULE_OPENGL_ES2_SYMBOL;
67 stmod_name = ST_MODULE_OPENVG_SYMBOL;
77 lib = util_dl_open(NULL);
79 mod = (const struct st_module *)
80 util_dl_get_proc_address(lib, stmod_name);
83 if (!mod || mod->api != api)
86 return mod->create_api();
90 egl_g3d_st_manager_get_egl_image(struct st_manager *smapi,
91 struct st_egl_image *stimg)
93 struct egl_g3d_st_manager *gsmapi = egl_g3d_st_manager(smapi);
94 EGLImageKHR handle = (EGLImageKHR) stimg->egl_image;
96 struct egl_g3d_image *gimg;
98 /* this is called from state trackers */
99 _eglLockMutex(&gsmapi->display->Mutex);
101 img = _eglLookupImage(handle, gsmapi->display);
103 _eglUnlockMutex(&gsmapi->display->Mutex);
107 gimg = egl_g3d_image(img);
109 stimg->texture = NULL;
110 pipe_texture_reference(&stimg->texture, gimg->texture);
111 stimg->face = gimg->face;
112 stimg->level = gimg->level;
113 stimg->zslice = gimg->zslice;
115 _eglUnlockMutex(&gsmapi->display->Mutex);
121 egl_g3d_create_st_manager(_EGLDisplay *dpy)
123 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
124 struct egl_g3d_st_manager *gsmapi;
126 gsmapi = CALLOC_STRUCT(egl_g3d_st_manager);
128 gsmapi->display = dpy;
130 gsmapi->base.screen = gdpy->native->screen;
131 gsmapi->base.get_egl_image = egl_g3d_st_manager_get_egl_image;
134 return &gsmapi->base;;
138 egl_g3d_destroy_st_manager(struct st_manager *smapi)
140 struct egl_g3d_st_manager *gsmapi = egl_g3d_st_manager(smapi);
145 egl_g3d_st_framebuffer_flush_front_pbuffer(struct st_framebuffer_iface *stfbi,
146 enum st_attachment_type statt)
152 egl_g3d_st_framebuffer_validate_pbuffer(struct st_framebuffer_iface *stfbi,
153 const enum st_attachment_type *statts,
155 struct pipe_texture **out)
157 _EGLSurface *surf = (_EGLSurface *) stfbi->st_manager_private;
158 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
159 struct pipe_texture templ;
162 for (i = 0; i < count; i++) {
165 if (gsurf->stvis.render_buffer != statts[i])
168 if (!gsurf->render_texture) {
169 struct egl_g3d_display *gdpy =
170 egl_g3d_display(gsurf->base.Resource.Display);
171 struct pipe_screen *screen = gdpy->native->screen;
173 memset(&templ, 0, sizeof(templ));
174 templ.target = PIPE_TEXTURE_2D;
175 templ.last_level = 0;
176 templ.width0 = gsurf->base.Width;
177 templ.height0 = gsurf->base.Height;
179 templ.format = gsurf->stvis.color_format;
180 templ.tex_usage = PIPE_TEXTURE_USAGE_RENDER_TARGET;
182 gsurf->render_texture = screen->texture_create(screen, &templ);
185 pipe_texture_reference(&out[i], gsurf->render_texture);
192 egl_g3d_st_framebuffer_flush_front(struct st_framebuffer_iface *stfbi,
193 enum st_attachment_type statt)
195 _EGLSurface *surf = (_EGLSurface *) stfbi->st_manager_private;
196 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
198 return gsurf->native->flush_frontbuffer(gsurf->native);
202 egl_g3d_st_framebuffer_validate(struct st_framebuffer_iface *stfbi,
203 const enum st_attachment_type *statts,
205 struct pipe_texture **out)
207 _EGLSurface *surf = (_EGLSurface *) stfbi->st_manager_private;
208 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
209 struct pipe_texture *textures[NUM_NATIVE_ATTACHMENTS];
210 uint attachment_mask = 0;
213 for (i = 0; i < count; i++) {
217 case ST_ATTACHMENT_FRONT_LEFT:
218 natt = NATIVE_ATTACHMENT_FRONT_LEFT;
220 case ST_ATTACHMENT_BACK_LEFT:
221 natt = NATIVE_ATTACHMENT_BACK_LEFT;
223 case ST_ATTACHMENT_FRONT_RIGHT:
224 natt = NATIVE_ATTACHMENT_FRONT_RIGHT;
226 case ST_ATTACHMENT_BACK_RIGHT:
227 natt = NATIVE_ATTACHMENT_BACK_RIGHT;
235 attachment_mask |= 1 << natt;
238 if (!gsurf->native->validate(gsurf->native, attachment_mask,
239 &gsurf->sequence_number, textures, &gsurf->base.Width,
240 &gsurf->base.Height))
243 for (i = 0; i < count; i++) {
244 struct pipe_texture *tex;
248 case ST_ATTACHMENT_FRONT_LEFT:
249 natt = NATIVE_ATTACHMENT_FRONT_LEFT;
251 case ST_ATTACHMENT_BACK_LEFT:
252 natt = NATIVE_ATTACHMENT_BACK_LEFT;
254 case ST_ATTACHMENT_FRONT_RIGHT:
255 natt = NATIVE_ATTACHMENT_FRONT_RIGHT;
257 case ST_ATTACHMENT_BACK_RIGHT:
258 natt = NATIVE_ATTACHMENT_BACK_RIGHT;
266 tex = textures[natt];
268 if (statts[i] == stfbi->visual->render_buffer)
269 pipe_texture_reference(&gsurf->render_texture, tex);
271 if (attachment_mask & (1 << natt)) {
272 /* transfer the ownership to the caller */
274 attachment_mask &= ~(1 << natt);
277 /* the attachment is listed more than once */
278 pipe_texture_reference(&out[i], tex);
286 struct st_framebuffer_iface *
287 egl_g3d_create_st_framebuffer(_EGLSurface *surf)
289 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
290 struct st_framebuffer_iface *stfbi;
292 stfbi = CALLOC_STRUCT(st_framebuffer_iface);
296 stfbi->visual = &gsurf->stvis;
297 if (gsurf->base.Type != EGL_PBUFFER_BIT) {
298 stfbi->flush_front = egl_g3d_st_framebuffer_flush_front;
299 stfbi->validate = egl_g3d_st_framebuffer_validate;
302 stfbi->flush_front = egl_g3d_st_framebuffer_flush_front_pbuffer;
303 stfbi->validate = egl_g3d_st_framebuffer_validate_pbuffer;
305 stfbi->st_manager_private = (void *) &gsurf->base;
311 egl_g3d_destroy_st_framebuffer(struct st_framebuffer_iface *stfbi)