1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2021 Google LLC
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "tools/graphite/ContextFactory.h"
9cb93a386Sopenharmony_ci
10cb93a386Sopenharmony_ci#include "experimental/graphite/include/Context.h"
11cb93a386Sopenharmony_ci
12cb93a386Sopenharmony_ci#ifdef SK_METAL
13cb93a386Sopenharmony_ci#include "tools/graphite/mtl/GraphiteMtlTestContext.h"
14cb93a386Sopenharmony_ci#endif
15cb93a386Sopenharmony_ci
16cb93a386Sopenharmony_cinamespace skiatest::graphite {
17cb93a386Sopenharmony_ci
18cb93a386Sopenharmony_ciContextFactory::ContextInfo::ContextInfo(ContextInfo&& other)
19cb93a386Sopenharmony_ci    : fType(other.fType)
20cb93a386Sopenharmony_ci    , fTestContext(std::move(other.fTestContext))
21cb93a386Sopenharmony_ci    , fContext(std::move(other.fContext)) {
22cb93a386Sopenharmony_ci}
23cb93a386Sopenharmony_ci
24cb93a386Sopenharmony_ciContextFactory::ContextInfo::ContextInfo(ContextFactory::ContextType type,
25cb93a386Sopenharmony_ci                                         std::unique_ptr<GraphiteTestContext> testContext,
26cb93a386Sopenharmony_ci                                         sk_sp<skgpu::Context> context)
27cb93a386Sopenharmony_ci    : fType(type)
28cb93a386Sopenharmony_ci    , fTestContext(std::move(testContext))
29cb93a386Sopenharmony_ci    , fContext(std::move(context)) {
30cb93a386Sopenharmony_ci}
31cb93a386Sopenharmony_ci
32cb93a386Sopenharmony_cisk_sp<skgpu::Context> ContextFactory::ContextInfo::refContext() const { return fContext; }
33cb93a386Sopenharmony_ci
34cb93a386Sopenharmony_ci////////////////////////////////////////////////////////////////////////////////////////////////////
35cb93a386Sopenharmony_cistd::tuple<GraphiteTestContext*, sk_sp<skgpu::Context>> ContextFactory::getContextInfo(
36cb93a386Sopenharmony_ci        ContextType type) {
37cb93a386Sopenharmony_ci
38cb93a386Sopenharmony_ci    for (ContextInfo& c : fContexts) {
39cb93a386Sopenharmony_ci        if (c.type() == type) {
40cb93a386Sopenharmony_ci            return { c.testContext(), c.refContext() };
41cb93a386Sopenharmony_ci        }
42cb93a386Sopenharmony_ci    }
43cb93a386Sopenharmony_ci
44cb93a386Sopenharmony_ci    std::unique_ptr<GraphiteTestContext> testCtx;
45cb93a386Sopenharmony_ci
46cb93a386Sopenharmony_ci    switch (type) {
47cb93a386Sopenharmony_ci        case ContextType::kMetal: {
48cb93a386Sopenharmony_ci#ifdef SK_METAL
49cb93a386Sopenharmony_ci            testCtx = mtl::TestContext::Make();
50cb93a386Sopenharmony_ci#endif
51cb93a386Sopenharmony_ci        } break;
52cb93a386Sopenharmony_ci
53cb93a386Sopenharmony_ci        default:
54cb93a386Sopenharmony_ci            break;
55cb93a386Sopenharmony_ci    }
56cb93a386Sopenharmony_ci
57cb93a386Sopenharmony_ci    if (!testCtx) {
58cb93a386Sopenharmony_ci        return {};
59cb93a386Sopenharmony_ci    }
60cb93a386Sopenharmony_ci
61cb93a386Sopenharmony_ci    sk_sp<skgpu::Context> context = testCtx->makeContext();
62cb93a386Sopenharmony_ci    if (!context) {
63cb93a386Sopenharmony_ci        return {};
64cb93a386Sopenharmony_ci    }
65cb93a386Sopenharmony_ci
66cb93a386Sopenharmony_ci    fContexts.push_back({ type, std::move(testCtx), std::move(context) });
67cb93a386Sopenharmony_ci
68cb93a386Sopenharmony_ci    return { fContexts.back().testContext(), fContexts.back().refContext() };
69cb93a386Sopenharmony_ci}
70cb93a386Sopenharmony_ci
71cb93a386Sopenharmony_ci} // namespace skiatest::graphite
72