Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Clean up additional NDK helper related code #50518

Merged
merged 6 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -6290,8 +6290,6 @@ ORIGIN: ../../../flutter/shell/gpu/gpu_surface_vulkan_delegate.h + ../../../flut
ORIGIN: ../../../flutter/shell/gpu/gpu_surface_vulkan_impeller.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/gpu/gpu_surface_vulkan_impeller.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/android/AndroidManifest.xml + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/android/android_choreographer.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/android/android_choreographer.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/android/android_context_gl_impeller.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/android/android_context_gl_impeller.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/android/android_context_gl_skia.cc + ../../../flutter/LICENSE
Expand Down Expand Up @@ -9130,8 +9128,6 @@ FILE: ../../../flutter/shell/gpu/gpu_surface_vulkan_delegate.h
FILE: ../../../flutter/shell/gpu/gpu_surface_vulkan_impeller.cc
FILE: ../../../flutter/shell/gpu/gpu_surface_vulkan_impeller.h
FILE: ../../../flutter/shell/platform/android/AndroidManifest.xml
FILE: ../../../flutter/shell/platform/android/android_choreographer.cc
FILE: ../../../flutter/shell/platform/android/android_choreographer.h
FILE: ../../../flutter/shell/platform/android/android_context_gl_impeller.cc
FILE: ../../../flutter/shell/platform/android/android_context_gl_impeller.h
FILE: ../../../flutter/shell/platform/android/android_context_gl_skia.cc
Expand Down
2 changes: 0 additions & 2 deletions shell/platform/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ source_set("flutter_shell_native_src") {
visibility = [ ":*" ]

sources = [
"android_choreographer.cc",
"android_choreographer.h",
"android_context_gl_impeller.cc",
"android_context_gl_impeller.h",
"android_context_gl_skia.cc",
Expand Down
61 changes: 0 additions & 61 deletions shell/platform/android/android_choreographer.cc

This file was deleted.

30 changes: 0 additions & 30 deletions shell/platform/android/android_choreographer.h

This file was deleted.

18 changes: 2 additions & 16 deletions shell/platform/android/flutter_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "flutter/runtime/dart_vm.h"
#include "flutter/shell/common/shell.h"
#include "flutter/shell/common/switches.h"
#include "flutter/shell/platform/android/ndk_helpers.h"
#include "third_party/dart/runtime/include/dart_tools_api.h"
#include "txt/platform.h"

Expand All @@ -39,21 +40,6 @@ extern const intptr_t kPlatformStrongDillSize;

namespace {

// This is only available on API 23+, so dynamically look it up.
// This method is only called once at shell creation.
// Do this in C++ because the API is available at level 23 here, but only 29+ in
// Java.
bool IsATraceEnabled() {
auto libandroid = fml::NativeLibrary::Create("libandroid.so");
FML_CHECK(libandroid);
auto atrace_fn =
libandroid->ResolveFunction<bool (*)(void)>("ATrace_isEnabled");
if (atrace_fn) {
return atrace_fn.value()();
}
return false;
}

fml::jni::ScopedJavaGlobalRef<jclass>* g_flutter_jni_class = nullptr;

} // anonymous namespace
Expand Down Expand Up @@ -95,7 +81,7 @@ void FlutterMain::Init(JNIEnv* env,
// Turn systracing on if ATrace_isEnabled is true and the user did not already
// request systracing
if (!settings.trace_systrace) {
settings.trace_systrace = IsATraceEnabled();
settings.trace_systrace = NDKHelpers::ATrace_isEnabled();
if (settings.trace_systrace) {
__android_log_print(
ANDROID_LOG_INFO, "Flutter",
Expand Down
3 changes: 3 additions & 0 deletions shell/platform/android/library_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
// Initialize the Java VM.
fml::jni::InitJavaVM(vm);

// Registery dlsym lookups for NDK functions
flutter::NDKHelpers::Init();

JNIEnv* env = fml::jni::AttachCurrentThread();
bool result = false;

Expand Down
69 changes: 62 additions & 7 deletions shell/platform/android/ndk_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ typedef void (*fp_AHardwareBuffer_describe)(AHardwareBuffer* buffer,
typedef void (*fp_AHardwareBuffer_getId)(AHardwareBuffer* buffer,
uint64_t* outId);

typedef bool (*fp_ATrace_isEnabled)(void);

typedef AChoreographer* (*fp_AChoreographer_getInstance)(void);
typedef void (*fp_AChoreographer_postFrameCallback)(
AChoreographer* choreographer,
AChoreographer_frameCallback callbackk,
void* data);
typedef void (*fp_AChoreographer_postFrameCallback64)(
AChoreographer* choreographer,
AChoreographer_frameCallback64 callbackk,
void* data);

typedef EGLClientBuffer (*fp_eglGetNativeClientBufferANDROID)(
AHardwareBuffer* buffer);

Expand All @@ -37,6 +49,17 @@ void (*_AHardwareBuffer_describe)(AHardwareBuffer* buffer,
AHardwareBuffer_Desc* desc) = nullptr;
void (*_AHardwareBuffer_getId)(AHardwareBuffer* buffer,
uint64_t* outId) = nullptr;
bool (*_ATrace_isEnabled)() = nullptr;
AChoreographer* (*_AChoreographer_getInstance)() = nullptr;
void (*_AChoreographer_postFrameCallback)(
AChoreographer* choreographer,
AChoreographer_frameCallback callbackk,
void* data) = nullptr;
void (*_AChoreographer_postFrameCallback64)(
AChoreographer* choreographer,
AChoreographer_frameCallback64 callbackk,
void* data) = nullptr;

EGLClientBuffer (*_eglGetNativeClientBufferANDROID)(AHardwareBuffer* buffer) =
nullptr;

Expand Down Expand Up @@ -75,6 +98,28 @@ void InitOnceCallback() {
->ResolveFunction<fp_AHardwareBuffer_describe>(
"AHardwareBuffer_describe")
.value_or(nullptr);

_ATrace_isEnabled =
android->ResolveFunction<fp_ATrace_isEnabled>("ATrace_isEnabled")
.value_or(nullptr);

_AChoreographer_getInstance =
android
->ResolveFunction<fp_AChoreographer_getInstance>(
"AChoreographer_getInstance")
.value_or(nullptr);
if (_AChoreographer_getInstance) {
_AChoreographer_postFrameCallback =
android->ResolveFunction<fp_AChoreographer_postFrameCallback64>(
"AChoreographer_postFrameCallback64");
#if FML_ARCH_CPU_64_BITS
if (!_AChoreographer_postFrameCallback64) {
_AChoreographer_postFrameCallback =
android->ResolveFunction<fp_AChoreographer_postFrameCallback>(
"AChoreographer_postFrameCallback");
}
#endif
}
}

} // namespace
Expand All @@ -83,42 +128,53 @@ void NDKHelpers::Init() {
std::call_once(init_once, InitOnceCallback);
}

bool NDKHelpers::ATrace_isEnabled() {
if (_ATrace_isEnabled) {
return _ATrace_isEnabled();
}
return false;
}

ChoreographerSupportStatus NDKHelpers::ChoreographerSupported() {
if (_AChoreographer_postFrameCallback64) {
return ChoreographerSupportStatus::kSupported64;
}
if (_AChoreographer_postFrameCallback) {
return ChoreographerSupportStatus::kSupported32;
}
return ChoreographerSupportStatus::kUnsupported;
}

bool NDKHelpers::HardwareBufferSupported() {
NDKHelpers::Init();
const bool r = _AHardwareBuffer_fromHardwareBuffer != nullptr;
return r;
}

AHardwareBuffer* NDKHelpers::AHardwareBuffer_fromHardwareBuffer(
JNIEnv* env,
jobject hardwareBufferObj) {
NDKHelpers::Init();
FML_CHECK(_AHardwareBuffer_fromHardwareBuffer != nullptr);
return _AHardwareBuffer_fromHardwareBuffer(env, hardwareBufferObj);
}

void NDKHelpers::AHardwareBuffer_acquire(AHardwareBuffer* buffer) {
NDKHelpers::Init();
FML_CHECK(_AHardwareBuffer_acquire != nullptr);
_AHardwareBuffer_acquire(buffer);
}

void NDKHelpers::AHardwareBuffer_release(AHardwareBuffer* buffer) {
NDKHelpers::Init();
FML_CHECK(_AHardwareBuffer_release != nullptr);
_AHardwareBuffer_release(buffer);
}

void NDKHelpers::AHardwareBuffer_describe(AHardwareBuffer* buffer,
AHardwareBuffer_Desc* desc) {
NDKHelpers::Init();
FML_CHECK(_AHardwareBuffer_describe != nullptr);
_AHardwareBuffer_describe(buffer, desc);
}

std::optional<HardwareBufferKey> NDKHelpers::AHardwareBuffer_getId(
AHardwareBuffer* buffer) {
NDKHelpers::Init();
if (_AHardwareBuffer_getId == nullptr) {
return std::nullopt;
}
Expand All @@ -129,7 +185,6 @@ std::optional<HardwareBufferKey> NDKHelpers::AHardwareBuffer_getId(

EGLClientBuffer NDKHelpers::eglGetNativeClientBufferANDROID(
AHardwareBuffer* buffer) {
NDKHelpers::Init();
FML_CHECK(_eglGetNativeClientBufferANDROID != nullptr);
return _eglGetNativeClientBufferANDROID(buffer);
}
Expand Down
37 changes: 34 additions & 3 deletions shell/platform/android/ndk_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,44 @@

#include "flutter/impeller/toolkit/egl/egl.h"

#include <android/choreographer.h>
#include <android/hardware_buffer.h>
#include <android/surface_control.h>
#include <android/trace.h>

namespace flutter {

using HardwareBufferKey = uint64_t;

enum class ChoreographerSupportStatus {
// Unavailable, API level < 24.
kUnsupported,
// Available, but only with postFrameCallback.
kSupported32,
// Available, but only with postFrameCallback64.
kSupported64,
};

// A collection of NDK functions that are available depending on the version of
// the Android SDK we are linked with at runtime.
class NDKHelpers {
public:
// Safe to call multiple times.
// Normally called from JNI_OnLoad.
static void Init();

// API Version 23
static bool ATrace_isEnabled();

// API Version 24
static ChoreographerSupportStatus ChoreographerSupported();
static AChoreographer* AChoreographer_getInstance();
// Deprecated in 29, available since 24.
static void AChoreographer_postFrameCallback(
AChoreographer* choreographer,
AChoreographer_frameCallback callback,
void* data);

// API Version 26
static bool HardwareBufferSupported();
static AHardwareBuffer* AHardwareBuffer_fromHardwareBuffer(
Expand All @@ -32,14 +60,17 @@ class NDKHelpers {
static EGLClientBuffer eglGetNativeClientBufferANDROID(
AHardwareBuffer* buffer);

// API Version 29
static void AChoreographer_postFrameCallback64(
AChoreographer* choreographer,
AChoreographer_frameCallback64 callback,
void* data);

// API Version 31

// Returns std::nullopt on API version 26 - 30.
static std::optional<HardwareBufferKey> AHardwareBuffer_getId(
AHardwareBuffer* buffer);

private:
static void Init();
};

} // namespace flutter
Expand Down
Loading