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

Conversation

@ColdPaleLight
Copy link
Member

@ColdPaleLight ColdPaleLight commented Mar 6, 2022

Fix flutter/flutter#62166
The main goal of this PR is to make await vsync happen on the UI thread instead of the Platform thread.

We have tried to do it using JNI but got reverted, which lets us know that we cannot use Choreographer class in java in non-main thread. #31750

However, AChoreographer is only available at API 24+ and has bugs on 32-bit architecture (can't get the correct timestamp), so we can't make this new feature available to all Android devices. See the table below for details

API Level & architecture API Vsync Thread
API 29+ NDK AChoreographer UI Thread
API 24+ & 64-bits NDK AChoreographer UI Thread
API 24+ & 32-bits Java Choreographer Platform Thread
other Java Choreographer Platform Thread

cc @blasten @chinmaygarde @dnfield @jason-simmons

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide and the C++, Objective-C, Java style guides.
  • I listed at least one issue that this PR fixes in the description above.
  • I added new tests to check the change I am making or feature I am adding, or Hixie said the PR is test-exempt. See testing the engine for instructions on
    writing and running engine tests.
  • I updated/added relevant documentation (doc comments with ///).
  • I signed the CLA.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@ColdPaleLight ColdPaleLight changed the title [WIP]Use AChoreographer methods to await vsync when available Use AChoreographer methods to await vsync when available Mar 8, 2022

//------------------------------------------------------------------------------
/// The Android Choreographer is used by `VsyncWaiterAndroid` to await vsync
/// signal. It's only avialalbe on API 29+ or API 24+ and architecture is 64-bit
Copy link

@blasten blasten Mar 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// signal. It's only avialalbe on API 29+ or API 24+ and architecture is 64-bit
/// signal. It's only available on API 29+ or API 24+ if the architecture is 64-bit

API 29 added support for an explicit 64-bit int (int64_t).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

"AChoreographer_postFrameCallback");
}
#endif
if (get_instance_fn && post_frame_callback_fn) {
Copy link

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_BITS and Choreographer_postFrameCallback64 == nullptr, then should_use_ndk_choreographer returns false.

Copy link
Member Author

@ColdPaleLight ColdPaleLight Mar 8, 2022

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_postFrameCallback in 32-bits app, because in the callback function AChoreographer_frameCallback(long frameTimeNanos, void *data), the type of frameTimeNanos is long, and in 32-bits architecture long is 32bit. And actually the correct frameTimeNanos should be 64 bit. So in 32-bits architecture we can't get the correct frameTimeNanos.

I have tested on two devices and the conclusion is that the 32-bits flutter app does not work correctly if using AChoreographer_postFrameCallback

Copy link

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.

Copy link
Member Author

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.

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.

Copy link

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it.

Copy link
Contributor

@dnfield dnfield left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with nits

VsyncWaiterAndroid::VsyncWaiterAndroid(flutter::TaskRunners task_runners)
: VsyncWaiter(std::move(task_runners)) {}
: VsyncWaiter(std::move(task_runners)),
should_use_ndk_choreographer_(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: here and elsewhere, drop should.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

AndroidChoreographer::PostFrameCallback(&OnVsyncFromNDK, weak_this);
});
} else {
// TODO: Remove the JNI fallback when we drop support for API level < 29.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please file and add a bug here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

platform-android waiting for tree to go green This PR is approved and tested, but waiting for the tree to be green to land.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider using dlsym/dlopen on Android embedding to dynamically use NDK API.

4 participants