forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_vulkan_surface.cc
112 lines (91 loc) · 3.53 KB
/
test_vulkan_surface.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/testing/test_vulkan_surface.h"
#include <memory>
#include "flutter/fml/logging.h"
#include "flutter/testing/test_vulkan_context.h"
#include "third_party/skia/include/core/SkColorSpace.h"
#include "third_party/skia/include/core/SkColorType.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "third_party/skia/include/core/SkSurfaceProps.h"
namespace flutter {
namespace testing {
TestVulkanSurface::TestVulkanSurface(TestVulkanImage&& image)
: image_(std::move(image)){};
std::unique_ptr<TestVulkanSurface> TestVulkanSurface::Create(
const TestVulkanContext& context,
const SkISize& surface_size) {
auto image_result = context.CreateImage(surface_size);
if (!image_result.has_value()) {
FML_LOG(ERROR) << "Could not create VkImage.";
return nullptr;
}
GrVkImageInfo image_info = {
.fImage = image_result.value().GetImage(),
.fImageTiling = VK_IMAGE_TILING_OPTIMAL,
.fImageLayout = VK_IMAGE_LAYOUT_UNDEFINED,
.fFormat = VK_FORMAT_R8G8B8A8_UNORM,
.fImageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
VK_IMAGE_USAGE_TRANSFER_DST_BIT |
VK_IMAGE_USAGE_SAMPLED_BIT,
.fSampleCount = 1,
.fLevelCount = 1,
};
GrBackendTexture backend_texture(surface_size.width(), //
surface_size.height(), //
image_info //
);
SkSurfaceProps surface_properties(0, kUnknown_SkPixelGeometry);
auto result = std::unique_ptr<TestVulkanSurface>(
new TestVulkanSurface(std::move(image_result.value())));
result->surface_ = SkSurface::MakeFromBackendTexture(
context.GetGrDirectContext().get(), // context
backend_texture, // back-end texture
kTopLeft_GrSurfaceOrigin, // surface origin
1, // sample count
kRGBA_8888_SkColorType, // color type
SkColorSpace::MakeSRGB(), // color space
&surface_properties, // surface properties
nullptr, // release proc
nullptr // release context
);
if (!result->surface_) {
FML_LOG(ERROR)
<< "Could not wrap VkImage as an SkSurface Vulkan render texture.";
return nullptr;
}
return result;
}
bool TestVulkanSurface::IsValid() const {
return surface_ != nullptr;
}
sk_sp<SkImage> TestVulkanSurface::GetSurfaceSnapshot() const {
if (!IsValid()) {
return nullptr;
}
if (!surface_) {
FML_LOG(ERROR) << "Aborting snapshot because of on-screen surface "
"acquisition failure.";
return nullptr;
}
auto device_snapshot = surface_->makeImageSnapshot();
if (!device_snapshot) {
FML_LOG(ERROR) << "Could not create the device snapshot while attempting "
"to snapshot the Vulkan surface.";
return nullptr;
}
auto host_snapshot = device_snapshot->makeRasterImage();
if (!host_snapshot) {
FML_LOG(ERROR) << "Could not create the host snapshot while attempting to "
"snapshot the Vulkan surface.";
return nullptr;
}
return host_snapshot;
}
VkImage TestVulkanSurface::GetImage() {
return image_.GetImage();
}
} // namespace testing
} // namespace flutter