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