| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef GPU_COMMAND_BUFFER_SERVICE_SHADER_MANAGER_H_ | 5 #ifndef GPU_COMMAND_BUFFER_SERVICE_SHADER_MANAGER_H_ |
| 6 #define GPU_COMMAND_BUFFER_SERVICE_SHADER_MANAGER_H_ | 6 #define GPU_COMMAND_BUFFER_SERVICE_SHADER_MANAGER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/logging.h" |
| 11 #include "base/ref_counted.h" | 12 #include "base/ref_counted.h" |
| 12 #include "gpu/command_buffer/service/gl_utils.h" | 13 #include "gpu/command_buffer/service/gl_utils.h" |
| 13 #include "gpu/command_buffer/service/shader_translator.h" | 14 #include "gpu/command_buffer/service/shader_translator.h" |
| 14 | 15 |
| 15 namespace gpu { | 16 namespace gpu { |
| 16 namespace gles2 { | 17 namespace gles2 { |
| 17 | 18 |
| 18 // Tracks the Shaders. | 19 // Tracks the Shaders. |
| 19 // | 20 // |
| 20 // NOTE: To support shared resources an instance of this class will | 21 // NOTE: To support shared resources an instance of this class will |
| 21 // need to be shared by multiple GLES2Decoders. | 22 // need to be shared by multiple GLES2Decoders. |
| 22 class ShaderManager { | 23 class ShaderManager { |
| 23 public: | 24 public: |
| 24 // This is used to keep the source code for a shader. This is because in order | 25 // This is used to keep the source code for a shader. This is because in order |
| 25 // to emluate GLES2 the shaders will have to be re-written before passed to | 26 // to emluate GLES2 the shaders will have to be re-written before passed to |
| 26 // the underlying OpenGL. But, when the user calls glGetShaderSource they | 27 // the underlying OpenGL. But, when the user calls glGetShaderSource they |
| 27 // should get the source they passed in, not the re-written source. | 28 // should get the source they passed in, not the re-written source. |
| 28 class ShaderInfo : public base::RefCounted<ShaderInfo> { | 29 class ShaderInfo : public base::RefCounted<ShaderInfo> { |
| 29 public: | 30 public: |
| 30 typedef scoped_refptr<ShaderInfo> Ref; | 31 typedef scoped_refptr<ShaderInfo> Ref; |
| 31 typedef ShaderTranslator::VariableInfo VariableInfo; | 32 typedef ShaderTranslator::VariableInfo VariableInfo; |
| 32 | 33 |
| 33 explicit ShaderInfo(GLuint service_id, GLenum shader_type) | 34 explicit ShaderInfo(GLuint service_id, GLenum shader_type) |
| 34 : service_id_(service_id), | 35 : use_count_(0), |
| 36 service_id_(service_id), |
| 35 shader_type_(shader_type), | 37 shader_type_(shader_type), |
| 36 valid_(false) { | 38 valid_(false) { |
| 37 } | 39 } |
| 38 | 40 |
| 39 void Update(const std::string& source) { | 41 void Update(const std::string& source) { |
| 40 source_ = source; | 42 source_ = source; |
| 41 } | 43 } |
| 42 | 44 |
| 43 GLuint service_id() const { | 45 GLuint service_id() const { |
| 44 return service_id_; | 46 return service_id_; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 64 } | 66 } |
| 65 | 67 |
| 66 bool IsValid() const { | 68 bool IsValid() const { |
| 67 return valid_; | 69 return valid_; |
| 68 } | 70 } |
| 69 | 71 |
| 70 bool IsDeleted() const { | 72 bool IsDeleted() const { |
| 71 return service_id_ == 0; | 73 return service_id_ == 0; |
| 72 } | 74 } |
| 73 | 75 |
| 76 bool InUse() const { |
| 77 DCHECK_GE(use_count_, 0); |
| 78 return use_count_ != 0; |
| 79 } |
| 80 |
| 74 private: | 81 private: |
| 75 typedef ShaderTranslator::VariableMap VariableMap; | 82 typedef ShaderTranslator::VariableMap VariableMap; |
| 76 | 83 |
| 77 friend class base::RefCounted<ShaderInfo>; | 84 friend class base::RefCounted<ShaderInfo>; |
| 78 friend class ShaderManager; | 85 friend class ShaderManager; |
| 79 ~ShaderInfo() { } | 86 ~ShaderInfo() { } |
| 80 | 87 |
| 88 void IncUseCount() { |
| 89 ++use_count_; |
| 90 } |
| 91 |
| 92 void DecUseCount() { |
| 93 --use_count_; |
| 94 DCHECK_GE(use_count_, 0); |
| 95 } |
| 96 |
| 81 void MarkAsDeleted() { | 97 void MarkAsDeleted() { |
| 98 DCHECK_NE(service_id_, 0u); |
| 82 service_id_ = 0; | 99 service_id_ = 0; |
| 83 } | 100 } |
| 84 | 101 |
| 102 int use_count_; |
| 103 |
| 85 // The shader this ShaderInfo is tracking. | 104 // The shader this ShaderInfo is tracking. |
| 86 GLuint service_id_; | 105 GLuint service_id_; |
| 87 // Type of shader - GL_VERTEX_SHADER or GL_FRAGMENT_SHADER. | 106 // Type of shader - GL_VERTEX_SHADER or GL_FRAGMENT_SHADER. |
| 88 GLenum shader_type_; | 107 GLenum shader_type_; |
| 89 | 108 |
| 90 // True if compilation succeeded. | 109 // True if compilation succeeded. |
| 91 bool valid_; | 110 bool valid_; |
| 92 | 111 |
| 93 // The shader source as passed to glShaderSource. | 112 // The shader source as passed to glShaderSource. |
| 94 std::string source_; | 113 std::string source_; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 109 | 128 |
| 110 // Creates a shader info for the given shader ID. | 129 // Creates a shader info for the given shader ID. |
| 111 void CreateShaderInfo(GLuint client_id, | 130 void CreateShaderInfo(GLuint client_id, |
| 112 GLuint service_id, | 131 GLuint service_id, |
| 113 GLenum shader_type); | 132 GLenum shader_type); |
| 114 | 133 |
| 115 // Gets an existing shader info for the given shader ID. Returns NULL if none | 134 // Gets an existing shader info for the given shader ID. Returns NULL if none |
| 116 // exists. | 135 // exists. |
| 117 ShaderInfo* GetShaderInfo(GLuint client_id); | 136 ShaderInfo* GetShaderInfo(GLuint client_id); |
| 118 | 137 |
| 119 // Deletes the shader info for the given shader. | |
| 120 void RemoveShaderInfo(GLuint client_id); | |
| 121 | |
| 122 // Gets a client id for a given service id. | 138 // Gets a client id for a given service id. |
| 123 bool GetClientId(GLuint service_id, GLuint* client_id) const; | 139 bool GetClientId(GLuint service_id, GLuint* client_id) const; |
| 124 | 140 |
| 141 void MarkAsDeleted(ShaderInfo* info); |
| 142 |
| 143 // Mark a shader as used |
| 144 void UseShader(ShaderInfo* info); |
| 145 |
| 146 // Unmark a shader as used. If it has been deleted and is not used |
| 147 // then we free the info. |
| 148 void UnuseShader(ShaderInfo* info); |
| 149 |
| 125 private: | 150 private: |
| 126 // Info for each shader by service side shader Id. | 151 // Info for each shader by service side shader Id. |
| 127 typedef std::map<GLuint, ShaderInfo::Ref> ShaderInfoMap; | 152 typedef std::map<GLuint, ShaderInfo::Ref> ShaderInfoMap; |
| 128 ShaderInfoMap shader_infos_; | 153 ShaderInfoMap shader_infos_; |
| 129 | 154 |
| 155 void RemoveShaderInfoIfUnused(ShaderInfo* info); |
| 156 |
| 130 DISALLOW_COPY_AND_ASSIGN(ShaderManager); | 157 DISALLOW_COPY_AND_ASSIGN(ShaderManager); |
| 131 }; | 158 }; |
| 132 | 159 |
| 133 } // namespace gles2 | 160 } // namespace gles2 |
| 134 } // namespace gpu | 161 } // namespace gpu |
| 135 | 162 |
| 136 #endif // GPU_COMMAND_BUFFER_SERVICE_SHADER_MANAGER_H_ | 163 #endif // GPU_COMMAND_BUFFER_SERVICE_SHADER_MANAGER_H_ |
| 137 | 164 |
| OLD | NEW |