blob: 1ad17aeadc78f05b3cbb5998a4050c8a5807c3ab [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/core/SkImage.h"
15#include "include/gpu/GrBackendSurface.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040016#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/gpu/vk/GrVkTypes.h"
Brian Salomon72050802020-10-12 20:45:06 -040018#include "include/gpu/vk/GrVkVulkan.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050019#include "src/gpu/GrSurfaceDrawContext.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000020#include "src/gpu/GrTexture.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040021#include "src/gpu/GrTextureProxy.h"
Greg Daniel797efca2019-05-09 14:04:20 -040022#include "src/gpu/SkGpuDevice.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/vk/GrVkGpu.h"
24#include "src/gpu/vk/GrVkImageLayout.h"
25#include "src/gpu/vk/GrVkTexture.h"
26#include "src/image/SkImage_Base.h"
Greg Daniel7c902112020-03-06 13:07:10 -050027#include "src/image/SkImage_GpuBase.h"
Greg Daniel797efca2019-05-09 14:04:20 -040028#include "src/image/SkSurface_Gpu.h"
Brian Salomon72050802020-10-12 20:45:06 -040029#include "tests/Test.h"
30#include "tools/gpu/ManagedBackendTexture.h"
Greg Daniel52e16d92018-04-10 09:34:07 -040031
32DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkImageLayoutTest, reporter, ctxInfo) {
Adlai Holler14dc7912020-08-11 15:48:49 +000033 auto dContext = ctxInfo.directContext();
Greg Daniel52e16d92018-04-10 09:34:07 -040034
Brian Salomon72050802020-10-12 20:45:06 -040035 auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData(
36 dContext, 1, 1, kRGBA_8888_SkColorType, GrMipmapped::kNo, GrRenderable::kNo);
37 if (!mbet) {
38 ERRORF(reporter, "Could not create backend texture.");
39 return;
40 }
Greg Daniel52e16d92018-04-10 09:34:07 -040041
42 GrVkImageInfo info;
Brian Salomon72050802020-10-12 20:45:06 -040043 REPORTER_ASSERT(reporter, mbet->texture().getVkImageInfo(&info));
Greg Daniel52e16d92018-04-10 09:34:07 -040044 VkImageLayout initLayout = info.fImageLayout;
45
46 // Verify that setting that layout via a copy of a backendTexture is reflected in all the
47 // backendTextures.
Brian Salomon72050802020-10-12 20:45:06 -040048 GrBackendTexture backendTex1 = mbet->texture();
49 GrBackendTexture backendTex2 = backendTex1;
50 REPORTER_ASSERT(reporter, backendTex2.getVkImageInfo(&info));
Greg Daniel52e16d92018-04-10 09:34:07 -040051 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
52
Brian Salomon72050802020-10-12 20:45:06 -040053 backendTex2.setVkImageLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Greg Daniel52e16d92018-04-10 09:34:07 -040054
Brian Salomon72050802020-10-12 20:45:06 -040055 REPORTER_ASSERT(reporter, backendTex1.getVkImageInfo(&info));
Greg Daniel52e16d92018-04-10 09:34:07 -040056 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout);
57
Brian Salomon72050802020-10-12 20:45:06 -040058 REPORTER_ASSERT(reporter, backendTex2.getVkImageInfo(&info));
Greg Daniel52e16d92018-04-10 09:34:07 -040059 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
Brian Salomon72050802020-10-12 20:45:06 -040062 backendTex1.setVkImageLayout(initLayout);
Greg Daniel52e16d92018-04-10 09:34:07 -040063
Brian Salomon72050802020-10-12 20:45:06 -040064 sk_sp<SkImage> wrappedImage = SkImage::MakeFromTexture(
65 dContext,
66 backendTex1,
67 kTopLeft_GrSurfaceOrigin,
68 kRGBA_8888_SkColorType,
69 kPremul_SkAlphaType,
70 /*color space*/ nullptr,
71 sk_gpu_test::ManagedBackendTexture::ReleaseProc,
72 mbet->releaseContext());
Greg Daniel52e16d92018-04-10 09:34:07 -040073 REPORTER_ASSERT(reporter, wrappedImage.get());
74
Adlai Holler14dc7912020-08-11 15:48:49 +000075 const GrSurfaceProxyView* view = as_IB(wrappedImage)->view(dContext);
Greg Danielfebdedf2020-02-05 17:06:27 -050076 REPORTER_ASSERT(reporter, view);
77 REPORTER_ASSERT(reporter, view->proxy()->isInstantiated());
78 GrTexture* texture = view->proxy()->peekTexture();
Greg Daniel52e16d92018-04-10 09:34:07 -040079 REPORTER_ASSERT(reporter, texture);
80
81 // Verify that modifying the layout via the GrVkTexture is reflected in the GrBackendTexture
82 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
83 REPORTER_ASSERT(reporter, initLayout == vkTexture->currentLayout());
84 vkTexture->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
85
Brian Salomon72050802020-10-12 20:45:06 -040086 REPORTER_ASSERT(reporter, backendTex1.getVkImageInfo(&info));
Greg Daniel52e16d92018-04-10 09:34:07 -040087 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == info.fImageLayout);
88
89 GrBackendTexture backendTexImage = wrappedImage->getBackendTexture(false);
90 REPORTER_ASSERT(reporter, backendTexImage.getVkImageInfo(&info));
91 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == info.fImageLayout);
92
93 // Verify that modifying the layout via the GrBackendTexutre is reflected in the GrVkTexture
94 backendTexImage.setVkImageLayout(VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
95 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL == vkTexture->currentLayout());
96
Greg Daniel52e16d92018-04-10 09:34:07 -040097 vkTexture->updateImageLayout(initLayout);
98
Brian Salomon72050802020-10-12 20:45:06 -040099 REPORTER_ASSERT(reporter, backendTex1.getVkImageInfo(&info));
Greg Daniel52e16d92018-04-10 09:34:07 -0400100 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
101
Brian Salomon72050802020-10-12 20:45:06 -0400102 REPORTER_ASSERT(reporter, backendTex2.getVkImageInfo(&info));
Greg Daniel52e16d92018-04-10 09:34:07 -0400103 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
104
105 REPORTER_ASSERT(reporter, backendTexImage.getVkImageInfo(&info));
106 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
107
108 // Check that we can do things like assigning the backend texture to invalid one, assign an
109 // invalid one, assin a backend texture to inself etc. Success here is that we don't hit any of
110 // our ref counting asserts.
Brian Salomon72050802020-10-12 20:45:06 -0400111 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(backendTex1, backendTex2));
Greg Daniel52e16d92018-04-10 09:34:07 -0400112
113 GrBackendTexture invalidTexture;
114 REPORTER_ASSERT(reporter, !invalidTexture.isValid());
Brian Salomon72050802020-10-12 20:45:06 -0400115 REPORTER_ASSERT(reporter, !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTex2));
Greg Daniel52e16d92018-04-10 09:34:07 -0400116
Brian Salomon72050802020-10-12 20:45:06 -0400117 backendTex2 = invalidTexture;
118 REPORTER_ASSERT(reporter, !backendTex2.isValid());
119 REPORTER_ASSERT(reporter, !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTex2));
Greg Daniel52e16d92018-04-10 09:34:07 -0400120
Brian Salomon72050802020-10-12 20:45:06 -0400121 invalidTexture = backendTex1;
Greg Daniel52e16d92018-04-10 09:34:07 -0400122 REPORTER_ASSERT(reporter, invalidTexture.isValid());
Brian Salomon72050802020-10-12 20:45:06 -0400123 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTex1));
Greg Daniel52e16d92018-04-10 09:34:07 -0400124
Ben Wagnerff134f22018-04-24 16:29:16 -0400125 invalidTexture = static_cast<decltype(invalidTexture)&>(invalidTexture);
Greg Daniel52e16d92018-04-10 09:34:07 -0400126 REPORTER_ASSERT(reporter, invalidTexture.isValid());
127 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, invalidTexture));
Greg Daniel52e16d92018-04-10 09:34:07 -0400128}
129
Greg Danieleed7d632019-06-26 13:48:12 -0400130// This test is disabled because it executes illegal vulkan calls which cause the validations layers
131// to fail and makes us assert. Once fixed to use a valid vulkan call sequence it should be
132// renenabled, see skbug.com/8936.
133#if 0
Eric Karl3f219cb2019-03-22 17:46:55 -0700134// Test to make sure we transition from the EXTERNAL queue even when no layout transition is needed.
135DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkTransitionExternalQueueTest, reporter, ctxInfo) {
Robert Phillips58adb342020-07-23 09:41:57 -0400136 auto dContext = ctxInfo.directContext();
137 GrGpu* gpu = dContext->priv().getGpu();
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400138 GrVkGpu* vkGpu = static_cast<GrVkGpu*>(gpu);
139 if (!vkGpu->vkCaps().supportsExternalMemory()) {
Eric Karl3f219cb2019-03-22 17:46:55 -0700140 return;
141 }
142
Robert Phillips58adb342020-07-23 09:41:57 -0400143 GrBackendTexture backendTex = dContext->createBackendTexture(
Robert Phillips80626792019-06-04 07:16:10 -0400144 1, 1, kRGBA_8888_SkColorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400145 SkColors::kTransparent, GrMipmapped::kNo, GrRenderable::kNo);
Eric Karl3f219cb2019-03-22 17:46:55 -0700146 sk_sp<SkImage> image;
147 // Make a backend texture with an external queue family and general layout.
148 GrVkImageInfo vkInfo;
149 if (!backendTex.getVkImageInfo(&vkInfo)) {
150 return;
151 }
152 vkInfo.fCurrentQueueFamily = VK_QUEUE_FAMILY_EXTERNAL;
153 // Use a read-only layout as these are the ones where we can otherwise skip a transition.
154 vkInfo.fImageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
155
156 GrBackendTexture vkExtTex(1, 1, vkInfo);
157 REPORTER_ASSERT(reporter, vkExtTex.isValid());
Robert Phillips58adb342020-07-23 09:41:57 -0400158 image = SkImage::MakeFromTexture(dContext, vkExtTex, kTopLeft_GrSurfaceOrigin,
Eric Karl3f219cb2019-03-22 17:46:55 -0700159 kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr, nullptr,
160 nullptr);
161
162 if (!image) {
163 return;
164 }
165
166 GrTexture* texture = image->getTexture();
167 REPORTER_ASSERT(reporter, texture);
168 GrVkTexture* vkTex = static_cast<GrVkTexture*>(texture);
169
170 // Change our backend texture to the internal queue, with the same layout. This should force a
171 // queue transition even though the layouts match.
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400172 vkTex->setImageLayout(vkGpu, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, 0,
Eric Karl3f219cb2019-03-22 17:46:55 -0700173 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, false, false);
174
175 // Get our image info again and make sure we transitioned queues.
176 GrBackendTexture newBackendTexture = image->getBackendTexture(true);
177 GrVkImageInfo newVkInfo;
178 REPORTER_ASSERT(reporter, newBackendTexture.getVkImageInfo(&newVkInfo));
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400179 REPORTER_ASSERT(reporter, newVkInfo.fCurrentQueueFamily == vkGpu->queueIndex());
Eric Karl3f219cb2019-03-22 17:46:55 -0700180
181 image.reset();
182 gpu->testingOnly_flushGpuAndSync();
Robert Phillips58adb342020-07-23 09:41:57 -0400183 dContext->deleteBackendTexture(backendTex);
Eric Karl3f219cb2019-03-22 17:46:55 -0700184}
Greg Danieleed7d632019-06-26 13:48:12 -0400185#endif
Eric Karl3f219cb2019-03-22 17:46:55 -0700186
Greg Daniel52e16d92018-04-10 09:34:07 -0400187#endif