blob: 2e1f938126247ea60d442852a38ad67ecf8fc4b4 [file] [log] [blame]
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ady Abraham55fa7272020-09-30 19:19:27 -070017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080018
Mathias Agopian8aedd472012-01-24 16:39:14 -080019#include <binder/IPCThreadState.h>
Mathias Agopianf1d8e872009-04-20 19:39:12 -070020#include <utils/Log.h>
Lloyd Pique78ce4182018-01-31 16:39:51 -080021#include <utils/Timers.h>
22#include <utils/threads.h>
Mathias Agopian8aedd472012-01-24 16:39:14 -080023
Dominik Laskowski63f12792023-01-21 16:58:22 -050024#include <scheduler/interface/ICompositor.h>
Mathias Agopianf1d8e872009-04-20 19:39:12 -070025
Mathias Agopian8aedd472012-01-24 16:39:14 -080026#include "EventThread.h"
Ady Abraham55fa7272020-09-30 19:19:27 -070027#include "FrameTimeline.h"
Lloyd Pique78ce4182018-01-31 16:39:51 -080028#include "MessageQueue.h"
Mathias Agopianf1d8e872009-04-20 19:39:12 -070029
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070030namespace android::impl {
Lloyd Pique3fcdef12018-01-22 17:14:00 -080031
Dominik Laskowski08fbd852022-07-14 08:53:42 -070032void MessageQueue::Handler::dispatchFrame(VsyncId vsyncId, TimePoint expectedVsyncTime) {
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -070033 if (!mFramePending.exchange(true)) {
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070034 mVsyncId = vsyncId;
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070035 mExpectedVsyncTime = expectedVsyncTime;
Ady Abrahamd11bade2022-08-01 16:18:03 -070036 mQueue.mLooper->sendMessage(sp<MessageHandler>::fromExisting(this), Message());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080037 }
38}
39
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070040bool MessageQueue::Handler::isFramePending() const {
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -070041 return mFramePending.load();
Ady Abraham562c2712021-05-07 15:10:42 -070042}
43
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -070044void MessageQueue::Handler::handleMessage(const Message&) {
45 mFramePending.store(false);
Dominik Laskowski08fbd852022-07-14 08:53:42 -070046 mQueue.onFrameSignal(mQueue.mCompositor, mVsyncId, mExpectedVsyncTime);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080047}
48
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070049MessageQueue::MessageQueue(ICompositor& compositor)
50 : MessageQueue(compositor, sp<Handler>::make(*this)) {}
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080051
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070052constexpr bool kAllowNonCallbacks = true;
53
54MessageQueue::MessageQueue(ICompositor& compositor, sp<Handler> handler)
55 : mCompositor(compositor),
56 mLooper(sp<Looper>::make(kAllowNonCallbacks)),
57 mHandler(std::move(handler)) {}
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080058
Ady Abraham55fa7272020-09-30 19:19:27 -070059void MessageQueue::vsyncCallback(nsecs_t vsyncTime, nsecs_t targetWakeupTime, nsecs_t readyTime) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +000060 SFTRACE_CALL();
Ady Abraham55fa7272020-09-30 19:19:27 -070061 // Trace VSYNC-sf
62 mVsync.value = (mVsync.value + 1) % 2;
63
Dominik Laskowski08fbd852022-07-14 08:53:42 -070064 const auto expectedVsyncTime = TimePoint::fromNs(vsyncTime);
Ady Abraham55fa7272020-09-30 19:19:27 -070065 {
66 std::lock_guard lock(mVsync.mutex);
Dominik Laskowski08fbd852022-07-14 08:53:42 -070067 mVsync.lastCallbackTime = expectedVsyncTime;
ramindani32a88b12024-01-31 18:45:30 -080068 mVsync.scheduledFrameTimeOpt.reset();
Ady Abraham55fa7272020-09-30 19:19:27 -070069 }
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070070
Dominik Laskowski08fbd852022-07-14 08:53:42 -070071 const auto vsyncId = VsyncId{mVsync.tokenManager->generateTokenForPredictions(
72 {targetWakeupTime, readyTime, vsyncTime})};
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070073
Dominik Laskowski08fbd852022-07-14 08:53:42 -070074 mHandler->dispatchFrame(vsyncId, expectedVsyncTime);
Ady Abraham55fa7272020-09-30 19:19:27 -070075}
76
Leon Scroggins IIIa7be94e2024-01-23 12:24:30 -050077void MessageQueue::initVsyncInternal(std::shared_ptr<scheduler::VSyncDispatch> dispatch,
78 frametimeline::TokenManager& tokenManager,
79 std::chrono::nanoseconds workDuration) {
Leon Scroggins IIIfb7ed562023-04-05 11:39:25 -040080 std::unique_ptr<scheduler::VSyncCallbackRegistration> oldRegistration;
81 {
82 std::lock_guard lock(mVsync.mutex);
83 mVsync.workDuration = workDuration;
84 mVsync.tokenManager = &tokenManager;
85 oldRegistration = onNewVsyncScheduleLocked(std::move(dispatch));
86 }
87
88 // See comments in onNewVsyncSchedule. Today, oldRegistration should be
Leon Scroggins IIIa7be94e2024-01-23 12:24:30 -050089 // empty, but nothing prevents us from calling initVsyncInternal multiple times, so
Leon Scroggins IIIfb7ed562023-04-05 11:39:25 -040090 // go ahead and destruct it outside the lock for safety.
91 oldRegistration.reset();
Leon Scroggins III67388622023-02-06 20:36:20 -050092}
93
94void MessageQueue::onNewVsyncSchedule(std::shared_ptr<scheduler::VSyncDispatch> dispatch) {
Leon Scroggins IIIfb7ed562023-04-05 11:39:25 -040095 std::unique_ptr<scheduler::VSyncCallbackRegistration> oldRegistration;
96 {
97 std::lock_guard lock(mVsync.mutex);
98 oldRegistration = onNewVsyncScheduleLocked(std::move(dispatch));
99 }
100
101 // The old registration needs to be deleted after releasing mVsync.mutex to
102 // avoid deadlock. This is because the callback may be running on the timer
103 // thread. In that case, timerCallback sets
104 // VSyncDispatchTimerQueueEntry::mRunning to true, then attempts to lock
105 // mVsync.mutex. But if it's already locked, the VSyncCallbackRegistration's
106 // destructor has to wait until VSyncDispatchTimerQueueEntry::mRunning is
107 // set back to false, but it won't be until mVsync.mutex is released.
108 oldRegistration.reset();
Leon Scroggins III67388622023-02-06 20:36:20 -0500109}
110
Leon Scroggins IIIfb7ed562023-04-05 11:39:25 -0400111std::unique_ptr<scheduler::VSyncCallbackRegistration> MessageQueue::onNewVsyncScheduleLocked(
112 std::shared_ptr<scheduler::VSyncDispatch> dispatch) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500113 const bool reschedule = mVsync.registration &&
114 mVsync.registration->cancel() == scheduler::CancelResult::Cancelled;
Leon Scroggins IIIfb7ed562023-04-05 11:39:25 -0400115 auto oldRegistration = std::move(mVsync.registration);
Ady Abraham55fa7272020-09-30 19:19:27 -0700116 mVsync.registration = std::make_unique<
Leon Scroggins III67388622023-02-06 20:36:20 -0500117 scheduler::VSyncCallbackRegistration>(std::move(dispatch),
Ady Abraham55fa7272020-09-30 19:19:27 -0700118 std::bind(&MessageQueue::vsyncCallback, this,
119 std::placeholders::_1,
120 std::placeholders::_2,
121 std::placeholders::_3),
122 "sf");
Leon Scroggins III67388622023-02-06 20:36:20 -0500123 if (reschedule) {
ramindani32a88b12024-01-31 18:45:30 -0800124 mVsync.scheduledFrameTimeOpt =
Leon Scroggins III67388622023-02-06 20:36:20 -0500125 mVsync.registration->schedule({.workDuration = mVsync.workDuration.get().count(),
126 .readyDuration = 0,
Ady Abraham4335afd2023-12-18 19:10:47 -0800127 .lastVsync = mVsync.lastCallbackTime.ns()});
Leon Scroggins III67388622023-02-06 20:36:20 -0500128 }
Leon Scroggins IIIfb7ed562023-04-05 11:39:25 -0400129 return oldRegistration;
Ady Abraham55fa7272020-09-30 19:19:27 -0700130}
131
Ady Abraham011f8ba2022-11-22 15:09:07 -0800132void MessageQueue::destroyVsync() {
133 std::lock_guard lock(mVsync.mutex);
134 mVsync.tokenManager = nullptr;
135 mVsync.registration.reset();
136}
137
Ady Abraham55fa7272020-09-30 19:19:27 -0700138void MessageQueue::setDuration(std::chrono::nanoseconds workDuration) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000139 SFTRACE_CALL();
Ady Abraham55fa7272020-09-30 19:19:27 -0700140 std::lock_guard lock(mVsync.mutex);
141 mVsync.workDuration = workDuration;
ramindani32a88b12024-01-31 18:45:30 -0800142 mVsync.scheduledFrameTimeOpt =
Ady Abraham011f8ba2022-11-22 15:09:07 -0800143 mVsync.registration->update({.workDuration = mVsync.workDuration.get().count(),
144 .readyDuration = 0,
Ady Abraham4335afd2023-12-18 19:10:47 -0800145 .lastVsync = mVsync.lastCallbackTime.ns()});
Ana Krulec98b5b242018-08-10 15:03:23 -0700146}
147
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800148void MessageQueue::waitMessage() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700149 do {
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800150 IPCThreadState::self()->flushCommands();
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800151 int32_t ret = mLooper->pollOnce(-1);
152 switch (ret) {
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800153 case Looper::POLL_WAKE:
154 case Looper::POLL_CALLBACK:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800155 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800156 case Looper::POLL_ERROR:
157 ALOGE("Looper::POLL_ERROR");
Pablo Ceballos53390e12015-08-04 11:25:59 -0700158 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800159 case Looper::POLL_TIMEOUT:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800160 // timeout (should not happen)
161 continue;
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800162 default:
163 // should not happen
Steve Blocke6f43dd2012-01-06 19:20:56 +0000164 ALOGE("Looper::pollOnce() returned unknown status %d", ret);
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800165 continue;
166 }
167 } while (true);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700168}
169
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700170void MessageQueue::postMessage(sp<MessageHandler>&& handler) {
171 mLooper->sendMessage(handler, Message());
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700172}
173
Chavi Weingarten076acac2023-01-19 17:20:43 +0000174void MessageQueue::postMessageDelayed(sp<MessageHandler>&& handler, nsecs_t uptimeDelay) {
175 mLooper->sendMessageDelayed(uptimeDelay, handler, Message());
176}
177
Dominik Laskowskif11728a2022-07-28 13:07:42 -0700178void MessageQueue::scheduleConfigure() {
179 struct ConfigureHandler : MessageHandler {
180 explicit ConfigureHandler(ICompositor& compositor) : compositor(compositor) {}
181
182 void handleMessage(const Message&) override { compositor.configure(); }
183
184 ICompositor& compositor;
185 };
186
187 // TODO(b/241285876): Batch configure tasks that happen within some duration.
188 postMessage(sp<ConfigureHandler>::make(mCompositor));
189}
190
Ady Abraham9953e062024-07-24 22:56:22 -0700191void MessageQueue::scheduleFrame(Duration workDurationSlack) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000192 SFTRACE_CALL();
Dominik Laskowski208235c2020-12-03 15:38:51 -0800193
Dominik Laskowski208235c2020-12-03 15:38:51 -0800194 std::lock_guard lock(mVsync.mutex);
Ady Abraham9953e062024-07-24 22:56:22 -0700195 const auto workDuration = Duration(mVsync.workDuration.get() - workDurationSlack);
ramindani32a88b12024-01-31 18:45:30 -0800196 mVsync.scheduledFrameTimeOpt =
Ady Abraham9953e062024-07-24 22:56:22 -0700197 mVsync.registration->schedule({.workDuration = workDuration.ns(),
Ady Abraham562c2712021-05-07 15:10:42 -0700198 .readyDuration = 0,
Ady Abraham4335afd2023-12-18 19:10:47 -0800199 .lastVsync = mVsync.lastCallbackTime.ns()});
Mathias Agopian8aedd472012-01-24 16:39:14 -0800200}
201
ramindani32a88b12024-01-31 18:45:30 -0800202std::optional<scheduler::ScheduleResult> MessageQueue::getScheduledFrameResult() const {
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700203 if (mHandler->isFramePending()) {
ramindani32a88b12024-01-31 18:45:30 -0800204 return scheduler::ScheduleResult{TimePoint::now(), mHandler->getExpectedVsyncTime()};
Ady Abraham562c2712021-05-07 15:10:42 -0700205 }
Ady Abraham562c2712021-05-07 15:10:42 -0700206 std::lock_guard lock(mVsync.mutex);
ramindani32a88b12024-01-31 18:45:30 -0800207 if (const auto scheduledFrameTimeline = mVsync.scheduledFrameTimeOpt) {
208 return scheduledFrameTimeline;
Ady Abraham562c2712021-05-07 15:10:42 -0700209 }
Ady Abraham562c2712021-05-07 15:10:42 -0700210 return std::nullopt;
211}
212
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700213} // namespace android::impl