st/mesa: Fix EGLImageTargetTexture2D for GL_TEXTURE_2D
[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/format/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 nr_storage_samples,
45 unsigned usage, bool *native_supported)
46 {
47 bool supported = screen->is_format_supported(screen, format, PIPE_TEXTURE_2D,
48 nr_samples, nr_storage_samples,
49 usage);
50 *native_supported = supported;
51
52 /* for sampling, some formats can be emulated.. it doesn't matter that
53 * the surface will have a format that the driver can't cope with because
54 * we'll give it sampler view formats that it can deal with and generate
55 * a shader variant that converts.
56 */
57 if ((usage == PIPE_BIND_SAMPLER_VIEW) && !supported) {
58 switch (format) {
59 case PIPE_FORMAT_IYUV:
60 supported = screen->is_format_supported(screen, PIPE_FORMAT_R8_UNORM,
61 PIPE_TEXTURE_2D, nr_samples,
62 nr_storage_samples, usage);
63 break;
64 case PIPE_FORMAT_NV12:
65 supported = screen->is_format_supported(screen, PIPE_FORMAT_R8_UNORM,
66 PIPE_TEXTURE_2D, nr_samples,
67 nr_storage_samples, usage) &&
68 screen->is_format_supported(screen, PIPE_FORMAT_R8G8_UNORM,
69 PIPE_TEXTURE_2D, nr_samples,
70 nr_storage_samples, usage);
71 break;
72 case PIPE_FORMAT_P016:
73 supported = screen->is_format_supported(screen, PIPE_FORMAT_R16_UNORM,
74 PIPE_TEXTURE_2D, nr_samples,
75 nr_storage_samples, usage) &&
76 screen->is_format_supported(screen, PIPE_FORMAT_R16G16_UNORM,
77 PIPE_TEXTURE_2D, nr_samples,
78 nr_storage_samples, usage);
79 break;
80 case PIPE_FORMAT_YUYV:
81 supported = screen->is_format_supported(screen, PIPE_FORMAT_RG88_UNORM,
82 PIPE_TEXTURE_2D, nr_samples,
83 nr_storage_samples, usage) &&
84 screen->is_format_supported(screen, PIPE_FORMAT_BGRA8888_UNORM,
85 PIPE_TEXTURE_2D, nr_samples,
86 nr_storage_samples, usage);
87 break;
88 case PIPE_FORMAT_UYVY:
89 supported = screen->is_format_supported(screen, PIPE_FORMAT_RG88_UNORM,
90 PIPE_TEXTURE_2D, nr_samples,
91 nr_storage_samples, usage) &&
92 screen->is_format_supported(screen, PIPE_FORMAT_RGBA8888_UNORM,
93 PIPE_TEXTURE_2D, nr_samples,
94 nr_storage_samples, usage);
95 break;
96 case PIPE_FORMAT_AYUV:
97 supported = screen->is_format_supported(screen, PIPE_FORMAT_RGBA8888_UNORM,
98 PIPE_TEXTURE_2D, nr_samples,
99 nr_storage_samples, usage);
100 break;
101 case PIPE_FORMAT_XYUV:
102 supported = screen->is_format_supported(screen, PIPE_FORMAT_RGBX8888_UNORM,
103 PIPE_TEXTURE_2D, nr_samples,
104 nr_storage_samples, usage);
105 break;
106 default:
107 break;
108 }
109 }
110
111 return supported;
112 }
113
114 /**
115 * Return the gallium texture of an EGLImage.
116 */
117 static bool
118 st_get_egl_image(struct gl_context *ctx, GLeglImageOES image_handle,
119 unsigned usage, const char *error, struct st_egl_image *out,
120 bool *native_supported)
121 {
122 struct st_context *st = st_context(ctx);
123 struct pipe_screen *screen = st->pipe->screen;
124 struct st_manager *smapi =
125 (struct st_manager *) st->iface.st_context_private;
126
127 if (!smapi || !smapi->get_egl_image)
128 return false;
129
130 memset(out, 0, sizeof(*out));
131 if (!smapi->get_egl_image(smapi, (void *) image_handle, out)) {
132 /* image_handle does not refer to a valid EGL image object */
133 _mesa_error(ctx, GL_INVALID_VALUE, "%s(image handle not found)", error);
134 return false;
135 }
136
137 if (!is_format_supported(screen, out->format, out->texture->nr_samples,
138 out->texture->nr_storage_samples, usage,
139 native_supported)) {
140 /* unable to specify a texture object using the specified EGL image */
141 pipe_resource_reference(&out->texture, NULL);
142 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(format not supported)", error);
143 return false;
144 }
145
146 return true;
147 }
148
149 /**
150 * Return the base format just like _mesa_base_fbo_format does.
151 */
152 static GLenum
153 st_pipe_format_to_base_format(enum pipe_format format)
154 {
155 GLenum base_format;
156
157 if (util_format_is_depth_or_stencil(format)) {
158 if (util_format_is_depth_and_stencil(format)) {
159 base_format = GL_DEPTH_STENCIL;
160 }
161 else {
162 if (format == PIPE_FORMAT_S8_UINT)
163 base_format = GL_STENCIL_INDEX;
164 else
165 base_format = GL_DEPTH_COMPONENT;
166 }
167 }
168 else {
169 /* is this enough? */
170 if (util_format_has_alpha(format))
171 base_format = GL_RGBA;
172 else
173 base_format = GL_RGB;
174 }
175
176 return base_format;
177 }
178
179 static void
180 st_egl_image_target_renderbuffer_storage(struct gl_context *ctx,
181 struct gl_renderbuffer *rb,
182 GLeglImageOES image_handle)
183 {
184 struct st_renderbuffer *strb = st_renderbuffer(rb);
185 struct st_egl_image stimg;
186 bool native_supported;
187
188 if (st_get_egl_image(ctx, image_handle, PIPE_BIND_RENDER_TARGET,
189 "glEGLImageTargetRenderbufferStorage",
190 &stimg, &native_supported)) {
191 struct pipe_context *pipe = st_context(ctx)->pipe;
192 struct pipe_surface *ps, surf_tmpl;
193
194 u_surface_default_template(&surf_tmpl, stimg.texture);
195 surf_tmpl.format = stimg.format;
196 surf_tmpl.u.tex.level = stimg.level;
197 surf_tmpl.u.tex.first_layer = stimg.layer;
198 surf_tmpl.u.tex.last_layer = stimg.layer;
199 ps = pipe->create_surface(pipe, stimg.texture, &surf_tmpl);
200 pipe_resource_reference(&stimg.texture, NULL);
201
202 if (!ps)
203 return;
204
205 strb->Base.Format = st_pipe_format_to_mesa_format(ps->format);
206 strb->Base._BaseFormat = st_pipe_format_to_base_format(ps->format);
207 strb->Base.InternalFormat = strb->Base._BaseFormat;
208
209 st_set_ws_renderbuffer_surface(strb, ps);
210 pipe_surface_reference(&ps, NULL);
211 }
212 }
213
214 static void
215 st_bind_egl_image(struct gl_context *ctx,
216 struct gl_texture_object *texObj,
217 struct gl_texture_image *texImage,
218 struct st_egl_image *stimg,
219 bool tex_storage,
220 bool native_supported)
221 {
222 struct st_context *st = st_context(ctx);
223 struct st_texture_object *stObj;
224 struct st_texture_image *stImage;
225 GLenum internalFormat;
226 mesa_format texFormat;
227
228 /* map pipe format to base format */
229 if (util_format_get_component_bits(stimg->format,
230 UTIL_FORMAT_COLORSPACE_RGB, 3) > 0)
231 internalFormat = GL_RGBA;
232 else
233 internalFormat = GL_RGB;
234
235 stObj = st_texture_object(texObj);
236 stImage = st_texture_image(texImage);
237
238 /* switch to surface based */
239 if (!stObj->surface_based) {
240 _mesa_clear_texture_object(ctx, texObj, NULL);
241 stObj->surface_based = GL_TRUE;
242 }
243
244 /* TODO RequiredTextureImageUnits should probably be reset back
245 * to 1 somewhere if different texture is bound??
246 */
247 if (!native_supported) {
248 switch (stimg->format) {
249 case PIPE_FORMAT_NV12:
250 texFormat = MESA_FORMAT_R_UNORM8;
251 texObj->RequiredTextureImageUnits = 2;
252 break;
253 case PIPE_FORMAT_P016:
254 texFormat = MESA_FORMAT_R_UNORM16;
255 texObj->RequiredTextureImageUnits = 2;
256 break;
257 case PIPE_FORMAT_IYUV:
258 texFormat = MESA_FORMAT_R_UNORM8;
259 texObj->RequiredTextureImageUnits = 3;
260 break;
261 case PIPE_FORMAT_YUYV:
262 case PIPE_FORMAT_UYVY:
263 texFormat = MESA_FORMAT_RG_UNORM8;
264 texObj->RequiredTextureImageUnits = 2;
265 break;
266 case PIPE_FORMAT_AYUV:
267 texFormat = MESA_FORMAT_R8G8B8A8_UNORM;
268 internalFormat = GL_RGBA;
269 texObj->RequiredTextureImageUnits = 1;
270 break;
271 case PIPE_FORMAT_XYUV:
272 texFormat = MESA_FORMAT_R8G8B8X8_UNORM;
273 texObj->RequiredTextureImageUnits = 1;
274 break;
275 default:
276 unreachable("unexpected emulated format");
277 break;
278 }
279 } else {
280 texFormat = st_pipe_format_to_mesa_format(stimg->format);
281 /* Use previously derived internalformat as specified by
282 * EXT_EGL_image_storage.
283 */
284 if (tex_storage && texObj->Target == GL_TEXTURE_2D
285 && stimg->internalformat) {
286 internalFormat = stimg->internalformat;
287 if (internalFormat == GL_NONE) {
288 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
289 return;
290 }
291 }
292 }
293 assert(texFormat != MESA_FORMAT_NONE);
294
295
296 /* Minify texture size based on level set on the EGLImage. */
297 uint32_t width = u_minify(stimg->texture->width0, stimg->level);
298 uint32_t height = u_minify(stimg->texture->height0, stimg->level);
299
300 _mesa_init_teximage_fields(ctx, texImage, width, height,
301 1, 0, internalFormat, texFormat);
302
303 pipe_resource_reference(&stObj->pt, stimg->texture);
304 st_texture_release_all_sampler_views(st, stObj);
305 pipe_resource_reference(&stImage->pt, stObj->pt);
306 if (st->pipe->screen->resource_changed)
307 st->pipe->screen->resource_changed(st->pipe->screen, stImage->pt);
308
309 stObj->surface_format = stimg->format;
310 stObj->level_override = stimg->level;
311 stObj->layer_override = stimg->layer;
312
313 _mesa_dirty_texobj(ctx, texObj);
314 }
315
316 static void
317 st_egl_image_target_texture_2d(struct gl_context *ctx, GLenum target,
318 struct gl_texture_object *texObj,
319 struct gl_texture_image *texImage,
320 GLeglImageOES image_handle)
321 {
322 struct st_egl_image stimg;
323 bool native_supported;
324
325 if (!st_get_egl_image(ctx, image_handle, PIPE_BIND_SAMPLER_VIEW,
326 "glEGLImageTargetTexture2D", &stimg,
327 &native_supported))
328 return;
329
330 st_bind_egl_image(ctx, texObj, texImage, &stimg,
331 target != GL_TEXTURE_EXTERNAL_OES,
332 native_supported);
333 pipe_resource_reference(&stimg.texture, NULL);
334 }
335
336 static void
337 st_egl_image_target_tex_storage(struct gl_context *ctx, GLenum target,
338 struct gl_texture_object *texObj,
339 struct gl_texture_image *texImage,
340 GLeglImageOES image_handle)
341 {
342 struct st_egl_image stimg;
343 bool native_supported;
344
345 if (!st_get_egl_image(ctx, image_handle, PIPE_BIND_SAMPLER_VIEW,
346 "glEGLImageTargetTexture2D", &stimg,
347 &native_supported))
348 return;
349
350 st_bind_egl_image(ctx, texObj, texImage, &stimg, true, native_supported);
351 pipe_resource_reference(&stimg.texture, NULL);
352 }
353
354 void
355 st_init_eglimage_functions(struct dd_function_table *functions)
356 {
357 functions->EGLImageTargetTexture2D = st_egl_image_target_texture_2d;
358 functions->EGLImageTargetTexStorage = st_egl_image_target_tex_storage;
359 functions->EGLImageTargetRenderbufferStorage = st_egl_image_target_renderbuffer_storage;
360 }