blob: 32bef4cea3ec3f9099eb087b40090f22f2ec41bb [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"
orglofchcad5a6742014-11-07 19:51:1216#include "gpu/command_buffer/service/valuebuffer_manager.h"
[email protected]e259eb412012-10-13 05:47:2417#include "gpu/command_buffer/service/vertex_attrib_manager.h"
18#include "gpu/command_buffer/service/vertex_array_manager.h"
[email protected]88a61bf2012-10-27 13:00:4219#include "gpu/gpu_export.h"
[email protected]e259eb412012-10-13 05:47:2420
21namespace gpu {
22namespace gles2 {
23
[email protected]31494b82013-02-28 10:10:2624class Buffer;
[email protected]d3eba342013-04-18 21:11:5025class ErrorState;
[email protected]828a3932014-04-02 14:43:1326class ErrorStateClient;
[email protected]b3cbad12012-12-05 19:56:3627class FeatureInfo;
[email protected]31494b82013-02-28 10:10:2628class Framebuffer;
29class Program;
30class Renderbuffer;
[email protected]b3cbad12012-12-05 19:56:3631
[email protected]e259eb412012-10-13 05:47:2432// State associated with each texture unit.
[email protected]88a61bf2012-10-27 13:00:4233struct GPU_EXPORT TextureUnit {
[email protected]e259eb412012-10-13 05:47:2434 TextureUnit();
35 ~TextureUnit();
36
37 // The last target that was bound to this texture unit.
38 GLenum bind_target;
39
40 // texture currently bound to this unit's GL_TEXTURE_2D with glBindTexture
[email protected]370eaf12013-05-18 09:19:4941 scoped_refptr<TextureRef> bound_texture_2d;
[email protected]e259eb412012-10-13 05:47:2442
43 // texture currently bound to this unit's GL_TEXTURE_CUBE_MAP with
44 // glBindTexture
[email protected]370eaf12013-05-18 09:19:4945 scoped_refptr<TextureRef> bound_texture_cube_map;
[email protected]e259eb412012-10-13 05:47:2446
47 // texture currently bound to this unit's GL_TEXTURE_EXTERNAL_OES with
48 // glBindTexture
[email protected]370eaf12013-05-18 09:19:4949 scoped_refptr<TextureRef> bound_texture_external_oes;
[email protected]e259eb412012-10-13 05:47:2450
51 // texture currently bound to this unit's GL_TEXTURE_RECTANGLE_ARB with
52 // glBindTexture
[email protected]370eaf12013-05-18 09:19:4953 scoped_refptr<TextureRef> bound_texture_rectangle_arb;
[email protected]e259eb412012-10-13 05:47:2454
zmoea06a6f2015-04-30 01:15:4655 // texture currently bound to this unit's GL_TEXTURE_3D with glBindTexture
56 scoped_refptr<TextureRef> bound_texture_3d;
57
58 // texture currently bound to this unit's GL_TEXTURE_2D_ARRAY with
59 // glBindTexture
60 scoped_refptr<TextureRef> bound_texture_2d_array;
61
[email protected]370eaf12013-05-18 09:19:4962 scoped_refptr<TextureRef> GetInfoForSamplerType(
[email protected]ed9f9cd2013-02-27 21:12:3563 GLenum type) {
[email protected]e259eb412012-10-13 05:47:2464 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;
zmoea06a6f2015-04-30 01:15:4675 case GL_SAMPLER_3D:
76 return bound_texture_3d;
77 case GL_SAMPLER_2D_ARRAY:
78 return bound_texture_2d_array;
[email protected]e259eb412012-10-13 05:47:2479 }
80
81 NOTREACHED();
82 return NULL;
83 }
84
[email protected]370eaf12013-05-18 09:19:4985 void Unbind(TextureRef* texture) {
[email protected]7cd76fd2013-06-02 21:11:1186 if (bound_texture_2d.get() == texture) {
[email protected]e259eb412012-10-13 05:47:2487 bound_texture_2d = NULL;
88 }
[email protected]7cd76fd2013-06-02 21:11:1189 if (bound_texture_cube_map.get() == texture) {
[email protected]e259eb412012-10-13 05:47:2490 bound_texture_cube_map = NULL;
91 }
[email protected]7cd76fd2013-06-02 21:11:1192 if (bound_texture_external_oes.get() == texture) {
[email protected]e259eb412012-10-13 05:47:2493 bound_texture_external_oes = NULL;
94 }
zmoea06a6f2015-04-30 01:15:4695 if (bound_texture_3d.get() == texture) {
96 bound_texture_3d = NULL;
97 }
98 if (bound_texture_2d_array.get() == texture) {
99 bound_texture_2d_array = NULL;
100 }
[email protected]e259eb412012-10-13 05:47:24101 }
102};
103
[email protected]af6380962012-11-29 23:24:13104struct Vec4 {
105 Vec4() {
106 v[0] = 0.0f;
107 v[1] = 0.0f;
108 v[2] = 0.0f;
109 v[3] = 1.0f;
110 }
111 float v[4];
112};
[email protected]e259eb412012-10-13 05:47:24113
[email protected]88a61bf2012-10-27 13:00:42114struct GPU_EXPORT ContextState {
[email protected]828a3932014-04-02 14:43:13115 ContextState(FeatureInfo* feature_info,
116 ErrorStateClient* error_state_client,
117 Logger* logger);
[email protected]e259eb412012-10-13 05:47:24118 ~ContextState();
119
[email protected]f731b9462012-10-30 00:35:22120 void Initialize();
121
[email protected]454157e2014-05-03 02:49:45122 void SetIgnoreCachedStateForTest(bool ignore) {
123 ignore_cached_state = ignore;
124 }
125
[email protected]8875a5f2014-06-27 08:33:47126 void RestoreState(const ContextState* prev_state);
[email protected]88ba52f2014-04-09 12:39:34127 void InitCapabilities(const ContextState* prev_state) const;
128 void InitState(const ContextState* prev_state) const;
[email protected]f731b9462012-10-30 00:35:22129
[email protected]29a4d902013-02-26 20:18:06130 void RestoreActiveTexture() const;
[email protected]5baa86bc2014-01-16 04:33:16131 void RestoreAllTextureUnitBindings(const ContextState* prev_state) const;
[email protected]4b2d2b262014-03-21 22:05:27132 void RestoreActiveTextureUnitBinding(unsigned int target) const;
[email protected]81f20a622014-04-18 01:54:52133 void RestoreVertexAttribValues() const;
134 void RestoreVertexAttribArrays(
135 const scoped_refptr<VertexAttribManager> attrib_manager) const;
136 void RestoreVertexAttribs() const;
[email protected]29a4d902013-02-26 20:18:06137 void RestoreBufferBindings() const;
[email protected]88ba52f2014-04-09 12:39:34138 void RestoreGlobalState(const ContextState* prev_state) const;
[email protected]29a4d902013-02-26 20:18:06139 void RestoreProgramBindings() const;
[email protected]8875a5f2014-06-27 08:33:47140 void RestoreRenderbufferBindings();
[email protected]5baa86bc2014-01-16 04:33:16141 void RestoreTextureUnitBindings(
142 GLuint unit, const ContextState* prev_state) const;
[email protected]29a4d902013-02-26 20:18:06143
[email protected]d058bca2012-11-26 10:27:26144 // Helper for getting cached state.
145 bool GetStateAsGLint(
146 GLenum pname, GLint* params, GLsizei* num_written) const;
147 bool GetStateAsGLfloat(
148 GLenum pname, GLfloat* params, GLsizei* num_written) const;
149 bool GetEnabled(GLenum cap) const;
150
[email protected]454157e2014-05-03 02:49:45151 inline void SetDeviceColorMask(GLboolean red,
152 GLboolean green,
153 GLboolean blue,
154 GLboolean alpha) {
155 if (cached_color_mask_red == red && cached_color_mask_green == green &&
156 cached_color_mask_blue == blue && cached_color_mask_alpha == alpha &&
157 !ignore_cached_state)
158 return;
159 cached_color_mask_red = red;
160 cached_color_mask_green = green;
161 cached_color_mask_blue = blue;
162 cached_color_mask_alpha = alpha;
163 glColorMask(red, green, blue, alpha);
164 }
165
166 inline void SetDeviceDepthMask(GLboolean mask) {
167 if (cached_depth_mask == mask && !ignore_cached_state)
168 return;
169 cached_depth_mask = mask;
170 glDepthMask(mask);
171 }
172
173 inline void SetDeviceStencilMaskSeparate(GLenum op, GLuint mask) {
174 if (op == GL_FRONT) {
175 if (cached_stencil_front_writemask == mask && !ignore_cached_state)
176 return;
177 cached_stencil_front_writemask = mask;
178 } else if (op == GL_BACK) {
179 if (cached_stencil_back_writemask == mask && !ignore_cached_state)
180 return;
181 cached_stencil_back_writemask = mask;
182 } else {
183 NOTREACHED();
184 return;
185 }
186 glStencilMaskSeparate(op, mask);
187 }
188
[email protected]d3eba342013-04-18 21:11:50189 ErrorState* GetErrorState();
190
[email protected]f731b9462012-10-30 00:35:22191 #include "gpu/command_buffer/service/context_state_autogen.h"
192
193 EnableFlags enable_flags;
194
[email protected]e259eb412012-10-13 05:47:24195 // Current active texture by 0 - n index.
196 // In other words, if we call glActiveTexture(GL_TEXTURE2) this value would
197 // be 2.
198 GLuint active_texture_unit;
199
[email protected]e259eb412012-10-13 05:47:24200 // The currently bound array buffer. If this is 0 it is illegal to call
201 // glVertexAttribPointer.
[email protected]16ccec12013-02-28 03:40:21202 scoped_refptr<Buffer> bound_array_buffer;
[email protected]e259eb412012-10-13 05:47:24203
204 // Which textures are bound to texture units through glActiveTexture.
[email protected]1868a342012-11-07 15:56:02205 std::vector<TextureUnit> texture_units;
[email protected]e259eb412012-10-13 05:47:24206
[email protected]af6380962012-11-29 23:24:13207 // The values for each attrib.
208 std::vector<Vec4> attrib_values;
209
[email protected]e259eb412012-10-13 05:47:24210 // Class that manages vertex attribs.
[email protected]ed9f9cd2013-02-27 21:12:35211 scoped_refptr<VertexAttribManager> vertex_attrib_manager;
[email protected]81f20a622014-04-18 01:54:52212 scoped_refptr<VertexAttribManager> default_vertex_attrib_manager;
[email protected]e259eb412012-10-13 05:47:24213
214 // The program in use by glUseProgram
[email protected]ed9f9cd2013-02-27 21:12:35215 scoped_refptr<Program> current_program;
[email protected]e259eb412012-10-13 05:47:24216
[email protected]e259eb412012-10-13 05:47:24217 // The currently bound renderbuffer
[email protected]ed9f9cd2013-02-27 21:12:35218 scoped_refptr<Renderbuffer> bound_renderbuffer;
[email protected]8875a5f2014-06-27 08:33:47219 bool bound_renderbuffer_valid;
[email protected]e259eb412012-10-13 05:47:24220
orglofchcad5a6742014-11-07 19:51:12221 // The currently bound valuebuffer
222 scoped_refptr<Valuebuffer> bound_valuebuffer;
223
[email protected]8ebd46c2014-01-08 12:06:13224 // A map of of target -> Query for current queries
225 typedef std::map<GLuint, scoped_refptr<QueryManager::Query> > QueryMap;
226 QueryMap current_queries;
[email protected]e259eb412012-10-13 05:47:24227
[email protected]e259eb412012-10-13 05:47:24228 bool pack_reverse_row_order;
[email protected]454157e2014-05-03 02:49:45229 bool ignore_cached_state;
[email protected]b3cbad12012-12-05 19:56:36230
zmo8ac3bab2015-04-18 02:30:58231 mutable bool fbo_binding_for_scissor_workaround_dirty;
[email protected]d3eba342013-04-18 21:11:50232
233 private:
zmo8ac3bab2015-04-18 02:30:58234 void EnableDisable(GLenum pname, bool enable) const;
235
236 FeatureInfo* feature_info_;
[email protected]d3eba342013-04-18 21:11:50237 scoped_ptr<ErrorState> error_state_;
[email protected]e259eb412012-10-13 05:47:24238};
239
240} // namespace gles2
241} // namespace gpu
242
243#endif // GPU_COMMAND_BUFFER_SERVICE_CONTEXT_STATE_H_
244