Remove dependence on libandroid_runtime from Bitmap.cpp

The end goal is to have Bitmap.cpp use AParcel, but until that
API is extended to support this use case this is an alternative
way to isolate the graphics files from the libandroid_runtime.

Test: CtsGraphicsTestCases
Bug: 145227478
Change-Id: Ie3854fe03dec4f7b1b485295bb9a5ebba52ddb7c
diff --git a/core/jni/android_graphics_GraphicBuffer.cpp b/core/jni/android_graphics_GraphicBuffer.cpp
index b6d5089..25a7332 100644
--- a/core/jni/android_graphics_GraphicBuffer.cpp
+++ b/core/jni/android_graphics_GraphicBuffer.cpp
@@ -31,6 +31,7 @@
 #include <android/native_window.h>
 #include <android/graphics/canvas.h>
 #include <android_runtime/android_graphics_GraphicBuffer.h>
+#include <android_runtime/android_hardware_HardwareBuffer.h>
 #include <private/android/AHardwareBufferHelpers.h>
 
 #include <private/gui/ComposerService.h>
@@ -260,6 +261,16 @@
     return obj;
 }
 
+// ----------------------------------------------------------------------------
+// AHB to GraphicBuffer Converter
+// ----------------------------------------------------------------------------
+
+static jobject android_graphics_GraphicBuffer_createFromHardwareBuffer(JNIEnv* env, jobject clazz,
+                                                                       jobject hb) {
+    AHardwareBuffer* ahb = android_hardware_HardwareBuffer_getNativeHardwareBuffer(env, hb);
+    return android_graphics_GraphicBuffer_createFromAHardwareBuffer(env, ahb);
+}
+
 };
 
 using namespace android;
