blob: db14d51cc967adf50da198fbaa167c7816b74ead [file] [log] [blame]
Greg Daniel52e16d92018-04-10 09:34:07 -04001/*
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 intializers to work
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkTypes.h"
Greg Daniel52e16d92018-04-10 09:34:07 -040011
Brian Osmanc7ad40f2018-05-31 14:27:17 -040012#if defined(SK_VULKAN)
Greg Daniel52e16d92018-04-10 09:34:07 -040013
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/gpu/vk/GrVkVulkan.h"
Greg Daniel54bfb182018-11-20 17:12:36 -050015
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "tests/Test.h"
Greg Daniel52e16d92018-04-10 09:34:07 -040017
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/core/SkImage.h"
19#include "include/gpu/GrBackendSurface.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/gpu/vk/GrVkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrContextPriv.h"
Greg Daniel797efca2019-05-09 14:04:20 -040022#include "src/gpu/GrRenderTargetContext.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000023#include "src/gpu/GrTexture.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040024#include "src/gpu/GrTextureProxy.h"
Greg Daniel797efca2019-05-09 14:04:20 -040025#include "src/gpu/SkGpuDevice.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/vk/GrVkGpu.h"
27#include "src/gpu/vk/GrVkImageLayout.h"
28#include "src/gpu/vk/GrVkTexture.h"
29#include "src/image/SkImage_Base.h"
Greg Daniel7c902112020-03-06 13:07:10 -050030#include "src/image/SkImage_GpuBase.h"
Greg Daniel797efca2019-05-09 14:04:20 -040031#include "src/image/SkSurface_Gpu.h"
Greg Daniel52e16d92018-04-10 09:34:07 -040032
33DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkImageLayoutTest, reporter, ctxInfo) {
34 GrContext* context = ctxInfo.grContext();
Greg Daniel52e16d92018-04-10 09:34:07 -040035
Robert Phillips4bdd36f2019-06-04 11:03:06 -040036 GrBackendTexture backendTex = context->createBackendTexture(1, 1,
37 kRGBA_8888_SkColorType,
38 SkColors::kTransparent,
39 GrMipMapped::kNo,
Robert Phillipsda2e67a2019-07-01 15:04:06 -040040 GrRenderable::kNo,
41 GrProtected::kNo);
Greg Daniel52e16d92018-04-10 09:34:07 -040042 REPORTER_ASSERT(reporter, backendTex.isValid());
43
44 GrVkImageInfo info;
45 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
46 VkImageLayout initLayout = info.fImageLayout;
47
48 // Verify that setting that layout via a copy of a backendTexture is reflected in all the
49 // backendTextures.
50 GrBackendTexture backendTexCopy = backendTex;
51 REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info));
52 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
53
54 backendTexCopy.setVkImageLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
55
56 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
57 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout);
58
59 REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info));
60 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout);
61
62 // Setting back the layout since we didn't actually change it
63 backendTex.setVkImageLayout(initLayout);
64
65 sk_sp<SkImage> wrappedImage = SkImage::MakeFromTexture(context, backendTex,
66 kTopLeft_GrSurfaceOrigin,
67 kRGBA_8888_SkColorType,
68 kPremul_SkAlphaType, nullptr);
69 REPORTER_ASSERT(reporter, wrappedImage.get());
70
Greg Danielfebdedf2020-02-05 17:06:27 -050071 const GrSurfaceProxyView* view = as_IB(wrappedImage)->view(context);
72 REPORTER_ASSERT(reporter, view);
73 REPORTER_ASSERT(reporter, view->proxy()->isInstantiated());
74 GrTexture* texture = view->proxy()->peekTexture();
Greg Daniel52e16d92018-04-10 09:34:07 -040075 REPORTER_ASSERT(reporter, texture);
76
77 // Verify that modifying the layout via the GrVkTexture is reflected in the GrBackendTexture
78 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
79 REPORTER_ASSERT(reporter, initLayout == vkTexture->currentLayout());
80 vkTexture->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
81
82 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
83 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == info.fImageLayout);
84
85 GrBackendTexture backendTexImage = wrappedImage->getBackendTexture(false);
86 REPORTER_ASSERT(reporter, backendTexImage.getVkImageInfo(&info));
87 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == info.fImageLayout);
88
89 // Verify that modifying the layout via the GrBackendTexutre is reflected in the GrVkTexture
90 backendTexImage.setVkImageLayout(VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
91 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL == vkTexture->currentLayout());
92
Greg Daniel52e16d92018-04-10 09:34:07 -040093 vkTexture->updateImageLayout(initLayout);
94
95 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
96 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
97
98 REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info));
99 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
100
101 REPORTER_ASSERT(reporter, backendTexImage.getVkImageInfo(&info));
102 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
103
104 // Check that we can do things like assigning the backend texture to invalid one, assign an
105 // invalid one, assin a backend texture to inself etc. Success here is that we don't hit any of
106 // our ref counting asserts.
107 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(backendTex, backendTexCopy));
108
109 GrBackendTexture invalidTexture;
110 REPORTER_ASSERT(reporter, !invalidTexture.isValid());
111 REPORTER_ASSERT(reporter, !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTexCopy));
112
113 backendTexCopy = invalidTexture;
114 REPORTER_ASSERT(reporter, !backendTexCopy.isValid());
115 REPORTER_ASSERT(reporter, !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTexCopy));
116
117 invalidTexture = backendTex;
118 REPORTER_ASSERT(reporter, invalidTexture.isValid());
119 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTex));
120
Ben Wagnerff134f22018-04-24 16:29:16 -0400121 invalidTexture = static_cast<decltype(invalidTexture)&>(invalidTexture);
Greg Daniel52e16d92018-04-10 09:34:07 -0400122 REPORTER_ASSERT(reporter, invalidTexture.isValid());
123 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, invalidTexture));
124
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400125 context->deleteBackendTexture(backendTex);
Greg Daniel52e16d92018-04-10 09:34:07 -0400126}
127
Greg Daniel59dc1482019-02-22 10:46:38 -0500128static void testing_release_proc(void* ctx) {
129 int* count = (int*)ctx;
130 *count += 1;
131}
132
133// Test to make sure we don't call our release proc on an image until we've transferred it back to
134// its original queue family.
135DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkReleaseExternalQueueTest, reporter, ctxInfo) {
136 GrContext* context = ctxInfo.grContext();
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400137 GrGpu* gpu = context->priv().getGpu();
138 GrVkGpu* vkGpu = static_cast<GrVkGpu*>(gpu);
139 if (!vkGpu->vkCaps().supportsExternalMemory()) {
Greg Daniel59dc1482019-02-22 10:46:38 -0500140 return;
141 }
142
143 for (bool useExternal : {false, true}) {
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400144 GrBackendTexture backendTex = context->createBackendTexture(1, 1,
145 kRGBA_8888_SkColorType,
146 SkColors::kTransparent,
147 GrMipMapped::kNo,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400148 GrRenderable::kNo,
149 GrProtected::kNo);
Greg Daniel59dc1482019-02-22 10:46:38 -0500150 sk_sp<SkImage> image;
151 int count = 0;
152 if (useExternal) {
153 // Make a backend texture with an external queue family;
154 GrVkImageInfo vkInfo;
155 if (!backendTex.getVkImageInfo(&vkInfo)) {
156 return;
157 }
158 vkInfo.fCurrentQueueFamily = VK_QUEUE_FAMILY_EXTERNAL;
159
160 GrBackendTexture vkExtTex(1, 1, vkInfo);
161 REPORTER_ASSERT(reporter, vkExtTex.isValid());
162 image = SkImage::MakeFromTexture(context, vkExtTex,
163 kTopLeft_GrSurfaceOrigin,
164 kRGBA_8888_SkColorType,
165 kPremul_SkAlphaType,
166 nullptr, testing_release_proc,
167 (void*)&count);
168
169 } else {
170 image = SkImage::MakeFromTexture(context, backendTex,
171 kTopLeft_GrSurfaceOrigin,
172 kRGBA_8888_SkColorType,
173 kPremul_SkAlphaType,
174 nullptr, testing_release_proc,
175 (void*)&count);
176 }
177
178 if (!image) {
179 continue;
180 }
181
182 REPORTER_ASSERT(reporter, !count);
183
Greg Daniel7c902112020-03-06 13:07:10 -0500184 SkImage_GpuBase* gpuImage = static_cast<SkImage_GpuBase*>(as_IB(image));
185 GrTexture* texture = gpuImage->getTexture();
Greg Daniel59dc1482019-02-22 10:46:38 -0500186 REPORTER_ASSERT(reporter, texture);
187 GrVkTexture* vkTex = static_cast<GrVkTexture*>(texture);
188
189 if (useExternal) {
190 // Testing helper so we claim that we don't need to transition from our fake external
191 // queue first.
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400192 vkTex->setCurrentQueueFamilyToGraphicsQueue(vkGpu);
Greg Daniel59dc1482019-02-22 10:46:38 -0500193 }
194
195 image.reset();
196
197 // Resetting the image should only trigger the release proc if we are not using an external
198 // queue. When using an external queue when we free the SkImage and the underlying
199 // GrTexture, we submit a queue transition on the command buffer.
200 if (useExternal) {
201 REPORTER_ASSERT(reporter, !count);
202 } else {
203 REPORTER_ASSERT(reporter, count == 1);
204 }
205
206 gpu->testingOnly_flushGpuAndSync();
207
208 // Now that we flushed and waited the release proc should have be triggered.
209 REPORTER_ASSERT(reporter, count == 1);
210
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400211 context->deleteBackendTexture(backendTex);
Greg Daniel59dc1482019-02-22 10:46:38 -0500212 }
213}
214
Greg Daniel797efca2019-05-09 14:04:20 -0400215// Test to make sure we transition to the original queue when requests for prepareforexternalio are
216// in flush calls
217DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkPrepareForExternalIOQueueTransitionTest, reporter, ctxInfo) {
218 GrContext* context = ctxInfo.grContext();
Robert Phillips9b16f812019-05-17 10:01:21 -0400219
220 GrVkGpu* vkGpu = static_cast<GrVkGpu*>(context->priv().getGpu());
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400221 if (!vkGpu->vkCaps().supportsExternalMemory()) {
Greg Daniel797efca2019-05-09 14:04:20 -0400222 return;
223 }
224
225 for (bool useSurface : {false, true}) {
226 for (bool preparePresent : {false, true}) {
227 if (!useSurface && preparePresent) {
228 // We don't set textures to present
229 continue;
230 }
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400231 GrBackendTexture backendTex = context->createBackendTexture(
Robert Phillips80626792019-06-04 07:16:10 -0400232 4, 4, kRGBA_8888_SkColorType,
233 SkColors::kTransparent, GrMipMapped::kNo,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400234 useSurface ? GrRenderable::kYes : GrRenderable::kNo,
235 GrProtected::kNo);
Greg Daniel797efca2019-05-09 14:04:20 -0400236
237 // Make a backend texture with an external queue family and general layout.
238 GrVkImageInfo vkInfo;
239 if (!backendTex.getVkImageInfo(&vkInfo)) {
240 return;
241 }
242
243 // We can't actually make an external texture in our test. However, we lie and say it is
244 // and then will manually go and swap the queue to the graphics queue once we wrap it.
245 if (preparePresent) {
246 // We don't transition to present to things that are going to external for foreign
247 // queues.
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400248 vkInfo.fCurrentQueueFamily = vkGpu->queueIndex();
Greg Daniel797efca2019-05-09 14:04:20 -0400249 } else {
250 vkInfo.fCurrentQueueFamily = VK_QUEUE_FAMILY_EXTERNAL;
251 }
252
253 GrBackendTexture vkExtTex(1, 1, vkInfo);
254
255 sk_sp<SkImage> image;
256 sk_sp<SkSurface> surface;
257 GrTexture* texture;
258 if (useSurface) {
259 surface = SkSurface::MakeFromBackendTexture(context, vkExtTex,
260 kTopLeft_GrSurfaceOrigin, 0, kRGBA_8888_SkColorType, nullptr, nullptr);
261 REPORTER_ASSERT(reporter, surface.get());
262 if (!surface) {
263 continue;
264 }
265 SkSurface_Gpu* gpuSurface = static_cast<SkSurface_Gpu*>(surface.get());
266 auto* rtc = gpuSurface->getDevice()->accessRenderTargetContext();
267 texture = rtc->asTextureProxy()->peekTexture();
268 } else {
269 image = SkImage::MakeFromTexture(context, vkExtTex, kTopLeft_GrSurfaceOrigin,
270 kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr, nullptr, nullptr);
271
272 REPORTER_ASSERT(reporter, image.get());
273 if (!image) {
274 continue;
275 }
276
Greg Daniel7c902112020-03-06 13:07:10 -0500277 SkImage_GpuBase* gpuImage = static_cast<SkImage_GpuBase*>(as_IB(image));
278 texture = gpuImage->getTexture();
Greg Daniel797efca2019-05-09 14:04:20 -0400279 }
280
281 REPORTER_ASSERT(reporter, texture);
282 GrVkTexture* vkTex = static_cast<GrVkTexture*>(texture);
283
284 // Testing helper so we claim that we don't need to transition from our fake external
285 // queue first.
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400286 vkTex->setCurrentQueueFamilyToGraphicsQueue(vkGpu);
Greg Daniel797efca2019-05-09 14:04:20 -0400287
288 GrBackendTexture newBackendTexture;
289 if (useSurface) {
290 newBackendTexture = surface->getBackendTexture(
291 SkSurface::kFlushRead_TextureHandleAccess);
292 } else {
293 newBackendTexture = image->getBackendTexture(false);
294 }
295 GrVkImageInfo newVkInfo;
296 REPORTER_ASSERT(reporter, newBackendTexture.getVkImageInfo(&newVkInfo));
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400297 REPORTER_ASSERT(reporter, newVkInfo.fCurrentQueueFamily == vkGpu->queueIndex());
Greg Daniel797efca2019-05-09 14:04:20 -0400298 VkImageLayout oldLayout = newVkInfo.fImageLayout;
299
300 GrPrepareForExternalIORequests externalRequests;
301 SkImage* imagePtr;
302 SkSurface* surfacePtr;
303 if (useSurface) {
304 externalRequests.fNumSurfaces = 1;
305 surfacePtr = surface.get();
306 externalRequests.fSurfaces = &surfacePtr;
307 externalRequests.fPrepareSurfaceForPresent = &preparePresent;
308 } else {
309 externalRequests.fNumImages = 1;
310 imagePtr = image.get();
311 externalRequests.fImages = &imagePtr;
312
313 }
314 context->flush(GrFlushInfo(), externalRequests);
315
316 if (useSurface) {
317 newBackendTexture = surface->getBackendTexture(
318 SkSurface::kFlushRead_TextureHandleAccess);
319 } else {
320 newBackendTexture = image->getBackendTexture(false);
321 }
322 REPORTER_ASSERT(reporter, newBackendTexture.getVkImageInfo(&newVkInfo));
323 if (preparePresent) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400324 REPORTER_ASSERT(reporter, newVkInfo.fCurrentQueueFamily == vkGpu->queueIndex());
Greg Daniel797efca2019-05-09 14:04:20 -0400325 REPORTER_ASSERT(reporter,
326 newVkInfo.fImageLayout == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
327 } else {
328 REPORTER_ASSERT(reporter, newVkInfo.fCurrentQueueFamily == VK_QUEUE_FAMILY_EXTERNAL);
329 REPORTER_ASSERT(reporter, newVkInfo.fImageLayout == oldLayout);
330 }
331
332 GrFlushInfo flushInfo;
333 flushInfo.fFlags = kSyncCpu_GrFlushFlag;
334 context->flush(flushInfo);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400335 context->deleteBackendTexture(backendTex);
Greg Daniel797efca2019-05-09 14:04:20 -0400336 }
337 }
338}
339
Greg Danieleed7d632019-06-26 13:48:12 -0400340// This test is disabled because it executes illegal vulkan calls which cause the validations layers
341// to fail and makes us assert. Once fixed to use a valid vulkan call sequence it should be
342// renenabled, see skbug.com/8936.
343#if 0
Eric Karl3f219cb2019-03-22 17:46:55 -0700344// Test to make sure we transition from the EXTERNAL queue even when no layout transition is needed.
345DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkTransitionExternalQueueTest, reporter, ctxInfo) {
346 GrContext* context = ctxInfo.grContext();
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400347 GrGpu* gpu = context->priv().getGpu();
348 GrVkGpu* vkGpu = static_cast<GrVkGpu*>(gpu);
349 if (!vkGpu->vkCaps().supportsExternalMemory()) {
Eric Karl3f219cb2019-03-22 17:46:55 -0700350 return;
351 }
352
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400353 GrBackendTexture backendTex = context->createBackendTexture(
Robert Phillips80626792019-06-04 07:16:10 -0400354 1, 1, kRGBA_8888_SkColorType,
355 SkColors::kTransparent, GrMipMapped::kNo, GrRenderable::kNo);
Eric Karl3f219cb2019-03-22 17:46:55 -0700356 sk_sp<SkImage> image;
357 // Make a backend texture with an external queue family and general layout.
358 GrVkImageInfo vkInfo;
359 if (!backendTex.getVkImageInfo(&vkInfo)) {
360 return;
361 }
362 vkInfo.fCurrentQueueFamily = VK_QUEUE_FAMILY_EXTERNAL;
363 // Use a read-only layout as these are the ones where we can otherwise skip a transition.
364 vkInfo.fImageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
365
366 GrBackendTexture vkExtTex(1, 1, vkInfo);
367 REPORTER_ASSERT(reporter, vkExtTex.isValid());
368 image = SkImage::MakeFromTexture(context, vkExtTex, kTopLeft_GrSurfaceOrigin,
369 kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr, nullptr,
370 nullptr);
371
372 if (!image) {
373 return;
374 }
375
376 GrTexture* texture = image->getTexture();
377 REPORTER_ASSERT(reporter, texture);
378 GrVkTexture* vkTex = static_cast<GrVkTexture*>(texture);
379
380 // Change our backend texture to the internal queue, with the same layout. This should force a
381 // queue transition even though the layouts match.
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400382 vkTex->setImageLayout(vkGpu, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, 0,
Eric Karl3f219cb2019-03-22 17:46:55 -0700383 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, false, false);
384
385 // Get our image info again and make sure we transitioned queues.
386 GrBackendTexture newBackendTexture = image->getBackendTexture(true);
387 GrVkImageInfo newVkInfo;
388 REPORTER_ASSERT(reporter, newBackendTexture.getVkImageInfo(&newVkInfo));
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400389 REPORTER_ASSERT(reporter, newVkInfo.fCurrentQueueFamily == vkGpu->queueIndex());
Eric Karl3f219cb2019-03-22 17:46:55 -0700390
391 image.reset();
392 gpu->testingOnly_flushGpuAndSync();
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400393 context->deleteBackendTexture(backendTex);
Eric Karl3f219cb2019-03-22 17:46:55 -0700394}
Greg Danieleed7d632019-06-26 13:48:12 -0400395#endif
Eric Karl3f219cb2019-03-22 17:46:55 -0700396
Greg Daniel52e16d92018-04-10 09:34:07 -0400397#endif