Flutter Impeller
impeller::ContextVK Class Referencefinal

#include <context_vk.h>

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

Classes

struct  Settings
 

Public Member Functions

uint64_t GetHash () const
 
 ~ContextVK () override
 
BackendType GetBackendType () const override
 Get the graphics backend of an Impeller context. More...
 
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...
 
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...
 
const std::shared_ptr< YUVConversionLibraryVK > & GetYUVConversionLibrary () const
 
void Shutdown () override
 Force all pending asynchronous work to finish. This is achieved by deleting all owned concurrent message loops. More...
 
void SetOffscreenFormat (PixelFormat pixel_format)
 
template<typename T >
bool SetDebugName (T handle, std::string_view label) const
 
std::shared_ptr< DeviceHolderVKGetDeviceHolder () const
 
vk::Instance GetInstance () const
 
const vk::Device & GetDevice () const
 
const std::unique_ptr< DriverInfoVK > & GetDriverInfo () const
 
const std::shared_ptr< fml::ConcurrentTaskRunner > GetConcurrentWorkerTaskRunner () const
 
std::shared_ptr< SurfaceContextVKCreateSurfaceContext ()
 
const std::shared_ptr< QueueVK > & GetGraphicsQueue () const
 
vk::PhysicalDevice GetPhysicalDevice () const
 
std::shared_ptr< FenceWaiterVKGetFenceWaiter () const
 
std::shared_ptr< ResourceManagerVKGetResourceManager () const
 
std::shared_ptr< CommandPoolRecyclerVKGetCommandPoolRecycler () const
 
std::shared_ptr< DescriptorPoolRecyclerVKGetDescriptorPoolRecycler () const
 
std::shared_ptr< CommandQueueGetCommandQueue () const override
 Return the graphics queue for submitting command buffers. More...
 
std::shared_ptr< GPUTracerVKGetGPUTracer () const
 
void RecordFrameEndTime () const
 
void InitializeCommonlyUsedShadersIfNeeded () const override
 
void DisposeThreadLocalCachedResources () override
 
bool GetShouldDisableSurfaceControlSwapchain () const
 Whether the Android Surface control based swapchain should be disabled, even if the device is capable of supporting it. More...
 
- Public Member Functions inherited from impeller::Context
virtual ~Context ()
 Destroys an Impeller context. More...
 
virtual bool UpdateOffscreenLayerPixelFormat (PixelFormat format)
 
virtual void StoreTaskForGPU (const fml::closure &task, const fml::closure &failure)
 

Static Public Member Functions

static size_t ChooseThreadCountForWorkers (size_t hardware_concurrency)
 
static std::shared_ptr< ContextVKCreate (Settings settings)
 
template<typename T >
static bool SetDebugName (const vk::Device &device, T handle, std::string_view label)
 
- Static Public Member Functions inherited from impeller::BackendCast< ContextVK, Context >
static ContextVKCast (Context &base)
 
static const ContextVKCast (const Context &base)
 
static ContextVKCast (Context *base)
 
static const ContextVKCast (const Context *base)
 

Additional Inherited Members

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

Detailed Description

Definition at line 42 of file context_vk.h.

Constructor & Destructor Documentation

◆ ~ContextVK()

impeller::ContextVK::~ContextVK ( )
override

Definition at line 125 of file context_vk.cc.

125  {
126  if (device_holder_ && device_holder_->device) {
127  [[maybe_unused]] auto result = device_holder_->device->waitIdle();
128  }
130 }

References impeller::CommandPoolRecyclerVK::DestroyThreadLocalPools().

Member Function Documentation

◆ ChooseThreadCountForWorkers()

size_t impeller::ContextVK::ChooseThreadCountForWorkers ( size_t  hardware_concurrency)
static

Choose the number of worker threads the context_vk will create.

Visible for testing.

Definition at line 108 of file context_vk.cc.

108  {
109  // Never create more than 4 worker threads. Attempt to use up to
110  // half of the available concurrency.
111  return std::clamp(hardware_concurrency / 2ull, /*lo=*/1ull, /*hi=*/4ull);
112 }

