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.
5 #ifndef GPU_COMMAND_BUFFER_SERVICE_SHADER_TRANSLATOR_CACHE_H_
6 #define GPU_COMMAND_BUFFER_SERVICE_SHADER_TRANSLATOR_CACHE_H_
12 #include "base/memory/ref_counted.h"
13 #include "gpu/command_buffer/service/shader_translator.h"
14 #include "third_party/angle/include/GLSLANG/ShaderLang.h"
19 // This class is not thread safe and can only be created and destroyed
20 // on a single thread. But it is safe to use two independent instances on two
21 // threads without synchronization.
23 // TODO(backer): Investigate using glReleaseShaderCompiler as an alternative to
25 class GPU_EXPORT ShaderTranslatorCache
26 : public base::RefCounted
<ShaderTranslatorCache
>,
27 public NON_EXPORTED_BASE(ShaderTranslator::DestructionObserver
) {
29 ShaderTranslatorCache();
31 // ShaderTranslator::DestructionObserver implementation
32 void OnDestruct(ShaderTranslator
* translator
) override
;
34 scoped_refptr
<ShaderTranslator
> GetTranslator(
35 sh::GLenum shader_type
,
36 ShShaderSpec shader_spec
,
37 const ShBuiltInResources
* resources
,
38 ShShaderOutput shader_output_language
,
39 ShCompileOptions driver_bug_workarounds
);
42 friend class base::RefCounted
<ShaderTranslatorCache
>;
43 friend class ShaderTranslatorCacheTest_InitParamComparable_Test
;
44 ~ShaderTranslatorCache() override
;
46 // Parameters passed into ShaderTranslator::Init
47 struct ShaderTranslatorInitParams
{
48 sh::GLenum shader_type
;
49 ShShaderSpec shader_spec
;
50 ShBuiltInResources resources
;
51 ShShaderOutput shader_output_language
;
52 ShCompileOptions driver_bug_workarounds
;
54 ShaderTranslatorInitParams(sh::GLenum shader_type
,
55 ShShaderSpec shader_spec
,
56 const ShBuiltInResources
& resources
,
57 ShShaderOutput shader_output_language
,
58 ShCompileOptions driver_bug_workarounds
) {
59 memset(this, 0, sizeof(*this));
60 this->shader_type
= shader_type
;
61 this->shader_spec
= shader_spec
;
62 this->resources
= resources
;
63 this->shader_output_language
= shader_output_language
;
64 this->driver_bug_workarounds
= driver_bug_workarounds
;
67 ShaderTranslatorInitParams(const ShaderTranslatorInitParams
& params
) {
68 memcpy(this, ¶ms
, sizeof(*this));
71 bool operator== (const ShaderTranslatorInitParams
& params
) const {
72 return memcmp(¶ms
, this, sizeof(*this)) == 0;
75 bool operator< (const ShaderTranslatorInitParams
& params
) const {
76 return memcmp(¶ms
, this, sizeof(*this)) < 0;
80 ShaderTranslatorInitParams();
81 ShaderTranslatorInitParams
& operator=(const ShaderTranslatorInitParams
&);
84 typedef std::map
<ShaderTranslatorInitParams
, ShaderTranslator
* > Cache
;
87 DISALLOW_COPY_AND_ASSIGN(ShaderTranslatorCache
);
93 #endif // GPU_COMMAND_BUFFER_SERVICE_SHADER_TRANSLATOR_CACHE_H_