- add sources.
[platform/framework/web/crosswalk.git] / src / gpu / command_buffer / service / shader_translator_cache.cc
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     ShCompileOptions driver_bug_workarounds) {
38   ShaderTranslatorInitParams params(shader_type,
39                                     shader_spec,
40                                     *resources,
41                                     glsl_implementation_type,
42                                     driver_bug_workarounds);
43
44   Cache::iterator it = cache_.find(params);
45   if (it != cache_.end())
46     return it->second;
47
48   ShaderTranslator* translator = new ShaderTranslator();
49   if (translator->Init(shader_type, shader_spec, resources,
50                        glsl_implementation_type,
51                        driver_bug_workarounds)) {
52     cache_[params] = translator;
53     translator->AddDestructionObserver(this);
54     return translator;
55   } else {
56     return NULL;
57   }
58 }
59
60 }  // namespace gles2
61 }  // namespace gpu