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 | |
| 10 | #include "SkTypes.h" |
| 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 | |
Greg Daniel | 54bfb18 | 2018-11-20 17:12:36 -0500 | [diff] [blame] | 14 | #include "vk/GrVkVulkan.h" |
| 15 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 16 | #include "Test.h" |
| 17 | |
| 18 | #include "GrBackendSurface.h" |
| 19 | #include "GrContextPriv.h" |
| 20 | #include "GrTextureProxy.h" |
| 21 | #include "GrTexture.h" |
| 22 | #include "SkImage.h" |
| 23 | #include "SkImage_Base.h" |
| 24 | #include "vk/GrVkGpu.h" |
| 25 | #include "vk/GrVkImageLayout.h" |
| 26 | #include "vk/GrVkTexture.h" |
| 27 | #include "vk/GrVkTypes.h" |
| 28 | |
| 29 | DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkImageLayoutTest, reporter, ctxInfo) { |
| 30 | GrContext* context = ctxInfo.grContext(); |
| 31 | GrVkGpu* gpu = static_cast<GrVkGpu*>(context->contextPriv().getGpu()); |
| 32 | |
| 33 | GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(nullptr, 1, 1, |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 34 | GrColorType::kRGBA_8888, |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 35 | false, |
| 36 | GrMipMapped::kNo); |
| 37 | REPORTER_ASSERT(reporter, backendTex.isValid()); |
| 38 | |
| 39 | GrVkImageInfo info; |
| 40 | REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info)); |
| 41 | VkImageLayout initLayout = info.fImageLayout; |
| 42 | |
| 43 | // Verify that setting that layout via a copy of a backendTexture is reflected in all the |
| 44 | // backendTextures. |
| 45 | GrBackendTexture backendTexCopy = backendTex; |
| 46 | REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info)); |
| 47 | REPORTER_ASSERT(reporter, initLayout == info.fImageLayout); |
| 48 | |
| 49 | backendTexCopy.setVkImageLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); |
| 50 | |
| 51 | REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info)); |
| 52 | REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout); |
| 53 | |
| 54 | REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info)); |
| 55 | REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout); |
| 56 | |
| 57 | // Setting back the layout since we didn't actually change it |
| 58 | backendTex.setVkImageLayout(initLayout); |
| 59 | |
| 60 | sk_sp<SkImage> wrappedImage = SkImage::MakeFromTexture(context, backendTex, |
| 61 | kTopLeft_GrSurfaceOrigin, |
| 62 | kRGBA_8888_SkColorType, |
| 63 | kPremul_SkAlphaType, nullptr); |
| 64 | REPORTER_ASSERT(reporter, wrappedImage.get()); |
| 65 | |
| 66 | sk_sp<GrTextureProxy> texProxy = as_IB(wrappedImage)->asTextureProxyRef(); |
| 67 | REPORTER_ASSERT(reporter, texProxy.get()); |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 68 | REPORTER_ASSERT(reporter, texProxy->isInstantiated()); |
| 69 | GrTexture* texture = texProxy->peekTexture(); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 70 | REPORTER_ASSERT(reporter, texture); |
| 71 | |
| 72 | // Verify that modifying the layout via the GrVkTexture is reflected in the GrBackendTexture |
| 73 | GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture); |
| 74 | REPORTER_ASSERT(reporter, initLayout == vkTexture->currentLayout()); |
| 75 | vkTexture->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); |
| 76 | |
| 77 | REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info)); |
| 78 | REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == info.fImageLayout); |
| 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 | |
| 84 | // Verify that modifying the layout via the GrBackendTexutre is reflected in the GrVkTexture |
| 85 | backendTexImage.setVkImageLayout(VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); |
| 86 | REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL == vkTexture->currentLayout()); |
| 87 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 88 | vkTexture->updateImageLayout(initLayout); |
| 89 | |
| 90 | REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info)); |
| 91 | REPORTER_ASSERT(reporter, initLayout == info.fImageLayout); |
| 92 | |
| 93 | REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info)); |
| 94 | REPORTER_ASSERT(reporter, initLayout == info.fImageLayout); |
| 95 | |
| 96 | REPORTER_ASSERT(reporter, backendTexImage.getVkImageInfo(&info)); |
| 97 | REPORTER_ASSERT(reporter, initLayout == info.fImageLayout); |
| 98 | |
| 99 | // Check that we can do things like assigning the backend texture to invalid one, assign an |
| 100 | // invalid one, assin a backend texture to inself etc. Success here is that we don't hit any of |
| 101 | // our ref counting asserts. |
| 102 | REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(backendTex, backendTexCopy)); |
| 103 | |
| 104 | GrBackendTexture invalidTexture; |
| 105 | REPORTER_ASSERT(reporter, !invalidTexture.isValid()); |
| 106 | REPORTER_ASSERT(reporter, !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTexCopy)); |
| 107 | |
| 108 | backendTexCopy = invalidTexture; |
| 109 | REPORTER_ASSERT(reporter, !backendTexCopy.isValid()); |
| 110 | REPORTER_ASSERT(reporter, !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTexCopy)); |
| 111 | |
| 112 | invalidTexture = backendTex; |
| 113 | REPORTER_ASSERT(reporter, invalidTexture.isValid()); |
| 114 | REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTex)); |
| 115 | |
Ben Wagner | ff134f2 | 2018-04-24 16:29:16 -0400 | [diff] [blame] | 116 | invalidTexture = static_cast<decltype(invalidTexture)&>(invalidTexture); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 117 | REPORTER_ASSERT(reporter, invalidTexture.isValid()); |
| 118 | REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, invalidTexture)); |
| 119 | |
| 120 | gpu->deleteTestingOnlyBackendTexture(backendTex); |
| 121 | } |
| 122 | |
| 123 | #endif |