|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#include "fml/platform/android/ndk_helpers.h" |
| 6 | + |
| 7 | +#include "fml/logging.h" |
| 8 | +#include "fml/native_library.h" |
| 9 | + |
| 10 | +#include <android/hardware_buffer.h> |
| 11 | +#include <dlfcn.h> |
| 12 | + |
| 13 | +namespace flutter { |
| 14 | + |
| 15 | +namespace { |
| 16 | + |
| 17 | +#define DECLARE_TYPES(ret, name, args) \ |
| 18 | + typedef ret(*fp_##name) args; \ |
| 19 | + ret(*_##name) args = nullptr |
| 20 | + |
| 21 | +DECLARE_TYPES(int, |
| 22 | + AHardwareBuffer_allocate, |
| 23 | + (const AHardwareBuffer_Desc* desc, AHardwareBuffer** outBuffer)); |
| 24 | +DECLARE_TYPES(int, |
| 25 | + AHardwareBuffer_isSupported, |
| 26 | + (const AHardwareBuffer_Desc* desc)); |
| 27 | +DECLARE_TYPES(AHardwareBuffer*, |
| 28 | + AHardwareBuffer_fromHardwareBuffer, |
| 29 | + (JNIEnv * env, jobject hardwareBufferObj)); |
| 30 | +DECLARE_TYPES(void, AHardwareBuffer_release, (AHardwareBuffer * buffer)); |
| 31 | +DECLARE_TYPES(void, |
| 32 | + AHardwareBuffer_describe, |
| 33 | + (AHardwareBuffer * buffer, AHardwareBuffer_Desc* desc)); |
| 34 | +DECLARE_TYPES(int, |
| 35 | + AHardwareBuffer_getId, |
| 36 | + (AHardwareBuffer * buffer, uint64_t* outId)); |
| 37 | + |
| 38 | +DECLARE_TYPES(bool, ATrace_isEnabled, (void)); |
| 39 | + |
| 40 | +DECLARE_TYPES(ASurfaceControl*, |
| 41 | + ASurfaceControl_createFromWindow, |
| 42 | + (ANativeWindow * parent, const char* debug_name)); |
| 43 | +DECLARE_TYPES(void, |
| 44 | + ASurfaceControl_release, |
| 45 | + (ASurfaceControl * surface_control)); |
| 46 | +DECLARE_TYPES(ASurfaceTransaction*, ASurfaceTransaction_create, (void)); |
| 47 | +DECLARE_TYPES(void, |
| 48 | + ASurfaceTransaction_delete, |
| 49 | + (ASurfaceTransaction * surface_transaction)); |
| 50 | +DECLARE_TYPES(void, |
| 51 | + ASurfaceTransaction_apply, |
| 52 | + (ASurfaceTransaction * surface_transaction)); |
| 53 | +DECLARE_TYPES(void, |
| 54 | + ASurfaceTransaction_setBuffer, |
| 55 | + (ASurfaceTransaction * transaction, |
| 56 | + ASurfaceControl* surface_control, |
| 57 | + AHardwareBuffer* buffer, |
| 58 | + int acquire_fence_fd)); |
| 59 | + |
| 60 | +DECLARE_TYPES(AChoreographer*, AChoreographer_getInstance, (void)); |
| 61 | +DECLARE_TYPES(void, |
| 62 | + AChoreographer_postFrameCallback, |
| 63 | + (AChoreographer * choreographer, |
| 64 | + AChoreographer_frameCallback callbackk, |
| 65 | + void* data)); |
| 66 | +DECLARE_TYPES(void, |
| 67 | + AChoreographer_postFrameCallback64, |
| 68 | + (AChoreographer * choreographer, |
| 69 | + AChoreographer_frameCallback64 callbackk, |
| 70 | + void* data)); |
| 71 | + |
| 72 | +DECLARE_TYPES(EGLClientBuffer, |
| 73 | + eglGetNativeClientBufferANDROID, |
| 74 | + (AHardwareBuffer * buffer)); |
| 75 | + |
| 76 | +#undef DECLARE_TYPES |
| 77 | + |
| 78 | +std::once_flag init_once; |
| 79 | + |
| 80 | +void InitOnceCallback() { |
| 81 | + static fml::RefPtr<fml::NativeLibrary> android = |
| 82 | + fml::NativeLibrary::Create("libandroid.so"); |
| 83 | + FML_CHECK(android.get() != nullptr); |
| 84 | + static fml::RefPtr<fml::NativeLibrary> egl = |
| 85 | + fml::NativeLibrary::Create("libEGL.so"); |
| 86 | + FML_CHECK(egl.get() != nullptr); |
| 87 | + |
| 88 | +#define LOOKUP(lib, func) \ |
| 89 | + _##func = lib->ResolveFunction<fp_##func>(#func).value_or(nullptr) |
| 90 | + |
| 91 | + LOOKUP(egl, eglGetNativeClientBufferANDROID); |
| 92 | + |
| 93 | + LOOKUP(android, AHardwareBuffer_fromHardwareBuffer); |
| 94 | + LOOKUP(android, AHardwareBuffer_release); |
| 95 | + LOOKUP(android, AHardwareBuffer_getId); |
| 96 | + LOOKUP(android, AHardwareBuffer_describe); |
| 97 | + LOOKUP(android, AHardwareBuffer_allocate); |
| 98 | + LOOKUP(android, AHardwareBuffer_isSupported); |
| 99 | + LOOKUP(android, ATrace_isEnabled); |
| 100 | + LOOKUP(android, AChoreographer_getInstance); |
| 101 | + if (_AChoreographer_getInstance) { |
| 102 | + LOOKUP(android, AChoreographer_postFrameCallback64); |
| 103 | +// See discussion at |
| 104 | +// https://github.com/flutter/engine/pull/31859#discussion_r822072987 |
| 105 | +// This method is not suitable for Flutter's use cases on 32 bit architectures, |
| 106 | +// and we should fall back to the Java based Choreographer. |
| 107 | +#if FML_ARCH_CPU_64_BITS |
| 108 | + if (!_AChoreographer_postFrameCallback64) { |
| 109 | + LOOKUP(android, AChoreographer_postFrameCallback); |
| 110 | + } |
| 111 | +#endif |
| 112 | + } |
| 113 | + |
| 114 | + LOOKUP(android, ASurfaceControl_createFromWindow); |
| 115 | + LOOKUP(android, ASurfaceControl_release); |
| 116 | + LOOKUP(android, ASurfaceTransaction_apply); |
| 117 | + LOOKUP(android, ASurfaceTransaction_create); |
| 118 | + LOOKUP(android, ASurfaceTransaction_delete); |
| 119 | + LOOKUP(android, ASurfaceTransaction_setBuffer); |
| 120 | +#undef LOOKUP |
| 121 | +} |
| 122 | + |
| 123 | +} // namespace |
| 124 | + |
| 125 | +void NDKHelpers::Init() { |
| 126 | + std::call_once(init_once, InitOnceCallback); |
| 127 | +} |
| 128 | + |
| 129 | +bool NDKHelpers::ATrace_isEnabled() { |
| 130 | + if (_ATrace_isEnabled) { |
| 131 | + return _ATrace_isEnabled(); |
| 132 | + } |
| 133 | + return false; |
| 134 | +} |
| 135 | + |
| 136 | +ChoreographerSupportStatus NDKHelpers::ChoreographerSupported() { |
| 137 | + if (_AChoreographer_postFrameCallback64) { |
| 138 | + return ChoreographerSupportStatus::kSupported64; |
| 139 | + } |
| 140 | + if (_AChoreographer_postFrameCallback) { |
| 141 | + return ChoreographerSupportStatus::kSupported32; |
| 142 | + } |
| 143 | + return ChoreographerSupportStatus::kUnsupported; |
| 144 | +} |
| 145 | + |
| 146 | +AChoreographer* NDKHelpers::AChoreographer_getInstance() { |
| 147 | + FML_CHECK(_AChoreographer_getInstance); |
| 148 | + return _AChoreographer_getInstance(); |
| 149 | +} |
| 150 | + |
| 151 | +void NDKHelpers::AChoreographer_postFrameCallback( |
| 152 | + AChoreographer* choreographer, |
| 153 | + AChoreographer_frameCallback callback, |
| 154 | + void* data) { |
| 155 | + FML_CHECK(_AChoreographer_postFrameCallback); |
| 156 | + return _AChoreographer_postFrameCallback(choreographer, callback, data); |
| 157 | +} |
| 158 | + |
| 159 | +void NDKHelpers::AChoreographer_postFrameCallback64( |
| 160 | + AChoreographer* choreographer, |
| 161 | + AChoreographer_frameCallback64 callback, |
| 162 | + void* data) { |
| 163 | + FML_CHECK(_AChoreographer_postFrameCallback64); |
| 164 | + return _AChoreographer_postFrameCallback64(choreographer, callback, data); |
| 165 | +} |
| 166 | + |
| 167 | +bool NDKHelpers::HardwareBufferSupported() { |
| 168 | + const bool r = _AHardwareBuffer_fromHardwareBuffer != nullptr; |
| 169 | + return r; |
| 170 | +} |
| 171 | + |
| 172 | +AHardwareBuffer* NDKHelpers::AHardwareBuffer_fromHardwareBuffer( |
| 173 | + JNIEnv* env, |
| 174 | + jobject hardwareBufferObj) { |
| 175 | + FML_CHECK(_AHardwareBuffer_fromHardwareBuffer != nullptr); |
| 176 | + return _AHardwareBuffer_fromHardwareBuffer(env, hardwareBufferObj); |
| 177 | +} |
| 178 | + |
| 179 | +void NDKHelpers::AHardwareBuffer_release(AHardwareBuffer* buffer) { |
| 180 | + FML_CHECK(_AHardwareBuffer_release != nullptr); |
| 181 | + _AHardwareBuffer_release(buffer); |
| 182 | +} |
| 183 | + |
| 184 | +void NDKHelpers::AHardwareBuffer_describe(AHardwareBuffer* buffer, |
| 185 | + AHardwareBuffer_Desc* desc) { |
| 186 | + FML_CHECK(_AHardwareBuffer_describe != nullptr); |
| 187 | + _AHardwareBuffer_describe(buffer, desc); |
| 188 | +} |
| 189 | + |
| 190 | +std::optional<HardwareBufferKey> NDKHelpers::AHardwareBuffer_getId( |
| 191 | + AHardwareBuffer* buffer) { |
| 192 | + if (_AHardwareBuffer_getId == nullptr) { |
| 193 | + return std::nullopt; |
| 194 | + } |
| 195 | + HardwareBufferKey outId; |
| 196 | + int result = _AHardwareBuffer_getId(buffer, &outId); |
| 197 | + if (result == 0) { |
| 198 | + return outId; |
| 199 | + } |
| 200 | + return std::nullopt; |
| 201 | +} |
| 202 | + |
| 203 | +EGLClientBuffer NDKHelpers::eglGetNativeClientBufferANDROID( |
| 204 | + AHardwareBuffer* buffer) { |
| 205 | + FML_CHECK(_eglGetNativeClientBufferANDROID != nullptr); |
| 206 | + return _eglGetNativeClientBufferANDROID(buffer); |
| 207 | +} |
| 208 | + |
| 209 | +bool NDKHelpers::SurfaceControlAndTransactionSupported() { |
| 210 | + return _ASurfaceControl_createFromWindow && _ASurfaceControl_release && |
| 211 | + _ASurfaceTransaction_create && _ASurfaceTransaction_apply && |
| 212 | + _ASurfaceTransaction_delete && _ASurfaceTransaction_setBuffer; |
| 213 | +} |
| 214 | + |
| 215 | +ASurfaceControl* NDKHelpers::ASurfaceControl_createFromWindow( |
| 216 | + ANativeWindow* parent, |
| 217 | + const char* debug_name) { |
| 218 | + FML_CHECK(_ASurfaceControl_createFromWindow); |
| 219 | + return _ASurfaceControl_createFromWindow(parent, debug_name); |
| 220 | +} |
| 221 | + |
| 222 | +void NDKHelpers::ASurfaceControl_release(ASurfaceControl* surface_control) { |
| 223 | + FML_CHECK(_ASurfaceControl_release); |
| 224 | + return _ASurfaceControl_release(surface_control); |
| 225 | +} |
| 226 | + |
| 227 | +ASurfaceTransaction* NDKHelpers::ASurfaceTransaction_create() { |
| 228 | + FML_CHECK(_ASurfaceTransaction_create); |
| 229 | + return _ASurfaceTransaction_create(); |
| 230 | +} |
| 231 | + |
| 232 | +void NDKHelpers::ASurfaceTransaction_delete( |
| 233 | + ASurfaceTransaction* surface_transaction) { |
| 234 | + FML_CHECK(_ASurfaceTransaction_delete); |
| 235 | + _ASurfaceTransaction_delete(surface_transaction); |
| 236 | +} |
| 237 | + |
| 238 | +void NDKHelpers::ASurfaceTransaction_apply( |
| 239 | + ASurfaceTransaction* surface_transaction) { |
| 240 | + FML_CHECK(_ASurfaceTransaction_apply); |
| 241 | + _ASurfaceTransaction_apply(surface_transaction); |
| 242 | +} |
| 243 | + |
| 244 | +void NDKHelpers::ASurfaceTransaction_setBuffer(ASurfaceTransaction* transaction, |
| 245 | + ASurfaceControl* surface_control, |
| 246 | + AHardwareBuffer* buffer, |
| 247 | + int acquire_fence_fd) { |
| 248 | + FML_CHECK(_ASurfaceTransaction_setBuffer); |
| 249 | + _ASurfaceTransaction_setBuffer(transaction, surface_control, buffer, |
| 250 | + acquire_fence_fd); |
| 251 | +} |
| 252 | + |
| 253 | +int NDKHelpers::AHardwareBuffer_isSupported(const AHardwareBuffer_Desc* desc) { |
| 254 | + FML_CHECK(_AHardwareBuffer_isSupported); |
| 255 | + return _AHardwareBuffer_isSupported(desc); |
| 256 | +} |
| 257 | + |
| 258 | +int NDKHelpers::AHardwareBuffer_allocate(const AHardwareBuffer_Desc* desc, |
| 259 | + AHardwareBuffer** outBuffer) { |
| 260 | + FML_CHECK(_AHardwareBuffer_allocate); |
| 261 | + return _AHardwareBuffer_allocate(desc, outBuffer); |
| 262 | +} |
| 263 | + |
| 264 | +} // namespace flutter |
0 commit comments