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
10 #include "include/core/SkColorSpace.h"
11 #include "include/core/SkSurface.h"
12 #include "include/gpu/MutableTextureState.h"
13 #include "include/gpu/graphite/BackendTexture.h"
14 #include "include/gpu/graphite/Context.h"
15 #include "include/gpu/graphite/Image.h"
16 #include "include/gpu/graphite/Recorder.h"
17 #include "include/gpu/graphite/Surface.h"
18 #include "include/gpu/graphite/vk/VulkanGraphiteTypes.h"
19 #include "include/gpu/vk/VulkanMutableTextureState.h"
20 #include "include/gpu/vk/VulkanTypes.h"
21 #include "src/gpu/graphite/Caps.h"
22 #include "src/gpu/graphite/ContextPriv.h"
23 #include "src/gpu/graphite/ResourceTypes.h"
24 #include "src/gpu/graphite/vk/VulkanGraphiteUtils.h"
25
26 using namespace skgpu;
27 using namespace skgpu::graphite;
28
29 namespace {
30 const SkISize kSize = {16, 16};
31 }
32
DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(BackendTextureTest,reporter,context,CtsEnforcement::kApiLevel_202504)33 DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(BackendTextureTest, reporter, context,
34 CtsEnforcement::kApiLevel_202504) {
35 auto caps = context->priv().caps();
36 auto recorder = context->makeRecorder();
37
38 Protected isProtected = Protected(context->supportsProtectedContent());
39
40 TextureInfo info = caps->getDefaultSampledTextureInfo(kRGBA_8888_SkColorType,
41 /*mipmapped=*/Mipmapped::kNo,
42 isProtected,
43 Renderable::kNo);
44 REPORTER_ASSERT(reporter, info.isValid());
45
46 auto texture1 = recorder->createBackendTexture(kSize, info);
47 REPORTER_ASSERT(reporter, texture1.isValid());
48
49 // We make a copy to do the remaining tests so we still have texture1 to safely delete the
50 // backend object.
51 auto texture1Copy = texture1;
52 REPORTER_ASSERT(reporter, texture1Copy.isValid());
53 REPORTER_ASSERT(reporter, texture1 == texture1Copy);
54
55 auto texture2 = recorder->createBackendTexture(kSize, info);
56 REPORTER_ASSERT(reporter, texture2.isValid());
57
58 REPORTER_ASSERT(reporter, texture1Copy != texture2);
59
60 // Test state after assignment
61 texture1Copy = texture2;
62 REPORTER_ASSERT(reporter, texture1Copy.isValid());
63 REPORTER_ASSERT(reporter, texture1Copy == texture2);
64
65 BackendTexture invalidTexture;
66 REPORTER_ASSERT(reporter, !invalidTexture.isValid());
67
68 texture1Copy = invalidTexture;
69 REPORTER_ASSERT(reporter, !texture1Copy.isValid());
70
71 texture1Copy = texture1;
72 REPORTER_ASSERT(reporter, texture1Copy.isValid());
73 REPORTER_ASSERT(reporter, texture1 == texture1Copy);
74
75 recorder->deleteBackendTexture(texture1);
76 recorder->deleteBackendTexture(texture2);
77
78 // Test that deleting is safe from the Context or a different Recorder.
79 texture1 = recorder->createBackendTexture(kSize, info);
80 context->deleteBackendTexture(texture1);
81
82 auto recorder2 = context->makeRecorder();
83 texture1 = recorder->createBackendTexture(kSize, info);
84 recorder2->deleteBackendTexture(texture1);
85 }
86
87 // Tests the wrapping of a BackendTexture in an SkSurface
DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(SurfaceBackendTextureTest,reporter,context,CtsEnforcement::kNextRelease)88 DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(SurfaceBackendTextureTest, reporter, context,
89 CtsEnforcement::kNextRelease) {
90 // TODO: Right now this just tests very basic combinations of surfaces. This should be expanded
91 // to cover a much broader set of things once we add more support in Graphite for different
92 // formats, color types, etc.
93
94 // TODO: Remove this check once Vulkan supports creating default TexutreInfo from caps and we
95 // implement createBackendTexture.
96 if (context->backend() == BackendApi::kVulkan) {
97 return;
98 }
99
100 auto caps = context->priv().caps();
101 std::unique_ptr<Recorder> recorder = context->makeRecorder();
102
103 TextureInfo info = caps->getDefaultSampledTextureInfo(kRGBA_8888_SkColorType,
104 /*mipmapped=*/Mipmapped::kNo,
105 Protected::kNo,
106 Renderable::kYes);
107
108 auto texture = recorder->createBackendTexture(kSize, info);
109 REPORTER_ASSERT(reporter, texture.isValid());
110
111 sk_sp<SkSurface> surface = SkSurfaces::WrapBackendTexture(recorder.get(),
112 texture,
113 kRGBA_8888_SkColorType,
114 /*colorSpace=*/nullptr,
115 /*props=*/nullptr);
116 REPORTER_ASSERT(reporter, surface);
117
118 surface.reset();
119
120 // We should fail when trying to wrap the same texture in a surface with a non-compatible
121 // color type.
122 surface = SkSurfaces::WrapBackendTexture(recorder.get(),
123 texture,
124 kAlpha_8_SkColorType,
125 /*colorSpace=*/nullptr,
126 /*props=*/nullptr);
127 REPORTER_ASSERT(reporter, !surface);
128
129 recorder->deleteBackendTexture(texture);
130
131 // We should fail to wrap a non-renderable texture in a surface.
132 info = caps->getDefaultSampledTextureInfo(kRGBA_8888_SkColorType,
133 /*mipmapped=*/Mipmapped::kNo,
134 Protected::kNo,
135 Renderable::kNo);
136 texture = recorder->createBackendTexture(kSize, info);
137 REPORTER_ASSERT(reporter, texture.isValid());
138
139 surface = SkSurfaces::WrapBackendTexture(recorder.get(),
140 texture,
141 kRGBA_8888_SkColorType,
142 /*colorSpace=*/nullptr,
143 /*props=*/nullptr);
144
145 REPORTER_ASSERT(reporter, !surface);
146 recorder->deleteBackendTexture(texture);
147 }
148
149 // Tests the wrapping of a BackendTexture in an SkImage
DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(ImageBackendTextureTest,reporter,context,CtsEnforcement::kNextRelease)150 DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(ImageBackendTextureTest, reporter, context,
151 CtsEnforcement::kNextRelease) {
152 // TODO: Right now this just tests very basic combinations of images. This should be expanded
153 // to cover a much broader set of things once we add more support in Graphite for different
154 // formats, color types, etc.
155
156 // TODO: Remove this check once Vulkan supports creating default TexutreInfo from caps and we
157 // implement createBackendTexture.
158 if (context->backend() == BackendApi::kVulkan) {
159 return;
160 }
161
162 const Caps* caps = context->priv().caps();
163 std::unique_ptr<Recorder> recorder = context->makeRecorder();
164
165 for (Mipmapped mipmapped : { Mipmapped::kYes, Mipmapped::kNo }) {
166 for (Renderable renderable : { Renderable::kYes, Renderable::kNo }) {
167
168 TextureInfo info = caps->getDefaultSampledTextureInfo(kRGBA_8888_SkColorType,
169 mipmapped,
170 Protected::kNo,
171 renderable);
172
173 BackendTexture texture = recorder->createBackendTexture(kSize, info);
174 REPORTER_ASSERT(reporter, texture.isValid());
175
176 sk_sp<SkImage> image = SkImages::WrapTexture(recorder.get(),
177 texture,
178 kRGBA_8888_SkColorType,
179 kPremul_SkAlphaType,
180 /*colorSpace=*/nullptr);
181 REPORTER_ASSERT(reporter, image);
182 REPORTER_ASSERT(reporter, image->hasMipmaps() == (mipmapped == Mipmapped::kYes));
183
184 image.reset();
185
186 // We should fail when trying to wrap the same texture in an image with a non-compatible
187 // color type.
188 image = SkImages::WrapTexture(recorder.get(),
189 texture,
190 kAlpha_8_SkColorType,
191 kPremul_SkAlphaType,
192 /* colorSpace= */ nullptr);
193 REPORTER_ASSERT(reporter, !image);
194
195 recorder->deleteBackendTexture(texture);
196 }
197 }
198 }
199
200 #ifdef SK_VULKAN
DEF_GRAPHITE_TEST_FOR_VULKAN_CONTEXT(VulkanBackendTextureMutableStateTest,reporter,context,CtsEnforcement::kApiLevel_202404)201 DEF_GRAPHITE_TEST_FOR_VULKAN_CONTEXT(VulkanBackendTextureMutableStateTest, reporter, context,
202 CtsEnforcement::kApiLevel_202404) {
203 VulkanTextureInfo info(/*sampleCount=*/1,
204 /*mipmapped=*/Mipmapped::kNo,
205 /*flags=*/0,
206 VK_FORMAT_R8G8B8A8_UNORM,
207 VK_IMAGE_TILING_OPTIMAL,
208 VK_IMAGE_USAGE_SAMPLED_BIT,
209 VK_SHARING_MODE_EXCLUSIVE,
210 VK_IMAGE_ASPECT_COLOR_BIT,
211 /*ycbcrConversionInfo*/{});
212
213 BackendTexture texture = BackendTextures::MakeVulkan({16, 16},
214 info,
215 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
216 /*queueFamilyIndex=*/1,
217 VK_NULL_HANDLE,
218 skgpu::VulkanAlloc());
219
220 REPORTER_ASSERT(reporter, texture.isValid());
221 REPORTER_ASSERT(
222 reporter,
223 BackendTextures::GetVkImageLayout(texture) == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
224 REPORTER_ASSERT(reporter, BackendTextures::GetVkQueueFamilyIndex(texture) == 1);
225
226 skgpu::MutableTextureState newState =
227 skgpu::MutableTextureStates::MakeVulkan(VK_IMAGE_LAYOUT_GENERAL, 0);
228 BackendTextures::SetMutableState(&texture, newState);
229
230 REPORTER_ASSERT(reporter,
231 BackendTextures::GetVkImageLayout(texture) == VK_IMAGE_LAYOUT_GENERAL);
232 REPORTER_ASSERT(reporter, BackendTextures::GetVkQueueFamilyIndex(texture) == 0);
233
234 // TODO: Add to this test to check that the setMutableState calls also update values we see in
235 // wrapped VulkanTextures once we have them. Also check that updates in VulkanTexture are also
236 // visible in the getters of BackendTexture. We will need a real VkImage to do these tests.
237 }
238 #endif // SK_VULKAN
239