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