Upstream version 7.35.139.0
[platform/framework/web/crosswalk.git] / src / gpu / command_buffer / service / context_state.h
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.
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
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/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"
19
20 namespace gpu {
21 namespace gles2 {
22
23 class Buffer;
24 class ErrorState;
25 class ErrorStateClient;
26 class FeatureInfo;
27 class Framebuffer;
28 class Program;
29 class Renderbuffer;
30
31 // State associated with each texture unit.
32 struct GPU_EXPORT TextureUnit {
33   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
40   scoped_refptr<TextureRef> bound_texture_2d;
41
42   // texture currently bound to this unit's GL_TEXTURE_CUBE_MAP with
43   // glBindTexture
44   scoped_refptr<TextureRef> bound_texture_cube_map;
45
46   // texture currently bound to this unit's GL_TEXTURE_EXTERNAL_OES with
47   // glBindTexture
48   scoped_refptr<TextureRef> bound_texture_external_oes;
49
50   // texture currently bound to this unit's GL_TEXTURE_RECTANGLE_ARB with
51   // glBindTexture
52   scoped_refptr<TextureRef> bound_texture_rectangle_arb;
53
54   scoped_refptr<TextureRef> GetInfoForSamplerType(
55       GLenum type) {
56     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
73   void Unbind(TextureRef* texture) {
74     if (bound_texture_2d.get() == texture) {
75       bound_texture_2d = NULL;
76     }
77     if (bound_texture_cube_map.get() == texture) {
78       bound_texture_cube_map = NULL;
79     }
80     if (bound_texture_external_oes.get() == texture) {
81       bound_texture_external_oes = NULL;
82     }
83   }
84 };
85
86 struct 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 };
95
96 struct GPU_EXPORT ContextState {
97   ContextState(FeatureInfo* feature_info,
98                ErrorStateClient* error_state_client,
99                Logger* logger);
100   ~ContextState();
101
102   void Initialize();
103
104   void RestoreState(const ContextState* prev_state) const;
105   void InitCapabilities() const;
106   void InitState() const;
107
108   void RestoreActiveTexture() const;
109   void RestoreAllTextureUnitBindings(const ContextState* prev_state) const;
110   void RestoreActiveTextureUnitBinding(unsigned int target) const;
111   void RestoreAttribute(GLuint index) const;
112   void RestoreBufferBindings() const;
113   void RestoreGlobalState() const;
114   void RestoreProgramBindings() const;
115   void RestoreRenderbufferBindings() const;
116   void RestoreTextureUnitBindings(
117       GLuint unit, const ContextState* prev_state) const;
118
119   // Helper for getting cached state.
120   bool GetStateAsGLint(
121       GLenum pname, GLint* params, GLsizei* num_written) const;
122   bool GetStateAsGLfloat(
123       GLenum pname, GLfloat* params, GLsizei* num_written) const;
124   bool GetEnabled(GLenum cap) const;
125
126   ErrorState* GetErrorState();
127
128   #include "gpu/command_buffer/service/context_state_autogen.h"
129
130   EnableFlags enable_flags;
131
132   // Current active texture by 0 - n index.
133   // In other words, if we call glActiveTexture(GL_TEXTURE2) this value would
134   // be 2.
135   GLuint active_texture_unit;
136
137   // The currently bound array buffer. If this is 0 it is illegal to call
138   // glVertexAttribPointer.
139   scoped_refptr<Buffer> bound_array_buffer;
140
141   // Which textures are bound to texture units through glActiveTexture.
142   std::vector<TextureUnit> texture_units;
143
144   // The values for each attrib.
145   std::vector<Vec4> attrib_values;
146
147   // Class that manages vertex attribs.
148   scoped_refptr<VertexAttribManager> vertex_attrib_manager;
149
150   // The program in use by glUseProgram
151   scoped_refptr<Program> current_program;
152
153   // The currently bound renderbuffer
154   scoped_refptr<Renderbuffer> bound_renderbuffer;
155
156   // A map of of target -> Query for current queries
157   typedef std::map<GLuint, scoped_refptr<QueryManager::Query> > QueryMap;
158   QueryMap current_queries;
159
160   bool pack_reverse_row_order;
161
162   mutable bool fbo_binding_for_scissor_workaround_dirty_;
163   FeatureInfo* feature_info_;
164
165  private:
166   scoped_ptr<ErrorState> error_state_;
167 };
168
169 }  // namespace gles2
170 }  // namespace gpu
171
172 #endif  // GPU_COMMAND_BUFFER_SERVICE_CONTEXT_STATE_H_
173