blob: dec8f59ee9a1a4cc5e79a95d4af54d83167026a3 [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
78void MessageQueue::initVsync(scheduler::VSyncDispatch& dispatch,
79 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;
84 mVsync.registration = std::make_unique<
85 scheduler::VSyncCallbackRegistration>(dispatch,
86 std::bind(&MessageQueue::vsyncCallback, this,
87 std::placeholders::_1,
88 std::placeholders::_2,
89 std::placeholders::_3),
90 "sf");
91}
92
Ady Abraham011f8ba2022-11-22 15:09:07 -080093void MessageQueue::destroyVsync() {
94 std::lock_guard lock(mVsync.mutex);
95 mVsync.tokenManager = nullptr;
96 mVsync.registration.reset();
97}
98
Ady Abraham55fa7272020-09-30 19:19:27 -070099void MessageQueue::setDuration(std::chrono::nanoseconds workDuration) {
100 ATRACE_CALL();
101 std::lock_guard lock(mVsync.mutex);
102 mVsync.workDuration = workDuration;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800103 mVsync.scheduledFrameTime =
104 mVsync.registration->update({.workDuration = mVsync.workDuration.get().count(),
105 .readyDuration = 0,
106 .earliestVsync = mVsync.lastCallbackTime.ns()});
Ana Krulec98b5b242018-08-10 15:03:23 -0700107}
108
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800109void MessageQueue::waitMessage() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700110 do {
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800111 IPCThreadState::self()->flushCommands();
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800112 int32_t ret = mLooper->pollOnce(-1);
113 switch (ret) {
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800114 case Looper::POLL_WAKE:
115 case Looper::POLL_CALLBACK:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800116 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800117 case Looper::POLL_ERROR:
118 ALOGE("Looper::POLL_ERROR");
Pablo Ceballos53390e12015-08-04 11:25:59 -0700119 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800120 case Looper::POLL_TIMEOUT:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800121 // timeout (should not happen)
122 continue;
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800123 default:
124 // should not happen
Steve Blocke6f43dd2012-01-06 19:20:56 +0000125 ALOGE("Looper::pollOnce() returned unknown status %d", ret);
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800126 continue;
127 }
128 } while (true);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700129}
130
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700131void MessageQueue::postMessage(sp<MessageHandler>&& handler) {
132 mLooper->sendMessage(handler, Message());
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700133}
134
Chavi Weingarten076acac2023-01-19 17:20:43 +0000135void MessageQueue::postMessageDelayed(sp<MessageHandler>&& handler, nsecs_t uptimeDelay) {
136 mLooper->sendMessageDelayed(uptimeDelay, handler, Message());
137}
138
Dominik Laskowskif11728a2022-07-28 13:07:42 -0700139void MessageQueue::scheduleConfigure() {
140 struct ConfigureHandler : MessageHandler {
141 explicit ConfigureHandler(ICompositor& compositor) : compositor(compositor) {}
142
143 void handleMessage(const Message&) override { compositor.configure(); }
144
145 ICompositor& compositor;
146 };
147
148 // TODO(b/241285876): Batch configure tasks that happen within some duration.
149 postMessage(sp<ConfigureHandler>::make(mCompositor));
150}
151
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -0700152void MessageQueue::scheduleFrame() {
Ady Abraham55fa7272020-09-30 19:19:27 -0700153 ATRACE_CALL();
Dominik Laskowski208235c2020-12-03 15:38:51 -0800154
Dominik Laskowski208235c2020-12-03 15:38:51 -0800155 std::lock_guard lock(mVsync.mutex);
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700156 mVsync.scheduledFrameTime =
Ady Abraham562c2712021-05-07 15:10:42 -0700157 mVsync.registration->schedule({.workDuration = mVsync.workDuration.get().count(),
158 .readyDuration = 0,
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700159 .earliestVsync = mVsync.lastCallbackTime.ns()});
Mathias Agopian8aedd472012-01-24 16:39:14 -0800160}
161
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700162auto MessageQueue::getScheduledFrameTime() const -> std::optional<Clock::time_point> {
163 if (mHandler->isFramePending()) {
164 return Clock::now();
Ady Abraham562c2712021-05-07 15:10:42 -0700165 }
166
167 std::lock_guard lock(mVsync.mutex);
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700168 if (const auto time = mVsync.scheduledFrameTime) {
169 return Clock::time_point(std::chrono::nanoseconds(*time));
Ady Abraham562c2712021-05-07 15:10:42 -0700170 }
171
172 return std::nullopt;
173}
174
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700175} // namespace android::impl