blob: 18c0a696d5add9481825d8b9f8f6bf413f80dcd0 [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>
Dominik Laskowski63f12792023-01-21 16:58:22 -050020#include <gui/DisplayEventReceiver.h>
Mathias Agopianf1d8e872009-04-20 19:39:12 -070021#include <utils/Log.h>
Lloyd Pique78ce4182018-01-31 16:39:51 -080022#include <utils/Timers.h>
23#include <utils/threads.h>
Mathias Agopian8aedd472012-01-24 16:39:14 -080024
Dominik Laskowski63f12792023-01-21 16:58:22 -050025#include <scheduler/interface/ICompositor.h>
Mathias Agopianf1d8e872009-04-20 19:39:12 -070026
Mathias Agopian8aedd472012-01-24 16:39:14 -080027#include "EventThread.h"
Ady Abraham55fa7272020-09-30 19:19:27 -070028#include "FrameTimeline.h"
Lloyd Pique78ce4182018-01-31 16:39:51 -080029#include "MessageQueue.h"
Mathias Agopianf1d8e872009-04-20 19:39:12 -070030
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070031namespace android::impl {
Lloyd Pique3fcdef12018-01-22 17:14:00 -080032
Dominik Laskowski08fbd852022-07-14 08:53:42 -070033void MessageQueue::Handler::dispatchFrame(VsyncId vsyncId, TimePoint expectedVsyncTime) {
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -070034 if (!mFramePending.exchange(true)) {
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070035 mVsyncId = vsyncId;
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070036 mExpectedVsyncTime = expectedVsyncTime;
Ady Abrahamd11bade2022-08-01 16:18:03 -070037 mQueue.mLooper->sendMessage(sp<MessageHandler>::fromExisting(this), Message());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080038 }
39}
40
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070041bool MessageQueue::Handler::isFramePending() const {
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -070042 return mFramePending.load();
Ady Abraham562c2712021-05-07 15:10:42 -070043}
44
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -070045void MessageQueue::Handler::handleMessage(const Message&) {
46 mFramePending.store(false);
Dominik Laskowski08fbd852022-07-14 08:53:42 -070047 mQueue.onFrameSignal(mQueue.mCompositor, mVsyncId, mExpectedVsyncTime);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080048}
49
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070050MessageQueue::MessageQueue(ICompositor& compositor)
51 : MessageQueue(compositor, sp<Handler>::make(*this)) {}
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080052
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070053constexpr bool kAllowNonCallbacks = true;
54
55MessageQueue::MessageQueue(ICompositor& compositor, sp<Handler> handler)
56 : mCompositor(compositor),
57 mLooper(sp<Looper>::make(kAllowNonCallbacks)),
58 mHandler(std::move(handler)) {}
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080059
Ady Abraham55fa7272020-09-30 19:19:27 -070060void MessageQueue::vsyncCallback(nsecs_t vsyncTime, nsecs_t targetWakeupTime, nsecs_t readyTime) {
61 ATRACE_CALL();
62 // Trace VSYNC-sf
63 mVsync.value = (mVsync.value + 1) % 2;
64
Dominik Laskowski08fbd852022-07-14 08:53:42 -070065 const auto expectedVsyncTime = TimePoint::fromNs(vsyncTime);
Ady Abraham55fa7272020-09-30 19:19:27 -070066 {
67 std::lock_guard lock(mVsync.mutex);
Dominik Laskowski08fbd852022-07-14 08:53:42 -070068 mVsync.lastCallbackTime = expectedVsyncTime;
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070069 mVsync.scheduledFrameTime.reset();
Ady Abraham55fa7272020-09-30 19:19:27 -070070 }
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070071
Dominik Laskowski08fbd852022-07-14 08:53:42 -070072 const auto vsyncId = VsyncId{mVsync.tokenManager->generateTokenForPredictions(
73 {targetWakeupTime, readyTime, vsyncTime})};
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070074
Dominik Laskowski08fbd852022-07-14 08:53:42 -070075 mHandler->dispatchFrame(vsyncId, expectedVsyncTime);
Ady Abraham55fa7272020-09-30 19:19:27 -070076}
77
Leon Scroggins III67388622023-02-06 20:36:20 -050078void MessageQueue::initVsync(std::shared_ptr<scheduler::VSyncDispatch> dispatch,
Ady Abraham55fa7272020-09-30 19:19:27 -070079 frametimeline::TokenManager& tokenManager,
80 std::chrono::nanoseconds workDuration) {
Leon Scroggins IIIfb7ed562023-04-05 11:39:25 -040081 std::unique_ptr<scheduler::VSyncCallbackRegistration> oldRegistration;
82 {
83 std::lock_guard lock(mVsync.mutex);
84 mVsync.workDuration = workDuration;
85 mVsync.tokenManager = &tokenManager;
86 oldRegistration = onNewVsyncScheduleLocked(std::move(dispatch));
87 }
88
89 // See comments in onNewVsyncSchedule. Today, oldRegistration should be
90 // empty, but nothing prevents us from calling initVsync multiple times, so
91 // go ahead and destruct it outside the lock for safety.
92 oldRegistration.reset();
Leon Scroggins III67388622023-02-06 20:36:20 -050093}
94
95void MessageQueue::onNewVsyncSchedule(std::shared_ptr<scheduler::VSyncDispatch> dispatch) {
Leon Scroggins IIIfb7ed562023-04-05 11:39:25 -040096 std::unique_ptr<scheduler::VSyncCallbackRegistration> oldRegistration;
97 {
98 std::lock_guard lock(mVsync.mutex);
99 oldRegistration = onNewVsyncScheduleLocked(std::move(dispatch));
100 }
101
102 // The old registration needs to be deleted after releasing mVsync.mutex to
103 // avoid deadlock. This is because the callback may be running on the timer
104 // thread. In that case, timerCallback sets
105 // VSyncDispatchTimerQueueEntry::mRunning to true, then attempts to lock
106 // mVsync.mutex. But if it's already locked, the VSyncCallbackRegistration's
107 // destructor has to wait until VSyncDispatchTimerQueueEntry::mRunning is
108 // set back to false, but it won't be until mVsync.mutex is released.
109 oldRegistration.reset();
Leon Scroggins III67388622023-02-06 20:36:20 -0500110}
111
Leon Scroggins IIIfb7ed562023-04-05 11:39:25 -0400112std::unique_ptr<scheduler::VSyncCallbackRegistration> MessageQueue::onNewVsyncScheduleLocked(
113 std::shared_ptr<scheduler::VSyncDispatch> dispatch) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500114 const bool reschedule = mVsync.registration &&
115 mVsync.registration->cancel() == scheduler::CancelResult::Cancelled;
Leon Scroggins IIIfb7ed562023-04-05 11:39:25 -0400116 auto oldRegistration = std::move(mVsync.registration);
Ady Abraham55fa7272020-09-30 19:19:27 -0700117 mVsync.registration = std::make_unique<
Leon Scroggins III67388622023-02-06 20:36:20 -0500118 scheduler::VSyncCallbackRegistration>(std::move(dispatch),
Ady Abraham55fa7272020-09-30 19:19:27 -0700119 std::bind(&MessageQueue::vsyncCallback, this,
120 std::placeholders::_1,
121 std::placeholders::_2,
122 std::placeholders::_3),
123 "sf");
Leon Scroggins III67388622023-02-06 20:36:20 -0500124 if (reschedule) {
125 mVsync.scheduledFrameTime =
126 mVsync.registration->schedule({.workDuration = mVsync.workDuration.get().count(),
127 .readyDuration = 0,
128 .earliestVsync = mVsync.lastCallbackTime.ns()});
129 }
Leon Scroggins IIIfb7ed562023-04-05 11:39:25 -0400130 return oldRegistration;
Ady Abraham55fa7272020-09-30 19:19:27 -0700131}
132
Ady Abraham011f8ba2022-11-22 15:09:07 -0800133void MessageQueue::destroyVsync() {
134 std::lock_guard lock(mVsync.mutex);
135 mVsync.tokenManager = nullptr;
136 mVsync.registration.reset();
137}
138
Ady Abraham55fa7272020-09-30 19:19:27 -0700139void MessageQueue::setDuration(std::chrono::nanoseconds workDuration) {
140 ATRACE_CALL();
141 std::lock_guard lock(mVsync.mutex);
142 mVsync.workDuration = workDuration;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800143 mVsync.scheduledFrameTime =
144 mVsync.registration->update({.workDuration = mVsync.workDuration.get().count(),
145 .readyDuration = 0,
146 .earliestVsync = mVsync.lastCallbackTime.ns()});
Ana Krulec98b5b242018-08-10 15:03:23 -0700147}
148
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800149void MessageQueue::waitMessage() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700150 do {
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800151 IPCThreadState::self()->flushCommands();
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800152 int32_t ret = mLooper->pollOnce(-1);
153 switch (ret) {
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800154 case Looper::POLL_WAKE:
155 case Looper::POLL_CALLBACK:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800156 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800157 case Looper::POLL_ERROR:
158 ALOGE("Looper::POLL_ERROR");
Pablo Ceballos53390e12015-08-04 11:25:59 -0700159 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800160 case Looper::POLL_TIMEOUT:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800161 // timeout (should not happen)
162 continue;
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800163 default:
164 // should not happen
Steve Blocke6f43dd2012-01-06 19:20:56 +0000165 ALOGE("Looper::pollOnce() returned unknown status %d", ret);
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800166 continue;
167 }
168 } while (true);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700169}
170
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700171void MessageQueue::postMessage(sp<MessageHandler>&& handler) {
172 mLooper->sendMessage(handler, Message());
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700173}
174
Chavi Weingarten076acac2023-01-19 17:20:43 +0000175void MessageQueue::postMessageDelayed(sp<MessageHandler>&& handler, nsecs_t uptimeDelay) {
176 mLooper->sendMessageDelayed(uptimeDelay, handler, Message());
177}
178
Dominik Laskowskif11728a2022-07-28 13:07:42 -0700179void MessageQueue::scheduleConfigure() {
180 struct ConfigureHandler : MessageHandler {
181 explicit ConfigureHandler(ICompositor& compositor) : compositor(compositor) {}
182
183 void handleMessage(const Message&) override { compositor.configure(); }
184
185 ICompositor& compositor;
186 };
187
188 // TODO(b/241285876): Batch configure tasks that happen within some duration.
189 postMessage(sp<ConfigureHandler>::make(mCompositor));
190}
191
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -0700192void MessageQueue::scheduleFrame() {
Ady Abraham55fa7272020-09-30 19:19:27 -0700193 ATRACE_CALL();
Dominik Laskowski208235c2020-12-03 15:38:51 -0800194
Dominik Laskowski208235c2020-12-03 15:38:51 -0800195 std::lock_guard lock(mVsync.mutex);
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700196 mVsync.scheduledFrameTime =
Ady Abraham562c2712021-05-07 15:10:42 -0700197 mVsync.registration->schedule({.workDuration = mVsync.workDuration.get().count(),
198 .readyDuration = 0,
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700199 .earliestVsync = mVsync.lastCallbackTime.ns()});
Mathias Agopian8aedd472012-01-24 16:39:14 -0800200}
201
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700202auto MessageQueue::getScheduledFrameTime() const -> std::optional<Clock::time_point> {
203 if (mHandler->isFramePending()) {
204 return Clock::now();
Ady Abraham562c2712021-05-07 15:10:42 -0700205 }
206
207 std::lock_guard lock(mVsync.mutex);
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700208 if (const auto time = mVsync.scheduledFrameTime) {
209 return Clock::time_point(std::chrono::nanoseconds(*time));
Ady Abraham562c2712021-05-07 15:10:42 -0700210 }
211
212 return std::nullopt;
213}
214
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700215} // namespace android::impl