Add support for importing AHardwareBuffers into SkSurfaces.

Bug: skia:
Change-Id: I0a3b3d607e1b74787480b03520a07839c87486dc
Reviewed-on: https://skia-review.googlesource.com/c/189980
Reviewed-by: Jim Van Verth <jvanverth@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Greg Daniel <egdaniel@google.com>
diff --git a/tests/GrAHardwareBufferTest.cpp b/tests/GrAHardwareBufferTest.cpp
index c15fb1a..8fed798 100644
--- a/tests/GrAHardwareBufferTest.cpp
+++ b/tests/GrAHardwareBufferTest.cpp
@@ -207,4 +207,72 @@
     basic_draw_test_helper(reporter, context_info, kBottomLeft_GrSurfaceOrigin);
 }
 
+static void surface_draw_test_helper(skiatest::Reporter* reporter,
+                                     const sk_gpu_test::ContextInfo& info,
+                                     GrSurfaceOrigin surfaceOrigin) {
+
+    GrContext* context = info.grContext();
+    if (!context->priv().caps()->supportsAHardwareBufferImages()) {
+        return;
+    }
+
+    ///////////////////////////////////////////////////////////////////////////
+    // Setup SkBitmaps
+    ///////////////////////////////////////////////////////////////////////////
+
+    const SkBitmap srcBitmap = make_src_bitmap();
+
+    ///////////////////////////////////////////////////////////////////////////
+    // Setup AHardwareBuffer
+    ///////////////////////////////////////////////////////////////////////////
+
+    AHardwareBuffer* buffer = nullptr;
+
+    AHardwareBuffer_Desc hwbDesc;
+    hwbDesc.width = DEV_W;
+    hwbDesc.height = DEV_H;
+    hwbDesc.layers = 1;
+    hwbDesc.usage = AHARDWAREBUFFER_USAGE_CPU_READ_NEVER |
+                    AHARDWAREBUFFER_USAGE_CPU_WRITE_NEVER |
+                    AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
+                    AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT;
+
+    hwbDesc.format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
+    // The following three are not used in the allocate
+    hwbDesc.stride = 0;
+    hwbDesc.rfu0= 0;
+    hwbDesc.rfu1= 0;
+
+    if (int error = AHardwareBuffer_allocate(&hwbDesc, &buffer)) {
+        ERRORF(reporter, "Failed to allocated hardware buffer, error: %d", error);
+        cleanup_resources(buffer);
+        return;
+    }
+
+    sk_sp<SkSurface> surface = SkSurface::MakeFromAHardwareBuffer(context, buffer, surfaceOrigin,
+                                                                  nullptr, nullptr);
+    if (!surface) {
+        ERRORF(reporter, "Failed to make SkSurface.");
+        cleanup_resources(buffer);
+        return;
+    }
+
+    surface->getCanvas()->drawBitmap(srcBitmap, 0, 0);
+
+    SkBitmap readbackBitmap;
+    readbackBitmap.allocN32Pixels(DEV_W, DEV_H);
+
+    REPORTER_ASSERT(reporter, surface->readPixels(readbackBitmap, 0, 0));
+    REPORTER_ASSERT(reporter, check_read(reporter, srcBitmap, readbackBitmap));
+
+    cleanup_resources(buffer);
+}
+
+// Test to make sure we can import an AHardwareBuffer into an SkSurface and draw into it.
+DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrAHardwareBuffer_ImportAsSurface,
+                                   reporter, context_info) {
+    surface_draw_test_helper(reporter, context_info, kTopLeft_GrSurfaceOrigin);
+    surface_draw_test_helper(reporter, context_info, kBottomLeft_GrSurfaceOrigin);
+}
+
 #endif