Flutter Impeller
impeller::ContextMTL Class Referencefinal

#include <context_mtl.h>

Inheritance diagram for impeller::ContextMTL:
impeller::Context impeller::BackendCast< ContextMTL, Context >

Public Member Functions

 ~ContextMTL () override
 
BackendType GetBackendType () const override
 Get the graphics backend of an Impeller context. More...
 
id< MTLDevice > GetMTLDevice () const
 
std::string DescribeGpuModel () const override
 
bool IsValid () const override
 Determines if a context is valid. If the caller ever receives an invalid context, they must discard it and construct a new context. There is no recovery mechanism to repair a bad context. More...
 
std::shared_ptr< AllocatorGetResourceAllocator () const override
 Returns the allocator used to create textures and buffers on the device. More...
 
std::shared_ptr< ShaderLibraryGetShaderLibrary () const override
 Returns the library of shaders used to specify the programmable stages of a pipeline. More...
 
std::shared_ptr< SamplerLibraryGetSamplerLibrary () const override
 Returns the library of combined image samplers used in shaders. More...
 
std::shared_ptr< PipelineLibraryGetPipelineLibrary () const override
 Returns the library of pipelines used by render or compute commands. More...
 
std::shared_ptr< CommandBufferCreateCommandBuffer () const override
 Create a new command buffer. Command buffers can be used to encode graphics, blit, or compute commands to be submitted to the device. More...
 
std::shared_ptr< CommandQueueGetCommandQueue () const override
 Return the graphics queue for submitting command buffers. More...
 
const std::shared_ptr< const Capabilities > & GetCapabilities () const override
 Get the capabilities of Impeller context. All optionally supported feature of the platform, client-rendering API, and device can be queried using the Capabilities. More...
 
RuntimeStageBackend GetRuntimeStageBackend () const override
 Retrieve the runtime stage for this context type. More...
 
void SetCapabilities (const std::shared_ptr< const Capabilities > &capabilities)
 
bool UpdateOffscreenLayerPixelFormat (PixelFormat format) override
 
void Shutdown () override
 Force all pending asynchronous work to finish. This is achieved by deleting all owned concurrent message loops. More...
 
id< MTLCommandBuffer > CreateMTLCommandBuffer (const std::string &label) const
 
std::shared_ptr< const fml::SyncSwitch > GetIsGpuDisabledSyncSwitch () const
 
void StoreTaskForGPU (const fml::closure &task, const fml::closure &failure) override
 
void FlushTasksAwaitingGPU ()
 
- Public Member Functions inherited from impeller::Context
virtual ~Context ()
 Destroys an Impeller context. More...
 
virtual void InitializeCommonlyUsedShadersIfNeeded () const
 
virtual void DisposeThreadLocalCachedResources ()
 
virtual bool EnqueueCommandBuffer (std::shared_ptr< CommandBuffer > command_buffer)
 Enqueue command_buffer for submission by the end of the frame. More...
 
virtual bool FlushCommandBuffers ()
 Flush all pending command buffers. More...
 
virtual bool AddTrackingFence (const std::shared_ptr< Texture > &texture) const
 
virtual std::shared_ptr< const IdleWaiterGetIdleWaiter () const
 
virtual void ResetThreadLocalState () const
 
virtual bool SubmitOnscreen (std::shared_ptr< CommandBuffer > cmd_buffer)
 Submit the command buffer that renders to the onscreen surface. More...
 
const FlagsGetFlags () const
 

Static Public Member Functions

static std::shared_ptr< ContextMTLCreate (const Flags &flags, const std::vector< std::string > &shader_library_paths, std::shared_ptr< const fml::SyncSwitch > is_gpu_disabled_sync_switch)
 
static std::shared_ptr< ContextMTLCreate (const Flags &flags, const std::vector< std::shared_ptr< fml::Mapping >> &shader_libraries_data, std::shared_ptr< const fml::SyncSwitch > is_gpu_disabled_sync_switch, const std::string &label, std::optional< PixelFormat > pixel_format_override=std::nullopt)
 
