[graphite] Add ContextFactory

Bug: skia:12466
Change-Id: I3299940af72cffde3904cf5f6262955807d6d1bc
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/453637
Reviewed-by: Jim Van Verth <jvanverth@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/tools/graphite/ContextFactory.cpp b/tools/graphite/ContextFactory.cpp
new file mode 100644
index 0000000..0bc2c54
--- /dev/null
+++ b/tools/graphite/ContextFactory.cpp
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2021 Google LLC
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "tools/graphite/ContextFactory.h"
+
+#include "experimental/graphite/include/Context.h"
+
+#ifdef SK_METAL
+#include "tools/graphite/mtl/GraphiteMtlTestContext.h"
+#endif
+
+namespace sk_graphite_test {
+
+ std::tuple<GraphiteTestContext*, sk_sp<skgpu::Context>> ContextFactory::getContextInfo(
+        ContextType type) {
+
+    for (ContextInfo& c : fContexts) {
+        if (c.type() == type) {
+            return { c.testContext(), c.refContext() };
+        }
+    }
+
+    std::unique_ptr<GraphiteTestContext> testCtx;
+
+    switch (type) {
+        case ContextType::kMetal: {
+#ifdef SK_METAL
+            testCtx = mtl::TestContext::Make();
+#endif
+        } break;
+
+        default:
+            break;
+    }
+
+    if (!testCtx) {
+        return {};
+    }
+
+    sk_sp<skgpu::Context> context = testCtx->makeContext();
+    if (!context) {
+        return {};
+    }
+
+    fContexts.push_back({ type, std::move(testCtx), std::move(context) });
+
+    return { fContexts.back().testContext(), fContexts.back().refContext() };
+}
+
+} // namespace sk_graphite_test
diff --git a/tools/graphite/ContextFactory.h b/tools/graphite/ContextFactory.h
new file mode 100644
index 0000000..46f0121
--- /dev/null
+++ b/tools/graphite/ContextFactory.h
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2021 Google LLC
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef sk_graphite_test_ContextFactory_DEFINED
+#define sk_graphite_test_ContextFactory_DEFINED
+
+#include <vector>
+#include "experimental/graphite/include/GraphiteTypes.h"
+#include "include/core/SkRefCnt.h"
+#include "tools/graphite/GraphiteTestContext.h"
+
+namespace skgpu {
+    class Context;
+};
+
+namespace sk_graphite_test {
+
+class ContextFactory {
+public:
+    enum class ContextType {
+        kMetal,
+        kMock,
+    };
+
+    class ContextInfo {
+    public:
+        ContextInfo() = default;
+        ContextInfo(ContextInfo&& other)
+           : fType(other.fType)
+           , fTestContext(std::move(other.fTestContext))
+           , fContext(std::move(other.fContext)) {
+        }
+
+        ~ContextInfo() = default;
+
+        ContextFactory::ContextType type() const { return fType; }
+
+        skgpu::Context* context() const { return fContext.get(); }
+        sk_sp<skgpu::Context> refContext() const { return fContext; }
+        GraphiteTestContext* testContext() const { return fTestContext.get(); }
+
+    private:
+        friend class ContextFactory; // for ctor
+
+        ContextInfo(ContextFactory::ContextType type,
+                    std::unique_ptr<GraphiteTestContext> testContext,
+                    sk_sp<skgpu::Context> context)
+            : fType(type)
+            , fTestContext(std::move(testContext))
+            , fContext(std::move(context)) {
+        }
+
+        ContextType                          fType = ContextType::kMock;
+        std::unique_ptr<GraphiteTestContext> fTestContext;
+        sk_sp<skgpu::Context>                fContext;
+    };
+
+    ContextFactory() = default;
+    ContextFactory(const ContextFactory&) = delete;
+    ContextFactory& operator=(const ContextFactory&) = delete;
+
+    ~ContextFactory() = default;
+
+    std::tuple<GraphiteTestContext*, sk_sp<skgpu::Context>> getContextInfo(ContextType);
+
+private:
+    std::vector<ContextInfo> fContexts;
+};
+
+} // namespace sk_graphite_test
+
+#endif // sk_graphite_test_ContextFactory_DEFINED
diff --git a/tools/graphite/GraphiteTestContext.h b/tools/graphite/GraphiteTestContext.h
index 789e30f..0d97fb7 100644
--- a/tools/graphite/GraphiteTestContext.h
+++ b/tools/graphite/GraphiteTestContext.h
@@ -5,8 +5,8 @@
  * found in the LICENSE file.
  */
 
-#ifndef skgpu_GraphiteTestContext_DEFINED
-#define skgpu_GraphiteTestContext_DEFINED
+#ifndef sk_graphite_test_GraphiteTestContext_DEFINED
+#define sk_graphite_test_GraphiteTestContext_DEFINED
 
 #include "experimental/graphite/include/GraphiteTypes.h"
 #include "include/core/SkRefCnt.h"
@@ -37,4 +37,4 @@
 
 }  // namespace sk_graphite_test
 
-#endif // skgpu_GraphiteTestContext_DEFINED
+#endif // sk_graphite_test_GraphiteTestContext_DEFINED
diff --git a/tools/graphite/mtl/GraphiteMtlTestContext.h b/tools/graphite/mtl/GraphiteMtlTestContext.h
index f58a470..ea06ad9 100644
--- a/tools/graphite/mtl/GraphiteMtlTestContext.h
+++ b/tools/graphite/mtl/GraphiteMtlTestContext.h
@@ -20,7 +20,7 @@
 public:
     ~TestContext() override {}
 
-    static GraphiteTestContext* Make();
+    static std::unique_ptr<GraphiteTestContext> Make();
 
     skgpu::BackendApi backend() override { return skgpu::BackendApi::kMetal; }
 
diff --git a/tools/graphite/mtl/MtlTestContext.mm b/tools/graphite/mtl/MtlTestContext.mm
index 9ec00b3..ea8f263 100644
--- a/tools/graphite/mtl/MtlTestContext.mm
+++ b/tools/graphite/mtl/MtlTestContext.mm
@@ -16,7 +16,7 @@
 
 namespace sk_graphite_test::mtl {
 
-GraphiteTestContext* TestContext::Make() {
+std::unique_ptr<GraphiteTestContext> TestContext::Make() {
     sk_cfp<id<MTLDevice>> device;
 #ifdef SK_BUILD_FOR_MAC
     sk_cfp<NSArray<id <MTLDevice>>*> availableDevices(MTLCopyAllDevices());
@@ -44,7 +44,7 @@
     backendContext.fDevice.retain(device.get());
     backendContext.fQueue.retain([*device newCommandQueue]);
 
-    return new TestContext(backendContext);
+    return std::unique_ptr<GraphiteTestContext>(new TestContext(backendContext));
 }
 
 sk_sp<skgpu::Context> TestContext::makeContext() {