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/query_manager.h"
15 #include "gpu/command_buffer/service/texture_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;
31 // State associated with each texture unit.
32 struct GPU_EXPORT TextureUnit {
36 // The last target that was bound to this texture unit.
39 // texture currently bound to this unit's GL_TEXTURE_2D with glBindTexture
40 scoped_refptr<TextureRef> bound_texture_2d;
42 // texture currently bound to this unit's GL_TEXTURE_CUBE_MAP with
44 scoped_refptr<TextureRef> bound_texture_cube_map;
46 // texture currently bound to this unit's GL_TEXTURE_EXTERNAL_OES with
48 scoped_refptr<TextureRef> bound_texture_external_oes;
50 // texture currently bound to this unit's GL_TEXTURE_RECTANGLE_ARB with
52 scoped_refptr<TextureRef> bound_texture_rectangle_arb;
54 scoped_refptr<TextureRef> GetInfoForSamplerType(
56 DCHECK(type == GL_SAMPLER_2D || type == GL_SAMPLER_CUBE ||
57 type == GL_SAMPLER_EXTERNAL_OES || type == GL_SAMPLER_2D_RECT_ARB);
60 return bound_texture_2d;
62 return bound_texture_cube_map;
63 case GL_SAMPLER_EXTERNAL_OES:
64 return bound_texture_external_oes;
65 case GL_SAMPLER_2D_RECT_ARB:
66 return bound_texture_rectangle_arb;
73 void Unbind(TextureRef* texture) {
74 if (bound_texture_2d.get() == texture) {
75 bound_texture_2d = NULL;
77 if (bound_texture_cube_map.get() == texture) {
78 bound_texture_cube_map = NULL;
80 if (bound_texture_external_oes.get() == texture) {
81 bound_texture_external_oes = NULL;
96 struct GPU_EXPORT ContextState {
97 ContextState(FeatureInfo* feature_info,
98 ErrorStateClient* error_state_client,
104 void SetIgnoreCachedStateForTest(bool ignore) {
105 ignore_cached_state = ignore;
108 void RestoreState(const ContextState* prev_state) const;
109 void InitCapabilities(const ContextState* prev_state) const;
110 void InitState(const ContextState* prev_state) const;
112 void RestoreActiveTexture() const;
113 void RestoreAllTextureUnitBindings(const ContextState* prev_state) const;
114 void RestoreActiveTextureUnitBinding(unsigned int target) const;
115 void RestoreVertexAttribValues() const;
116 void RestoreVertexAttribArrays(
117 const scoped_refptr<VertexAttribManager> attrib_manager) const;
118 void RestoreVertexAttribs() const;
119 void RestoreBufferBindings() const;
120 void RestoreGlobalState(const ContextState* prev_state) const;
121 void RestoreProgramBindings() const;
122 void RestoreRenderbufferBindings() const;
123 void RestoreTextureUnitBindings(
124 GLuint unit, const ContextState* prev_state) const;
126 // Helper for getting cached state.
127 bool GetStateAsGLint(
128 GLenum pname, GLint* params, GLsizei* num_written) const;
129 bool GetStateAsGLfloat(
130 GLenum pname, GLfloat* params, GLsizei* num_written) const;
131 bool GetEnabled(GLenum cap) const;
133 inline void SetDeviceColorMask(GLboolean red,
137 if (cached_color_mask_red == red && cached_color_mask_green == green &&
138 cached_color_mask_blue == blue && cached_color_mask_alpha == alpha &&
139 !ignore_cached_state)
141 cached_color_mask_red = red;
142 cached_color_mask_green = green;
143 cached_color_mask_blue = blue;
144 cached_color_mask_alpha = alpha;
145 glColorMask(red, green, blue, alpha);
148 inline void SetDeviceDepthMask(GLboolean mask) {
149 if (cached_depth_mask == mask && !ignore_cached_state)
151 cached_depth_mask = mask;
155 inline void SetDeviceStencilMaskSeparate(GLenum op, GLuint mask) {
156 if (op == GL_FRONT) {
157 if (cached_stencil_front_writemask == mask && !ignore_cached_state)
159 cached_stencil_front_writemask = mask;
160 } else if (op == GL_BACK) {
161 if (cached_stencil_back_writemask == mask && !ignore_cached_state)
163 cached_stencil_back_writemask = mask;
168 glStencilMaskSeparate(op, mask);
171 ErrorState* GetErrorState();
173 #include "gpu/command_buffer/service/context_state_autogen.h"
175 EnableFlags enable_flags;
177 // Current active texture by 0 - n index.
178 // In other words, if we call glActiveTexture(GL_TEXTURE2) this value would
180 GLuint active_texture_unit;
182 // The currently bound array buffer. If this is 0 it is illegal to call
183 // glVertexAttribPointer.
184 scoped_refptr<Buffer> bound_array_buffer;
186 // Which textures are bound to texture units through glActiveTexture.
187 std::vector<TextureUnit> texture_units;
189 // The values for each attrib.
190 std::vector<Vec4> attrib_values;
192 // Class that manages vertex attribs.
193 scoped_refptr<VertexAttribManager> vertex_attrib_manager;
194 scoped_refptr<VertexAttribManager> default_vertex_attrib_manager;
196 // The program in use by glUseProgram
197 scoped_refptr<Program> current_program;
199 // The currently bound renderbuffer
200 scoped_refptr<Renderbuffer> bound_renderbuffer;
202 // A map of of target -> Query for current queries
203 typedef std::map<GLuint, scoped_refptr<QueryManager::Query> > QueryMap;
204 QueryMap current_queries;
206 bool pack_reverse_row_order;
207 bool ignore_cached_state;
209 mutable bool fbo_binding_for_scissor_workaround_dirty_;
210 FeatureInfo* feature_info_;
213 scoped_ptr<ErrorState> error_state_;
219 #endif // GPU_COMMAND_BUFFER_SERVICE_CONTEXT_STATE_H_