Flutter Engine
 
Loading...
Searching...
No Matches
test_vulkan_surface.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#include <memory>
9
10#include "third_party/skia/include/core/SkColorSpace.h"
11#include "third_party/skia/include/core/SkColorType.h"
12#include "third_party/skia/include/core/SkSurface.h"
13#include "third_party/skia/include/core/SkSurfaceProps.h"
14#include "third_party/skia/include/gpu/ganesh/GrBackendSurface.h"
15#include "third_party/skia/include/gpu/ganesh/SkSurfaceGanesh.h"
16#include "third_party/skia/include/gpu/ganesh/vk/GrVkBackendSurface.h"
17#include "third_party/skia/include/gpu/ganesh/vk/GrVkTypes.h"
18
19namespace flutter::testing {
20
21TestVulkanSurface::TestVulkanSurface(TestVulkanImage&& image)
22 : image_(std::move(image)) {};
23
24std::unique_ptr<TestVulkanSurface> TestVulkanSurface::Create(
25 const TestVulkanContext& context,
26 const DlISize& surface_size) {
27 auto image_result = context.CreateImage(surface_size);
28
29 if (!image_result.has_value()) {
30 FML_LOG(ERROR) << "Could not create VkImage.";
31 return nullptr;
32 }
33
34 GrVkImageInfo image_info = {
35 .fImage = image_result.value().GetImage(),
36 .fImageTiling = VK_IMAGE_TILING_OPTIMAL,
37 .fImageLayout = VK_IMAGE_LAYOUT_UNDEFINED,
38 .fFormat = VK_FORMAT_R8G8B8A8_UNORM,
39 .fImageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
40 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
41 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
42 VK_IMAGE_USAGE_SAMPLED_BIT,
43 .fSampleCount = 1,
44 .fLevelCount = 1,
45 };
46 auto backend_texture = GrBackendTextures::MakeVk(
47 surface_size.width, surface_size.height, image_info);
48
49 SkSurfaceProps surface_properties(0, kUnknown_SkPixelGeometry);
50
51 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
52 auto result = std::unique_ptr<TestVulkanSurface>(
53 new TestVulkanSurface(std::move(image_result.value())));
54 result->surface_ = SkSurfaces::WrapBackendTexture(
55 context.GetGrDirectContext().get(), // context
56 backend_texture, // back-end texture
57 kTopLeft_GrSurfaceOrigin, // surface origin
58 1, // sample count
59 kRGBA_8888_SkColorType, // color type
60 SkColorSpace::MakeSRGB(), // color space
61 &surface_properties, // surface properties
62 nullptr, // release proc
63 nullptr // release context
64 );
65
66 if (!result->surface_) {
67 FML_LOG(ERROR)
68 << "Could not wrap VkImage as an SkSurface Vulkan render texture.";
69 return nullptr;
70 }
71
72 return result;
73}
74
75bool TestVulkanSurface::IsValid() const {
76 return surface_ != nullptr;
77}
78
79sk_sp<SkImage> TestVulkanSurface::GetSurfaceSnapshot() const {
80 if (!IsValid()) {
81 return nullptr;
82 }
83
84 if (!surface_) {
85 FML_LOG(ERROR) << "Aborting snapshot because of on-screen surface "
86 "acquisition failure.";
87 return nullptr;
88 }
89
90 auto device_snapshot = surface_->makeImageSnapshot();
91
92 if (!device_snapshot) {
93 FML_LOG(ERROR) << "Could not create the device snapshot while attempting "
94 "to snapshot the Vulkan surface.";
95 return nullptr;
96 }
97
98 auto host_snapshot = device_snapshot->makeRasterImage(nullptr);
99
100 if (!host_snapshot) {
101 FML_LOG(ERROR) << "Could not create the host snapshot while attempting to "
102 "snapshot the Vulkan surface.";
103 return nullptr;
104 }
105
106 return host_snapshot;
107}
108
109VkImage TestVulkanSurface::GetImage() {
110 return image_.GetImage();
111}
112
113} // namespace flutter::testing
sk_sp< GrDirectContext > GetGrDirectContext() const
std::optional< TestVulkanImage > CreateImage(const DlISize &size) const
FlutterVulkanImage * image
#define FML_LOG(severity)
Definition logging.h:101
EGLSurface surface_
Definition ref_ptr.h:261
Type height
Definition size.h:29
Type width
Definition size.h:28