| Nader Jawad | a352185 | 2023-01-30 20:23:46 -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 | |
| Jerome Gaillard | da745d2 | 2024-04-16 14:06:37 +0100 | [diff] [blame] | 19 | #ifdef __ANDROID__ |
| Nader Jawad | a352185 | 2023-01-30 20:23:46 -0800 | [diff] [blame] | 20 | #include <dlfcn.h> |
| Jerome Gaillard | da745d2 | 2024-04-16 14:06:37 +0100 | [diff] [blame] | 21 | #endif |
| Nader Jawad | a352185 | 2023-01-30 20:23:46 -0800 | [diff] [blame] | 22 | #include <log/log.h> |
| 23 | |
| 24 | #ifdef __ANDROID__ |
| 25 | typedef AHardwareBuffer* (*AHB_from_HB)(JNIEnv*, jobject); |
| 26 | typedef jobject (*AHB_to_HB)(JNIEnv*, AHardwareBuffer*); |
| 27 | static AHB_from_HB fromHardwareBuffer = nullptr; |
| 28 | static AHB_to_HB toHardwareBuffer = nullptr; |
| 29 | #endif |
| 30 | |
| 31 | void android::uirenderer::HardwareBufferHelpers::init() { |
| 32 | #ifdef __ANDROID__ // Layoutlib does not support graphic buffer or parcel |
| 33 | void* handle_ = dlopen("libandroid.so", RTLD_NOW | RTLD_NODELETE); |
| 34 | fromHardwareBuffer = (AHB_from_HB)dlsym(handle_, "AHardwareBuffer_fromHardwareBuffer"); |
| 35 | LOG_ALWAYS_FATAL_IF(fromHardwareBuffer == nullptr, |
| 36 | "Failed to find required symbol AHardwareBuffer_fromHardwareBuffer!"); |
| 37 | |
| 38 | toHardwareBuffer = (AHB_to_HB)dlsym(handle_, "AHardwareBuffer_toHardwareBuffer"); |
| 39 | LOG_ALWAYS_FATAL_IF(toHardwareBuffer == nullptr, |
| 40 | " Failed to find required symbol AHardwareBuffer_toHardwareBuffer!"); |
| 41 | #endif |
| 42 | } |
| 43 | |
| 44 | AHardwareBuffer* android::uirenderer::HardwareBufferHelpers::AHardwareBuffer_fromHardwareBuffer( |
| 45 | JNIEnv* env, jobject hardwarebuffer) { |
| 46 | #ifdef __ANDROID__ |
| 47 | LOG_ALWAYS_FATAL_IF(fromHardwareBuffer == nullptr, |
| 48 | "Failed to find symbol AHardwareBuffer_fromHardwareBuffer, did you forget " |
| 49 | "to call HardwareBufferHelpers::init?"); |
| 50 | return fromHardwareBuffer(env, hardwarebuffer); |
| 51 | #else |
| 52 | ALOGE("ERROR attempting to invoke AHardwareBuffer_fromHardwareBuffer on non Android " |
| 53 | "configuration"); |
| 54 | return nullptr; |
| 55 | #endif |
| 56 | } |
| 57 | |
| 58 | jobject android::uirenderer::HardwareBufferHelpers::AHardwareBuffer_toHardwareBuffer( |
| 59 | JNIEnv* env, AHardwareBuffer* ahardwarebuffer) { |
| 60 | #ifdef __ANDROID__ |
| 61 | LOG_ALWAYS_FATAL_IF(toHardwareBuffer == nullptr, |
| 62 | "Failed to find symbol AHardwareBuffer_toHardwareBuffer, did you forget to " |
| 63 | "call HardwareBufferHelpers::init?"); |
| 64 | return toHardwareBuffer(env, ahardwarebuffer); |
| 65 | #else |
| 66 | ALOGE("ERROR attempting to invoke AHardwareBuffer_toHardwareBuffer on non Android " |
| 67 | "configuration"); |
| 68 | return nullptr; |
| 69 | #endif |
| 70 | } |