implemented onWritePixels and related tests for metal gpu backend

Bug: skia:
Change-Id: Iab8bc8611a9de8afcf0eaeb58a8ef46fdc38d4f1
Reviewed-on: https://skia-review.googlesource.com/140571
Commit-Queue: Timothy Liang <timliang@google.com>
Reviewed-by: Greg Daniel <egdaniel@google.com>
diff --git a/gn/tests.gni b/gn/tests.gni
index 6b08b82..b78cc57 100644
--- a/gn/tests.gni
+++ b/gn/tests.gni
@@ -105,6 +105,7 @@
   "$_tests/GrTestingBackendTextureUploadTest.cpp",
   "$_tests/GrTextureMipMapInvalidationTest.cpp",
   "$_tests/GrTRecorderTest.cpp",
+  "$_tests/GrUploadPixelsTests.cpp",
   "$_tests/HashTest.cpp",
   "$_tests/HighContrastFilterTest.cpp",
   "$_tests/HSVRoundTripTest.cpp",
@@ -277,7 +278,6 @@
   "$_tests/VerticesTest.cpp",
   "$_tests/VkBackendSurfaceTest.cpp",
   "$_tests/VkMakeCopyPipelineTest.cpp",
-  "$_tests/VkUploadPixelsTests.cpp",
   "$_tests/VkWrapTests.cpp",
   "$_tests/VptrTest.cpp",
   "$_tests/WindowRectanglesTest.cpp",
diff --git a/src/gpu/mtl/GrMtlGpu.h b/src/gpu/mtl/GrMtlGpu.h
index 1c25ded..054f1e6 100644
--- a/src/gpu/mtl/GrMtlGpu.h
+++ b/src/gpu/mtl/GrMtlGpu.h
@@ -113,9 +113,7 @@
                       void* buffer, size_t rowBytes) override;
 
     bool onWritePixels(GrSurface*, int left, int top, int width, int height, GrColorType,
-                       const GrMipLevel[], int) override {
-        return false;
-    }
+                       const GrMipLevel[], int mipLevelCount) override;
 
     bool onTransferPixels(GrTexture*,
                           int left, int top, int width, int height,
diff --git a/src/gpu/mtl/GrMtlGpu.mm b/src/gpu/mtl/GrMtlGpu.mm
index 892e4bc..e4c7c64 100644
--- a/src/gpu/mtl/GrMtlGpu.mm
+++ b/src/gpu/mtl/GrMtlGpu.mm
@@ -527,6 +527,25 @@
 }
 #endif // GR_TEST_UTILS
 
+bool GrMtlGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
+                             GrColorType srcColorType, const GrMipLevel texels[],
+                             int mipLevelCount) {
+    GrMtlTexture* mtlTexture = static_cast<GrMtlTexture*>(surface->asTexture());
+    if (!mtlTexture) {
+        return false;
+    }
+    if (!mipLevelCount) {
+        return false;
+    }
+#ifdef SK_DEBUG
+    for (int i = 0; i < mipLevelCount; i++) {
+        SkASSERT(texels[i].fPixels);
+    }
+#endif
+    return this->uploadToTexture(mtlTexture, left, top, width, height, srcColorType, texels,
+                                 mipLevelCount);
+}
+
 bool GrMtlGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
                             GrColorType dstColorType, void* buffer, size_t rowBytes) {
     SkASSERT(surface);
diff --git a/tests/VkUploadPixelsTests.cpp b/tests/GrUploadPixelsTests.cpp
similarity index 97%
rename from tests/VkUploadPixelsTests.cpp
rename to tests/GrUploadPixelsTests.cpp
index b13de41..88a6bc1 100644
--- a/tests/VkUploadPixelsTests.cpp
+++ b/tests/GrUploadPixelsTests.cpp
@@ -9,8 +9,6 @@
 
 #include "SkTypes.h"
 
-#if defined(SK_VULKAN)
-
 #include "GrContextFactory.h"
 #include "GrContextPriv.h"
 #include "GrSurfaceProxy.h"
@@ -19,7 +17,6 @@
 #include "SkGr.h"
 #include "Test.h"
 #include "TestUtils.h"
-#include "vk/GrVkGpu.h"
 
 using sk_gpu_test::GrContextFactory;
 
@@ -94,7 +91,7 @@
     }
 }
 
-DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkUploadPixelsTests, reporter, ctxInfo) {
+DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrUploadPixelsTests, reporter, ctxInfo) {
     // RGBA
     basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_SkColorType, false);
     basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_SkColorType, true);
@@ -103,5 +100,3 @@
     basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_SkColorType, false);
     basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_SkColorType, true);
 }
-
-#endif