• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
9 #include "tests/Test.h"
10 
11 #ifdef SK_VULKAN
12 #include "include/core/SkAlphaType.h"
13 #include "include/core/SkColorSpace.h"
14 #include "include/core/SkColorType.h"
15 #include "include/core/SkImage.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/gpu/GpuTypes.h"
18 #include "include/gpu/GrBackendSurface.h"
19 #include "include/gpu/GrDirectContext.h"
20 #include "include/gpu/GrTypes.h"
21 #include "include/gpu/MutableTextureState.h"
22 #include "include/gpu/vk/GrVkTypes.h"
23 #include "src/gpu/ganesh/GrDirectContextPriv.h"
24 #include "src/gpu/ganesh/GrSurfaceProxy.h"
25 #include "src/gpu/ganesh/GrTextureProxy.h"
26 #include "src/gpu/ganesh/vk/GrVkCaps.h"
27 #include "src/gpu/ganesh/vk/GrVkGpu.h"
28 #include "src/gpu/ganesh/vk/GrVkImage.h"
29 #include "src/gpu/ganesh/vk/GrVkTexture.h"
30 #include "tests/CtsEnforcement.h"
31 #include "tools/gpu/ProxyUtils.h"
32 
33 #include <vulkan/vulkan_core.h>
34 #include <cstdint>
35 
36 class GrTexture;
37 struct GrContextOptions;
38 
DEF_GANESH_TEST_FOR_VULKAN_CONTEXT(VkBackendSurfaceMutableStateTest,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)39 DEF_GANESH_TEST_FOR_VULKAN_CONTEXT(VkBackendSurfaceMutableStateTest,
40                                    reporter,
41                                    ctxInfo,
42                                    CtsEnforcement::kApiLevel_T) {
43     auto dContext = ctxInfo.directContext();
44 
45     GrBackendFormat format = GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8A8_UNORM);
46     GrBackendTexture backendTex = dContext->createBackendTexture(
47             32, 32, format, GrMipmapped::kNo, GrRenderable::kNo, GrProtected::kNo);
48 
49     REPORTER_ASSERT(reporter, backendTex.isValid());
50 
51     GrVkImageInfo info;
52     REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
53     VkImageLayout initLayout = info.fImageLayout;
54     uint32_t initQueue = info.fCurrentQueueFamily;
55     skgpu::MutableTextureState initState(initLayout, initQueue);
56 
57     // Verify that setting that state via a copy of a backendTexture is reflected in all the
58     // backendTextures.
59     GrBackendTexture backendTexCopy = backendTex;
60     REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info));
61     REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
62     REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily);
63 
64     skgpu::MutableTextureState newState(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
65                                         VK_QUEUE_FAMILY_IGNORED);
66     backendTexCopy.setMutableState(newState);
67 
68     REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
69     REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout);
70     REPORTER_ASSERT(reporter, VK_QUEUE_FAMILY_IGNORED == info.fCurrentQueueFamily);
71 
72     REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info));
73     REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout);
74     REPORTER_ASSERT(reporter, VK_QUEUE_FAMILY_IGNORED == info.fCurrentQueueFamily);
75 
76     // Setting back to the init state since we didn't actually change it
77     backendTex.setMutableState(initState);
78 
79     sk_sp<SkImage> wrappedImage = SkImage::MakeFromTexture(dContext, backendTex,
80                                                            kTopLeft_GrSurfaceOrigin,
81                                                            kRGBA_8888_SkColorType,
82                                                            kPremul_SkAlphaType, nullptr);
83 
84     GrSurfaceProxy* proxy = sk_gpu_test::GetTextureImageProxy(wrappedImage.get(), dContext);
85 
86     REPORTER_ASSERT(reporter, proxy);
87     REPORTER_ASSERT(reporter, proxy->isInstantiated());
88     GrTexture* texture = proxy->peekTexture();
89     REPORTER_ASSERT(reporter, texture);
90 
91     // Verify that modifying the layout via the GrVkTexture is reflected in the GrBackendTexture
92     GrVkImage* vkTexture = static_cast<GrVkTexture*>(texture)->textureImage();
93     REPORTER_ASSERT(reporter, initLayout == vkTexture->currentLayout());
94     REPORTER_ASSERT(reporter, initQueue == vkTexture->currentQueueFamilyIndex());
95     vkTexture->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
96 
97     REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
98     REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == info.fImageLayout);
99     REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily);
100 
101     GrBackendTexture backendTexImage = wrappedImage->getBackendTexture(false);
102     REPORTER_ASSERT(reporter, backendTexImage.getVkImageInfo(&info));
103     REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == info.fImageLayout);
104     REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily);
105 
106     // Verify that modifying the layout via the GrBackendTexutre is reflected in the GrVkTexture
107     backendTexImage.setMutableState(newState);
108     REPORTER_ASSERT(reporter,
109                     VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == vkTexture->currentLayout());
110     REPORTER_ASSERT(reporter, VK_QUEUE_FAMILY_IGNORED == info.fCurrentQueueFamily);
111 
112     vkTexture->setQueueFamilyIndex(initQueue);
113     vkTexture->updateImageLayout(initLayout);
114 
115     REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
116     REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
117     REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily);
118 
119     REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info));
120     REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
121     REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily);
122 
123     REPORTER_ASSERT(reporter, backendTexImage.getVkImageInfo(&info));
124     REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
125     REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily);
126 
127     // Test using the setBackendTextureStateAPI. Unlike the previous test this will actually add
128     // real transitions to the image so we need to be careful about doing actual valid transitions.
129     GrVkGpu* gpu = static_cast<GrVkGpu*>(dContext->priv().getGpu());
130 
131     skgpu::MutableTextureState previousState;
132 
133     dContext->setBackendTextureState(backendTex, newState, &previousState);
134 
135     REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
136     REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout);
137     REPORTER_ASSERT(reporter, gpu->queueIndex() == info.fCurrentQueueFamily);
138 
139     REPORTER_ASSERT(reporter, previousState.isValid());
140     REPORTER_ASSERT(reporter, previousState.backend() == skgpu::BackendApi::kVulkan);
141     REPORTER_ASSERT(reporter, previousState.getVkImageLayout() == initLayout);
142     REPORTER_ASSERT(reporter, previousState.getQueueFamilyIndex() == initQueue);
143 
144     // Make sure passing in VK_IMAGE_LAYOUT_UNDEFINED does not change the layout
145     skgpu::MutableTextureState noopState(VK_IMAGE_LAYOUT_UNDEFINED, VK_QUEUE_FAMILY_IGNORED);
146     dContext->setBackendTextureState(backendTex, noopState, &previousState);
147     REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
148     REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout);
149     REPORTER_ASSERT(reporter, gpu->queueIndex() == info.fCurrentQueueFamily);
150 
151     REPORTER_ASSERT(reporter, previousState.isValid());
152     REPORTER_ASSERT(reporter, previousState.backend() == skgpu::BackendApi::kVulkan);
153     REPORTER_ASSERT(reporter,
154                     previousState.getVkImageLayout() == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
155     REPORTER_ASSERT(reporter, previousState.getQueueFamilyIndex() == gpu->queueIndex());
156 
157     // To test queue transitions, we don't have any other valid queue available so instead we try
158     // to transition to external queue.
159     if (gpu->vkCaps().supportsExternalMemory()) {
160         skgpu::MutableTextureState externalState(VK_IMAGE_LAYOUT_GENERAL, VK_QUEUE_FAMILY_EXTERNAL);
161 
162         dContext->setBackendTextureState(backendTex, externalState, &previousState);
163 
164         REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
165         REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_GENERAL == info.fImageLayout);
166         REPORTER_ASSERT(reporter, VK_QUEUE_FAMILY_EXTERNAL == info.fCurrentQueueFamily);
167 
168         REPORTER_ASSERT(reporter, previousState.isValid());
169         REPORTER_ASSERT(reporter, previousState.backend() == skgpu::BackendApi::kVulkan);
170         REPORTER_ASSERT(reporter,
171                 previousState.getVkImageLayout() == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
172         REPORTER_ASSERT(reporter, previousState.getQueueFamilyIndex() == gpu->queueIndex());
173 
174         dContext->submit();
175 
176         // Go back to the initial queue. Also we should stay in VK_IMAGE_LAYOUT_GENERAL since we
177         // are passing in VK_IMAGE_LAYOUT_UNDEFINED
178         skgpu::MutableTextureState externalState2(VK_IMAGE_LAYOUT_UNDEFINED, initQueue);
179         dContext->setBackendTextureState(backendTex, externalState2, &previousState);
180 
181         REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
182         REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_GENERAL == info.fImageLayout);
183         REPORTER_ASSERT(reporter, gpu->queueIndex() == info.fCurrentQueueFamily);
184 
185         REPORTER_ASSERT(reporter, previousState.isValid());
186         REPORTER_ASSERT(reporter, previousState.backend() == skgpu::BackendApi::kVulkan);
187         REPORTER_ASSERT(reporter, previousState.getVkImageLayout() == VK_IMAGE_LAYOUT_GENERAL);
188         REPORTER_ASSERT(reporter, previousState.getQueueFamilyIndex() == VK_QUEUE_FAMILY_EXTERNAL);
189     }
190 
191     // We must submit this work before we try to delete the backend texture.
192     dContext->submit(true);
193 
194     dContext->deleteBackendTexture(backendTex);
195 }
196 
197 #endif
198