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