Greg Daniel | 6c6caf4 | 2020-05-29 12:11:05 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 Google LLC |
| 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 | #include "include/core/SkImage.h" |
| 9 | #include "include/gpu/GrBackendSurface.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 10 | #include "include/gpu/GrDirectContext.h" |
Greg Daniel | 6c6caf4 | 2020-05-29 12:11:05 -0400 | [diff] [blame] | 11 | #include "include/gpu/vk/GrVkTypes.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrDirectContextPriv.h" |
Greg Daniel | 6c6caf4 | 2020-05-29 12:11:05 -0400 | [diff] [blame] | 13 | #include "src/gpu/GrTexture.h" |
| 14 | #include "src/gpu/GrTextureProxy.h" |
| 15 | #include "src/image/SkImage_Base.h" |
| 16 | #include "tests/Test.h" |
| 17 | |
| 18 | #ifdef SK_VULKAN |
Greg Daniel | 1db8e79 | 2020-06-09 17:29:32 -0400 | [diff] [blame] | 19 | #include "src/gpu/vk/GrVkGpu.h" |
Greg Daniel | 6c6caf4 | 2020-05-29 12:11:05 -0400 | [diff] [blame] | 20 | #include "src/gpu/vk/GrVkTexture.h" |
| 21 | |
| 22 | DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkBackendSurfaceMutableStateTest, reporter, ctxInfo) { |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 23 | auto dContext = ctxInfo.directContext(); |
Greg Daniel | 6c6caf4 | 2020-05-29 12:11:05 -0400 | [diff] [blame] | 24 | |
| 25 | GrBackendFormat format = GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8A8_UNORM); |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 26 | GrBackendTexture backendTex = dContext->createBackendTexture( |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 27 | 32, 32, format, GrMipmapped::kNo, GrRenderable::kNo, GrProtected::kNo); |
Greg Daniel | 6c6caf4 | 2020-05-29 12:11:05 -0400 | [diff] [blame] | 28 | |
| 29 | REPORTER_ASSERT(reporter, backendTex.isValid()); |
| 30 | |
| 31 | GrVkImageInfo info; |
| 32 | REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info)); |
| 33 | VkImageLayout initLayout = info.fImageLayout; |
| 34 | uint32_t initQueue = info.fCurrentQueueFamily; |
| 35 | GrBackendSurfaceMutableState initState(initLayout, initQueue); |
| 36 | |
| 37 | // Verify that setting that state via a copy of a backendTexture is reflected in all the |
| 38 | // backendTextures. |
| 39 | GrBackendTexture backendTexCopy = backendTex; |
| 40 | REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info)); |
| 41 | REPORTER_ASSERT(reporter, initLayout == info.fImageLayout); |
| 42 | REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily); |
| 43 | |
| 44 | GrBackendSurfaceMutableState newState(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, |
| 45 | VK_QUEUE_FAMILY_IGNORED); |
| 46 | backendTexCopy.setMutableState(newState); |
| 47 | |
| 48 | REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info)); |
| 49 | REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout); |
| 50 | REPORTER_ASSERT(reporter, VK_QUEUE_FAMILY_IGNORED == info.fCurrentQueueFamily); |
| 51 | |
| 52 | REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info)); |
| 53 | REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout); |
| 54 | REPORTER_ASSERT(reporter, VK_QUEUE_FAMILY_IGNORED == info.fCurrentQueueFamily); |
| 55 | |
| 56 | // Setting back to the init state since we didn't actually change it |
| 57 | backendTex.setMutableState(initState); |
| 58 | |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 59 | sk_sp<SkImage> wrappedImage = SkImage::MakeFromTexture(dContext, backendTex, |
Greg Daniel | 6c6caf4 | 2020-05-29 12:11:05 -0400 | [diff] [blame] | 60 | kTopLeft_GrSurfaceOrigin, |
| 61 | kRGBA_8888_SkColorType, |
| 62 | kPremul_SkAlphaType, nullptr); |
| 63 | |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 64 | const GrSurfaceProxyView* view = as_IB(wrappedImage)->view(dContext); |
Greg Daniel | 6c6caf4 | 2020-05-29 12:11:05 -0400 | [diff] [blame] | 65 | REPORTER_ASSERT(reporter, view); |
| 66 | REPORTER_ASSERT(reporter, view->proxy()->isInstantiated()); |
| 67 | GrTexture* texture = view->proxy()->peekTexture(); |
| 68 | REPORTER_ASSERT(reporter, texture); |
| 69 | |
| 70 | // Verify that modifying the layout via the GrVkTexture is reflected in the GrBackendTexture |
| 71 | GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture); |
| 72 | REPORTER_ASSERT(reporter, initLayout == vkTexture->currentLayout()); |
| 73 | REPORTER_ASSERT(reporter, initQueue == vkTexture->currentQueueFamilyIndex()); |
| 74 | vkTexture->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); |
| 75 | |
| 76 | REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info)); |
| 77 | REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == info.fImageLayout); |
| 78 | REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily); |
| 79 | |
| 80 | GrBackendTexture backendTexImage = wrappedImage->getBackendTexture(false); |
| 81 | REPORTER_ASSERT(reporter, backendTexImage.getVkImageInfo(&info)); |
| 82 | REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == info.fImageLayout); |
| 83 | REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily); |
| 84 | |
| 85 | // Verify that modifying the layout via the GrBackendTexutre is reflected in the GrVkTexture |
| 86 | backendTexImage.setMutableState(newState); |
| 87 | REPORTER_ASSERT(reporter, |
| 88 | VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == vkTexture->currentLayout()); |
| 89 | REPORTER_ASSERT(reporter, VK_QUEUE_FAMILY_IGNORED == info.fCurrentQueueFamily); |
| 90 | |
| 91 | vkTexture->setQueueFamilyIndex(initQueue); |
| 92 | vkTexture->updateImageLayout(initLayout); |
| 93 | |
| 94 | REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info)); |
| 95 | REPORTER_ASSERT(reporter, initLayout == info.fImageLayout); |
| 96 | REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily); |
| 97 | |
| 98 | REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info)); |
| 99 | REPORTER_ASSERT(reporter, initLayout == info.fImageLayout); |
| 100 | REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily); |
| 101 | |
| 102 | REPORTER_ASSERT(reporter, backendTexImage.getVkImageInfo(&info)); |
| 103 | REPORTER_ASSERT(reporter, initLayout == info.fImageLayout); |
| 104 | REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily); |
| 105 | |
Greg Daniel | 1db8e79 | 2020-06-09 17:29:32 -0400 | [diff] [blame] | 106 | // Test using the setBackendTextureStateAPI. Unlike the previous test this will actually add |
| 107 | // real transitions to the image so we need to be careful about doing actual valid transitions. |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 108 | GrVkGpu* gpu = static_cast<GrVkGpu*>(dContext->priv().getGpu()); |
Greg Daniel | 1db8e79 | 2020-06-09 17:29:32 -0400 | [diff] [blame] | 109 | |
Greg Daniel | 1d3c8c1 | 2020-09-23 14:23:36 -0400 | [diff] [blame] | 110 | GrBackendSurfaceMutableState previousState; |
| 111 | |
| 112 | dContext->setBackendTextureState(backendTex, newState, &previousState); |
Greg Daniel | 1db8e79 | 2020-06-09 17:29:32 -0400 | [diff] [blame] | 113 | |
| 114 | REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info)); |
| 115 | REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout); |
| 116 | REPORTER_ASSERT(reporter, gpu->queueIndex() == info.fCurrentQueueFamily); |
| 117 | |
Greg Daniel | 1d3c8c1 | 2020-09-23 14:23:36 -0400 | [diff] [blame] | 118 | REPORTER_ASSERT(reporter, previousState.isValid()); |
| 119 | REPORTER_ASSERT(reporter, previousState.backend() == GrBackendApi::kVulkan); |
| 120 | REPORTER_ASSERT(reporter, previousState.getVkImageLayout() == initLayout); |
| 121 | REPORTER_ASSERT(reporter, previousState.getQueueFamilyIndex() == initQueue); |
| 122 | |
Greg Daniel | 9a48697 | 2020-09-21 16:02:21 -0400 | [diff] [blame] | 123 | // Make sure passing in VK_IMAGE_LAYOUT_UNDEFINED does not change the layout |
| 124 | GrBackendSurfaceMutableState noopState(VK_IMAGE_LAYOUT_UNDEFINED, VK_QUEUE_FAMILY_IGNORED); |
Greg Daniel | 1d3c8c1 | 2020-09-23 14:23:36 -0400 | [diff] [blame] | 125 | dContext->setBackendTextureState(backendTex, noopState, &previousState); |
Greg Daniel | 9a48697 | 2020-09-21 16:02:21 -0400 | [diff] [blame] | 126 | REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info)); |
| 127 | REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout); |
| 128 | REPORTER_ASSERT(reporter, gpu->queueIndex() == info.fCurrentQueueFamily); |
| 129 | |
Greg Daniel | 1d3c8c1 | 2020-09-23 14:23:36 -0400 | [diff] [blame] | 130 | REPORTER_ASSERT(reporter, previousState.isValid()); |
| 131 | REPORTER_ASSERT(reporter, previousState.backend() == GrBackendApi::kVulkan); |
| 132 | REPORTER_ASSERT(reporter, |
| 133 | previousState.getVkImageLayout() == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); |
| 134 | REPORTER_ASSERT(reporter, previousState.getQueueFamilyIndex() == gpu->queueIndex()); |
| 135 | |
Greg Daniel | 1db8e79 | 2020-06-09 17:29:32 -0400 | [diff] [blame] | 136 | // To test queue transitions, we don't have any other valid queue available so instead we try |
| 137 | // to transition to external queue. |
| 138 | if (gpu->vkCaps().supportsExternalMemory()) { |
| 139 | GrBackendSurfaceMutableState externalState(VK_IMAGE_LAYOUT_GENERAL, |
| 140 | VK_QUEUE_FAMILY_EXTERNAL); |
| 141 | |
Greg Daniel | 1d3c8c1 | 2020-09-23 14:23:36 -0400 | [diff] [blame] | 142 | dContext->setBackendTextureState(backendTex, externalState, &previousState); |
Greg Daniel | 1db8e79 | 2020-06-09 17:29:32 -0400 | [diff] [blame] | 143 | |
| 144 | REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info)); |
| 145 | REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_GENERAL == info.fImageLayout); |
| 146 | REPORTER_ASSERT(reporter, VK_QUEUE_FAMILY_EXTERNAL == info.fCurrentQueueFamily); |
| 147 | |
Greg Daniel | 1d3c8c1 | 2020-09-23 14:23:36 -0400 | [diff] [blame] | 148 | REPORTER_ASSERT(reporter, previousState.isValid()); |
| 149 | REPORTER_ASSERT(reporter, previousState.backend() == GrBackendApi::kVulkan); |
| 150 | REPORTER_ASSERT(reporter, |
| 151 | previousState.getVkImageLayout() == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); |
| 152 | REPORTER_ASSERT(reporter, previousState.getQueueFamilyIndex() == gpu->queueIndex()); |
| 153 | |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 154 | dContext->submit(); |
Greg Daniel | 1db8e79 | 2020-06-09 17:29:32 -0400 | [diff] [blame] | 155 | |
Greg Daniel | 9a48697 | 2020-09-21 16:02:21 -0400 | [diff] [blame] | 156 | // Go back to the initial queue. Also we should stay in VK_IMAGE_LAYOUT_GENERAL since we |
| 157 | // are passing in VK_IMAGE_LAYOUT_UNDEFINED |
| 158 | GrBackendSurfaceMutableState externalState2(VK_IMAGE_LAYOUT_UNDEFINED, initQueue); |
Greg Daniel | 1d3c8c1 | 2020-09-23 14:23:36 -0400 | [diff] [blame] | 159 | dContext->setBackendTextureState(backendTex, externalState2, &previousState); |
Greg Daniel | 1db8e79 | 2020-06-09 17:29:32 -0400 | [diff] [blame] | 160 | |
| 161 | REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info)); |
| 162 | REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_GENERAL == info.fImageLayout); |
| 163 | REPORTER_ASSERT(reporter, gpu->queueIndex() == info.fCurrentQueueFamily); |
Greg Daniel | 1d3c8c1 | 2020-09-23 14:23:36 -0400 | [diff] [blame] | 164 | |
| 165 | REPORTER_ASSERT(reporter, previousState.isValid()); |
| 166 | REPORTER_ASSERT(reporter, previousState.backend() == GrBackendApi::kVulkan); |
| 167 | REPORTER_ASSERT(reporter, previousState.getVkImageLayout() == VK_IMAGE_LAYOUT_GENERAL); |
| 168 | REPORTER_ASSERT(reporter, previousState.getQueueFamilyIndex() == VK_QUEUE_FAMILY_EXTERNAL); |
Greg Daniel | 1db8e79 | 2020-06-09 17:29:32 -0400 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | // We must submit this work before we try to delete the backend texture. |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 172 | dContext->submit(true); |
Greg Daniel | 1db8e79 | 2020-06-09 17:29:32 -0400 | [diff] [blame] | 173 | |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 174 | dContext->deleteBackendTexture(backendTex); |
Greg Daniel | 6c6caf4 | 2020-05-29 12:11:05 -0400 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | #endif |