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

Commit e08b6c8

Browse files
authored
Clean up additional NDK helper related code (#50518)
Moves more dlsym related code to ndk_helpers.h/cc.
1 parent 5e0df01 commit e08b6c8

File tree

10 files changed

+169
-145
lines changed

10 files changed

+169
-145
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6291,8 +6291,6 @@ ORIGIN: ../../../flutter/shell/gpu/gpu_surface_vulkan_delegate.h + ../../../flut
62916291
ORIGIN: ../../../flutter/shell/gpu/gpu_surface_vulkan_impeller.cc + ../../../flutter/LICENSE
62926292
ORIGIN: ../../../flutter/shell/gpu/gpu_surface_vulkan_impeller.h + ../../../flutter/LICENSE
62936293
ORIGIN: ../../../flutter/shell/platform/android/AndroidManifest.xml + ../../../flutter/LICENSE
6294-
ORIGIN: ../../../flutter/shell/platform/android/android_choreographer.cc + ../../../flutter/LICENSE
6295-
ORIGIN: ../../../flutter/shell/platform/android/android_choreographer.h + ../../../flutter/LICENSE
62966294
ORIGIN: ../../../flutter/shell/platform/android/android_context_gl_impeller.cc + ../../../flutter/LICENSE
62976295
ORIGIN: ../../../flutter/shell/platform/android/android_context_gl_impeller.h + ../../../flutter/LICENSE
62986296
ORIGIN: ../../../flutter/shell/platform/android/android_context_gl_skia.cc + ../../../flutter/LICENSE
@@ -9132,8 +9130,6 @@ FILE: ../../../flutter/shell/gpu/gpu_surface_vulkan_delegate.h
91329130
FILE: ../../../flutter/shell/gpu/gpu_surface_vulkan_impeller.cc
91339131
FILE: ../../../flutter/shell/gpu/gpu_surface_vulkan_impeller.h
91349132
FILE: ../../../flutter/shell/platform/android/AndroidManifest.xml
9135-
FILE: ../../../flutter/shell/platform/android/android_choreographer.cc
9136-
FILE: ../../../flutter/shell/platform/android/android_choreographer.h
91379133
FILE: ../../../flutter/shell/platform/android/android_context_gl_impeller.cc
91389134
FILE: ../../../flutter/shell/platform/android/android_context_gl_impeller.h
91399135
FILE: ../../../flutter/shell/platform/android/android_context_gl_skia.cc

shell/platform/android/BUILD.gn

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ source_set("flutter_shell_native_src") {
7878
visibility = [ ":*" ]
7979

8080
sources = [
81-
"android_choreographer.cc",
82-
"android_choreographer.h",
8381
"android_context_gl_impeller.cc",
8482
"android_context_gl_impeller.h",
8583
"android_context_gl_skia.cc",

shell/platform/android/android_choreographer.cc

Lines changed: 0 additions & 61 deletions
This file was deleted.

shell/platform/android/android_choreographer.h

Lines changed: 0 additions & 30 deletions
This file was deleted.

shell/platform/android/flutter_main.cc

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "flutter/runtime/dart_vm.h"
2525
#include "flutter/shell/common/shell.h"
2626
#include "flutter/shell/common/switches.h"
27+
#include "flutter/shell/platform/android/ndk_helpers.h"
2728
#include "third_party/dart/runtime/include/dart_tools_api.h"
2829
#include "txt/platform.h"
2930

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

4041
namespace {
4142

42-
// This is only available on API 23+, so dynamically look it up.
43-
// This method is only called once at shell creation.
44-
// Do this in C++ because the API is available at level 23 here, but only 29+ in
45-
// Java.
46-
bool IsATraceEnabled() {
47-
auto libandroid = fml::NativeLibrary::Create("libandroid.so");
48-
FML_CHECK(libandroid);
49-
auto atrace_fn =
50-
libandroid->ResolveFunction<bool (*)(void)>("ATrace_isEnabled");
51-
if (atrace_fn) {
52-
return atrace_fn.value()();
53-
}
54-
return false;
55-
}
56-
5743
fml::jni::ScopedJavaGlobalRef<jclass>* g_flutter_jni_class = nullptr;
5844

5945
} // anonymous namespace
@@ -95,7 +81,7 @@ void FlutterMain::Init(JNIEnv* env,
9581
// Turn systracing on if ATrace_isEnabled is true and the user did not already
9682
// request systracing
9783
if (!settings.trace_systrace) {
98-
settings.trace_systrace = IsATraceEnabled();
84+
settings.trace_systrace = NDKHelpers::ATrace_isEnabled();
9985
if (settings.trace_systrace) {
10086
__android_log_print(
10187
ANDROID_LOG_INFO, "Flutter",

shell/platform/android/library_loader.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
1414
// Initialize the Java VM.
1515
fml::jni::InitJavaVM(vm);
1616

17+
// Registery dlsym lookups for NDK functions
18+
flutter::NDKHelpers::Init();
19+
1720
JNIEnv* env = fml::jni::AttachCurrentThread();
1821
bool result = false;
1922

shell/platform/android/ndk_helpers.cc

Lines changed: 87 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ typedef void (*fp_AHardwareBuffer_describe)(AHardwareBuffer* buffer,
2525
typedef void (*fp_AHardwareBuffer_getId)(AHardwareBuffer* buffer,
2626
uint64_t* outId);
2727

28+
typedef bool (*fp_ATrace_isEnabled)(void);
29+
30+
typedef AChoreographer* (*fp_AChoreographer_getInstance)(void);
31+
typedef void (*fp_AChoreographer_postFrameCallback)(
32+
AChoreographer* choreographer,
33+
AChoreographer_frameCallback callbackk,
34+
void* data);
35+
typedef void (*fp_AChoreographer_postFrameCallback64)(
36+
AChoreographer* choreographer,
37+
AChoreographer_frameCallback64 callbackk,
38+
void* data);
39+
2840
typedef EGLClientBuffer (*fp_eglGetNativeClientBufferANDROID)(
2941
AHardwareBuffer* buffer);
3042

@@ -37,6 +49,17 @@ void (*_AHardwareBuffer_describe)(AHardwareBuffer* buffer,
3749
AHardwareBuffer_Desc* desc) = nullptr;
3850
void (*_AHardwareBuffer_getId)(AHardwareBuffer* buffer,
3951
uint64_t* outId) = nullptr;
52+
bool (*_ATrace_isEnabled)() = nullptr;
53+
AChoreographer* (*_AChoreographer_getInstance)() = nullptr;
54+
void (*_AChoreographer_postFrameCallback)(
55+
AChoreographer* choreographer,
56+
AChoreographer_frameCallback callbackk,
57+
void* data) = nullptr;
58+
void (*_AChoreographer_postFrameCallback64)(
59+
AChoreographer* choreographer,
60+
AChoreographer_frameCallback64 callbackk,
61+
void* data) = nullptr;
62+
4063
EGLClientBuffer (*_eglGetNativeClientBufferANDROID)(AHardwareBuffer* buffer) =
4164
nullptr;
4265

@@ -75,6 +98,32 @@ void InitOnceCallback() {
7598
->ResolveFunction<fp_AHardwareBuffer_describe>(
7699
"AHardwareBuffer_describe")
77100
.value_or(nullptr);
101+
102+
_ATrace_isEnabled =
103+
android->ResolveFunction<fp_ATrace_isEnabled>("ATrace_isEnabled")
104+
.value_or(nullptr);
105+
106+
_AChoreographer_getInstance =
107+
android
108+
->ResolveFunction<fp_AChoreographer_getInstance>(
109+
"AChoreographer_getInstance")
110+
.value_or(nullptr);
111+
if (_AChoreographer_getInstance) {
112+
_AChoreographer_postFrameCallback64 =
113+
android
114+
->ResolveFunction<fp_AChoreographer_postFrameCallback64>(
115+
"AChoreographer_postFrameCallback64")
116+
.value_or(nullptr);
117+
#if FML_ARCH_CPU_64_BITS
118+
if (!_AChoreographer_postFrameCallback64) {
119+
_AChoreographer_postFrameCallback =
120+
android
121+
->ResolveFunction<fp_AChoreographer_postFrameCallback>(
122+
"AChoreographer_postFrameCallback")
123+
.value_or(nullptr);
124+
}
125+
#endif
126+
}
78127
}
79128

80129
} // namespace
@@ -83,42 +132,74 @@ void NDKHelpers::Init() {
83132
std::call_once(init_once, InitOnceCallback);
84133
}
85134

135+
bool NDKHelpers::ATrace_isEnabled() {
136+
if (_ATrace_isEnabled) {
137+
return _ATrace_isEnabled();
138+
}
139+
return false;
140+
}
141+
142+
ChoreographerSupportStatus NDKHelpers::ChoreographerSupported() {
143+
if (_AChoreographer_postFrameCallback64) {
144+
return ChoreographerSupportStatus::kSupported64;
145+
}
146+
if (_AChoreographer_postFrameCallback) {
147+
return ChoreographerSupportStatus::kSupported32;
148+
}
149+
return ChoreographerSupportStatus::kUnsupported;
150+
}
151+
152+
AChoreographer* NDKHelpers::AChoreographer_getInstance() {
153+
FML_CHECK(_AChoreographer_getInstance);
154+
return _AChoreographer_getInstance();
155+
}
156+
157+
void NDKHelpers::AChoreographer_postFrameCallback(
158+
AChoreographer* choreographer,
159+
AChoreographer_frameCallback callback,
160+
void* data) {
161+
FML_CHECK(_AChoreographer_postFrameCallback);
162+
return _AChoreographer_postFrameCallback(choreographer, callback, data);
163+
}
164+
165+
void NDKHelpers::AChoreographer_postFrameCallback64(
166+
AChoreographer* choreographer,
167+
AChoreographer_frameCallback64 callback,
168+
void* data) {
169+
FML_CHECK(_AChoreographer_postFrameCallback64);
170+
return _AChoreographer_postFrameCallback64(choreographer, callback, data);
171+
}
172+
86173
bool NDKHelpers::HardwareBufferSupported() {
87-
NDKHelpers::Init();
88174
const bool r = _AHardwareBuffer_fromHardwareBuffer != nullptr;
89175
return r;
90176
}
91177

92178
AHardwareBuffer* NDKHelpers::AHardwareBuffer_fromHardwareBuffer(
93179
JNIEnv* env,
94180
jobject hardwareBufferObj) {
95-
NDKHelpers::Init();
96181
FML_CHECK(_AHardwareBuffer_fromHardwareBuffer != nullptr);
97182
return _AHardwareBuffer_fromHardwareBuffer(env, hardwareBufferObj);
98183
}
99184

100185
void NDKHelpers::AHardwareBuffer_acquire(AHardwareBuffer* buffer) {
101-
NDKHelpers::Init();
102186
FML_CHECK(_AHardwareBuffer_acquire != nullptr);
103187
_AHardwareBuffer_acquire(buffer);
104188
}
105189

106190
void NDKHelpers::AHardwareBuffer_release(AHardwareBuffer* buffer) {
107-
NDKHelpers::Init();
108191
FML_CHECK(_AHardwareBuffer_release != nullptr);
109192
_AHardwareBuffer_release(buffer);
110193
}
111194

112195
void NDKHelpers::AHardwareBuffer_describe(AHardwareBuffer* buffer,
113196
AHardwareBuffer_Desc* desc) {
114-
NDKHelpers::Init();
115197
FML_CHECK(_AHardwareBuffer_describe != nullptr);
116198
_AHardwareBuffer_describe(buffer, desc);
117199
}
118200

119201
std::optional<HardwareBufferKey> NDKHelpers::AHardwareBuffer_getId(
120202
AHardwareBuffer* buffer) {
121-
NDKHelpers::Init();
122203
if (_AHardwareBuffer_getId == nullptr) {
123204
return std::nullopt;
124205
}
@@ -129,7 +210,6 @@ std::optional<HardwareBufferKey> NDKHelpers::AHardwareBuffer_getId(
129210

130211
EGLClientBuffer NDKHelpers::eglGetNativeClientBufferANDROID(
131212
AHardwareBuffer* buffer) {
132-
NDKHelpers::Init();
133213
FML_CHECK(_eglGetNativeClientBufferANDROID != nullptr);
134214
return _eglGetNativeClientBufferANDROID(buffer);
135215
}

shell/platform/android/ndk_helpers.h

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,44 @@
1010

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

13+
#include <android/choreographer.h>
1314
#include <android/hardware_buffer.h>
15+
#include <android/surface_control.h>
16+
#include <android/trace.h>
1417

1518
namespace flutter {
1619

1720
using HardwareBufferKey = uint64_t;
1821

22+
enum class ChoreographerSupportStatus {
23+
// Unavailable, API level < 24.
24+
kUnsupported,
25+
// Available, but only with postFrameCallback.
26+
kSupported32,
27+
// Available, but only with postFrameCallback64.
28+
kSupported64,
29+
};
30+
1931
// A collection of NDK functions that are available depending on the version of
2032
// the Android SDK we are linked with at runtime.
2133
class NDKHelpers {
2234
public:
35+
// Safe to call multiple times.
36+
// Normally called from JNI_OnLoad.
37+
static void Init();
38+
39+
// API Version 23
40+
static bool ATrace_isEnabled();
41+
42+
// API Version 24
43+
static ChoreographerSupportStatus ChoreographerSupported();
44+
static AChoreographer* AChoreographer_getInstance();
45+
// Deprecated in 29, available since 24.
46+
static void AChoreographer_postFrameCallback(
47+
AChoreographer* choreographer,
48+
AChoreographer_frameCallback callback,
49+
void* data);
50+
2351
// API Version 26
2452
static bool HardwareBufferSupported();
2553
static AHardwareBuffer* AHardwareBuffer_fromHardwareBuffer(
@@ -32,14 +60,17 @@ class NDKHelpers {
3260
static EGLClientBuffer eglGetNativeClientBufferANDROID(
3361
AHardwareBuffer* buffer);
3462

63+
// API Version 29
64+
static void AChoreographer_postFrameCallback64(
65+
AChoreographer* choreographer,
66+
AChoreographer_frameCallback64 callback,
67+
void* data);
68+
3569
// API Version 31
3670

3771
// Returns std::nullopt on API version 26 - 30.
3872
static std::optional<HardwareBufferKey> AHardwareBuffer_getId(
3973
AHardwareBuffer* buffer);
40-
41-
private:
42-
static void Init();
4374
};
4475

4576
} // namespace flutter

0 commit comments

Comments
 (0)