Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 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 intializers to work |
| 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkTypes.h" |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 11 | |
Brian Osman | c7ad40f | 2018-05-31 14:27:17 -0400 | [diff] [blame] | 12 | #if defined(SK_VULKAN) |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 13 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/gpu/vk/GrVkVulkan.h" |
Greg Daniel | 54bfb18 | 2018-11-20 17:12:36 -0500 | [diff] [blame] | 15 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "tests/Test.h" |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 17 | #include "tests/TestUtils.h" |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 18 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "include/core/SkImage.h" |
| 20 | #include "include/gpu/GrBackendSurface.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 21 | #include "include/gpu/GrDirectContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "include/gpu/vk/GrVkTypes.h" |
Greg Daniel | 797efca | 2019-05-09 14:04:20 -0400 | [diff] [blame] | 23 | #include "src/gpu/GrRenderTargetContext.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 24 | #include "src/gpu/GrTexture.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 25 | #include "src/gpu/GrTextureProxy.h" |
Greg Daniel | 797efca | 2019-05-09 14:04:20 -0400 | [diff] [blame] | 26 | #include "src/gpu/SkGpuDevice.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 27 | #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 Daniel | 7c90211 | 2020-03-06 13:07:10 -0500 | [diff] [blame] | 31 | #include "src/image/SkImage_GpuBase.h" |
Greg Daniel | 797efca | 2019-05-09 14:04:20 -0400 | [diff] [blame] | 32 | #include "src/image/SkSurface_Gpu.h" |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 33 | |
| 34 | DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkImageLayoutTest, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 35 | auto context = ctxInfo.directContext(); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 36 | |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 37 | GrBackendTexture backendTex; |
| 38 | CreateBackendTexture(context, &backendTex, 1, 1, kRGBA_8888_SkColorType, SkColors::kTransparent, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame^] | 39 | GrMipmapped::kNo, GrRenderable::kNo, GrProtected::kNo); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 40 | 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 Daniel | febdedf | 2020-02-05 17:06:27 -0500 | [diff] [blame] | 69 | 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 Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 73 | 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 Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 91 | 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 Wagner | ff134f2 | 2018-04-24 16:29:16 -0400 | [diff] [blame] | 119 | invalidTexture = static_cast<decltype(invalidTexture)&>(invalidTexture); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 120 | REPORTER_ASSERT(reporter, invalidTexture.isValid()); |
| 121 | REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, invalidTexture)); |
| 122 | |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame] | 123 | context->deleteBackendTexture(backendTex); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 124 | } |
| 125 | |
Greg Daniel | eed7d63 | 2019-06-26 13:48:12 -0400 | [diff] [blame] | 126 | // 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 Karl | 3f219cb | 2019-03-22 17:46:55 -0700 | [diff] [blame] | 130 | // Test to make sure we transition from the EXTERNAL queue even when no layout transition is needed. |
| 131 | DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkTransitionExternalQueueTest, reporter, ctxInfo) { |
| 132 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 133 | GrGpu* gpu = context->priv().getGpu(); |
| 134 | GrVkGpu* vkGpu = static_cast<GrVkGpu*>(gpu); |
| 135 | if (!vkGpu->vkCaps().supportsExternalMemory()) { |
Eric Karl | 3f219cb | 2019-03-22 17:46:55 -0700 | [diff] [blame] | 136 | return; |
| 137 | } |
| 138 | |
Robert Phillips | 4bdd36f | 2019-06-04 11:03:06 -0400 | [diff] [blame] | 139 | GrBackendTexture backendTex = context->createBackendTexture( |
Robert Phillips | 8062679 | 2019-06-04 07:16:10 -0400 | [diff] [blame] | 140 | 1, 1, kRGBA_8888_SkColorType, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame^] | 141 | SkColors::kTransparent, GrMipmapped::kNo, GrRenderable::kNo); |
Eric Karl | 3f219cb | 2019-03-22 17:46:55 -0700 | [diff] [blame] | 142 | 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 Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 168 | vkTex->setImageLayout(vkGpu, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, 0, |
Eric Karl | 3f219cb | 2019-03-22 17:46:55 -0700 | [diff] [blame] | 169 | 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 Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 175 | REPORTER_ASSERT(reporter, newVkInfo.fCurrentQueueFamily == vkGpu->queueIndex()); |
Eric Karl | 3f219cb | 2019-03-22 17:46:55 -0700 | [diff] [blame] | 176 | |
| 177 | image.reset(); |
| 178 | gpu->testingOnly_flushGpuAndSync(); |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame] | 179 | context->deleteBackendTexture(backendTex); |
Eric Karl | 3f219cb | 2019-03-22 17:46:55 -0700 | [diff] [blame] | 180 | } |
Greg Daniel | eed7d63 | 2019-06-26 13:48:12 -0400 | [diff] [blame] | 181 | #endif |
Eric Karl | 3f219cb | 2019-03-22 17:46:55 -0700 | [diff] [blame] | 182 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 183 | #endif |