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

Commit 898ce751 authored by Leon Scroggins III's avatar Leon Scroggins III
Browse files

[HWUI] Remove private references to AHardwareBuffer

Bug: 135299581
Test: CtsGraphicsTestCases (BitmapTest)

Previously, we were calling
android_hardware_HardwareBuffer_getNativeHardwareBuffer, which is
private. There is already an NDK API for doing the same thing. Ideally,
we would use it directly. However, libandroid_graphics (where the
calling code resides) cannot depend on libandroid (where the NDK API
resides), because that would introduce a circular dependency. Instead,
use dlopen/dlsym to find the necessary method. A similar approach was
used in ag/9975329.

Change-Id: I660bbcd153c52c339245ce2b4ee7765c4efa4623
parent 1ed2833b
Loading
Loading
Loading
Loading
+15 −4
Original line number Original line Diff line number Diff line
@@ -22,8 +22,7 @@
#include <binder/Parcel.h>
#include <binder/Parcel.h>
#include <renderthread/RenderProxy.h>
#include <renderthread/RenderProxy.h>
#include <android_runtime/android_graphics_GraphicBuffer.h>
#include <android_runtime/android_graphics_GraphicBuffer.h>
#include <android_runtime/android_hardware_HardwareBuffer.h>
#include <dlfcn.h>
#include <private/android/AHardwareBufferHelpers.h>
#endif
#endif


#include "core_jni_helpers.h"
#include "core_jni_helpers.h"
@@ -1027,11 +1026,15 @@ static jobject Bitmap_copyPreserveInternalConfig(JNIEnv* env, jobject, jlong bit
    return createBitmap(env, bitmap.release(), getPremulBitmapCreateFlags(false));
    return createBitmap(env, bitmap.release(), getPremulBitmapCreateFlags(false));
}
}


#ifdef __ANDROID__ // Layoutlib does not support graphic buffer
typedef AHardwareBuffer* (*AHB_from_HB)(JNIEnv*, jobject);
AHB_from_HB AHardwareBuffer_fromHardwareBuffer;
#endif

static jobject Bitmap_wrapHardwareBufferBitmap(JNIEnv* env, jobject, jobject hardwareBuffer,
static jobject Bitmap_wrapHardwareBufferBitmap(JNIEnv* env, jobject, jobject hardwareBuffer,
                                               jlong colorSpacePtr) {
                                               jlong colorSpacePtr) {
#ifdef __ANDROID__ // Layoutlib does not support graphic buffer
#ifdef __ANDROID__ // Layoutlib does not support graphic buffer
    AHardwareBuffer* buffer = android_hardware_HardwareBuffer_getNativeHardwareBuffer(env,
    AHardwareBuffer* buffer = AHardwareBuffer_fromHardwareBuffer(env, hardwareBuffer);
        hardwareBuffer);
    sk_sp<Bitmap> bitmap = Bitmap::createFrom(buffer,
    sk_sp<Bitmap> bitmap = Bitmap::createFrom(buffer,
                                              GraphicsJNI::getNativeColorSpace(colorSpacePtr));
                                              GraphicsJNI::getNativeColorSpace(colorSpacePtr));
    if (!bitmap.get()) {
    if (!bitmap.get()) {
@@ -1140,6 +1143,14 @@ int register_android_graphics_Bitmap(JNIEnv* env)
    gBitmap_nativePtr = GetFieldIDOrDie(env, gBitmap_class, "mNativePtr", "J");
    gBitmap_nativePtr = GetFieldIDOrDie(env, gBitmap_class, "mNativePtr", "J");
    gBitmap_constructorMethodID = GetMethodIDOrDie(env, gBitmap_class, "<init>", "(JIIIZ[BLandroid/graphics/NinePatch$InsetStruct;Z)V");
    gBitmap_constructorMethodID = GetMethodIDOrDie(env, gBitmap_class, "<init>", "(JIIIZ[BLandroid/graphics/NinePatch$InsetStruct;Z)V");
    gBitmap_reinitMethodID = GetMethodIDOrDie(env, gBitmap_class, "reinit", "(IIZ)V");
    gBitmap_reinitMethodID = GetMethodIDOrDie(env, gBitmap_class, "reinit", "(IIZ)V");

#ifdef __ANDROID__ // Layoutlib does not support graphic buffer
    void* handle_ = dlopen("libandroid.so", RTLD_NOW | RTLD_NODELETE);
    AHardwareBuffer_fromHardwareBuffer =
            (AHB_from_HB)dlsym(handle_, "AHardwareBuffer_fromHardwareBuffer");
    LOG_ALWAYS_FATAL_IF(AHardwareBuffer_fromHardwareBuffer == nullptr,
                        "Failed to find required symbol AHardwareBuffer_fromHardwareBuffer!");
#endif
    return android::RegisterMethodsOrDie(env, "android/graphics/Bitmap", gBitmapMethods,
    return android::RegisterMethodsOrDie(env, "android/graphics/Bitmap", gBitmapMethods,
                                         NELEM(gBitmapMethods));
                                         NELEM(gBitmapMethods));
}
}