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 "SkTypes.h"
11
12 #if defined(SK_VULKAN)
13
14 #include "vk/GrVkVulkan.h"
15
16 #include "GrBackendSurface.h"
17 #include "GrContextPriv.h"
18 #include "GrContextFactory.h"
19 #include "GrRenderTarget.h"
20 #include "GrTexture.h"
21
22 #include "Test.h"
23 #include "vk/GrVkCaps.h"
24 #include "vk/GrVkGpu.h"
25 #include "vk/GrVkMemory.h"
26 #include "vk/GrVkTypes.h"
27
28 using sk_gpu_test::GrContextFactory;
29
30 const int kW = 1024;
31 const int kH = 1024;
32 const GrPixelConfig kPixelConfig = kRGBA_8888_GrPixelConfig;
33 const GrColorType kColorType = GrColorType::kRGBA_8888;
34
wrap_tex_test(skiatest::Reporter * reporter,GrContext * context)35 void wrap_tex_test(skiatest::Reporter* reporter, GrContext* context) {
36
37 GrVkGpu* gpu = static_cast<GrVkGpu*>(context->contextPriv().getGpu());
38
39 GrBackendTexture origBackendTex = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
40 kColorType, false,
41 GrMipMapped::kNo);
42 GrVkImageInfo imageInfo;
43 SkAssertResult(origBackendTex.getVkImageInfo(&imageInfo));
44
45 sk_sp<GrTexture> tex = gpu->wrapBackendTexture(origBackendTex, kBorrow_GrWrapOwnership,
46 GrWrapCacheable::kNo, kRead_GrIOType);
47 REPORTER_ASSERT(reporter, tex);
48
49 // image is null
50 {
51 GrVkImageInfo backendCopy = imageInfo;
52 backendCopy.fImage = VK_NULL_HANDLE;
53 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
54 backendTex.setPixelConfig(kPixelConfig);
55 tex = gpu->wrapBackendTexture(backendTex, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
56 kRead_GrIOType);
57 REPORTER_ASSERT(reporter, !tex);
58 tex = gpu->wrapBackendTexture(backendTex, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo,
59 kRead_GrIOType);
60 REPORTER_ASSERT(reporter, !tex);
61 }
62
63 // alloc is null
64 {
65 GrVkImageInfo backendCopy = imageInfo;
66 backendCopy.fAlloc = GrVkAlloc();
67 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
68 backendTex.setPixelConfig(kPixelConfig);
69 tex = gpu->wrapBackendTexture(backendTex, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
70 kRead_GrIOType);
71 REPORTER_ASSERT(reporter, !tex);
72 tex = gpu->wrapBackendTexture(backendTex, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo,
73 kRead_GrIOType);
74 REPORTER_ASSERT(reporter, !tex);
75 }
76
77 // check adopt creation
78 {
79 GrVkImageInfo backendCopy = imageInfo;
80 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
81 backendTex.setPixelConfig(kPixelConfig);
82 tex = gpu->wrapBackendTexture(backendTex, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo,
83 kRead_GrIOType);
84
85 REPORTER_ASSERT(reporter, tex);
86 }
87 }
88
wrap_rt_test(skiatest::Reporter * reporter,GrContext * context)89 void wrap_rt_test(skiatest::Reporter* reporter, GrContext* context) {
90 GrVkGpu* gpu = static_cast<GrVkGpu*>(context->contextPriv().getGpu());
91
92 GrBackendTexture origBackendTex = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
93 kColorType, true,
94 GrMipMapped::kNo);
95
96 GrVkImageInfo imageInfo;
97 SkAssertResult(origBackendTex.getVkImageInfo(&imageInfo));
98
99 GrBackendRenderTarget origBackendRT(kW, kH, 1, 0, imageInfo);
100 origBackendRT.setPixelConfig(kPixelConfig);
101
102 sk_sp<GrRenderTarget> rt = gpu->wrapBackendRenderTarget(origBackendRT);
103 REPORTER_ASSERT(reporter, rt);
104
105 // image is null
106 {
107 GrVkImageInfo backendCopy = imageInfo;
108 backendCopy.fImage = VK_NULL_HANDLE;
109 GrBackendRenderTarget backendRT(kW, kH, 1, 0, backendCopy);
110 backendRT.setPixelConfig(kPixelConfig);
111 rt = gpu->wrapBackendRenderTarget(backendRT);
112 REPORTER_ASSERT(reporter, !rt);
113 }
114
115 // alloc is null
116 {
117 GrVkImageInfo backendCopy = imageInfo;
118 backendCopy.fAlloc = GrVkAlloc();
119 // can wrap null alloc
120 GrBackendRenderTarget backendRT(kW, kH, 1, 0, backendCopy);
121 backendRT.setPixelConfig(kPixelConfig);
122 rt = gpu->wrapBackendRenderTarget(backendRT);
123 REPORTER_ASSERT(reporter, rt);
124 }
125
126 // When we wrapBackendRenderTarget it is always borrowed, so we must make sure to free the
127 // resource when we're done.
128 gpu->deleteTestingOnlyBackendTexture(origBackendTex);
129 }
130
wrap_trt_test(skiatest::Reporter * reporter,GrContext * context)131 void wrap_trt_test(skiatest::Reporter* reporter, GrContext* context) {
132 GrVkGpu* gpu = static_cast<GrVkGpu*>(context->contextPriv().getGpu());
133
134 GrBackendTexture origBackendTex = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
135 kColorType, true,
136 GrMipMapped::kNo);
137 GrVkImageInfo imageInfo;
138 SkAssertResult(origBackendTex.getVkImageInfo(&imageInfo));
139
140 sk_sp<GrTexture> tex = gpu->wrapRenderableBackendTexture(
141 origBackendTex, 1, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo);
142 REPORTER_ASSERT(reporter, tex);
143
144 // image is null
145 {
146 GrVkImageInfo backendCopy = imageInfo;
147 backendCopy.fImage = VK_NULL_HANDLE;
148 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
149 backendTex.setPixelConfig(kPixelConfig);
150 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kBorrow_GrWrapOwnership,
151 GrWrapCacheable::kNo);
152 REPORTER_ASSERT(reporter, !tex);
153 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership,
154 GrWrapCacheable::kNo);
155 REPORTER_ASSERT(reporter, !tex);
156 }
157
158 // alloc is null
159 {
160 GrVkImageInfo backendCopy = imageInfo;
161 backendCopy.fAlloc = GrVkAlloc();
162 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
163 backendTex.setPixelConfig(kPixelConfig);
164 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kBorrow_GrWrapOwnership,
165 GrWrapCacheable::kNo);
166 REPORTER_ASSERT(reporter, !tex);
167 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership,
168 GrWrapCacheable::kNo);
169 REPORTER_ASSERT(reporter, !tex);
170 }
171
172 // check adopt creation
173 {
174 GrVkImageInfo backendCopy = imageInfo;
175 GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
176 backendTex.setPixelConfig(kPixelConfig);
177 tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership,
178 GrWrapCacheable::kNo);
179 REPORTER_ASSERT(reporter, tex);
180 }
181 }
182
DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkWrapTests,reporter,ctxInfo)183 DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkWrapTests, reporter, ctxInfo) {
184 wrap_tex_test(reporter, ctxInfo.grContext());
185 wrap_rt_test(reporter, ctxInfo.grContext());
186 wrap_trt_test(reporter, ctxInfo.grContext());
187 }
188
189 #endif
190