Referenced by impeller::testing::TEST().

◆ Create()

std::shared_ptr< ContextVK > impeller::ContextVK::Create ( Settings  settings)
static

Definition at line 98 of file context_vk.cc.

98  {
99  auto context = std::shared_ptr<ContextVK>(new ContextVK());
100  context->Setup(std::move(settings));
101  if (!context->IsValid()) {
102  return nullptr;
103  }
104  return context;
105 }

Referenced by impeller::PlaygroundImplVK::PlaygroundImplVK().

◆ CreateCommandBuffer()

std::shared_ptr< CommandBuffer > impeller::ContextVK::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 492 of file context_vk.cc.

492  {
493  const auto& recycler = GetCommandPoolRecycler();
494  auto tls_pool = recycler->Get();
495  if (!tls_pool) {
496  return nullptr;
497  }
498 
499  auto tracked_objects = std::make_shared<TrackedObjectsVK>(
500  weak_from_this(), std::move(tls_pool), GetGPUTracer()->CreateGPUProbe());
501  auto queue = GetGraphicsQueue();
502 
503  if (!tracked_objects || !tracked_objects->IsValid() || !queue) {
504  return nullptr;
505  }
506 
507  vk::CommandBufferBeginInfo begin_info;
508  begin_info.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit;
509  if (tracked_objects->GetCommandBuffer().begin(begin_info) !=
510  vk::Result::eSuccess) {
511  VALIDATION_LOG << "Could not begin command buffer.";
512  return nullptr;
513  }
514 
515  tracked_objects->GetGPUProbe().RecordCmdBufferStart(
516  tracked_objects->GetCommandBuffer());
517 
518  return std::shared_ptr<CommandBufferVK>(new CommandBufferVK(
519  shared_from_this(), //
520  GetDeviceHolder(), //
521  std::move(tracked_objects), //
522  GetFenceWaiter() //
523  ));
524 }

References GetCommandPoolRecycler(), GetDeviceHolder(), GetFenceWaiter(), GetGPUTracer(), GetGraphicsQueue(), and VALIDATION_LOG.

Referenced by impeller::GPUTracerVK::InitializeQueryPool().

◆ CreateSurfaceContext()

std::shared_ptr< SurfaceContextVK > impeller::ContextVK::CreateSurfaceContext ( )

Definition at line 551 of file context_vk.cc.

551  {
552  return std::make_shared<SurfaceContextVK>(shared_from_this());
553 }

◆ DescribeGpuModel()

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

Implements impeller::Context.

Definition at line 468 of file context_vk.cc.

468  {
469  return device_name_;
470 }

◆ DisposeThreadLocalCachedResources()

void impeller::ContextVK::DisposeThreadLocalCachedResources ( )
overridevirtual

Dispose resources that are cached on behalf of the current thread.

Some backends such as Vulkan may cache resources that can be reused while executing a rendering operation. This API can be called after the operation completes in order to clear the cache.

Reimplemented from impeller::Context.

Definition at line 632 of file context_vk.cc.

632  {
633  command_pool_recycler_->Dispose();
634 }

◆ GetBackendType()

Context::BackendType impeller::ContextVK::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 132 of file context_vk.cc.

132  {
134 }

References impeller::Context::kVulkan.

◆ GetCapabilities()

const std::shared_ptr< const Capabilities > & impeller::ContextVK::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 555 of file context_vk.cc.

555  {
556  return device_capabilities_;
557 }

Referenced by impeller::AllocatedTextureSourceVK::AllocatedTextureSourceVK().

◆ GetCommandPoolRecycler()

std::shared_ptr< CommandPoolRecyclerVK > impeller::ContextVK::GetCommandPoolRecycler ( ) const

Definition at line 575 of file context_vk.cc.

576  {
577  return command_pool_recycler_;
578 }

Referenced by CreateCommandBuffer().

◆ GetCommandQueue()

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

Return the graphics queue for submitting command buffers.

Implements impeller::Context.

Definition at line 589 of file context_vk.cc.

589  {
590  return command_queue_vk_;
591 }

Referenced by impeller::GPUTracerVK::InitializeQueryPool().

