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