blob: 47a4f42fccb8889fdec8cdfcec27e3090dfd44ef [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);
Ady Abraham326ecde2020-11-06 15:05:53 -080093 mVsync.mScheduled = false;
Ady Abraham55fa7272020-09-30 19:19:27 -070094 }
95 mHandler->dispatchInvalidate(mVsync.tokenManager->generateTokenForPredictions(
96 {targetWakeupTime, readyTime, vsyncTime}),
97 vsyncTime);
98}
99
100void MessageQueue::initVsync(scheduler::VSyncDispatch& dispatch,
101 frametimeline::TokenManager& tokenManager,
102 std::chrono::nanoseconds workDuration) {
103 setDuration(workDuration);
104 mVsync.tokenManager = &tokenManager;
105 mVsync.registration = std::make_unique<
106 scheduler::VSyncCallbackRegistration>(dispatch,
107 std::bind(&MessageQueue::vsyncCallback, this,
108 std::placeholders::_1,
109 std::placeholders::_2,
110 std::placeholders::_3),
111 "sf");
112}
113
114void MessageQueue::setDuration(std::chrono::nanoseconds workDuration) {
115 ATRACE_CALL();
116 std::lock_guard lock(mVsync.mutex);
117 mVsync.workDuration = workDuration;
Ady Abraham326ecde2020-11-06 15:05:53 -0800118 if (mVsync.mScheduled) {
119 mVsync.registration->schedule({mVsync.workDuration.get().count(), /*readyDuration=*/0,
120 mVsync.lastCallbackTime.count()});
121 }
Ana Krulec98b5b242018-08-10 15:03:23 -0700122}
123
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800124void MessageQueue::waitMessage() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700125 do {
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800126 IPCThreadState::self()->flushCommands();
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800127 int32_t ret = mLooper->pollOnce(-1);
128 switch (ret) {
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800129 case Looper::POLL_WAKE:
130 case Looper::POLL_CALLBACK:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800131 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800132 case Looper::POLL_ERROR:
133 ALOGE("Looper::POLL_ERROR");
Pablo Ceballos53390e12015-08-04 11:25:59 -0700134 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800135 case Looper::POLL_TIMEOUT:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800136 // timeout (should not happen)
137 continue;
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800138 default:
139 // should not happen
Steve Blocke6f43dd2012-01-06 19:20:56 +0000140 ALOGE("Looper::pollOnce() returned unknown status %d", ret);
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800141 continue;
142 }
143 } while (true);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700144}
145
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700146void MessageQueue::postMessage(sp<MessageHandler>&& handler) {
147 mLooper->sendMessage(handler, Message());
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700148}
149
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800150void MessageQueue::invalidate() {
Ady Abraham55fa7272020-09-30 19:19:27 -0700151 ATRACE_CALL();
152 if (mEvents) {
153 mEvents->requestNextVsync();
154 } else {
Ady Abraham326ecde2020-11-06 15:05:53 -0800155 std::lock_guard lock(mVsync.mutex);
156 mVsync.mScheduled = true;
157 mVsync.registration->schedule({mVsync.workDuration.get().count(), /*readyDuration=*/0,
158 mVsync.lastCallbackTime.count()});
Ady Abraham55fa7272020-09-30 19:19:27 -0700159 }
Mathias Agopian8aedd472012-01-24 16:39:14 -0800160}
161
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800162void MessageQueue::refresh() {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700163 mHandler->dispatchRefresh();
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700164}
165
Mathias Agopian8aedd472012-01-24 16:39:14 -0800166int MessageQueue::cb_eventReceiver(int fd, int events, void* data) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800167 MessageQueue* queue = reinterpret_cast<MessageQueue*>(data);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800168 return queue->eventReceiver(fd, events);
169}
170
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700171int MessageQueue::eventReceiver(int /*fd*/, int /*events*/) {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800172 ssize_t n;
173 DisplayEventReceiver::Event buffer[8];
Dan Stoza6b698e42017-04-03 13:09:08 -0700174 while ((n = DisplayEventReceiver::getEvents(&mEventTube, buffer, 8)) > 0) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800175 for (int i = 0; i < n; i++) {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800176 if (buffer[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700177 mHandler->dispatchInvalidate(buffer[i].vsync.vsyncId,
178 buffer[i].vsync.expectedVSyncTimestamp);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800179 break;
180 }
181 }
182 }
183 return 1;
184}
185
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700186} // namespace android::impl
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800187