| 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/swapchain/surface_vk.h"
6
7 #include "impeller/core/formats.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<SwapchainTransientsVK>& transients,
15 const std::shared_ptr<TextureSourceVK>& swapchain_image,
16 SwapCallback swap_callback) {
17 if (!transients || !swapchain_image || !swap_callback) {
18 return nullptr;
19 }
20
21 auto context = transients->GetContext().lock();
22 if (!context) {
23 return nullptr;
24 }
25
26 const auto enable_msaa = transients->IsMSAAEnabled();
27
28 const auto swapchain_tex_desc = swapchain_image->GetTextureDescriptor();
29
30 TextureDescriptor resolve_tex_desc;
31 resolve_tex_desc.type = TextureType::kTexture2D;
32 resolve_tex_desc.format = swapchain_tex_desc.format;
33 resolve_tex_desc.size = swapchain_tex_desc.size;
34 resolve_tex_desc.usage = TextureUsage::kRenderTarget;
35 resolve_tex_desc.sample_count = SampleCount::kCount1;
36 resolve_tex_desc.storage_mode = StorageMode::kDevicePrivate;
37
38 std::shared_ptr<Texture> resolve_tex =
39 std::make_shared<TextureVK>(context, //
40 swapchain_image //
41 );
42
43 if (!resolve_tex) {
44 return nullptr;
45 }
46 resolve_tex->SetLabel("ImpellerOnscreenResolve");
47
48 ColorAttachment color0;
49 color0.clear_color = Color::DarkSlateGray();
50 color0.load_action = LoadAction::kClear;
51 if (enable_msaa) {
52 color0.texture = transients->GetMSAATexture();
53 color0.store_action = StoreAction::kMultisampleResolve;
54 color0.resolve_texture = resolve_tex;
55 } else {
56 color0.texture = resolve_tex;
57 color0.store_action = StoreAction::kStore;
58 }
59
60 RenderTarget render_target_desc;
61 render_target_desc.SetColorAttachment(color0, 0u);
62 render_target_desc.SetupDepthStencilAttachments(
63 /*context=*/*context, //
64 /*allocator=*/*context->GetResourceAllocator(), //
65 /*size=*/swapchain_tex_desc.size, //
66 /*msaa=*/enable_msaa, //
67 /*label=*/"Onscreen", //
68 /*stencil_attachment_config=*/
69 RenderTarget::kDefaultStencilAttachmentConfig, //
70 /*depth_stencil_texture=*/transients->GetDepthStencilTexture() //
71 );
72
73 // The constructor is private. So make_unique may not be used.
74 return std::unique_ptr<SurfaceVK>(
75 new SurfaceVK(render_target_desc, std::move(swap_callback)));
76 }
77
78 SurfaceVK::SurfaceVK(const RenderTarget& target, SwapCallback swap_callback)
79 : Surface(target), swap_callback_(std::move(swap_callback)) {}
80
81 SurfaceVK::~SurfaceVK() = default;
82
83 bool SurfaceVK::Present() const {
84 return swap_callback_ ? swap_callback_() : false;
85 }
86
87 } // namespace impeller
impeller::RenderTarget
Definition: render_target.h:38
impeller::RenderTarget::SetColorAttachment
RenderTarget & SetColorAttachment(const ColorAttachment &attachment, size_t index)
Definition: render_target.cc:199
impeller::RenderTarget::kDefaultStencilAttachmentConfig
static constexpr AttachmentConfig kDefaultStencilAttachmentConfig
Definition: render_target.h:68
impeller::RenderTarget::SetupDepthStencilAttachments
void SetupDepthStencilAttachments(const Context &context, Allocator &allocator, ISize size, bool msaa, std::string_view label="Offscreen", RenderTarget::AttachmentConfig stencil_attachment_config=RenderTarget::kDefaultStencilAttachmentConfig, const std::shared_ptr< Texture > &depth_stencil_texture=nullptr)
Definition: render_target.cc:475
impeller::Surface
Definition: surface.h:12
impeller::SurfaceVK
Definition: surface_vk.h:17
impeller::SurfaceVK::~SurfaceVK
~SurfaceVK() override
impeller::SurfaceVK::WrapSwapchainImage
static std::unique_ptr< SurfaceVK > WrapSwapchainImage(const std::shared_ptr< SwapchainTransientsVK > &transients, const std::shared_ptr< TextureSourceVK > &swapchain_image, SwapCallback swap_callback)
Wrap the swapchain image in a Surface, which provides the additional configuration required for usage...
Definition: surface_vk.cc:13
impeller::SurfaceVK::SwapCallback
std::function< bool(void)> SwapCallback
Definition: surface_vk.h:19
formats.h
impeller
Definition: allocation.cc:12
impeller::StorageMode::kDevicePrivate
@ kDevicePrivate
impeller::TextureType::kTexture2D
@ kTexture2D
impeller::LoadAction::kClear
@ kClear
impeller::StoreAction::kStore
@ kStore
impeller::StoreAction::kMultisampleResolve
@ kMultisampleResolve
impeller::TextureUsage::kRenderTarget
@ kRenderTarget
impeller::SampleCount::kCount1
@ kCount1
std
Definition: comparable.h:95
surface_vk.h
surface.h
impeller::Attachment::resolve_texture
std::shared_ptr< Texture > resolve_texture
Definition: formats.h:658
impeller::Attachment::load_action
LoadAction load_action
Definition: formats.h:659
impeller::Attachment::texture
std::shared_ptr< Texture > texture
Definition: formats.h:657
impeller::Attachment::store_action
StoreAction store_action
Definition: formats.h:660
impeller::ColorAttachment
Definition: formats.h:665
impeller::ColorAttachment::clear_color
Color clear_color
Definition: formats.h:666
impeller::Color::DarkSlateGray
static constexpr Color DarkSlateGray()
Definition: color.h:418
impeller::TextureDescriptor
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
Definition: texture_descriptor.h:38
impeller::TextureDescriptor::usage
TextureUsageMask usage
Definition: texture_descriptor.h:44
impeller::TextureDescriptor::type
TextureType type
Definition: texture_descriptor.h:40
impeller::TextureDescriptor::size
ISize size
Definition: texture_descriptor.h:42
impeller::TextureDescriptor::storage_mode
StorageMode storage_mode
Definition: texture_descriptor.h:39
impeller::TextureDescriptor::sample_count
SampleCount sample_count
Definition: texture_descriptor.h:45
impeller::TextureDescriptor::format
PixelFormat format
Definition: texture_descriptor.h:41
texture_vk.h