Debian Codegraph @

tools/gpu/vk/VkTestHelper.cpp

raw
1 /*
2 * Copyright 2020 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 #include "tools/gpu/vk/VkTestHelper.h"
9
10 #if defined(SK_VULKAN)
11
12 #include "include/core/SkSurface.h"
13 #include "include/gpu/vk/VulkanBackendContext.h"
14 #include "include/gpu/vk/VulkanMemoryAllocator.h"
15 #include "tests/TestType.h"
16 #include "tools/gpu/ProtectedUtils.h"
17 #include "tools/gpu/vk/VkTestUtils.h"
18
19 #if defined(SK_GANESH)
20 #include "include/gpu/ganesh/GrDirectContext.h"
21 #include "include/gpu/ganesh/GrTypes.h"
22 #include "include/gpu/ganesh/vk/GrVkDirectContext.h"
23 #endif
24
25 #if defined(SK_GRAPHITE)
26 #include "include/gpu/graphite/Context.h"
27 #include "include/gpu/graphite/ContextOptions.h"
28 #include "include/gpu/graphite/vk/VulkanGraphiteContext.h"
29 #include "src/gpu/graphite/ContextOptionsPriv.h"
30 #endif
31
32 #define ACQUIRE_INST_VK_PROC(name) \
33 fVk##name = reinterpret_cast<PFN_vk##name>(instProc(fBackendContext.fInstance, "vk" #name)); \
34 if (fVk##name == nullptr) { \
35 SkDebugf("Function ptr for vk%s could not be acquired\n", #name); \
36 return false; \
37 }
38
39 #define ACQUIRE_DEVICE_VK_PROC(name) \
40 fVk##name = reinterpret_cast<PFN_vk##name>(fVkGetDeviceProcAddr(fDevice, "vk" #name)); \
41 if (fVk##name == nullptr) { \
42 SkDebugf("Function ptr for vk%s could not be acquired\n", #name); \
43 return false; \
44 }
45
46 #if defined(SK_GANESH)
47
48 class GaneshVkTestHelper : public VkTestHelper {
49 public:
50 GaneshVkTestHelper(bool isProtected) : VkTestHelper(isProtected) {}
51
52 ~GaneshVkTestHelper() override {
53 // Make sure any work, release procs, etc left on the context are finished with before we
54 // start tearing everything down.
55 if (fDirectContext) {
56 fDirectContext->flushAndSubmit(GrSyncCpu::kYes);
57 }
58
59 fDirectContext.reset();
60 }
61
62 bool isValid() const override { return fDirectContext != nullptr; }
63
64 sk_sp<SkSurface> createSurface(SkISize size, bool textureable, bool isProtected) override {
65 // Make Ganesh use DMSAA to better match Graphite's behavior
66 SkSurfaceProps props(SkSurfaceProps::kDynamicMSAA_Flag, kUnknown_SkPixelGeometry);
67
68 return ProtectedUtils::CreateProtectedSkSurface(fDirectContext.get(), size,
69 textureable, isProtected,
70 &props);
71 }
72
73 void submitAndWaitForCompletion(bool* completionMarker) override {
74 fDirectContext->submit();
75 while (!*completionMarker) {
76 fDirectContext->checkAsyncWorkCompletion();
77 }
78 }
79
80 GrDirectContext* directContext() override { return fDirectContext.get(); }
81
82 protected:
83 bool init() override {
84 if (!this->setupBackendContext()) {
85 return false;
86 }
87
88 SkASSERT(fBackendContext.fMemoryAllocator);
89 fDirectContext = GrDirectContexts::MakeVulkan(fBackendContext);
90 if (!fDirectContext) {
91 return false;
92 }
93
94 SkASSERT(fDirectContext->supportsProtectedContent() == fIsProtected);
95 return true;
96 }
97
98 private:
99 sk_sp<GrDirectContext> fDirectContext;
100 };
101
102 #endif // SK_GANESH
103
104 #if defined(SK_GRAPHITE)
105
106 class GraphiteVkTestHelper : public VkTestHelper {
107 public:
108 GraphiteVkTestHelper(bool isProtected) : VkTestHelper(isProtected) {}
109
110 ~GraphiteVkTestHelper() override {
111 // Make sure any work, release procs, etc left on the context are finished with before we
112 // start tearing everything down.
113
114 std::unique_ptr<skgpu::graphite::Recording> recording;
115 if (fRecorder) {
116 recording = fRecorder->snap();
117 }
118
119 if (fContext) {
120 fContext->insertRecording({ recording.get() });
121 fContext->submit(skgpu::graphite::SyncToCpu::kYes);
122 }
123
124 recording.reset();
125 fRecorder.reset();
126 fContext.reset();
127 }
128
129 bool isValid() const override { return fContext != nullptr && fRecorder != nullptr; }
130
131 sk_sp<SkSurface> createSurface(SkISize size,
132 bool /* textureable */,
133 bool isProtected) override {
134 return ProtectedUtils::CreateProtectedSkSurface(fRecorder.get(), size,
135 skgpu::Protected(isProtected));
136 }
137
138 void submitAndWaitForCompletion(bool* completionMarker) override {
139 fContext->submit();
140 while (!*completionMarker) {
141 fContext->checkAsyncWorkCompletion();
142 }
143 }
144
145 skgpu::graphite::Recorder* recorder() override { return fRecorder.get(); }
146 skgpu::graphite::Context* context() override { return fContext.get(); }
147
148 protected:
149 bool init() override {
150 if (!this->setupBackendContext()) {
151 return false;
152 }
153
154 skgpu::graphite::ContextOptions contextOptions;
155 skgpu::graphite::ContextOptionsPriv contextOptionsPriv;
156 // Needed to make ManagedGraphiteTexture::ReleaseProc (w/in CreateProtectedSkSurface) work
157 contextOptionsPriv.fStoreContextRefInRecorder = true;
158 contextOptions.fOptionsPriv = &contextOptionsPriv;
159
160 fContext = skgpu::graphite::ContextFactory::MakeVulkan(fBackendContext, contextOptions);
161 if (!fContext) {
162 return false;
163 }
164
165 SkASSERT(fContext->supportsProtectedContent() == fIsProtected);
166
167 fRecorder = fContext->makeRecorder();
168 if (!fRecorder) {
169 return false;
170 }
171
172 return true;
173 }
174
175 private:
176 std::unique_ptr<skgpu::graphite::Context> fContext;
177 std::unique_ptr<skgpu::graphite::Recorder> fRecorder;
178 };
179
180 #endif // SK_GRAPHITE
181
182 std::unique_ptr<VkTestHelper> VkTestHelper::Make(skiatest::TestType testType,
183 bool isProtected) {
184 std::unique_ptr<VkTestHelper> helper;
185
186 switch (testType) {
187 #if defined(SK_GANESH)
188 case skiatest::TestType::kGanesh:
189 helper = std::make_unique<GaneshVkTestHelper>(isProtected);
190 break;
191 #endif
192 #if defined(SK_GRAPHITE)
193 case skiatest::TestType::kGraphite:
194 helper = std::make_unique<GraphiteVkTestHelper>(isProtected);
195 break;
196 #endif
197 default:
198 return nullptr;
199 }
200 if (!helper->init()) {
201 return nullptr;
202 }
203
204 return helper;
205 }
206
207 bool VkTestHelper::setupBackendContext() {
208 PFN_vkGetInstanceProcAddr instProc;
209 if (!sk_gpu_test::LoadVkLibraryAndGetProcAddrFuncs(&instProc)) {
210 return false;
211 }
212
213 fBackendContext.fInstance = VK_NULL_HANDLE;
214 fBackendContext.fDevice = VK_NULL_HANDLE;
215
216 if (!sk_gpu_test::CreateVkBackendContext(instProc,
217 &fBackendContext,
218 &fExtensions,
219 &fFeatures,
220 &fDebugMessenger,
221 nullptr,
222 sk_gpu_test::CanPresentFn(),
223 fIsProtected)) {
224 return false;
225 }
226 fDevice = fBackendContext.fDevice;
227
228 if (fDebugMessenger != VK_NULL_HANDLE) {
229 fDestroyDebugCallback = reinterpret_cast<PFN_vkDestroyDebugUtilsMessengerEXT>(
230 instProc(fBackendContext.fInstance, "vkDestroyDebugUtilsMessengerEXT"));
231 }
232 ACQUIRE_INST_VK_PROC(DestroyInstance)
233 ACQUIRE_INST_VK_PROC(DeviceWaitIdle)
234 ACQUIRE_INST_VK_PROC(DestroyDevice)
235
236 ACQUIRE_INST_VK_PROC(GetPhysicalDeviceFormatProperties)
237 ACQUIRE_INST_VK_PROC(GetPhysicalDeviceMemoryProperties)
238
239 ACQUIRE_INST_VK_PROC(GetDeviceProcAddr)
240
241 ACQUIRE_DEVICE_VK_PROC(CreateImage)
242 ACQUIRE_DEVICE_VK_PROC(DestroyImage)
243 ACQUIRE_DEVICE_VK_PROC(GetImageMemoryRequirements)
244 ACQUIRE_DEVICE_VK_PROC(AllocateMemory)
245 ACQUIRE_DEVICE_VK_PROC(FreeMemory)
246 ACQUIRE_DEVICE_VK_PROC(BindImageMemory)
247 ACQUIRE_DEVICE_VK_PROC(MapMemory)
248 ACQUIRE_DEVICE_VK_PROC(UnmapMemory)
249 ACQUIRE_DEVICE_VK_PROC(FlushMappedMemoryRanges)
250 ACQUIRE_DEVICE_VK_PROC(GetImageSubresourceLayout)
251 return true;
252 }
253
254 VkTestHelper::~VkTestHelper() {
255 fBackendContext.fMemoryAllocator.reset();
256 if (fDevice != VK_NULL_HANDLE) {
257 fVkDeviceWaitIdle(fDevice);
258 fVkDestroyDevice(fDevice, nullptr);
259 fDevice = VK_NULL_HANDLE;
260 }
261 if (fDebugMessenger != VK_NULL_HANDLE) {
262 fDestroyDebugCallback(fBackendContext.fInstance, fDebugMessenger, nullptr);
263 }
264
265 if (fBackendContext.fInstance != VK_NULL_HANDLE) {
266 fVkDestroyInstance(fBackendContext.fInstance, nullptr);
267 fBackendContext.fInstance = VK_NULL_HANDLE;
268 }
269 }
270
271 #endif // SK_VULKAN