2 * Mesa 3-D graphics library
5 * Copyright (C) 2010 LunarG Inc.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
26 * Chia-I Wu <olv@lunarg.com>
29 #include "main/mfeatures.h"
30 #include "main/texobj.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_context.h"
37 #include "st_texture.h"
38 #include "st_format.h"
39 #include "st_manager.h"
42 * Return the base format just like _mesa_base_fbo_format does.
45 st_pipe_format_to_base_format(enum pipe_format format
)
49 if (util_format_is_depth_or_stencil(format
)) {
50 if (util_format_is_depth_and_stencil(format
)) {
51 base_format
= GL_DEPTH_STENCIL
;
54 if (format
== PIPE_FORMAT_S8_UINT
)
55 base_format
= GL_STENCIL_INDEX
;
57 base_format
= GL_DEPTH_COMPONENT
;
62 if (util_format_has_alpha(format
))
63 base_format
= GL_RGBA
;
72 st_egl_image_target_renderbuffer_storage(struct gl_context
*ctx
,
73 struct gl_renderbuffer
*rb
,
74 GLeglImageOES image_handle
)
76 struct st_context
*st
= st_context(ctx
);
77 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
78 struct pipe_surface
*ps
;
80 ps
= st_manager_get_egl_image_surface(st
, (void *) image_handle
);
82 strb
->Base
.Width
= ps
->width
;
83 strb
->Base
.Height
= ps
->height
;
84 strb
->Base
.Format
= st_pipe_format_to_mesa_format(ps
->format
);
85 strb
->Base
._BaseFormat
= st_pipe_format_to_base_format(ps
->format
);
86 strb
->Base
.InternalFormat
= strb
->Base
._BaseFormat
;
88 pipe_surface_reference(&strb
->surface
, ps
);
89 pipe_resource_reference(&strb
->texture
, ps
->texture
);
91 pipe_surface_reference(&ps
, NULL
);
96 st_bind_surface(struct gl_context
*ctx
, GLenum target
,
97 struct gl_texture_object
*texObj
,
98 struct gl_texture_image
*texImage
,
99 struct pipe_surface
*ps
)
101 struct st_texture_object
*stObj
;
102 struct st_texture_image
*stImage
;
103 GLenum internalFormat
;
106 /* map pipe format to base format */
107 if (util_format_get_component_bits(ps
->format
, UTIL_FORMAT_COLORSPACE_RGB
, 3) > 0)
108 internalFormat
= GL_RGBA
;
110 internalFormat
= GL_RGB
;
112 stObj
= st_texture_object(texObj
);
113 stImage
= st_texture_image(texImage
);
115 /* switch to surface based */
116 if (!stObj
->surface_based
) {
117 _mesa_clear_texture_object(ctx
, texObj
);
118 stObj
->surface_based
= GL_TRUE
;
121 texFormat
= st_pipe_format_to_mesa_format(ps
->format
);
123 _mesa_init_teximage_fields(ctx
, texImage
,
124 ps
->width
, ps
->height
, 1, 0, internalFormat
,
127 /* FIXME create a non-default sampler view from the pipe_surface? */
128 pipe_resource_reference(&stObj
->pt
, ps
->texture
);
129 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
130 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
132 stObj
->width0
= ps
->width
;
133 stObj
->height0
= ps
->height
;
136 _mesa_dirty_texobj(ctx
, texObj
, GL_TRUE
);
140 st_egl_image_target_texture_2d(struct gl_context
*ctx
, GLenum target
,
141 struct gl_texture_object
*texObj
,
142 struct gl_texture_image
*texImage
,
143 GLeglImageOES image_handle
)
145 struct st_context
*st
= st_context(ctx
);
146 struct pipe_surface
*ps
;
148 ps
= st_manager_get_egl_image_surface(st
, (void *) image_handle
);
150 st_bind_surface(ctx
, target
, texObj
, texImage
, ps
);
151 pipe_surface_reference(&ps
, NULL
);
156 st_init_eglimage_functions(struct dd_function_table
*functions
)
158 functions
->EGLImageTargetTexture2D
= st_egl_image_target_texture_2d
;
159 functions
->EGLImageTargetRenderbufferStorage
= st_egl_image_target_renderbuffer_storage
;