Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / gpu / command_buffer / service / shader_manager.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 #ifndef GPU_COMMAND_BUFFER_SERVICE_SHADER_MANAGER_H_
6 #define GPU_COMMAND_BUFFER_SERVICE_SHADER_MANAGER_H_
7
8 #include <string>
9 #include "base/basictypes.h"
10 #include "base/containers/hash_tables.h"
11 #include "base/logging.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "gpu/command_buffer/service/gl_utils.h"
15 #include "gpu/command_buffer/service/shader_translator.h"
16 #include "gpu/gpu_export.h"
17
18 namespace gpu {
19 namespace gles2 {
20
21 // This is used to keep the source code for a shader. This is because in order
22 // to emluate GLES2 the shaders will have to be re-written before passed to
23 // the underlying OpenGL. But, when the user calls glGetShaderSource they
24 // should get the source they passed in, not the re-written source.
25 class GPU_EXPORT Shader : public base::RefCounted<Shader> {
26  public:
27   enum TranslatedShaderSourceType {
28     kANGLE,
29     kGL,  // GL or GLES
30   };
31
32   typedef ShaderTranslator::VariableInfo VariableInfo;
33
34   void DoCompile(ShaderTranslatorInterface* translator,
35                  TranslatedShaderSourceType type);
36
37   GLuint service_id() const {
38     return service_id_;
39   }
40
41   GLenum shader_type() const {
42     return shader_type_;
43   }
44
45   const std::string& source() const {
46     return source_;
47   }
48
49   void set_source(const std::string& source) {
50     source_ = source;
51   }
52
53   const std::string& translated_source() const {
54     return translated_source_;
55   }
56
57   const std::string& signature_source() const {
58     return signature_source_;
59   }
60
61   const VariableInfo* GetAttribInfo(const std::string& name) const;
62   const VariableInfo* GetUniformInfo(const std::string& name) const;
63   const VariableInfo* GetVaryingInfo(const std::string& name) const;
64
65   // If the original_name is not found, return NULL.
66   const std::string* GetAttribMappedName(
67       const std::string& original_name) const;
68
69   // If the hashed_name is not found, return NULL.
70   const std::string* GetOriginalNameFromHashedName(
71       const std::string& hashed_name) const;
72
73   const std::string& log_info() const {
74     return log_info_;
75   }
76
77   bool valid() const {
78     return valid_;
79   }
80
81   bool IsDeleted() const {
82     return service_id_ == 0;
83   }
84
85   bool InUse() const {
86     DCHECK_GE(use_count_, 0);
87     return use_count_ != 0;
88   }
89
90   // Used by program cache.
91   const ShaderTranslator::VariableMap& attrib_map() const {
92     return attrib_map_;
93   }
94
95   // Used by program cache.
96   const ShaderTranslator::VariableMap& uniform_map() const {
97     return uniform_map_;
98   }
99
100   // Used by program cache.
101   const ShaderTranslator::VariableMap& varying_map() const {
102     return varying_map_;
103   }
104
105   // Used by program cache.
106   void set_attrib_map(const ShaderTranslator::VariableMap& attrib_map) {
107     // copied because cache might be cleared
108     attrib_map_ = ShaderTranslator::VariableMap(attrib_map);
109   }
110
111   // Used by program cache.
112   void set_uniform_map(const ShaderTranslator::VariableMap& uniform_map) {
113     // copied because cache might be cleared
114     uniform_map_ = ShaderTranslator::VariableMap(uniform_map);
115   }
116
117   // Used by program cache.
118   void set_varying_map(const ShaderTranslator::VariableMap& varying_map) {
119     // copied because cache might be cleared
120     varying_map_ = ShaderTranslator::VariableMap(varying_map);
121   }
122
123  private:
124   typedef ShaderTranslator::VariableMap VariableMap;
125   typedef ShaderTranslator::NameMap NameMap;
126
127   friend class base::RefCounted<Shader>;
128   friend class ShaderManager;
129
130   Shader(GLuint service_id, GLenum shader_type);
131   ~Shader();
132
133   void IncUseCount();
134   void DecUseCount();
135   void MarkAsDeleted();
136
137   int use_count_;
138
139   // The shader this Shader is tracking.
140   GLuint service_id_;
141   // Type of shader - GL_VERTEX_SHADER or GL_FRAGMENT_SHADER.
142   GLenum shader_type_;
143
144   // True if compilation succeeded.
145   bool valid_;
146
147   // The shader source as passed to glShaderSource.
148   std::string source_;
149
150   // The source the last compile used.
151   std::string signature_source_;
152
153   // The translated shader source.
154   std::string translated_source_;
155
156   // The shader translation log.
157   std::string log_info_;
158
159   // The type info when the shader was last compiled.
160   VariableMap attrib_map_;
161   VariableMap uniform_map_;
162   VariableMap varying_map_;
163
164   // The name hashing info when the shader was last compiled.
165   NameMap name_map_;
166 };
167
168 // Tracks the Shaders.
169 //
170 // NOTE: To support shared resources an instance of this class will
171 // need to be shared by multiple GLES2Decoders.
172 class GPU_EXPORT ShaderManager {
173  public:
174   ShaderManager();
175   ~ShaderManager();
176
177   // Must call before destruction.
178   void Destroy(bool have_context);
179
180   // Creates a shader for the given shader ID.
181   Shader* CreateShader(
182       GLuint client_id,
183       GLuint service_id,
184       GLenum shader_type);
185
186   // Gets an existing shader info for the given shader ID. Returns NULL if none
187   // exists.
188   Shader* GetShader(GLuint client_id);
189
190   // Gets a client id for a given service id.
191   bool GetClientId(GLuint service_id, GLuint* client_id) const;
192
193   void MarkAsDeleted(Shader* shader);
194
195   // Mark a shader as used
196   void UseShader(Shader* shader);
197
198   // Unmark a shader as used. If it has been deleted and is not used
199   // then we free the shader.
200   void UnuseShader(Shader* shader);
201
202   // Check if a Shader is owned by this ShaderManager.
203   bool IsOwned(Shader* shader);
204
205  private:
206   friend class Shader;
207
208   // Info for each shader by service side shader Id.
209   typedef base::hash_map<GLuint, scoped_refptr<Shader> > ShaderMap;
210   ShaderMap shaders_;
211
212   void RemoveShader(Shader* shader);
213
214   DISALLOW_COPY_AND_ASSIGN(ShaderManager);
215 };
216
217 }  // namespace gles2
218 }  // namespace gpu
219
220 #endif  // GPU_COMMAND_BUFFER_SERVICE_SHADER_MANAGER_H_
221