@@ -283,7 +294,10 @@
     { "nUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)Z",
             (void*) android_graphics_GraphicBuffer_unlockCanvasAndPost },
     { "nWrapGraphicBuffer", "(J)J",
-            (void*) android_graphics_GraphicBuffer_wrap }
+            (void*) android_graphics_GraphicBuffer_wrap },
+    { "nCreateFromHardwareBuffer",
+            "(Landroid/hardware/HardwareBuffer;)Landroid/graphics/GraphicBuffer;",
+            (void*) android_graphics_GraphicBuffer_createFromHardwareBuffer }
 };
 
 int register_android_graphics_GraphicBuffer(JNIEnv* env) {
diff --git a/graphics/java/android/graphics/Bitmap.java b/graphics/java/android/graphics/Bitmap.java
index c1e7a36..f787759 100644
--- a/graphics/java/android/graphics/Bitmap.java
+++ b/graphics/java/android/graphics/Bitmap.java
@@ -2240,7 +2240,7 @@
      */
     @UnsupportedAppUsage
     public GraphicBuffer createGraphicBufferHandle() {
-        return nativeCreateGraphicBufferHandle(mNativePtr);
+        return GraphicBuffer.createFromHardwareBuffer(getHardwareBuffer());
     }
 
     /**
@@ -2320,7 +2320,6 @@
     private static native Bitmap nativeCopyPreserveInternalConfig(long nativeBitmap);
     private static native Bitmap nativeWrapHardwareBufferBitmap(HardwareBuffer buffer,
                                                                 long nativeColorSpace);
-    private static native GraphicBuffer nativeCreateGraphicBufferHandle(long nativeBitmap);
     private static native HardwareBuffer nativeGetHardwareBuffer(long nativeBitmap);
     private static native ColorSpace nativeComputeColorSpace(long nativePtr);
     private static native void nativeSetColorSpace(long nativePtr, long nativeColorSpace);
diff --git a/graphics/java/android/graphics/GraphicBuffer.java b/graphics/java/android/graphics/GraphicBuffer.java
index 99fa5ee..04308478 100644
--- a/graphics/java/android/graphics/GraphicBuffer.java
+++ b/graphics/java/android/graphics/GraphicBuffer.java
@@ -17,6 +17,7 @@
 package android.graphics;
 
 import android.compat.annotation.UnsupportedAppUsage;
+import android.hardware.HardwareBuffer;
 import android.os.Parcel;
 import android.os.Parcelable;
 
@@ -110,6 +111,14 @@
     }
 
     /**
+     * For Bitmap until all usages are updated to AHB
+     * @hide
+     */
+    public static final GraphicBuffer createFromHardwareBuffer(HardwareBuffer buffer) {
+        return nCreateFromHardwareBuffer(buffer);
+    }
+
+    /**
      * Returns the width of this buffer in pixels.
      */
     public int getWidth() {
@@ -305,4 +314,5 @@
     private static native boolean nLockCanvas(long nativeObject, Canvas canvas, Rect dirty);
     private static native boolean nUnlockCanvasAndPost(long nativeObject, Canvas canvas);
     private static native long nWrapGraphicBuffer(long nativeObject);
+    private static native GraphicBuffer nCreateFromHardwareBuffer(HardwareBuffer buffer);
 }
diff --git a/libs/hwui/Android.bp b/libs/hwui/Android.bp
index 973f857..ac2fd98 100644
--- a/libs/hwui/Android.bp
+++ b/libs/hwui/Android.bp
@@ -311,7 +311,6 @@
 
     shared_libs: [
         "libbase",
-        "libbinder",
         "libcutils",
         "libharfbuzz_ng",
         "liblog",
@@ -341,6 +340,7 @@
             ],
             shared_libs: [
                 "libandroidfw",
+                "libbinder",
                 "libbinder_ndk",
                 "libmediandk",
                 "libnativedisplay",
diff --git a/libs/hwui/jni/Bitmap.cpp b/libs/hwui/jni/Bitmap.cpp
index 9981e89..ba66905 100755
--- a/libs/hwui/jni/Bitmap.cpp
+++ b/libs/hwui/jni/Bitmap.cpp
@@ -12,7 +12,6 @@
 #include "SkStream.h"
 #include "SkWebpEncoder.h"
 
-#include "android_os_Parcel.h"
 #include "android_nio_utils.h"
 #include "CreateJavaOutputStreamAdaptor.h"
 #include <hwui/Paint.h>
@@ -20,7 +19,7 @@
 #include <utils/Color.h>
 
 #ifdef __ANDROID__ // Layoutlib does not support graphic buffer, parcel or render thread
-#include <android_runtime/android_graphics_GraphicBuffer.h>
+#include <private/android/AHardwareBufferHelpers.h>
 #include <binder/Parcel.h>
 #include <dlfcn.h>
 #include <renderthread/RenderProxy.h>
@@ -568,6 +567,25 @@
 
 ///////////////////////////////////////////////////////////////////////////////
 
+#ifdef __ANDROID__ // Layoutlib does not support parcel
+static struct parcel_offsets_t
+{
+  jclass clazz;
+  jfieldID mNativePtr;
+} gParcelOffsets;
+
+static Parcel* parcelForJavaObject(JNIEnv* env, jobject obj) {
+    if (obj) {
+        Parcel* p = (Parcel*)env->GetLongField(obj, gParcelOffsets.mNativePtr);
+        if (p != NULL) {
+            return p;
+        }
+        jniThrowException(env, "java/lang/IllegalStateException", "Parcel has been finalized!");
+    }
+    return NULL;
+}
+#endif
+
 // This is the maximum possible size because the SkColorSpace must be
 // representable (and therefore serializable) using a matrix and numerical
 // transfer function.  If we allow more color space representations in the
@@ -581,7 +599,7 @@
         return NULL;
     }
 
-    android::Parcel* p = android::parcelForJavaObject(env, parcel);
+    android::Parcel* p = parcelForJavaObject(env, parcel);
 
     const SkColorType colorType = (SkColorType)p->readInt32();
     const SkAlphaType alphaType = (SkAlphaType)p->readInt32();
@@ -704,7 +722,7 @@
         return JNI_FALSE;
     }
 
