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