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 
6 
7 #include "flutter/fml/trace_event.h"
12 
13 namespace impeller {
14 
15 SurfaceContextVK::SurfaceContextVK(const std::shared_ptr<ContextVK>& parent)
16  : parent_(parent) {}
17 
19 
21  return parent_->GetBackendType();
22 }
23 
25  return parent_->DescribeGpuModel();
26 }
27 
29  return parent_->IsValid();
30 }
31 
32 std::shared_ptr<Allocator> SurfaceContextVK::GetResourceAllocator() const {
33  return parent_->GetResourceAllocator();
34 }
35 
36 std::shared_ptr<ShaderLibrary> SurfaceContextVK::GetShaderLibrary() const {
37  return parent_->GetShaderLibrary();
38 }
39 
40 std::shared_ptr<SamplerLibrary> SurfaceContextVK::GetSamplerLibrary() const {
41  return parent_->GetSamplerLibrary();
42 }
43 
44 std::shared_ptr<PipelineLibrary> SurfaceContextVK::GetPipelineLibrary() const {
45  return parent_->GetPipelineLibrary();
46 }
47 
48 std::shared_ptr<CommandBuffer> SurfaceContextVK::CreateCommandBuffer() const {
49  return parent_->CreateCommandBuffer();
50 }
51 
52 std::shared_ptr<CommandQueue> SurfaceContextVK::GetCommandQueue() const {
53  return parent_->GetCommandQueue();
54 }
55 
56 const std::shared_ptr<const Capabilities>& SurfaceContextVK::GetCapabilities()
57  const {
58  return parent_->GetCapabilities();
59 }
60 
62  parent_->Shutdown();
63 }
64 
65 bool SurfaceContextVK::SetWindowSurface(vk::UniqueSurfaceKHR surface,
66  const ISize& size) {
67  auto swapchain = KHRSwapchainVK::Create(parent_, std::move(surface), size);
68  if (!swapchain) {
69  VALIDATION_LOG << "Could not create swapchain.";
70  return false;
71  }
72  if (!swapchain->IsValid()) {
73  VALIDATION_LOG << "Could not create valid swapchain.";
74  return false;
75  }
76  swapchain_ = std::move(swapchain);
77  return true;
78 }
79 
80 std::unique_ptr<Surface> SurfaceContextVK::AcquireNextSurface() {
81  TRACE_EVENT0("impeller", __FUNCTION__);
82  auto surface = swapchain_ ? swapchain_->AcquireNextDrawable() : nullptr;
83  if (!surface) {
84  return nullptr;
85  }
86  if (auto pipeline_library = parent_->GetPipelineLibrary()) {
87  impeller::PipelineLibraryVK::Cast(*pipeline_library)
89  }
90  parent_->GetCommandPoolRecycler()->Dispose();
91  parent_->GetResourceAllocator()->DebugTraceMemoryStatistics();
92  return surface;
93 }
94 
95 void SurfaceContextVK::UpdateSurfaceSize(const ISize& size) const {
96  swapchain_->UpdateSurfaceSize(size);
97 }
98 
99 #ifdef FML_OS_ANDROID
100 
101 vk::UniqueSurfaceKHR SurfaceContextVK::CreateAndroidSurface(
102  ANativeWindow* window) const {
103  if (!parent_->GetInstance()) {
104  return vk::UniqueSurfaceKHR{VK_NULL_HANDLE};
105  }
106 
107  auto create_info = vk::AndroidSurfaceCreateInfoKHR().setWindow(window);
108  auto surface_res =
109  parent_->GetInstance().createAndroidSurfaceKHRUnique(create_info);
110 
111  if (surface_res.result != vk::Result::eSuccess) {
112  VALIDATION_LOG << "Could not create Android surface, error: "
113  << vk::to_string(surface_res.result);
114  return vk::UniqueSurfaceKHR{VK_NULL_HANDLE};
115  }
116 
117  return std::move(surface_res.value);
118 }
119 
120 #endif // FML_OS_ANDROID
121 
122 const vk::Device& SurfaceContextVK::GetDevice() const {
123  return parent_->GetDevice();
124 }
125 
127  parent_->InitializeCommonlyUsedShadersIfNeeded();
128 }
129 
131  return *parent_;
132 }
133 
134 } // namespace impeller
khr_swapchain_vk.h
impeller::Context::BackendType
BackendType
Definition: context.h:48
impeller::SurfaceContextVK::GetParent
const ContextVK & GetParent() const
Definition: surface_context_vk.cc:130
impeller::SurfaceContextVK::AcquireNextSurface
std::unique_ptr< Surface > AcquireNextSurface()
Definition: surface_context_vk.cc:80
impeller::KHRSwapchainVK::Create
static std::shared_ptr< KHRSwapchainVK > Create(const std::shared_ptr< Context > &context, vk::UniqueSurfaceKHR surface, const ISize &size, bool enable_msaa=true)
Definition: khr_swapchain_vk.cc:13
impeller::SurfaceContextVK::SetWindowSurface
bool SetWindowSurface(vk::UniqueSurfaceKHR surface, const ISize &size)
Definition: surface_context_vk.cc:65
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:40
impeller::SurfaceContextVK::~SurfaceContextVK
~SurfaceContextVK() override
surface_context_vk.h
impeller::SurfaceContextVK::GetBackendType
BackendType GetBackendType() const override
Get the graphics backend of an Impeller context.
Definition: surface_context_vk.cc:20
impeller::SurfaceContextVK::InitializeCommonlyUsedShadersIfNeeded
void InitializeCommonlyUsedShadersIfNeeded() const override
Definition: surface_context_vk.cc:126
impeller::SurfaceContextVK::GetDevice
const vk::Device & GetDevice() const
Definition: surface_context_vk.cc:122
impeller::PipelineLibraryVK::DidAcquireSurfaceFrame
void DidAcquireSurfaceFrame()
Definition: pipeline_library_vk.cc:257
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:48
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:36
command_pool_vk.h
surface.h
impeller::TSize< int64_t >
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:95
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:56
impeller::SurfaceContextVK::SurfaceContextVK
SurfaceContextVK(const std::shared_ptr< ContextVK > &parent)
Definition: surface_context_vk.cc:15
impeller::SurfaceContextVK::DescribeGpuModel
std::string DescribeGpuModel() const override
Definition: surface_context_vk.cc:24
impeller::ContextVK
Definition: context_vk.h:42
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:73
impeller::BackendCast< PipelineLibraryVK, PipelineLibrary >::Cast
static PipelineLibraryVK & Cast(PipelineLibrary &base)
Definition: backend_cast.h:13
impeller::SurfaceContextVK::GetCommandQueue
std::shared_ptr< CommandQueue > GetCommandQueue() const override
Return the graphics queue for submitting command buffers.
Definition: surface_context_vk.cc:52
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:32
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:44
context_vk.h
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:28
impeller
Definition: aiks_blur_unittests.cc:20
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:61