Flutter Engine
 
Loading...
Searching...
No Matches
vulkan_surface.h
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#ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_VULKAN_SURFACE_H_
6#define FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_VULKAN_SURFACE_H_
7
8#include <fuchsia/sysmem2/cpp/fidl.h>
9#include <lib/async/cpp/wait.h>
10#include <lib/zx/event.h>
11#include <lib/zx/vmo.h>
12
13#include <array>
14#include <cstdint>
15#include <memory>
16
17#include "flutter/fml/macros.h"
22#include "third_party/skia/include/core/SkColorType.h"
23#include "third_party/skia/include/core/SkRefCnt.h"
24#include "third_party/skia/include/core/SkSize.h"
25#include "third_party/skia/include/core/SkSurface.h"
26
27#include "surface_producer.h"
28
29namespace flutter_runner {
30
31// A |VkImage| and its relevant metadata.
44
46 public:
48 fuchsia::sysmem2::AllocatorSyncPtr& sysmem_allocator,
49 fuchsia::ui::composition::AllocatorPtr& flatland_allocator,
50 sk_sp<GrDirectContext> context,
51 const SkISize& size);
52
53 ~VulkanSurface() override;
54
55 // |SurfaceProducerSurface|
56 size_t AdvanceAndGetAge() override;
57
58 // |SurfaceProducerSurface|
60
61 // |SurfaceProducerSurface|
62 bool IsValid() const override;
63
64 // |SurfaceProducerSurface|
65 SkISize GetSize() const override;
66
67 // Note: It is safe for the caller to collect the surface in the
68 // |on_writes_committed| callback.
70 const std::function<void(void)>& on_writes_committed) override;
71
72 // |SurfaceProducerSurface|
73 void SetImageId(uint32_t image_id) override;
74
75 // |SurfaceProducerSurface|
76 uint32_t GetImageId() override;
77
78 // |SurfaceProducerSurface|
79 sk_sp<SkSurface> GetSkiaSurface() const override;
80
81 // |SurfaceProducerSurface|
82 fuchsia::ui::composition::BufferCollectionImportToken
84
85 // |SurfaceProducerSurface|
86 zx::event GetAcquireFence() override;
87
88 // |SurfaceProducerSurface|
89 zx::event GetReleaseFence() override;
90
91 // |SurfaceProducerSurface|
93 ReleaseImageCallback release_image_callback) override;
94
96 return vulkan_image_.vk_image;
97 }
98
100 return acquire_semaphore_;
101 }
102
105 if (!command_buffer_)
106 command_buffer_ = std::make_unique<vulkan::VulkanCommandBuffer>(
107 vulkan_provider_.vk(), vulkan_provider_.vk_device(), pool);
108 return command_buffer_.get();
109 }
110
112 return command_buffer_fence_;
113 }
114
115 size_t GetAllocationSize() const { return vk_memory_info_.allocationSize; }
116
118 return vulkan_image_.vk_memory_requirements.size;
119 }
120
121 bool IsOversized() const {
123 }
124
125 bool HasStableSizeHistory() const {
126 return std::equal(size_history_.begin() + 1, size_history_.end(),
127 size_history_.begin());
128 }
129
130 private:
131 static constexpr int kSizeHistorySize = 4;
132
133 void OnHandleReady(async_dispatcher_t* dispatcher,
134 async::WaitBase* wait,
135 zx_status_t status,
136 const zx_packet_signal_t* signal);
137
138 bool AllocateDeviceMemory(
139 fuchsia::sysmem2::AllocatorSyncPtr& sysmem_allocator,
140 fuchsia::ui::composition::AllocatorPtr& flatland_allocator,
141 sk_sp<GrDirectContext> context,
142 const SkISize& size);
143
144 bool CreateVulkanImage(vulkan::VulkanProvider& vulkan_provider,
145 const SkISize& size,
146 VulkanImage* out_vulkan_image);
147
148 bool SetupSkiaSurface(sk_sp<GrDirectContext> context,
149 const SkISize& size,
150 SkColorType color_type,
151 const VkImageCreateInfo& image_create_info,
152 const VkMemoryRequirements& memory_reqs);
153
154 bool CreateFences();
155
156 void Reset();
157
158 vulkan::VulkanHandle<VkSemaphore> SemaphoreFromEvent(
159 const zx::event& event) const;
160
161 vulkan::VulkanProvider& vulkan_provider_;
162 VulkanImage vulkan_image_;
164 VkMemoryAllocateInfo vk_memory_info_;
165 vulkan::VulkanHandle<VkFence> command_buffer_fence_;
166 sk_sp<SkSurface> sk_surface_;
167 fuchsia::ui::composition::BufferCollectionImportToken import_token_;
168 uint32_t image_id_ = 0;
170 zx::event acquire_event_;
171 vulkan::VulkanHandle<VkSemaphore> acquire_semaphore_;
172 std::unique_ptr<vulkan::VulkanCommandBuffer> command_buffer_;
173 zx::event release_event_;
174 async::WaitMethod<VulkanSurface, &VulkanSurface::OnHandleReady> wait_;
175 std::function<void()> pending_on_writes_committed_;
176 std::array<SkISize, kSizeHistorySize> size_history_;
177 int size_history_index_ = 0;
178 size_t age_ = 0;
179 bool valid_ = false;
180 ReleaseImageCallback release_image_callback_;
181
183};
184
185} // namespace flutter_runner
186
187#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_VULKAN_SURFACE_H_
const vulkan::VulkanHandle< VkFence > & GetCommandBufferFence()
const vulkan::VulkanHandle< VkImage > & GetVkImage()
void SetReleaseImageCallback(ReleaseImageCallback release_image_callback) override
fuchsia::ui::composition::BufferCollectionImportToken GetBufferCollectionImportToken() override
bool IsValid() const override
vulkan::VulkanCommandBuffer * GetCommandBuffer(const vulkan::VulkanHandle< VkCommandPool > &pool)
zx::event GetAcquireFence() override
void SignalWritesFinished(const std::function< void(void)> &on_writes_committed) override
SkISize GetSize() const override
bool FlushSessionAcquireAndReleaseEvents() override
void SetImageId(uint32_t image_id) override
const vulkan::VulkanHandle< VkSemaphore > & GetAcquireVkSemaphore()
sk_sp< SkSurface > GetSkiaSurface() const override
zx::event GetReleaseFence() override
size_t GetImageMemoryRequirementsSize() const
virtual const vulkan::VulkanProcTable & vk()=0
virtual const vulkan::VulkanHandle< VkDevice > & vk_device()=0
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
std::function< void()> ReleaseImageCallback
uint32_t color_type
VkImageCreateInfo vk_image_create_info
VkMemoryRequirements vk_memory_requirements
VkBufferCollectionImageCreateInfoFUCHSIA vk_collection_image_create_info
VulkanImage & operator=(VulkanImage &&)=default
vulkan::VulkanHandle< VkImage > vk_image
VulkanImage(VulkanImage &&)=default
FML_DISALLOW_COPY_AND_ASSIGN(VulkanImage)