Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / gpu / command_buffer / service / program_cache.h
blob44593df6d84e3fa4f62507430c74f8c486d3cc32
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.
5 #ifndef GPU_COMMAND_BUFFER_SERVICE_PROGRAM_CACHE_H_
6 #define GPU_COMMAND_BUFFER_SERVICE_PROGRAM_CACHE_H_
8 #include <map>
9 #include <string>
11 #include "base/containers/hash_tables.h"
12 #include "base/sha1.h"
13 #include "gpu/command_buffer/common/gles2_cmd_format.h"
14 #include "gpu/command_buffer/service/gles2_cmd_decoder.h"
15 #include "gpu/command_buffer/service/shader_manager.h"
17 namespace gpu {
18 namespace gles2 {
20 class Shader;
22 // Program cache base class for caching linked gpu programs
23 class GPU_EXPORT ProgramCache {
24 public:
25 static const size_t kHashLength = base::kSHA1Length;
27 typedef std::map<std::string, GLint> LocationMap;
29 enum LinkedProgramStatus {
30 LINK_UNKNOWN,
31 LINK_SUCCEEDED
34 enum ProgramLoadResult {
35 PROGRAM_LOAD_FAILURE,
36 PROGRAM_LOAD_SUCCESS
39 ProgramCache();
40 virtual ~ProgramCache();
42 LinkedProgramStatus GetLinkedProgramStatus(
43 const std::string& shader_signature_a,
44 const std::string& shader_signature_b,
45 const LocationMap* bind_attrib_location_map,
46 const std::vector<std::string>& transform_feedback_varyings,
47 GLenum transform_feedback_buffer_mode) const;
49 // Loads the linked program from the cache. If the program is not found or
50 // there was an error, PROGRAM_LOAD_FAILURE should be returned.
51 virtual ProgramLoadResult LoadLinkedProgram(
52 GLuint program,
53 Shader* shader_a,
54 Shader* shader_b,
55 const LocationMap* bind_attrib_location_map,
56 const std::vector<std::string>& transform_feedback_varyings,
57 GLenum transform_feedback_buffer_mode,
58 const ShaderCacheCallback& shader_callback) = 0;
60 // Saves the program into the cache. If successful, the implementation should
61 // call LinkedProgramCacheSuccess.
62 virtual void SaveLinkedProgram(
63 GLuint program,
64 const Shader* shader_a,
65 const Shader* shader_b,
66 const LocationMap* bind_attrib_location_map,
67 const std::vector<std::string>& transform_feedback_varyings,
68 GLenum transform_feedback_buffer_mode,
69 const ShaderCacheCallback& shader_callback) = 0;
71 virtual void LoadProgram(const std::string& program) = 0;
73 // clears the cache
74 void Clear();
76 // Only for testing
77 void LinkedProgramCacheSuccess(const std::string& shader_signature_a,
78 const std::string& shader_signature_b,
79 const LocationMap* bind_attrib_location_map,
80 const std::vector<std::string>& transform_feedback_varyings,
81 GLenum transform_feedback_buffer_mode);
83 protected:
84 // called by implementing class after a shader was successfully cached
85 void LinkedProgramCacheSuccess(const std::string& program_hash);
87 // result is not null terminated
88 void ComputeShaderHash(const std::string& shader,
89 char* result) const;
91 // result is not null terminated. hashed shaders are expected to be
92 // kHashLength in length
93 void ComputeProgramHash(
94 const char* hashed_shader_0,
95 const char* hashed_shader_1,
96 const LocationMap* bind_attrib_location_map,
97 const std::vector<std::string>& transform_feedback_varyings,
98 GLenum transform_feedback_buffer_mode,
99 char* result) const;
101 void Evict(const std::string& program_hash);
103 private:
104 typedef base::hash_map<std::string,
105 LinkedProgramStatus> LinkStatusMap;
107 // called to clear the backend cache
108 virtual void ClearBackend() = 0;
110 LinkStatusMap link_status_;
112 DISALLOW_COPY_AND_ASSIGN(ProgramCache);
115 } // namespace gles2
116 } // namespace gpu
118 #endif // GPU_COMMAND_BUFFER_SERVICE_PROGRAM_CACHE_H_