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_MEMORY_PROGRAM_CACHE_H_
6 #define GPU_COMMAND_BUFFER_SERVICE_MEMORY_PROGRAM_CACHE_H_
11 #include "base/containers/hash_tables.h"
12 #include "base/containers/mru_cache.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "gpu/command_buffer/service/gles2_cmd_decoder.h"
16 #include "gpu/command_buffer/service/program_cache.h"
17 #include "gpu/command_buffer/service/shader_translator.h"
22 // Program cache that stores binaries completely in-memory
23 class GPU_EXPORT MemoryProgramCache
: public ProgramCache
{
26 explicit MemoryProgramCache(const size_t max_cache_size_bytes
);
27 virtual ~MemoryProgramCache();
29 virtual ProgramLoadResult
LoadLinkedProgram(
32 const ShaderTranslatorInterface
* translator_a
,
34 const ShaderTranslatorInterface
* translator_b
,
35 const LocationMap
* bind_attrib_location_map
,
36 const ShaderCacheCallback
& shader_callback
) OVERRIDE
;
37 virtual void SaveLinkedProgram(
39 const Shader
* shader_a
,
40 const ShaderTranslatorInterface
* translator_a
,
41 const Shader
* shader_b
,
42 const ShaderTranslatorInterface
* translator_b
,
43 const LocationMap
* bind_attrib_location_map
,
44 const ShaderCacheCallback
& shader_callback
) OVERRIDE
;
46 virtual void LoadProgram(const std::string
& program
) OVERRIDE
;
49 virtual void ClearBackend() OVERRIDE
;
51 class ProgramCacheValue
: public base::RefCounted
<ProgramCacheValue
> {
53 ProgramCacheValue(GLsizei length
,
56 const std::string
& program_hash
,
57 const char* shader_0_hash
,
58 const ShaderTranslator::VariableMap
& attrib_map_0
,
59 const ShaderTranslator::VariableMap
& uniform_map_0
,
60 const ShaderTranslator::VariableMap
& varying_map_0
,
61 const char* shader_1_hash
,
62 const ShaderTranslator::VariableMap
& attrib_map_1
,
63 const ShaderTranslator::VariableMap
& uniform_map_1
,
64 const ShaderTranslator::VariableMap
& varying_map_1
,
65 MemoryProgramCache
* program_cache
);
67 GLsizei
length() const {
71 GLenum
format() const {
75 const char* data() const {
79 const std::string
& shader_0_hash() const {
80 return shader_0_hash_
;
83 const ShaderTranslator::VariableMap
& attrib_map_0() const {
87 const ShaderTranslator::VariableMap
& uniform_map_0() const {
88 return uniform_map_0_
;
91 const ShaderTranslator::VariableMap
& varying_map_0() const {
92 return varying_map_0_
;
95 const std::string
& shader_1_hash() const {
96 return shader_1_hash_
;
99 const ShaderTranslator::VariableMap
& attrib_map_1() const {
100 return attrib_map_1_
;
103 const ShaderTranslator::VariableMap
& uniform_map_1() const {
104 return uniform_map_1_
;
107 const ShaderTranslator::VariableMap
& varying_map_1() const {
108 return varying_map_1_
;
112 friend class base::RefCounted
<ProgramCacheValue
>;
114 ~ProgramCacheValue();
116 const GLsizei length_
;
117 const GLenum format_
;
118 const scoped_ptr
<const char[]> data_
;
119 const std::string program_hash_
;
120 const std::string shader_0_hash_
;
121 const ShaderTranslator::VariableMap attrib_map_0_
;
122 const ShaderTranslator::VariableMap uniform_map_0_
;
123 const ShaderTranslator::VariableMap varying_map_0_
;
124 const std::string shader_1_hash_
;
125 const ShaderTranslator::VariableMap attrib_map_1_
;
126 const ShaderTranslator::VariableMap uniform_map_1_
;
127 const ShaderTranslator::VariableMap varying_map_1_
;
128 MemoryProgramCache
* const program_cache_
;
130 DISALLOW_COPY_AND_ASSIGN(ProgramCacheValue
);
133 friend class ProgramCacheValue
;
135 typedef base::MRUCache
<std::string
,
136 scoped_refptr
<ProgramCacheValue
> > ProgramMRUCache
;
138 const size_t max_size_bytes_
;
139 size_t curr_size_bytes_
;
140 ProgramMRUCache store_
;
142 DISALLOW_COPY_AND_ASSIGN(MemoryProgramCache
);
148 #endif // GPU_COMMAND_BUFFER_SERVICE_MEMORY_PROGRAM_CACHE_H_