Merge branch 'gallium_draw_llvm'
[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
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
26 */
27
28 #include "main/texobj.h"
29 #include "main/texfetch.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_texture.h"
36 #include "st_format.h"
37 #include "st_manager.h"
38
39 #if FEATURE_OES_EGL_image
40
41 /**
42 * Return the base format just like _mesa_base_fbo_format does.
43 */
44 static GLenum
45 st_pipe_format_to_base_format(enum pipe_format format)
46 {
47 GLenum base_format;
48
49 if (util_format_is_depth_or_stencil(format)) {
50 if (util_format_is_depth_and_stencil(format)) {
51 base_format = GL_DEPTH_STENCIL;
52 }
53 else {
54 if (format == PIPE_FORMAT_S8_USCALED)
55 base_format = GL_STENCIL_INDEX;
56 else
57 base_format = GL_DEPTH_COMPONENT;
58 }
59 }
60 else {
61 /* is this enough? */
62 if (util_format_has_alpha(format))
63 base_format = GL_RGBA;
64 else
65 base_format = GL_RGB;
66 }
67
68 return base_format;
69 }
70
71 static void
72 st_egl_image_target_renderbuffer_storage(GLcontext *ctx,
73 struct gl_renderbuffer *rb,
74 GLeglImageOES image_handle)
75 {
76 struct st_context *st = ctx->st;
77 struct st_renderbuffer *strb = st_renderbuffer(rb);
78 struct pipe_surface *ps;
79 unsigned usage;
80
81 usage = PIPE_BUFFER_USAGE_GPU_READ | PIPE_BUFFER_USAGE_GPU_WRITE;
82 ps = st_manager_get_egl_image_surface(st, (void *) image_handle, usage);
83 if (ps) {
84 strb->Base.Width = ps->width;
85 strb->Base.Height = ps->height;
86 strb->Base.Format = st_pipe_format_to_mesa_format(ps->format);
87 strb->Base.DataType = st_format_datatype(ps->format);
88 strb->Base._BaseFormat = st_pipe_format_to_base_format(ps->format);
89 strb->Base.InternalFormat = strb->Base._BaseFormat;
90
91 pipe_surface_reference(&strb->surface, ps);
92 pipe_texture_reference(&strb->texture, ps->texture);
93
94 pipe_surface_reference(&ps, NULL);
95 }
96 }
97
98 static void
99 st_bind_surface(GLcontext *ctx, GLenum target,
100 struct gl_texture_object *texObj,
101 struct gl_texture_image *texImage,
102 struct pipe_surface *ps)
103 {
104 struct st_texture_object *stObj;
105 struct st_texture_image *stImage;
106 GLenum internalFormat;
107
108 /* map pipe format to base format */
109 if (util_format_get_component_bits(ps->format, UTIL_FORMAT_COLORSPACE_RGB, 3) > 0)
110 internalFormat = GL_RGBA;
111 else
112 internalFormat = GL_RGB;
113
114 stObj = st_texture_object(texObj);
115 stImage = st_texture_image(texImage);
116
117 /* switch to surface based */
118 if (!stObj->surface_based) {
119 _mesa_clear_texture_object(ctx, texObj);
120 stObj->surface_based = GL_TRUE;
121 }
122
123 _mesa_init_teximage_fields(ctx, target, texImage,
124 ps->width, ps->height, 1, 0, internalFormat);
125 texImage->TexFormat = st_pipe_format_to_mesa_format(ps->format);
126 _mesa_set_fetch_functions(texImage, 2);
127
128 stObj->pipe = ctx->st->pipe;
129 /* FIXME create a non-default sampler view from the pipe_surface? */
130 pipe_texture_reference(&stImage->pt, ps->texture);
131
132 _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
133 }
134
135 static void
136 st_egl_image_target_texture_2d(GLcontext *ctx, GLenum target,
137 struct gl_texture_object *texObj,
138 struct gl_texture_image *texImage,
139 GLeglImageOES image_handle)
140 {
141 struct st_context *st = ctx->st;
142 struct pipe_surface *ps;
143 unsigned usage;
144
145 usage = PIPE_BUFFER_USAGE_GPU_READ | PIPE_BUFFER_USAGE_GPU_WRITE;
146 ps = st_manager_get_egl_image_surface(st, (void *) image_handle, usage);
147 if (ps) {
148 st_bind_surface(ctx, target, texObj, texImage, ps);
149 pipe_surface_reference(&ps, NULL);
150 }
151 }
152
153 void
154 st_init_eglimage_functions(struct dd_function_table *functions)
155 {
156 functions->EGLImageTargetTexture2D = st_egl_image_target_texture_2d;
157 functions->EGLImageTargetRenderbufferStorage = st_egl_image_target_renderbuffer_storage;
158 }
159
160 #endif /* FEATURE_OES_EGL_image */