◆ GetConcurrentWorkerTaskRunner()

const std::shared_ptr< fml::ConcurrentTaskRunner > impeller::ContextVK::GetConcurrentWorkerTaskRunner ( ) const

Definition at line 535 of file context_vk.cc.

535  {
536  return raster_message_loop_->GetTaskRunner();
537 }

◆ GetDescriptorPoolRecycler()

std::shared_ptr< DescriptorPoolRecyclerVK > impeller::ContextVK::GetDescriptorPoolRecycler ( ) const

Definition at line 584 of file context_vk.cc.

585  {
586  return descriptor_pool_recycler_;
587 }

◆ GetDevice()

const vk::Device & impeller::ContextVK::GetDevice ( ) const

Definition at line 530 of file context_vk.cc.

530  {
531  return device_holder_->device.get();
532 }

Referenced by impeller::DescriptorPoolVK::AllocateDescriptorSets(), InitializeCommonlyUsedShadersIfNeeded(), impeller::GPUTracerVK::InitializeQueryPool(), and SetDebugName().

◆ GetDeviceHolder()

std::shared_ptr<DeviceHolderVK> impeller::ContextVK::GetDeviceHolder ( ) const
inline

Definition at line 137 of file context_vk.h.

137  {
138  return device_holder_;
139  }

Referenced by CreateCommandBuffer().

◆ GetDriverInfo()

const std::unique_ptr< DriverInfoVK > & impeller::ContextVK::GetDriverInfo ( ) const

Definition at line 641 of file context_vk.cc.

641  {
642  return driver_info_;
643 }

◆ GetFenceWaiter()

std::shared_ptr< FenceWaiterVK > impeller::ContextVK::GetFenceWaiter ( ) const

Definition at line 567 of file context_vk.cc.

567  {
568  return fence_waiter_;
569 }

Referenced by CreateCommandBuffer().

◆ GetGPUTracer()

std::shared_ptr< GPUTracerVK > impeller::ContextVK::GetGPUTracer ( ) const

Definition at line 580 of file context_vk.cc.

580  {
581  return gpu_tracer_;
582 }

Referenced by impeller::AHBSwapchainImplVK::AcquireNextDrawable(), and CreateCommandBuffer().

◆ GetGraphicsQueue()

const std::shared_ptr< QueueVK > & impeller::ContextVK::GetGraphicsQueue ( ) const

Definition at line 559 of file context_vk.cc.

559  {
560  return queues_.graphics_queue;
561 }

References impeller::QueuesVK::graphics_queue.

Referenced by CreateCommandBuffer().

◆ GetHash()

uint64_t impeller::ContextVK::GetHash ( ) const
inline

Definition at line 68 of file context_vk.h.

68 { return hash_; }

Referenced by impeller::CommandPoolRecyclerVK::DestroyThreadLocalPools().

◆ GetInstance()

vk::Instance impeller::ContextVK::GetInstance ( ) const

Definition at line 526 of file context_vk.cc.

526  {
527  return *device_holder_->instance;
528 }

◆ GetPhysicalDevice()

vk::PhysicalDevice impeller::ContextVK::GetPhysicalDevice ( ) const

Definition at line 563 of file context_vk.cc.

563  {
564  return device_holder_->physical_device;
565 }

◆ GetPipelineLibrary()

std::shared_ptr< PipelineLibrary > impeller::ContextVK::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 488 of file context_vk.cc.

488  {
489  return pipeline_library_;
490 }

◆ GetResourceAllocator()

std::shared_ptr< Allocator > impeller::ContextVK::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 476 of file context_vk.cc.

476  {
477  return allocator_;
478 }

Referenced by InitializeCommonlyUsedShadersIfNeeded().

◆ GetResourceManager()

std::shared_ptr< ResourceManagerVK > impeller::ContextVK::GetResourceManager ( ) const

Definition at line 571 of file context_vk.cc.

571  {
572  return resource_manager_;
573 }

◆ GetSamplerLibrary()

std::shared_ptr< SamplerLibrary > impeller::ContextVK::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 484 of file context_vk.cc.

484  {
485  return sampler_library_;
486 }

