Elim cr-checkbox
[chromium-blink-merge.git] / gpu / command_buffer / service / context_state.h
blobf9d4ff7b1929f1c2cce2a15757558e0375c4793f
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_
10 #include <vector>
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"
20 namespace gpu {
21 namespace gles2 {
23 class Buffer;
24 class ErrorState;
25 class ErrorStateClient;
26 class FeatureInfo;
27 class Framebuffer;
28 class Logger;
29 class Program;
30 class Renderbuffer;
32 // State associated with each texture unit.
33 struct GPU_EXPORT TextureUnit {
34 TextureUnit();
35 ~TextureUnit();
37 // The last target that was bound to this texture unit.
38 GLenum bind_target;
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
44 // glBindTexture
45 scoped_refptr<TextureRef> bound_texture_cube_map;
47 // texture currently bound to this unit's GL_TEXTURE_EXTERNAL_OES with
48 // glBindTexture
49 scoped_refptr<TextureRef> bound_texture_external_oes;
51 // texture currently bound to this unit's GL_TEXTURE_RECTANGLE_ARB with
52 // glBindTexture
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
59 // glBindTexture
60 scoped_refptr<TextureRef> bound_texture_2d_array;
62 scoped_refptr<TextureRef> GetInfoForSamplerType(
63 GLenum type) {
64 DCHECK(type == GL_SAMPLER_2D || type == GL_SAMPLER_CUBE ||
65 type == GL_SAMPLER_EXTERNAL_OES || type == GL_SAMPLER_2D_RECT_ARB);
66 switch (type) {
67 case GL_SAMPLER_2D:
68 return bound_texture_2d;
69 case GL_SAMPLER_CUBE:
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;
75 case GL_SAMPLER_3D:
76 return bound_texture_3d;
77 case GL_SAMPLER_2D_ARRAY:
78 return bound_texture_2d_array;
81 NOTREACHED();
82 return NULL;
86 class GPU_EXPORT Vec4 {
87 public:
88 enum DataType {
89 kFloat,
90 kInt,
91 kUInt,
94 Vec4() {
95 v_[0].float_value = 0.0f;
96 v_[1].float_value = 0.0f;
97 v_[2].float_value = 0.0f;
98 v_[3].float_value = 1.0f;
99 type_ = kFloat;
102 template <typename T>
103 void GetValues(T* values) const;
105 template <typename T>
106 void SetValues(const T* values);
108 DataType type() const {
109 return type_;
112 bool Equal(const Vec4& other) const;
114 private:
115 union ValueUnion {
116 GLfloat float_value;
117 GLint int_value;
118 GLuint uint_value;
121 ValueUnion v_[4];
122 DataType type_;
125 template <>
126 GPU_EXPORT void Vec4::GetValues<GLfloat>(GLfloat* values) const;
127 template <>
128 GPU_EXPORT void Vec4::GetValues<GLint>(GLint* values) const;
129 template <>
130 GPU_EXPORT void Vec4::GetValues<GLuint>(GLuint* values) const;
132 template <>
133 GPU_EXPORT void Vec4::SetValues<GLfloat>(const GLfloat* values);
134 template <>
135 GPU_EXPORT void Vec4::SetValues<GLint>(const GLint* values);
136 template <>
137 GPU_EXPORT void Vec4::SetValues<GLuint>(const GLuint* values);
139 struct GPU_EXPORT ContextState {
140 ContextState(FeatureInfo* feature_info,
141 ErrorStateClient* error_state_client,
142 Logger* logger);
143 ~ContextState();
145 void Initialize();
147 void SetIgnoreCachedStateForTest(bool ignore) {
148 ignore_cached_state = ignore;
151 void RestoreState(const ContextState* prev_state);
152 void InitCapabilities(const ContextState* prev_state) const;
153 void InitState(const ContextState* prev_state) const;
155 void RestoreActiveTexture() const;
156 void RestoreAllTextureUnitBindings(const ContextState* prev_state) const;
157 void RestoreActiveTextureUnitBinding(unsigned int target) const;
158 void RestoreVertexAttribValues() const;
159 void RestoreVertexAttribArrays(
160 const scoped_refptr<VertexAttribManager> attrib_manager) const;
161 void RestoreVertexAttribs() const;
162 void RestoreBufferBindings() const;
163 void RestoreGlobalState(const ContextState* prev_state) const;
164 void RestoreProgramBindings() const;
165 void RestoreRenderbufferBindings();
166 void RestoreTextureUnitBindings(
167 GLuint unit, const ContextState* prev_state) const;
169 // Helper for getting cached state.
170 bool GetStateAsGLint(
171 GLenum pname, GLint* params, GLsizei* num_written) const;
172 bool GetStateAsGLfloat(
173 GLenum pname, GLfloat* params, GLsizei* num_written) const;
174 bool GetEnabled(GLenum cap) const;
176 inline void SetDeviceColorMask(GLboolean red,
177 GLboolean green,
178 GLboolean blue,
179 GLboolean alpha) {
180 if (cached_color_mask_red == red && cached_color_mask_green == green &&
181 cached_color_mask_blue == blue && cached_color_mask_alpha == alpha &&
182 !ignore_cached_state)
183 return;
184 cached_color_mask_red = red;
185 cached_color_mask_green = green;
186 cached_color_mask_blue = blue;
187 cached_color_mask_alpha = alpha;
188 glColorMask(red, green, blue, alpha);
191 inline void SetDeviceDepthMask(GLboolean mask) {
192 if (cached_depth_mask == mask && !ignore_cached_state)
193 return;
194 cached_depth_mask = mask;
195 glDepthMask(mask);
198 inline void SetDeviceStencilMaskSeparate(GLenum op, GLuint mask) {
199 if (op == GL_FRONT) {
200 if (cached_stencil_front_writemask == mask && !ignore_cached_state)
201 return;
202 cached_stencil_front_writemask = mask;
203 } else if (op == GL_BACK) {
204 if (cached_stencil_back_writemask == mask && !ignore_cached_state)
205 return;
206 cached_stencil_back_writemask = mask;
207 } else {
208 NOTREACHED();
209 return;
211 glStencilMaskSeparate(op, mask);
214 ErrorState* GetErrorState();
216 void SetBoundBuffer(GLenum target, Buffer* buffer);
217 void RemoveBoundBuffer(Buffer* buffer);
219 void UnbindTexture(TextureRef* texture);
221 #include "gpu/command_buffer/service/context_state_autogen.h"
223 EnableFlags enable_flags;
225 // Current active texture by 0 - n index.
226 // In other words, if we call glActiveTexture(GL_TEXTURE2) this value would
227 // be 2.
228 GLuint active_texture_unit;
230 // The currently bound array buffer. If this is 0 it is illegal to call
231 // glVertexAttribPointer.
232 scoped_refptr<Buffer> bound_array_buffer;
234 scoped_refptr<Buffer> bound_copy_read_buffer;
235 scoped_refptr<Buffer> bound_copy_write_buffer;
236 scoped_refptr<Buffer> bound_pixel_pack_buffer;
237 scoped_refptr<Buffer> bound_pixel_unpack_buffer;
238 scoped_refptr<Buffer> bound_transform_feedback_buffer;
239 scoped_refptr<Buffer> bound_uniform_buffer;
241 // Which textures are bound to texture units through glActiveTexture.
242 std::vector<TextureUnit> texture_units;
244 // The values for each attrib.
245 std::vector<Vec4> attrib_values;
247 // Class that manages vertex attribs.
248 scoped_refptr<VertexAttribManager> vertex_attrib_manager;
249 scoped_refptr<VertexAttribManager> default_vertex_attrib_manager;
251 // The program in use by glUseProgram
252 scoped_refptr<Program> current_program;
254 // The currently bound renderbuffer
255 scoped_refptr<Renderbuffer> bound_renderbuffer;
256 bool bound_renderbuffer_valid;
258 // The currently bound valuebuffer
259 scoped_refptr<Valuebuffer> bound_valuebuffer;
261 bool pack_reverse_row_order;
262 bool ignore_cached_state;
264 mutable bool fbo_binding_for_scissor_workaround_dirty;
266 private:
267 void EnableDisable(GLenum pname, bool enable) const;
269 FeatureInfo* feature_info_;
270 scoped_ptr<ErrorState> error_state_;
273 } // namespace gles2
274 } // namespace gpu
276 #endif // GPU_COMMAND_BUFFER_SERVICE_CONTEXT_STATE_H_