blob: e1ac301622413c92945e868142a51d01f36b9821 [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>
20
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
Lloyd Pique3fcdef12018-01-22 17:14:00 -080025#include <gui/DisplayEventReceiver.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 -070060// TODO(b/169865816): refactor VSyncInjections to use MessageQueue directly
61// and remove the EventThread from MessageQueue
Dominik Laskowski208235c2020-12-03 15:38:51 -080062void MessageQueue::setInjector(sp<EventThreadConnection> connection) {
63 auto& tube = mInjector.tube;
64
65 if (const int fd = tube.getFd(); fd >= 0) {
66 mLooper->removeFd(fd);
Ana Krulec98b5b242018-08-10 15:03:23 -070067 }
68
Dominik Laskowski208235c2020-12-03 15:38:51 -080069 if (connection) {
70 // The EventThreadConnection is retained when disabling injection, so avoid subsequently
71 // stealing invalid FDs. Note that the stolen FDs are kept open.
72 if (tube.getFd() < 0) {
73 connection->stealReceiveChannel(&tube);
74 } else {
75 ALOGW("Recycling channel for VSYNC injection.");
76 }
77
78 mLooper->addFd(
79 tube.getFd(), 0, Looper::EVENT_INPUT,
80 [](int, int, void* data) {
81 reinterpret_cast<MessageQueue*>(data)->injectorCallback();
82 return 1; // Keep registration.
83 },
84 this);
Ady Abraham55fa7272020-09-30 19:19:27 -070085 }
Dominik Laskowski208235c2020-12-03 15:38:51 -080086
87 std::lock_guard lock(mInjector.mutex);
88 mInjector.connection = std::move(connection);
Ady Abraham55fa7272020-09-30 19:19:27 -070089}
90
91void MessageQueue::vsyncCallback(nsecs_t vsyncTime, nsecs_t targetWakeupTime, nsecs_t readyTime) {
92 ATRACE_CALL();
93 // Trace VSYNC-sf
94 mVsync.value = (mVsync.value + 1) % 2;
95
Dominik Laskowski08fbd852022-07-14 08:53:42 -070096 const auto expectedVsyncTime = TimePoint::fromNs(vsyncTime);
Ady Abraham55fa7272020-09-30 19:19:27 -070097 {
98 std::lock_guard lock(mVsync.mutex);
Dominik Laskowski08fbd852022-07-14 08:53:42 -070099 mVsync.lastCallbackTime = expectedVsyncTime;
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700100 mVsync.scheduledFrameTime.reset();
Ady Abraham55fa7272020-09-30 19:19:27 -0700101 }
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700102
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700103 const auto vsyncId = VsyncId{mVsync.tokenManager->generateTokenForPredictions(
104 {targetWakeupTime, readyTime, vsyncTime})};
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700105
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700106 mHandler->dispatchFrame(vsyncId, expectedVsyncTime);
Ady Abraham55fa7272020-09-30 19:19:27 -0700107}
108
109void MessageQueue::initVsync(scheduler::VSyncDispatch& dispatch,
110 frametimeline::TokenManager& tokenManager,
111 std::chrono::nanoseconds workDuration) {
112 setDuration(workDuration);
113 mVsync.tokenManager = &tokenManager;
114 mVsync.registration = std::make_unique<
115 scheduler::VSyncCallbackRegistration>(dispatch,
116 std::bind(&MessageQueue::vsyncCallback, this,
117 std::placeholders::_1,
118 std::placeholders::_2,
119 std::placeholders::_3),
120 "sf");
121}
122
123void MessageQueue::setDuration(std::chrono::nanoseconds workDuration) {
124 ATRACE_CALL();
125 std::lock_guard lock(mVsync.mutex);
126 mVsync.workDuration = workDuration;
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700127 if (mVsync.scheduledFrameTime) {
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700128 mVsync.scheduledFrameTime =
129 mVsync.registration->schedule({.workDuration = mVsync.workDuration.get().count(),
130 .readyDuration = 0,
131 .earliestVsync = mVsync.lastCallbackTime.ns()});
Ady Abraham326ecde2020-11-06 15:05:53 -0800132 }
Ana Krulec98b5b242018-08-10 15:03:23 -0700133}
134
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800135void MessageQueue::waitMessage() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700136 do {
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800137 IPCThreadState::self()->flushCommands();
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800138 int32_t ret = mLooper->pollOnce(-1);
139 switch (ret) {
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800140 case Looper::POLL_WAKE:
141 case Looper::POLL_CALLBACK:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800142 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800143 case Looper::POLL_ERROR:
144 ALOGE("Looper::POLL_ERROR");
Pablo Ceballos53390e12015-08-04 11:25:59 -0700145 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800146 case Looper::POLL_TIMEOUT:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800147 // timeout (should not happen)
148 continue;
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800149 default:
150 // should not happen
Steve Blocke6f43dd2012-01-06 19:20:56 +0000151 ALOGE("Looper::pollOnce() returned unknown status %d", ret);
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800152 continue;
153 }
154 } while (true);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700155}
156
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700157void MessageQueue::postMessage(sp<MessageHandler>&& handler) {
158 mLooper->sendMessage(handler, Message());
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700159}
160
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -0700161void MessageQueue::scheduleFrame() {
Ady Abraham55fa7272020-09-30 19:19:27 -0700162 ATRACE_CALL();
Dominik Laskowski208235c2020-12-03 15:38:51 -0800163
164 {
165 std::lock_guard lock(mInjector.mutex);
166 if (CC_UNLIKELY(mInjector.connection)) {
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700167 ALOGD("%s while injecting VSYNC", __func__);
Dominik Laskowski208235c2020-12-03 15:38:51 -0800168 mInjector.connection->requestNextVsync();
169 return;
170 }
Ady Abraham55fa7272020-09-30 19:19:27 -0700171 }
Dominik Laskowski208235c2020-12-03 15:38:51 -0800172
173 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 Laskowski208235c2020-12-03 15:38:51 -0800180void MessageQueue::injectorCallback() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800181 ssize_t n;
182 DisplayEventReceiver::Event buffer[8];
Dominik Laskowski208235c2020-12-03 15:38:51 -0800183 while ((n = DisplayEventReceiver::getEvents(&mInjector.tube, buffer, 8)) > 0) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800184 for (int i = 0; i < n; i++) {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800185 if (buffer[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700186 auto& vsync = buffer[i].vsync.vsyncData;
187 mHandler->dispatchFrame(VsyncId{vsync.preferredVsyncId()},
188 TimePoint::fromNs(
189 vsync.preferredExpectedPresentationTime()));
Mathias Agopian8aedd472012-01-24 16:39:14 -0800190 break;
191 }
192 }
193 }
Mathias Agopian8aedd472012-01-24 16:39:14 -0800194}
195
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700196auto MessageQueue::getScheduledFrameTime() const -> std::optional<Clock::time_point> {
197 if (mHandler->isFramePending()) {
198 return Clock::now();
Ady Abraham562c2712021-05-07 15:10:42 -0700199 }
200
201 std::lock_guard lock(mVsync.mutex);
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700202 if (const auto time = mVsync.scheduledFrameTime) {
203 return Clock::time_point(std::chrono::nanoseconds(*time));
Ady Abraham562c2712021-05-07 15:10:42 -0700204 }
205
206 return std::nullopt;
207}
208
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700209} // namespace android::impl