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