Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
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.
 
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.
 
std::shared_ptr< AllocatorGetResourceAllocator () const override
 Returns the allocator used to create textures and buffers on the device.
 
std::shared_ptr< ShaderLibraryGetShaderLibrary () const override
 Returns the library of shaders used to specify the programmable stages of a pipeline.
 
std::shared_ptr< SamplerLibraryGetSamplerLibrary () const override
 Returns the library of combined image samplers used in shaders.
 
std::shared_ptr< PipelineLibraryGetPipelineLibrary () const override
 Returns the library of pipelines used by render or compute commands.
 
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.
 
std::shared_ptr< CommandQueueGetCommandQueue () const override
 Return the graphics queue for submitting command buffers.
 
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.
 
RuntimeStageBackend GetRuntimeStageBackend () const override
 Retrieve the runtime stage for this context type.
 
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.
 
id< MTLCommandBuffer > CreateMTLCommandBuffer (const std::string &label) const
 
std::shared_ptr< const fml::SyncSwitchGetIsGpuDisabledSyncSwitch () const
 
void StoreTaskForGPU (const fml::closure &task, const fml::closure &failure) override
 
void FlushTasksAwaitingGPU ()
 
bool FinishQueue () override
 Wait until all previously submitted command buffers are processed and displayed by the GPU.
 
- Public Member Functions inherited from impeller::Context
virtual ~Context ()
 Destroys an Impeller context.
 
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.
 
virtual bool FlushCommandBuffers ()
 Flush all pending command buffers.
 
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.
 
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 324 of file context_mtl.mm.

324 {
325 is_gpu_disabled_sync_switch_->RemoveObserver(sync_switch_observer_.get());
326}

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 282 of file context_mtl.mm.

287 {
288 auto device = CreateMetalDevice();
289 auto command_queue = CreateMetalCommandQueue(device);
290 if (!command_queue) {
291 return nullptr;
292 }
293 auto context = std::shared_ptr<ContextMTL>(new ContextMTL(
294 flags, device, command_queue,
295 MTLShaderLibraryFromFileData(device, shader_libraries_data,
296 library_label),
297 std::move(is_gpu_disabled_sync_switch), pixel_format_override));
298 if (!context->IsValid()) {
299 FML_LOG(ERROR) << "Could not create Metal context.";
300 return nullptr;
301 }
302 return context;
303}
VkDevice device
Definition main.cc:69
#define FML_LOG(severity)
Definition logging.h:101
static id< MTLCommandQueue > CreateMetalCommandQueue(id< MTLDevice > device)
static id< MTLDevice > CreateMetalDevice()
static NSArray< id< MTLLibrary > > * MTLShaderLibraryFromFileData(id< MTLDevice > device, const std::vector< std::shared_ptr< fml::Mapping > > &libraries_data, const std::string &label)
std::shared_ptr< ContextGLES > context

References context, impeller::CreateMetalCommandQueue(), impeller::CreateMetalDevice(), device, FML_LOG, 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 262 of file context_mtl.mm.

265 {
266 auto device = CreateMetalDevice();
267 auto command_queue = CreateMetalCommandQueue(device);
268 if (!command_queue) {
269 return nullptr;
270 }
271 auto context = std::shared_ptr<ContextMTL>(new ContextMTL(
272 flags, device, command_queue,
273 MTLShaderLibraryFromFilePaths(device, shader_library_paths),
274 std::move(is_gpu_disabled_sync_switch)));
275 if (!context->IsValid()) {
276 FML_LOG(ERROR) << "Could not create Metal context.";
277 return nullptr;
278 }
279 return context;
280}
static NSArray< id< MTLLibrary > > * MTLShaderLibraryFromFilePaths(id< MTLDevice > device, const std::vector< std::string > &libraries_paths)

References context, impeller::CreateMetalCommandQueue(), impeller::CreateMetalDevice(), device, FML_LOG, and impeller::MTLShaderLibraryFromFilePaths().