◆ GetShaderLibrary()

std::shared_ptr< ShaderLibrary > impeller::ContextVK::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 480 of file context_vk.cc.

480  {
481  return shader_library_;
482 }

◆ GetShouldDisableSurfaceControlSwapchain()

bool impeller::ContextVK::GetShouldDisableSurfaceControlSwapchain ( ) const

Whether the Android Surface control based swapchain should be disabled, even if the device is capable of supporting it.

Definition at line 645 of file context_vk.cc.

645  {
646  return should_disable_surface_control_;
647 }

◆ GetYUVConversionLibrary()

const std::shared_ptr< YUVConversionLibraryVK > & impeller::ContextVK::GetYUVConversionLibrary ( ) const

Definition at line 637 of file context_vk.cc.

637  {
638  return yuv_conversion_library_;
639 }

Referenced by impeller::CreateYUVConversion().

◆ InitializeCommonlyUsedShadersIfNeeded()

void impeller::ContextVK::InitializeCommonlyUsedShadersIfNeeded ( ) const
overridevirtual

Run backend specific additional setup and create common shader variants.

This bootstrap is intended to improve the performance of several first frame benchmarks that are tracked in the flutter device lab. The workload includes initializing commonly used but not default shader variants, as well as forcing driver initialization.

Reimplemented from impeller::Context.

Definition at line 596 of file context_vk.cc.

596  {
597  RenderTargetAllocator rt_allocator(GetResourceAllocator());
598  RenderTarget render_target =
599  rt_allocator.CreateOffscreenMSAA(*this, {1, 1}, 1);
600 
601  RenderPassBuilderVK builder;
602  for (const auto& [bind_point, color] : render_target.GetColorAttachments()) {
603  builder.SetColorAttachment(
604  bind_point, //
605  color.texture->GetTextureDescriptor().format, //
606  color.texture->GetTextureDescriptor().sample_count, //
607  color.load_action, //
608  color.store_action //
609  );
610  }
611 
612  if (auto depth = render_target.GetDepthAttachment(); depth.has_value()) {
613  builder.SetDepthStencilAttachment(
614  depth->texture->GetTextureDescriptor().format, //
615  depth->texture->GetTextureDescriptor().sample_count, //
616  depth->load_action, //
617  depth->store_action //
618  );
619  } else if (auto stencil = render_target.GetStencilAttachment();
620  stencil.has_value()) {
621  builder.SetStencilAttachment(
622  stencil->texture->GetTextureDescriptor().format, //
623  stencil->texture->GetTextureDescriptor().sample_count, //
624  stencil->load_action, //
625  stencil->store_action //
626  );
627  }
628 
629  auto pass = builder.Build(GetDevice());
630 }

References impeller::RenderPassBuilderVK::Build(), color, impeller::RenderTargetAllocator::CreateOffscreenMSAA(), impeller::RenderTarget::GetColorAttachments(), impeller::RenderTarget::GetDepthAttachment(), GetDevice(), GetResourceAllocator(), impeller::RenderTarget::GetStencilAttachment(), impeller::RenderPassBuilderVK::SetColorAttachment(), impeller::RenderPassBuilderVK::SetDepthStencilAttachment(), and impeller::RenderPassBuilderVK::SetStencilAttachment().

◆ IsValid()

bool impeller::ContextVK::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 472 of file context_vk.cc.

472  {
473  return is_valid_;
474 }

◆ RecordFrameEndTime()

void impeller::ContextVK::RecordFrameEndTime ( ) const

◆ SetDebugName() [1/2]

template<typename T >
static bool impeller::ContextVK::SetDebugName ( const vk::Device &  device,
handle,
std::string_view  label 
)
inlinestatic

Definition at line 114 of file context_vk.h.

116  {
117  if (!HasValidationLayers()) {
118  // No-op if validation layers are not enabled.
119  return true;
120  }
121 
122  auto c_handle = static_cast<typename T::CType>(handle);
123 
124  vk::DebugUtilsObjectNameInfoEXT info;
125  info.objectType = T::objectType;
126  info.pObjectName = label.data();
127  info.objectHandle = reinterpret_cast<decltype(info.objectHandle)>(c_handle);
128 
129  if (device.setDebugUtilsObjectNameEXT(info) != vk::Result::eSuccess) {
130  VALIDATION_LOG << "Unable to set debug name: " << label;
131  return false;
132  }
133 
134  return true;
135  }

