| 1 |
/* |
| 2 |
* Copyright 2017 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 |
* This file implements many functions defined in Test.h that are required to be implemented by |
| 8 |
* test runners (such as DM) to support GPU backends. |
| 9 |
*/ |
| 10 |
|
| 11 |
#include "include/gpu/GpuTypes.h" |
| 12 |
#include "tests/Test.h" |
| 13 |
|
| 14 |
#if defined(SK_GANESH) |
| 15 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 16 |
#endif |
| 17 |
|
| 18 |
#if defined(SK_GRAPHITE) |
| 19 |
#include "include/gpu/graphite/Context.h" |
| 20 |
#include "tools/graphite/ContextFactory.h" |
| 21 |
#endif |
| 22 |
|
| 23 |
#if defined(SK_GANESH) |
| 24 |
using sk_gpu_test::GrContextFactory; |
| 25 |
using sk_gpu_test::ContextInfo; |
| 26 |
#endif |
| 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 |
#if defined(SK_GANESH) |
| 36 |
return skgpu::ganesh::ContextTypeBackend(type) == GrBackendApi::kOpenGL; |
| 37 |
#else |
| 38 |
return false; |
| 39 |
#endif |
| 40 |
} |
| 41 |
|
| 42 |
bool IsVulkanContextType(skgpu::ContextType type) { |
| 43 |
#if defined(SK_GANESH) |
| 44 |
return skgpu::ganesh::ContextTypeBackend(type) == GrBackendApi::kVulkan; |
| 45 |
#elif defined(SK_GRAPHITE) |
| 46 |
return skgpu::graphite::ContextTypeBackend(type) == skgpu::BackendApi::kVulkan; |
| 47 |
#else |
| 48 |
return false; |
| 49 |
#endif |
| 50 |
} |
| 51 |
|
| 52 |
bool IsMetalContextType(skgpu::ContextType type) { |
| 53 |
#if defined(SK_GANESH) |
| 54 |
return skgpu::ganesh::ContextTypeBackend(type) == GrBackendApi::kMetal; |
| 55 |
#elif defined(SK_GRAPHITE) |
| 56 |
return skgpu::graphite::ContextTypeBackend(type) == skgpu::BackendApi::kMetal; |
| 57 |
#else |
| 58 |
return false; |
| 59 |
#endif |
| 60 |
} |
| 61 |
|
| 62 |
bool IsDirect3DContextType(skgpu::ContextType type) { |
| 63 |
#if defined(SK_GANESH) |
| 64 |
return skgpu::ganesh::ContextTypeBackend(type) == GrBackendApi::kDirect3D; |
| 65 |
#else |
| 66 |
return false; |
| 67 |
#endif |
| 68 |
} |
| 69 |
|
| 70 |
bool IsDawnContextType(skgpu::ContextType type) { |
| 71 |
#if defined(SK_GRAPHITE) |
| 72 |
return skgpu::graphite::ContextTypeBackend(type) == skgpu::BackendApi::kDawn; |
| 73 |
#else |
| 74 |
return false; |
| 75 |
#endif |
| 76 |
} |
| 77 |
|
| 78 |
bool IsMockContextType(skgpu::ContextType type) { |
| 79 |
return type == skgpu::ContextType::kMock; |
| 80 |
} |
| 81 |
|
| 82 |
#if defined(SK_GANESH) |
| 83 |
void RunWithGaneshTestContexts(GrContextTestFn* testFn, ContextTypeFilterFn* filter, |
| 84 |
Reporter* reporter, const GrContextOptions& options) { |
| 85 |
#if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC) |
| 86 |
static constexpr auto kNativeGLType = skgpu::ContextType::kGL; |
| 87 |
#else |
| 88 |
static constexpr auto kNativeGLType = skgpu::ContextType::kGLES; |
| 89 |
#endif |
| 90 |
|
| 91 |
for (int typeInt = 0; typeInt < skgpu::kContextTypeCount; ++typeInt) { |
| 92 |
skgpu::ContextType contextType = static_cast<skgpu::ContextType>(typeInt); |
| 93 |
// Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on |
| 94 |
// desktop since tests do not account for not fixing skbug.com/40033921 |
| 95 |
if (contextType == skgpu::ContextType::kGL || |
| 96 |
contextType == skgpu::ContextType::kGLES) { |
| 97 |
if (contextType != kNativeGLType) { |
| 98 |
continue; |
| 99 |
} |
| 100 |
} |
| 101 |
// We destroy the factory and its associated contexts after each test. This is due to the |
| 102 |
// fact that the command buffer sits on top of the native GL windowing (cgl, wgl, ...) but |
| 103 |
// also tracks which of its contexts is current above that API and gets tripped up if the |
| 104 |
// native windowing API is used directly outside of the command buffer code. |
| 105 |
GrContextFactory factory(options); |
| 106 |
ContextInfo ctxInfo = factory.getContextInfo(contextType); |
| 107 |
if (filter && !(*filter)(contextType)) { |
| 108 |
continue; |
| 109 |
} |
| 110 |
|
| 111 |
ReporterContext ctx(reporter, SkString(skgpu::ContextTypeName(contextType))); |
| 112 |
if (ctxInfo.directContext()) { |
| 113 |
(*testFn)(reporter, ctxInfo); |
| 114 |
// In case the test changed the current context make sure we move it back before |
| 115 |
// calling flush. |
| 116 |
ctxInfo.testContext()->makeCurrent(); |
| 117 |
// Sync so any release/finished procs get called. |
| 118 |
ctxInfo.directContext()->flushAndSubmit(GrSyncCpu::kYes); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
#endif |
| 123 |
|
| 124 |
#if defined(SK_GRAPHITE) |
| 125 |
|
| 126 |
namespace graphite { |
| 127 |
|
| 128 |
void RunWithGraphiteTestContexts(GraphiteTestFn* test, |
| 129 |
ContextTypeFilterFn* filter, |
| 130 |
Reporter* reporter, |
| 131 |
const skiatest::graphite::TestOptions& options) { |
| 132 |
ContextFactory factory(options); |
| 133 |
for (int typeInt = 0; typeInt < skgpu::kContextTypeCount; ++typeInt) { |
| 134 |
skgpu::ContextType contextType = static_cast<skgpu::ContextType>(typeInt); |
| 135 |
if (filter && !(*filter)(contextType)) { |
| 136 |
continue; |
| 137 |
} |
| 138 |
|
| 139 |
skiatest::graphite::ContextInfo ctxInfo = factory.getContextInfo(contextType); |
| 140 |
if (!ctxInfo.fContext) { |
| 141 |
continue; |
| 142 |
} |
| 143 |
|
| 144 |
ReporterContext ctx(reporter, SkString(skgpu::ContextTypeName(contextType))); |
| 145 |
(*test)(reporter, ctxInfo.fContext, ctxInfo.fTestContext, options); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
} // namespace graphite |
| 150 |
|
| 151 |
#endif // SK_GRAPHITE |
| 152 |
|
| 153 |
} // namespace skiatest |