blob: 4d51125156397db8a9560695bfac1ee4e54cf76a [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 Agopian99ce5cd2012-01-31 18:24:27 -080030#include "SurfaceFlinger.h"
Mathias Agopianf1d8e872009-04-20 19:39:12 -070031
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070032namespace android::impl {
Lloyd Pique3fcdef12018-01-22 17:14:00 -080033
Mathias Agopian4fec8732012-06-29 14:12:52 -070034void MessageQueue::Handler::dispatchRefresh() {
Ady Abraham562c2712021-05-07 15:10:42 -070035 if ((mEventMask.fetch_or(eventMaskRefresh) & eventMaskRefresh) == 0) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080036 mQueue.mLooper->sendMessage(this, Message(MessageQueue::REFRESH));
37 }
38}
39
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070040void MessageQueue::Handler::dispatchInvalidate(int64_t vsyncId, nsecs_t expectedVSyncTimestamp) {
Ady Abraham562c2712021-05-07 15:10:42 -070041 if ((mEventMask.fetch_or(eventMaskInvalidate) & eventMaskInvalidate) == 0) {
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070042 mVsyncId = vsyncId;
Ady Abraham5facfb12020-04-22 15:18:31 -070043 mExpectedVSyncTime = expectedVSyncTimestamp;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080044 mQueue.mLooper->sendMessage(this, Message(MessageQueue::INVALIDATE));
45 }
46}
47
Ady Abraham562c2712021-05-07 15:10:42 -070048bool MessageQueue::Handler::invalidatePending() {
49 constexpr auto pendingMask = eventMaskInvalidate | eventMaskRefresh;
50 return (mEventMask.load() & pendingMask) != 0;
51}
52
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080053void MessageQueue::Handler::handleMessage(const Message& message) {
54 switch (message.what) {
55 case INVALIDATE:
Ady Abraham562c2712021-05-07 15:10:42 -070056 mEventMask.fetch_and(~eventMaskInvalidate);
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070057 mQueue.mFlinger->onMessageReceived(message.what, mVsyncId, mExpectedVSyncTime);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080058 break;
59 case REFRESH:
Ady Abraham562c2712021-05-07 15:10:42 -070060 mEventMask.fetch_and(~eventMaskRefresh);
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070061 mQueue.mFlinger->onMessageReceived(message.what, mVsyncId, mExpectedVSyncTime);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080062 break;
63 }
64}
65
66// ---------------------------------------------------------------------------
67
Lloyd Pique78ce4182018-01-31 16:39:51 -080068void MessageQueue::init(const sp<SurfaceFlinger>& flinger) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080069 mFlinger = flinger;
70 mLooper = new Looper(true);
71 mHandler = new Handler(*this);
72}
73
Ady Abraham55fa7272020-09-30 19:19:27 -070074// TODO(b/169865816): refactor VSyncInjections to use MessageQueue directly
75// and remove the EventThread from MessageQueue
Dominik Laskowski208235c2020-12-03 15:38:51 -080076void MessageQueue::setInjector(sp<EventThreadConnection> connection) {
77 auto& tube = mInjector.tube;
78
79 if (const int fd = tube.getFd(); fd >= 0) {
80 mLooper->removeFd(fd);
Ana Krulec98b5b242018-08-10 15:03:23 -070081 }
82
Dominik Laskowski208235c2020-12-03 15:38:51 -080083 if (connection) {
84 // The EventThreadConnection is retained when disabling injection, so avoid subsequently
85 // stealing invalid FDs. Note that the stolen FDs are kept open.
86 if (tube.getFd() < 0) {
87 connection->stealReceiveChannel(&tube);
88 } else {
89 ALOGW("Recycling channel for VSYNC injection.");
90 }
91
92 mLooper->addFd(
93 tube.getFd(), 0, Looper::EVENT_INPUT,
94 [](int, int, void* data) {
95 reinterpret_cast<MessageQueue*>(data)->injectorCallback();
96 return 1; // Keep registration.
97 },
98 this);
Ady Abraham55fa7272020-09-30 19:19:27 -070099 }
Dominik Laskowski208235c2020-12-03 15:38:51 -0800100
101 std::lock_guard lock(mInjector.mutex);
102 mInjector.connection = std::move(connection);
Ady Abraham55fa7272020-09-30 19:19:27 -0700103}
104
105void MessageQueue::vsyncCallback(nsecs_t vsyncTime, nsecs_t targetWakeupTime, nsecs_t readyTime) {
106 ATRACE_CALL();
107 // Trace VSYNC-sf
108 mVsync.value = (mVsync.value + 1) % 2;
109
110 {
111 std::lock_guard lock(mVsync.mutex);
112 mVsync.lastCallbackTime = std::chrono::nanoseconds(vsyncTime);
Ady Abraham562c2712021-05-07 15:10:42 -0700113 mVsync.scheduled = false;
Ady Abraham55fa7272020-09-30 19:19:27 -0700114 }
115 mHandler->dispatchInvalidate(mVsync.tokenManager->generateTokenForPredictions(
116 {targetWakeupTime, readyTime, vsyncTime}),
117 vsyncTime);
118}
119
120void MessageQueue::initVsync(scheduler::VSyncDispatch& dispatch,
121 frametimeline::TokenManager& tokenManager,
122 std::chrono::nanoseconds workDuration) {
123 setDuration(workDuration);
124 mVsync.tokenManager = &tokenManager;
125 mVsync.registration = std::make_unique<
126 scheduler::VSyncCallbackRegistration>(dispatch,
127 std::bind(&MessageQueue::vsyncCallback, this,
128 std::placeholders::_1,
129 std::placeholders::_2,
130 std::placeholders::_3),
131 "sf");
132}
133
134void MessageQueue::setDuration(std::chrono::nanoseconds workDuration) {
135 ATRACE_CALL();
136 std::lock_guard lock(mVsync.mutex);
137 mVsync.workDuration = workDuration;
Ady Abraham562c2712021-05-07 15:10:42 -0700138 if (mVsync.scheduled) {
139 mVsync.expectedWakeupTime = mVsync.registration->schedule(
140 {mVsync.workDuration.get().count(),
141 /*readyDuration=*/0, mVsync.lastCallbackTime.count()});
Ady Abraham326ecde2020-11-06 15:05:53 -0800142 }
Ana Krulec98b5b242018-08-10 15:03:23 -0700143}
144
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800145void MessageQueue::waitMessage() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700146 do {
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800147 IPCThreadState::self()->flushCommands();
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800148 int32_t ret = mLooper->pollOnce(-1);
149 switch (ret) {
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800150 case Looper::POLL_WAKE:
151 case Looper::POLL_CALLBACK:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800152 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800153 case Looper::POLL_ERROR:
154 ALOGE("Looper::POLL_ERROR");
Pablo Ceballos53390e12015-08-04 11:25:59 -0700155 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800156 case Looper::POLL_TIMEOUT:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800157 // timeout (should not happen)
158 continue;
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800159 default:
160 // should not happen
Steve Blocke6f43dd2012-01-06 19:20:56 +0000161 ALOGE("Looper::pollOnce() returned unknown status %d", ret);
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800162 continue;
163 }
164 } while (true);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700165}
166
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700167void MessageQueue::postMessage(sp<MessageHandler>&& handler) {
168 mLooper->sendMessage(handler, Message());
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700169}
170
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800171void MessageQueue::invalidate() {
Ady Abraham55fa7272020-09-30 19:19:27 -0700172 ATRACE_CALL();
Dominik Laskowski208235c2020-12-03 15:38:51 -0800173
174 {
175 std::lock_guard lock(mInjector.mutex);
176 if (CC_UNLIKELY(mInjector.connection)) {
177 ALOGD("%s while injecting VSYNC", __FUNCTION__);
178 mInjector.connection->requestNextVsync();
179 return;
180 }
Ady Abraham55fa7272020-09-30 19:19:27 -0700181 }
Dominik Laskowski208235c2020-12-03 15:38:51 -0800182
183 std::lock_guard lock(mVsync.mutex);
Ady Abraham562c2712021-05-07 15:10:42 -0700184 mVsync.scheduled = true;
185 mVsync.expectedWakeupTime =
186 mVsync.registration->schedule({.workDuration = mVsync.workDuration.get().count(),
187 .readyDuration = 0,
188 .earliestVsync = mVsync.lastCallbackTime.count()});
Mathias Agopian8aedd472012-01-24 16:39:14 -0800189}
190
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800191void MessageQueue::refresh() {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700192 mHandler->dispatchRefresh();
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700193}
194
Dominik Laskowski208235c2020-12-03 15:38:51 -0800195void MessageQueue::injectorCallback() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800196 ssize_t n;
197 DisplayEventReceiver::Event buffer[8];
Dominik Laskowski208235c2020-12-03 15:38:51 -0800198 while ((n = DisplayEventReceiver::getEvents(&mInjector.tube, buffer, 8)) > 0) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800199 for (int i = 0; i < n; i++) {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800200 if (buffer[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700201 mHandler->dispatchInvalidate(buffer[i].vsync.vsyncId,
202 buffer[i].vsync.expectedVSyncTimestamp);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800203 break;
204 }
205 }
206 }
Mathias Agopian8aedd472012-01-24 16:39:14 -0800207}
208
Ady Abraham562c2712021-05-07 15:10:42 -0700209std::optional<std::chrono::steady_clock::time_point> MessageQueue::nextExpectedInvalidate() {
210 if (mHandler->invalidatePending()) {
211 return std::chrono::steady_clock::now();
212 }
213
214 std::lock_guard lock(mVsync.mutex);
215 if (mVsync.scheduled) {
216 LOG_ALWAYS_FATAL_IF(!mVsync.expectedWakeupTime.has_value(), "callback was never scheduled");
217 const auto expectedWakeupTime = std::chrono::nanoseconds(*mVsync.expectedWakeupTime);
218 return std::optional<std::chrono::steady_clock::time_point>(expectedWakeupTime);
219 }
220
221 return std::nullopt;
222}
223
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700224} // namespace android::impl