st/mesa: release EGLImage on EGLImageTarget* error
[mesa.git] / src / mesa / state_tracker / st_cb_eglimage.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2010 LunarG Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
26 */
27
28 #include "main/errors.h"
29 #include "main/texobj.h"
30 #include "main/teximage.h"
31 #include "util/u_inlines.h"
32 #include "util/u_format.h"
33 #include "st_cb_eglimage.h"
34 #include "st_cb_fbo.h"
35 #include "st_context.h"
36 #include "st_texture.h"
37 #include "st_format.h"
38 #include "st_manager.h"
39 #include "st_sampler_view.h"
40 #include "util/u_surface.h"
41
42 static bool
43 is_format_supported(struct pipe_screen *screen, enum pipe_format format,
44 unsigned nr_samples, unsigned usage)
45 {
46 bool supported = screen->is_format_supported(screen, format, PIPE_TEXTURE_2D,
47 nr_samples, usage);
48
49 /* for sampling, some formats can be emulated.. it doesn't matter that
50 * the surface will have a format that the driver can't cope with because
51 * we'll give it sampler view formats that it can deal with and generate
52 * a shader variant that converts.
53 */
54 if ((usage == PIPE_BIND_SAMPLER_VIEW) && !supported) {
55 if (format == PIPE_FORMAT_IYUV) {
56 supported = screen->is_format_supported(screen, PIPE_FORMAT_R8_UNORM,
57 PIPE_TEXTURE_2D, nr_samples,
58 usage);
59 } else if (format == PIPE_FORMAT_NV12) {
60 supported = screen->is_format_supported(screen, PIPE_FORMAT_R8_UNORM,
61 PIPE_TEXTURE_2D, nr_samples,
62 usage) &&
63 screen->is_format_supported(screen, PIPE_FORMAT_R8G8_UNORM,
64 PIPE_TEXTURE_2D, nr_samples,
65 usage);
66 }
67 }
68
69 return supported;
70 }
71
72 /**
73 * Return the surface of an EGLImage.
74 * FIXME: I think this should operate on resources, not surfaces
75 */
76 static struct pipe_surface *
77 st_egl_image_get_surface(struct gl_context *ctx, GLeglImageOES image_handle,
78 unsigned usage, const char *error)
79 {
80 struct st_context *st = st_context(ctx);
81 struct pipe_screen *screen = st->pipe->screen;
82 struct st_manager *smapi =
83 (struct st_manager *) st->iface.st_context_private;
84 struct st_egl_image stimg;
85 struct pipe_surface *ps, surf_tmpl;
86
87 if (!smapi || !smapi->get_egl_image)
88 return NULL;
89
90 memset(&stimg, 0, sizeof(stimg));
91 if (!smapi->get_egl_image(smapi, (void *) image_handle, &stimg)) {
92 /* image_handle does not refer to a valid EGL image object */
93 _mesa_error(ctx, GL_INVALID_VALUE, "%s(image handle not found)", error);
94 return NULL;
95 }
96
97 if (!is_format_supported(screen, stimg.format, stimg.texture->nr_samples, usage)) {
98 /* unable to specify a texture object using the specified EGL image */
99 pipe_resource_reference(&stimg.texture, NULL);
100 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(format not supported)", error);
101 return NULL;
102 }
103
104 u_surface_default_template(&surf_tmpl, stimg.texture);
105 surf_tmpl.format = stimg.format;
106 surf_tmpl.u.tex.level = stimg.level;
107 surf_tmpl.u.tex.first_layer = stimg.layer;
108 surf_tmpl.u.tex.last_layer = stimg.layer;
109 ps = st->pipe->create_surface(st->pipe, stimg.texture, &surf_tmpl);
110 pipe_resource_reference(&stimg.texture, NULL);
111
112 return ps;
113 }
114
115 /**
116 * Return the base format just like _mesa_base_fbo_format does.
117 */
118 static GLenum
119 st_pipe_format_to_base_format(enum pipe_format format)
120 {
121 GLenum base_format;
122
123 if (util_format_is_depth_or_stencil(format)) {
124 if (util_format_is_depth_and_stencil(format)) {
125 base_format = GL_DEPTH_STENCIL;
126 }
127 else {
128 if (format == PIPE_FORMAT_S8_UINT)
129 base_format = GL_STENCIL_INDEX;
130 else
131 base_format = GL_DEPTH_COMPONENT;
132 }
133 }
134 else {
135 /* is this enough? */
136 if (util_format_has_alpha(format))
137 base_format = GL_RGBA;
138 else
139 base_format = GL_RGB;
140 }
141
142 return base_format;
143 }
144
145 static void
146 st_egl_image_target_renderbuffer_storage(struct gl_context *ctx,
147 struct gl_renderbuffer *rb,
148 GLeglImageOES image_handle)
149 {
150 struct st_renderbuffer *strb = st_renderbuffer(rb);
151 struct pipe_surface *ps;
152
153 ps = st_egl_image_get_surface(ctx, image_handle, PIPE_BIND_RENDER_TARGET,
154 "glEGLImageTargetRenderbufferStorage");
155 if (ps) {
156 strb->Base.Width = ps->width;
157 strb->Base.Height = ps->height;
158 strb->Base.Format = st_pipe_format_to_mesa_format(ps->format);
159 strb->Base._BaseFormat = st_pipe_format_to_base_format(ps->format);
160 strb->Base.InternalFormat = strb->Base._BaseFormat;
161
162 struct pipe_surface **psurf =
163 util_format_is_srgb(ps->format) ? &strb->surface_srgb :
164 &strb->surface_linear;
165
166 pipe_surface_reference(psurf, ps);
167 strb->surface = *psurf;
168 pipe_resource_reference(&strb->texture, ps->texture);
169
170 pipe_surface_reference(&ps, NULL);
171 }
172 }
173
174 static void
175 st_bind_surface(struct gl_context *ctx, GLenum target,
176 struct gl_texture_object *texObj,
177 struct gl_texture_image *texImage,
178 struct pipe_surface *ps)
179 {
180 struct st_context *st = st_context(ctx);
181 struct st_texture_object *stObj;
182 struct st_texture_image *stImage;
183 GLenum internalFormat;
184 mesa_format texFormat;
185
186 /* map pipe format to base format */
187 if (util_format_get_component_bits(ps->format, UTIL_FORMAT_COLORSPACE_RGB, 3) > 0)
188 internalFormat = GL_RGBA;
189 else
190 internalFormat = GL_RGB;
191
192 stObj = st_texture_object(texObj);
193 stImage = st_texture_image(texImage);
194
195 /* switch to surface based */
196 if (!stObj->surface_based) {
197 _mesa_clear_texture_object(ctx, texObj);
198 stObj->surface_based = GL_TRUE;
199 }
200
201 texFormat = st_pipe_format_to_mesa_format(ps->format);
202
203 /* TODO RequiredTextureImageUnits should probably be reset back
204 * to 1 somewhere if different texture is bound??
205 */
206 if (texFormat == MESA_FORMAT_NONE) {
207 switch (ps->format) {
208 case PIPE_FORMAT_NV12:
209 texFormat = MESA_FORMAT_R_UNORM8;
210 texObj->RequiredTextureImageUnits = 2;
211 break;
212 case PIPE_FORMAT_IYUV:
213 texFormat = MESA_FORMAT_R_UNORM8;
214 texObj->RequiredTextureImageUnits = 3;
215 break;
216 default:
217 unreachable("bad YUV format!");
218 }
219 }
220
221 _mesa_init_teximage_fields(ctx, texImage,
222 ps->width, ps->height, 1, 0, internalFormat,
223 texFormat);
224
225 /* FIXME create a non-default sampler view from the pipe_surface? */
226 pipe_resource_reference(&stObj->pt, ps->texture);
227 st_texture_release_all_sampler_views(st, stObj);
228 pipe_resource_reference(&stImage->pt, stObj->pt);
229
230 stObj->surface_format = ps->format;
231
232 _mesa_dirty_texobj(ctx, texObj);
233 }
234
235 static void
236 st_egl_image_target_texture_2d(struct gl_context *ctx, GLenum target,
237 struct gl_texture_object *texObj,
238 struct gl_texture_image *texImage,
239 GLeglImageOES image_handle)
240 {
241 struct pipe_surface *ps;
242
243 ps = st_egl_image_get_surface(ctx, image_handle, PIPE_BIND_SAMPLER_VIEW,
244 "glEGLImageTargetTexture2D");
245 if (ps) {
246 st_bind_surface(ctx, target, texObj, texImage, ps);
247 pipe_surface_reference(&ps, NULL);
248 }
249 }
250
251 void
252 st_init_eglimage_functions(struct dd_function_table *functions)
253 {
254 functions->EGLImageTargetTexture2D = st_egl_image_target_texture_2d;
255 functions->EGLImageTargetRenderbufferStorage = st_egl_image_target_renderbuffer_storage;
256 }