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