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 #pragma once
6 
7 #include <Metal/Metal.h>
8 
9 #include <string>
10 #include <vector>
11 
12 #include "flutter/fml/concurrent_message_loop.h"
13 #include "flutter/fml/macros.h"
14 #include "flutter/fml/synchronization/sync_switch.h"
16 #include "impeller/core/sampler.h"
23 
24 #if TARGET_OS_SIMULATOR
25 #define IMPELLER_CA_METAL_LAYER_AVAILABLE API_AVAILABLE(macos(10.11), ios(13.0))
26 #else // TARGET_OS_SIMULATOR
27 #define IMPELLER_CA_METAL_LAYER_AVAILABLE API_AVAILABLE(macos(10.11), ios(8.0))
28 #endif // TARGET_OS_SIMULATOR
29 
30 namespace impeller {
31 
32 class ContextMTL final : public Context,
33  public BackendCast<ContextMTL, Context>,
34  public std::enable_shared_from_this<ContextMTL> {
35  public:
36  static std::shared_ptr<ContextMTL> Create(
37  const std::vector<std::string>& shader_library_paths,
38  std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch);
39 
40  static std::shared_ptr<ContextMTL> Create(
41  const std::vector<std::shared_ptr<fml::Mapping>>& shader_libraries_data,
42  std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch,
43  const std::string& label);
44 
45  static std::shared_ptr<ContextMTL> Create(
46  id<MTLDevice> device,
47  id<MTLCommandQueue> command_queue,
48  const std::vector<std::shared_ptr<fml::Mapping>>& shader_libraries_data,
49  std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch,
50  const std::string& label);
51 
52  // |Context|
53  ~ContextMTL() override;
54 
55  // |Context|
56  BackendType GetBackendType() const override;
57 
58  id<MTLDevice> GetMTLDevice() const;
59 
60  // |Context|
61  std::string DescribeGpuModel() const override;
62 
63  // |Context|
64  bool IsValid() const override;
65 
66  // |Context|
67  std::shared_ptr<Allocator> GetResourceAllocator() const override;
68 
69  // |Context|
70  std::shared_ptr<ShaderLibrary> GetShaderLibrary() const override;
71 
72  // |Context|
73  std::shared_ptr<SamplerLibrary> GetSamplerLibrary() const override;
74 
75  // |Context|
76  std::shared_ptr<PipelineLibrary> GetPipelineLibrary() const override;
77 
78  // |Context|
79  std::shared_ptr<CommandBuffer> CreateCommandBuffer() const override;
80 
81  // |Context|
82  const std::shared_ptr<const Capabilities>& GetCapabilities() const override;
83 
84  // |Context|
85  bool UpdateOffscreenLayerPixelFormat(PixelFormat format) override;
86 
87  // |Context|
88  void Shutdown() override;
89 
90  id<MTLCommandBuffer> CreateMTLCommandBuffer(const std::string& label) const;
91 
92  const std::shared_ptr<fml::ConcurrentTaskRunner> GetWorkerTaskRunner() const;
93 
94  std::shared_ptr<const fml::SyncSwitch> GetIsGpuDisabledSyncSwitch() const;
95 
96  private:
97  id<MTLDevice> device_ = nullptr;
98  id<MTLCommandQueue> command_queue_ = nullptr;
99  std::shared_ptr<ShaderLibraryMTL> shader_library_;
100  std::shared_ptr<PipelineLibraryMTL> pipeline_library_;
101  std::shared_ptr<SamplerLibrary> sampler_library_;
102  std::shared_ptr<AllocatorMTL> resource_allocator_;
103  std::shared_ptr<const Capabilities> device_capabilities_;
104  std::shared_ptr<fml::ConcurrentMessageLoop> raster_message_loop_;
105  std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch_;
106  bool is_valid_ = false;
107 
108  ContextMTL(
109  id<MTLDevice> device,
110  id<MTLCommandQueue> command_queue,
111  NSArray<id<MTLLibrary>>* shader_libraries,
112  std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch);
113 
114  std::shared_ptr<CommandBuffer> CreateCommandBufferInQueue(
115  id<MTLCommandQueue> queue) const;
116 
117  FML_DISALLOW_COPY_AND_ASSIGN(ContextMTL);
118 };
119 
120 } // namespace impeller
impeller::ContextMTL::GetIsGpuDisabledSyncSwitch
std::shared_ptr< const fml::SyncSwitch > GetIsGpuDisabledSyncSwitch() const
Definition: context_mtl.mm:333
impeller::ContextMTL::GetWorkerTaskRunner
const std::shared_ptr< fml::ConcurrentTaskRunner > GetWorkerTaskRunner() const
Definition: context_mtl.mm:329
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:314
allocator_mtl.h
impeller::ContextMTL::GetBackendType
BackendType GetBackendType() const override
Get the graphics backend of an Impeller context.
Definition: context_mtl.mm:289
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:229
sampler.h
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:352
shader_library_mtl.h
impeller::ContextMTL::UpdateOffscreenLayerPixelFormat
bool UpdateOffscreenLayerPixelFormat(PixelFormat format) override
Definition: context_mtl.mm:365
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:324
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:299
pipeline_library_mtl.h
impeller::ContextMTL::GetMTLDevice
id< MTLDevice > GetMTLDevice() const
Definition: context_mtl.mm:356
impeller::Context
To do anything rendering related with Impeller, you need a context.
Definition: context.h:47
impeller::BackendCast
Definition: backend_cast.h:12
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:309
impeller::ContextMTL::DescribeGpuModel
std::string DescribeGpuModel() const override
Definition: context_mtl.mm:294
impeller::ContextMTL::CreateMTLCommandBuffer
id< MTLCommandBuffer > CreateMTLCommandBuffer(const std::string &label) const
Definition: context_mtl.mm:370
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:360
impeller::PixelFormat
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition: formats.h:94
impeller::ContextMTL
Definition: context_mtl.h:32
impeller
Definition: aiks_context.cc:10
impeller::ContextMTL::~ContextMTL
~ContextMTL() override
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:319
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:304