Flutter Impeller
context_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_CONTEXT_MTL_H_
6 #define FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_CONTEXT_MTL_H_
7 
8 #include <Metal/Metal.h>
9 
10 #include <deque>
11 #include <string>
12 
13 #include "flutter/fml/concurrent_message_loop.h"
14 #include "flutter/fml/macros.h"
15 #include "flutter/fml/synchronization/sync_switch.h"
17 #include "impeller/core/sampler.h"
25 
26 #if TARGET_OS_SIMULATOR
27 #define IMPELLER_CA_METAL_LAYER_AVAILABLE API_AVAILABLE(macos(10.11), ios(13.0))
28 #else // TARGET_OS_SIMULATOR
29 #define IMPELLER_CA_METAL_LAYER_AVAILABLE API_AVAILABLE(macos(10.11), ios(8.0))
30 #endif // TARGET_OS_SIMULATOR
31 
32 namespace impeller {
33 
34 class ContextMTL final : public Context,
35  public BackendCast<ContextMTL, Context>,
36  public std::enable_shared_from_this<ContextMTL> {
37  public:
38  static std::shared_ptr<ContextMTL> Create(
39  const std::vector<std::string>& shader_library_paths,
40  std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch);
41 
42  static std::shared_ptr<ContextMTL> Create(
43  const std::vector<std::shared_ptr<fml::Mapping>>& shader_libraries_data,
44  std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch,
45  const std::string& label);
46 
47  static std::shared_ptr<ContextMTL> Create(
48  id<MTLDevice> device,
49  id<MTLCommandQueue> command_queue,
50  const std::vector<std::shared_ptr<fml::Mapping>>& shader_libraries_data,
51  std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch,
52  const std::string& label);
53 
54  // |Context|
55  ~ContextMTL() override;
56 
57  // |Context|
58  BackendType GetBackendType() const override;
59 
60  id<MTLDevice> GetMTLDevice() const;
61 
62  // |Context|
63  std::string DescribeGpuModel() const override;
64 
65  // |Context|
66  bool IsValid() const override;
67 
68  // |Context|
69  std::shared_ptr<Allocator> GetResourceAllocator() const override;
70 
71  // |Context|
72  std::shared_ptr<ShaderLibrary> GetShaderLibrary() const override;
73 
74  // |Context|
75  std::shared_ptr<SamplerLibrary> GetSamplerLibrary() const override;
76 
77  // |Context|
78  std::shared_ptr<PipelineLibrary> GetPipelineLibrary() const override;
79 
80  // |Context|
81  std::shared_ptr<CommandBuffer> CreateCommandBuffer() const override;
82 
83  // |Context|
84  const std::shared_ptr<const Capabilities>& GetCapabilities() const override;
85 
86  void SetCapabilities(const std::shared_ptr<const Capabilities>& capabilities);
87 
88  // |Context|
89  bool UpdateOffscreenLayerPixelFormat(PixelFormat format) override;
90 
91  // |Context|
92  void Shutdown() override;
93 
94  id<MTLCommandBuffer> CreateMTLCommandBuffer(const std::string& label) const;
95 
96  const std::shared_ptr<fml::ConcurrentTaskRunner> GetWorkerTaskRunner() const;
97 
98  std::shared_ptr<const fml::SyncSwitch> GetIsGpuDisabledSyncSwitch() const;
99 
100 #ifdef IMPELLER_DEBUG
101  std::shared_ptr<GPUTracerMTL> GetGPUTracer() const;
102 #endif // IMPELLER_DEBUG
103 
104  // |Context|
105  void StoreTaskForGPU(const std::function<void()>& task) override;
106 
107  private:
108  class SyncSwitchObserver : public fml::SyncSwitch::Observer {
109  public:
110  explicit SyncSwitchObserver(ContextMTL& parent);
111  virtual ~SyncSwitchObserver() = default;
112  void OnSyncSwitchUpdate(bool new_value) override;
113 
114  private:
115  ContextMTL& parent_;
116  };
117 
118  id<MTLDevice> device_ = nullptr;
119  id<MTLCommandQueue> command_queue_ = nullptr;
120  std::shared_ptr<ShaderLibraryMTL> shader_library_;
121  std::shared_ptr<PipelineLibraryMTL> pipeline_library_;
122  std::shared_ptr<SamplerLibrary> sampler_library_;
123  std::shared_ptr<AllocatorMTL> resource_allocator_;
124  std::shared_ptr<const Capabilities> device_capabilities_;
125  std::shared_ptr<fml::ConcurrentMessageLoop> raster_message_loop_;
126  std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch_;
127 #ifdef IMPELLER_DEBUG
128  std::shared_ptr<GPUTracerMTL> gpu_tracer_;
129 #endif // IMPELLER_DEBUG
130  std::deque<std::function<void()>> tasks_awaiting_gpu_;
131  std::unique_ptr<SyncSwitchObserver> sync_switch_observer_;
132  bool is_valid_ = false;
133 
134  ContextMTL(
135  id<MTLDevice> device,
136  id<MTLCommandQueue> command_queue,
137  NSArray<id<MTLLibrary>>* shader_libraries,
138  std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch);
139 
140  std::shared_ptr<CommandBuffer> CreateCommandBufferInQueue(
141  id<MTLCommandQueue> queue) const;
142 
143  void FlushTasksAwaitingGPU();
144 
145  ContextMTL(const ContextMTL&) = delete;
146 
147  ContextMTL& operator=(const ContextMTL&) = delete;
148 };
149 
150 } // namespace impeller
151 
152 #endif // FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_CONTEXT_MTL_H_
impeller::ContextMTL::GetIsGpuDisabledSyncSwitch
std::shared_ptr< const fml::SyncSwitch > GetIsGpuDisabledSyncSwitch() const
Definition: context_mtl.mm:348
impeller::ContextMTL::GetWorkerTaskRunner
const std::shared_ptr< fml::ConcurrentTaskRunner > GetWorkerTaskRunner() const
Definition: context_mtl.mm:344
impeller::Context::BackendType
BackendType
Definition: context.h:49
command_buffer_mtl.h
impeller::ContextMTL::GetSamplerLibrary
std::shared_ptr< SamplerLibrary > GetSamplerLibrary() const override
Returns the library of combined image samplers used in shaders.
Definition: context_mtl.mm:323
allocator_mtl.h
impeller::ContextMTL::GetBackendType
BackendType GetBackendType() const override
Get the graphics backend of an Impeller context.
Definition: context_mtl.mm:298
impeller::ContextMTL::Create
static std::shared_ptr< ContextMTL > Create(const std::vector< std::string > &shader_library_paths, std::shared_ptr< const fml::SyncSwitch > is_gpu_disabled_sync_switch)
Definition: context_mtl.mm:236
sampler.h
impeller::PixelFormat
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition: formats.h:94
impeller::ContextMTL::GetResourceAllocator
std::shared_ptr< Allocator > GetResourceAllocator() const override
Returns the allocator used to create textures and buffers on the device.
Definition: context_mtl.mm:367
impeller::ContextMTL::SetCapabilities
void SetCapabilities(const std::shared_ptr< const Capabilities > &capabilities)
Definition: context_mtl.mm:379
shader_library_mtl.h
impeller::ContextMTL::UpdateOffscreenLayerPixelFormat
bool UpdateOffscreenLayerPixelFormat(PixelFormat format) override
Definition: context_mtl.mm:385
backend_cast.h
capabilities.h
impeller::ContextMTL::Shutdown
void Shutdown() override
Force all pending asynchronous work to finish. This is achieved by deleting all owned concurrent mess...
Definition: context_mtl.mm:333
impeller::ContextMTL::IsValid
bool IsValid() const override
Determines if a context is valid. If the caller ever receives an invalid context, they must discard i...
Definition: context_mtl.mm:308
pipeline_library_mtl.h
impeller::ContextMTL::GetMTLDevice
id< MTLDevice > GetMTLDevice() const
Definition: context_mtl.mm:371
impeller::Context
To do anything rendering related with Impeller, you need a context.
Definition: context.h:47
impeller::BackendCast
Definition: backend_cast.h:13
impeller::ContextMTL::StoreTaskForGPU
void StoreTaskForGPU(const std::function< void()> &task) override
Definition: context_mtl.mm:399
impeller::ContextMTL::GetPipelineLibrary
std::shared_ptr< PipelineLibrary > GetPipelineLibrary() const override
Returns the library of pipelines used by render or compute commands.
Definition: context_mtl.mm:318
impeller::ContextMTL::DescribeGpuModel
std::string DescribeGpuModel() const override
Definition: context_mtl.mm:303
impeller::ContextMTL::CreateMTLCommandBuffer
id< MTLCommandBuffer > CreateMTLCommandBuffer(const std::string &label) const
Definition: context_mtl.mm:390
gpu_tracer_mtl.h
context.h
impeller::ContextMTL::GetCapabilities
const std::shared_ptr< const Capabilities > & GetCapabilities() const override
Get the capabilities of Impeller context. All optionally supported feature of the platform,...
Definition: context_mtl.mm:375
impeller::ContextMTL
Definition: context_mtl.h:34
impeller
Definition: aiks_context.cc:10
impeller::ContextMTL::~ContextMTL
~ContextMTL() override
Definition: context_mtl.mm:294
impeller::ContextMTL::CreateCommandBuffer
std::shared_ptr< CommandBuffer > CreateCommandBuffer() const override
Create a new command buffer. Command buffers can be used to encode graphics, blit,...
Definition: context_mtl.mm:328
impeller::ContextMTL::GetShaderLibrary
std::shared_ptr< ShaderLibrary > GetShaderLibrary() const override
Returns the library of shaders used to specify the programmable stages of a pipeline.
Definition: context_mtl.mm:313