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  swapchain_ = std::move(swapchain);
67  return true;
68 }
69 
70 std::unique_ptr<Surface> SurfaceContextVK::AcquireNextSurface() {
71  TRACE_EVENT0("impeller", __FUNCTION__);
72  auto surface = swapchain_ ? swapchain_->AcquireNextDrawable() : nullptr;
73  if (!surface) {
74  return nullptr;
75  }
76  if (auto pipeline_library = parent_->GetPipelineLibrary()) {
77  impeller::PipelineLibraryVK::Cast(*pipeline_library)
79  }
80  if (auto allocator = parent_->GetResourceAllocator()) {
81  allocator->DidAcquireSurfaceFrame();
82  }
83  parent_->GetCommandPoolRecycler()->Dispose();
84  return surface;
85 }
86 
88  parent_->SetSyncPresentation(value);
89 }
90 
91 #ifdef FML_OS_ANDROID
92 
93 vk::UniqueSurfaceKHR SurfaceContextVK::CreateAndroidSurface(
94  ANativeWindow* window) const {
95  if (!parent_->GetInstance()) {
96  return vk::UniqueSurfaceKHR{VK_NULL_HANDLE};
97  }
98 
99  auto create_info = vk::AndroidSurfaceCreateInfoKHR().setWindow(window);
100  auto surface_res =
101  parent_->GetInstance().createAndroidSurfaceKHRUnique(create_info);
102 
103  if (surface_res.result != vk::Result::eSuccess) {
104  VALIDATION_LOG << "Could not create Android surface, error: "
105  << vk::to_string(surface_res.result);
106  return vk::UniqueSurfaceKHR{VK_NULL_HANDLE};
107  }
108 
109  return std::move(surface_res.value);
110 }
111 
112 #endif // FML_OS_ANDROID
113 
114 } // namespace impeller
impeller::Context::BackendType
BackendType
Definition: context.h:49
impeller::SurfaceContextVK::AcquireNextSurface
std::unique_ptr< Surface > AcquireNextSurface()
Definition: surface_context_vk.cc:70
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:12
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:679
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:60
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:87
impeller::BackendCast< PipelineLibraryVK, PipelineLibrary >::Cast
static PipelineLibraryVK & Cast(PipelineLibrary &base)
Definition: backend_cast.h:14
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