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