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/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       ShCompileOptions driver_bug_workarounds);
     40 
     41  private:
     42   ShaderTranslatorCache();
     43   virtual ~ShaderTranslatorCache();
     44 
     45   friend struct DefaultSingletonTraits<ShaderTranslatorCache>;
     46 
     47   // Parameters passed into ShaderTranslator::Init
     48   struct ShaderTranslatorInitParams {
     49     ShShaderType shader_type;
     50     ShShaderSpec shader_spec;
     51     ShBuiltInResources resources;
     52     ShaderTranslatorInterface::GlslImplementationType
     53         glsl_implementation_type;
     54     ShCompileOptions driver_bug_workarounds;
     55 
     56     ShaderTranslatorInitParams(
     57         ShShaderType shader_type,
     58         ShShaderSpec shader_spec,
     59         const ShBuiltInResources& resources,
     60         ShaderTranslatorInterface::GlslImplementationType
     61             glsl_implementation_type,
     62         ShCompileOptions driver_bug_workarounds)
     63       : shader_type(shader_type),
     64         shader_spec(shader_spec),
     65         resources(resources),
     66         glsl_implementation_type(glsl_implementation_type),
     67         driver_bug_workarounds(driver_bug_workarounds) {
     68     }
     69 
     70     ShaderTranslatorInitParams(const ShaderTranslatorInitParams& params) {
     71       memcpy(this, &params, sizeof(*this));
     72     }
     73 
     74     bool operator== (const ShaderTranslatorInitParams& params) const {
     75       return memcmp(&params, this, sizeof(*this)) == 0;
     76     }
     77 
     78     bool operator< (const ShaderTranslatorInitParams& params) const {
     79       return memcmp(&params, this, sizeof(*this)) < 0;
     80     }
     81   };
     82 
     83   typedef std::map<ShaderTranslatorInitParams, ShaderTranslator* > Cache;
     84   Cache cache_;
     85 
     86   DISALLOW_COPY_AND_ASSIGN(ShaderTranslatorCache);
     87 };
     88 
     89 }  // namespace gles2
     90 }  // namespace gpu
     91 
     92 #endif  // GPU_COMMAND_BUFFER_SERVICE_SHADER_TRANSLATOR_CACHE_H_
     93