Flutter Impeller
shader_library_mtl.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_METAL_SHADER_LIBRARY_MTL_H_
6 #define FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_SHADER_LIBRARY_MTL_H_
7 
8 #include <Foundation/Foundation.h>
9 #include <Metal/Metal.h>
10 
11 #include <memory>
12 #include <string>
13 #include <unordered_map>
14 
16 #include "impeller/base/thread.h"
19 
20 namespace impeller {
21 
22 class ShaderLibraryMTL final : public ShaderLibrary {
23  public:
25 
26  // |ShaderLibrary|
27  ~ShaderLibraryMTL() override;
28 
29  // |ShaderLibrary|
30  bool IsValid() const override;
31 
32  private:
33  friend class ContextMTL;
34 
35  UniqueID library_id_;
36  mutable RWMutex libraries_mutex_;
37  NSMutableArray<id<MTLLibrary>>* libraries_ IPLR_GUARDED_BY(libraries_mutex_) =
38  nullptr;
39  ShaderFunctionMap functions_;
40  bool is_valid_ = false;
41 
42  explicit ShaderLibraryMTL(NSArray<id<MTLLibrary>>* libraries);
43 
44  // |ShaderLibrary|
45  std::shared_ptr<const ShaderFunction> GetFunction(std::string_view name,
46  ShaderStage stage) override;
47 
48  // |ShaderLibrary|
49  void RegisterFunction(std::string name,
50  ShaderStage stage,
51  std::shared_ptr<fml::Mapping> code,
52  RegistrationCallback callback) override;
53 
54  // |ShaderLibrary|
55  void UnregisterFunction(std::string name, ShaderStage stage) override;
56 
57  id<MTLDevice> GetDevice() const;
58 
59  // Adds the just-compiled `library` to `libraries_` and caches its
60  // stage-matching function under `ShaderKey(name, stage)` in `functions_`
61  // so that namespaced registration names (e.g. `re:<library_id>:<entry>`)
62  // resolve via the cache rather than via the MSL function-name lookup that
63  // `GetFunction` falls back to.
64  void RegisterLibraryAndCacheFunction(id<MTLLibrary> library,
65  const std::string& name,
66  ShaderStage stage);
67 
68  ShaderLibraryMTL(const ShaderLibraryMTL&) = delete;
69 
70  ShaderLibraryMTL& operator=(const ShaderLibraryMTL&) = delete;
71 };
72 
73 } // namespace impeller
74 
75 #endif // FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_SHADER_LIBRARY_MTL_H_
std::function< void(bool)> RegistrationCallback
bool IsValid() const override
std::unordered_map< ShaderKey, std::shared_ptr< const ShaderFunction >, ShaderKey::Hash, ShaderKey::Equal > ShaderFunctionMap
Definition: shader_key.h:74
#define IPLR_GUARDED_BY(x)
Definition: thread_safety.h:19