[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
This commit is contained in:
Leon Scroggins III
2020-02-18 12:22:17 -05:00
parent 1ed2833be3
commit 898ce7516d

View File

@@ -22,8 +22,7 @@
#include <binder/Parcel.h>
#include <renderthread/RenderProxy.h>
#include <android_runtime/android_graphics_GraphicBuffer.h>
#include <android_runtime/android_hardware_HardwareBuffer.h>
#include <private/android/AHardwareBufferHelpers.h>
#include <dlfcn.h>
#endif
#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));
}
#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,
jlong colorSpacePtr) {
#ifdef __ANDROID__ // Layoutlib does not support graphic buffer
AHardwareBuffer* buffer = android_hardware_HardwareBuffer_getNativeHardwareBuffer(env,
hardwareBuffer);
AHardwareBuffer* buffer = AHardwareBuffer_fromHardwareBuffer(env, hardwareBuffer);
sk_sp<Bitmap> bitmap = Bitmap::createFrom(buffer,
GraphicsJNI::getNativeColorSpace(colorSpacePtr));
if (!bitmap.get()) {
@@ -1140,6 +1143,14 @@ int register_android_graphics_Bitmap(JNIEnv* env)
gBitmap_nativePtr = GetFieldIDOrDie(env, gBitmap_class, "mNativePtr", "J");
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
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,
NELEM(gBitmapMethods));
}