diff --git a/gfx/layers/AndroidHardwareBuffer.cpp b/gfx/layers/AndroidHardwareBuffer.cpp index d7fd883ce2cf..020dfaf892dd 100644 --- a/gfx/layers/AndroidHardwareBuffer.cpp +++ b/gfx/layers/AndroidHardwareBuffer.cpp @@ -6,6 +6,8 @@ #include "AndroidHardwareBuffer.h" +#include + #include "mozilla/gfx/2D.h" #include "mozilla/gfx/gfxVars.h" #include "mozilla/layers/ImageBridgeChild.h" @@ -53,22 +55,41 @@ void AndroidHardwareBufferApi::Shutdown() { sInstance = nullptr; } AndroidHardwareBufferApi::AndroidHardwareBufferApi() {} bool AndroidHardwareBufferApi::Load() { - if (__builtin_available(android 26, *)) { - mAHardwareBuffer_allocate = AHardwareBuffer_allocate; // API 26 - mAHardwareBuffer_acquire = AHardwareBuffer_acquire; // API 26 - mAHardwareBuffer_release = AHardwareBuffer_release; // API 26 - mAHardwareBuffer_describe = AHardwareBuffer_describe; // API 26 - mAHardwareBuffer_lock = AHardwareBuffer_lock; // API 26 - mAHardwareBuffer_unlock = AHardwareBuffer_unlock; // API 26 - mAHardwareBuffer_sendHandleToUnixSocket = - AHardwareBuffer_sendHandleToUnixSocket; // API 26 - mAHardwareBuffer_recvHandleFromUnixSocket = - AHardwareBuffer_recvHandleFromUnixSocket; // API 26 - return true; - } else { + void* handle = dlopen("libandroid.so", RTLD_LAZY | RTLD_LOCAL); + MOZ_ASSERT(handle); + if (!handle) { + gfxCriticalNote << "Failed to load libandroid.so"; + return false; + } + + mAHardwareBuffer_allocate = + (_AHardwareBuffer_allocate)dlsym(handle, "AHardwareBuffer_allocate"); + mAHardwareBuffer_acquire = + (_AHardwareBuffer_acquire)dlsym(handle, "AHardwareBuffer_acquire"); + mAHardwareBuffer_release = + (_AHardwareBuffer_release)dlsym(handle, "AHardwareBuffer_release"); + mAHardwareBuffer_describe = + (_AHardwareBuffer_describe)dlsym(handle, "AHardwareBuffer_describe"); + mAHardwareBuffer_lock = + (_AHardwareBuffer_lock)dlsym(handle, "AHardwareBuffer_lock"); + mAHardwareBuffer_unlock = + (_AHardwareBuffer_unlock)dlsym(handle, "AHardwareBuffer_unlock"); + mAHardwareBuffer_sendHandleToUnixSocket = + (_AHardwareBuffer_sendHandleToUnixSocket)dlsym( + handle, "AHardwareBuffer_sendHandleToUnixSocket"); + mAHardwareBuffer_recvHandleFromUnixSocket = + (_AHardwareBuffer_recvHandleFromUnixSocket)dlsym( + handle, "AHardwareBuffer_recvHandleFromUnixSocket"); + + if (!mAHardwareBuffer_allocate || !mAHardwareBuffer_acquire || + !mAHardwareBuffer_release || !mAHardwareBuffer_describe || + !mAHardwareBuffer_lock || !mAHardwareBuffer_unlock || + !mAHardwareBuffer_sendHandleToUnixSocket || + !mAHardwareBuffer_recvHandleFromUnixSocket) { gfxCriticalNote << "Failed to load AHardwareBuffer"; return false; } + return true; } void AndroidHardwareBufferApi::Allocate(const AHardwareBuffer_Desc* aDesc, diff --git a/gfx/thebes/AndroidSystemFontIterator.cpp b/gfx/thebes/AndroidSystemFontIterator.cpp index 2b13088ec103..4590df4dbafe 100644 --- a/gfx/thebes/AndroidSystemFontIterator.cpp +++ b/gfx/thebes/AndroidSystemFontIterator.cpp @@ -8,7 +8,7 @@ #include "mozilla/Assertions.h" #include "nsDebug.h" -#include +#include namespace mozilla { @@ -36,14 +36,26 @@ AndroidSystemFontIterator::~AndroidSystemFontIterator() { bool AndroidSystemFontIterator::Init() { if (!sSystemFontIterator_open) { - if (__builtin_available(android 29, *)) { - sSystemFontIterator_open = ASystemFontIterator_open; - sSystemFontIterator_next = ASystemFontIterator_next; - sSystemFontIterator_close = ASystemFontIterator_close; - AndroidFont::sFont_getFontFilePath = AFont_getFontFilePath; - AndroidFont::sFont_close = AFont_close; - } else { - return NS_WARN_IF(false); + void* handle = dlopen("libandroid.so", RTLD_LAZY | RTLD_LOCAL); + MOZ_ASSERT(handle); + + sSystemFontIterator_open = + (_ASystemFontIterator_open)dlsym(handle, "ASystemFontIterator_open"); + sSystemFontIterator_next = + (_ASystemFontIterator_next)dlsym(handle, "ASystemFontIterator_next"); + sSystemFontIterator_close = + (_ASystemFontIterator_close)dlsym(handle, "ASystemFontIterator_close"); + AndroidFont::sFont_getFontFilePath = + (_AFont_getFontFilePath)dlsym(handle, "AFont_getFontFilePath"); + AndroidFont::sFont_close = (_AFont_close)dlsym(handle, "AFont_close"); + + if (NS_WARN_IF(!sSystemFontIterator_open) || + NS_WARN_IF(!sSystemFontIterator_next) || + NS_WARN_IF(!sSystemFontIterator_close) || + NS_WARN_IF(!AndroidFont::sFont_getFontFilePath) || + NS_WARN_IF(!AndroidFont::sFont_close)) { + sSystemFontIterator_open = nullptr; + return false; } } @@ -61,7 +73,7 @@ Maybe AndroidSystemFontIterator::Next() { return Nothing(); } - AFont* font = sSystemFontIterator_next(mIterator); + void* font = sSystemFontIterator_next(mIterator); if (!font) { sSystemFontIterator_close(mIterator); mIterator = nullptr; diff --git a/gfx/thebes/AndroidSystemFontIterator.h b/gfx/thebes/AndroidSystemFontIterator.h index 6eb989f89a50..f1507585129b 100644 --- a/gfx/thebes/AndroidSystemFontIterator.h +++ b/gfx/thebes/AndroidSystemFontIterator.h @@ -7,23 +7,19 @@ #define AndroidSystemFontIterator_h__ #include "mozilla/Maybe.h" -#include -#include namespace mozilla { -typedef ASystemFontIterator* _Nullable (*_ASystemFontIterator_open)(); -typedef AFont* _Nullable (*_ASystemFontIterator_next)( - ASystemFontIterator* _Nonnull iterator); -typedef void (*_ASystemFontIterator_close)( - ASystemFontIterator* _Nullable iterator); -typedef const char* _Nonnull (*_AFont_getFontFilePath)( - const AFont* _Nonnull font); -typedef void (*_AFont_close)(AFont* _Nullable font); +typedef void* (*_ASystemFontIterator_open)(); +typedef void* (*_ASystemFontIterator_next)(void*); +typedef void (*_ASystemFontIterator_close)(void*); + +typedef const char* (*_AFont_getFontFilePath)(const void*); +typedef void (*_AFont_close)(void*); class AndroidFont final { public: - explicit AndroidFont(AFont* _Nullable aFont) : mFont(aFont) {}; + explicit AndroidFont(void* aFont) : mFont(aFont) {}; AndroidFont() = delete; AndroidFont(AndroidFont&) = delete; @@ -35,13 +31,13 @@ class AndroidFont final { ~AndroidFont(); - const char* _Nullable GetFontFilePath(); + const char* GetFontFilePath(); private: - AFont* _Nullable mFont; + void* mFont; - static _AFont_getFontFilePath _Nullable sFont_getFontFilePath; - static _AFont_close _Nullable sFont_close; + static _AFont_getFontFilePath sFont_getFontFilePath; + static _AFont_close sFont_close; friend class AndroidSystemFontIterator; }; @@ -57,11 +53,11 @@ class AndroidSystemFontIterator final { Maybe Next(); private: - ASystemFontIterator* _Nullable mIterator = nullptr; + void* mIterator = nullptr; - static _ASystemFontIterator_open _Nullable sSystemFontIterator_open; - static _ASystemFontIterator_next _Nullable sSystemFontIterator_next; - static _ASystemFontIterator_close _Nullable sSystemFontIterator_close; + static _ASystemFontIterator_open sSystemFontIterator_open; + static _ASystemFontIterator_next sSystemFontIterator_next; + static _ASystemFontIterator_close sSystemFontIterator_close; }; } // namespace mozilla diff --git a/hal/android/AndroidPerformanceHintManager.cpp b/hal/android/AndroidPerformanceHintManager.cpp index 743ee602feb5..a9507a82724b 100644 --- a/hal/android/AndroidPerformanceHintManager.cpp +++ b/hal/android/AndroidPerformanceHintManager.cpp @@ -10,18 +10,26 @@ #include "AndroidBuild.h" +#include #include #include #include -#include - typedef struct APerformanceHintManager APerformanceHintManager; typedef struct APerformanceHintSession APerformanceHintSession; namespace mozilla { namespace hal_impl { +#define LOAD_FN(api, lib, name) \ + do { \ + api->m##name = reinterpret_cast(dlsym(handle, #name)); \ + if (!api->m##name) { \ + HAL_ERR("Failed to load %s", #name); \ + return nullptr; \ + } \ + } while (false) + class PerformanceHintManagerApi final { public: static PerformanceHintManagerApi* Get() { @@ -65,21 +73,20 @@ class PerformanceHintManagerApi final { return nullptr; } - if (__builtin_available(android 33, *)) { - auto api = WrapUnique(new PerformanceHintManagerApi()); - api->mAPerformanceHint_getManager = ::APerformanceHint_getManager; - api->mAPerformanceHint_createSession = ::APerformanceHint_createSession; - api->mAPerformanceHint_updateTargetWorkDuration = - ::APerformanceHint_updateTargetWorkDuration; - api->mAPerformanceHint_reportActualWorkDuration = - ::APerformanceHint_reportActualWorkDuration; - api->mAPerformanceHint_closeSession = ::APerformanceHint_closeSession; - - return api; - } else { - HAL_ERR("Failed to load PerformanceHintManager symbols"); + void* const handle = dlopen("libandroid.so", RTLD_LAZY | RTLD_LOCAL); + if (!handle) { + HAL_ERR("Failed to open libandroid.so"); return nullptr; } + + auto api = WrapUnique(new PerformanceHintManagerApi()); + LOAD_FN(api, handle, APerformanceHint_getManager); + LOAD_FN(api, handle, APerformanceHint_createSession); + LOAD_FN(api, handle, APerformanceHint_updateTargetWorkDuration); + LOAD_FN(api, handle, APerformanceHint_reportActualWorkDuration); + LOAD_FN(api, handle, APerformanceHint_closeSession); + + return api; } using FnAPerformanceHint_getManager = APerformanceHintManager* (*)(); diff --git a/mozglue/android/Ashmem.cpp b/mozglue/android/Ashmem.cpp index 24fa31855f64..edfeb495dd9c 100644 --- a/mozglue/android/Ashmem.cpp +++ b/mozglue/android/Ashmem.cpp @@ -3,6 +3,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include +#include #include #include #include @@ -11,16 +12,22 @@ #include #include -#include - #include "Ashmem.h" namespace mozilla { namespace android { +static void* libhandle() { + static void* handle = dlopen("libandroid.so", RTLD_LAZY | RTLD_LOCAL); + return handle; +} + int ashmem_create(const char* name, size_t size) { - if (__builtin_available(android 26, *)) { - return ASharedMemory_create(name, size); + static auto fCreate = + (int (*)(const char*, size_t))dlsym(libhandle(), "ASharedMemory_create"); + + if (fCreate) { + return fCreate(name, size); } int fd = open("/" ASHMEM_NAME_DEF, O_RDWR); @@ -43,16 +50,20 @@ int ashmem_create(const char* name, size_t size) { } size_t ashmem_getSize(int fd) { - if (__builtin_available(android 26, *)) { - return ASharedMemory_getSize(fd); + static auto fGetSize = + (size_t(*)(int))dlsym(libhandle(), "ASharedMemory_getSize"); + if (fGetSize) { + return fGetSize(fd); } return (size_t)ioctl(fd, ASHMEM_GET_SIZE, nullptr); } int ashmem_setProt(int fd, int prot) { - if (__builtin_available(android 26, *)) { - return ASharedMemory_setProt(fd, prot); + static auto fSetProt = + (int (*)(int, int))dlsym(libhandle(), "ASharedMemory_setProt"); + if (fSetProt) { + return fSetProt(fd, prot); } return ioctl(fd, ASHMEM_SET_PROT_MASK, prot);