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