Nader Jawad | 7612c61 | 2022-11-15 10:55:54 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "HardwareBufferHelpers.h" |
| 18 | |
| 19 | #include <dlfcn.h> |
| 20 | #include <log/log.h> |
| 21 | |
| 22 | #ifdef __ANDROID__ |
| 23 | typedef AHardwareBuffer* (*AHB_from_HB)(JNIEnv*, jobject); |
| 24 | typedef jobject (*AHB_to_HB)(JNIEnv*, AHardwareBuffer*); |
| 25 | static AHB_from_HB fromHardwareBuffer = nullptr; |
| 26 | static AHB_to_HB toHardwareBuffer = nullptr; |
| 27 | #endif |
| 28 | |
| 29 | void android::uirenderer::HardwareBufferHelpers::init() { |
| 30 | #ifdef __ANDROID__ // Layoutlib does not support graphic buffer or parcel |
| 31 | void* handle_ = dlopen("libandroid.so", RTLD_NOW | RTLD_NODELETE); |
| 32 | fromHardwareBuffer = (AHB_from_HB)dlsym(handle_, "AHardwareBuffer_fromHardwareBuffer"); |
| 33 | LOG_ALWAYS_FATAL_IF(fromHardwareBuffer == nullptr, |
| 34 | "Failed to find required symbol AHardwareBuffer_fromHardwareBuffer!"); |
| 35 | |
| 36 | toHardwareBuffer = (AHB_to_HB)dlsym(handle_, "AHardwareBuffer_toHardwareBuffer"); |
| 37 | LOG_ALWAYS_FATAL_IF(toHardwareBuffer == nullptr, |
| 38 | " Failed to find required symbol AHardwareBuffer_toHardwareBuffer!"); |
| 39 | #endif |
| 40 | } |
| 41 | |
| 42 | AHardwareBuffer* android::uirenderer::HardwareBufferHelpers::AHardwareBuffer_fromHardwareBuffer( |
| 43 | JNIEnv* env, jobject hardwarebuffer) { |
| 44 | #ifdef __ANDROID__ |
| 45 | LOG_ALWAYS_FATAL_IF(fromHardwareBuffer == nullptr, |
| 46 | "Failed to find symbol AHardwareBuffer_fromHardwareBuffer, did you forget " |
| 47 | "to call HardwareBufferHelpers::init?"); |
| 48 | return fromHardwareBuffer(env, hardwarebuffer); |
| 49 | #else |
| 50 | ALOGE("ERROR attempting to invoke AHardwareBuffer_fromHardwareBuffer on non Android " |
| 51 | "configuration"); |
| 52 | return nullptr; |
| 53 | #endif |
| 54 | } |
| 55 | |
| 56 | jobject android::uirenderer::HardwareBufferHelpers::AHardwareBuffer_toHardwareBuffer( |
| 57 | JNIEnv* env, AHardwareBuffer* ahardwarebuffer) { |
| 58 | #ifdef __ANDROID__ |
| 59 | LOG_ALWAYS_FATAL_IF(toHardwareBuffer == nullptr, |
| 60 | "Failed to find symbol AHardwareBuffer_toHardwareBuffer, did you forget to " |
| 61 | "call HardwareBufferHelpers::init?"); |
| 62 | return toHardwareBuffer(env, ahardwarebuffer); |
| 63 | #else |
| 64 | ALOGE("ERROR attempting to invoke AHardwareBuffer_toHardwareBuffer on non Android " |
| 65 | "configuration"); |
| 66 | return nullptr; |
| 67 | #endif |
| 68 | } |