• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,sk_sp<skgpu::Context> context)24 ContextFactory::ContextInfo::ContextInfo(ContextFactory::ContextType type,
25                                          std::unique_ptr<GraphiteTestContext> testContext,
26                                          sk_sp<skgpu::Context> context)
27     : fType(type)
28     , fTestContext(std::move(testContext))
29     , fContext(std::move(context)) {
30 }
31 
refContext() const32 sk_sp<skgpu::Context> ContextFactory::ContextInfo::refContext() const { return fContext; }
33 
34 ////////////////////////////////////////////////////////////////////////////////////////////////////
getContextInfo(ContextType type)35 std::tuple<GraphiteTestContext*, sk_sp<skgpu::Context>> ContextFactory::getContextInfo(
36         ContextType type) {
37 
38     for (ContextInfo& c : fContexts) {
39         if (c.type() == type) {
40             return { c.testContext(), c.refContext() };
41         }
42     }
43 
44     std::unique_ptr<GraphiteTestContext> testCtx;
45 
46     switch (type) {
47         case ContextType::kMetal: {
48 #ifdef SK_METAL
49             testCtx = mtl::TestContext::Make();
50 #endif
51         } break;
52 
53         default:
54             break;
55     }
56 
57     if (!testCtx) {
58         return {};
59     }
60 
61     sk_sp<skgpu::Context> context = testCtx->makeContext();
62     if (!context) {
63         return {};
64     }
65 
66     fContexts.push_back({ type, std::move(testCtx), std::move(context) });
67 
68     return { fContexts.back().testContext(), fContexts.back().refContext() };
69 }
70 
71 } // namespace skiatest::graphite
72