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 "experimental/graphite/include/Context.h"
11
12 #ifdef SK_METAL
13 #include "tools/graphite/mtl/GraphiteMtlTestContext.h"
14 #endif
15
16 namespace skiatest::graphite {
17
ContextInfo(ContextInfo && other)18 ContextFactory::ContextInfo::ContextInfo(ContextInfo&& other)
19 : fType(other.fType)
20 , fTestContext(std::move(other.fTestContext))
21 , fContext(std::move(other.fContext)) {
22 }
23
ContextInfo(ContextFactory::ContextType type,std::unique_ptr<GraphiteTestContext> testContext,std::unique_ptr<skgpu::Context> context)24 ContextFactory::ContextInfo::ContextInfo(ContextFactory::ContextType type,
25 std::unique_ptr<GraphiteTestContext> testContext,
26 std::unique_ptr<skgpu::Context> context)
27 : fType(type)
28 , fTestContext(std::move(testContext))
29 , fContext(std::move(context)) {
30 }
31
32 ////////////////////////////////////////////////////////////////////////////////////////////////////
getContextInfo(ContextType type)33 std::tuple<GraphiteTestContext*, skgpu::Context*> ContextFactory::getContextInfo(
34 ContextType type) {
35
36 for (ContextInfo& c : fContexts) {
37 if (c.type() == type) {
38 return { c.testContext(), c.context() };
39 }
40 }
41
42 std::unique_ptr<GraphiteTestContext> testCtx;
43
44 switch (type) {
45 case ContextType::kMetal: {
46 #ifdef SK_METAL
47 testCtx = mtl::TestContext::Make();
48 #endif
49 } break;
50
51 default:
52 break;
53 }
54
55 if (!testCtx) {
56 return {};
57 }
58
59 std::unique_ptr<skgpu::Context> context = testCtx->makeContext();
60 if (!context) {
61 return {};
62 }
63
64 fContexts.push_back({ type, std::move(testCtx), std::move(context) });
65
66 return { fContexts.back().testContext(), fContexts.back().context() };
67 }
68
69 } // namespace skiatest::graphite
70