blob: 0791951f59438859df3218b676c4199b63bb5508 [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) {
Robert Phillips6d344c32020-07-06 10:56:46 -040035 auto context = ctxInfo.directContext();
Greg Daniel52e16d92018-04-10 09:34:07 -040036
Greg Danielc1ad77c2020-05-06 11:40:03 -040037 GrBackendTexture backendTex;
38 CreateBackendTexture(context, &backendTex, 1, 1, kRGBA_8888_SkColorType, SkColors::kTransparent,
Brian Salomon7e67dca2020-07-21 09:27:25 -040039 GrMipmapped::kNo, GrRenderable::kNo, GrProtected::kNo);
Greg Daniel52e16d92018-04-10 09:34:07 -040040 REPORTER_ASSERT(reporter, backendTex.isValid());
41
42 GrVkImageInfo info;
43 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
44 VkImageLayout initLayout = info.fImageLayout;
45
46 // Verify that setting that layout via a copy of a backendTexture is reflected in all the
47 // backendTextures.
48 GrBackendTexture backendTexCopy = backendTex;
49 REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info));
50 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
51
52 backendTexCopy.setVkImageLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
53
54 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
55 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout);
56
57 REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info));
58 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout);
59
60 // Setting back the layout since we didn't actually change it
61 backendTex.setVkImageLayout(initLayout);
62
63 sk_sp<SkImage> wrappedImage = SkImage::MakeFromTexture(context, backendTex,
64 kTopLeft_GrSurfaceOrigin,
65 kRGBA_8888_SkColorType,
66 kPremul_SkAlphaType, nullptr);
67 REPORTER_ASSERT(reporter, wrappedImage.get());
68
Greg Danielfebdedf2020-02-05 17:06:27 -050069 const GrSurfaceProxyView* view = as_IB(wrappedImage)->view(context);
70 REPORTER_ASSERT(reporter, view);
71 REPORTER_ASSERT(reporter, view->proxy()->isInstantiated());
72 GrTexture* texture = view->proxy()->peekTexture();
Greg Daniel52e16d92018-04-10 09:34:07 -040073 REPORTER_ASSERT(reporter, texture);
74
75 // Verify that modifying the layout via the GrVkTexture is reflected in the GrBackendTexture
76 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
77 REPORTER_ASSERT(reporter, initLayout == vkTexture->currentLayout());
78 vkTexture->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
79
80 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
81 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == info.fImageLayout);
82
83 GrBackendTexture backendTexImage = wrappedImage->getBackendTexture(false);
84 REPORTER_ASSERT(reporter, backendTexImage.getVkImageInfo(&info));
85 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == info.fImageLayout);
86
87 // Verify that modifying the layout via the GrBackendTexutre is reflected in the GrVkTexture
88 backendTexImage.setVkImageLayout(VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
89 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL == vkTexture->currentLayout());
90
Greg Daniel52e16d92018-04-10 09:34:07 -040091 vkTexture->updateImageLayout(initLayout);
92
93 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
94 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
95
96 REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info));
97 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
98
99 REPORTER_ASSERT(reporter, backendTexImage.getVkImageInfo(&info));
100 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
101
102 // Check that we can do things like assigning the backend texture to invalid one, assign an
103 // invalid one, assin a backend texture to inself etc. Success here is that we don't hit any of
104 // our ref counting asserts.
105 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(backendTex, backendTexCopy));
106
107 GrBackendTexture invalidTexture;
108 REPORTER_ASSERT(reporter, !invalidTexture.isValid());
109 REPORTER_ASSERT(reporter, !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTexCopy));
110
111 backendTexCopy = invalidTexture;
112 REPORTER_ASSERT(reporter, !backendTexCopy.isValid());
113 REPORTER_ASSERT(reporter, !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTexCopy));
114
115 invalidTexture = backendTex;
116 REPORTER_ASSERT(reporter, invalidTexture.isValid());
117 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTex));
118
Ben Wagnerff134f22018-04-24 16:29:16 -0400119 invalidTexture = static_cast<decltype(invalidTexture)&>(invalidTexture);
Greg Daniel52e16d92018-04-10 09:34:07 -0400120 REPORTER_ASSERT(reporter, invalidTexture.isValid());
121 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, invalidTexture));
122
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400123 context->deleteBackendTexture(backendTex);
Greg Daniel52e16d92018-04-10 09:34:07 -0400124}
125
Greg Danieleed7d632019-06-26 13:48:12 -0400126// This test is disabled because it executes illegal vulkan calls which cause the validations layers
127// to fail and makes us assert. Once fixed to use a valid vulkan call sequence it should be
128// renenabled, see skbug.com/8936.
129#if 0
Eric Karl3f219cb2019-03-22 17:46:55 -0700130// Test to make sure we transition from the EXTERNAL queue even when no layout transition is needed.
131DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkTransitionExternalQueueTest, reporter, ctxInfo) {
132 GrContext* context = ctxInfo.grContext();
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400133 GrGpu* gpu = context->priv().getGpu();
134 GrVkGpu* vkGpu = static_cast<GrVkGpu*>(gpu);
135 if (!vkGpu->vkCaps().supportsExternalMemory()) {
Eric Karl3f219cb2019-03-22 17:46:55 -0700136 return;
137 }
138
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400139 GrBackendTexture backendTex = context->createBackendTexture(
Robert Phillips80626792019-06-04 07:16:10 -0400140 1, 1, kRGBA_8888_SkColorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400141 SkColors::kTransparent, GrMipmapped::kNo, GrRenderable::kNo);
Eric Karl3f219cb2019-03-22 17:46:55 -0700142 sk_sp<SkImage> image;
143 // Make a backend texture with an external queue family and general layout.
144 GrVkImageInfo vkInfo;
145 if (!backendTex.getVkImageInfo(&vkInfo)) {
146 return;
147 }
148 vkInfo.fCurrentQueueFamily = VK_QUEUE_FAMILY_EXTERNAL;
149 // Use a read-only layout as these are the ones where we can otherwise skip a transition.
150 vkInfo.fImageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
151
152 GrBackendTexture vkExtTex(1, 1, vkInfo);
153 REPORTER_ASSERT(reporter, vkExtTex.isValid());
154 image = SkImage::MakeFromTexture(context, vkExtTex, kTopLeft_GrSurfaceOrigin,
155 kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr, nullptr,
156 nullptr);
157
158 if (!image) {
159 return;
160 }
161
162 GrTexture* texture = image->getTexture();
163 REPORTER_ASSERT(reporter, texture);
164 GrVkTexture* vkTex = static_cast<GrVkTexture*>(texture);
165
166 // Change our backend texture to the internal queue, with the same layout. This should force a
167 // queue transition even though the layouts match.
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400168 vkTex->setImageLayout(vkGpu, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, 0,
Eric Karl3f219cb2019-03-22 17:46:55 -0700169 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, false, false);
170
171 // Get our image info again and make sure we transitioned queues.
172 GrBackendTexture newBackendTexture = image->getBackendTexture(true);
173 GrVkImageInfo newVkInfo;
174 REPORTER_ASSERT(reporter, newBackendTexture.getVkImageInfo(&newVkInfo));
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400175 REPORTER_ASSERT(reporter, newVkInfo.fCurrentQueueFamily == vkGpu->queueIndex());
Eric Karl3f219cb2019-03-22 17:46:55 -0700176
177 image.reset();
178 gpu->testingOnly_flushGpuAndSync();
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400179 context->deleteBackendTexture(backendTex);
Eric Karl3f219cb2019-03-22 17:46:55 -0700180}
Greg Danieleed7d632019-06-26 13:48:12 -0400181#endif
Eric Karl3f219cb2019-03-22 17:46:55 -0700182
Greg Daniel52e16d92018-04-10 09:34:07 -0400183#endif