References impeller::HasValidationLayers(), and VALIDATION_LOG.

◆ SetDebugName() [2/2]

template<typename T >
bool impeller::ContextVK::SetDebugName ( handle,
std::string_view  label 
) const
inline

Definition at line 109 of file context_vk.h.

109  {
110  return SetDebugName(GetDevice(), handle, label);
111  }

References GetDevice().

Referenced by impeller::CreateCompatRenderPassForPipeline(), impeller::CreateSampler(), and impeller::QueuesVK::QueuesVK().

◆ SetOffscreenFormat()

void impeller::ContextVK::SetOffscreenFormat ( PixelFormat  pixel_format)

Definition at line 463 of file context_vk.cc.

463  {
464  CapabilitiesVK::Cast(*device_capabilities_).SetOffscreenFormat(pixel_format);
465 }

References impeller::BackendCast< CapabilitiesVK, Capabilities >::Cast(), and impeller::CapabilitiesVK::SetOffscreenFormat().

◆ Shutdown()

void impeller::ContextVK::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 539 of file context_vk.cc.

539  {
540  // There are multiple objects, for example |CommandPoolVK|, that in their
541  // destructors make a strong reference to |ContextVK|. Resetting these shared
542  // pointers ensures that cleanup happens in a correct order.
543  //
544  // tl;dr: Without it, we get thread::join failures on shutdown.
545  fence_waiter_.reset();
546  resource_manager_.reset();
547 
548  raster_message_loop_->Terminate();
549 }

The documentation for this class was generated from the following files:
impeller::ContextVK::GetDeviceHolder
std::shared_ptr< DeviceHolderVK > GetDeviceHolder() const
Definition: context_vk.h:137
impeller::CapabilitiesVK::SetOffscreenFormat
void SetOffscreenFormat(PixelFormat pixel_format) const
Definition: capabilities_vk.cc:464
impeller::CommandPoolRecyclerVK::DestroyThreadLocalPools
static void DestroyThreadLocalPools(const ContextVK *context)
Clean up resources held by all per-thread command pools associated with the given context.
Definition: command_pool_vk.cc:285
impeller::ContextVK::GetResourceAllocator
std::shared_ptr< Allocator > GetResourceAllocator() const override
Returns the allocator used to create textures and buffers on the device.
Definition: context_vk.cc:476
impeller::ContextVK::GetGraphicsQueue
const std::shared_ptr< QueueVK > & GetGraphicsQueue() const
Definition: context_vk.cc:559
impeller::QueuesVK::graphics_queue
std::shared_ptr< QueueVK > graphics_queue
Definition: queue_vk.h:64
impeller::ContextVK::SetDebugName
bool SetDebugName(T handle, std::string_view label) const
Definition: context_vk.h:109
impeller::ContextVK::GetCommandPoolRecycler
std::shared_ptr< CommandPoolRecyclerVK > GetCommandPoolRecycler() const
Definition: context_vk.cc:575
impeller::ContextVK::GetGPUTracer
std::shared_ptr< GPUTracerVK > GetGPUTracer() const
Definition: context_vk.cc:580
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91
impeller::Context::BackendType::kVulkan
@ kVulkan
impeller::ContextVK::GetDevice
const vk::Device & GetDevice() const
Definition: context_vk.cc:530
impeller::BackendCast< CapabilitiesVK, Capabilities >::Cast
static CapabilitiesVK & Cast(Capabilities &base)
Definition: backend_cast.h:13
color
DlColor color
Definition: dl_golden_blur_unittests.cc:24
impeller::ContextVK::GetFenceWaiter
std::shared_ptr< FenceWaiterVK > GetFenceWaiter() const
Definition: context_vk.cc:567
impeller::HasValidationLayers
bool HasValidationLayers()
Definition: context_vk.cc:46