934b70dc1a6efa0fa8e61ca55850badb80242b49
[profile/ivi/mesa.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/texobj.h"
30 #include "main/texfetch.h"
31 #include "main/teximage.h"
32 #include "util/u_inlines.h"
33 #include "util/u_format.h"
34 #include "st_cb_eglimage.h"
35 #include "st_cb_fbo.h"
36 #include "st_texture.h"
37 #include "st_format.h"
38 #include "st_manager.h"
39
40 #if FEATURE_OES_EGL_image
41
42 /**
43  * Return the base format just like _mesa_base_fbo_format does.
44  */
45 static GLenum
46 st_pipe_format_to_base_format(enum pipe_format format)
47 {
48    GLenum base_format;
49
50    if (util_format_is_depth_or_stencil(format)) {
51       if (util_format_is_depth_and_stencil(format)) {
52          base_format = GL_DEPTH_STENCIL;
53       }
54       else {
55          if (format == PIPE_FORMAT_S8_USCALED)
56             base_format = GL_STENCIL_INDEX;
57          else
58             base_format = GL_DEPTH_COMPONENT;
59       }
60    }
61    else {
62       /* is this enough? */
63       if (util_format_has_alpha(format))
64          base_format = GL_RGBA;
65       else
66          base_format = GL_RGB;
67    }
68
69    return base_format;
70 }
71
72 static void
73 st_egl_image_target_renderbuffer_storage(GLcontext *ctx,
74                                          struct gl_renderbuffer *rb,
75                                          GLeglImageOES image_handle)
76 {
77    struct st_context *st = ctx->st;
78    struct st_renderbuffer *strb = st_renderbuffer(rb);
79    struct pipe_surface *ps;
80    unsigned usage;
81
82    usage = PIPE_BUFFER_USAGE_GPU_READ | PIPE_BUFFER_USAGE_GPU_WRITE;
83    ps = st_manager_get_egl_image_surface(st, (void *) image_handle, usage);
84    if (ps) {
85       strb->Base.Width = ps->width;
86       strb->Base.Height = ps->height;
87       strb->Base.Format = st_pipe_format_to_mesa_format(ps->format);
88       strb->Base.DataType = st_format_datatype(ps->format);
89       strb->Base._BaseFormat = st_pipe_format_to_base_format(ps->format);
90       strb->Base.InternalFormat = strb->Base._BaseFormat;
91
92       pipe_surface_reference(&strb->surface, ps);
93       pipe_texture_reference(&strb->texture, ps->texture);
94
95       pipe_surface_reference(&ps, NULL);
96    }
97 }
98
99 static void
100 st_bind_surface(GLcontext *ctx, GLenum target,
101                 struct gl_texture_object *texObj,
102                 struct gl_texture_image *texImage,
103                 struct pipe_surface *ps)
104 {
105    struct st_texture_object *stObj;
106    struct st_texture_image *stImage;
107    GLenum internalFormat;
108
109    /* map pipe format to base format */
110    if (util_format_get_component_bits(ps->format, UTIL_FORMAT_COLORSPACE_RGB, 3) > 0)
111       internalFormat = GL_RGBA;
112    else
113       internalFormat = GL_RGB;
114
115    stObj = st_texture_object(texObj);
116    stImage = st_texture_image(texImage);
117
118    /* switch to surface based */
119    if (!stObj->surface_based) {
120       _mesa_clear_texture_object(ctx, texObj);
121       stObj->surface_based = GL_TRUE;
122    }
123
124    _mesa_init_teximage_fields(ctx, target, texImage,
125                               ps->width, ps->height, 1, 0, internalFormat);
126    texImage->TexFormat = st_pipe_format_to_mesa_format(ps->format);
127    _mesa_set_fetch_functions(texImage, 2);
128
129    stObj->pipe = ctx->st->pipe;
130    /* FIXME create a non-default sampler view from the pipe_surface? */
131    pipe_texture_reference(&stImage->pt, ps->texture);
132
133    _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
134 }
135
136 static void
137 st_egl_image_target_texture_2d(GLcontext *ctx, GLenum target,
138                                struct gl_texture_object *texObj,
139                                struct gl_texture_image *texImage,
140                                GLeglImageOES image_handle)
141 {
142    struct st_context *st = ctx->st;
143    struct pipe_surface *ps;
144    unsigned usage;
145
146    usage = PIPE_BUFFER_USAGE_GPU_READ | PIPE_BUFFER_USAGE_GPU_WRITE;
147    ps = st_manager_get_egl_image_surface(st, (void *) image_handle, usage);
148    if (ps) {
149       st_bind_surface(ctx, target, texObj, texImage, ps);
150       pipe_surface_reference(&ps, NULL);
151    }
152 }
153
154 void
155 st_init_eglimage_functions(struct dd_function_table *functions)
156 {
157    functions->EGLImageTargetTexture2D = st_egl_image_target_texture_2d;
158    functions->EGLImageTargetRenderbufferStorage = st_egl_image_target_renderbuffer_storage;
159 }
160
161 #endif /* FEATURE_OES_EGL_image */