static std::shared_ptr< ContextMTLCreate (const Flags &flags, id< MTLDevice > device, id< MTLCommandQueue > command_queue, const std::vector< std::shared_ptr< fml::Mapping >> &shader_libraries_data, std::shared_ptr< const fml::SyncSwitch > is_gpu_disabled_sync_switch, const std::string &label)
 
- Static Public Member Functions inherited from impeller::BackendCast< ContextMTL, Context >
static ContextMTLCast (Context &base)
 
static const ContextMTLCast (const Context &base)
 
static ContextMTLCast (Context *base)
 
static const ContextMTLCast (const Context *base)
 

Additional Inherited Members

- Public Types inherited from impeller::Context
enum class  BackendType {
  kMetal ,
  kOpenGLES ,
  kVulkan
}
 
- Static Public Attributes inherited from impeller::Context
static constexpr int32_t kMaxTasksAwaitingGPU = 1024
 
- Protected Member Functions inherited from impeller::Context
 Context (const Flags &flags)
 
- Protected Attributes inherited from impeller::Context
Flags flags_
 
std::vector< std::function< void()> > per_frame_task_
 

Detailed Description

Definition at line 65 of file context_mtl.h.

Constructor & Destructor Documentation

◆ ~ContextMTL()

impeller::ContextMTL::~ContextMTL ( )
override

Definition at line 294 of file context_mtl.mm.

294  {
295  is_gpu_disabled_sync_switch_->RemoveObserver(sync_switch_observer_.get());
296 }

Member Function Documentation

◆ Create() [1/3]

std::shared_ptr< ContextMTL > impeller::ContextMTL::Create ( const Flags flags,
const std::vector< std::shared_ptr< fml::Mapping >> &  shader_libraries_data,
std::shared_ptr< const fml::SyncSwitch >  is_gpu_disabled_sync_switch,
const std::string &  label,
std::optional< PixelFormat pixel_format_override = std::nullopt 
)
static

Definition at line 252 of file context_mtl.mm.

257  {
258  auto device = CreateMetalDevice();
259  auto command_queue = CreateMetalCommandQueue(device);
260  if (!command_queue) {
261  return nullptr;
262  }
263  auto context = std::shared_ptr<ContextMTL>(new ContextMTL(
264  flags, device, command_queue,
265  MTLShaderLibraryFromFileData(device, shader_libraries_data,
266  library_label),
267  std::move(is_gpu_disabled_sync_switch), pixel_format_override));
268  if (!context->IsValid()) {
269  FML_LOG(ERROR) << "Could not create Metal context.";
270  return nullptr;
271  }
272  return context;
273 }
static NSArray< id< MTLLibrary > > * MTLShaderLibraryFromFileData(id< MTLDevice > device, const std::vector< std::shared_ptr< fml::Mapping >> &libraries_data, const std::string &label)
Definition: context_mtl.mm:175
static id< MTLDevice > CreateMetalDevice()
Definition: context_mtl.mm:218
static id< MTLCommandQueue > CreateMetalCommandQueue(id< MTLDevice > device)
Definition: context_mtl.mm:222

References impeller::CreateMetalCommandQueue(), impeller::CreateMetalDevice(), and impeller::MTLShaderLibraryFromFileData().

◆ Create() [2/3]

std::shared_ptr< ContextMTL > impeller::ContextMTL::Create ( const Flags flags,
const std::vector< std::string > &  shader_library_paths,
std::shared_ptr< const fml::SyncSwitch >  is_gpu_disabled_sync_switch 
)
static

Definition at line 232 of file context_mtl.mm.

