blob: e0f5c7fa6a7b1a31134b131c9a8db055fc86e833 [file] [log] [blame]
Greg Daniel6c6caf42020-05-29 12:11:05 -04001/*
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#include "include/core/SkImage.h"
9#include "include/gpu/GrBackendSurface.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040010#include "include/gpu/GrDirectContext.h"
Greg Daniel6c6caf42020-05-29 12:11:05 -040011#include "include/gpu/vk/GrVkTypes.h"
Adlai Hollera0693042020-10-14 11:23:11 -040012#include "src/gpu/GrDirectContextPriv.h"
Greg Daniel6c6caf42020-05-29 12:11:05 -040013#include "src/gpu/GrTexture.h"
14#include "src/gpu/GrTextureProxy.h"
15#include "src/image/SkImage_Base.h"
16#include "tests/Test.h"
17
18#ifdef SK_VULKAN
Greg Daniel1db8e792020-06-09 17:29:32 -040019#include "src/gpu/vk/GrVkGpu.h"
Greg Daniel6c6caf42020-05-29 12:11:05 -040020#include "src/gpu/vk/GrVkTexture.h"
21
22DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkBackendSurfaceMutableStateTest, reporter, ctxInfo) {
Adlai Holler14dc7912020-08-11 15:48:49 +000023 auto dContext = ctxInfo.directContext();
Greg Daniel6c6caf42020-05-29 12:11:05 -040024
25 GrBackendFormat format = GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8A8_UNORM);
Adlai Holler14dc7912020-08-11 15:48:49 +000026 GrBackendTexture backendTex = dContext->createBackendTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -040027 32, 32, format, GrMipmapped::kNo, GrRenderable::kNo, GrProtected::kNo);
Greg Daniel6c6caf42020-05-29 12:11:05 -040028
29 REPORTER_ASSERT(reporter, backendTex.isValid());
30
31 GrVkImageInfo info;
32 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
33 VkImageLayout initLayout = info.fImageLayout;
34 uint32_t initQueue = info.fCurrentQueueFamily;
35 GrBackendSurfaceMutableState initState(initLayout, initQueue);
36
37 // Verify that setting that state via a copy of a backendTexture is reflected in all the
38 // backendTextures.
39 GrBackendTexture backendTexCopy = backendTex;
40 REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info));
41 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
42 REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily);
43
44 GrBackendSurfaceMutableState newState(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
45 VK_QUEUE_FAMILY_IGNORED);
46 backendTexCopy.setMutableState(newState);
47
48 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
49 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout);
50 REPORTER_ASSERT(reporter, VK_QUEUE_FAMILY_IGNORED == info.fCurrentQueueFamily);
51
52 REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info));
53 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout);
54 REPORTER_ASSERT(reporter, VK_QUEUE_FAMILY_IGNORED == info.fCurrentQueueFamily);
55
56 // Setting back to the init state since we didn't actually change it
57 backendTex.setMutableState(initState);
58
Adlai Holler14dc7912020-08-11 15:48:49 +000059 sk_sp<SkImage> wrappedImage = SkImage::MakeFromTexture(dContext, backendTex,
Greg Daniel6c6caf42020-05-29 12:11:05 -040060 kTopLeft_GrSurfaceOrigin,
61 kRGBA_8888_SkColorType,
62 kPremul_SkAlphaType, nullptr);
63
Adlai Holler14dc7912020-08-11 15:48:49 +000064 const GrSurfaceProxyView* view = as_IB(wrappedImage)->view(dContext);
Greg Daniel6c6caf42020-05-29 12:11:05 -040065 REPORTER_ASSERT(reporter, view);
66 REPORTER_ASSERT(reporter, view->proxy()->isInstantiated());
67 GrTexture* texture = view->proxy()->peekTexture();
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 REPORTER_ASSERT(reporter, initQueue == vkTexture->currentQueueFamilyIndex());
74 vkTexture->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
75
76 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
77 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == info.fImageLayout);
78 REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily);
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 REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily);
84
85 // Verify that modifying the layout via the GrBackendTexutre is reflected in the GrVkTexture
86 backendTexImage.setMutableState(newState);
87 REPORTER_ASSERT(reporter,
88 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == vkTexture->currentLayout());
89 REPORTER_ASSERT(reporter, VK_QUEUE_FAMILY_IGNORED == info.fCurrentQueueFamily);
90
91 vkTexture->setQueueFamilyIndex(initQueue);
92 vkTexture->updateImageLayout(initLayout);
93
94 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
95 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
96 REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily);
97
98 REPORTER_ASSERT(reporter, backendTexCopy.getVkImageInfo(&info));
99 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
100 REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily);
101
102 REPORTER_ASSERT(reporter, backendTexImage.getVkImageInfo(&info));
103 REPORTER_ASSERT(reporter, initLayout == info.fImageLayout);
104 REPORTER_ASSERT(reporter, initQueue == info.fCurrentQueueFamily);
105
Greg Daniel1db8e792020-06-09 17:29:32 -0400106 // Test using the setBackendTextureStateAPI. Unlike the previous test this will actually add
107 // real transitions to the image so we need to be careful about doing actual valid transitions.
Adlai Holler14dc7912020-08-11 15:48:49 +0000108 GrVkGpu* gpu = static_cast<GrVkGpu*>(dContext->priv().getGpu());
Greg Daniel1db8e792020-06-09 17:29:32 -0400109
Greg Daniel1d3c8c12020-09-23 14:23:36 -0400110 GrBackendSurfaceMutableState previousState;
111
112 dContext->setBackendTextureState(backendTex, newState, &previousState);
Greg Daniel1db8e792020-06-09 17:29:32 -0400113
114 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
115 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout);
116 REPORTER_ASSERT(reporter, gpu->queueIndex() == info.fCurrentQueueFamily);
117
Greg Daniel1d3c8c12020-09-23 14:23:36 -0400118 REPORTER_ASSERT(reporter, previousState.isValid());
119 REPORTER_ASSERT(reporter, previousState.backend() == GrBackendApi::kVulkan);
120 REPORTER_ASSERT(reporter, previousState.getVkImageLayout() == initLayout);
121 REPORTER_ASSERT(reporter, previousState.getQueueFamilyIndex() == initQueue);
122
Greg Daniel9a486972020-09-21 16:02:21 -0400123 // Make sure passing in VK_IMAGE_LAYOUT_UNDEFINED does not change the layout
124 GrBackendSurfaceMutableState noopState(VK_IMAGE_LAYOUT_UNDEFINED, VK_QUEUE_FAMILY_IGNORED);
Greg Daniel1d3c8c12020-09-23 14:23:36 -0400125 dContext->setBackendTextureState(backendTex, noopState, &previousState);
Greg Daniel9a486972020-09-21 16:02:21 -0400126 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
127 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == info.fImageLayout);
128 REPORTER_ASSERT(reporter, gpu->queueIndex() == info.fCurrentQueueFamily);
129
Greg Daniel1d3c8c12020-09-23 14:23:36 -0400130 REPORTER_ASSERT(reporter, previousState.isValid());
131 REPORTER_ASSERT(reporter, previousState.backend() == GrBackendApi::kVulkan);
132 REPORTER_ASSERT(reporter,
133 previousState.getVkImageLayout() == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
134 REPORTER_ASSERT(reporter, previousState.getQueueFamilyIndex() == gpu->queueIndex());
135
Greg Daniel1db8e792020-06-09 17:29:32 -0400136 // To test queue transitions, we don't have any other valid queue available so instead we try
137 // to transition to external queue.
138 if (gpu->vkCaps().supportsExternalMemory()) {
139 GrBackendSurfaceMutableState externalState(VK_IMAGE_LAYOUT_GENERAL,
140 VK_QUEUE_FAMILY_EXTERNAL);
141
Greg Daniel1d3c8c12020-09-23 14:23:36 -0400142 dContext->setBackendTextureState(backendTex, externalState, &previousState);
Greg Daniel1db8e792020-06-09 17:29:32 -0400143
144 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
145 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_GENERAL == info.fImageLayout);
146 REPORTER_ASSERT(reporter, VK_QUEUE_FAMILY_EXTERNAL == info.fCurrentQueueFamily);
147
Greg Daniel1d3c8c12020-09-23 14:23:36 -0400148 REPORTER_ASSERT(reporter, previousState.isValid());
149 REPORTER_ASSERT(reporter, previousState.backend() == GrBackendApi::kVulkan);
150 REPORTER_ASSERT(reporter,
151 previousState.getVkImageLayout() == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
152 REPORTER_ASSERT(reporter, previousState.getQueueFamilyIndex() == gpu->queueIndex());
153
Adlai Holler14dc7912020-08-11 15:48:49 +0000154 dContext->submit();
Greg Daniel1db8e792020-06-09 17:29:32 -0400155
Greg Daniel9a486972020-09-21 16:02:21 -0400156 // Go back to the initial queue. Also we should stay in VK_IMAGE_LAYOUT_GENERAL since we
157 // are passing in VK_IMAGE_LAYOUT_UNDEFINED
158 GrBackendSurfaceMutableState externalState2(VK_IMAGE_LAYOUT_UNDEFINED, initQueue);
Greg Daniel1d3c8c12020-09-23 14:23:36 -0400159 dContext->setBackendTextureState(backendTex, externalState2, &previousState);
Greg Daniel1db8e792020-06-09 17:29:32 -0400160
161 REPORTER_ASSERT(reporter, backendTex.getVkImageInfo(&info));
162 REPORTER_ASSERT(reporter, VK_IMAGE_LAYOUT_GENERAL == info.fImageLayout);
163 REPORTER_ASSERT(reporter, gpu->queueIndex() == info.fCurrentQueueFamily);
Greg Daniel1d3c8c12020-09-23 14:23:36 -0400164
165 REPORTER_ASSERT(reporter, previousState.isValid());
166 REPORTER_ASSERT(reporter, previousState.backend() == GrBackendApi::kVulkan);
167 REPORTER_ASSERT(reporter, previousState.getVkImageLayout() == VK_IMAGE_LAYOUT_GENERAL);
168 REPORTER_ASSERT(reporter, previousState.getQueueFamilyIndex() == VK_QUEUE_FAMILY_EXTERNAL);
Greg Daniel1db8e792020-06-09 17:29:32 -0400169 }
170
171 // We must submit this work before we try to delete the backend texture.
Adlai Holler14dc7912020-08-11 15:48:49 +0000172 dContext->submit(true);
Greg Daniel1db8e792020-06-09 17:29:32 -0400173
Adlai Holler14dc7912020-08-11 15:48:49 +0000174 dContext->deleteBackendTexture(backendTex);
Greg Daniel6c6caf42020-05-29 12:11:05 -0400175}
176
177#endif