Switch to using GrMtlBackendContext for GrDirectContext creation.

Makes the Metal backend more consistent with the other backends,
and allows new init parameters to be added without significantly
changing API.

Added updated sk_cf_obj because I needed some of its functionality.

Bug: skia:10804
Change-Id: I6f1dd1c03ddc4c4b702ea75eff14bc0f98ab5ad2
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/334426
Commit-Queue: Jim Van Verth <jvanverth@google.com>
Reviewed-by: Greg Daniel <egdaniel@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
diff --git a/src/gpu/GrDirectContext.cpp b/src/gpu/GrDirectContext.cpp
index 8982015..6ababe8 100644
--- a/src/gpu/GrDirectContext.cpp
+++ b/src/gpu/GrDirectContext.cpp
@@ -28,6 +28,7 @@
 #include "src/gpu/text/GrAtlasManager.h"
 #include "src/gpu/text/GrStrikeCache.h"
 #ifdef SK_METAL
+#include "include/gpu/mtl/GrMtlBackendContext.h"
 #include "src/gpu/mtl/GrMtlTrampoline.h"
 #endif
 #ifdef SK_VULKAN
@@ -958,22 +959,40 @@
 
 #ifdef SK_METAL
 /*************************************************************************************************/
-sk_sp<GrDirectContext> GrDirectContext::MakeMetal(void* device, void* queue) {
+sk_sp<GrDirectContext> GrDirectContext::MakeMetal(const GrMtlBackendContext& backendContext) {
     GrContextOptions defaultOptions;
-    return MakeMetal(device, queue, defaultOptions);
+    return MakeMetal(backendContext, defaultOptions);
 }
 
-sk_sp<GrDirectContext> GrDirectContext::MakeMetal(void* device, void* queue,
-                                                  const GrContextOptions& options) {
+sk_sp<GrDirectContext> GrDirectContext::MakeMetal(const GrMtlBackendContext& backendContext,
+                                                     const GrContextOptions& options) {
     sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kMetal, options));
 
-    direct->fGpu = GrMtlTrampoline::MakeGpu(direct.get(), options, device, queue);
+    direct->fGpu = GrMtlTrampoline::MakeGpu(backendContext, options, direct.get());
     if (!direct->init()) {
         return nullptr;
     }
 
     return direct;
 }
+
+// deprecated
+sk_sp<GrDirectContext> GrDirectContext::MakeMetal(void* device, void* queue) {
+    GrContextOptions defaultOptions;
+    return MakeMetal(device, queue, defaultOptions);
+}
+
+// deprecated
+// remove include/gpu/mtl/GrMtlBackendContext.h, above, when removed
+sk_sp<GrDirectContext> GrDirectContext::MakeMetal(void* device, void* queue,
+                                                  const GrContextOptions& options) {
+    sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kMetal, options));
+    GrMtlBackendContext backendContext = {};
+    backendContext.fDevice.reset(device);
+    backendContext.fQueue.reset(queue);
+
+    return GrDirectContext::MakeMetal(backendContext, options);
+}
 #endif
 
 #ifdef SK_DIRECT3D
diff --git a/src/gpu/mtl/GrMtlGpu.h b/src/gpu/mtl/GrMtlGpu.h
index a52baee..1a7f609 100644
--- a/src/gpu/mtl/GrMtlGpu.h
+++ b/src/gpu/mtl/GrMtlGpu.h
@@ -8,7 +8,9 @@
 #ifndef GrMtlGpu_DEFINED
 #define GrMtlGpu_DEFINED
 
+#include "include/gpu/mtl/GrMtlBackendContext.h"
 #include "include/private/SkDeque.h"
+
 #include "src/gpu/GrFinishCallbacks.h"
 #include "src/gpu/GrGpu.h"
 #include "src/gpu/GrRenderTarget.h"
@@ -27,7 +29,6 @@
 class GrMtlOpsRenderPass;
 class GrMtlTexture;
 class GrSemaphore;