235  {
236  auto device = CreateMetalDevice();
237  auto command_queue = CreateMetalCommandQueue(device);
238  if (!command_queue) {
239  return nullptr;
240  }
241  auto context = std::shared_ptr<ContextMTL>(new ContextMTL(
242  flags, device, command_queue,
243  MTLShaderLibraryFromFilePaths(device, shader_library_paths),
244  std::move(is_gpu_disabled_sync_switch)));
245  if (!context->IsValid()) {
246  FML_LOG(ERROR) << "Could not create Metal context.";
247  return nullptr;
248  }
249  return context;
250 }
static NSArray< id< MTLLibrary > > * MTLShaderLibraryFromFilePaths(id< MTLDevice > device, const std::vector< std::string > &libraries_paths)
Definition: context_mtl.mm:152

References impeller::CreateMetalCommandQueue(), impeller::CreateMetalDevice(), and impeller::MTLShaderLibraryFromFilePaths().

Referenced by impeller::interop::ContextMTL::Create(), and impeller::PlaygroundImplMTL::PlaygroundImplMTL().

◆ Create() [3/3]

std::shared_ptr< ContextMTL > impeller::ContextMTL::Create ( const Flags flags,
id< MTLDevice >  device,
id< MTLCommandQueue >  command_queue,
const std::vector< std::shared_ptr< fml::Mapping >> &  shader_libraries_data,
std::shared_ptr< const fml::SyncSwitch >  is_gpu_disabled_sync_switch,
const std::string &  label 
)
static

Definition at line 275 of file context_mtl.mm.

281  {
282  auto context = std::shared_ptr<ContextMTL>(
283  new ContextMTL(flags, device, command_queue,
284  MTLShaderLibraryFromFileData(device, shader_libraries_data,
285  library_label),
286  std::move(is_gpu_disabled_sync_switch)));
287  if (!context->IsValid()) {
288  FML_LOG(ERROR) << "Could not create Metal context.";
289  return nullptr;
290  }
291  return context;
292 }

References impeller::MTLShaderLibraryFromFileData().

◆ CreateCommandBuffer()

std::shared_ptr< CommandBuffer > impeller::ContextMTL::CreateCommandBuffer ( ) const
overridevirtual

Create a new command buffer. Command buffers can be used to encode graphics, blit, or compute commands to be submitted to the device.

A command buffer can only be used on a single thread. Multi-threaded render, blit, or compute passes must create a new command buffer on each thread.

Returns
A new command buffer.

Implements impeller::Context.

Definition at line 328 of file context_mtl.mm.

328  {
329  return CreateCommandBufferInQueue(command_queue_);
330 }

Referenced by impeller::SurfaceMTL::PreparePresent().

◆ CreateMTLCommandBuffer()

id< MTLCommandBuffer > impeller::ContextMTL::CreateMTLCommandBuffer ( const std::string &  label) const

Definition at line 383 of file context_mtl.mm.

384  {
385  auto buffer = [command_queue_ commandBuffer];
386  if (!label.empty()) {
387  [buffer setLabel:@(label.data())];
388  }
389  return buffer;
390 }

Referenced by impeller::SurfaceMTL::Present().

◆ DescribeGpuModel()

std::string impeller::ContextMTL::DescribeGpuModel ( ) const
overridevirtual

Implements impeller::Context.

Definition at line 303 of file context_mtl.mm.

303  {
304  return std::string([[device_ name] UTF8String]);
305 }

◆ FlushTasksAwaitingGPU()

void impeller::ContextMTL::FlushTasksAwaitingGPU ( )

Definition at line 418 of file context_mtl.mm.

418  {
419  std::deque<PendingTasks> tasks_awaiting_gpu;
420  {
421  Lock lock(tasks_awaiting_gpu_mutex_);
422  std::swap(tasks_awaiting_gpu, tasks_awaiting_gpu_);
423  }
424  std::vector<PendingTasks> tasks_to_queue;
425  for (const auto& task : tasks_awaiting_gpu) {
426  is_gpu_disabled_sync_switch_->Execute(fml::SyncSwitch::Handlers()
427  .SetIfFalse([&] { task.task(); })
428  .SetIfTrue([&] {
429  // Lost access to the GPU
430  // immediately after it was
431  // activated. This may happen if
432  // the app was quickly
433  // foregrounded/backgrounded
434  // from a push notification.
435  // Store the tasks on the
436  // context again.
437  tasks_to_queue.push_back(task);
438  }));
439  }
440  if (!tasks_to_queue.empty()) {
441  Lock lock(tasks_awaiting_gpu_mutex_);
442  tasks_awaiting_gpu_.insert(tasks_awaiting_gpu_.end(),
443  tasks_to_queue.begin(), tasks_to_queue.end());
444  }
445 }

