blob: 043a53621608c62cd900499c835a41b39a00e784 [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 Laskowskie0e0cde2021-07-30 10:42:05 -070033void MessageQueue::Handler::dispatchComposite() {
34 if ((mEventMask.fetch_or(kComposite) & kComposite) == 0) {
35 mQueue.mLooper->sendMessage(this, Message(kComposite));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080036 }
37}
38
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070039void MessageQueue::Handler::dispatchCommit(int64_t vsyncId, nsecs_t expectedVsyncTime) {
40 if ((mEventMask.fetch_or(kCommit) & kCommit) == 0) {
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070041 mVsyncId = vsyncId;
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070042 mExpectedVsyncTime = expectedVsyncTime;
43 mQueue.mLooper->sendMessage(this, Message(kCommit));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080044 }
45}
46
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070047bool MessageQueue::Handler::isFramePending() const {
48 constexpr auto kPendingMask = kCommit | kComposite;
49 return (mEventMask.load() & kPendingMask) != 0;
Ady Abraham562c2712021-05-07 15:10:42 -070050}
51
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080052void MessageQueue::Handler::handleMessage(const Message& message) {
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070053 const nsecs_t frameTime = systemTime();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080054 switch (message.what) {
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070055 case kCommit:
56 mEventMask.fetch_and(~kCommit);
57 if (!mQueue.mCompositor.commit(frameTime, mVsyncId, mExpectedVsyncTime)) {
58 return;
59 }
60 // Composite immediately, rather than after pending tasks through scheduleComposite.
61 [[fallthrough]];
62 case kComposite:
63 mEventMask.fetch_and(~kComposite);
64 mQueue.mCompositor.composite(frameTime);
65 mQueue.mCompositor.sample();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080066 break;
67 }
68}
69
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070070MessageQueue::MessageQueue(ICompositor& compositor)
71 : MessageQueue(compositor, sp<Handler>::make(*this)) {}
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080072
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070073constexpr bool kAllowNonCallbacks = true;
74
75MessageQueue::MessageQueue(ICompositor& compositor, sp<Handler> handler)
76 : mCompositor(compositor),
77 mLooper(sp<Looper>::make(kAllowNonCallbacks)),
78 mHandler(std::move(handler)) {}
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080079
Ady Abraham55fa7272020-09-30 19:19:27 -070080// TODO(b/169865816): refactor VSyncInjections to use MessageQueue directly
81// and remove the EventThread from MessageQueue
Dominik Laskowski208235c2020-12-03 15:38:51 -080082void MessageQueue::setInjector(sp<EventThreadConnection> connection) {
83 auto& tube = mInjector.tube;
84
85 if (const int fd = tube.getFd(); fd >= 0) {
86 mLooper->removeFd(fd);
Ana Krulec98b5b242018-08-10 15:03:23 -070087 }
88
Dominik Laskowski208235c2020-12-03 15:38:51 -080089 if (connection) {
90 // The EventThreadConnection is retained when disabling injection, so avoid subsequently
91 // stealing invalid FDs. Note that the stolen FDs are kept open.
92 if (tube.getFd() < 0) {
93 connection->stealReceiveChannel(&tube);
94 } else {
95 ALOGW("Recycling channel for VSYNC injection.");
96 }
97
98 mLooper->addFd(
99 tube.getFd(), 0, Looper::EVENT_INPUT,
100 [](int, int, void* data) {
101 reinterpret_cast<MessageQueue*>(data)->injectorCallback();
102 return 1; // Keep registration.
103 },
104 this);
Ady Abraham55fa7272020-09-30 19:19:27 -0700105 }
Dominik Laskowski208235c2020-12-03 15:38:51 -0800106
107 std::lock_guard lock(mInjector.mutex);
108 mInjector.connection = std::move(connection);
Ady Abraham55fa7272020-09-30 19:19:27 -0700109}
110
111void MessageQueue::vsyncCallback(nsecs_t vsyncTime, nsecs_t targetWakeupTime, nsecs_t readyTime) {
112 ATRACE_CALL();
113 // Trace VSYNC-sf
114 mVsync.value = (mVsync.value + 1) % 2;
115
116 {
117 std::lock_guard lock(mVsync.mutex);
118 mVsync.lastCallbackTime = std::chrono::nanoseconds(vsyncTime);
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700119 mVsync.scheduledFrameTime.reset();
Ady Abraham55fa7272020-09-30 19:19:27 -0700120 }
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700121
122 const auto vsyncId = mVsync.tokenManager->generateTokenForPredictions(
123 {targetWakeupTime, readyTime, vsyncTime});
124
125 mHandler->dispatchCommit(vsyncId, vsyncTime);
Ady Abraham55fa7272020-09-30 19:19:27 -0700126}
127
128void MessageQueue::initVsync(scheduler::VSyncDispatch& dispatch,
129 frametimeline::TokenManager& tokenManager,
130 std::chrono::nanoseconds workDuration) {
131 setDuration(workDuration);
132 mVsync.tokenManager = &tokenManager;
133 mVsync.registration = std::make_unique<
134 scheduler::VSyncCallbackRegistration>(dispatch,
135 std::bind(&MessageQueue::vsyncCallback, this,
136 std::placeholders::_1,
137 std::placeholders::_2,
138 std::placeholders::_3),
139 "sf");
140}
141
142void MessageQueue::setDuration(std::chrono::nanoseconds workDuration) {
143 ATRACE_CALL();
144 std::lock_guard lock(mVsync.mutex);
145 mVsync.workDuration = workDuration;
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700146 if (mVsync.scheduledFrameTime) {
147 mVsync.scheduledFrameTime = mVsync.registration->schedule(
Ady Abraham562c2712021-05-07 15:10:42 -0700148 {mVsync.workDuration.get().count(),
149 /*readyDuration=*/0, mVsync.lastCallbackTime.count()});
Ady Abraham326ecde2020-11-06 15:05:53 -0800150 }
Ana Krulec98b5b242018-08-10 15:03:23 -0700151}
152
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800153void MessageQueue::waitMessage() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700154 do {
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800155 IPCThreadState::self()->flushCommands();
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800156 int32_t ret = mLooper->pollOnce(-1);
157 switch (ret) {
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800158 case Looper::POLL_WAKE:
159 case Looper::POLL_CALLBACK:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800160 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800161 case Looper::POLL_ERROR:
162 ALOGE("Looper::POLL_ERROR");
Pablo Ceballos53390e12015-08-04 11:25:59 -0700163 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800164 case Looper::POLL_TIMEOUT:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800165 // timeout (should not happen)
166 continue;
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800167 default:
168 // should not happen
Steve Blocke6f43dd2012-01-06 19:20:56 +0000169 ALOGE("Looper::pollOnce() returned unknown status %d", ret);
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800170 continue;
171 }
172 } while (true);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700173}
174
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700175void MessageQueue::postMessage(sp<MessageHandler>&& handler) {
176 mLooper->sendMessage(handler, Message());
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700177}
178
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700179void MessageQueue::scheduleCommit() {
Ady Abraham55fa7272020-09-30 19:19:27 -0700180 ATRACE_CALL();
Dominik Laskowski208235c2020-12-03 15:38:51 -0800181
182 {
183 std::lock_guard lock(mInjector.mutex);
184 if (CC_UNLIKELY(mInjector.connection)) {
185 ALOGD("%s while injecting VSYNC", __FUNCTION__);
186 mInjector.connection->requestNextVsync();
187 return;
188 }
Ady Abraham55fa7272020-09-30 19:19:27 -0700189 }
Dominik Laskowski208235c2020-12-03 15:38:51 -0800190
191 std::lock_guard lock(mVsync.mutex);
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700192 mVsync.scheduledFrameTime =
Ady Abraham562c2712021-05-07 15:10:42 -0700193 mVsync.registration->schedule({.workDuration = mVsync.workDuration.get().count(),
194 .readyDuration = 0,
195 .earliestVsync = mVsync.lastCallbackTime.count()});
Mathias Agopian8aedd472012-01-24 16:39:14 -0800196}
197
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700198void MessageQueue::scheduleComposite() {
199 mHandler->dispatchComposite();
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700200}
201
Dominik Laskowski208235c2020-12-03 15:38:51 -0800202void MessageQueue::injectorCallback() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800203 ssize_t n;
204 DisplayEventReceiver::Event buffer[8];
Dominik Laskowski208235c2020-12-03 15:38:51 -0800205 while ((n = DisplayEventReceiver::getEvents(&mInjector.tube, buffer, 8)) > 0) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800206 for (int i = 0; i < n; i++) {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800207 if (buffer[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700208 mHandler->dispatchCommit(buffer[i].vsync.vsyncId,
209 buffer[i].vsync.expectedVSyncTimestamp);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800210 break;
211 }
212 }
213 }
Mathias Agopian8aedd472012-01-24 16:39:14 -0800214}
215
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700216auto MessageQueue::getScheduledFrameTime() const -> std::optional<Clock::time_point> {
217 if (mHandler->isFramePending()) {
218 return Clock::now();
Ady Abraham562c2712021-05-07 15:10:42 -0700219 }
220
221 std::lock_guard lock(mVsync.mutex);
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700222 if (const auto time = mVsync.scheduledFrameTime) {
223 return Clock::time_point(std::chrono::nanoseconds(*time));
Ady Abraham562c2712021-05-07 15:10:42 -0700224 }
225
226 return std::nullopt;
227}
228
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700229} // namespace android::impl