2 * Copyright 2020 Google LLC
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "include/core/SkColorSpace.h"
9 #include "include/core/SkImage.h"
10 #include "include/gpu/GrBackendSurface.h"
11 #include "include/gpu/GrDirectContext.h"
12 #include "include/gpu/vk/GrVkTypes.h"
13 #include "src/gpu/ganesh/GrDirectContextPriv.h"
14 #include "src/gpu/ganesh/GrTexture.h"
15 #include "src/gpu/ganesh/GrTextureProxy.h"
16 #include "src/image/SkImage_Base.h"
17 #include "tests/Test.h"
18 #include "tools/gpu/ProxyUtils.h"
21 #include "src/gpu/ganesh/vk/GrVkGpu.h"
22 #include "src/gpu/ganesh/vk/GrVkTexture.h"
24 DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkBackendSurfaceMutableStateTest, reporter, ctxInfo) {
25 auto dContext = ctxInfo.directContext();
27 GrBackendFormat format = GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8A8_UNORM);
28 GrBackendTexture backendTex = dContext->createBackendTexture(
29 32, 32, format, GrMipmapped::kNo, GrRenderable::kNo, GrProtected::kNo);
31 REPORTER_ASSERT(reporter, backendTex.isValid());
34 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
35 VkImageLayout initLayout = info.fImageLayout;
36 uint32_t initQueue = info.fCurrentQueueFamily;
37 GrBackendSurfaceMutableState initState(initLayout, initQueue);
39 // Verify that setting that state via a copy of a backendTexture is reflected in all the
41 GrBackendTexture backendTexCopy = backendTex;
42 REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info));
43 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
44 REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily);
46 GrBackendSurfaceMutableState newState(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
47 VK_QUEUE_FAMILY_IGNORED);
48 backendTexCopy.setMutableState(newState);
50 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
51 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout);
52 REPORTER_ASSERT(reporter, VK_QUEUE_FAMILY_IGNORED == info.fCurrentQueueFamily);
54 REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info));
55 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout);
56 REPORTER_ASSERT(reporter, VK_QUEUE_FAMILY_IGNORED == info.fCurrentQueueFamily);
58 // Setting back to the init state since we didn't actually change it
59 backendTex.setMutableState(initState);
61 sk_sp<SkImage> wrappedImage = SkImage::MakeFromTexture(dContext, backendTex,
62 kTopLeft_GrSurfaceOrigin,
63 kRGBA_8888_SkColorType,
64 kPremul_SkAlphaType, nullptr);
66 GrSurfaceProxy* proxy = sk_gpu_test::GetTextureImageProxy(wrappedImage.get(), dContext);
68 REPORTER_ASSERT(reporter, proxy);
69 REPORTER_ASSERT(reporter, proxy->isInstantiated());
70 GrTexture* texture = proxy->peekTexture();
71 REPORTER_ASSERT(reporter, texture);
73 // Verify that modifying the layout via the GrVkTexture is reflected in the GrBackendTexture
74 GrVkImage* vkTexture = static_cast<GrVkTexture*>(texture)->textureImage();
75 REPORTER_ASSERT(reporter, initLayout == vkTexture->currentLayout());
76 REPORTER_ASSERT(reporter, initQueue == vkTexture->currentQueueFamilyIndex());
77 vkTexture->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
79 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
80 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == info.fImageLayout);
81 REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily);
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 REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily);
88 // Verify that modifying the layout via the GrBackendTexutre is reflected in the GrVkTexture
89 backendTexImage.setMutableState(newState);
90 REPORTER_ASSERT(reporter,
91 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == vkTexture->currentLayout());
92 REPORTER_ASSERT(reporter, VK_QUEUE_FAMILY_IGNORED == info.fCurrentQueueFamily);
94 vkTexture->setQueueFamilyIndex(initQueue);
95 vkTexture->updateImageLayout(initLayout);
97 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
98 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
99 REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily);
101 REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info));
102 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
103 REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily);
105 REPORTER_ASSERT(reporter, backendTexImage.getVkImageInfo(&info));
106 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
107 REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily);
109 // Test using the setBackendTextureStateAPI. Unlike the previous test this will actually add
110 // real transitions to the image so we need to be careful about doing actual valid transitions.
111 GrVkGpu* gpu = static_cast<GrVkGpu*>(dContext->priv().getGpu());
113 GrBackendSurfaceMutableState previousState;
115 dContext->setBackendTextureState(backendTex, newState, &previousState);
117 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
118 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout);
119 REPORTER_ASSERT(reporter, gpu->queueIndex() == info.fCurrentQueueFamily);
121 REPORTER_ASSERT(reporter, previousState.isValid());
122 REPORTER_ASSERT(reporter, previousState.backend() == GrBackendApi::kVulkan);
123 REPORTER_ASSERT(reporter, previousState.getVkImageLayout() == initLayout);
124 REPORTER_ASSERT(reporter, previousState.getQueueFamilyIndex() == initQueue);
126 // Make sure passing in VK_IMAGE_LAYOUT_UNDEFINED does not change the layout
127 GrBackendSurfaceMutableState noopState(VK_IMAGE_LAYOUT_UNDEFINED, VK_QUEUE_FAMILY_IGNORED);
128 dContext->setBackendTextureState(backendTex, noopState, &previousState);
129 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
130 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout);
131 REPORTER_ASSERT(reporter, gpu->queueIndex() == info.fCurrentQueueFamily);
133 REPORTER_ASSERT(reporter, previousState.isValid());
134 REPORTER_ASSERT(reporter, previousState.backend() == GrBackendApi::kVulkan);
135 REPORTER_ASSERT(reporter,
136 previousState.getVkImageLayout() == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
137 REPORTER_ASSERT(reporter, previousState.getQueueFamilyIndex() == gpu->queueIndex());
139 // To test queue transitions, we don't have any other valid queue available so instead we try
140 // to transition to external queue.
141 if (gpu->vkCaps().supportsExternalMemory()) {
142 GrBackendSurfaceMutableState externalState(VK_IMAGE_LAYOUT_GENERAL,
143 VK_QUEUE_FAMILY_EXTERNAL);
145 dContext->setBackendTextureState(backendTex, externalState, &previousState);
147 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
148 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_GENERAL == info.fImageLayout);
149 REPORTER_ASSERT(reporter, VK_QUEUE_FAMILY_EXTERNAL == info.fCurrentQueueFamily);
151 REPORTER_ASSERT(reporter, previousState.isValid());
152 REPORTER_ASSERT(reporter, previousState.backend() == GrBackendApi::kVulkan);
153 REPORTER_ASSERT(reporter,
154 previousState.getVkImageLayout() == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
155 REPORTER_ASSERT(reporter, previousState.getQueueFamilyIndex() == gpu->queueIndex());
159 // Go back to the initial queue. Also we should stay in VK_IMAGE_LAYOUT_GENERAL since we
160 // are passing in VK_IMAGE_LAYOUT_UNDEFINED
161 GrBackendSurfaceMutableState externalState2(VK_IMAGE_LAYOUT_UNDEFINED, initQueue);
162 dContext->setBackendTextureState(backendTex, externalState2, &previousState);
164 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
165 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_GENERAL == info.fImageLayout);
166 REPORTER_ASSERT(reporter, gpu->queueIndex() == info.fCurrentQueueFamily);
168 REPORTER_ASSERT(reporter, previousState.isValid());
169 REPORTER_ASSERT(reporter, previousState.backend() == GrBackendApi::kVulkan);
170 REPORTER_ASSERT(reporter, previousState.getVkImageLayout() == VK_IMAGE_LAYOUT_GENERAL);
171 REPORTER_ASSERT(reporter, previousState.getQueueFamilyIndex() == VK_QUEUE_FAMILY_EXTERNAL);
174 // We must submit this work before we try to delete the backend texture.
175 dContext->submit(true);
177 dContext->deleteBackendTexture(backendTex);