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
 
- Public Member Functions inherited from impeller::Context
virtual ~Context ()
 Destroys an Impeller context. More...
 
virtual bool UpdateOffscreenLayerPixelFormat (PixelFormat format)
 
virtual void StoreTaskForGPU (const std::function< void()> &task)
 

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 127 of file context_vk.cc.

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

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 110 of file context_vk.cc.

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

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

◆ Create()

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

Definition at line 100 of file context_vk.cc.

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

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 502 of file context_vk.cc.

502  {
503  return std::shared_ptr<CommandBufferVK>(
504  new CommandBufferVK(shared_from_this(), //
505  CreateGraphicsCommandEncoderFactory()) //
506  );
507 }

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

◆ CreateSurfaceContext()

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

Definition at line 534 of file context_vk.cc.

534  {
535  return std::make_shared<SurfaceContextVK>(shared_from_this());
536 }

◆ DescribeGpuModel()

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

Implements impeller::Context.

Definition at line 478 of file context_vk.cc.

478  {
479  return device_name_;
480 }

◆ 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 134 of file context_vk.cc.

134  {
136 }

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 538 of file context_vk.cc.

538  {
539  return device_capabilities_;
540 }

◆ GetCommandPoolRecycler()

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

Definition at line 558 of file context_vk.cc.

559  {
560  return command_pool_recycler_;
561 }

◆ GetCommandQueue()

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

Return the graphics queue for submitting command buffers.

Implements impeller::Context.

Definition at line 577 of file context_vk.cc.

577  {
578  return command_queue_vk_;
579 }

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

◆ GetConcurrentWorkerTaskRunner()

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

Definition at line 518 of file context_vk.cc.

518  {
519  return raster_message_loop_->GetTaskRunner();
520 }

◆ GetDescriptorPoolRecycler()

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

Definition at line 572 of file context_vk.cc.

573  {
574  return descriptor_pool_recycler_;
575 }

◆ GetDevice()

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

Definition at line 513 of file context_vk.cc.

513  {
514  return device_holder_->device.get();
515 }

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

◆ GetDeviceHolder()

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

Definition at line 136 of file context_vk.h.

136  {
137  return device_holder_;
138  }

◆ GetDriverInfo()

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

Definition at line 625 of file context_vk.cc.

625  {
626  return driver_info_;
627 }

◆ GetFenceWaiter()

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

Definition at line 550 of file context_vk.cc.

550  {
551  return fence_waiter_;
552 }

◆ GetGPUTracer()

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

Definition at line 568 of file context_vk.cc.

568  {
569  return gpu_tracer_;
570 }

Referenced by impeller::AHBSwapchainImplVK::AcquireNextDrawable().

◆ GetGraphicsQueue()

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

Definition at line 542 of file context_vk.cc.

542  {
543  return queues_.graphics_queue;
544 }

References impeller::QueuesVK::graphics_queue.

◆ GetHash()

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

Definition at line 67 of file context_vk.h.

67 { return hash_; }

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

◆ GetInstance()

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

Definition at line 509 of file context_vk.cc.

509  {
510  return *device_holder_->instance;
511 }

◆ GetPhysicalDevice()

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

Definition at line 546 of file context_vk.cc.

546  {
547  return device_holder_->physical_device;
548 }

◆ 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 498 of file context_vk.cc.

498  {
499  return pipeline_library_;
500 }

◆ 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 486 of file context_vk.cc.

486  {
487  return allocator_;
488 }

Referenced by InitializeCommonlyUsedShadersIfNeeded().

◆ GetResourceManager()

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

Definition at line 554 of file context_vk.cc.

554  {
555  return resource_manager_;
556 }

◆ 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 494 of file context_vk.cc.

494  {
495  return sampler_library_;
496 }

◆ 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 490 of file context_vk.cc.

490  {
491  return shader_library_;
492 }

◆ GetYUVConversionLibrary()

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

Definition at line 621 of file context_vk.cc.

621  {
622  return yuv_conversion_library_;
623 }

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 584 of file context_vk.cc.

584  {
585  RenderTargetAllocator rt_allocator(GetResourceAllocator());
586  RenderTarget render_target =
587  rt_allocator.CreateOffscreenMSAA(*this, {1, 1}, 1);
588 
589  RenderPassBuilderVK builder;
590  for (const auto& [bind_point, color] : render_target.GetColorAttachments()) {
591  builder.SetColorAttachment(
592  bind_point, //
593  color.texture->GetTextureDescriptor().format, //
594  color.texture->GetTextureDescriptor().sample_count, //
595  color.load_action, //
596  color.store_action //
597  );
598  }
599 
600  if (auto depth = render_target.GetDepthAttachment(); depth.has_value()) {
601  builder.SetDepthStencilAttachment(
602  depth->texture->GetTextureDescriptor().format, //
603  depth->texture->GetTextureDescriptor().sample_count, //
604  depth->load_action, //
605  depth->store_action //
606  );
607  } else if (auto stencil = render_target.GetStencilAttachment();
608  stencil.has_value()) {
609  builder.SetStencilAttachment(
610  stencil->texture->GetTextureDescriptor().format, //
611  stencil->texture->GetTextureDescriptor().sample_count, //
612  stencil->load_action, //
613  stencil->store_action //
614  );
615  }
616 
617  auto pass = builder.Build(GetDevice());
618 }

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 482 of file context_vk.cc.

482  {
483  return is_valid_;
484 }

◆ 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 113 of file context_vk.h.

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

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 108 of file context_vk.h.

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

References GetDevice().

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

◆ SetOffscreenFormat()

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

Definition at line 473 of file context_vk.cc.

473  {
474  CapabilitiesVK::Cast(*device_capabilities_).SetOffscreenFormat(pixel_format);
475 }

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 522 of file context_vk.cc.

522  {
523  // There are multiple objects, for example |CommandPoolVK|, that in their
524  // destructors make a strong reference to |ContextVK|. Resetting these shared
525  // pointers ensures that cleanup happens in a correct order.
526  //
527  // tl;dr: Without it, we get thread::join failures on shutdown.
528  fence_waiter_.reset();
529  resource_manager_.reset();
530 
531  raster_message_loop_->Terminate();
532 }

The documentation for this class was generated from the following files:
impeller::CapabilitiesVK::SetOffscreenFormat
void SetOffscreenFormat(PixelFormat pixel_format) const
Definition: capabilities_vk.cc:433
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:486
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:108
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:73
impeller::Context::BackendType::kVulkan
@ kVulkan
impeller::ContextVK::GetDevice
const vk::Device & GetDevice() const
Definition: context_vk.cc:513
impeller::BackendCast< CapabilitiesVK, Capabilities >::Cast
static CapabilitiesVK & Cast(Capabilities &base)
Definition: backend_cast.h:13
color
DlColor color
Definition: dl_golden_blur_unittests.cc:23
impeller::HasValidationLayers
bool HasValidationLayers()
Definition: context_vk.cc:48