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