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 (at) 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 gallium texture of an EGLImage. 74 */ 75 static bool 76 st_get_egl_image(struct gl_context *ctx, GLeglImageOES image_handle, 77 unsigned usage, const char *error, struct st_egl_image *out) 78 { 79 struct st_context *st = st_context(ctx); 80 struct pipe_screen *screen = st->pipe->screen; 81 struct st_manager *smapi = 82 (struct st_manager *) st->iface.st_context_private; 83 84 if (!smapi || !smapi->get_egl_image) 85 return false; 86 87 memset(out, 0, sizeof(*out)); 88 if (!smapi->get_egl_image(smapi, (void *) image_handle, out)) { 89 /* image_handle does not refer to a valid EGL image object */ 90 _mesa_error(ctx, GL_INVALID_VALUE, "%s(image handle not found)", error); 91 return false; 92 } 93 94 if (!is_format_supported(screen, out->format, out->texture->nr_samples, usage)) { 95 /* unable to specify a texture object using the specified EGL image */ 96 pipe_resource_reference(&out->texture, NULL); 97 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(format not supported)", error); 98 return false; 99 } 100 101 return true; 102 } 103 104 /** 105 * Return the base format just like _mesa_base_fbo_format does. 106 */ 107 static GLenum 108 st_pipe_format_to_base_format(enum pipe_format format) 109 { 110 GLenum base_format; 111 112 if (util_format_is_depth_or_stencil(format)) { 113 if (util_format_is_depth_and_stencil(format)) { 114 base_format = GL_DEPTH_STENCIL; 115 } 116 else { 117 if (format == PIPE_FORMAT_S8_UINT) 118 base_format = GL_STENCIL_INDEX; 119 else 120 base_format = GL_DEPTH_COMPONENT; 121 } 122 } 123 else { 124 /* is this enough? */ 125 if (util_format_has_alpha(format)) 126 base_format = GL_RGBA; 127 else 128 base_format = GL_RGB; 129 } 130 131 return base_format; 132 } 133 134 static void 135 st_egl_image_target_renderbuffer_storage(struct gl_context *ctx, 136 struct gl_renderbuffer *rb, 137 GLeglImageOES image_handle) 138 { 139 struct st_renderbuffer *strb = st_renderbuffer(rb); 140 struct st_egl_image stimg; 141 142 if (st_get_egl_image(ctx, image_handle, PIPE_BIND_RENDER_TARGET, 143 "glEGLImageTargetRenderbufferStorage", 144 &stimg)) { 145 struct pipe_context *pipe = st_context(ctx)->pipe; 146 struct pipe_surface *ps, surf_tmpl; 147 148 u_surface_default_template(&surf_tmpl, stimg.texture); 149 surf_tmpl.format = stimg.format; 150 surf_tmpl.u.tex.level = stimg.level; 151 surf_tmpl.u.tex.first_layer = stimg.layer; 152 surf_tmpl.u.tex.last_layer = stimg.layer; 153 ps = pipe->create_surface(pipe, stimg.texture, &surf_tmpl); 154 pipe_resource_reference(&stimg.texture, NULL); 155 156 if (!ps) 157 return; 158 159 strb->Base.Width = ps->width; 160 strb->Base.Height = ps->height; 161 strb->Base.Format = st_pipe_format_to_mesa_format(ps->format); 162 strb->Base._BaseFormat = st_pipe_format_to_base_format(ps->format); 163 strb->Base.InternalFormat = strb->Base._BaseFormat; 164 165 struct pipe_surface **psurf = 166 util_format_is_srgb(ps->format) ? &strb->surface_srgb : 167 &strb->surface_linear; 168 169 pipe_surface_reference(psurf, ps); 170 strb->surface = *psurf; 171 pipe_resource_reference(&strb->texture, ps->texture); 172 173 pipe_surface_reference(&ps, NULL); 174 } 175 } 176 177 static void 178 st_bind_egl_image(struct gl_context *ctx, 179 struct gl_texture_object *texObj, 180 struct gl_texture_image *texImage, 181 struct st_egl_image *stimg) 182 { 183 struct st_context *st = st_context(ctx); 184 struct st_texture_object *stObj; 185 struct st_texture_image *stImage; 186 GLenum internalFormat; 187 mesa_format texFormat; 188 189 /* map pipe format to base format */ 190 if (util_format_get_component_bits(stimg->format, 191 UTIL_FORMAT_COLORSPACE_RGB, 3) > 0) 192 internalFormat = GL_RGBA; 193 else 194 internalFormat = GL_RGB; 195 196 stObj = st_texture_object(texObj); 197 stImage = st_texture_image(texImage); 198 199 /* switch to surface based */ 200 if (!stObj->surface_based) { 201 _mesa_clear_texture_object(ctx, texObj, NULL); 202 stObj->surface_based = GL_TRUE; 203 } 204 205 texFormat = st_pipe_format_to_mesa_format(stimg->format); 206 207 /* TODO RequiredTextureImageUnits should probably be reset back 208 * to 1 somewhere if different texture is bound?? 209 */ 210 if (texFormat == MESA_FORMAT_NONE) { 211 switch (stimg->format) { 212 case PIPE_FORMAT_NV12: 213 texFormat = MESA_FORMAT_R_UNORM8; 214 texObj->RequiredTextureImageUnits = 2; 215 break; 216 case PIPE_FORMAT_IYUV: 217 texFormat = MESA_FORMAT_R_UNORM8; 218 texObj->RequiredTextureImageUnits = 3; 219 break; 220 default: 221 unreachable("bad YUV format!"); 222 } 223 } 224 225 _mesa_init_teximage_fields(ctx, texImage, 226 stimg->texture->width0, stimg->texture->height0, 227 1, 0, internalFormat, texFormat); 228 229 pipe_resource_reference(&stObj->pt, stimg->texture); 230 st_texture_release_all_sampler_views(st, stObj); 231 pipe_resource_reference(&stImage->pt, stObj->pt); 232 233 stObj->surface_format = stimg->format; 234 stObj->level_override = stimg->level; 235 stObj->layer_override = stimg->layer; 236 237 _mesa_dirty_texobj(ctx, texObj); 238 } 239 240 static void 241 st_egl_image_target_texture_2d(struct gl_context *ctx, GLenum target, 242 struct gl_texture_object *texObj, 243 struct gl_texture_image *texImage, 244 GLeglImageOES image_handle) 245 { 246 struct st_egl_image stimg; 247 248 if (!st_get_egl_image(ctx, image_handle, PIPE_BIND_SAMPLER_VIEW, 249 "glEGLImageTargetTexture2D", &stimg)) 250 return; 251 252 st_bind_egl_image(ctx, texObj, texImage, &stimg); 253 pipe_resource_reference(&stimg.texture, NULL); 254 } 255 256 void 257 st_init_eglimage_functions(struct dd_function_table *functions) 258 { 259 functions->EGLImageTargetTexture2D = st_egl_image_target_texture_2d; 260 functions->EGLImageTargetRenderbufferStorage = st_egl_image_target_renderbuffer_storage; 261 } 262