xref: /aosp_15_r20/external/skia/tests/VkWrapTests.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2016 Google Inc.
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 // This is a GPU-backend specific test. It relies on static initializers to work
9 
10 #include "include/core/SkTypes.h"
11 
12 #if defined(SK_VULKAN)
13 
14 #include "include/core/SkColorType.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/gpu/GpuTypes.h"
17 #include "include/gpu/ganesh/GrBackendSurface.h"
18 #include "include/gpu/ganesh/GrDirectContext.h"
19 #include "include/gpu/ganesh/GrTypes.h"
20 #include "include/gpu/ganesh/vk/GrVkBackendSurface.h"
21 #include "include/gpu/ganesh/vk/GrVkTypes.h"
22 #include "include/gpu/vk/VulkanTypes.h"
23 #include "include/private/base/SkTo.h"
24 #include "include/private/gpu/ganesh/GrTypesPriv.h"
25 #include "src/gpu/ganesh/GrCaps.h"
26 #include "src/gpu/ganesh/GrDirectContextPriv.h"
27 #include "src/gpu/ganesh/GrGpu.h"
28 #include "src/gpu/ganesh/GrRenderTarget.h"
29 #include "src/gpu/ganesh/GrTexture.h"
30 #include "tests/CtsEnforcement.h"
31 #include "tests/Test.h"
32 #include "tools/gpu/ManagedBackendTexture.h"
33 
34 #include <vulkan/vulkan_core.h>
35 #include <initializer_list>
36 
37 struct GrContextOptions;
38 
39 using sk_gpu_test::GrContextFactory;
40 
41 const int kW = 1024;
42 const int kH = 1024;
43 const SkColorType kColorType = SkColorType::kRGBA_8888_SkColorType;
44 
wrap_tex_test(skiatest::Reporter * reporter,GrDirectContext * dContext)45 void wrap_tex_test(skiatest::Reporter* reporter, GrDirectContext* dContext) {
46     using namespace skgpu;
47 
48     GrGpu* gpu = dContext->priv().getGpu();
49 
50     Protected isProtected = Protected(dContext->priv().caps()->supportsProtectedContent());
51 
52     auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData(
53             dContext, kW, kH, kRGBA_8888_SkColorType, skgpu::Mipmapped::kNo, GrRenderable::kNo,
54             isProtected);
55     if (!mbet) {
56         ERRORF(reporter, "Could not create backend texture.");
57         return;
58     }
59 
60     GrBackendTexture origBackendTex = mbet->texture();
61 
62     GrVkImageInfo imageInfo;
63     SkAssertResult(GrBackendTextures::GetVkImageInfo(origBackendTex, &imageInfo));
64 
65     {
66         sk_sp<GrTexture> tex = gpu->wrapBackendTexture(origBackendTex, kBorrow_GrWrapOwnership,
67                                                        GrWrapCacheable::kNo, kRead_GrIOType);
68         REPORTER_ASSERT(reporter, tex);
69     }
70 
71     // image is null
72     {
73         GrVkImageInfo backendCopy = imageInfo;
74         backendCopy.fImage = VK_NULL_HANDLE;
75         GrBackendTexture backendTex = GrBackendTextures::MakeVk(kW, kH, backendCopy);
76         sk_sp<GrTexture> tex = gpu->wrapBackendTexture(
77                 backendTex, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType);
78         REPORTER_ASSERT(reporter, !tex);
79         tex = gpu->wrapBackendTexture(
80                 backendTex, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType);
81         REPORTER_ASSERT(reporter, !tex);
82     }
83 
84     // alloc is null
85     {
86         GrVkImageInfo backendCopy = imageInfo;
87         backendCopy.fAlloc = skgpu::VulkanAlloc();
88         GrBackendTexture backendTex = GrBackendTextures::MakeVk(kW, kH, backendCopy);
89         sk_sp<GrTexture> tex = gpu->wrapBackendTexture(
90                 backendTex, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType);
91         REPORTER_ASSERT(reporter, tex);
92         tex = gpu->wrapBackendTexture(
93                 backendTex, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType);
94         REPORTER_ASSERT(reporter, !tex);
95     }
96 
97     // check adopt creation
98     {
99         GrVkImageInfo backendCopy = imageInfo;
100         GrBackendTexture backendTex = GrBackendTextures::MakeVk(kW, kH, backendCopy);
101         sk_sp<GrTexture> tex = gpu->wrapBackendTexture(
102                 backendTex, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType);
103 
104         REPORTER_ASSERT(reporter, tex);
105         if (tex) {
106             mbet->wasAdopted();
107         }
108     }
109 }
110 
wrap_rt_test(skiatest::Reporter * reporter,GrDirectContext * dContext)111 void wrap_rt_test(skiatest::Reporter* reporter, GrDirectContext* dContext) {
112     using namespace skgpu;
113 
114     GrGpu* gpu = dContext->priv().getGpu();
115     GrColorType ct = SkColorTypeToGrColorType(kColorType);
116 
117     Protected isProtected = Protected(dContext->priv().caps()->supportsProtectedContent());
118 
119     for (int sampleCnt : {1, 4}) {
120         GrBackendFormat format = gpu->caps()->getDefaultBackendFormat(ct, GrRenderable::kYes);
121         if (sampleCnt > gpu->caps()->maxRenderTargetSampleCount(format)) {
122             continue;
123         }
124 
125         GrBackendRenderTarget origBackendRT =
126                 gpu->createTestingOnlyBackendRenderTarget({kW, kH}, ct, sampleCnt, isProtected);
127         if (!origBackendRT.isValid()) {
128             ERRORF(reporter, "Could not create backend render target.");
129         }
130 
131         GrVkImageInfo imageInfo;
132         REPORTER_ASSERT(reporter,
133                         GrBackendRenderTargets::GetVkImageInfo(origBackendRT, &imageInfo));
134 
135         sk_sp<GrRenderTarget> rt = gpu->wrapBackendRenderTarget(origBackendRT);
136         REPORTER_ASSERT(reporter, rt);
137 
138         // image is null
139         {
140             GrVkImageInfo backendCopy = imageInfo;
141             backendCopy.fImage = VK_NULL_HANDLE;
142             GrBackendRenderTarget backendRT = GrBackendRenderTargets::MakeVk(kW, kH, backendCopy);
143             rt = gpu->wrapBackendRenderTarget(backendRT);
144             REPORTER_ASSERT(reporter, !rt);
145         }
146 
147         // alloc is null
148         {
149             GrVkImageInfo backendCopy = imageInfo;
150             backendCopy.fAlloc = VulkanAlloc();
151             // can wrap null alloc
152             GrBackendRenderTarget backendRT = GrBackendRenderTargets::MakeVk(kW, kH, backendCopy);
153             rt = gpu->wrapBackendRenderTarget(backendRT);
154             REPORTER_ASSERT(reporter, rt);
155         }
156 
157         gpu->deleteTestingOnlyBackendRenderTarget(origBackendRT);
158     }
159 }
160 
wrap_trt_test(skiatest::Reporter * reporter,GrDirectContext * dContext)161 void wrap_trt_test(skiatest::Reporter* reporter, GrDirectContext* dContext) {
162     using namespace skgpu;
163 
164     GrGpu* gpu = dContext->priv().getGpu();
165 
166     Protected isProtected = Protected(dContext->priv().caps()->supportsProtectedContent());
167 
168     auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData(
169             dContext, kW, kH, kRGBA_8888_SkColorType, Mipmapped::kNo, GrRenderable::kYes,
170             isProtected);
171     if (!mbet) {
172         ERRORF(reporter, "Could not create renderable backend texture.");
173         return;
174     }
175     GrBackendTexture origBackendTex = mbet->texture();
176 
177     GrVkImageInfo imageInfo;
178     SkAssertResult(GrBackendTextures::GetVkImageInfo(origBackendTex, &imageInfo));
179 
180     sk_sp<GrTexture> tex = gpu->wrapRenderableBackendTexture(
181             origBackendTex, 1, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo);
182     REPORTER_ASSERT(reporter, tex);
183 
184     // image is null
185     {
186         GrVkImageInfo backendCopy = imageInfo;
187         backendCopy.fImage = VK_NULL_HANDLE;
188         GrBackendTexture backendTex = GrBackendTextures::MakeVk(kW, kH, backendCopy);
189         tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kBorrow_GrWrapOwnership,
190                                                 GrWrapCacheable::kNo);
191         REPORTER_ASSERT(reporter, !tex);
192         tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership,
193                                                 GrWrapCacheable::kNo);
194         REPORTER_ASSERT(reporter, !tex);
195     }
196 
197     // alloc is null
198     {
199         GrVkImageInfo backendCopy = imageInfo;
200         backendCopy.fAlloc = VulkanAlloc();
201         GrBackendTexture backendTex = GrBackendTextures::MakeVk(kW, kH, backendCopy);
202         tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kBorrow_GrWrapOwnership,
203                                                 GrWrapCacheable::kNo);
204         REPORTER_ASSERT(reporter, tex);
205         tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership,
206                                                 GrWrapCacheable::kNo);
207         REPORTER_ASSERT(reporter, !tex);
208     }
209 
210     // check rendering with MSAA
211     {
212         int maxSamples = dContext->priv().caps()->maxRenderTargetSampleCount(
213                 origBackendTex.getBackendFormat());
214         bool shouldSucceed = maxSamples > 1;
215         tex = gpu->wrapRenderableBackendTexture(origBackendTex, 2, kBorrow_GrWrapOwnership,
216                                                 GrWrapCacheable::kNo);
217         REPORTER_ASSERT(reporter, SkToBool(tex) == shouldSucceed);
218     }
219 
220     // check adopt creation
221     {
222         GrVkImageInfo backendCopy = imageInfo;
223         GrBackendTexture backendTex = GrBackendTextures::MakeVk(kW, kH, backendCopy);
224         tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership,
225                                                 GrWrapCacheable::kNo);
226         REPORTER_ASSERT(reporter, tex);
227         if (tex) {
228             mbet->wasAdopted();
229         }
230     }
231 }
232 
DEF_GANESH_TEST_FOR_VULKAN_CONTEXT(VkWrapTests,reporter,ctxInfo,CtsEnforcement::kNever)233 DEF_GANESH_TEST_FOR_VULKAN_CONTEXT(VkWrapTests, reporter, ctxInfo, CtsEnforcement::kNever) {
234     auto dContext = ctxInfo.directContext();
235 
236     wrap_tex_test(reporter, dContext);
237     wrap_rt_test(reporter, dContext);
238     wrap_trt_test(reporter, dContext);
239 }
240 
241 #endif
242