blob: 7457b840112f0a18caf3ffbc7d387ac8e92123f4 [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) {
Ady Abraham011f8ba2022-11-22 15:09:07 -080081 std::lock_guard lock(mVsync.mutex);
82 mVsync.workDuration = workDuration;
Ady Abraham55fa7272020-09-30 19:19:27 -070083 mVsync.tokenManager = &tokenManager;
Leon Scroggins III67388622023-02-06 20:36:20 -050084 onNewVsyncScheduleLocked(std::move(dispatch));
85}
86
87void MessageQueue::onNewVsyncSchedule(std::shared_ptr<scheduler::VSyncDispatch> dispatch) {
88 std::lock_guard lock(mVsync.mutex);
89 onNewVsyncScheduleLocked(std::move(dispatch));
90}
91
92void MessageQueue::onNewVsyncScheduleLocked(std::shared_ptr<scheduler::VSyncDispatch> dispatch) {
93 const bool reschedule = mVsync.registration &&
94 mVsync.registration->cancel() == scheduler::CancelResult::Cancelled;
Ady Abraham55fa7272020-09-30 19:19:27 -070095 mVsync.registration = std::make_unique<
Leon Scroggins III67388622023-02-06 20:36:20 -050096 scheduler::VSyncCallbackRegistration>(std::move(dispatch),
Ady Abraham55fa7272020-09-30 19:19:27 -070097 std::bind(&MessageQueue::vsyncCallback, this,
98 std::placeholders::_1,
99 std::placeholders::_2,
100 std::placeholders::_3),
101 "sf");
Leon Scroggins III67388622023-02-06 20:36:20 -0500102 if (reschedule) {
103 mVsync.scheduledFrameTime =
104 mVsync.registration->schedule({.workDuration = mVsync.workDuration.get().count(),
105 .readyDuration = 0,
106 .earliestVsync = mVsync.lastCallbackTime.ns()});
107 }
Ady Abraham55fa7272020-09-30 19:19:27 -0700108}
109
Ady Abraham011f8ba2022-11-22 15:09:07 -0800110void MessageQueue::destroyVsync() {
111 std::lock_guard lock(mVsync.mutex);
112 mVsync.tokenManager = nullptr;
113 mVsync.registration.reset();
114}
115
Ady Abraham55fa7272020-09-30 19:19:27 -0700116void MessageQueue::setDuration(std::chrono::nanoseconds workDuration) {
117 ATRACE_CALL();
118 std::lock_guard lock(mVsync.mutex);
119 mVsync.workDuration = workDuration;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800120 mVsync.scheduledFrameTime =
121 mVsync.registration->update({.workDuration = mVsync.workDuration.get().count(),
122 .readyDuration = 0,
123 .earliestVsync = mVsync.lastCallbackTime.ns()});
Ana Krulec98b5b242018-08-10 15:03:23 -0700124}
125
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800126void MessageQueue::waitMessage() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700127 do {
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800128 IPCThreadState::self()->flushCommands();
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800129 int32_t ret = mLooper->pollOnce(-1);
130 switch (ret) {
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800131 case Looper::POLL_WAKE:
132 case Looper::POLL_CALLBACK:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800133 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800134 case Looper::POLL_ERROR:
135 ALOGE("Looper::POLL_ERROR");
Pablo Ceballos53390e12015-08-04 11:25:59 -0700136 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800137 case Looper::POLL_TIMEOUT:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800138 // timeout (should not happen)
139 continue;
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800140 default:
141 // should not happen
Steve Blocke6f43dd2012-01-06 19:20:56 +0000142 ALOGE("Looper::pollOnce() returned unknown status %d", ret);
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800143 continue;
144 }
145 } while (true);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700146}
147
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700148void MessageQueue::postMessage(sp<MessageHandler>&& handler) {
149 mLooper->sendMessage(handler, Message());
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700150}
151
Chavi Weingarten076acac2023-01-19 17:20:43 +0000152void MessageQueue::postMessageDelayed(sp<MessageHandler>&& handler, nsecs_t uptimeDelay) {
153 mLooper->sendMessageDelayed(uptimeDelay, handler, Message());
154}
155
Dominik Laskowskif11728a2022-07-28 13:07:42 -0700156void MessageQueue::scheduleConfigure() {
157 struct ConfigureHandler : MessageHandler {
158 explicit ConfigureHandler(ICompositor& compositor) : compositor(compositor) {}
159
160 void handleMessage(const Message&) override { compositor.configure(); }
161
162 ICompositor& compositor;
163 };
164
165 // TODO(b/241285876): Batch configure tasks that happen within some duration.
166 postMessage(sp<ConfigureHandler>::make(mCompositor));
167}
168
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -0700169void MessageQueue::scheduleFrame() {
Ady Abraham55fa7272020-09-30 19:19:27 -0700170 ATRACE_CALL();
Dominik Laskowski208235c2020-12-03 15:38:51 -0800171
Dominik Laskowski208235c2020-12-03 15:38:51 -0800172 std::lock_guard lock(mVsync.mutex);
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700173 mVsync.scheduledFrameTime =
Ady Abraham562c2712021-05-07 15:10:42 -0700174 mVsync.registration->schedule({.workDuration = mVsync.workDuration.get().count(),
175 .readyDuration = 0,
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700176 .earliestVsync = mVsync.lastCallbackTime.ns()});
Mathias Agopian8aedd472012-01-24 16:39:14 -0800177}
178
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700179auto MessageQueue::getScheduledFrameTime() const -> std::optional<Clock::time_point> {
180 if (mHandler->isFramePending()) {
181 return Clock::now();
Ady Abraham562c2712021-05-07 15:10:42 -0700182 }
183
184 std::lock_guard lock(mVsync.mutex);
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700185 if (const auto time = mVsync.scheduledFrameTime) {
186 return Clock::time_point(std::chrono::nanoseconds(*time));
Ady Abraham562c2712021-05-07 15:10:42 -0700187 }
188
189 return std::nullopt;
190}
191
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700192} // namespace android::impl