1 /*
2 * Copyright 2018 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 #include "include/core/SkAlphaType.h"
14 #include "include/core/SkColorSpace.h"
15 #include "include/core/SkColorType.h"
16 #include "include/core/SkImage.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/gpu/GpuTypes.h"
19 #include "include/gpu/GrBackendSurface.h"
20 #include "include/gpu/GrDirectContext.h"
21 #include "include/gpu/GrTypes.h"
22 #include "include/gpu/vk/GrVkTypes.h"
23 #include "include/private/gpu/ganesh/GrTypesPriv.h"
24 #include "src/gpu/ganesh/GrDirectContextPriv.h"
25 #include "src/gpu/ganesh/GrSurface.h"
26 #include "src/gpu/ganesh/GrSurfaceProxy.h"
27 #include "src/gpu/ganesh/GrTextureProxy.h"
28 #include "src/gpu/ganesh/vk/GrVkCaps.h"
29 #include "src/gpu/ganesh/vk/GrVkImage.h"
30 #include "src/gpu/ganesh/vk/GrVkTexture.h"
31 #include "src/image/SkImage_Base.h"
32 #include "tests/CtsEnforcement.h"
33 #include "tests/Test.h"
34 #include "tools/gpu/ManagedBackendTexture.h"
35 #include "tools/gpu/ProxyUtils.h"
36
37 #include <vulkan/vulkan_core.h>
38
39 class GrTexture;
40 struct GrContextOptions;
41
DEF_GANESH_TEST_FOR_VULKAN_CONTEXT(VkDRMModifierTest,reporter,ctxInfo,CtsEnforcement::kNever)42 DEF_GANESH_TEST_FOR_VULKAN_CONTEXT(VkDRMModifierTest, reporter, ctxInfo, CtsEnforcement::kNever) {
43 auto dContext = ctxInfo.directContext();
44
45 const GrVkCaps* vkCaps = static_cast<const GrVkCaps*>(dContext->priv().caps());
46 if (!vkCaps->supportsDRMFormatModifiers()) {
47 return;
48 }
49
50 // First make a normal backend texture with DRM
51 auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData(
52 dContext, 1, 1, kRGBA_8888_SkColorType, GrMipmapped::kNo, GrRenderable::kNo);
53 if (!mbet) {
54 ERRORF(reporter, "Could not create backend texture.");
55 return;
56 }
57
58 GrVkImageInfo info;
59 REPORTER_ASSERT(reporter, mbet->texture().getVkImageInfo(&info));
60
61 // Next we will use the same VkImageInfo but lie to say tiling is a DRM modifier. This should
62 // cause us to think the resource is eternal/read only internally. Though since we don't
63 // explicitly pass in the tiling to anything, this shouldn't cause us to do anything illegal.
64 info.fImageTiling = VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT;
65
66 GrBackendTexture drmBETex = GrBackendTexture(1, 1, info);
67 GrBackendFormat drmFormat = GrBackendFormat::MakeVk(info.fFormat, true);
68 REPORTER_ASSERT(reporter, drmFormat == drmBETex.getBackendFormat());
69 REPORTER_ASSERT(reporter, drmBETex.textureType() == GrTextureType::kExternal);
70
71 // Now wrap the texture in an SkImage and make sure we have the required read only properties
72 sk_sp<SkImage> drmImage = SkImage::MakeFromTexture(dContext,
73 drmBETex,
74 kTopLeft_GrSurfaceOrigin,
75 kRGBA_8888_SkColorType,
76 kPremul_SkAlphaType,
77 nullptr);
78 REPORTER_ASSERT(reporter, drmImage);
79
80 REPORTER_ASSERT(reporter,
81 GrBackendTexture::TestingOnly_Equals(drmImage->getBackendTexture(false), drmBETex));
82
83 auto[view, _] = as_IB(drmImage.get()) -> asView(dContext, GrMipmapped::kNo);
84 REPORTER_ASSERT(reporter, view);
85 const GrSurfaceProxy* proxy = view.proxy();
86 REPORTER_ASSERT(reporter, proxy);
87
88 REPORTER_ASSERT(reporter, proxy->readOnly());
89
90 const GrSurface* surf = proxy->peekSurface();
91 REPORTER_ASSERT(reporter, surf);
92 REPORTER_ASSERT(reporter, surf->readOnly());
93
94 drmImage.reset();
95 }
96
DEF_GANESH_TEST_FOR_VULKAN_CONTEXT(VkImageLayoutTest,reporter,ctxInfo,CtsEnforcement::kNever)97 DEF_GANESH_TEST_FOR_VULKAN_CONTEXT(VkImageLayoutTest, reporter, ctxInfo, CtsEnforcement::kNever) {
98 auto dContext = ctxInfo.directContext();
99
100 auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData(
101 dContext, 1, 1, kRGBA_8888_SkColorType, GrMipmapped::kNo, GrRenderable::kNo);
102 if (!mbet) {
103 ERRORF(reporter, "Could not create backend texture.");
104 return;
105 }
106
107 GrVkImageInfo info;
108 REPORTER_ASSERT(reporter, mbet->texture().getVkImageInfo(&info));
109 VkImageLayout initLayout = info.fImageLayout;
110
111 // Verify that setting that layout via a copy of a backendTexture is reflected in all the
112 // backendTextures.
113 GrBackendTexture backendTex1 = mbet->texture();
114 GrBackendTexture backendTex2 = backendTex1;
115 REPORTER_ASSERT(reporter, backendTex2.getVkImageInfo(&info));
116 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
117
118 backendTex2.setVkImageLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
119
120 REPORTER_ASSERT(reporter, backendTex1.getVkImageInfo(&info));
121 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout);
122
123 REPORTER_ASSERT(reporter, backendTex2.getVkImageInfo(&info));
124 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout);
125
126 // Setting back the layout since we didn't actually change it
127 backendTex1.setVkImageLayout(initLayout);
128
129 sk_sp<SkImage> wrappedImage = SkImage::MakeFromTexture(
130 dContext,
131 backendTex1,
132 kTopLeft_GrSurfaceOrigin,
133 kRGBA_8888_SkColorType,
134 kPremul_SkAlphaType,
135 /*color space*/ nullptr,
136 sk_gpu_test::ManagedBackendTexture::ReleaseProc,
137 mbet->releaseContext());
138 REPORTER_ASSERT(reporter, wrappedImage.get());
139
140 GrSurfaceProxy* proxy = sk_gpu_test::GetTextureImageProxy(wrappedImage.get(), dContext);
141 REPORTER_ASSERT(reporter, proxy);
142 REPORTER_ASSERT(reporter, proxy->isInstantiated());
143 GrTexture* texture = proxy->peekTexture();
144 REPORTER_ASSERT(reporter, texture);
145
146 // Verify that modifying the layout via the GrVkTexture is reflected in the GrBackendTexture
147 GrVkImage* vkTexture = static_cast<GrVkTexture*>(texture)->textureImage();
148 REPORTER_ASSERT(reporter, initLayout == vkTexture->currentLayout());
149 vkTexture->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
150
151 REPORTER_ASSERT(reporter, backendTex1.getVkImageInfo(&info));
152 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == info.fImageLayout);
153
154 GrBackendTexture backendTexImage = wrappedImage->getBackendTexture(false);
155 REPORTER_ASSERT(reporter, backendTexImage.getVkImageInfo(&info));
156 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == info.fImageLayout);
157
158 // Verify that modifying the layout via the GrBackendTexutre is reflected in the GrVkTexture
159 backendTexImage.setVkImageLayout(VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
160 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL == vkTexture->currentLayout());
161
162 vkTexture->updateImageLayout(initLayout);
163
164 REPORTER_ASSERT(reporter, backendTex1.getVkImageInfo(&info));
165 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
166
167 REPORTER_ASSERT(reporter, backendTex2.getVkImageInfo(&info));
168 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
169
170 REPORTER_ASSERT(reporter, backendTexImage.getVkImageInfo(&info));
171 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
172
173 // Check that we can do things like assigning the backend texture to invalid one, assign an
174 // invalid one, assin a backend texture to inself etc. Success here is that we don't hit any of
175 // our ref counting asserts.
176 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(backendTex1, backendTex2));
177
178 GrBackendTexture invalidTexture;
179 REPORTER_ASSERT(reporter, !invalidTexture.isValid());
180 REPORTER_ASSERT(reporter, !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTex2));
181
182 backendTex2 = invalidTexture;
183 REPORTER_ASSERT(reporter, !backendTex2.isValid());
184 REPORTER_ASSERT(reporter, !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTex2));
185
186 invalidTexture = backendTex1;
187 REPORTER_ASSERT(reporter, invalidTexture.isValid());
188 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTex1));
189
190 invalidTexture = static_cast<decltype(invalidTexture)&>(invalidTexture);
191 REPORTER_ASSERT(reporter, invalidTexture.isValid());
192 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, invalidTexture));
193 }
194
195 // This test is disabled because it executes illegal vulkan calls which cause the validations layers
196 // to fail and makes us assert. Once fixed to use a valid vulkan call sequence it should be
197 // renenabled, see skbug.com/8936.
198 #if 0
199 // Test to make sure we transition from the EXTERNAL queue even when no layout transition is needed.
200 DEF_GANESH_TEST_FOR_VULKAN_CONTEXT(VkTransitionExternalQueueTest, reporter, ctxInfo,
201 CtsEnforcement::kApiLevel_T) {
202 auto dContext = ctxInfo.directContext();
203 GrGpu* gpu = dContext->priv().getGpu();
204 GrVkGpu* vkGpu = static_cast<GrVkGpu*>(gpu);
205 if (!vkGpu->vkCaps().supportsExternalMemory()) {
206 return;
207 }
208
209 GrBackendTexture backendTex = dContext->createBackendTexture(
210 1, 1, kRGBA_8888_SkColorType,
211 SkColors::kTransparent, GrMipmapped::kNo, GrRenderable::kNo);
212 sk_sp<SkImage> image;
213 // Make a backend texture with an external queue family and general layout.
214 GrVkImageInfo vkInfo;
215 if (!backendTex.getVkImageInfo(&vkInfo)) {
216 return;
217 }
218 vkInfo.fCurrentQueueFamily = VK_QUEUE_FAMILY_EXTERNAL;
219 // Use a read-only layout as these are the ones where we can otherwise skip a transition.
220 vkInfo.fImageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
221
222 GrBackendTexture vkExtTex(1, 1, vkInfo);
223 REPORTER_ASSERT(reporter, vkExtTex.isValid());
224 image = SkImage::MakeFromTexture(dContext, vkExtTex, kTopLeft_GrSurfaceOrigin,
225 kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr, nullptr,
226 nullptr);
227
228 if (!image) {
229 return;
230 }
231
232 GrTexture* texture = image->getTexture();
233 REPORTER_ASSERT(reporter, texture);
234 GrVkTexture* vkTex = static_cast<GrVkTexture*>(texture);
235
236 // Change our backend texture to the internal queue, with the same layout. This should force a
237 // queue transition even though the layouts match.
238 vkTex->setImageLayout(vkGpu, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, 0,
239 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, false, false);
240
241 // Get our image info again and make sure we transitioned queues.
242 GrBackendTexture newBackendTexture = image->getBackendTexture(true);
243 GrVkImageInfo newVkInfo;
244 REPORTER_ASSERT(reporter, newBackendTexture.getVkImageInfo(&newVkInfo));
245 REPORTER_ASSERT(reporter, newVkInfo.fCurrentQueueFamily == vkGpu->queueIndex());
246
247 image.reset();
248 dContext->submit(true);
249 dContext->deleteBackendTexture(backendTex);
250 }
251 #endif
252
253 #endif
254