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 #include "gpu/command_buffer/service/shader_translator_cache.h"
      6 
      7 namespace gpu {
      8 namespace gles2 {
      9 
     10 ShaderTranslatorCache* ShaderTranslatorCache::GetInstance() {
     11   return Singleton<ShaderTranslatorCache>::get();
     12 }
     13 
     14 ShaderTranslatorCache::ShaderTranslatorCache() {
     15 }
     16 
     17 ShaderTranslatorCache::~ShaderTranslatorCache() {
     18 }
     19 
     20 void ShaderTranslatorCache::OnDestruct(ShaderTranslator* translator) {
     21   Cache::iterator it = cache_.begin();
     22   while (it != cache_.end()) {
     23     if (it->second == translator) {
     24       cache_.erase(it);
     25       return;
     26     }
     27     it++;
     28   }
     29 }
     30 
     31 scoped_refptr<ShaderTranslator> ShaderTranslatorCache::GetTranslator(
     32     ShShaderType shader_type,
     33     ShShaderSpec shader_spec,
     34     const ShBuiltInResources* resources,
     35     ShaderTranslatorInterface::GlslImplementationType
     36         glsl_implementation_type,
     37     ShaderTranslatorInterface::GlslBuiltInFunctionBehavior
     38         glsl_built_in_function_behavior) {
     39   ShaderTranslatorInitParams params(shader_type,
     40                                     shader_spec,
     41                                     *resources,
     42                                     glsl_implementation_type,
     43                                     glsl_built_in_function_behavior);
     44 
     45   Cache::iterator it = cache_.find(params);
     46   if (it != cache_.end())
     47     return it->second;
     48 
     49   ShaderTranslator* translator = new ShaderTranslator();
     50   if (translator->Init(shader_type, shader_spec, resources,
     51                        glsl_implementation_type,
     52                        glsl_built_in_function_behavior)) {
     53     cache_[params] = translator;
     54     translator->AddDestructionObserver(this);
     55     return translator;
     56   } else {
     57     return NULL;
     58   }
     59 }
     60 
     61 }  // namespace gles2
     62 }  // namespace gpu
     63