Graphite: add MtlGpu.

Bug: skia:12466
Change-Id: I51dc73a213e672578767b41c01c487c942465964
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/452722
Commit-Queue: Jim Van Verth <jvanverth@google.com>
Reviewed-by: Robert Phillips <robertphillips@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index 1c9765c..99a66dd 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -1026,10 +1026,22 @@
 }
 
 optional("graphite") {
+  libs = []
+  frameworks = []
+
   enabled = skia_enable_graphite
   public_defines = [ "SK_GRAPHITE_ENABLED" ]
   public = skia_graphite_public
   sources = skia_graphite_sources
+  if (skia_use_metal) {
+    public_defines += [ "SK_METAL" ]
+    sources += skia_graphite_mtl_sources
+    if (skia_enable_metal_debug_info) {
+      public_defines += [ "SK_ENABLE_MTL_DEBUG_INFO" ]
+    }
+    frameworks += [ "Metal.framework" ]
+    frameworks += [ "Foundation.framework" ]
+  }
 }
 
 optional("pdf") {
diff --git a/experimental/graphite/src/mtl/MtlGpu.h b/experimental/graphite/src/mtl/MtlGpu.h
new file mode 100644
index 0000000..6ef99a5
--- /dev/null
+++ b/experimental/graphite/src/mtl/MtlGpu.h
@@ -0,0 +1,36 @@
+/*
+ * 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 skgpu_MtlGpu_DEFINED
+#define skgpu_MtlGpu_DEFINED
+
+#include "experimental/graphite/src/Gpu.h"
+
+#include "include/gpu/mtl/GrMtlBackendContext.h"
+#include "include/ports/SkCFObject.h"
+
+#import <Metal/Metal.h>
+
+namespace skgpu::mtl {
+
+class Gpu final : public skgpu::Gpu {
+public:
+    static sk_sp<skgpu::Gpu> Make(const GrMtlBackendContext&);
+    ~Gpu() override;
+
+    id<MTLDevice> device() const { return fDevice.get(); }
+
+private:
+    Gpu(sk_cfp<id<MTLDevice>>, sk_cfp<id<MTLCommandQueue>>);
+
+    sk_cfp<id<MTLDevice>> fDevice;
+    sk_cfp<id<MTLCommandQueue>> fQueue;
+};
+
+} // namespace skgpu::mtl
+
+#endif // skgpu_MtlGpu_DEFINED
diff --git a/experimental/graphite/src/mtl/MtlGpu.mm b/experimental/graphite/src/mtl/MtlGpu.mm
new file mode 100644
index 0000000..82f79dd
--- /dev/null
+++ b/experimental/graphite/src/mtl/MtlGpu.mm
@@ -0,0 +1,29 @@
+/*
+ * 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 "experimental/graphite/src/mtl/MtlGpu.h"
+
+#include "experimental/graphite/src/Caps.h"
+
+namespace skgpu::mtl {
+
+sk_sp<skgpu::Gpu> Gpu::Make(const GrMtlBackendContext& context) {
+    sk_cfp<id<MTLDevice>> device = sk_ret_cfp((id<MTLDevice>)(context.fDevice.get()));
+    sk_cfp<id<MTLCommandQueue>> queue = sk_ret_cfp((id<MTLCommandQueue>)(context.fQueue.get()));
+
+    return sk_sp<skgpu::Gpu>(new Gpu(std::move(device), std::move(queue)));
+}
+
+Gpu::Gpu(sk_cfp<id<MTLDevice>> device, sk_cfp<id<MTLCommandQueue>> queue)
+    : fDevice(std::move(device))
+    , fQueue(std::move(queue)) {
+}
+
+Gpu::~Gpu() {
+}
+
+} // namespace skgpu::mtl
diff --git a/gn/graphite.gni b/gn/graphite.gni
index 55c0157..a88c529 100644
--- a/gn/graphite.gni
+++ b/gn/graphite.gni
@@ -37,3 +37,8 @@
   "$_src/Task.cpp",
   "$_src/Task.h",
 ]
+
+skia_graphite_mtl_sources = [
+  "$_src/mtl/MtlGpu.h",
+  "$_src/mtl/MtlGpu.mm",
+]