Referenced by impeller::interop::ContextMTL::Create(), FlutterDarwinContextMetalImpeller::createExternalTextureWithIdentifier:texture:, flutter::testing::CreateImpellerContext(), CreateImpellerContext(), flutter::EmbedderSurfaceMetalImpeller::EmbedderSurfaceMetalImpeller(), flutter::TesterContextMTL::Initialize(), 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 305 of file context_mtl.mm.

311 {
312 auto context = std::shared_ptr<ContextMTL>(
313 new ContextMTL(flags, device, command_queue,
314 MTLShaderLibraryFromFileData(device, shader_libraries_data,
315 library_label),
316 std::move(is_gpu_disabled_sync_switch)));
317 if (!context->IsValid()) {
318 FML_LOG(ERROR) << "Could not create Metal context.";
319 return nullptr;
320 }
321 return context;
322}

References context, device, FML_LOG, and 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 358 of file context_mtl.mm.

358 {
359 return CreateCommandBufferInQueue(command_queue_);
360}

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

◆ CreateMTLCommandBuffer()

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

Definition at line 413 of file context_mtl.mm.

414 {
415 auto buffer = [command_queue_ commandBuffer];
416 if (!label.empty()) {
417 [buffer setLabel:@(label.data())];
418 }
419 return buffer;
420}
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set profile Make the profiler discard new samples once the profiler sample buffer is full When this flag is not the profiler sample buffer is used as a ring buffer
Definition switch_defs.h:98

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

◆ DescribeGpuModel()

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

Implements impeller::Context.

Definition at line 333 of file context_mtl.mm.

333 {
334 return std::string([[device_ name] UTF8String]);
335}
const char * name
Definition fuchsia.cc:50

References name.

◆ FinishQueue()

bool impeller::ContextMTL::FinishQueue ( )
overridevirtual

Wait until all previously submitted command buffers are processed and displayed by the GPU.

WARNING: This method call is unnecessary for nearly all use cases since asynchronous workloads are the expected norm and calling this method can negatively affect application performance. Its only use is for cases like benchmarking where proper performance metrics can only be collected by including all background processing of the GPU.

Outstanding unflushed command buffers are not committed, submitted, or enqueued by this method and such buffers should have already been flushed before this method is called. The method will only wait for work to be done that has already been submitted to the GPU.

Returns
True if the queue was successfully processed, otherwise false if there was an error or if the backend does not support synchronous reporting of completion. Implementations intended entirely for testing might simply return false, but they would not be used for benchmarking or other situations where we might measure GPU performance.

Implements impeller::Context.

Definition at line 477 of file context_mtl.mm.

477 {
478 id<MTLCommandBuffer> command_buffer =
479 ContextMTL::Cast(this)->CreateMTLCommandBuffer("Finish Queue Waiter");
480 [command_buffer commit];
481 // clang-format off
482 // This isn't documented in the method, but there are places where they
483 // imply that they will wait even for an empty buffer...
484 //
485 // See https://developer.apple.com/documentation/metalperformanceshaders/tuning-hints
486 // clang-format on
487 [command_buffer waitUntilCompleted];
488 return true;
489}
static ContextMTL & Cast(Context &base)
id< MTLCommandBuffer > CreateMTLCommandBuffer(const std::string &label) const
std::shared_ptr< CommandBuffer > command_buffer

References command_buffer.

◆ FlushTasksAwaitingGPU()

void impeller::ContextMTL::FlushTasksAwaitingGPU ( )

Definition at line 448 of file context_mtl.mm.

