1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This file contains the ContextState class.
7 #ifndef GPU_COMMAND_BUFFER_SERVICE_CONTEXT_STATE_H_
8 #define GPU_COMMAND_BUFFER_SERVICE_CONTEXT_STATE_H_
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "gpu/command_buffer/service/gl_utils.h"
14 #include "gpu/command_buffer/service/texture_manager.h"
15 #include "gpu/command_buffer/service/valuebuffer_manager.h"
16 #include "gpu/command_buffer/service/vertex_attrib_manager.h"
17 #include "gpu/command_buffer/service/vertex_array_manager.h"
18 #include "gpu/gpu_export.h"
25 class ErrorStateClient
;
32 // State associated with each texture unit.
33 struct GPU_EXPORT TextureUnit
{
37 // The last target that was bound to this texture unit.
40 // texture currently bound to this unit's GL_TEXTURE_2D with glBindTexture
41 scoped_refptr
<TextureRef
> bound_texture_2d
;
43 // texture currently bound to this unit's GL_TEXTURE_CUBE_MAP with
45 scoped_refptr
<TextureRef
> bound_texture_cube_map
;
47 // texture currently bound to this unit's GL_TEXTURE_EXTERNAL_OES with
49 scoped_refptr
<TextureRef
> bound_texture_external_oes
;
51 // texture currently bound to this unit's GL_TEXTURE_RECTANGLE_ARB with
53 scoped_refptr
<TextureRef
> bound_texture_rectangle_arb
;
55 // texture currently bound to this unit's GL_TEXTURE_3D with glBindTexture
56 scoped_refptr
<TextureRef
> bound_texture_3d
;
58 // texture currently bound to this unit's GL_TEXTURE_2D_ARRAY with
60 scoped_refptr
<TextureRef
> bound_texture_2d_array
;
62 scoped_refptr
<TextureRef
> GetInfoForSamplerType(
64 DCHECK(type
== GL_SAMPLER_2D
|| type
== GL_SAMPLER_CUBE
||
65 type
== GL_SAMPLER_EXTERNAL_OES
|| type
== GL_SAMPLER_2D_RECT_ARB
);
68 return bound_texture_2d
;
70 return bound_texture_cube_map
;
71 case GL_SAMPLER_EXTERNAL_OES
:
72 return bound_texture_external_oes
;
73 case GL_SAMPLER_2D_RECT_ARB
:
74 return bound_texture_rectangle_arb
;
76 return bound_texture_3d
;
77 case GL_SAMPLER_2D_ARRAY
:
78 return bound_texture_2d_array
;
85 void Unbind(TextureRef
* texture
) {
86 if (bound_texture_2d
.get() == texture
) {
87 bound_texture_2d
= NULL
;
89 if (bound_texture_cube_map
.get() == texture
) {
90 bound_texture_cube_map
= NULL
;
92 if (bound_texture_external_oes
.get() == texture
) {
93 bound_texture_external_oes
= NULL
;
95 if (bound_texture_3d
.get() == texture
) {
96 bound_texture_3d
= NULL
;
98 if (bound_texture_2d_array
.get() == texture
) {
99 bound_texture_2d_array
= NULL
;
104 class GPU_EXPORT Vec4
{
113 v_
[0].float_value
= 0.0f
;
114 v_
[1].float_value
= 0.0f
;
115 v_
[2].float_value
= 0.0f
;
116 v_
[3].float_value
= 1.0f
;
120 template <typename T
>
121 void GetValues(T
* values
) const;
123 template <typename T
>
124 void SetValues(const T
* values
);
126 DataType
type() const {
130 bool Equal(const Vec4
& other
) const;
144 GPU_EXPORT
void Vec4::GetValues
<GLfloat
>(GLfloat
* values
) const;
146 GPU_EXPORT
void Vec4::GetValues
<GLint
>(GLint
* values
) const;
148 GPU_EXPORT
void Vec4::GetValues
<GLuint
>(GLuint
* values
) const;
151 GPU_EXPORT
void Vec4::SetValues
<GLfloat
>(const GLfloat
* values
);
153 GPU_EXPORT
void Vec4::SetValues
<GLint
>(const GLint
* values
);
155 GPU_EXPORT
void Vec4::SetValues
<GLuint
>(const GLuint
* values
);
157 struct GPU_EXPORT ContextState
{
158 ContextState(FeatureInfo
* feature_info
,
159 ErrorStateClient
* error_state_client
,
165 void SetIgnoreCachedStateForTest(bool ignore
) {
166 ignore_cached_state
= ignore
;
169 void RestoreState(const ContextState
* prev_state
);
170 void InitCapabilities(const ContextState
* prev_state
) const;
171 void InitState(const ContextState
* prev_state
) const;
173 void RestoreActiveTexture() const;
174 void RestoreAllTextureUnitBindings(const ContextState
* prev_state
) const;
175 void RestoreActiveTextureUnitBinding(unsigned int target
) const;
176 void RestoreVertexAttribValues() const;
177 void RestoreVertexAttribArrays(
178 const scoped_refptr
<VertexAttribManager
> attrib_manager
) const;
179 void RestoreVertexAttribs() const;
180 void RestoreBufferBindings() const;
181 void RestoreGlobalState(const ContextState
* prev_state
) const;
182 void RestoreProgramBindings() const;
183 void RestoreRenderbufferBindings();
184 void RestoreTextureUnitBindings(
185 GLuint unit
, const ContextState
* prev_state
) const;
187 // Helper for getting cached state.
188 bool GetStateAsGLint(
189 GLenum pname
, GLint
* params
, GLsizei
* num_written
) const;
190 bool GetStateAsGLfloat(
191 GLenum pname
, GLfloat
* params
, GLsizei
* num_written
) const;
192 bool GetEnabled(GLenum cap
) const;
194 inline void SetDeviceColorMask(GLboolean red
,
198 if (cached_color_mask_red
== red
&& cached_color_mask_green
== green
&&
199 cached_color_mask_blue
== blue
&& cached_color_mask_alpha
== alpha
&&
200 !ignore_cached_state
)
202 cached_color_mask_red
= red
;
203 cached_color_mask_green
= green
;
204 cached_color_mask_blue
= blue
;
205 cached_color_mask_alpha
= alpha
;
206 glColorMask(red
, green
, blue
, alpha
);
209 inline void SetDeviceDepthMask(GLboolean mask
) {
210 if (cached_depth_mask
== mask
&& !ignore_cached_state
)
212 cached_depth_mask
= mask
;
216 inline void SetDeviceStencilMaskSeparate(GLenum op
, GLuint mask
) {
217 if (op
== GL_FRONT
) {
218 if (cached_stencil_front_writemask
== mask
&& !ignore_cached_state
)
220 cached_stencil_front_writemask
= mask
;
221 } else if (op
== GL_BACK
) {
222 if (cached_stencil_back_writemask
== mask
&& !ignore_cached_state
)
224 cached_stencil_back_writemask
= mask
;
229 glStencilMaskSeparate(op
, mask
);
232 ErrorState
* GetErrorState();
234 void SetBoundBuffer(GLenum target
, Buffer
* buffer
);
235 void RemoveBoundBuffer(Buffer
* buffer
);
237 #include "gpu/command_buffer/service/context_state_autogen.h"
239 EnableFlags enable_flags
;
241 // Current active texture by 0 - n index.
242 // In other words, if we call glActiveTexture(GL_TEXTURE2) this value would
244 GLuint active_texture_unit
;
246 // The currently bound array buffer. If this is 0 it is illegal to call
247 // glVertexAttribPointer.
248 scoped_refptr
<Buffer
> bound_array_buffer
;
250 scoped_refptr
<Buffer
> bound_copy_read_buffer
;
251 scoped_refptr
<Buffer
> bound_copy_write_buffer
;
252 scoped_refptr
<Buffer
> bound_pixel_pack_buffer
;
253 scoped_refptr
<Buffer
> bound_pixel_unpack_buffer
;
254 scoped_refptr
<Buffer
> bound_transform_feedback_buffer
;
255 scoped_refptr
<Buffer
> bound_uniform_buffer
;
257 // Which textures are bound to texture units through glActiveTexture.
258 std::vector
<TextureUnit
> texture_units
;
260 // The values for each attrib.
261 std::vector
<Vec4
> attrib_values
;
263 // Class that manages vertex attribs.
264 scoped_refptr
<VertexAttribManager
> vertex_attrib_manager
;
265 scoped_refptr
<VertexAttribManager
> default_vertex_attrib_manager
;
267 // The program in use by glUseProgram
268 scoped_refptr
<Program
> current_program
;
270 // The currently bound renderbuffer
271 scoped_refptr
<Renderbuffer
> bound_renderbuffer
;
272 bool bound_renderbuffer_valid
;
274 // The currently bound valuebuffer
275 scoped_refptr
<Valuebuffer
> bound_valuebuffer
;
277 bool pack_reverse_row_order
;
278 bool ignore_cached_state
;
280 mutable bool fbo_binding_for_scissor_workaround_dirty
;
283 void EnableDisable(GLenum pname
, bool enable
) const;
285 FeatureInfo
* feature_info_
;
286 scoped_ptr
<ErrorState
> error_state_
;
292 #endif // GPU_COMMAND_BUFFER_SERVICE_CONTEXT_STATE_H_