| Flutter Impeller |

surface_context_vk.cc
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 #include "impeller/renderer/backend/vulkan/surface_context_vk.h"
6
7 #include "flutter/fml/trace_event.h"
8 #include "impeller/core/runtime_types.h"
9 #include "impeller/renderer/backend/vulkan/command_pool_vk.h"
10 #include "impeller/renderer/backend/vulkan/context_vk.h"
11 #include "impeller/renderer/backend/vulkan/swapchain/khr/khr_swapchain_vk.h"
12 #include "impeller/renderer/surface.h"
13
14 namespace impeller {
15
16 SurfaceContextVK::SurfaceContextVK(const std::shared_ptr<ContextVK>& parent)
17 : Context(parent->GetFlags()), parent_(parent) {}
18
19 SurfaceContextVK::~SurfaceContextVK() = default;
20
21 Context::BackendType SurfaceContextVK::GetBackendType() const {
22 return parent_->GetBackendType();
23 }
24
25 std::string SurfaceContextVK::DescribeGpuModel() const {
26 return parent_->DescribeGpuModel();
27 }
28
29 bool SurfaceContextVK::IsValid() const {
30 return parent_->IsValid();
31 }
32
33 std::shared_ptr<Allocator> SurfaceContextVK::GetResourceAllocator() const {
34 return parent_->GetResourceAllocator();
35 }
36
37 std::shared_ptr<ShaderLibrary> SurfaceContextVK::GetShaderLibrary() const {
38 return parent_->GetShaderLibrary();
39 }
40
41 std::shared_ptr<SamplerLibrary> SurfaceContextVK::GetSamplerLibrary() const {
42 return parent_->GetSamplerLibrary();
43 }
44
45 std::shared_ptr<PipelineLibrary> SurfaceContextVK::GetPipelineLibrary() const {
46 return parent_->GetPipelineLibrary();
47 }
48
49 std::shared_ptr<CommandBuffer> SurfaceContextVK::CreateCommandBuffer() const {
50 return parent_->CreateCommandBuffer();
51 }
52
53 std::shared_ptr<CommandQueue> SurfaceContextVK::GetCommandQueue() const {
54 return parent_->GetCommandQueue();
55 }
56
57 const std::shared_ptr<const Capabilities>& SurfaceContextVK::GetCapabilities()
58 const {
59 return parent_->GetCapabilities();
60 }
61
62 std::shared_ptr<const IdleWaiter> SurfaceContextVK::GetIdleWaiter() const {
63 return parent_->GetIdleWaiter();
64 }
65
66 void SurfaceContextVK::Shutdown() {
67 parent_->Shutdown();
68 }
69
70 bool SurfaceContextVK::SetWindowSurface(vk::UniqueSurfaceKHR surface,
71 const ISize& size) {
72 return SetSwapchain(SwapchainVK::Create(parent_, std::move(surface), size));
73 }
74
75 void SurfaceContextVK::TeardownSwapchain() {
76 // When background the application, tear down the swapchain to release memory
77 // from the images. When returning to the foreground, SetWindowSurface will be
78 // called which will re-create the swapchain.
79 swapchain_.reset();
80 }
81
82 bool SurfaceContextVK::SetSwapchain(std::shared_ptr<SwapchainVK> swapchain) {
83 if (!swapchain || !swapchain->IsValid()) {
84 VALIDATION_LOG << "Invalid swapchain.";
85 return false;
86 }
87 swapchain_ = std::move(swapchain);
88 return true;
89 }
90
91 std::unique_ptr<Surface> SurfaceContextVK::AcquireNextSurface() {
92 TRACE_EVENT0("impeller", __FUNCTION__);
93 auto surface = swapchain_ ? swapchain_->AcquireNextDrawable() : nullptr;
94 if (!surface) {
95 return nullptr;
96 }
97 MarkFrameEnd();
98 return surface;
99 }
100
101 void SurfaceContextVK::MarkFrameEnd() {
102 if (auto pipeline_library = parent_->GetPipelineLibrary()) {
103 impeller::PipelineLibraryVK::Cast(*pipeline_library)
104 .DidAcquireSurfaceFrame();
105 }
106 parent_->DisposeThreadLocalCachedResources();
107 parent_->GetResourceAllocator()->DebugTraceMemoryStatistics();
108 }
109
110 void SurfaceContextVK::UpdateSurfaceSize(const ISize& size) const {
111 swapchain_->UpdateSurfaceSize(size);
112 }
113
114 const vk::Device& SurfaceContextVK::GetDevice() const {
115 return parent_->GetDevice();
116 }
117
118 void SurfaceContextVK::InitializeCommonlyUsedShadersIfNeeded() const {
119 parent_->InitializeCommonlyUsedShadersIfNeeded();
120 }
121
122 void SurfaceContextVK::DisposeThreadLocalCachedResources() {
123 parent_->DisposeThreadLocalCachedResources();
124 }
125
126 const std::shared_ptr<ContextVK>& SurfaceContextVK::GetParent() const {
127 return parent_;
128 }
129
130 bool SurfaceContextVK::EnqueueCommandBuffer(
131 std::shared_ptr<CommandBuffer> command_buffer) {
132 return parent_->EnqueueCommandBuffer(std::move(command_buffer));
133 }
134
135 bool SurfaceContextVK::FlushCommandBuffers() {
136 return parent_->FlushCommandBuffers();
137 }
138
139 bool SurfaceContextVK::SubmitOnscreen(
140 std::shared_ptr<CommandBuffer> cmd_buffer) {
141 swapchain_->AddFinalCommandBuffer(std::move(cmd_buffer));
142 return true;
143 }
144
145 RuntimeStageBackend SurfaceContextVK::GetRuntimeStageBackend() const {
146 return parent_->GetRuntimeStageBackend();
147 }
148
149 } // namespace impeller
impeller::BackendCast< PipelineLibraryVK, PipelineLibrary >::Cast
static PipelineLibraryVK & Cast(PipelineLibrary &base)
Definition: backend_cast.h:13
impeller::Context
To do anything rendering related with Impeller, you need a context.
Definition: context.h:65
impeller::Context::BackendType
BackendType
Definition: context.h:67
impeller::PipelineLibraryVK::DidAcquireSurfaceFrame
void DidAcquireSurfaceFrame()
Definition: pipeline_library_vk.cc:276
impeller::SurfaceContextVK::SetWindowSurface
bool SetWindowSurface(vk::UniqueSurfaceKHR surface, const ISize &size)
Definition: surface_context_vk.cc:70
impeller::SurfaceContextVK::SurfaceContextVK
SurfaceContextVK(const std::shared_ptr< ContextVK > &parent)
Definition: surface_context_vk.cc:16
impeller::SurfaceContextVK::GetRuntimeStageBackend
RuntimeStageBackend GetRuntimeStageBackend() const override
Retrieve the runtime stage for this context type.
Definition: surface_context_vk.cc:145
impeller::SurfaceContextVK::GetPipelineLibrary
std::shared_ptr< PipelineLibrary > GetPipelineLibrary() const override
Returns the library of pipelines used by render or compute commands.
Definition: surface_context_vk.cc:45
impeller::SurfaceContextVK::MarkFrameEnd
void MarkFrameEnd()
Performs frame incrementing processes like AcquireNextSurface but without the surface.
Definition: surface_context_vk.cc:101
impeller::SurfaceContextVK::TeardownSwapchain
void TeardownSwapchain()
Can be called when the surface is destroyed to reduce memory usage.
Definition: surface_context_vk.cc:75
impeller::SurfaceContextVK::EnqueueCommandBuffer
bool EnqueueCommandBuffer(std::shared_ptr< CommandBuffer > command_buffer) override
Enqueue command_buffer for submission by the end of the frame.
Definition: surface_context_vk.cc:130
impeller::SurfaceContextVK::SubmitOnscreen
bool SubmitOnscreen(std::shared_ptr< CommandBuffer > cmd_buffer) override
Submit the command buffer that renders to the onscreen surface.
Definition: surface_context_vk.cc:139
impeller::SurfaceContextVK::GetParent
const std::shared_ptr< ContextVK > & GetParent() const
Definition: surface_context_vk.cc:126
impeller::SurfaceContextVK::SetSwapchain
bool SetSwapchain(std::shared_ptr< SwapchainVK > swapchain)
Definition: surface_context_vk.cc:82
impeller::SurfaceContextVK::FlushCommandBuffers
bool FlushCommandBuffers() override
Flush all pending command buffers.
Definition: surface_context_vk.cc:135
impeller::SurfaceContextVK::~SurfaceContextVK
~SurfaceContextVK() override
impeller::SurfaceContextVK::GetCapabilities
const std::shared_ptr< const Capabilities > & GetCapabilities() const override
Get the capabilities of Impeller context. All optionally supported feature of the platform,...
Definition: surface_context_vk.cc:57
impeller::SurfaceContextVK::GetBackendType
BackendType GetBackendType() const override
Get the graphics backend of an Impeller context.
Definition: surface_context_vk.cc:21
impeller::SurfaceContextVK::DescribeGpuModel
std::string DescribeGpuModel() const override
Definition: surface_context_vk.cc:25
impeller::SurfaceContextVK::IsValid
bool IsValid() const override
Determines if a context is valid. If the caller ever receives an invalid context, they must discard i...
Definition: surface_context_vk.cc:29
impeller::SurfaceContextVK::UpdateSurfaceSize
void UpdateSurfaceSize(const ISize &size) const
Mark the current swapchain configuration as dirty, forcing it to be recreated on the next frame.
Definition: surface_context_vk.cc:110
impeller::SurfaceContextVK::AcquireNextSurface
std::unique_ptr< Surface > AcquireNextSurface()
Definition: surface_context_vk.cc:91
impeller::SurfaceContextVK::GetIdleWaiter
std::shared_ptr< const IdleWaiter > GetIdleWaiter() const override
Definition: surface_context_vk.cc:62
impeller::SurfaceContextVK::GetDevice
const vk::Device & GetDevice() const
Definition: surface_context_vk.cc:114
impeller::SurfaceContextVK::DisposeThreadLocalCachedResources
void DisposeThreadLocalCachedResources() override
Definition: surface_context_vk.cc:122
impeller::SurfaceContextVK::GetSamplerLibrary
std::shared_ptr< SamplerLibrary > GetSamplerLibrary() const override
Returns the library of combined image samplers used in shaders.
Definition: surface_context_vk.cc:41
impeller::SurfaceContextVK::GetCommandQueue
std::shared_ptr< CommandQueue > GetCommandQueue() const override
Return the graphics queue for submitting command buffers.
Definition: surface_context_vk.cc:53
impeller::SurfaceContextVK::Shutdown
void Shutdown() override
Force all pending asynchronous work to finish. This is achieved by deleting all owned concurrent mess...
Definition: surface_context_vk.cc:66
impeller::SurfaceContextVK::CreateCommandBuffer
std::shared_ptr< CommandBuffer > CreateCommandBuffer() const override
Create a new command buffer. Command buffers can be used to encode graphics, blit,...
Definition: surface_context_vk.cc:49
impeller::SurfaceContextVK::GetShaderLibrary
std::shared_ptr< ShaderLibrary > GetShaderLibrary() const override
Returns the library of shaders used to specify the programmable stages of a pipeline.
Definition: surface_context_vk.cc:37
impeller::SurfaceContextVK::InitializeCommonlyUsedShadersIfNeeded
void InitializeCommonlyUsedShadersIfNeeded() const override
Definition: surface_context_vk.cc:118
impeller::SurfaceContextVK::GetResourceAllocator
std::shared_ptr< Allocator > GetResourceAllocator() const override
Returns the allocator used to create textures and buffers on the device.
Definition: surface_context_vk.cc:33
impeller::SwapchainVK::Create
static std::shared_ptr< SwapchainVK > Create(const std::shared_ptr< Context > &context, vk::UniqueSurfaceKHR surface, const ISize &size, bool enable_msaa=true)
Definition: swapchain_vk.cc:18
command_pool_vk.h
khr_swapchain_vk.h
impeller
Definition: allocation.cc:12
impeller::RuntimeStageBackend
RuntimeStageBackend
Definition: runtime_types.h:16
context_vk.h
surface.h
runtime_types.h
impeller::TSize
Definition: size.h:25
surface_context_vk.h
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91