◆ GetBackendType()

Context::BackendType impeller::ContextMTL::GetBackendType ( ) const
overridevirtual

Get the graphics backend of an Impeller context.

        This is useful for cases where a renderer needs to track and
        lookup backend-specific resources, like shaders or uniform
        layout information.

        It's not recommended to use this as a substitute for
        per-backend capability checking. Instead, check for specific
        capabilities via `GetCapabilities()`.
Returns
The graphics backend of the Context.

Implements impeller::Context.

Definition at line 298 of file context_mtl.mm.

◆ GetCapabilities()

const std::shared_ptr< const Capabilities > & impeller::ContextMTL::GetCapabilities ( ) const
overridevirtual

Get the capabilities of Impeller context. All optionally supported feature of the platform, client-rendering API, and device can be queried using the Capabilities.

Returns
The capabilities. Can never be nullptr for a valid context.

Implements impeller::Context.

Definition at line 368 of file context_mtl.mm.

368  {
369  return device_capabilities_;
370 }

◆ GetCommandQueue()

std::shared_ptr< CommandQueue > impeller::ContextMTL::GetCommandQueue ( ) const
overridevirtual

Return the graphics queue for submitting command buffers.

Implements impeller::Context.

Definition at line 457 of file context_mtl.mm.

457  {
458  return command_queue_ip_;
459 }

◆ GetIsGpuDisabledSyncSwitch()

std::shared_ptr< const fml::SyncSwitch > impeller::ContextMTL::GetIsGpuDisabledSyncSwitch ( ) const

Definition at line 341 of file context_mtl.mm.

342  {
343  return is_gpu_disabled_sync_switch_;
344 }

◆ GetMTLDevice()

id< MTLDevice > impeller::ContextMTL::GetMTLDevice ( ) const

Definition at line 364 of file context_mtl.mm.

364  {
365  return device_;
366 }

Referenced by impeller::PlaygroundImplMTL::PlaygroundImplMTL().

◆ GetPipelineLibrary()

std::shared_ptr< PipelineLibrary > impeller::ContextMTL::GetPipelineLibrary ( ) const
overridevirtual

Returns the library of pipelines used by render or compute commands.

Returns
The pipeline library. Can never be nullptr for a valid context.

Implements impeller::Context.

Definition at line 318 of file context_mtl.mm.

318  {
319  return pipeline_library_;
320 }

◆ GetResourceAllocator()

std::shared_ptr< Allocator > impeller::ContextMTL::GetResourceAllocator ( ) const
overridevirtual

Returns the allocator used to create textures and buffers on the device.

Returns
The resource allocator. Can never be nullptr for a valid context.

Implements impeller::Context.

Definition at line 360 of file context_mtl.mm.

360  {
361  return resource_allocator_;
362 }

◆ GetRuntimeStageBackend()

RuntimeStageBackend impeller::ContextMTL::GetRuntimeStageBackend ( ) const
overridevirtual

Retrieve the runtime stage for this context type.

This is used by the engine shell and other subsystems for loading the correct shader types.

Implements impeller::Context.

Definition at line 462 of file context_mtl.mm.

References impeller::kMetal.

◆ GetSamplerLibrary()

std::shared_ptr< SamplerLibrary > impeller::ContextMTL::GetSamplerLibrary ( ) const
overridevirtual

Returns the library of combined image samplers used in shaders.

Returns
The sampler library. Can never be nullptr for a valid context.

Implements impeller::Context.

Definition at line 323 of file context_mtl.mm.

323  {
324  return sampler_library_;
325 }

