Flutter Engine
The Flutter Engine
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
5#include "flutter/testing/test_vulkan_surface.h"
6#include <memory>
7#include "flutter/fml/logging.h"
8#include "flutter/testing/test_vulkan_context.h"
9
18
19namespace flutter {
20namespace testing {
21
22TestVulkanSurface::TestVulkanSurface(TestVulkanImage&& image)
23 : image_(std::move(image)){};
24
25std::unique_ptr<TestVulkanSurface> TestVulkanSurface::Create(
26 const TestVulkanContext& context,
27 const SkISize& surface_size) {
28 auto image_result = context.CreateImage(surface_size);
29
30 if (!image_result.has_value()) {
31 FML_LOG(ERROR) << "Could not create VkImage.";
32 return nullptr;
33 }
34
35 GrVkImageInfo image_info = {
36 .fImage = image_result.value().GetImage(),
37 .fImageTiling = VK_IMAGE_TILING_OPTIMAL,
38 .fImageLayout = VK_IMAGE_LAYOUT_UNDEFINED,
39 .fFormat = VK_FORMAT_R8G8B8A8_UNORM,
40 .fImageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
44 .fSampleCount = 1,
45 .fLevelCount = 1,
46 };
47 auto backend_texture = GrBackendTextures::MakeVk(
48 surface_size.width(), surface_size.height(), image_info);
49
50 SkSurfaceProps surface_properties(0, kUnknown_SkPixelGeometry);
51
52 auto result = std::unique_ptr<TestVulkanSurface>(
53 new TestVulkanSurface(std::move(image_result.value())));
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_) {
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();
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 testing
114} // namespace flutter
@ kTopLeft_GrSurfaceOrigin
Definition: GrTypes.h:148
static sk_sp< Effect > Create()
Definition: RefCntTest.cpp:117
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
@ kUnknown_SkPixelGeometry
static sk_sp< SkColorSpace > MakeSRGB()
sk_sp< GrDirectContext > GetGrDirectContext() const
std::optional< TestVulkanImage > CreateImage(const SkISize &size) const
T * get() const
Definition: SkRefCnt.h:303
GAsyncResult * result
#define FML_LOG(severity)
Definition: logging.h:82
EGLSurface surface_
SK_API GrBackendTexture MakeVk(int width, int height, const GrVkImageInfo &, std::string_view label={})
sk_sp< const SkImage > image
Definition: SkRecords.h:269
SK_API sk_sp< SkSurface > WrapBackendTexture(GrRecordingContext *context, const GrBackendTexture &backendTexture, GrSurfaceOrigin origin, int sampleCnt, SkColorType colorType, sk_sp< SkColorSpace > colorSpace, const SkSurfaceProps *surfaceProps, TextureReleaseProc textureReleaseProc=nullptr, ReleaseContext releaseContext=nullptr)
Definition: ref_ptr.h:256
VkImage fImage
Definition: GrVkTypes.h:26
Definition: SkSize.h:16
constexpr int32_t width() const
Definition: SkSize.h:36
constexpr int32_t height() const
Definition: SkSize.h:37
#define ERROR(message)
Definition: elf_loader.cc:260
@ VK_IMAGE_LAYOUT_UNDEFINED
Definition: vulkan_core.h:1331
@ VK_IMAGE_TILING_OPTIMAL
Definition: vulkan_core.h:1767
@ VK_IMAGE_USAGE_TRANSFER_DST_BIT
Definition: vulkan_core.h:2353
@ VK_IMAGE_USAGE_SAMPLED_BIT
Definition: vulkan_core.h:2354
@ VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
Definition: vulkan_core.h:2356
@ VK_IMAGE_USAGE_TRANSFER_SRC_BIT
Definition: vulkan_core.h:2352
@ VK_FORMAT_R8G8B8A8_UNORM
Definition: vulkan_core.h:1496