Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 42c50042 authored by Derek Sollenberger's avatar Derek Sollenberger
Browse files

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
parent 15da7e24
Loading
Loading
Loading
Loading
+15 −1
Original line number Diff line number Diff line
@@ -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 @@ jobject android_graphics_GraphicBuffer_createFromAHardwareBuffer(JNIEnv* env,
    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 @@ static const JNINativeMethod gMethods[] = {
    { "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) {
+1 −2
Original line number Diff line number Diff line
@@ -2240,7 +2240,7 @@ public final class Bitmap implements Parcelable {
     */
    @UnsupportedAppUsage
    public GraphicBuffer createGraphicBufferHandle() {
        return nativeCreateGraphicBufferHandle(mNativePtr);
        return GraphicBuffer.createFromHardwareBuffer(getHardwareBuffer());
    }

    /**
@@ -2320,7 +2320,6 @@ public final class Bitmap implements Parcelable {
    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);
+10 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package android.graphics;

import android.compat.annotation.UnsupportedAppUsage;
import android.hardware.HardwareBuffer;
import android.os.Parcel;
import android.os.Parcelable;

@@ -109,6 +110,14 @@ public class GraphicBuffer implements Parcelable {
        return null;
    }

    /**
     * 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.
     */
@@ -305,4 +314,5 @@ public class GraphicBuffer implements Parcelable {
    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);
}
+1 −1
Original line number Diff line number Diff line
@@ -311,7 +311,6 @@ cc_defaults {

    shared_libs: [
        "libbase",
        "libbinder",
        "libcutils",
        "libharfbuzz_ng",
        "liblog",
@@ -341,6 +340,7 @@ cc_defaults {
            ],
            shared_libs: [
                "libandroidfw",
                "libbinder",
                "libbinder_ndk",
                "libmediandk",
                "libnativedisplay",
+28 −20
Original line number Diff line number Diff line
@@ -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 @@ static void Bitmap_setHasMipMap(JNIEnv* env, jobject, jlong bitmapHandle,

///////////////////////////////////////////////////////////////////////////////

#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 @@ static jobject Bitmap_createFromParcel(JNIEnv* env, jobject, jobject parcel) {
        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 @@ static jboolean Bitmap_writeToParcel(JNIEnv* env, jobject,
        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 @@ static jobject Bitmap_wrapHardwareBufferBitmap(JNIEnv* env, jobject, jobject har
#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 @@ static const JNINativeMethod gBitmapMethods[] = {
        (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 @@ static const JNINativeMethod gBitmapMethods[] = {

};

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 @@ int register_android_graphics_Bitmap(JNIEnv* env)
    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 @@ int register_android_graphics_Bitmap(JNIEnv* env)
    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));