st/mesa: improve error messages and fix security warning
[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
43 /**
44 * Return the surface of an EGLImage.
45 * FIXME: I think this should operate on resources, not surfaces
46 */
47 static struct pipe_surface *
48 st_egl_image_get_surface(struct gl_context *ctx, GLeglImageOES image_handle,
49 unsigned usage, const char *error)
50 {
51 struct st_context *st = st_context(ctx);
52 struct pipe_screen *screen = st->pipe->screen;
53 struct st_manager *smapi =
54 (struct st_manager *) st->iface.st_context_private;
55 struct st_egl_image stimg;
56 struct pipe_surface *ps, surf_tmpl;
57
58 if (!smapi || !smapi->get_egl_image)
59 return NULL;
60
61 memset(&stimg, 0, sizeof(stimg));
62 if (!smapi->get_egl_image(smapi, (void *) image_handle, &stimg)) {
63 /* image_handle does not refer to a valid EGL image object */
64 _mesa_error(ctx, GL_INVALID_VALUE, "%s(image handle not found)", error);
65 return NULL;
66 }
67
68 if (!screen->is_format_supported(screen, stimg.format, PIPE_TEXTURE_2D,
69 stimg.texture->nr_samples, usage)) {
70 /* unable to specify a texture object using the specified EGL image */
71 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(format not supported)", error);
72 return NULL;
73 }
74
75 u_surface_default_template(&surf_tmpl, stimg.texture);
76 surf_tmpl.format = stimg.format;
77 surf_tmpl.u.tex.level = stimg.level;
78 surf_tmpl.u.tex.first_layer = stimg.layer;
79 surf_tmpl.u.tex.last_layer = stimg.layer;
80 ps = st->pipe->create_surface(st->pipe, stimg.texture, &surf_tmpl);
81 pipe_resource_reference(&stimg.texture, NULL);
82
83 return ps;
84 }
85
86 /**
87 * Return the base format just like _mesa_base_fbo_format does.
88 */
89 static GLenum
90 st_pipe_format_to_base_format(enum pipe_format format)
91 {
92 GLenum base_format;
93
94 if (util_format_is_depth_or_stencil(format)) {
95 if (util_format_is_depth_and_stencil(format)) {
96 base_format = GL_DEPTH_STENCIL;
97 }
98 else {
99 if (format == PIPE_FORMAT_S8_UINT)
100 base_format = GL_STENCIL_INDEX;
101 else
102 base_format = GL_DEPTH_COMPONENT;
103 }
104 }
105 else {
106 /* is this enough? */
107 if (util_format_has_alpha(format))
108 base_format = GL_RGBA;
109 else
110 base_format = GL_RGB;
111 }
112
113 return base_format;
114 }
115
116 static void
117 st_egl_image_target_renderbuffer_storage(struct gl_context *ctx,
118 struct gl_renderbuffer *rb,
119 GLeglImageOES image_handle)
120 {
121 struct st_renderbuffer *strb = st_renderbuffer(rb);
122 struct pipe_surface *ps;
123
124 ps = st_egl_image_get_surface(ctx, image_handle, PIPE_BIND_RENDER_TARGET,
125 "glEGLImageTargetRenderbufferStorage");
126 if (ps) {
127 strb->Base.Width = ps->width;
128 strb->Base.Height = ps->height;
129 strb->Base.Format = st_pipe_format_to_mesa_format(ps->format);
130 strb->Base._BaseFormat = st_pipe_format_to_base_format(ps->format);
131 strb->Base.InternalFormat = strb->Base._BaseFormat;
132
133 pipe_surface_reference(&strb->surface, ps);
134 pipe_resource_reference(&strb->texture, ps->texture);
135
136 pipe_surface_reference(&ps, NULL);
137 }
138 }
139
140 static void
141 st_bind_surface(struct gl_context *ctx, GLenum target,
142 struct gl_texture_object *texObj,
143 struct gl_texture_image *texImage,
144 struct pipe_surface *ps)
145 {
146 struct st_context *st = st_context(ctx);
147 struct st_texture_object *stObj;
148 struct st_texture_image *stImage;
149 GLenum internalFormat;
150 mesa_format texFormat;
151
152 /* map pipe format to base format */
153 if (util_format_get_component_bits(ps->format, UTIL_FORMAT_COLORSPACE_RGB, 3) > 0)
154 internalFormat = GL_RGBA;
155 else
156 internalFormat = GL_RGB;
157
158 stObj = st_texture_object(texObj);
159 stImage = st_texture_image(texImage);
160
161 /* switch to surface based */
162 if (!stObj->surface_based) {
163 _mesa_clear_texture_object(ctx, texObj);
164 stObj->surface_based = GL_TRUE;
165 }
166
167 texFormat = st_pipe_format_to_mesa_format(ps->format);
168
169 /* TODO RequiredTextureImageUnits should probably be reset back
170 * to 1 somewhere if different texture is bound??
171 */
172 if (texFormat == MESA_FORMAT_NONE) {
173 switch (ps->format) {
174 case PIPE_FORMAT_NV12:
175 texFormat = MESA_FORMAT_R_UNORM8;
176 texObj->RequiredTextureImageUnits = 2;
177 break;
178 case PIPE_FORMAT_IYUV:
179 texFormat = MESA_FORMAT_R_UNORM8;
180 texObj->RequiredTextureImageUnits = 3;
181 break;
182 default:
183 unreachable("bad YUV format!");
184 }
185 }
186
187 _mesa_init_teximage_fields(ctx, texImage,
188 ps->width, ps->height, 1, 0, internalFormat,
189 texFormat);
190
191 /* FIXME create a non-default sampler view from the pipe_surface? */
192 pipe_resource_reference(&stObj->pt, ps->texture);
193 st_texture_release_all_sampler_views(st, stObj);
194 pipe_resource_reference(&stImage->pt, stObj->pt);
195
196 stObj->surface_format = ps->format;
197
198 _mesa_dirty_texobj(ctx, texObj);
199 }
200
201 static void
202 st_egl_image_target_texture_2d(struct gl_context *ctx, GLenum target,
203 struct gl_texture_object *texObj,
204 struct gl_texture_image *texImage,
205 GLeglImageOES image_handle)
206 {
207 struct pipe_surface *ps;
208
209 ps = st_egl_image_get_surface(ctx, image_handle, PIPE_BIND_SAMPLER_VIEW,
210 "glEGLImageTargetTexture2D");
211 if (ps) {
212 st_bind_surface(ctx, target, texObj, texImage, ps);
213 pipe_surface_reference(&ps, NULL);
214 }
215 }
216
217 void
218 st_init_eglimage_functions(struct dd_function_table *functions)
219 {
220 functions->EGLImageTargetTexture2D = st_egl_image_target_texture_2d;
221 functions->EGLImageTargetRenderbufferStorage = st_egl_image_target_renderbuffer_storage;
222 }