Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / mesa / state_tracker / st_cb_eglimage.c
1 /*
2  * Mesa 3-D graphics library
3  * Version:  7.9
4  *
5  * Copyright (C) 2010 LunarG Inc.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  *
25  * Authors:
26  *    Chia-I Wu <olv@lunarg.com>
27  */
28
29 #include "main/mfeatures.h"
30 #include "main/texobj.h"
31 #include "main/texfetch.h"
32 #include "main/teximage.h"
33 #include "util/u_inlines.h"
34 #include "util/u_format.h"
35 #include "st_cb_eglimage.h"
36 #include "st_cb_fbo.h"
37 #include "st_context.h"
38 #include "st_texture.h"
39 #include "st_format.h"
40 #include "st_manager.h"
41
42 #if FEATURE_OES_EGL_image
43
44 /**
45  * Return the base format just like _mesa_base_fbo_format does.
46  */
47 static GLenum
48 st_pipe_format_to_base_format(enum pipe_format format)
49 {
50    GLenum base_format;
51
52    if (util_format_is_depth_or_stencil(format)) {
53       if (util_format_is_depth_and_stencil(format)) {
54          base_format = GL_DEPTH_STENCIL;
55       }
56       else {
57          if (format == PIPE_FORMAT_S8_USCALED)
58             base_format = GL_STENCIL_INDEX;
59          else
60             base_format = GL_DEPTH_COMPONENT;
61       }
62    }
63    else {
64       /* is this enough? */
65       if (util_format_has_alpha(format))
66          base_format = GL_RGBA;
67       else
68          base_format = GL_RGB;
69    }
70
71    return base_format;
72 }
73
74 static void
75 st_egl_image_target_renderbuffer_storage(struct gl_context *ctx,
76                                          struct gl_renderbuffer *rb,
77                                          GLeglImageOES image_handle)
78 {
79    struct st_context *st = st_context(ctx);
80    struct st_renderbuffer *strb = st_renderbuffer(rb);
81    struct pipe_surface *ps;
82    unsigned usage;
83
84    usage = PIPE_BIND_RENDER_TARGET;
85    ps = st_manager_get_egl_image_surface(st, (void *) image_handle, usage);
86    if (ps) {
87       strb->Base.Width = ps->width;
88       strb->Base.Height = ps->height;
89       strb->Base.Format = st_pipe_format_to_mesa_format(ps->format);
90       strb->Base.DataType = st_format_datatype(ps->format);
91       strb->Base._BaseFormat = st_pipe_format_to_base_format(ps->format);
92       strb->Base.InternalFormat = strb->Base._BaseFormat;
93
94       pipe_surface_reference(&strb->surface, ps);
95       pipe_resource_reference(&strb->texture, ps->texture);
96
97       pipe_surface_reference(&ps, NULL);
98    }
99 }
100
101 static void
102 st_bind_surface(struct gl_context *ctx, GLenum target,
103                 struct gl_texture_object *texObj,
104                 struct gl_texture_image *texImage,
105                 struct pipe_surface *ps)
106 {
107    struct st_texture_object *stObj;
108    struct st_texture_image *stImage;
109    GLenum internalFormat;
110    gl_format texFormat;
111
112    /* map pipe format to base format */
113    if (util_format_get_component_bits(ps->format, UTIL_FORMAT_COLORSPACE_RGB, 3) > 0)
114       internalFormat = GL_RGBA;
115    else
116       internalFormat = GL_RGB;
117
118    stObj = st_texture_object(texObj);
119    stImage = st_texture_image(texImage);
120
121    /* switch to surface based */
122    if (!stObj->surface_based) {
123       _mesa_clear_texture_object(ctx, texObj);
124       stObj->surface_based = GL_TRUE;
125    }
126
127    texFormat = st_pipe_format_to_mesa_format(ps->format);
128
129    _mesa_init_teximage_fields(ctx, target, texImage,
130                               ps->width, ps->height, 1, 0, internalFormat,
131                               texFormat);
132
133    /* FIXME create a non-default sampler view from the pipe_surface? */
134    pipe_resource_reference(&stObj->pt, ps->texture);
135    pipe_sampler_view_reference(&stObj->sampler_view, NULL);
136    pipe_resource_reference(&stImage->pt, stObj->pt);
137
138    stObj->width0 = ps->width;
139    stObj->height0 = ps->height;
140    stObj->depth0 = 1;
141
142    _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
143 }
144
145 static void
146 st_egl_image_target_texture_2d(struct gl_context *ctx, GLenum target,
147                                struct gl_texture_object *texObj,
148                                struct gl_texture_image *texImage,
149                                GLeglImageOES image_handle)
150 {
151    struct st_context *st = st_context(ctx);
152    struct pipe_surface *ps;
153    unsigned usage;
154
155    usage = PIPE_BIND_SAMPLER_VIEW;
156    ps = st_manager_get_egl_image_surface(st, (void *) image_handle, usage);
157    if (ps) {
158       st_bind_surface(ctx, target, texObj, texImage, ps);
159       pipe_surface_reference(&ps, NULL);
160    }
161 }
162
163 void
164 st_init_eglimage_functions(struct dd_function_table *functions)
165 {
166    functions->EGLImageTargetTexture2D = st_egl_image_target_texture_2d;
167    functions->EGLImageTargetRenderbufferStorage = st_egl_image_target_renderbuffer_storage;
168 }
169
170 #endif /* FEATURE_OES_EGL_image */