Debian Codegraph @

tools/skqp/src/skqp_GpuTestProcs.cpp

raw
1 /*
2 * Copyright 2022 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 "tests/Test.h"
9 #include "tools/skqp/src/skqp.h"
10
11 #include "include/core/SkStream.h"
12 #include "include/gpu/ganesh/GrDirectContext.h"
13
14 #ifdef SK_VULKAN
15 #include "tools/ganesh/vk/VkTestContext.h"
16 #endif
17 #if defined(SK_GRAPHITE)
18 #include "include/gpu/graphite/Context.h"
19 #include "tools/graphite/ContextFactory.h"
20 #endif
21
22 #include <mutex>
23
24 using sk_gpu_test::ContextInfo;
25 using sk_gpu_test::GrContextFactory;
26 using sk_gpu_test::TestContext;
27
28 #ifdef SK_GL
29 using sk_gpu_test::GLTestContext;
30 #endif
31
32 namespace skiatest {
33
34 bool IsGLContextType(skgpu::ContextType type) {
35 return skgpu::ganesh::ContextTypeBackend(type) == GrBackendApi::kOpenGL;
36 }
37 bool IsVulkanContextType(skgpu::ContextType type) {
38 return skgpu::ganesh::ContextTypeBackend(type) == GrBackendApi::kVulkan;
39 }
40 bool IsMockContextType(skgpu::ContextType type) {
41 return type == skgpu::ContextType::kMock;
42 }
43
44 // These are not supported
45 bool IsMetalContextType(skgpu::ContextType type) { return false; }
46 bool IsDirect3DContextType(skgpu::ContextType type) { return false; }
47 bool IsDawnContextType(skgpu::ContextType type) { return false; }
48
49 static bool vk_has_physical_devices() {
50 static bool supported = false;
51 #ifdef SK_VULKAN
52 static std::once_flag flag;
53 std::call_once(flag, []() {
54 // We could create a VkInstance and call vkEnumeratePhysicalDevices devices directly, but
55 // CreatePlatformVkTestContext is already configured to do that and will return nullptr if
56 // there are no available devices.
57 std::unique_ptr<TestContext> testCtx(sk_gpu_test::CreatePlatformVkTestContext(nullptr));
58 if (testCtx) {
59 supported = true;
60 }
61 });
62 #endif
63 return supported;
64 }
65
66 #if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC)
67 // Used for testing on desktop machines.
68 static constexpr auto kNativeGLType = skgpu::ContextType::kGL;
69 #else
70 static constexpr auto kNativeGLType = skgpu::ContextType::kGLES;
71 #endif
72
73 #ifdef SK_BUILD_FOR_ANDROID
74 static_assert(kNativeGLType == skgpu::ContextType::kGLES, "CTS requires GLES");
75 #endif
76
77 static bool skip_context(skgpu::ContextType contextType) {
78 // Use "native" instead of explicitly trying both OpenGL and OpenGL ES.
79 if (contextType == skgpu::ContextType::kGL || contextType == skgpu::ContextType::kGLES) {
80 if (contextType != kNativeGLType) {
81 return true;
82 }
83 }
84
85 // The Android CDD (https://source.android.com/compatibility/12/android-12-cdd.pdf) does not
86 // require Vulkan, but if it enumerates at least one VkPhysicalDevice then it is expected that
87 // Vulkan is supported
88 if (contextType == skgpu::ContextType::kVulkan && !vk_has_physical_devices()) {
89 return true;
90 }
91 return false;
92 }
93
94 void RunWithGaneshTestContexts(GrContextTestFn* testFn,
95 ContextTypeFilterFn* filter,
96 Reporter* reporter,
97 const GrContextOptions& options) {
98 for (int typeInt = 0; typeInt < skgpu::kContextTypeCount; ++typeInt) {
99 skgpu::ContextType contextType = static_cast<skgpu::ContextType>(typeInt);
100 if (skip_context(contextType)) {
101 continue;
102 }
103
104 // The logic below is intended to mirror the behavior in DMGpuTestProcs.cpp
105 if (filter && !(*filter)(contextType)) {
106 continue;
107 }
108
109 sk_gpu_test::GrContextFactory factory(options);
110 sk_gpu_test::ContextInfo ctxInfo = factory.getContextInfo(contextType);
111
112 ReporterContext ctx(reporter, SkString(skgpu::ContextTypeName(contextType)));
113 if (ctxInfo.directContext()) {
114 ctxInfo.testContext()->makeCurrent();
115 (*testFn)(reporter, ctxInfo);
116 // Sync so any release/finished procs get called.
117 ctxInfo.directContext()->flushAndSubmit(GrSyncCpu::kYes);
118 }
119 }
120 }
121
122 #if defined(SK_GRAPHITE)
123
124 namespace graphite {
125
126 void RunWithGraphiteTestContexts(GraphiteTestFn* test,
127 ContextTypeFilterFn* filter,
128 Reporter* reporter,
129 const TestOptions& options) {
130 ContextFactory factory(options);
131 for (int typeInt = 0; typeInt < skgpu::kContextTypeCount; ++typeInt) {
132 skgpu::ContextType contextType = static_cast<skgpu::ContextType>(typeInt);
133 if (skip_context(contextType)) {
134 continue;
135 }
136
137 // The logic below is intended to mirror the behavior in DMGpuTestProcs.cpp
138 if (filter && !(*filter)(contextType)) {
139 continue;
140 }
141
142 skiatest::graphite::ContextInfo ctxInfo = factory.getContextInfo(contextType);
143 if (!ctxInfo.fContext) {
144 continue;
145 }
146
147 ReporterContext ctx(reporter, SkString(skgpu::ContextTypeName(contextType)));
148 (*test)(reporter, ctxInfo.fContext, ctxInfo.fTestContext, options);
149 }
150 }
151
152 } // namespace graphite
153
154 #endif // SK_GRAPHITE
155
156 } // namespace skiatest
157
158 void SkQP::printBackendInfo(const char* dstPath) {
159 #ifdef SK_ENABLE_DUMP_GPU
160 SkFILEWStream out(dstPath);
161 out.writeText("[\n");
162
163 skgpu::ContextType contextsToDump[] = {skiatest::kNativeGLType, skgpu::ContextType::kVulkan};
164
165 for (auto contextType : contextsToDump) {
166 std::unique_ptr<TestContext> testCtx;
167 switch (contextType) {
168 #ifdef SK_GL
169 case skgpu::ContextType::kGL:
170 testCtx.reset(sk_gpu_test::CreatePlatformGLTestContext(kGL_GrGLStandard, nullptr));
171 break;
172 case skgpu::ContextType::kGLES:
173 testCtx.reset(
174 sk_gpu_test::CreatePlatformGLTestContext(kGLES_GrGLStandard, nullptr));
175 break;
176 #endif
177 #ifdef SK_VULKAN
178 case skgpu::ContextType::kVulkan:
179 testCtx.reset(sk_gpu_test::CreatePlatformVkTestContext(nullptr));
180 break;
181 #endif
182 default: {
183 }
184 }
185
186 if (testCtx) {
187 GrContextOptions options;
188 testCtx->makeCurrent();
189 if (sk_sp<GrDirectContext> ctx = testCtx->makeContext(options)) {
190 SkString info = ctx->dump();
191 // remove null
192 out.write(info.c_str(), info.size());
193 out.writeText(",\n");
194 }
195 }
196 }
197 out.writeText("]\n");
198 #endif
199 }