MetalSurface: set layer.device to a ref of the system default device.

If layer.device is null, [layer nextDrawable] returns nil, so on surface
initialization we set layer.device to a dummy MTLDevice by getting a
reference of the system default device.

Using MTLCreateSystemDefaultDevice() requires linking against
Metal.framework but Chromium needs to ship on platforms that don't have
Metal. This means we can't directly link against the framework, instead
we "weakly" link against it so that if it is not present, the function
pointers are just null (instead of failing to launch).

Bug: dawn:269
Change-Id: I8719c45a19718ff79ef21f47515fe1c15b99628b
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/43112
Presubmit-Ready: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Tested-by: Corentin Wallez <cwallez@google.com>
diff --git a/src/WSI/BUILD.gn b/src/WSI/BUILD.gn
index f1feb31..6a55805 100644
--- a/src/WSI/BUILD.gn
+++ b/src/WSI/BUILD.gn
@@ -14,6 +14,15 @@
 
 import("../swiftshader.gni")
 
+config("WSI_metal_weak_framework") {
+  if (is_mac) {
+    ldflags = [
+      "-weak_framework",
+      "Metal",
+    ]
+  }
+}
+
 swiftshader_source_set("WSI") {
   sources = [
     "VkSurfaceKHR.cpp",
@@ -42,13 +51,14 @@
 
   if (is_mac) {
     sources += [
-      "MetalSurface.mm",
       "MetalSurface.h",
+      "MetalSurface.mm",
     ]
     libs = [
       "Cocoa.framework",
       "QuartzCore.framework",
     ]
+    public_configs = [ ":WSI_metal_weak_framework" ]
   }
 
   include_dirs = [
@@ -63,5 +73,5 @@
     "../Vulkan:swiftshader_libvulkan_headers",
   ]
 
-  configs = [ "../Vulkan:swiftshader_libvulkan_private_config", ]
+  configs = [ "../Vulkan:swiftshader_libvulkan_private_config" ]
 }
diff --git a/src/WSI/MetalSurface.mm b/src/WSI/MetalSurface.mm
index c31248a..f88689e 100644
--- a/src/WSI/MetalSurface.mm
+++ b/src/WSI/MetalSurface.mm
@@ -40,6 +40,7 @@
         {
             layer = (CAMetalLayer*)[obj retain];
             layer.framebufferOnly = false;
+            layer.device = MTLCreateSystemDefaultDevice();
         }
         else
         {
@@ -68,6 +69,7 @@
     {
         if(layer)
         {
+            [layer.device release];
             [layer release];
         }
         if(view)