Flutter Impeller
khr_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 
6 
11 
12 namespace impeller {
13 
14 std::unique_ptr<KHRSurfaceVK> KHRSurfaceVK::WrapSwapchainImage(
15  const std::shared_ptr<Context>& context,
16  std::shared_ptr<KHRSwapchainImageVK>& swapchain_image,
17  SwapCallback swap_callback,
18  bool enable_msaa) {
19  if (!context || !swapchain_image || !swap_callback) {
20  return nullptr;
21  }
22 
23  std::shared_ptr<Texture> msaa_tex;
24  if (enable_msaa) {
25  TextureDescriptor msaa_tex_desc;
28  msaa_tex_desc.sample_count = SampleCount::kCount4;
29  msaa_tex_desc.format = swapchain_image->GetPixelFormat();
30  msaa_tex_desc.size = swapchain_image->GetSize();
31  msaa_tex_desc.usage = TextureUsage::kRenderTarget;
32 
33  if (!swapchain_image->GetMSAATexture()) {
34  msaa_tex = context->GetResourceAllocator()->CreateTexture(msaa_tex_desc);
35  msaa_tex->SetLabel("ImpellerOnscreenColorMSAA");
36  if (!msaa_tex) {
37  VALIDATION_LOG << "Could not allocate MSAA color texture.";
38  return nullptr;
39  }
40  } else {
41  msaa_tex = swapchain_image->GetMSAATexture();
42  }
43  }
44 
45  TextureDescriptor resolve_tex_desc;
46  resolve_tex_desc.type = TextureType::kTexture2D;
47  resolve_tex_desc.format = swapchain_image->GetPixelFormat();
48  resolve_tex_desc.size = swapchain_image->GetSize();
49  resolve_tex_desc.usage = TextureUsage::kRenderTarget;
50  resolve_tex_desc.sample_count = SampleCount::kCount1;
51  resolve_tex_desc.storage_mode = StorageMode::kDevicePrivate;
52 
53  std::shared_ptr<Texture> resolve_tex =
54  std::make_shared<TextureVK>(context, //
55  swapchain_image //
56  );
57 
58  if (!resolve_tex) {
59  VALIDATION_LOG << "Could not wrap resolve texture.";
60  return nullptr;
61  }
62  resolve_tex->SetLabel("ImpellerOnscreenResolve");
63 
64  ColorAttachment color0;
67  if (enable_msaa) {
68  color0.texture = msaa_tex;
70  color0.resolve_texture = resolve_tex;
71  } else {
72  color0.texture = resolve_tex;
74  }
75 
76  RenderTarget render_target_desc;
77  render_target_desc.SetColorAttachment(color0, 0u);
78  render_target_desc.SetupDepthStencilAttachments(
79  /*context=*/*context, //
80  /*allocator=*/*context->GetResourceAllocator(), //
81  /*size=*/swapchain_image->GetSize(), //
82  /*msaa=*/enable_msaa, //
83  /*label=*/"Onscreen", //
84  /*stencil_attachment_config=*/
86  /*depth_stencil_texture=*/swapchain_image->GetDepthStencilTexture() //
87  );
88 
89  // The constructor is private. So make_unique may not be used.
90  return std::unique_ptr<KHRSurfaceVK>(
91  new KHRSurfaceVK(render_target_desc, std::move(swap_callback)));
92 }
93 
94 KHRSurfaceVK::KHRSurfaceVK(const RenderTarget& target,
95  SwapCallback swap_callback)
96  : Surface(target), swap_callback_(std::move(swap_callback)) {}
97 
98 KHRSurfaceVK::~KHRSurfaceVK() = default;
99 
100 bool KHRSurfaceVK::Present() const {
101  return swap_callback_ ? swap_callback_() : false;
102 }
103 
104 } // namespace impeller
impeller::StoreAction::kMultisampleResolve
@ kMultisampleResolve
khr_surface_vk.h
impeller::Attachment::store_action
StoreAction store_action
Definition: formats.h:642
impeller::KHRSurfaceVK::SwapCallback
std::function< bool(void)> SwapCallback
Definition: khr_surface_vk.h:18
impeller::ColorAttachment
Definition: formats.h:647
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:169
formats.h
impeller::TextureUsage::kRenderTarget
@ kRenderTarget
khr_swapchain_image_vk.h
impeller::TextureDescriptor::sample_count
SampleCount sample_count
Definition: texture_descriptor.h:44
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:648
impeller::Attachment::texture
std::shared_ptr< Texture > texture
Definition: formats.h:639
impeller::RenderTarget::kDefaultStencilAttachmentConfig
static constexpr AttachmentConfig kDefaultStencilAttachmentConfig
Definition: render_target.h:68
impeller::StorageMode::kDevicePrivate
@ kDevicePrivate
impeller::KHRSurfaceVK
Definition: khr_surface_vk.h:16
impeller::KHRSurfaceVK::~KHRSurfaceVK
~KHRSurfaceVK() override
impeller::RenderTarget
Definition: render_target.h:38
impeller::StoreAction::kStore
@ kStore
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:73
impeller::Attachment::resolve_texture
std::shared_ptr< Texture > resolve_texture
Definition: formats.h:640
std
Definition: comparable.h:95
impeller::Attachment::load_action
LoadAction load_action
Definition: formats.h:641
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::RenderTarget::SetupDepthStencilAttachments
void SetupDepthStencilAttachments(const Context &context, Allocator &allocator, ISize size, bool msaa, const std::string &label="Offscreen", RenderTarget::AttachmentConfig stencil_attachment_config=RenderTarget::kDefaultStencilAttachmentConfig, const std::shared_ptr< Texture > &depth_stencil_texture=nullptr)
Definition: render_target.cc:413
impeller
Definition: aiks_blur_unittests.cc:20
impeller::KHRSurfaceVK::WrapSwapchainImage
static std::unique_ptr< KHRSurfaceVK > WrapSwapchainImage(const std::shared_ptr< Context > &context, std::shared_ptr< KHRSwapchainImageVK > &swapchain_image, SwapCallback swap_callback, bool enable_msaa=true)
Wrap the swapchain image in a Surface, which provides the additional configuration required for usage...
Definition: khr_surface_vk.cc:14