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_SHADER_TRANSLATOR_CACHE_H_
      6 #define GPU_COMMAND_BUFFER_SERVICE_SHADER_TRANSLATOR_CACHE_H_
      7 
      8 #include <string.h>
      9 
     10 #include <map>
     11 
     12 #include "base/memory/ref_counted.h"
     13 #include "base/memory/singleton.h"
     14 #include "gpu/command_buffer/service/shader_translator.h"
     15 #include "third_party/angle_dx11/include/GLSLANG/ShaderLang.h"
     16 
     17 namespace gpu {
     18 namespace gles2 {
     19 
     20 // This singleton and the cache that it implements is NOT thread safe.
     21 // We're relying on the fact that the all GLES2DecoderImpl's are used
     22 // on one thread.
     23 //
     24 // TODO(backer): Investigate using glReleaseShaderCompiler as an alternative to
     25 // to this cache.
     26 class ShaderTranslatorCache : public ShaderTranslator::DestructionObserver {
     27  public:
     28   static ShaderTranslatorCache* GetInstance();
     29 
     30   // ShaderTranslator::DestructionObserver implementation
     31   virtual void OnDestruct(ShaderTranslator* translator) OVERRIDE;
     32 
     33   scoped_refptr<ShaderTranslator> GetTranslator(
     34       ShShaderType shader_type,
     35       ShShaderSpec shader_spec,
     36       const ShBuiltInResources* resources,
     37       ShaderTranslatorInterface::GlslImplementationType
     38           glsl_implementation_type,
     39       ShaderTranslatorInterface::GlslBuiltInFunctionBehavior
     40           glsl_built_in_function_behavior);
     41 
     42  private:
     43   ShaderTranslatorCache();
     44   virtual ~ShaderTranslatorCache();
     45 
     46   friend struct DefaultSingletonTraits<ShaderTranslatorCache>;
     47 
     48   // Parameters passed into ShaderTranslator::Init
     49   struct ShaderTranslatorInitParams {
     50     ShShaderType shader_type;
     51     ShShaderSpec shader_spec;
     52     ShBuiltInResources resources;
     53     ShaderTranslatorInterface::GlslImplementationType
     54         glsl_implementation_type;
     55     ShaderTranslatorInterface::GlslBuiltInFunctionBehavior
     56         glsl_built_in_function_behavior;
     57 
     58     ShaderTranslatorInitParams(
     59         ShShaderType shader_type,
     60         ShShaderSpec shader_spec,
     61         const ShBuiltInResources& resources,
     62         ShaderTranslatorInterface::GlslImplementationType
     63             glsl_implementation_type,
     64         ShaderTranslatorInterface::GlslBuiltInFunctionBehavior
     65             glsl_built_in_function_behavior)
     66       : shader_type(shader_type),
     67         shader_spec(shader_spec),
     68         resources(resources),
     69         glsl_implementation_type(glsl_implementation_type),
     70         glsl_built_in_function_behavior(glsl_built_in_function_behavior) {
     71     }
     72 
     73     ShaderTranslatorInitParams(const ShaderTranslatorInitParams& params) {
     74       memcpy(this, &params, sizeof(*this));
     75     }
     76 
     77     bool operator== (const ShaderTranslatorInitParams& params) const {
     78       return memcmp(&params, this, sizeof(*this)) == 0;
     79     }
     80 
     81     bool operator< (const ShaderTranslatorInitParams& params) const {
     82       return memcmp(&params, this, sizeof(*this)) < 0;
     83     }
     84   };
     85 
     86   typedef std::map<ShaderTranslatorInitParams, ShaderTranslator* > Cache;
     87   Cache cache_;
     88 
     89   DISALLOW_COPY_AND_ASSIGN(ShaderTranslatorCache);
     90 };
     91 
     92 }  // namespace gles2
     93 }  // namespace gpu
     94 
     95 #endif  // GPU_COMMAND_BUFFER_SERVICE_SHADER_TRANSLATOR_CACHE_H_
     96