Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry pick PR #3027: [Android] Fix CPU spinning of StarboardMain. #3046

Merged
merged 3 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 10 additions & 5 deletions starboard/android/shared/application_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ ApplicationAndroid::ApplicationAndroid(ALooper* looper)
QueueApplication(sb_event_handle_callback),
#endif // SB_API_VERSION >= 15
last_is_accessibility_high_contrast_text_enabled_(false) {
handle_system_events_.store(true);
// Initialize Time Zone early so that local time works correctly.
// Called once here to help SbTimeZoneGet*Name()
tzset();
Expand Down Expand Up @@ -208,19 +209,23 @@ bool ApplicationAndroid::DestroyWindow(SbWindow window) {

Event* ApplicationAndroid::WaitForSystemEventWithTimeout(SbTime time) {
// Limit the polling time in case some non-system event is injected.
const int kMaxPollingTimeMillisecond = 10;
const int kMaxPollingTimeMillisecond = 1000;

// Convert from microseconds to milliseconds, taking the ceiling value.
// If we take the floor, or round, then we end up busy looping every time
// the next event time is less than one millisecond.
int timeout_millis = (time + kSbTimeMillisecond - 1) / kSbTimeMillisecond;
int timeout_millis =
(time <
std::min(kSbInt64Max - 1000, 1000 * static_cast<int64_t>(INT_MAX - 1)))
gbournou marked this conversation as resolved.
Show resolved Hide resolved
? (time + 1000 - 1) / 1000
: INT_MAX;
int looper_events;
int ident = ALooper_pollAll(
int ident = ALooper_pollOnce(
std::min(std::max(timeout_millis, 0), kMaxPollingTimeMillisecond), NULL,
&looper_events, NULL);

// Ignore new system events while processing one.
handle_system_events_ = false;
handle_system_events_.store(false);

switch (ident) {
case kLooperIdAndroidCommand:
Expand All @@ -231,7 +236,7 @@ Event* ApplicationAndroid::WaitForSystemEventWithTimeout(SbTime time) {
break;
}

handle_system_events_ = true;
handle_system_events_.store(true);

// Always return NULL since we already dispatched our own system events.
return NULL;
Expand Down
4 changes: 2 additions & 2 deletions starboard/android/shared/application_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class ApplicationAndroid
void OnSuspend() override;

// --- QueueApplication overrides ---
bool MayHaveSystemEvents() override { return handle_system_events_; }
bool MayHaveSystemEvents() override { return handle_system_events_.load(); }
Event* WaitForSystemEventWithTimeout(SbTime time) override;
void WakeSystemEventWait() override;

Expand All @@ -144,7 +144,7 @@ class ApplicationAndroid

// In certain situations, the Starboard thread should not try to process new
// system events (e.g. while one is being processed).
bool handle_system_events_ = true;
atomic_bool handle_system_events_;

// Synchronization for commands that change availability of Android resources
// such as the input and/or native_window_.
Expand Down
Loading