Skip to content

Instantly share code, notes, and snippets.

@mstange
Created November 5, 2020 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mstange/0ad8deaeceb547b64eff9c289040c523 to your computer and use it in GitHub Desktop.
Save mstange/0ad8deaeceb547b64eff9c289040c523 to your computer and use it in GitHub Desktop.
Pseudocode for ticking the refresh driver as quickly as possible when a key event comes in
bool mPendingTick = false;
bool mAlreadyTickedThisVsyncInterval = false;
void ScheduleTick() {
if (mPendingTick) {
return;
}
mPendingTick = true;
EnsureListeningToVsync([](){ OnVSync(); });
if (!mAlreadyTickedThisVsyncInterval) {
ScheduleImmediatelyWithVsyncPriority([](){ Tick(); });
}
}
void OnVSync() {
// This notification indicates the start of a new “vsync interval”.
mAlreadyTickedThisVsyncInterval = false;
if (mPendingTick) {
Tick();
}
}
void Tick() {
MOZ_RELEASE_ASSERT(mPendingTick);
MOZ_RELEASE_ASSERT(!mAlreadyTickedThisVsyncInterval);
mAlreadyTickedThisVsyncInterval = true;
mPendingTick = false;
DoTheWork();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment