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"
11 
12 namespace impeller {
13 
14 SurfaceContextVK::SurfaceContextVK(const std::shared_ptr<ContextVK>& parent)
15  : parent_(parent) {}
16 
18 
20  return parent_->GetBackendType();
21 }
22 
24  return parent_->DescribeGpuModel();
25 }
26 
28  return parent_->IsValid();
29 }
30 
31 std::shared_ptr<Allocator> SurfaceContextVK::GetResourceAllocator() const {
32  return parent_->GetResourceAllocator();
33 }
34 
35 std::shared_ptr<ShaderLibrary> SurfaceContextVK::GetShaderLibrary() const {
36  return parent_->GetShaderLibrary();
37 }
38 
39 std::shared_ptr<SamplerLibrary> SurfaceContextVK::GetSamplerLibrary() const {
40  return parent_->GetSamplerLibrary();
41 }
42 
43 std::shared_ptr<PipelineLibrary> SurfaceContextVK::GetPipelineLibrary() const {
44  return parent_->GetPipelineLibrary();
45 }
46 
47 std::shared_ptr<CommandBuffer> SurfaceContextVK::CreateCommandBuffer() const {
48  return parent_->CreateCommandBuffer();
49 }
50 
51 const std::shared_ptr<const Capabilities>& SurfaceContextVK::GetCapabilities()
52  const {
53  return parent_->GetCapabilities();
54 }
55 
57  parent_->Shutdown();
58 }
59 
60 bool SurfaceContextVK::SetWindowSurface(vk::UniqueSurfaceKHR surface) {
61  auto swapchain = SwapchainVK::Create(parent_, std::move(surface));
62  if (!swapchain) {
63  VALIDATION_LOG << "Could not create swapchain.";
64  return false;
65  }
66  if (!swapchain->IsValid()) {
67  VALIDATION_LOG << "Could not create valid swapchain.";
68  return false;
69  }
70  swapchain_ = std::move(swapchain);
71  return true;
72 }
73 
74 std::unique_ptr<Surface> SurfaceContextVK::AcquireNextSurface() {
75  TRACE_EVENT0("impeller", __FUNCTION__);
76  auto surface = swapchain_ ? swapchain_->AcquireNextDrawable() : nullptr;
77  if (!surface) {
78  return nullptr;
79  }
80  if (auto pipeline_library = parent_->GetPipelineLibrary()) {
81  impeller::PipelineLibraryVK::Cast(*pipeline_library)
83  }
84  if (auto allocator = parent_->GetResourceAllocator()) {
85  allocator->DidAcquireSurfaceFrame();
86  }
87  parent_->GetCommandPoolRecycler()->Dispose();
88  return surface;
89 }
90 
92  parent_->SetSyncPresentation(value);
93 }
94 
95 #ifdef FML_OS_ANDROID
96 
97 vk::UniqueSurfaceKHR SurfaceContextVK::CreateAndroidSurface(
98  ANativeWindow* window) const {
99  if (!parent_->GetInstance()) {
100  return vk::UniqueSurfaceKHR{VK_NULL_HANDLE};
101  }
102 
103  auto create_info = vk::AndroidSurfaceCreateInfoKHR().setWindow(window);
104  auto surface_res =
105  parent_->GetInstance().createAndroidSurfaceKHRUnique(create_info);
106 
107  if (surface_res.result != vk::Result::eSuccess) {
108  VALIDATION_LOG << "Could not create Android surface, error: "
109  << vk::to_string(surface_res.result);
110  return vk::UniqueSurfaceKHR{VK_NULL_HANDLE};
111  }
112 
113  return std::move(surface_res.value);
114 }
115 
116 #endif // FML_OS_ANDROID
117 
118 } // namespace impeller
impeller::Context::BackendType
BackendType
Definition: context.h:49
impeller::SurfaceContextVK::AcquireNextSurface
std::unique_ptr< Surface > AcquireNextSurface()
Definition: surface_context_vk.cc:74
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:39
impeller::SurfaceContextVK::~SurfaceContextVK
~SurfaceContextVK() override
swapchain_vk.h
impeller::SwapchainVK::Create
static std::shared_ptr< SwapchainVK > Create(const std::shared_ptr< Context > &context, vk::UniqueSurfaceKHR surface)
Definition: swapchain_vk.cc:13
surface_context_vk.h
impeller::SurfaceContextVK::GetBackendType
BackendType GetBackendType() const override
Get the graphics backend of an Impeller context.
Definition: surface_context_vk.cc:19
impeller::PipelineLibraryVK::DidAcquireSurfaceFrame
void DidAcquireSurfaceFrame()
Definition: pipeline_library_vk.cc:727
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:47
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:35
command_pool_vk.h
impeller::SurfaceContextVK::SetWindowSurface
bool SetWindowSurface(vk::UniqueSurfaceKHR surface)
Definition: surface_context_vk.cc:60
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:51
impeller::SurfaceContextVK::SurfaceContextVK
SurfaceContextVK(const std::shared_ptr< ContextVK > &parent)
Definition: surface_context_vk.cc:14
impeller::SurfaceContextVK::DescribeGpuModel
std::string DescribeGpuModel() const override
Definition: surface_context_vk.cc:23
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:67
impeller::SurfaceContextVK::SetSyncPresentation
void SetSyncPresentation(bool value) override
Force the Vulkan presentation (submitKHR) to be performed on the raster task runner.
Definition: surface_context_vk.cc:91
impeller::BackendCast< PipelineLibraryVK, PipelineLibrary >::Cast
static PipelineLibraryVK & Cast(PipelineLibrary &base)
Definition: backend_cast.h:15
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:31
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:43
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:27
impeller
Definition: aiks_context.cc:10
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:56