Flutter Engine
The Flutter Engine
BackendTextureTest.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2021 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "tests/Test.h"
9
23
24using namespace skgpu;
25using namespace skgpu::graphite;
26
27namespace {
28 const SkISize kSize = {16, 16};
29}
30
31DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(BackendTextureTest, reporter, context,
33 // TODO: Remove this check once Vulkan supports creating default TexutreInfo from caps and we
34 // implement createBackendTexture.
35 if (context->backend() == BackendApi::kVulkan) {
36 return;
37 }
38
39 auto caps = context->priv().caps();
40 auto recorder = context->makeRecorder();
41
42 TextureInfo info = caps->getDefaultSampledTextureInfo(kRGBA_8888_SkColorType,
43 /*mipmapped=*/Mipmapped::kNo,
46 REPORTER_ASSERT(reporter, info.isValid());
47
48 auto texture1 = recorder->createBackendTexture(kSize, info);
49 REPORTER_ASSERT(reporter, texture1.isValid());
50
51 // We make a copy to do the remaining tests so we still have texture1 to safely delete the
52 // backend object.
53 auto texture1Copy = texture1;
54 REPORTER_ASSERT(reporter, texture1Copy.isValid());
55 REPORTER_ASSERT(reporter, texture1 == texture1Copy);
56
57 auto texture2 = recorder->createBackendTexture(kSize, info);
58 REPORTER_ASSERT(reporter, texture2.isValid());
59
60 REPORTER_ASSERT(reporter, texture1Copy != texture2);
61
62 // Test state after assignment
63 texture1Copy = texture2;
64 REPORTER_ASSERT(reporter, texture1Copy.isValid());
65 REPORTER_ASSERT(reporter, texture1Copy == texture2);
66
67 BackendTexture invalidTexture;
68 REPORTER_ASSERT(reporter, !invalidTexture.isValid());
69
70 texture1Copy = invalidTexture;
71 REPORTER_ASSERT(reporter, !texture1Copy.isValid());
72
73 texture1Copy = texture1;
74 REPORTER_ASSERT(reporter, texture1Copy.isValid());
75 REPORTER_ASSERT(reporter, texture1 == texture1Copy);
76
77 recorder->deleteBackendTexture(texture1);
78 recorder->deleteBackendTexture(texture2);
79
80 // Test that deleting is safe from the Context or a different Recorder.
81 texture1 = recorder->createBackendTexture(kSize, info);
82 context->deleteBackendTexture(texture1);
83
84 auto recorder2 = context->makeRecorder();
85 texture1 = recorder->createBackendTexture(kSize, info);
86 recorder2->deleteBackendTexture(texture1);
87}
88
89// Tests the wrapping of a BackendTexture in an SkSurface
90DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(SurfaceBackendTextureTest, reporter, context,
92 // TODO: Right now this just tests very basic combinations of surfaces. This should be expanded
93 // to cover a much broader set of things once we add more support in Graphite for different
94 // formats, color types, etc.
95
96 // TODO: Remove this check once Vulkan supports creating default TexutreInfo from caps and we
97 // implement createBackendTexture.
98 if (context->backend() == BackendApi::kVulkan) {
99 return;
100 }
101
102 auto caps = context->priv().caps();
103 std::unique_ptr<Recorder> recorder = context->makeRecorder();
104
105 TextureInfo info = caps->getDefaultSampledTextureInfo(kRGBA_8888_SkColorType,
106 /*mipmapped=*/Mipmapped::kNo,
109
110 auto texture = recorder->createBackendTexture(kSize, info);
111 REPORTER_ASSERT(reporter, texture.isValid());
112
114 texture,
116 /*colorSpace=*/nullptr,
117 /*props=*/nullptr);
119
120 surface.reset();
121
122 // We should fail when trying to wrap the same texture in a surface with a non-compatible
123 // color type.
125 texture,
127 /*colorSpace=*/nullptr,
128 /*props=*/nullptr);
130
131 recorder->deleteBackendTexture(texture);
132
133 // We should fail to wrap a non-renderable texture in a surface.
134 info = caps->getDefaultSampledTextureInfo(kRGBA_8888_SkColorType,
135 /*mipmapped=*/Mipmapped::kNo,
138 texture = recorder->createBackendTexture(kSize, info);
139 REPORTER_ASSERT(reporter, texture.isValid());
140
142 texture,
144 /*colorSpace=*/nullptr,
145 /*props=*/nullptr);
146
148 recorder->deleteBackendTexture(texture);
149}
150
151// Tests the wrapping of a BackendTexture in an SkImage
152DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(ImageBackendTextureTest, reporter, context,
154 // TODO: Right now this just tests very basic combinations of images. This should be expanded
155 // to cover a much broader set of things once we add more support in Graphite for different
156 // formats, color types, etc.
157
158 // TODO: Remove this check once Vulkan supports creating default TexutreInfo from caps and we
159 // implement createBackendTexture.
160 if (context->backend() == BackendApi::kVulkan) {
161 return;
162 }
163
164 const Caps* caps = context->priv().caps();
165 std::unique_ptr<Recorder> recorder = context->makeRecorder();
166
167 for (Mipmapped mipmapped : { Mipmapped::kYes, Mipmapped::kNo }) {
168 for (Renderable renderable : { Renderable::kYes, Renderable::kNo }) {
169
171 mipmapped,
173 renderable);
174
175 BackendTexture texture = recorder->createBackendTexture(kSize, info);
176 REPORTER_ASSERT(reporter, texture.isValid());
177
179 texture,
182 /*colorSpace=*/nullptr);
185
186 image.reset();
187
188 // We should fail when trying to wrap the same texture in an image with a non-compatible
189 // color type.
190 image = SkImages::WrapTexture(recorder.get(),
191 texture,
194 /* colorSpace= */ nullptr);
196
197 recorder->deleteBackendTexture(texture);
198 }
199 }
200}
201
202#ifdef SK_VULKAN
203DEF_GRAPHITE_TEST_FOR_VULKAN_CONTEXT(VulkanBackendTextureMutableStateTest, reporter, context,
205 VulkanTextureInfo info(/*sampleCount=*/1,
206 /*mipmapped=*/Mipmapped::kNo,
207 /*flags=*/0,
213 /*ycbcrConversionInfo*/{});
214
215 BackendTexture texture({16, 16},
216 info,
218 /*queueFamilyIndex=*/1,
221
222 REPORTER_ASSERT(reporter, texture.isValid());
225 REPORTER_ASSERT(reporter, texture.getVkQueueFamilyIndex() == 1);
226
229 texture.setMutableState(newState);
230
232 texture.getVkImageLayout() == VK_IMAGE_LAYOUT_GENERAL);
233 REPORTER_ASSERT(reporter, texture.getVkQueueFamilyIndex() == 0);
234
235 // TODO: Add to this test to check that the setMutableState calls also update values we see in
236 // wrapped VulkanTextures once we have them. Also check that updates in VulkanTexture are also
237 // visible in the getters of BackendTexture. We will need a real VkImage to do these tests.
238}
239#endif // SK_VULKAN
DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(BackendTextureTest, reporter, context, CtsEnforcement::kNextRelease)
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
reporter
Definition: FontMgrTest.cpp:39
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
@ kAlpha_8_SkColorType
pixel with alpha in 8-bit byte
Definition: SkColorType.h:21
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
#define DEF_GRAPHITE_TEST_FOR_VULKAN_CONTEXT(name, reporter, graphite_context, ctsEnforcement)
Definition: Test.h:381
bool hasMipmaps() const
Definition: SkImage.cpp:292
void reset(T *ptr=nullptr)
Definition: SkRefCnt.h:310
virtual TextureInfo getDefaultSampledTextureInfo(SkColorType, Mipmapped mipmapped, Protected, Renderable) const =0
@ kVulkan
Definition: embedder.h:86
VkSurfaceKHR surface
Definition: main.cc:49
constexpr int kSize
FlTexture * texture
SK_API sk_sp< SkImage > WrapTexture(skgpu::graphite::Recorder *, const skgpu::graphite::BackendTexture &, SkColorType colorType, SkAlphaType alphaType, sk_sp< SkColorSpace > colorSpace, skgpu::Origin origin, GenerateMipmapsFromBase generateMipmapsFromBase, TextureReleaseProc=nullptr, ReleaseContext=nullptr, 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)
SK_API MutableTextureState MakeVulkan(VkImageLayout layout, uint32_t queueFamilyIndex)
Definition: GpuTools.h:21
Renderable
Definition: GpuTypes.h:69
Mipmapped
Definition: GpuTypes.h:53
Definition: SkSize.h:16
@ VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
Definition: vulkan_core.h:1336
@ VK_IMAGE_LAYOUT_GENERAL
Definition: vulkan_core.h:1332
@ VK_SHARING_MODE_EXCLUSIVE
Definition: vulkan_core.h:1813
@ VK_IMAGE_TILING_OPTIMAL
Definition: vulkan_core.h:1767
@ VK_IMAGE_ASPECT_COLOR_BIT
Definition: vulkan_core.h:2238
@ VK_IMAGE_USAGE_SAMPLED_BIT
Definition: vulkan_core.h:2354
#define VK_NULL_HANDLE
Definition: vulkan_core.h:46
@ VK_FORMAT_R8G8B8A8_UNORM
Definition: vulkan_core.h:1496