• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/vk/VulkanMutableTextureState.h"
19 #include "include/gpu/vk/VulkanTypes.h"
20 #include "src/gpu/graphite/Caps.h"
21 #include "src/gpu/graphite/ContextPriv.h"
22 #include "src/gpu/graphite/ResourceTypes.h"
23 
24 using namespace skgpu;
25 using namespace skgpu::graphite;
26 
27 namespace {
28     const SkISize kSize = {16, 16};
29 }
30 
DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(BackendTextureTest,reporter,context,CtsEnforcement::kNextRelease)31 DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(BackendTextureTest, reporter, context,
32                                    CtsEnforcement::kNextRelease) {
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,
44                                                           Protected::kNo,
45                                                           Renderable::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
DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(SurfaceBackendTextureTest,reporter,context,CtsEnforcement::kNextRelease)90 DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(SurfaceBackendTextureTest, reporter, context,
91                                    CtsEnforcement::kNextRelease) {
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,
107                                                           Protected::kNo,
108                                                           Renderable::kYes);
109 
110     auto texture = recorder->createBackendTexture(kSize, info);
111     REPORTER_ASSERT(reporter, texture.isValid());
112 
113     sk_sp<SkSurface> surface = SkSurfaces::WrapBackendTexture(recorder.get(),
114                                                               texture,
115                                                               kRGBA_8888_SkColorType,
116                                                               /*colorSpace=*/nullptr,
117                                                               /*props=*/nullptr);
118     REPORTER_ASSERT(reporter, surface);
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.
124     surface = SkSurfaces::WrapBackendTexture(recorder.get(),
125                                              texture,
126                                              kAlpha_8_SkColorType,
127                                              /*colorSpace=*/nullptr,
128                                              /*props=*/nullptr);
129     REPORTER_ASSERT(reporter, !surface);
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,
136                                               Protected::kNo,
137                                               Renderable::kNo);
138     texture = recorder->createBackendTexture(kSize, info);
139     REPORTER_ASSERT(reporter, texture.isValid());
140 
141     surface = SkSurfaces::WrapBackendTexture(recorder.get(),
142                                              texture,
143                                              kRGBA_8888_SkColorType,
144                                              /*colorSpace=*/nullptr,
145                                              /*props=*/nullptr);
146 
147     REPORTER_ASSERT(reporter, !surface);
148     recorder->deleteBackendTexture(texture);
149 }
150 
151 // Tests the wrapping of a BackendTexture in an SkImage
DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(ImageBackendTextureTest,reporter,context,CtsEnforcement::kNextRelease)152 DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(ImageBackendTextureTest, reporter, context,
153                                    CtsEnforcement::kNextRelease) {
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 
170             TextureInfo info = caps->getDefaultSampledTextureInfo(kRGBA_8888_SkColorType,
171                                                                   mipmapped,
172                                                                   Protected::kNo,
173                                                                   renderable);
174 
175             BackendTexture texture = recorder->createBackendTexture(kSize, info);
176             REPORTER_ASSERT(reporter, texture.isValid());
177 
178             sk_sp<SkImage> image = SkImages::WrapTexture(recorder.get(),
179                                                          texture,
180                                                          kRGBA_8888_SkColorType,
181                                                          kPremul_SkAlphaType,
182                                                          /*colorSpace=*/nullptr);
183             REPORTER_ASSERT(reporter, image);
184             REPORTER_ASSERT(reporter, image->hasMipmaps() == (mipmapped == Mipmapped::kYes));
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,
192                                           kAlpha_8_SkColorType,
193                                           kPremul_SkAlphaType,
194                                           /* colorSpace= */ nullptr);
195             REPORTER_ASSERT(reporter, !image);
196 
197             recorder->deleteBackendTexture(texture);
198         }
199     }
200 }
201 
202 #ifdef SK_VULKAN
DEF_GRAPHITE_TEST_FOR_VULKAN_CONTEXT(VulkanBackendTextureMutableStateTest,reporter,context,CtsEnforcement::kNextRelease)203 DEF_GRAPHITE_TEST_FOR_VULKAN_CONTEXT(VulkanBackendTextureMutableStateTest, reporter, context,
204                                      CtsEnforcement::kNextRelease) {
205     VulkanTextureInfo info(/*sampleCount=*/1,
206                            /*mipmapped=*/Mipmapped::kNo,
207                            /*flags=*/0,
208                            VK_FORMAT_R8G8B8A8_UNORM,
209                            VK_IMAGE_TILING_OPTIMAL,
210                            VK_IMAGE_USAGE_SAMPLED_BIT,
211                            VK_SHARING_MODE_EXCLUSIVE,
212                            VK_IMAGE_ASPECT_COLOR_BIT,
213                            /*ycbcrConversionInfo*/{});
214 
215     BackendTexture texture({16, 16},
216                            info,
217                            VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
218                            /*queueFamilyIndex=*/1,
219                            VK_NULL_HANDLE,
220                            skgpu::VulkanAlloc());
221 
222     REPORTER_ASSERT(reporter, texture.isValid());
223     REPORTER_ASSERT(reporter,
224                     texture.getVkImageLayout() == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
225     REPORTER_ASSERT(reporter, texture.getVkQueueFamilyIndex() == 1);
226 
227     skgpu::MutableTextureState newState =
228             skgpu::MutableTextureStates::MakeVulkan(VK_IMAGE_LAYOUT_GENERAL, 0);
229     texture.setMutableState(newState);
230 
231     REPORTER_ASSERT(reporter,
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
240