blob: 9d6e1d864abaea60a5b8c908a0162083a5d6f57a [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 Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Mathias Agopianf1d8e872009-04-20 19:39:12 -070021#include <errno.h>
Lloyd Pique78ce4182018-01-31 16:39:51 -080022#include <stdint.h>
Mathias Agopianf1d8e872009-04-20 19:39:12 -070023#include <sys/types.h>
24
Mathias Agopian8aedd472012-01-24 16:39:14 -080025#include <binder/IPCThreadState.h>
26
Mathias Agopianf1d8e872009-04-20 19:39:12 -070027#include <utils/Log.h>
Lloyd Pique78ce4182018-01-31 16:39:51 -080028#include <utils/Timers.h>
29#include <utils/threads.h>
Mathias Agopian8aedd472012-01-24 16:39:14 -080030
Lloyd Pique3fcdef12018-01-22 17:14:00 -080031#include <gui/DisplayEventReceiver.h>
Mathias Agopian8aedd472012-01-24 16:39:14 -080032#include <gui/IDisplayEventConnection.h>
Mathias Agopianf1d8e872009-04-20 19:39:12 -070033
Mathias Agopian8aedd472012-01-24 16:39:14 -080034#include "EventThread.h"
Lloyd Pique78ce4182018-01-31 16:39:51 -080035#include "MessageQueue.h"
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080036#include "SurfaceFlinger.h"
Mathias Agopianf1d8e872009-04-20 19:39:12 -070037
38namespace android {
39
40// ---------------------------------------------------------------------------
41
Lloyd Pique78ce4182018-01-31 16:39:51 -080042MessageBase::MessageBase() : MessageHandler() {}
Mathias Agopianb6683b52009-04-28 03:17:50 -070043
Lloyd Pique78ce4182018-01-31 16:39:51 -080044MessageBase::~MessageBase() {}
Mathias Agopianb6683b52009-04-28 03:17:50 -070045
Mathias Agopianf61c57f2011-11-23 16:49:10 -080046void MessageBase::handleMessage(const Message&) {
47 this->handler();
48 barrier.open();
49};
50
Mathias Agopianb6683b52009-04-28 03:17:50 -070051// ---------------------------------------------------------------------------
52
Lloyd Pique3fcdef12018-01-22 17:14:00 -080053MessageQueue::~MessageQueue() = default;
54
55// ---------------------------------------------------------------------------
56
57namespace impl {
58
Mathias Agopian4fec8732012-06-29 14:12:52 -070059void MessageQueue::Handler::dispatchRefresh() {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080060 if ((android_atomic_or(eventMaskRefresh, &mEventMask) & eventMaskRefresh) == 0) {
61 mQueue.mLooper->sendMessage(this, Message(MessageQueue::REFRESH));
62 }
63}
64
Ady Abraham5facfb12020-04-22 15:18:31 -070065void MessageQueue::Handler::dispatchInvalidate(nsecs_t expectedVSyncTimestamp) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080066 if ((android_atomic_or(eventMaskInvalidate, &mEventMask) & eventMaskInvalidate) == 0) {
Ady Abraham5facfb12020-04-22 15:18:31 -070067 mExpectedVSyncTime = expectedVSyncTimestamp;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080068 mQueue.mLooper->sendMessage(this, Message(MessageQueue::INVALIDATE));
69 }
70}
71
72void MessageQueue::Handler::handleMessage(const Message& message) {
73 switch (message.what) {
74 case INVALIDATE:
75 android_atomic_and(~eventMaskInvalidate, &mEventMask);
Ady Abraham5facfb12020-04-22 15:18:31 -070076 mQueue.mFlinger->onMessageReceived(message.what, mExpectedVSyncTime);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080077 break;
78 case REFRESH:
79 android_atomic_and(~eventMaskRefresh, &mEventMask);
Ady Abraham5facfb12020-04-22 15:18:31 -070080 mQueue.mFlinger->onMessageReceived(message.what, mExpectedVSyncTime);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080081 break;
82 }
83}
84
85// ---------------------------------------------------------------------------
86
Lloyd Pique78ce4182018-01-31 16:39:51 -080087void MessageQueue::init(const sp<SurfaceFlinger>& flinger) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080088 mFlinger = flinger;
89 mLooper = new Looper(true);
90 mHandler = new Handler(*this);
91}
92
Ana Krulec85c39af2018-12-26 17:29:57 -080093void MessageQueue::setEventConnection(const sp<EventThreadConnection>& connection) {
Ana Krulec98b5b242018-08-10 15:03:23 -070094 if (mEventTube.getFd() >= 0) {
95 mLooper->removeFd(mEventTube.getFd());
96 }
97
98 mEvents = connection;
99 mEvents->stealReceiveChannel(&mEventTube);
100 mLooper->addFd(mEventTube.getFd(), 0, Looper::EVENT_INPUT, MessageQueue::cb_eventReceiver,
101 this);
102}
103
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800104void MessageQueue::waitMessage() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700105 do {
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800106 IPCThreadState::self()->flushCommands();
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800107 int32_t ret = mLooper->pollOnce(-1);
108 switch (ret) {
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800109 case Looper::POLL_WAKE:
110 case Looper::POLL_CALLBACK:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800111 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800112 case Looper::POLL_ERROR:
113 ALOGE("Looper::POLL_ERROR");
Pablo Ceballos53390e12015-08-04 11:25:59 -0700114 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800115 case Looper::POLL_TIMEOUT:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800116 // timeout (should not happen)
117 continue;
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800118 default:
119 // should not happen
Steve Blocke6f43dd2012-01-06 19:20:56 +0000120 ALOGE("Looper::pollOnce() returned unknown status %d", ret);
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800121 continue;
122 }
123 } while (true);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700124}
125
Lloyd Pique78ce4182018-01-31 16:39:51 -0800126status_t MessageQueue::postMessage(const sp<MessageBase>& messageHandler, nsecs_t relTime) {
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800127 const Message dummyMessage;
128 if (relTime > 0) {
129 mLooper->sendMessageDelayed(relTime, messageHandler, dummyMessage);
130 } else {
131 mLooper->sendMessage(messageHandler, dummyMessage);
132 }
133 return NO_ERROR;
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700134}
135
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800136void MessageQueue::invalidate() {
Mathias Agopian69a655c2012-04-11 20:43:19 -0700137 mEvents->requestNextVsync();
Mathias Agopian8aedd472012-01-24 16:39:14 -0800138}
139
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800140void MessageQueue::refresh() {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700141 mHandler->dispatchRefresh();
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700142}
143
Mathias Agopian8aedd472012-01-24 16:39:14 -0800144int MessageQueue::cb_eventReceiver(int fd, int events, void* data) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800145 MessageQueue* queue = reinterpret_cast<MessageQueue*>(data);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800146 return queue->eventReceiver(fd, events);
147}
148
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700149int MessageQueue::eventReceiver(int /*fd*/, int /*events*/) {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800150 ssize_t n;
151 DisplayEventReceiver::Event buffer[8];
Dan Stoza6b698e42017-04-03 13:09:08 -0700152 while ((n = DisplayEventReceiver::getEvents(&mEventTube, buffer, 8)) > 0) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800153 for (int i = 0; i < n; i++) {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800154 if (buffer[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
Ady Abraham5facfb12020-04-22 15:18:31 -0700155 mHandler->dispatchInvalidate(buffer[i].vsync.expectedVSyncTimestamp);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800156 break;
157 }
158 }
159 }
160 return 1;
161}
162
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700163// ---------------------------------------------------------------------------
164
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800165} // namespace impl
166} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800167
168// TODO(b/129481165): remove the #pragma below and fix conversion issues
169#pragma clang diagnostic pop // ignored "-Wconversion"