-    android::Parcel* p = android::parcelForJavaObject(env, parcel);
+    android::Parcel* p = parcelForJavaObject(env, parcel);
     SkBitmap bitmap;
 
     auto bitmapWrapper = reinterpret_cast<BitmapWrapper*>(bitmapHandle);
@@ -1048,19 +1066,6 @@
 #endif
 }
 
-static jobject Bitmap_createGraphicBufferHandle(JNIEnv* env, jobject, jlong bitmapPtr) {
-#ifdef __ANDROID__ // Layoutlib does not support graphic buffer
-    LocalScopedBitmap bitmapHandle(bitmapPtr);
-    LOG_ALWAYS_FATAL_IF(!bitmapHandle->isHardware(),
-            "Hardware config is only supported config in Bitmap_getGraphicBuffer");
-
-    Bitmap& bitmap = bitmapHandle->bitmap();
-    return android_graphics_GraphicBuffer_createFromAHardwareBuffer(env, bitmap.hardwareBuffer());
-#else
-    return NULL;
-#endif
-}
-
 static jobject Bitmap_getHardwareBuffer(JNIEnv* env, jobject, jlong bitmapPtr) {
 #ifdef __ANDROID__ // Layoutlib does not support graphic buffer
     LocalScopedBitmap bitmapHandle(bitmapPtr);
@@ -1138,8 +1143,6 @@
         (void*)Bitmap_copyPreserveInternalConfig },
     {   "nativeWrapHardwareBufferBitmap", "(Landroid/hardware/HardwareBuffer;J)Landroid/graphics/Bitmap;",
         (void*) Bitmap_wrapHardwareBufferBitmap },
-    {   "nativeCreateGraphicBufferHandle", "(J)Landroid/graphics/GraphicBuffer;",
-        (void*) Bitmap_createGraphicBufferHandle },
     {   "nativeGetHardwareBuffer", "(J)Landroid/hardware/HardwareBuffer;",
         (void*) Bitmap_getHardwareBuffer },
     {   "nativeComputeColorSpace",  "(J)Landroid/graphics/ColorSpace;", (void*)Bitmap_computeColorSpace },
@@ -1153,6 +1156,8 @@
 
 };
 
+const char* const kParcelPathName = "android/os/Parcel";
+
 int register_android_graphics_Bitmap(JNIEnv* env)
 {
     gBitmap_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Bitmap"));
@@ -1160,7 +1165,7 @@
     gBitmap_constructorMethodID = GetMethodIDOrDie(env, gBitmap_class, "<init>", "(JIIIZ[BLandroid/graphics/NinePatch$InsetStruct;Z)V");
     gBitmap_reinitMethodID = GetMethodIDOrDie(env, gBitmap_class, "reinit", "(IIZ)V");
 
-#ifdef __ANDROID__ // Layoutlib does not support graphic buffer
+#ifdef __ANDROID__ // Layoutlib does not support graphic buffer or parcel
     void* handle_ = dlopen("libandroid.so", RTLD_NOW | RTLD_NODELETE);
     AHardwareBuffer_fromHardwareBuffer =
             (AHB_from_HB)dlsym(handle_, "AHardwareBuffer_fromHardwareBuffer");
@@ -1170,6 +1175,9 @@
     AHardwareBuffer_toHardwareBuffer = (AHB_to_HB)dlsym(handle_, "AHardwareBuffer_toHardwareBuffer");
     LOG_ALWAYS_FATAL_IF(AHardwareBuffer_toHardwareBuffer == nullptr,
                         " Failed to find required symbol AHardwareBuffer_toHardwareBuffer!");
+
+    gParcelOffsets.clazz = MakeGlobalRefOrDie(env, FindClassOrDie(env, kParcelPathName));
+    gParcelOffsets.mNativePtr = GetFieldIDOrDie(env, gParcelOffsets.clazz, "mNativePtr", "J");
 #endif
     return android::RegisterMethodsOrDie(env, "android/graphics/Bitmap", gBitmapMethods,
                                          NELEM(gBitmapMethods));