Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: gpu/command_buffer/service/program_manager.h

Issue 10797055: gpu in-memory program cache implementation with a memory limit + lru eviction. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: simplified binary loading logic Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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_PROGRAM_MANAGER_H_ 5 #ifndef GPU_COMMAND_BUFFER_SERVICE_PROGRAM_MANAGER_H_
6 #define GPU_COMMAND_BUFFER_SERVICE_PROGRAM_MANAGER_H_ 6 #define GPU_COMMAND_BUFFER_SERVICE_PROGRAM_MANAGER_H_
7 7
8 #include <map> 8 #include <map>
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "gpu/command_buffer/service/common_decoder.h" 14 #include "gpu/command_buffer/service/common_decoder.h"
16 #include "gpu/command_buffer/service/gl_utils.h" 15 #include "gpu/command_buffer/service/gl_utils.h"
17 #include "gpu/command_buffer/service/shader_manager.h" 16 #include "gpu/command_buffer/service/shader_manager.h"
18 #include "gpu/gpu_export.h" 17 #include "gpu/gpu_export.h"
19 18
20 namespace gpu { 19 namespace gpu {
21 namespace gles2 { 20 namespace gles2 {
21 class ProgramCache;
22 class FeatureInfo;
22 23
23 // Tracks the Programs. 24 // Tracks the Programs.
24 // 25 //
25 // NOTE: To support shared resources an instance of this class will 26 // NOTE: To support shared resources an instance of this class will
26 // need to be shared by multiple GLES2Decoders. 27 // need to be shared by multiple GLES2Decoders.
27 class GPU_EXPORT ProgramManager { 28 class GPU_EXPORT ProgramManager {
28 public: 29 public:
30 typedef std::map<std::string, GLint> LocationMap;
31
29 // This is used to track which attributes a particular program needs 32 // This is used to track which attributes a particular program needs
30 // so we can verify at glDrawXXX time that every attribute is either disabled 33 // so we can verify at glDrawXXX time that every attribute is either disabled
31 // or if enabled that it points to a valid source. 34 // or if enabled that it points to a valid source.
32 class GPU_EXPORT ProgramInfo : public base::RefCounted<ProgramInfo> { 35 class GPU_EXPORT ProgramInfo : public base::RefCounted<ProgramInfo> {
33 public: 36 public:
34 typedef scoped_refptr<ProgramInfo> Ref; 37 typedef scoped_refptr<ProgramInfo> Ref;
35 38
36 static const int kMaxAttachedShaders = 2; 39 static const int kMaxAttachedShaders = 2;
37 40
38 struct UniformInfo { 41 struct UniformInfo {
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 bool IsValid() const { 146 bool IsValid() const {
144 return valid_; 147 return valid_;
145 } 148 }
146 149
147 bool AttachShader(ShaderManager* manager, ShaderManager::ShaderInfo* info); 150 bool AttachShader(ShaderManager* manager, ShaderManager::ShaderInfo* info);
148 bool DetachShader(ShaderManager* manager, ShaderManager::ShaderInfo* info); 151 bool DetachShader(ShaderManager* manager, ShaderManager::ShaderInfo* info);
149 152
150 bool CanLink() const; 153 bool CanLink() const;
151 154
152 // Performs glLinkProgram and related activities. 155 // Performs glLinkProgram and related activities.
153 bool Link(); 156 bool Link(ShaderManager* manager,
157 ShaderTranslator* vertex_translator,
158 ShaderTranslator* fragment_shader,
159 FeatureInfo* feature_info);
154 160
155 // Performs glValidateProgram and related activities. 161 // Performs glValidateProgram and related activities.
156 void Validate(); 162 void Validate();
157 163
158 const std::string* log_info() const { 164 const std::string* log_info() const {
159 return log_info_.get(); 165 return log_info_.get();
160 } 166 }
161 167
162 bool InUse() const { 168 bool InUse() const {
163 DCHECK_GE(use_count_, 0); 169 DCHECK_GE(use_count_, 0);
164 return use_count_ != 0; 170 return use_count_ != 0;
165 } 171 }
166 172
167 // Sets attribute-location binding from a glBindAttribLocation() call. 173 // Sets attribute-location binding from a glBindAttribLocation() call.
168 void SetAttribLocationBinding(const std::string& attrib, GLint location) { 174 void SetAttribLocationBinding(const std::string& attrib, GLint location) {
169 bind_attrib_location_map_[attrib] = location; 175 bind_attrib_location_map_[attrib] = location;
170 } 176 }
171 177
172 // Sets uniform-location binding from a glBindUniformLocationCHROMIUM call. 178 // Sets uniform-location binding from a glBindUniformLocationCHROMIUM call.
173 // returns false if error. 179 // returns false if error.
174 bool SetUniformLocationBinding(const std::string& name, GLint location); 180 bool SetUniformLocationBinding(const std::string& name, GLint location);
175 181
176 // Detects if there are attribute location conflicts from 182 // Detects if there are attribute location conflicts from
177 // glBindAttribLocation() calls. 183 // glBindAttribLocation() calls.
178 // We only consider the declared attributes in the program. 184 // We only consider the declared attributes in the program.
179 bool DetectAttribLocationBindingConflicts() const; 185 bool DetectAttribLocationBindingConflicts() const;
180 186
187 // Visible for testing
188 const LocationMap& bind_attrib_location_map() const {
189 return bind_attrib_location_map_;
190 }
191
181 private: 192 private:
182 friend class base::RefCounted<ProgramInfo>; 193 friend class base::RefCounted<ProgramInfo>;
183 friend class ProgramManager; 194 friend class ProgramManager;
184 195
185 typedef std::map<std::string, GLint> LocationMap;
186
187 ~ProgramInfo(); 196 ~ProgramInfo();
188 197
189 void set_log_info(const char* str) { 198 void set_log_info(const char* str) {
190 log_info_.reset(str ? new std::string(str) : NULL); 199 log_info_.reset(str ? new std::string(str) : NULL);
191 } 200 }
192 201
193 void ClearLinkStatus() { 202 void ClearLinkStatus() {
194 link_status_ = false; 203 link_status_ = false;
195 } 204 }
196 205
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 // Log info 301 // Log info
293 scoped_ptr<std::string> log_info_; 302 scoped_ptr<std::string> log_info_;
294 303
295 // attribute-location binding map from glBindAttribLocation() calls. 304 // attribute-location binding map from glBindAttribLocation() calls.
296 LocationMap bind_attrib_location_map_; 305 LocationMap bind_attrib_location_map_;
297 306
298 // uniform-location binding map from glBindUniformLocationCHROMIUM() calls. 307 // uniform-location binding map from glBindUniformLocationCHROMIUM() calls.
299 LocationMap bind_uniform_location_map_; 308 LocationMap bind_uniform_location_map_;
300 }; 309 };
301 310
302 ProgramManager(); 311 explicit ProgramManager(ProgramCache* program_cache);
303 ~ProgramManager(); 312 ~ProgramManager();
304 313
305 // Must call before destruction. 314 // Must call before destruction.
306 void Destroy(bool have_context); 315 void Destroy(bool have_context);
307 316
308 // Creates a new program info. 317 // Creates a new program info.
309 ProgramInfo* CreateProgramInfo(GLuint client_id, GLuint service_id); 318 ProgramInfo* CreateProgramInfo(GLuint client_id, GLuint service_id);
310 319
311 // Gets a program info 320 // Gets a program info
312 ProgramInfo* GetProgramInfo(GLuint client_id); 321 ProgramInfo* GetProgramInfo(GLuint client_id);
313 322
314 // Gets a client id for a given service id. 323 // Gets a client id for a given service id.
315 bool GetClientId(GLuint service_id, GLuint* client_id) const; 324 bool GetClientId(GLuint service_id, GLuint* client_id) const;
316 325
326 // Gets the shader cache
327 ProgramCache* program_cache() const;
328
317 // Marks a program as deleted. If it is not used the info will be deleted. 329 // Marks a program as deleted. If it is not used the info will be deleted.
318 void MarkAsDeleted(ShaderManager* shader_manager, ProgramInfo* info); 330 void MarkAsDeleted(ShaderManager* shader_manager, ProgramInfo* info);
319 331
320 // Marks a program as used. 332 // Marks a program as used.
321 void UseProgram(ProgramInfo* info); 333 void UseProgram(ProgramInfo* info);
322 334
323 // Makes a program as unused. If deleted the program info will be removed. 335 // Makes a program as unused. If deleted the program info will be removed.
324 void UnuseProgram(ShaderManager* shader_manager, ProgramInfo* info); 336 void UnuseProgram(ShaderManager* shader_manager, ProgramInfo* info);
325 337
326 // Clears the uniforms for this program. 338 // Clears the uniforms for this program.
327 void ClearUniforms(ProgramInfo* info); 339 void ClearUniforms(ProgramInfo* info);
328 340
329 // Returns true if prefix is invalid for gl. 341 // Returns true if prefix is invalid for gl.
330 static bool IsInvalidPrefix(const char* name, size_t length); 342 static bool IsInvalidPrefix(const char* name, size_t length);
331 343
332 // Check if a ProgramInfo is owned by this ProgramManager. 344 // Check if a ProgramInfo is owned by this ProgramManager.
333 bool IsOwned(ProgramInfo* info); 345 bool IsOwned(ProgramInfo* info);
334 346
335 static int32 MakeFakeLocation(int32 index, int32 element); 347 static int32 MakeFakeLocation(int32 index, int32 element);
336 348
349 // Cache-aware shader compiling. If no cache or if the shader wasn't
350 // previously compiled, ForceCompileShader is called
351 void DoCompileShader(ShaderManager::ShaderInfo* info,
352 ShaderTranslator* translator,
353 FeatureInfo* feature_info);
354
337 private: 355 private:
356 // Actually compiles the shader
357 void ForceCompileShader(const std::string* source,
358 ShaderManager::ShaderInfo* info,
359 ShaderTranslator* translator,
360 FeatureInfo* feature_info);
361
338 void StartTracking(ProgramInfo* info); 362 void StartTracking(ProgramInfo* info);
339 void StopTracking(ProgramInfo* info); 363 void StopTracking(ProgramInfo* info);
340 364
341 // Info for each "successfully linked" program by service side program Id. 365 // Info for each "successfully linked" program by service side program Id.
342 // TODO(gman): Choose a faster container. 366 // TODO(gman): Choose a faster container.
343 typedef std::map<GLuint, ProgramInfo::Ref> ProgramInfoMap; 367 typedef std::map<GLuint, ProgramInfo::Ref> ProgramInfoMap;
344 ProgramInfoMap program_infos_; 368 ProgramInfoMap program_infos_;
345 369
346 // Counts the number of ProgramInfo allocated with 'this' as its manager. 370 // Counts the number of ProgramInfo allocated with 'this' as its manager.
347 // Allows to check no ProgramInfo will outlive this. 371 // Allows to check no ProgramInfo will outlive this.
348 unsigned int program_info_count_; 372 unsigned int program_info_count_;
349 373
350 bool have_context_; 374 bool have_context_;
351 375
352 bool disable_workarounds_; 376 bool disable_workarounds_;
353 377
354 // Used to clear uniforms. 378 // Used to clear uniforms.
355 std::vector<uint8> zero_; 379 std::vector<uint8> zero_;
356 380
381 ProgramCache* program_cache_;
382
357 void RemoveProgramInfoIfUnused( 383 void RemoveProgramInfoIfUnused(
358 ShaderManager* shader_manager, ProgramInfo* info); 384 ShaderManager* shader_manager, ProgramInfo* info);
359 385
360 DISALLOW_COPY_AND_ASSIGN(ProgramManager); 386 DISALLOW_COPY_AND_ASSIGN(ProgramManager);
361 }; 387 };
362 388
363 } // namespace gles2 389 } // namespace gles2
364 } // namespace gpu 390 } // namespace gpu
365 391
366 #endif // GPU_COMMAND_BUFFER_SERVICE_PROGRAM_MANAGER_H_ 392 #endif // GPU_COMMAND_BUFFER_SERVICE_PROGRAM_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698