448 {
449 std::deque<PendingTasks> tasks_awaiting_gpu;
450 {
451 Lock lock(tasks_awaiting_gpu_mutex_);
452 std::swap(tasks_awaiting_gpu, tasks_awaiting_gpu_);
453 }
454 std::vector<PendingTasks> tasks_to_queue;
455 for (const auto& task : tasks_awaiting_gpu) {
456 is_gpu_disabled_sync_switch_->Execute(fml::SyncSwitch::Handlers()
457 .SetIfFalse([&] { task.task(); })
458 .SetIfTrue([&] {
459 // Lost access to the GPU
460 // immediately after it was
461 // activated. This may happen if
462 // the app was quickly
463 // foregrounded/backgrounded
464 // from a push notification.
465 // Store the tasks on the
466 // context again.
467 tasks_to_queue.push_back(task);
468 }));
469 }
470 if (!tasks_to_queue.empty()) {
471 Lock lock(tasks_awaiting_gpu_mutex_);
472 tasks_awaiting_gpu_.insert(tasks_awaiting_gpu_.end(),
473 tasks_to_queue.begin(), tasks_to_queue.end());
474 }
475}
Represents the 2 code paths available when calling |SyncSwitchExecute|.
Definition sync_switch.h:35

References fml::SyncSwitch::Handlers::SetIfFalse().

◆ 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 328 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 398 of file context_mtl.mm.

398 {
399 return device_capabilities_;
400}

◆ GetCommandQueue()

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

Return the graphics queue for submitting command buffers.

Implements impeller::Context.

Definition at line 501 of file context_mtl.mm.

501 {
502 return command_queue_ip_;
503}

◆ GetIsGpuDisabledSyncSwitch()

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

Definition at line 371 of file context_mtl.mm.

372 {
373 return is_gpu_disabled_sync_switch_;
374}

◆ GetMTLDevice()

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

Definition at line 394 of file context_mtl.mm.

394 {
395 return device_;
396}

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 348 of file context_mtl.mm.

348 {
349 return pipeline_library_;
350}

◆ 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 390 of file context_mtl.mm.

390 {
391 return resource_allocator_;
392}

◆ 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 506 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 353 of file context_mtl.mm.

353 {
354 return sampler_library_;
355}

◆ 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 343 of file context_mtl.mm.

343 {
344 return shader_library_;
345}

◆ 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 338 of file context_mtl.mm.

338 {
339 return is_valid_;
340}

◆ SetCapabilities()

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

Definition at line 402 of file context_mtl.mm.

403 {
404 device_capabilities_ = capabilities;
405}

◆ 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 363 of file context_mtl.mm.

363{}

◆ 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 422 of file context_mtl.mm.

423 {
424 std::vector<PendingTasks> failed_tasks;
425 {
426 Lock lock(tasks_awaiting_gpu_mutex_);
427 tasks_awaiting_gpu_.push_back(PendingTasks{task, failure});
428 int32_t failed_task_count =
429 tasks_awaiting_gpu_.size() - kMaxTasksAwaitingGPU;
430 if (failed_task_count > 0) {
431 failed_tasks.reserve(failed_task_count);
432 failed_tasks.insert(failed_tasks.end(),
433 std::make_move_iterator(tasks_awaiting_gpu_.begin()),
434 std::make_move_iterator(tasks_awaiting_gpu_.begin() +
435 failed_task_count));
436 tasks_awaiting_gpu_.erase(
437 tasks_awaiting_gpu_.begin(),
438 tasks_awaiting_gpu_.begin() + failed_task_count);
439 }
440 }
441 for (const PendingTasks& task : failed_tasks) {
442 if (task.failure) {
443 task.failure();
444 }
445 }
446}
static constexpr int32_t kMaxTasksAwaitingGPU
Definition context.h:83

◆ UpdateOffscreenLayerPixelFormat()

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

Reimplemented from impeller::Context.

Definition at line 408 of file context_mtl.mm.

408 {
409 device_capabilities_ = InferMetalCapabilities(device_, format);
410 return true;
411}
static std::unique_ptr< Capabilities > InferMetalCapabilities(id< MTLDevice > device, PixelFormat color_format)

References format, and impeller::InferMetalCapabilities().


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