◆ GetShaderLibrary()

std::shared_ptr< ShaderLibrary > impeller::ContextMTL::GetShaderLibrary ( ) const
overridevirtual

Returns the library of shaders used to specify the programmable stages of a pipeline.

Returns
The shader library. Can never be nullptr for a valid context.

Implements impeller::Context.

Definition at line 313 of file context_mtl.mm.

313  {
314  return shader_library_;
315 }

◆ IsValid()

bool impeller::ContextMTL::IsValid ( ) const
overridevirtual

Determines if a context is valid. If the caller ever receives an invalid context, they must discard it and construct a new context. There is no recovery mechanism to repair a bad context.

It is convention in Impeller to never return an invalid context from a call that returns an pointer to a context. The call implementation performs validity checks itself and return a null context instead of a pointer to an invalid context.

How a context goes invalid is backend specific. It could happen due to device loss, or any other unrecoverable error.

Returns
If the context is valid.

Implements impeller::Context.

Definition at line 308 of file context_mtl.mm.

308  {
309  return is_valid_;
310 }

◆ SetCapabilities()

void impeller::ContextMTL::SetCapabilities ( const std::shared_ptr< const Capabilities > &  capabilities)

Definition at line 372 of file context_mtl.mm.

373  {
374  device_capabilities_ = capabilities;
375 }

◆ Shutdown()

void impeller::ContextMTL::Shutdown ( )
overridevirtual

Force all pending asynchronous work to finish. This is achieved by deleting all owned concurrent message loops.

Implements impeller::Context.

Definition at line 333 of file context_mtl.mm.

333 {}

◆ StoreTaskForGPU()

void impeller::ContextMTL::StoreTaskForGPU ( const fml::closure &  task,
const fml::closure &  failure 
)
overridevirtual

Stores a task on the ContextMTL that is awaiting access for the GPU.

The task will be executed in the event that the GPU access has changed to being available or that the task has been canceled. The task should operate with the SyncSwitch to make sure the GPU is accessible.

If the queue of pending tasks is cleared without GPU access, then the failure callback will be invoked and the primary task function will not

Threadsafe.

task will be executed on the platform thread.

Reimplemented from impeller::Context.

Definition at line 392 of file context_mtl.mm.

393  {
394  std::vector<PendingTasks> failed_tasks;
395  {
396  Lock lock(tasks_awaiting_gpu_mutex_);
397  tasks_awaiting_gpu_.push_back(PendingTasks{task, failure});
398  int32_t failed_task_count =
399  tasks_awaiting_gpu_.size() - kMaxTasksAwaitingGPU;
400  if (failed_task_count > 0) {
401  failed_tasks.reserve(failed_task_count);
402  failed_tasks.insert(failed_tasks.end(),
403  std::make_move_iterator(tasks_awaiting_gpu_.begin()),
404  std::make_move_iterator(tasks_awaiting_gpu_.begin() +
405  failed_task_count));
406  tasks_awaiting_gpu_.erase(
407  tasks_awaiting_gpu_.begin(),
408  tasks_awaiting_gpu_.begin() + failed_task_count);
409  }
410  }
411  for (const PendingTasks& task : failed_tasks) {
412  if (task.failure) {
413  task.failure();
414  }
415  }
416 }
static constexpr int32_t kMaxTasksAwaitingGPU
Definition: context.h:79

◆ UpdateOffscreenLayerPixelFormat()

bool impeller::ContextMTL::UpdateOffscreenLayerPixelFormat ( PixelFormat  format)
overridevirtual

Reimplemented from impeller::Context.

Definition at line 378 of file context_mtl.mm.

378  {
379  device_capabilities_ = InferMetalCapabilities(device_, format);
380  return true;
381 }
static std::unique_ptr< Capabilities > InferMetalCapabilities(id< MTLDevice > device, PixelFormat color_format)
Definition: context_mtl.mm:52

References impeller::InferMetalCapabilities().


The documentation for this class was generated from the following files: