blob: 134337588bdc23b846eefb8f4461bccd3458efd0 [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
Ana Krulec85c39af2018-12-26 17:29:57 -080072void MessageQueue::setEventConnection(const sp<EventThreadConnection>& connection) {
Ana Krulec98b5b242018-08-10 15:03:23 -070073 if (mEventTube.getFd() >= 0) {
74 mLooper->removeFd(mEventTube.getFd());
75 }
76
77 mEvents = connection;
Ady Abraham55fa7272020-09-30 19:19:27 -070078 if (mEvents) {
79 mEvents->stealReceiveChannel(&mEventTube);
80 mLooper->addFd(mEventTube.getFd(), 0, Looper::EVENT_INPUT, MessageQueue::cb_eventReceiver,
81 this);
82 }
83}
84
85void MessageQueue::vsyncCallback(nsecs_t vsyncTime, nsecs_t targetWakeupTime, nsecs_t readyTime) {
86 ATRACE_CALL();
87 // Trace VSYNC-sf
88 mVsync.value = (mVsync.value + 1) % 2;
89
90 {
91 std::lock_guard lock(mVsync.mutex);
92 mVsync.lastCallbackTime = std::chrono::nanoseconds(vsyncTime);
93 }
94 mHandler->dispatchInvalidate(mVsync.tokenManager->generateTokenForPredictions(
95 {targetWakeupTime, readyTime, vsyncTime}),
96 vsyncTime);
97}
98
99void MessageQueue::initVsync(scheduler::VSyncDispatch& dispatch,
100 frametimeline::TokenManager& tokenManager,
101 std::chrono::nanoseconds workDuration) {
102 setDuration(workDuration);
103 mVsync.tokenManager = &tokenManager;
104 mVsync.registration = std::make_unique<
105 scheduler::VSyncCallbackRegistration>(dispatch,
106 std::bind(&MessageQueue::vsyncCallback, this,
107 std::placeholders::_1,
108 std::placeholders::_2,
109 std::placeholders::_3),
110 "sf");
111}
112
113void MessageQueue::setDuration(std::chrono::nanoseconds workDuration) {
114 ATRACE_CALL();
115 std::lock_guard lock(mVsync.mutex);
116 mVsync.workDuration = workDuration;
Ana Krulec98b5b242018-08-10 15:03:23 -0700117}
118
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800119void MessageQueue::waitMessage() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700120 do {
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800121 IPCThreadState::self()->flushCommands();
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800122 int32_t ret = mLooper->pollOnce(-1);
123 switch (ret) {
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800124 case Looper::POLL_WAKE:
125 case Looper::POLL_CALLBACK:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800126 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800127 case Looper::POLL_ERROR:
128 ALOGE("Looper::POLL_ERROR");
Pablo Ceballos53390e12015-08-04 11:25:59 -0700129 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800130 case Looper::POLL_TIMEOUT:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800131 // timeout (should not happen)
132 continue;
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800133 default:
134 // should not happen
Steve Blocke6f43dd2012-01-06 19:20:56 +0000135 ALOGE("Looper::pollOnce() returned unknown status %d", ret);
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800136 continue;
137 }
138 } while (true);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700139}
140
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700141void MessageQueue::postMessage(sp<MessageHandler>&& handler) {
142 mLooper->sendMessage(handler, Message());
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700143}
144
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800145void MessageQueue::invalidate() {
Ady Abraham55fa7272020-09-30 19:19:27 -0700146 ATRACE_CALL();
147 if (mEvents) {
148 mEvents->requestNextVsync();
149 } else {
150 const auto [workDuration, lastVsyncCallback] = [&] {
151 std::lock_guard lock(mVsync.mutex);
152 std::chrono::nanoseconds mWorkDurationNanos = mVsync.workDuration;
153 return std::make_pair(mWorkDurationNanos.count(), mVsync.lastCallbackTime.count());
154 }();
155
156 mVsync.registration->schedule({workDuration, /*readyDuration=*/0, lastVsyncCallback});
157 }
Mathias Agopian8aedd472012-01-24 16:39:14 -0800158}
159
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800160void MessageQueue::refresh() {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700161 mHandler->dispatchRefresh();
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700162}
163
Mathias Agopian8aedd472012-01-24 16:39:14 -0800164int MessageQueue::cb_eventReceiver(int fd, int events, void* data) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800165 MessageQueue* queue = reinterpret_cast<MessageQueue*>(data);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800166 return queue->eventReceiver(fd, events);
167}
168
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700169int MessageQueue::eventReceiver(int /*fd*/, int /*events*/) {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800170 ssize_t n;
171 DisplayEventReceiver::Event buffer[8];
Dan Stoza6b698e42017-04-03 13:09:08 -0700172 while ((n = DisplayEventReceiver::getEvents(&mEventTube, buffer, 8)) > 0) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800173 for (int i = 0; i < n; i++) {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800174 if (buffer[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700175 mHandler->dispatchInvalidate(buffer[i].vsync.vsyncId,
176 buffer[i].vsync.expectedVSyncTimestamp);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800177 break;
178 }
179 }
180 }
181 return 1;
182}
183
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700184} // namespace android::impl
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800185