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