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/core/SkImage.h" |
| 15 | #include "include/gpu/GrBackendSurface.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 16 | #include "include/gpu/GrDirectContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "include/gpu/vk/GrVkTypes.h" |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 18 | #include "include/gpu/vk/GrVkVulkan.h" |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 19 | #include "src/gpu/GrSurfaceDrawContext.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 20 | #include "src/gpu/GrTexture.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 21 | #include "src/gpu/GrTextureProxy.h" |
Greg Daniel | 797efca | 2019-05-09 14:04:20 -0400 | [diff] [blame] | 22 | #include "src/gpu/SkGpuDevice.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #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 Daniel | 7c90211 | 2020-03-06 13:07:10 -0500 | [diff] [blame] | 27 | #include "src/image/SkImage_GpuBase.h" |
Greg Daniel | 797efca | 2019-05-09 14:04:20 -0400 | [diff] [blame] | 28 | #include "src/image/SkSurface_Gpu.h" |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 29 | #include "tests/Test.h" |
| 30 | #include "tools/gpu/ManagedBackendTexture.h" |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 31 | |
| 32 | DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkImageLayoutTest, reporter, ctxInfo) { |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 33 | auto dContext = ctxInfo.directContext(); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 34 | |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 35 | 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 Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 41 | |
| 42 | GrVkImageInfo info; |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 43 | REPORTER_ASSERT(reporter, mbet->texture().getVkImageInfo(&info)); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 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. |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 48 | GrBackendTexture backendTex1 = mbet->texture(); |
| 49 | GrBackendTexture backendTex2 = backendTex1; |
| 50 | REPORTER_ASSERT(reporter, backendTex2.getVkImageInfo(&info)); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 51 | REPORTER_ASSERT(reporter, initLayout == info.fImageLayout); |
| 52 | |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 53 | backendTex2.setVkImageLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 54 | |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 55 | REPORTER_ASSERT(reporter, backendTex1.getVkImageInfo(&info)); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 56 | REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout); |
| 57 | |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 58 | REPORTER_ASSERT(reporter, backendTex2.getVkImageInfo(&info)); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 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 |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 62 | backendTex1.setVkImageLayout(initLayout); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 63 | |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 64 | 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 Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 73 | REPORTER_ASSERT(reporter, wrappedImage.get()); |
| 74 | |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 75 | const GrSurfaceProxyView* view = as_IB(wrappedImage)->view(dContext); |
Greg Daniel | febdedf | 2020-02-05 17:06:27 -0500 | [diff] [blame] | 76 | REPORTER_ASSERT(reporter, view); |
| 77 | REPORTER_ASSERT(reporter, view->proxy()->isInstantiated()); |
| 78 | GrTexture* texture = view->proxy()->peekTexture(); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 79 | 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 Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 86 | REPORTER_ASSERT(reporter, backendTex1.getVkImageInfo(&info)); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 87 | 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 Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 97 | vkTexture->updateImageLayout(initLayout); |
| 98 | |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 99 | REPORTER_ASSERT(reporter, backendTex1.getVkImageInfo(&info)); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 100 | REPORTER_ASSERT(reporter, initLayout == info.fImageLayout); |
| 101 | |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 102 | REPORTER_ASSERT(reporter, backendTex2.getVkImageInfo(&info)); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 103 | 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 Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 111 | REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(backendTex1, backendTex2)); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 112 | |
| 113 | GrBackendTexture invalidTexture; |
| 114 | REPORTER_ASSERT(reporter, !invalidTexture.isValid()); |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 115 | REPORTER_ASSERT(reporter, !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTex2)); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 116 | |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 117 | backendTex2 = invalidTexture; |
| 118 | REPORTER_ASSERT(reporter, !backendTex2.isValid()); |
| 119 | REPORTER_ASSERT(reporter, !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTex2)); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 120 | |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 121 | invalidTexture = backendTex1; |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 122 | REPORTER_ASSERT(reporter, invalidTexture.isValid()); |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 123 | REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTex1)); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 124 | |
Ben Wagner | ff134f2 | 2018-04-24 16:29:16 -0400 | [diff] [blame] | 125 | invalidTexture = static_cast<decltype(invalidTexture)&>(invalidTexture); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 126 | REPORTER_ASSERT(reporter, invalidTexture.isValid()); |
| 127 | REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, invalidTexture)); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 128 | } |
| 129 | |
Greg Daniel | eed7d63 | 2019-06-26 13:48:12 -0400 | [diff] [blame] | 130 | // 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 Karl | 3f219cb | 2019-03-22 17:46:55 -0700 | [diff] [blame] | 134 | // Test to make sure we transition from the EXTERNAL queue even when no layout transition is needed. |
| 135 | DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkTransitionExternalQueueTest, reporter, ctxInfo) { |
Robert Phillips | 58adb34 | 2020-07-23 09:41:57 -0400 | [diff] [blame] | 136 | auto dContext = ctxInfo.directContext(); |
| 137 | GrGpu* gpu = dContext->priv().getGpu(); |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 138 | GrVkGpu* vkGpu = static_cast<GrVkGpu*>(gpu); |
| 139 | if (!vkGpu->vkCaps().supportsExternalMemory()) { |
Eric Karl | 3f219cb | 2019-03-22 17:46:55 -0700 | [diff] [blame] | 140 | return; |
| 141 | } |
| 142 | |
Robert Phillips | 58adb34 | 2020-07-23 09:41:57 -0400 | [diff] [blame] | 143 | GrBackendTexture backendTex = dContext->createBackendTexture( |
Robert Phillips | 8062679 | 2019-06-04 07:16:10 -0400 | [diff] [blame] | 144 | 1, 1, kRGBA_8888_SkColorType, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 145 | SkColors::kTransparent, GrMipmapped::kNo, GrRenderable::kNo); |
Eric Karl | 3f219cb | 2019-03-22 17:46:55 -0700 | [diff] [blame] | 146 | 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 Phillips | 58adb34 | 2020-07-23 09:41:57 -0400 | [diff] [blame] | 158 | image = SkImage::MakeFromTexture(dContext, vkExtTex, kTopLeft_GrSurfaceOrigin, |
Eric Karl | 3f219cb | 2019-03-22 17:46:55 -0700 | [diff] [blame] | 159 | 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 Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 172 | vkTex->setImageLayout(vkGpu, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, 0, |
Eric Karl | 3f219cb | 2019-03-22 17:46:55 -0700 | [diff] [blame] | 173 | 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 Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 179 | REPORTER_ASSERT(reporter, newVkInfo.fCurrentQueueFamily == vkGpu->queueIndex()); |
Eric Karl | 3f219cb | 2019-03-22 17:46:55 -0700 | [diff] [blame] | 180 | |
| 181 | image.reset(); |
| 182 | gpu->testingOnly_flushGpuAndSync(); |
Robert Phillips | 58adb34 | 2020-07-23 09:41:57 -0400 | [diff] [blame] | 183 | dContext->deleteBackendTexture(backendTex); |
Eric Karl | 3f219cb | 2019-03-22 17:46:55 -0700 | [diff] [blame] | 184 | } |
Greg Daniel | eed7d63 | 2019-06-26 13:48:12 -0400 | [diff] [blame] | 185 | #endif |
Eric Karl | 3f219cb | 2019-03-22 17:46:55 -0700 | [diff] [blame] | 186 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 187 | #endif |