This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Use AChoreographer methods to await vsync when available #31859
Merged
fluttergithubbot
merged 5 commits into
flutter:main
from
ColdPaleLight:android_vsync_ndk
Mar 9, 2022
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6fde4c4
Use AChoreographer methods to await vsync when available
ColdPaleLight 39705b6
Add some comments
ColdPaleLight 8a6bbd8
Merge branch 'flutter:main' into android_vsync_ndk
ColdPaleLight 9bed5af
tweak the code
ColdPaleLight 52cfcf5
Merge branch 'flutter:main' into android_vsync_ndk
ColdPaleLight File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include "flutter/shell/platform/android/android_choreographer.h" | ||
|
|
||
| #include "flutter/fml/native_library.h" | ||
|
|
||
| // Only avialalbe on API 24+ | ||
| typedef void AChoreographer; | ||
| // Only available on API 29+ or API 24+ if the architecture is 64-bit. | ||
| typedef void (*AChoreographer_frameCallback)(int64_t frameTimeNanos, | ||
| void* data); | ||
| // Only avialalbe on API 24+ | ||
| typedef AChoreographer* (*AChoreographer_getInstance_FPN)(); | ||
| typedef void (*AChoreographer_postFrameCallback_FPN)( | ||
| AChoreographer* choreographer, | ||
| AChoreographer_frameCallback callback, | ||
| void* data); | ||
| static AChoreographer_getInstance_FPN AChoreographer_getInstance; | ||
| static AChoreographer_postFrameCallback_FPN AChoreographer_postFrameCallback; | ||
|
|
||
| namespace flutter { | ||
|
|
||
| bool AndroidChoreographer::ShouldUseNDKChoreographer() { | ||
| static std::optional<bool> use_ndk_choreographer; | ||
| if (use_ndk_choreographer) { | ||
| return use_ndk_choreographer.value(); | ||
| } | ||
| auto libandroid = fml::NativeLibrary::Create("libandroid.so"); | ||
| FML_DCHECK(libandroid); | ||
| auto get_instance_fn = | ||
| libandroid->ResolveFunction<AChoreographer_getInstance_FPN>( | ||
| "AChoreographer_getInstance"); | ||
| auto post_frame_callback_fn = | ||
| libandroid->ResolveFunction<AChoreographer_postFrameCallback_FPN>( | ||
| "AChoreographer_postFrameCallback64"); | ||
| #if FML_ARCH_CPU_64_BITS | ||
| if (!post_frame_callback_fn) { | ||
| post_frame_callback_fn = | ||
| libandroid->ResolveFunction<AChoreographer_postFrameCallback_FPN>( | ||
| "AChoreographer_postFrameCallback"); | ||
| } | ||
| #endif | ||
| if (get_instance_fn && post_frame_callback_fn) { | ||
| AChoreographer_getInstance = get_instance_fn.value(); | ||
| AChoreographer_postFrameCallback = post_frame_callback_fn.value(); | ||
| use_ndk_choreographer = true; | ||
| } else { | ||
| use_ndk_choreographer = false; | ||
| } | ||
| return use_ndk_choreographer.value(); | ||
| } | ||
|
|
||
| void AndroidChoreographer::PostFrameCallback(OnFrameCallback callback, | ||
| void* data) { | ||
| AChoreographer* choreographer = AChoreographer_getInstance(); | ||
| AChoreographer_postFrameCallback(choreographer, callback, data); | ||
| } | ||
|
|
||
| } // namespace flutter | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #ifndef FLUTTER_SHELL_PLATFORM_ANDROID_ANDROID_CHOREOGRAPHER_H_ | ||
| #define FLUTTER_SHELL_PLATFORM_ANDROID_ANDROID_CHOREOGRAPHER_H_ | ||
|
|
||
| #include "flutter/fml/macros.h" | ||
|
|
||
| #include <cstdint> | ||
|
|
||
| namespace flutter { | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| /// The Android Choreographer is used by `VsyncWaiterAndroid` to await vsync | ||
| /// signal. It's only available on API 29+ or API 24+ if the architecture is | ||
| /// 64-bit. | ||
| /// | ||
| class AndroidChoreographer { | ||
| public: | ||
| typedef void (*OnFrameCallback)(int64_t frame_time_nanos, void* data); | ||
| static bool ShouldUseNDKChoreographer(); | ||
| static void PostFrameCallback(OnFrameCallback callback, void* data); | ||
|
|
||
| FML_DISALLOW_COPY_AND_ASSIGN(AndroidChoreographer); | ||
| }; | ||
|
|
||
| } // namespace flutter | ||
|
|
||
| #endif // FLUTTER_SHELL_PLATFORM_ANDROID_ANDROID_CHOREOGRAPHER_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this right?
if
!FML_ARCH_CPU_64_BITSandChoreographer_postFrameCallback64 == nullptr, thenshould_use_ndk_choreographerreturns false.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we can not use
AChoreographer_postFrameCallbackin 32-bits app, because in the callback functionAChoreographer_frameCallback(long frameTimeNanos, void *data), the type offrameTimeNanosislong, and in 32-bits architecturelongis 32bit. And actually the correctframeTimeNanosshould be 64 bit. So in 32-bits architecture we can't get the correctframeTimeNanos.I have tested on two devices and the conclusion is that the 32-bits flutter app does not work correctly if using
AChoreographer_postFrameCallbackThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. I didn't realize this was going to be this complicated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can also simplify it, for example , API 29+ use NDK
AChoreographer_postFrameCallback64, others use Java fallback. So that we don't need to consider whether the app is 32-bits or 64-bits.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if we are doing this, then making it work on API level >=24 for x64 is better than only >= 29.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it.