blob: e436e74e280a4a65636beefc1989feadc4d67ee4 [file] [log] [blame]
[email protected]e259eb412012-10-13 05:47:241// 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.
4
5// This file contains the ContextState class.
6
7#ifndef GPU_COMMAND_BUFFER_SERVICE_CONTEXT_STATE_H_
8#define GPU_COMMAND_BUFFER_SERVICE_CONTEXT_STATE_H_
9
[email protected]1868a342012-11-07 15:56:0210#include <vector>
[email protected]e259eb412012-10-13 05:47:2411#include "base/logging.h"
[email protected]d3eba342013-04-18 21:11:5012#include "base/memory/scoped_ptr.h"
[email protected]e259eb412012-10-13 05:47:2413#include "gpu/command_buffer/service/gl_utils.h"
[email protected]e259eb412012-10-13 05:47:2414#include "gpu/command_buffer/service/query_manager.h"
[email protected]e259eb412012-10-13 05:47:2415#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"
[email protected]88a61bf2012-10-27 13:00:4218#include "gpu/gpu_export.h"
[email protected]e259eb412012-10-13 05:47:2419
20namespace gpu {
21namespace gles2 {
22
[email protected]31494b82013-02-28 10:10:2623class Buffer;
[email protected]d3eba342013-04-18 21:11:5024class ErrorState;
[email protected]828a3932014-04-02 14:43:1325class ErrorStateClient;
[email protected]b3cbad12012-12-05 19:56:3626class FeatureInfo;
[email protected]31494b82013-02-28 10:10:2627class Framebuffer;
28class Program;
29class Renderbuffer;
[email protected]b3cbad12012-12-05 19:56:3630
[email protected]e259eb412012-10-13 05:47:2431// State associated with each texture unit.
[email protected]88a61bf2012-10-27 13:00:4232struct GPU_EXPORT TextureUnit {
[email protected]e259eb412012-10-13 05:47:2433 TextureUnit();
34 ~TextureUnit();
35
36 // The last target that was bound to this texture unit.
37 GLenum bind_target;
38
39 // texture currently bound to this unit's GL_TEXTURE_2D with glBindTexture
[email protected]370eaf12013-05-18 09:19:4940 scoped_refptr<TextureRef> bound_texture_2d;
[email protected]e259eb412012-10-13 05:47:2441
42 // texture currently bound to this unit's GL_TEXTURE_CUBE_MAP with
43 // glBindTexture
[email protected]370eaf12013-05-18 09:19:4944 scoped_refptr<TextureRef> bound_texture_cube_map;
[email protected]e259eb412012-10-13 05:47:2445
46 // texture currently bound to this unit's GL_TEXTURE_EXTERNAL_OES with
47 // glBindTexture
[email protected]370eaf12013-05-18 09:19:4948 scoped_refptr<TextureRef> bound_texture_external_oes;
[email protected]e259eb412012-10-13 05:47:2449
50 // texture currently bound to this unit's GL_TEXTURE_RECTANGLE_ARB with
51 // glBindTexture
[email protected]370eaf12013-05-18 09:19:4952 scoped_refptr<TextureRef> bound_texture_rectangle_arb;
[email protected]e259eb412012-10-13 05:47:2453
[email protected]370eaf12013-05-18 09:19:4954 scoped_refptr<TextureRef> GetInfoForSamplerType(
[email protected]ed9f9cd2013-02-27 21:12:3555 GLenum type) {
[email protected]e259eb412012-10-13 05:47:2456 DCHECK(type == GL_SAMPLER_2D || type == GL_SAMPLER_CUBE ||
57 type == GL_SAMPLER_EXTERNAL_OES || type == GL_SAMPLER_2D_RECT_ARB);
58 switch (type) {
59 case GL_SAMPLER_2D:
60 return bound_texture_2d;
61 case GL_SAMPLER_CUBE:
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;
67 }
68
69 NOTREACHED();
70 return NULL;
71 }
72
[email protected]370eaf12013-05-18 09:19:4973 void Unbind(TextureRef* texture) {
[email protected]7cd76fd2013-06-02 21:11:1174 if (bound_texture_2d.get() == texture) {
[email protected]e259eb412012-10-13 05:47:2475 bound_texture_2d = NULL;
76 }
[email protected]7cd76fd2013-06-02 21:11:1177 if (bound_texture_cube_map.get() == texture) {
[email protected]e259eb412012-10-13 05:47:2478 bound_texture_cube_map = NULL;
79 }
[email protected]7cd76fd2013-06-02 21:11:1180 if (bound_texture_external_oes.get() == texture) {
[email protected]e259eb412012-10-13 05:47:2481 bound_texture_external_oes = NULL;
82 }
83 }
84};
85
[email protected]af6380962012-11-29 23:24:1386struct Vec4 {
87 Vec4() {
88 v[0] = 0.0f;
89 v[1] = 0.0f;
90 v[2] = 0.0f;
91 v[3] = 1.0f;
92 }
93 float v[4];
94};
[email protected]e259eb412012-10-13 05:47:2495
[email protected]88a61bf2012-10-27 13:00:4296struct GPU_EXPORT ContextState {
[email protected]828a3932014-04-02 14:43:1397 ContextState(FeatureInfo* feature_info,
98 ErrorStateClient* error_state_client,
99 Logger* logger);
[email protected]e259eb412012-10-13 05:47:24100 ~ContextState();
101
[email protected]f731b9462012-10-30 00:35:22102 void Initialize();
103
[email protected]454157e2014-05-03 02:49:45104 void SetIgnoreCachedStateForTest(bool ignore) {
105 ignore_cached_state = ignore;
106 }
107
[email protected]5baa86bc2014-01-16 04:33:16108 void RestoreState(const ContextState* prev_state) const;
[email protected]88ba52f2014-04-09 12:39:34109 void InitCapabilities(const ContextState* prev_state) const;
110 void InitState(const ContextState* prev_state) const;
[email protected]f731b9462012-10-30 00:35:22111
[email protected]29a4d902013-02-26 20:18:06112 void RestoreActiveTexture() const;
[email protected]5baa86bc2014-01-16 04:33:16113 void RestoreAllTextureUnitBindings(const ContextState* prev_state) const;
[email protected]4b2d2b262014-03-21 22:05:27114 void RestoreActiveTextureUnitBinding(unsigned int target) const;
[email protected]81f20a622014-04-18 01:54:52115 void RestoreVertexAttribValues() const;
116 void RestoreVertexAttribArrays(
117 const scoped_refptr<VertexAttribManager> attrib_manager) const;
118 void RestoreVertexAttribs() const;
[email protected]29a4d902013-02-26 20:18:06119 void RestoreBufferBindings() const;
[email protected]88ba52f2014-04-09 12:39:34120 void RestoreGlobalState(const ContextState* prev_state) const;
[email protected]29a4d902013-02-26 20:18:06121 void RestoreProgramBindings() const;
122 void RestoreRenderbufferBindings() const;
[email protected]5baa86bc2014-01-16 04:33:16123 void RestoreTextureUnitBindings(
124 GLuint unit, const ContextState* prev_state) const;
[email protected]29a4d902013-02-26 20:18:06125
[email protected]d058bca2012-11-26 10:27:26126 // 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;
132
[email protected]454157e2014-05-03 02:49:45133 inline void SetDeviceColorMask(GLboolean red,
134 GLboolean green,
135 GLboolean blue,
136 GLboolean alpha) {
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)
140 return;
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);
146 }
147
148 inline void SetDeviceDepthMask(GLboolean mask) {
149 if (cached_depth_mask == mask && !ignore_cached_state)
150 return;
151 cached_depth_mask = mask;
152 glDepthMask(mask);
153 }
154
155 inline void SetDeviceStencilMaskSeparate(GLenum op, GLuint mask) {
156 if (op == GL_FRONT) {
157 if (cached_stencil_front_writemask == mask && !ignore_cached_state)
158 return;
159 cached_stencil_front_writemask = mask;
160 } else if (op == GL_BACK) {
161 if (cached_stencil_back_writemask == mask && !ignore_cached_state)
162 return;
163 cached_stencil_back_writemask = mask;
164 } else {
165 NOTREACHED();
166 return;
167 }
168 glStencilMaskSeparate(op, mask);
169 }
170
[email protected]d3eba342013-04-18 21:11:50171 ErrorState* GetErrorState();
172
[email protected]f731b9462012-10-30 00:35:22173 #include "gpu/command_buffer/service/context_state_autogen.h"
174
175 EnableFlags enable_flags;
176
[email protected]e259eb412012-10-13 05:47:24177 // Current active texture by 0 - n index.
178 // In other words, if we call glActiveTexture(GL_TEXTURE2) this value would
179 // be 2.
180 GLuint active_texture_unit;
181
[email protected]e259eb412012-10-13 05:47:24182 // The currently bound array buffer. If this is 0 it is illegal to call
183 // glVertexAttribPointer.
[email protected]16ccec12013-02-28 03:40:21184 scoped_refptr<Buffer> bound_array_buffer;
[email protected]e259eb412012-10-13 05:47:24185
186 // Which textures are bound to texture units through glActiveTexture.
[email protected]1868a342012-11-07 15:56:02187 std::vector<TextureUnit> texture_units;
[email protected]e259eb412012-10-13 05:47:24188
[email protected]af6380962012-11-29 23:24:13189 // The values for each attrib.
190 std::vector<Vec4> attrib_values;
191
[email protected]e259eb412012-10-13 05:47:24192 // Class that manages vertex attribs.
[email protected]ed9f9cd2013-02-27 21:12:35193 scoped_refptr<VertexAttribManager> vertex_attrib_manager;
[email protected]81f20a622014-04-18 01:54:52194 scoped_refptr<VertexAttribManager> default_vertex_attrib_manager;
[email protected]e259eb412012-10-13 05:47:24195
196 // The program in use by glUseProgram
[email protected]ed9f9cd2013-02-27 21:12:35197 scoped_refptr<Program> current_program;
[email protected]e259eb412012-10-13 05:47:24198
[email protected]e259eb412012-10-13 05:47:24199 // The currently bound renderbuffer
[email protected]ed9f9cd2013-02-27 21:12:35200 scoped_refptr<Renderbuffer> bound_renderbuffer;
[email protected]e259eb412012-10-13 05:47:24201
[email protected]8ebd46c2014-01-08 12:06:13202 // A map of of target -> Query for current queries
203 typedef std::map<GLuint, scoped_refptr<QueryManager::Query> > QueryMap;
204 QueryMap current_queries;
[email protected]e259eb412012-10-13 05:47:24205
[email protected]e259eb412012-10-13 05:47:24206 bool pack_reverse_row_order;
[email protected]454157e2014-05-03 02:49:45207 bool ignore_cached_state;
[email protected]b3cbad12012-12-05 19:56:36208
[email protected]28718a92013-04-04 12:12:51209 mutable bool fbo_binding_for_scissor_workaround_dirty_;
[email protected]b3cbad12012-12-05 19:56:36210 FeatureInfo* feature_info_;
[email protected]d3eba342013-04-18 21:11:50211
212 private:
213 scoped_ptr<ErrorState> error_state_;
[email protected]e259eb412012-10-13 05:47:24214};
215
216} // namespace gles2
217} // namespace gpu
218
219#endif // GPU_COMMAND_BUFFER_SERVICE_CONTEXT_STATE_H_
220