From 9b64168201cbc942e12135d02ff15f3a98db995b Mon Sep 17 00:00:00 2001 From: ramindani Date: Tue, 9 May 2023 15:01:48 -0700 Subject: [PATCH] [hwui] Update dispatchFrameCallbacks to be called with timeUntilDeadline/4 instead of timeUntilDeadline * 0.25f Previous value of multiply by 0.25f caused the overflow when timeUntilDeadline was long. Test: verified with a test app BUG: 278433880 Change-Id: I54347e0a4f6a1806b3f8c96bfbec7b758dfb6f00 --- libs/hwui/renderthread/RenderThread.cpp | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/libs/hwui/renderthread/RenderThread.cpp b/libs/hwui/renderthread/RenderThread.cpp index 0afd949cf5c98..9ba67a2110c1d 100644 --- a/libs/hwui/renderthread/RenderThread.cpp +++ b/libs/hwui/renderthread/RenderThread.cpp @@ -133,12 +133,23 @@ void RenderThread::frameCallback(int64_t vsyncId, int64_t frameDeadline, int64_t if (timeLord().vsyncReceived(frameTimeNanos, frameTimeNanos, vsyncId, frameDeadline, frameInterval) && !mFrameCallbackTaskPending) { - ATRACE_NAME("queue mFrameCallbackTask"); mFrameCallbackTaskPending = true; - nsecs_t timeUntilDeadline = frameDeadline - frameTimeNanos; - nsecs_t runAt = (frameTimeNanos + (timeUntilDeadline * 0.25f)); - queue().postAt(runAt, [=]() { dispatchFrameCallbacks(); }); + using SteadyClock = std::chrono::steady_clock; + using Nanos = std::chrono::nanoseconds; + using toNsecs_t = std::chrono::duration; + using toFloatMillis = std::chrono::duration; + + const auto frameTimeTimePoint = SteadyClock::time_point(Nanos(frameTimeNanos)); + const auto deadlineTimePoint = SteadyClock::time_point(Nanos(frameDeadline)); + + const auto timeUntilDeadline = deadlineTimePoint - frameTimeTimePoint; + const auto runAt = (frameTimeTimePoint + (timeUntilDeadline / 4)); + + ATRACE_FORMAT("queue mFrameCallbackTask to run after %.2fms", + toFloatMillis(runAt - SteadyClock::now()).count()); + queue().postAt(toNsecs_t(runAt.time_since_epoch()).count(), + [=]() { dispatchFrameCallbacks(); }); } }