Home | History | Annotate | Download | only in service
      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.
      4 
      5 #ifndef GPU_COMMAND_BUFFER_SERVICE_MEMORY_PROGRAM_CACHE_H_
      6 #define GPU_COMMAND_BUFFER_SERVICE_MEMORY_PROGRAM_CACHE_H_
      7 
      8 #include <map>
      9 #include <string>
     10 
     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"
     18 
     19 namespace gpu {
     20 namespace gles2 {
     21 
     22 // Program cache that stores binaries completely in-memory
     23 class GPU_EXPORT MemoryProgramCache : public ProgramCache {
     24  public:
     25   MemoryProgramCache();
     26   explicit MemoryProgramCache(const size_t max_cache_size_bytes);
     27   virtual ~MemoryProgramCache();
     28 
     29   virtual ProgramLoadResult LoadLinkedProgram(
     30       GLuint program,
     31       Shader* shader_a,
     32       const ShaderTranslatorInterface* translator_a,
     33       Shader* shader_b,
     34       const ShaderTranslatorInterface* translator_b,
     35       const LocationMap* bind_attrib_location_map,
     36       const ShaderCacheCallback& shader_callback) OVERRIDE;
     37   virtual void SaveLinkedProgram(
     38       GLuint program,
     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;
     45 
     46   virtual void LoadProgram(const std::string& program) OVERRIDE;
     47 
     48  private:
     49   virtual void ClearBackend() OVERRIDE;
     50 
     51   class ProgramCacheValue : public base::RefCounted<ProgramCacheValue> {
     52    public:
     53     ProgramCacheValue(GLsizei length,
     54                       GLenum format,
     55                       const char* data,
     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 char* shader_1_hash,
     61                       const ShaderTranslator::VariableMap& attrib_map_1,
     62                       const ShaderTranslator::VariableMap& uniform_map_1,
     63                       MemoryProgramCache* program_cache);
     64 
     65     GLsizei length() const {
     66       return length_;
     67     }
     68 
     69     GLenum format() const {
     70       return format_;
     71     }
     72 
     73     const char* data() const {
     74       return data_.get();
     75     }
     76 
     77     const std::string& shader_0_hash() const {
     78       return shader_0_hash_;
     79     }
     80 
     81     const ShaderTranslator::VariableMap& attrib_map_0() const {
     82       return attrib_map_0_;
     83     }
     84 
     85     const ShaderTranslator::VariableMap& uniform_map_0() const {
     86       return uniform_map_0_;
     87     }
     88 
     89     const std::string& shader_1_hash() const {
     90       return shader_1_hash_;
     91     }
     92 
     93     const ShaderTranslator::VariableMap& attrib_map_1() const {
     94       return attrib_map_1_;
     95     }
     96 
     97     const ShaderTranslator::VariableMap& uniform_map_1() const {
     98       return uniform_map_1_;
     99     }
    100 
    101    private:
    102     friend class base::RefCounted<ProgramCacheValue>;
    103 
    104     ~ProgramCacheValue();
    105 
    106     const GLsizei length_;
    107     const GLenum format_;
    108     const scoped_ptr<const char[]> data_;
    109     const std::string program_hash_;
    110     const std::string shader_0_hash_;
    111     const ShaderTranslator::VariableMap attrib_map_0_;
    112     const ShaderTranslator::VariableMap uniform_map_0_;
    113     const std::string shader_1_hash_;
    114     const ShaderTranslator::VariableMap attrib_map_1_;
    115     const ShaderTranslator::VariableMap uniform_map_1_;
    116     MemoryProgramCache* const program_cache_;
    117 
    118     DISALLOW_COPY_AND_ASSIGN(ProgramCacheValue);
    119   };
    120 
    121   friend class ProgramCacheValue;
    122 
    123   typedef base::MRUCache<std::string,
    124                          scoped_refptr<ProgramCacheValue> > ProgramMRUCache;
    125 
    126   const size_t max_size_bytes_;
    127   size_t curr_size_bytes_;
    128   ProgramMRUCache store_;
    129 
    130   DISALLOW_COPY_AND_ASSIGN(MemoryProgramCache);
    131 };
    132 
    133 }  // namespace gles2
    134 }  // namespace gpu
    135 
    136 #endif  // GPU_COMMAND_BUFFER_SERVICE_MEMORY_PROGRAM_CACHE_H_
    137