-struct GrMtlBackendContext;
 class GrMtlCommandBuffer;
 
 namespace SkSL {
@@ -36,8 +37,7 @@
 
 class GrMtlGpu : public GrGpu {
 public:
-    static sk_sp<GrGpu> Make(GrDirectContext*, const GrContextOptions&,
-                             id<MTLDevice>, id<MTLCommandQueue>);
+    static sk_sp<GrGpu> Make(const GrMtlBackendContext&, const GrContextOptions&, GrDirectContext*);
     ~GrMtlGpu() override;
 
     void disconnect(DisconnectType) override;
diff --git a/src/gpu/mtl/GrMtlGpu.mm b/src/gpu/mtl/GrMtlGpu.mm
index 3bed568..0b0b441 100644
--- a/src/gpu/mtl/GrMtlGpu.mm
+++ b/src/gpu/mtl/GrMtlGpu.mm
@@ -100,9 +100,9 @@
     return false;
 }
 
-sk_sp<GrGpu> GrMtlGpu::Make(GrDirectContext* direct, const GrContextOptions& options,
-                            id<MTLDevice> device, id<MTLCommandQueue> queue) {
-    if (!device || !queue) {
+sk_sp<GrGpu> GrMtlGpu::Make(const GrMtlBackendContext& context, const GrContextOptions& options,
+                            GrDirectContext* direct) {
+    if (!context.fDevice || !context.fQueue) {
         return nullptr;
     }
     if (@available(macOS 10.14, iOS 9.0, *)) {
@@ -117,6 +117,8 @@
 #endif
     }
 
+    id<MTLDevice> device = (__bridge id<MTLDevice>)(context.fDevice.get());
+    id<MTLCommandQueue> queue = (__bridge id<MTLCommandQueue>)(context.fQueue.get());
     MTLFeatureSet featureSet;
     if (!get_feature_set(device, &featureSet)) {
         return nullptr;
diff --git a/src/gpu/mtl/GrMtlTrampoline.h b/src/gpu/mtl/GrMtlTrampoline.h
index 7ddb105..e6dffa6 100644
--- a/src/gpu/mtl/GrMtlTrampoline.h
+++ b/src/gpu/mtl/GrMtlTrampoline.h
@@ -14,6 +14,7 @@
 class GrDirectContext;
 class GrGpu;
 struct GrContextOptions;
+struct GrMtlBackendContext;
 
 /*
  * This class is used to hold functions which trampoline from the Ganesh cpp code to the GrMtl
@@ -21,8 +22,8 @@
  */
 class GrMtlTrampoline {
 public:
-    static sk_sp<GrGpu> MakeGpu(GrDirectContext*, const GrContextOptions&,
-                                void* device, void* queue);
+    static sk_sp<GrGpu> MakeGpu(const GrMtlBackendContext&, const GrContextOptions&,
+                                GrDirectContext*);
 };
 
 #endif
diff --git a/src/gpu/mtl/GrMtlTrampoline.mm b/src/gpu/mtl/GrMtlTrampoline.mm
index 46f64ae..590c7a3 100644
--- a/src/gpu/mtl/GrMtlTrampoline.mm
+++ b/src/gpu/mtl/GrMtlTrampoline.mm
@@ -13,13 +13,8 @@
 #error This file must be compiled with Arc. Use -fobjc-arc flag
 #endif
 
-sk_sp<GrGpu> GrMtlTrampoline::MakeGpu(GrDirectContext* direct,
+sk_sp<GrGpu> GrMtlTrampoline::MakeGpu(const GrMtlBackendContext& backendContext,
                                       const GrContextOptions& options,
-                                      void* device,
-                                      void* queue) {
-    return GrMtlGpu::Make(direct,
-                          options,
-                          (__bridge id<MTLDevice>)device,
-                          (__bridge id<MTLCommandQueue>)queue);
+                                      GrDirectContext* direct) {
+    return GrMtlGpu::Make(backendContext, options, direct);
 }
-