blob: 7ff0ddfab402aedb025b491f62763e88beee7e96 [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 Agopian8aedd472012-01-24 16:39:14 -080026#include <gui/IDisplayEventConnection.h>
Mathias Agopianf1d8e872009-04-20 19:39:12 -070027
Mathias Agopian8aedd472012-01-24 16:39:14 -080028#include "EventThread.h"
Ady Abraham55fa7272020-09-30 19:19:27 -070029#include "FrameTimeline.h"
Lloyd Pique78ce4182018-01-31 16:39:51 -080030#include "MessageQueue.h"
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080031#include "SurfaceFlinger.h"
Mathias Agopianf1d8e872009-04-20 19:39:12 -070032
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070033namespace android::impl {
Lloyd Pique3fcdef12018-01-22 17:14:00 -080034
Mathias Agopian4fec8732012-06-29 14:12:52 -070035void MessageQueue::Handler::dispatchRefresh() {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080036 if ((android_atomic_or(eventMaskRefresh, &mEventMask) & eventMaskRefresh) == 0) {
37 mQueue.mLooper->sendMessage(this, Message(MessageQueue::REFRESH));
38 }
39}
40
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070041void MessageQueue::Handler::dispatchInvalidate(int64_t vsyncId, nsecs_t expectedVSyncTimestamp) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080042 if ((android_atomic_or(eventMaskInvalidate, &mEventMask) & eventMaskInvalidate) == 0) {
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070043 mVsyncId = vsyncId;
Ady Abraham5facfb12020-04-22 15:18:31 -070044 mExpectedVSyncTime = expectedVSyncTimestamp;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080045 mQueue.mLooper->sendMessage(this, Message(MessageQueue::INVALIDATE));
46 }
47}
48
49void MessageQueue::Handler::handleMessage(const Message& message) {
50 switch (message.what) {
51 case INVALIDATE:
52 android_atomic_and(~eventMaskInvalidate, &mEventMask);
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070053 mQueue.mFlinger->onMessageReceived(message.what, mVsyncId, mExpectedVSyncTime);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080054 break;
55 case REFRESH:
56 android_atomic_and(~eventMaskRefresh, &mEventMask);
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 }
60}
61
62// ---------------------------------------------------------------------------
63
Lloyd Pique78ce4182018-01-31 16:39:51 -080064void MessageQueue::init(const sp<SurfaceFlinger>& flinger) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080065 mFlinger = flinger;
66 mLooper = new Looper(true);
67 mHandler = new Handler(*this);
68}
69
Ady Abraham55fa7272020-09-30 19:19:27 -070070// TODO(b/169865816): refactor VSyncInjections to use MessageQueue directly
71// and remove the EventThread from MessageQueue
Dominik Laskowski208235c2020-12-03 15:38:51 -080072void MessageQueue::setInjector(sp<EventThreadConnection> connection) {
73 auto& tube = mInjector.tube;
74
75 if (const int fd = tube.getFd(); fd >= 0) {
76 mLooper->removeFd(fd);
Ana Krulec98b5b242018-08-10 15:03:23 -070077 }
78
Dominik Laskowski208235c2020-12-03 15:38:51 -080079 if (connection) {
80 // The EventThreadConnection is retained when disabling injection, so avoid subsequently
81 // stealing invalid FDs. Note that the stolen FDs are kept open.
82 if (tube.getFd() < 0) {
83 connection->stealReceiveChannel(&tube);
84 } else {
85 ALOGW("Recycling channel for VSYNC injection.");
86 }
87
88 mLooper->addFd(
89 tube.getFd(), 0, Looper::EVENT_INPUT,
90 [](int, int, void* data) {
91 reinterpret_cast<MessageQueue*>(data)->injectorCallback();
92 return 1; // Keep registration.
93 },
94 this);
Ady Abraham55fa7272020-09-30 19:19:27 -070095 }
Dominik Laskowski208235c2020-12-03 15:38:51 -080096
97 std::lock_guard lock(mInjector.mutex);
98 mInjector.connection = std::move(connection);
Ady Abraham55fa7272020-09-30 19:19:27 -070099}
100
101void MessageQueue::vsyncCallback(nsecs_t vsyncTime, nsecs_t targetWakeupTime, nsecs_t readyTime) {
102 ATRACE_CALL();
103 // Trace VSYNC-sf
104 mVsync.value = (mVsync.value + 1) % 2;
105
106 {
107 std::lock_guard lock(mVsync.mutex);
108 mVsync.lastCallbackTime = std::chrono::nanoseconds(vsyncTime);
Ady Abraham326ecde2020-11-06 15:05:53 -0800109 mVsync.mScheduled = false;
Ady Abraham55fa7272020-09-30 19:19:27 -0700110 }
111 mHandler->dispatchInvalidate(mVsync.tokenManager->generateTokenForPredictions(
112 {targetWakeupTime, readyTime, vsyncTime}),
113 vsyncTime);
114}
115
116void MessageQueue::initVsync(scheduler::VSyncDispatch& dispatch,
117 frametimeline::TokenManager& tokenManager,
118 std::chrono::nanoseconds workDuration) {
119 setDuration(workDuration);
120 mVsync.tokenManager = &tokenManager;
121 mVsync.registration = std::make_unique<
122 scheduler::VSyncCallbackRegistration>(dispatch,
123 std::bind(&MessageQueue::vsyncCallback, this,
124 std::placeholders::_1,
125 std::placeholders::_2,
126 std::placeholders::_3),
127 "sf");
128}
129
130void MessageQueue::setDuration(std::chrono::nanoseconds workDuration) {
131 ATRACE_CALL();
132 std::lock_guard lock(mVsync.mutex);
133 mVsync.workDuration = workDuration;
Ady Abraham326ecde2020-11-06 15:05:53 -0800134 if (mVsync.mScheduled) {
135 mVsync.registration->schedule({mVsync.workDuration.get().count(), /*readyDuration=*/0,
136 mVsync.lastCallbackTime.count()});
137 }
Ana Krulec98b5b242018-08-10 15:03:23 -0700138}
139
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800140void MessageQueue::waitMessage() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700141 do {
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800142 IPCThreadState::self()->flushCommands();
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800143 int32_t ret = mLooper->pollOnce(-1);
144 switch (ret) {
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800145 case Looper::POLL_WAKE:
146 case Looper::POLL_CALLBACK:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800147 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800148 case Looper::POLL_ERROR:
149 ALOGE("Looper::POLL_ERROR");
Pablo Ceballos53390e12015-08-04 11:25:59 -0700150 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800151 case Looper::POLL_TIMEOUT:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800152 // timeout (should not happen)
153 continue;
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800154 default:
155 // should not happen
Steve Blocke6f43dd2012-01-06 19:20:56 +0000156 ALOGE("Looper::pollOnce() returned unknown status %d", ret);
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800157 continue;
158 }
159 } while (true);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700160}
161
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700162void MessageQueue::postMessage(sp<MessageHandler>&& handler) {
163 mLooper->sendMessage(handler, Message());
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700164}
165
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800166void MessageQueue::invalidate() {
Ady Abraham55fa7272020-09-30 19:19:27 -0700167 ATRACE_CALL();
Dominik Laskowski208235c2020-12-03 15:38:51 -0800168
169 {
170 std::lock_guard lock(mInjector.mutex);
171 if (CC_UNLIKELY(mInjector.connection)) {
172 ALOGD("%s while injecting VSYNC", __FUNCTION__);
173 mInjector.connection->requestNextVsync();
174 return;
175 }
Ady Abraham55fa7272020-09-30 19:19:27 -0700176 }
Dominik Laskowski208235c2020-12-03 15:38:51 -0800177
178 std::lock_guard lock(mVsync.mutex);
179 mVsync.mScheduled = true;
180 mVsync.registration->schedule({.workDuration = mVsync.workDuration.get().count(),
181 .readyDuration = 0,
182 .earliestVsync = mVsync.lastCallbackTime.count()});
Mathias Agopian8aedd472012-01-24 16:39:14 -0800183}
184
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800185void MessageQueue::refresh() {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700186 mHandler->dispatchRefresh();
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700187}
188
Dominik Laskowski208235c2020-12-03 15:38:51 -0800189void MessageQueue::injectorCallback() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800190 ssize_t n;
191 DisplayEventReceiver::Event buffer[8];
Dominik Laskowski208235c2020-12-03 15:38:51 -0800192 while ((n = DisplayEventReceiver::getEvents(&mInjector.tube, buffer, 8)) > 0) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800193 for (int i = 0; i < n; i++) {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800194 if (buffer[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700195 mHandler->dispatchInvalidate(buffer[i].vsync.vsyncId,
196 buffer[i].vsync.expectedVSyncTimestamp);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800197 break;
198 }
199 }
200 }
Mathias Agopian8aedd472012-01-24 16:39:14 -0800201}
202
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700203} // namespace android::impl