| Flutter Impeller |

surface_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_vk.h"
6
7 #include "impeller/renderer/backend/vulkan/swapchain_image_vk.h"
8 #include "impeller/renderer/backend/vulkan/texture_vk.h"
9 #include "impeller/renderer/surface.h"
10
11 namespace impeller {
12
13 std::unique_ptr<SurfaceVK> SurfaceVK::WrapSwapchainImage(
14 const std::shared_ptr<Context>& context,
15 std::shared_ptr<SwapchainImageVK>& swapchain_image,
16 SwapCallback swap_callback) {
17 if (!context || !swapchain_image || !swap_callback) {
18 return nullptr;
19 }
20
21 TextureDescriptor msaa_tex_desc;
22 msaa_tex_desc.storage_mode = StorageMode::kDeviceTransient;
23 msaa_tex_desc.type = TextureType::kTexture2DMultisample;
24 msaa_tex_desc.sample_count = SampleCount::kCount4;
25 msaa_tex_desc.format = swapchain_image->GetPixelFormat();
26 msaa_tex_desc.size = swapchain_image->GetSize();
27 msaa_tex_desc.usage = static_cast<uint64_t>(TextureUsage::kRenderTarget);
28
29 std::shared_ptr<Texture> msaa_tex;
30 if (!swapchain_image->HasMSAATexture()) {
31 msaa_tex = context->GetResourceAllocator()->CreateTexture(msaa_tex_desc);
32 msaa_tex->SetLabel("ImpellerOnscreenColorMSAA");
33 if (!msaa_tex) {
34 VALIDATION_LOG << "Could not allocate MSAA color texture.";
35 return nullptr;
36 }
37 swapchain_image->SetMSAATexture(msaa_tex);
38 } else {
39 msaa_tex = swapchain_image->GetMSAATexture();
40 }
41
42 TextureDescriptor resolve_tex_desc;
43 resolve_tex_desc.type = TextureType::kTexture2D;
44 resolve_tex_desc.format = swapchain_image->GetPixelFormat();
45 resolve_tex_desc.size = swapchain_image->GetSize();
46 resolve_tex_desc.usage =
47 static_cast<TextureUsageMask>(TextureUsage::kRenderTarget);
48 resolve_tex_desc.sample_count = SampleCount::kCount1;
49 resolve_tex_desc.storage_mode = StorageMode::kDevicePrivate;
50
51 std::shared_ptr<Texture> resolve_tex =
52 std::make_shared<TextureVK>(context, //
53 swapchain_image //
54 );
55
56 if (!resolve_tex) {
57 VALIDATION_LOG << "Could not wrap resolve texture.";
58 return nullptr;
59 }
60 resolve_tex->SetLabel("ImpellerOnscreenResolve");
61
62 ColorAttachment color0;
63 color0.texture = msaa_tex;
64 color0.clear_color = Color::DarkSlateGray();
65 color0.load_action = LoadAction::kClear;
66 color0.store_action = StoreAction::kMultisampleResolve;
67 color0.resolve_texture = resolve_tex;
68
69 RenderTarget render_target_desc;
70 render_target_desc.SetColorAttachment(color0, 0u);
71
72 // The constructor is private. So make_unique may not be used.
73 return std::unique_ptr<SurfaceVK>(
74 new SurfaceVK(render_target_desc, std::move(swap_callback)));
75 }
76
77 SurfaceVK::SurfaceVK(const RenderTarget& target, SwapCallback swap_callback)
78 : Surface(target), swap_callback_(std::move(swap_callback)) {}
79
80 SurfaceVK::~SurfaceVK() = default;
81
82 bool SurfaceVK::Present() const {
83 return swap_callback_ ? swap_callback_() : false;
84 }
85
86 } // namespace impeller
impeller::StoreAction::kMultisampleResolve
@ kMultisampleResolve
impeller::TextureUsageMask
uint64_t TextureUsageMask
Definition: formats.h:295
impeller::Attachment::store_action
StoreAction store_action
Definition: formats.h:637
impeller::SurfaceVK
Definition: surface_vk.h:17
impeller::SurfaceVK::WrapSwapchainImage
static std::unique_ptr< SurfaceVK > WrapSwapchainImage(const std::shared_ptr< Context > &context, std::shared_ptr< SwapchainImageVK > &swapchain_image, SwapCallback swap_callback)
Definition: surface_vk.cc:13
surface_vk.h
impeller::ColorAttachment
Definition: formats.h:642
impeller::TextureDescriptor::format
PixelFormat format
Definition: texture_descriptor.h:40
impeller::RenderTarget::SetColorAttachment
RenderTarget & SetColorAttachment(const ColorAttachment &attachment, size_t index)
Definition: render_target.cc:180
impeller::TextureUsage::kRenderTarget
@ kRenderTarget
impeller::TextureDescriptor::sample_count
SampleCount sample_count
Definition: texture_descriptor.h:45
impeller::TextureDescriptor::usage
TextureUsageMask usage
Definition: texture_descriptor.h:43
impeller::Surface
Definition: surface.h:18
impeller::TextureDescriptor::type
TextureType type
Definition: texture_descriptor.h:39
surface.h
impeller::TextureType::kTexture2DMultisample
@ kTexture2DMultisample
impeller::Color::DarkSlateGray
static constexpr Color DarkSlateGray()
Definition: color.h:410
impeller::LoadAction::kClear
@ kClear
impeller::StorageMode::kDeviceTransient
@ kDeviceTransient
impeller::ColorAttachment::clear_color
Color clear_color
Definition: formats.h:643
impeller::Attachment::texture
std::shared_ptr< Texture > texture
Definition: formats.h:634
impeller::StorageMode::kDevicePrivate
@ kDevicePrivate
impeller::RenderTarget
Definition: render_target.h:49
impeller::TextureType::kTexture2D
@ kTexture2D
texture_vk.h
impeller::TextureDescriptor::size
ISize size
Definition: texture_descriptor.h:41
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:67
impeller::Attachment::resolve_texture
std::shared_ptr< Texture > resolve_texture
Definition: formats.h:635
std
Definition: comparable.h:95
impeller::Attachment::load_action
LoadAction load_action
Definition: formats.h:636
swapchain_image_vk.h
impeller::SurfaceVK::~SurfaceVK
~SurfaceVK() override
impeller::SampleCount::kCount1
@ kCount1
impeller::SampleCount::kCount4
@ kCount4
impeller::TextureDescriptor::storage_mode
StorageMode storage_mode
Definition: texture_descriptor.h:38
impeller::TextureDescriptor
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
Definition: texture_descriptor.h:37
impeller
Definition: aiks_context.cc:10
impeller::SurfaceVK::SwapCallback
std::function< bool(void)> SwapCallback
Definition: surface_vk.h:19