blob: 925f7395341d7bb98d03302882162139848cb21f [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 III31d41412022-11-18 16:42:53 -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 III31d41412022-11-18 16:42:53 -050084 updateVsyncRegistrationLocked(std::move(dispatch));
85}
86
87void MessageQueue::updateVsyncRegistration(std::shared_ptr<scheduler::VSyncDispatch> dispatch) {
88 std::lock_guard lock(mVsync.mutex);
89 updateVsyncRegistrationLocked(std::move(dispatch));
90}
91
92void MessageQueue::updateVsyncRegistrationLocked(
93 std::shared_ptr<scheduler::VSyncDispatch> dispatch) {
94 const bool reschedule = mVsync.registration &&
95 mVsync.registration->cancel() == scheduler::CancelResult::Cancelled;
Ady Abraham55fa7272020-09-30 19:19:27 -070096 mVsync.registration = std::make_unique<
Leon Scroggins III31d41412022-11-18 16:42:53 -050097 scheduler::VSyncCallbackRegistration>(std::move(dispatch),
Ady Abraham55fa7272020-09-30 19:19:27 -070098 std::bind(&MessageQueue::vsyncCallback, this,
99 std::placeholders::_1,
100 std::placeholders::_2,
101 std::placeholders::_3),
102 "sf");
Leon Scroggins III31d41412022-11-18 16:42:53 -0500103 if (reschedule) {
104 mVsync.scheduledFrameTime =
105 mVsync.registration->schedule({.workDuration = mVsync.workDuration.get().count(),
106 .readyDuration = 0,
107 .earliestVsync = mVsync.lastCallbackTime.ns()});
108 }
Ady Abraham55fa7272020-09-30 19:19:27 -0700109}
110
Ady Abraham011f8ba2022-11-22 15:09:07 -0800111void MessageQueue::destroyVsync() {
112 std::lock_guard lock(mVsync.mutex);
113 mVsync.tokenManager = nullptr;
114 mVsync.registration.reset();
115}
116
Ady Abraham55fa7272020-09-30 19:19:27 -0700117void MessageQueue::setDuration(std::chrono::nanoseconds workDuration) {
118 ATRACE_CALL();
119 std::lock_guard lock(mVsync.mutex);
120 mVsync.workDuration = workDuration;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800121 mVsync.scheduledFrameTime =
122 mVsync.registration->update({.workDuration = mVsync.workDuration.get().count(),
123 .readyDuration = 0,
124 .earliestVsync = mVsync.lastCallbackTime.ns()});
Ana Krulec98b5b242018-08-10 15:03:23 -0700125}
126
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800127void MessageQueue::waitMessage() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700128 do {
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800129 IPCThreadState::self()->flushCommands();
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800130 int32_t ret = mLooper->pollOnce(-1);
131 switch (ret) {
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800132 case Looper::POLL_WAKE:
133 case Looper::POLL_CALLBACK:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800134 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800135 case Looper::POLL_ERROR:
136 ALOGE("Looper::POLL_ERROR");
Pablo Ceballos53390e12015-08-04 11:25:59 -0700137 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800138 case Looper::POLL_TIMEOUT:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800139 // timeout (should not happen)
140 continue;
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800141 default:
142 // should not happen
Steve Blocke6f43dd2012-01-06 19:20:56 +0000143 ALOGE("Looper::pollOnce() returned unknown status %d", ret);
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800144 continue;
145 }
146 } while (true);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700147}
148
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700149void MessageQueue::postMessage(sp<MessageHandler>&& handler) {
150 mLooper->sendMessage(handler, Message());
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700151}
152
Chavi Weingarten076acac2023-01-19 17:20:43 +0000153void MessageQueue::postMessageDelayed(sp<MessageHandler>&& handler, nsecs_t uptimeDelay) {
154 mLooper->sendMessageDelayed(uptimeDelay, handler, Message());
155}
156
Dominik Laskowskif11728a2022-07-28 13:07:42 -0700157void MessageQueue::scheduleConfigure() {
158 struct ConfigureHandler : MessageHandler {
159 explicit ConfigureHandler(ICompositor& compositor) : compositor(compositor) {}
160
161 void handleMessage(const Message&) override { compositor.configure(); }
162
163 ICompositor& compositor;
164 };
165
166 // TODO(b/241285876): Batch configure tasks that happen within some duration.
167 postMessage(sp<ConfigureHandler>::make(mCompositor));
168}
169
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -0700170void MessageQueue::scheduleFrame() {
Ady Abraham55fa7272020-09-30 19:19:27 -0700171 ATRACE_CALL();
Dominik Laskowski208235c2020-12-03 15:38:51 -0800172
Dominik Laskowski208235c2020-12-03 15:38:51 -0800173 std::lock_guard lock(mVsync.mutex);
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700174 mVsync.scheduledFrameTime =
Ady Abraham562c2712021-05-07 15:10:42 -0700175 mVsync.registration->schedule({.workDuration = mVsync.workDuration.get().count(),
176 .readyDuration = 0,
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700177 .earliestVsync = mVsync.lastCallbackTime.ns()});
Mathias Agopian8aedd472012-01-24 16:39:14 -0800178}
179
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700180auto MessageQueue::getScheduledFrameTime() const -> std::optional<Clock::time_point> {
181 if (mHandler->isFramePending()) {
182 return Clock::now();
Ady Abraham562c2712021-05-07 15:10:42 -0700183 }
184
185 std::lock_guard lock(mVsync.mutex);
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700186 if (const auto time = mVsync.scheduledFrameTime) {
187 return Clock::time_point(std::chrono::nanoseconds(*time));
Ady Abraham562c2712021-05-07 15:10:42 -0700188 }
189
190 return std::nullopt;
191}
192
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700193} // namespace android::impl