/src/skia/tools/graphite/ContextFactory.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2021 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 "tools/graphite/ContextFactory.h" |
9 | | |
10 | | #include "include/gpu/graphite/Context.h" |
11 | | #include "src/gpu/graphite/Caps.h" |
12 | | #include "src/gpu/graphite/ContextPriv.h" |
13 | | |
14 | | #ifdef SK_DAWN |
15 | | #include "tools/graphite/dawn/GraphiteDawnTestContext.h" |
16 | | #endif |
17 | | #ifdef SK_METAL |
18 | | #include "tools/graphite/mtl/GraphiteMtlTestContext.h" |
19 | | #endif |
20 | | #ifdef SK_VULKAN |
21 | | #include "tools/graphite/vk/GraphiteVulkanTestContext.h" |
22 | | #endif |
23 | | |
24 | | namespace skiatest::graphite { |
25 | | |
26 | 0 | ContextFactory::OwnedContextInfo::OwnedContextInfo() = default; |
27 | | |
28 | | ContextFactory::OwnedContextInfo::OwnedContextInfo( |
29 | | skgpu::ContextType type, |
30 | | std::unique_ptr<GraphiteTestContext> testContext, |
31 | | std::unique_ptr<skgpu::graphite::Context> context) |
32 | 0 | : fType(type), fTestContext(std::move(testContext)), fContext(std::move(context)) {} |
33 | | |
34 | 0 | ContextFactory::OwnedContextInfo::~OwnedContextInfo() { |
35 | | // If we created a non-syncing Context then we have to wait for GPU work to finish before |
36 | | // destroying the Context. |
37 | 0 | if (fContext && !fContext->priv().caps()->allowCpuSync() && fContext->hasUnfinishedGpuWork()) { |
38 | 0 | fTestContext->syncedSubmit(fContext.get()); |
39 | 0 | SkASSERT(!fContext->hasUnfinishedGpuWork()); |
40 | 0 | } |
41 | 0 | } Unexecuted instantiation: skiatest::graphite::ContextFactory::OwnedContextInfo::~OwnedContextInfo() Unexecuted instantiation: skiatest::graphite::ContextFactory::OwnedContextInfo::~OwnedContextInfo() |
42 | | |
43 | 0 | ContextFactory::OwnedContextInfo::OwnedContextInfo(OwnedContextInfo&&) = default; |
44 | 0 | ContextFactory::OwnedContextInfo& ContextFactory::OwnedContextInfo::operator=(OwnedContextInfo&&) = |
45 | | default; |
46 | | |
47 | | ContextFactory::ContextFactory(const TestOptions& options) |
48 | 0 | : fOptions(options) {} |
49 | | |
50 | 0 | ContextInfo ContextFactory::AsContextInfo(const OwnedContextInfo& owned) { |
51 | 0 | return ContextInfo{owned.fTestContext.get(), owned.fContext.get()}; |
52 | 0 | } |
53 | | |
54 | 24 | ContextInfo ContextFactory::getContextInfo(skgpu::ContextType type) { |
55 | 24 | if (!skgpu::IsDawnBackend(type) && fOptions.fNeverYieldToWebGPU) { |
56 | 0 | return {}; |
57 | 0 | } |
58 | | |
59 | | // Look for an existing ContextInfo that we can re-use. |
60 | 24 | for (const OwnedContextInfo& ctxInfo : fContexts) { |
61 | 0 | if (ctxInfo.fType == type) { |
62 | 0 | return AsContextInfo(ctxInfo); |
63 | 0 | } |
64 | 0 | } |
65 | | |
66 | | // Create a new ContextInfo from this context type. |
67 | 24 | std::unique_ptr<GraphiteTestContext> testCtx; |
68 | | |
69 | 24 | switch (type) { |
70 | 0 | case skgpu::ContextType::kMetal: { |
71 | | #ifdef SK_METAL |
72 | | testCtx = graphite::MtlTestContext::Make(); |
73 | | #endif |
74 | 0 | } break; |
75 | 24 | case skgpu::ContextType::kVulkan: { |
76 | 24 | #ifdef SK_VULKAN |
77 | 24 | testCtx = graphite::VulkanTestContext::Make(); |
78 | 24 | #endif |
79 | 24 | } break; |
80 | | #ifdef SK_DAWN |
81 | | |
82 | | #define CASE(TYPE) \ |
83 | | case skgpu::ContextType::kDawn_##TYPE: \ |
84 | | testCtx = graphite::DawnTestContext::Make(wgpu::BackendType::TYPE); \ |
85 | | break; |
86 | | #else |
87 | 0 | #define CASE(TYPE) \ |
88 | 0 | case skgpu::ContextType::kDawn_##TYPE: \ |
89 | 0 | break; |
90 | 0 | #endif // SK_DAWN |
91 | 0 | CASE(D3D11) |
92 | 0 | CASE(D3D12) |
93 | 0 | CASE(Metal) |
94 | 0 | CASE(Vulkan) |
95 | 0 | CASE(OpenGL) |
96 | 0 | CASE(OpenGLES) |
97 | 0 | #undef CASE |
98 | | |
99 | 0 | default: |
100 | 0 | break; |
101 | 24 | } |
102 | | |
103 | 24 | if (!testCtx) { |
104 | 24 | return ContextInfo{}; |
105 | 24 | } |
106 | | |
107 | 0 | std::unique_ptr<skgpu::graphite::Context> context = testCtx->makeContext(fOptions); |
108 | 0 | if (!context) { |
109 | 0 | return ContextInfo{}; |
110 | 0 | } |
111 | | |
112 | 0 | fContexts.push_back({type, std::move(testCtx), std::move(context)}); |
113 | 0 | return AsContextInfo(fContexts.back()); |
114 | 0 | } |
115 | | |
116 | | } // namespace skiatest::graphite |