blob: dc61c39a143f6eda67466e664d26a6221516b573 [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 Danielc1ad77c2020-05-06 11:40:03 -040017#include "tests/TestUtils.h"
Greg Daniel52e16d92018-04-10 09:34:07 -040018
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/core/SkImage.h"
20#include "include/gpu/GrBackendSurface.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040021#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/gpu/vk/GrVkTypes.h"
Greg Daniel797efca2019-05-09 14:04:20 -040023#include "src/gpu/GrRenderTargetContext.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000024#include "src/gpu/GrTexture.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040025#include "src/gpu/GrTextureProxy.h"
Greg Daniel797efca2019-05-09 14:04:20 -040026#include "src/gpu/SkGpuDevice.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/vk/GrVkGpu.h"
28#include "src/gpu/vk/GrVkImageLayout.h"
29#include "src/gpu/vk/GrVkTexture.h"
30#include "src/image/SkImage_Base.h"
Greg Daniel7c902112020-03-06 13:07:10 -050031#include "src/image/SkImage_GpuBase.h"
Greg Daniel797efca2019-05-09 14:04:20 -040032#include "src/image/SkSurface_Gpu.h"
Greg Daniel52e16d92018-04-10 09:34:07 -040033
34DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkImageLayoutTest, reporter, ctxInfo) {
Adlai Holler14dc7912020-08-11 15:48:49 +000035 auto dContext = ctxInfo.directContext();
Greg Daniel52e16d92018-04-10 09:34:07 -040036
Greg Danielc1ad77c2020-05-06 11:40:03 -040037 GrBackendTexture backendTex;
Adlai Holler14dc7912020-08-11 15:48:49 +000038 CreateBackendTexture(dContext, &backendTex, 1, 1, kRGBA_8888_SkColorType,
39 SkColors::kTransparent, GrMipmapped::kNo,
40 GrRenderable::kNo, GrProtected::kNo);
Greg Daniel52e16d92018-04-10 09:34:07 -040041 REPORTER_ASSERT(reporter, backendTex.isValid());
42
43 GrVkImageInfo info;
44 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
45 VkImageLayout initLayout = info.fImageLayout;
46
47 // Verify that setting that layout via a copy of a backendTexture is reflected in all the
48 // backendTextures.
49 GrBackendTexture backendTexCopy = backendTex;
50 REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info));
51 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
52
53 backendTexCopy.setVkImageLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
54
55 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
56 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout);
57
58 REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info));
59 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout);
60
61 // Setting back the layout since we didn't actually change it
62 backendTex.setVkImageLayout(initLayout);
63
Adlai Holler14dc7912020-08-11 15:48:49 +000064 sk_sp<SkImage> wrappedImage = SkImage::MakeFromTexture(dContext, backendTex,
Greg Daniel52e16d92018-04-10 09:34:07 -040065 kTopLeft_GrSurfaceOrigin,
66 kRGBA_8888_SkColorType,
67 kPremul_SkAlphaType, nullptr);
68 REPORTER_ASSERT(reporter, wrappedImage.get());
69
Adlai Holler14dc7912020-08-11 15:48:49 +000070 const GrSurfaceProxyView* view = as_IB(wrappedImage)->view(dContext);
Greg Danielfebdedf2020-02-05 17:06:27 -050071 REPORTER_ASSERT(reporter, view);
72 REPORTER_ASSERT(reporter, view->proxy()->isInstantiated());
73 GrTexture* texture = view->proxy()->peekTexture();
Greg Daniel52e16d92018-04-10 09:34:07 -040074 REPORTER_ASSERT(reporter, texture);
75
76 // Verify that modifying the layout via the GrVkTexture is reflected in the GrBackendTexture
77 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
78 REPORTER_ASSERT(reporter, initLayout == vkTexture->currentLayout());
79 vkTexture->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
80
81 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
82 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == info.fImageLayout);
83
84 GrBackendTexture backendTexImage = wrappedImage->getBackendTexture(false);
85 REPORTER_ASSERT(reporter, backendTexImage.getVkImageInfo(&info));
86 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == info.fImageLayout);
87
88 // Verify that modifying the layout via the GrBackendTexutre is reflected in the GrVkTexture
89 backendTexImage.setVkImageLayout(VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
90 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL == vkTexture->currentLayout());
91
Greg Daniel52e16d92018-04-10 09:34:07 -040092 vkTexture->updateImageLayout(initLayout);
93
94 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
95 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
96
97 REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info));
98 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
99
100 REPORTER_ASSERT(reporter, backendTexImage.getVkImageInfo(&info));
101 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
102
103 // Check that we can do things like assigning the backend texture to invalid one, assign an
104 // invalid one, assin a backend texture to inself etc. Success here is that we don't hit any of
105 // our ref counting asserts.
106 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(backendTex, backendTexCopy));
107
108 GrBackendTexture invalidTexture;
109 REPORTER_ASSERT(reporter, !invalidTexture.isValid());
110 REPORTER_ASSERT(reporter, !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTexCopy));
111
112 backendTexCopy = invalidTexture;
113 REPORTER_ASSERT(reporter, !backendTexCopy.isValid());
114 REPORTER_ASSERT(reporter, !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTexCopy));
115
116 invalidTexture = backendTex;
117 REPORTER_ASSERT(reporter, invalidTexture.isValid());
118 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTex));
119
Ben Wagnerff134f22018-04-24 16:29:16 -0400120 invalidTexture = static_cast<decltype(invalidTexture)&>(invalidTexture);
Greg Daniel52e16d92018-04-10 09:34:07 -0400121 REPORTER_ASSERT(reporter, invalidTexture.isValid());
122 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, invalidTexture));
123
Adlai Holler14dc7912020-08-11 15:48:49 +0000124 dContext->deleteBackendTexture(backendTex);
Greg Daniel52e16d92018-04-10 09:34:07 -0400125}
126
Greg Danieleed7d632019-06-26 13:48:12 -0400127// This test is disabled because it executes illegal vulkan calls which cause the validations layers
128// to fail and makes us assert. Once fixed to use a valid vulkan call sequence it should be
129// renenabled, see skbug.com/8936.
130#if 0
Eric Karl3f219cb2019-03-22 17:46:55 -0700131// Test to make sure we transition from the EXTERNAL queue even when no layout transition is needed.
132DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkTransitionExternalQueueTest, reporter, ctxInfo) {
Robert Phillips58adb342020-07-23 09:41:57 -0400133 auto dContext = ctxInfo.directContext();
134 GrGpu* gpu = dContext->priv().getGpu();
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400135 GrVkGpu* vkGpu = static_cast<GrVkGpu*>(gpu);
136 if (!vkGpu->vkCaps().supportsExternalMemory()) {
Eric Karl3f219cb2019-03-22 17:46:55 -0700137 return;
138 }
139
Robert Phillips58adb342020-07-23 09:41:57 -0400140 GrBackendTexture backendTex = dContext->createBackendTexture(
Robert Phillips80626792019-06-04 07:16:10 -0400141 1, 1, kRGBA_8888_SkColorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400142 SkColors::kTransparent, GrMipmapped::kNo, GrRenderable::kNo);
Eric Karl3f219cb2019-03-22 17:46:55 -0700143 sk_sp<SkImage> image;
144 // Make a backend texture with an external queue family and general layout.
145 GrVkImageInfo vkInfo;
146 if (!backendTex.getVkImageInfo(&vkInfo)) {
147 return;
148 }
149 vkInfo.fCurrentQueueFamily = VK_QUEUE_FAMILY_EXTERNAL;
150 // Use a read-only layout as these are the ones where we can otherwise skip a transition.
151 vkInfo.fImageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
152
153 GrBackendTexture vkExtTex(1, 1, vkInfo);
154 REPORTER_ASSERT(reporter, vkExtTex.isValid());
Robert Phillips58adb342020-07-23 09:41:57 -0400155 image = SkImage::MakeFromTexture(dContext, vkExtTex, kTopLeft_GrSurfaceOrigin,
Eric Karl3f219cb2019-03-22 17:46:55 -0700156 kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr, nullptr,
157 nullptr);
158
159 if (!image) {
160 return;
161 }
162
163 GrTexture* texture = image->getTexture();
164 REPORTER_ASSERT(reporter, texture);
165 GrVkTexture* vkTex = static_cast<GrVkTexture*>(texture);
166
167 // Change our backend texture to the internal queue, with the same layout. This should force a
168 // queue transition even though the layouts match.
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400169 vkTex->setImageLayout(vkGpu, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, 0,
Eric Karl3f219cb2019-03-22 17:46:55 -0700170 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, false, false);
171
172 // Get our image info again and make sure we transitioned queues.
173 GrBackendTexture newBackendTexture = image->getBackendTexture(true);
174 GrVkImageInfo newVkInfo;
175 REPORTER_ASSERT(reporter, newBackendTexture.getVkImageInfo(&newVkInfo));
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400176 REPORTER_ASSERT(reporter, newVkInfo.fCurrentQueueFamily == vkGpu->queueIndex());
Eric Karl3f219cb2019-03-22 17:46:55 -0700177
178 image.reset();
179 gpu->testingOnly_flushGpuAndSync();
Robert Phillips58adb342020-07-23 09:41:57 -0400180 dContext->deleteBackendTexture(backendTex);
Eric Karl3f219cb2019-03-22 17:46:55 -0700181}
Greg Danieleed7d632019-06-26 13:48:12 -0400182#endif
Eric Karl3f219cb2019-03-22 17:46:55 -0700183
Greg Daniel52e16d92018-04-10 09:34:07 -0400184#endif