Flutter Impeller
pipeline_library_gles.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter 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 FLUTTER_IMPELLER_RENDERER_BACKEND_GLES_PIPELINE_LIBRARY_GLES_H_
6 #define FLUTTER_IMPELLER_RENDERER_BACKEND_GLES_PIPELINE_LIBRARY_GLES_H_
7 
8 #include <unordered_map>
9 #include <vector>
10 
11 #include "flutter/fml/hash_combine.h"
12 #include "impeller/base/thread.h"
17 
18 namespace impeller {
19 
20 class ContextGLES;
21 class PipelineGLES;
22 
24  : public PipelineLibrary,
25  public BackendCast<PipelineLibraryGLES, PipelineLibrary> {
26  public:
27  // |PipelineLibrary|
29 
31 
33 
34  private:
35  friend ContextGLES;
36 
37  //----------------------------------------------------------------------------
38  /// @brief A subset of the items in a pipeline descriptor (and the items
39  /// they reference in shader libraries) whose dynamism requires a
40  /// program object re-compilation and link. In all other cases,
41  /// creating a pipeline variant reuses an existing (compatible)
42  /// program object.
43  ///
44  struct ProgramKey {
45  std::shared_ptr<const ShaderFunction> vertex_shader;
46  std::shared_ptr<const ShaderFunction> fragment_shader;
47  //--------------------------------------------------------------------------
48  /// Specialization constants used in the shaders affect defines used when
49  /// compiling and linking the program.
50  ///
51  std::vector<Scalar> specialization_constants;
52 
53  ProgramKey(std::shared_ptr<const ShaderFunction> p_vertex_shader,
54  std::shared_ptr<const ShaderFunction> p_fragment_shader,
55  std::vector<Scalar> p_specialization_constants)
56  : vertex_shader(std::move(p_vertex_shader)),
57  fragment_shader(std::move(p_fragment_shader)),
58  specialization_constants(std::move(p_specialization_constants)) {}
59 
60  struct Hash {
61  std::size_t operator()(const ProgramKey& key) const {
62  auto seed = fml::HashCombine();
63  if (key.vertex_shader) {
64  fml::HashCombineSeed(seed, key.vertex_shader->GetHash());
65  }
66  if (key.fragment_shader) {
67  fml::HashCombineSeed(seed, key.fragment_shader->GetHash());
68  }
69  for (const auto& constant : key.specialization_constants) {
70  fml::HashCombineSeed(seed, constant);
71  }
72  return seed;
73  }
74  };
75 
76  struct Equal {
77  bool operator()(const ProgramKey& lhs, const ProgramKey& rhs) const {
78  return DeepComparePointer(lhs.vertex_shader, rhs.vertex_shader) &&
79  DeepComparePointer(lhs.fragment_shader, rhs.fragment_shader) &&
80  lhs.specialization_constants == rhs.specialization_constants;
81  }
82  };
83  };
84 
85  using ProgramMap = std::unordered_map<ProgramKey,
86  std::shared_ptr<UniqueHandleGLES>,
87  ProgramKey::Hash,
88  ProgramKey::Equal>;
89 
90  std::shared_ptr<ReactorGLES> reactor_;
91  PipelineMap pipelines_;
92  Mutex programs_mutex_;
93  ProgramMap programs_ IPLR_GUARDED_BY(programs_mutex_);
94 
95  explicit PipelineLibraryGLES(std::shared_ptr<ReactorGLES> reactor);
96 
97  // |PipelineLibrary|
98  bool IsValid() const override;
99 
100  // |PipelineLibrary|
102  bool async) override;
103 
104  // |PipelineLibrary|
106  ComputePipelineDescriptor descriptor,
107  bool async) override;
108 
109  // |PipelineLibrary|
110  bool HasPipeline(const PipelineDescriptor& descriptor) override;
111 
112  // |PipelineLibrary|
113  void RemovePipelinesWithEntryPoint(
114  std::shared_ptr<const ShaderFunction> function) override;
115 
116  const std::shared_ptr<ReactorGLES>& GetReactor() const;
117 
118  static std::shared_ptr<PipelineGLES> CreatePipeline(
119  const std::weak_ptr<PipelineLibrary>& weak_library,
120  const PipelineDescriptor& desc,
121  const std::shared_ptr<const ShaderFunction>& vert_shader,
122  const std::shared_ptr<const ShaderFunction>& frag_shader);
123 
124  std::shared_ptr<UniqueHandleGLES> GetProgramForKey(const ProgramKey& key);
125 
126  void SetProgramForKey(const ProgramKey& key,
127  std::shared_ptr<UniqueHandleGLES> program);
128 };
129 
130 } // namespace impeller
131 
132 #endif // FLUTTER_IMPELLER_RENDERER_BACKEND_GLES_PIPELINE_LIBRARY_GLES_H_
PipelineLibraryGLES(const PipelineLibraryGLES &)=delete
PipelineLibraryGLES & operator=(const PipelineLibraryGLES &)=delete
std::unordered_map< PipelineDescriptor, PipelineFuture< PipelineDescriptor >, ComparableHash< PipelineDescriptor >, ComparableEqual< PipelineDescriptor > > PipelineMap
bool DeepComparePointer(const std::shared_ptr< ComparableType > &lhs, const std::shared_ptr< ComparableType > &rhs)
Definition: comparable.h:57
bool operator()(const ProgramKey &lhs, const ProgramKey &rhs) const
std::size_t operator()(const ProgramKey &key) const