blob: c9c398969c7087c4c37477628d7fee5db053b482 [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
Mathias Agopianf1d8e872009-04-20 19:39:12 -070017#include <errno.h>
Lloyd Pique78ce4182018-01-31 16:39:51 -080018#include <stdint.h>
Mathias Agopianf1d8e872009-04-20 19:39:12 -070019#include <sys/types.h>
20
Mathias Agopian8aedd472012-01-24 16:39:14 -080021#include <binder/IPCThreadState.h>
22
Mathias Agopianf1d8e872009-04-20 19:39:12 -070023#include <utils/Log.h>
Lloyd Pique78ce4182018-01-31 16:39:51 -080024#include <utils/Timers.h>
25#include <utils/threads.h>
Mathias Agopian8aedd472012-01-24 16:39:14 -080026
27#include <gui/IDisplayEventConnection.h>
Mathias Agopianf1d8e872009-04-20 19:39:12 -070028
Mathias Agopian8aedd472012-01-24 16:39:14 -080029#include "EventThread.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
33namespace android {
34
35// ---------------------------------------------------------------------------
36
Lloyd Pique78ce4182018-01-31 16:39:51 -080037MessageBase::MessageBase() : MessageHandler() {}
Mathias Agopianb6683b52009-04-28 03:17:50 -070038
Lloyd Pique78ce4182018-01-31 16:39:51 -080039MessageBase::~MessageBase() {}
Mathias Agopianb6683b52009-04-28 03:17:50 -070040
Mathias Agopianf61c57f2011-11-23 16:49:10 -080041void MessageBase::handleMessage(const Message&) {
42 this->handler();
43 barrier.open();
44};
45
Mathias Agopianb6683b52009-04-28 03:17:50 -070046// ---------------------------------------------------------------------------
47
Mathias Agopian4fec8732012-06-29 14:12:52 -070048void MessageQueue::Handler::dispatchRefresh() {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080049 if ((android_atomic_or(eventMaskRefresh, &mEventMask) & eventMaskRefresh) == 0) {
50 mQueue.mLooper->sendMessage(this, Message(MessageQueue::REFRESH));
51 }
52}
53
Mathias Agopian4fec8732012-06-29 14:12:52 -070054void MessageQueue::Handler::dispatchInvalidate() {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080055 if ((android_atomic_or(eventMaskInvalidate, &mEventMask) & eventMaskInvalidate) == 0) {
56 mQueue.mLooper->sendMessage(this, Message(MessageQueue::INVALIDATE));
57 }
58}
59
60void MessageQueue::Handler::handleMessage(const Message& message) {
61 switch (message.what) {
62 case INVALIDATE:
63 android_atomic_and(~eventMaskInvalidate, &mEventMask);
64 mQueue.mFlinger->onMessageReceived(message.what);
65 break;
66 case REFRESH:
67 android_atomic_and(~eventMaskRefresh, &mEventMask);
68 mQueue.mFlinger->onMessageReceived(message.what);
69 break;
70 }
71}
72
73// ---------------------------------------------------------------------------
74
Lloyd Pique78ce4182018-01-31 16:39:51 -080075MessageQueue::MessageQueue() {}
Mathias Agopianf1d8e872009-04-20 19:39:12 -070076
Lloyd Pique78ce4182018-01-31 16:39:51 -080077MessageQueue::~MessageQueue() {}
Mathias Agopianb6683b52009-04-28 03:17:50 -070078
Lloyd Pique78ce4182018-01-31 16:39:51 -080079void MessageQueue::init(const sp<SurfaceFlinger>& flinger) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080080 mFlinger = flinger;
81 mLooper = new Looper(true);
82 mHandler = new Handler(*this);
83}
84
Lloyd Pique78ce4182018-01-31 16:39:51 -080085void MessageQueue::setEventThread(const sp<EventThread>& eventThread) {
Chia-I Wu90f669f2017-10-05 14:24:41 -070086 if (mEventThread == eventThread) {
87 return;
88 }
89
90 if (mEventTube.getFd() >= 0) {
91 mLooper->removeFd(mEventTube.getFd());
92 }
93
Mathias Agopian8aedd472012-01-24 16:39:14 -080094 mEventThread = eventThread;
95 mEvents = eventThread->createEventConnection();
Dan Stoza6b698e42017-04-03 13:09:08 -070096 mEvents->stealReceiveChannel(&mEventTube);
Lloyd Pique78ce4182018-01-31 16:39:51 -080097 mLooper->addFd(mEventTube.getFd(), 0, Looper::EVENT_INPUT, MessageQueue::cb_eventReceiver,
98 this);
Mathias Agopian8aedd472012-01-24 16:39:14 -080099}
100
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800101void MessageQueue::waitMessage() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700102 do {
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800103 IPCThreadState::self()->flushCommands();
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800104 int32_t ret = mLooper->pollOnce(-1);
105 switch (ret) {
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800106 case Looper::POLL_WAKE:
107 case Looper::POLL_CALLBACK:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800108 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800109 case Looper::POLL_ERROR:
110 ALOGE("Looper::POLL_ERROR");
Pablo Ceballos53390e12015-08-04 11:25:59 -0700111 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800112 case Looper::POLL_TIMEOUT:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800113 // timeout (should not happen)
114 continue;
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800115 default:
116 // should not happen
Steve Blocke6f43dd2012-01-06 19:20:56 +0000117 ALOGE("Looper::pollOnce() returned unknown status %d", ret);
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800118 continue;
119 }
120 } while (true);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700121}
122
Lloyd Pique78ce4182018-01-31 16:39:51 -0800123status_t MessageQueue::postMessage(const sp<MessageBase>& messageHandler, nsecs_t relTime) {
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800124 const Message dummyMessage;
125 if (relTime > 0) {
126 mLooper->sendMessageDelayed(relTime, messageHandler, dummyMessage);
127 } else {
128 mLooper->sendMessage(messageHandler, dummyMessage);
129 }
130 return NO_ERROR;
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700131}
132
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800133void MessageQueue::invalidate() {
Mathias Agopian69a655c2012-04-11 20:43:19 -0700134 mEvents->requestNextVsync();
Mathias Agopian8aedd472012-01-24 16:39:14 -0800135}
136
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800137void MessageQueue::refresh() {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700138 mHandler->dispatchRefresh();
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700139}
140
Mathias Agopian8aedd472012-01-24 16:39:14 -0800141int MessageQueue::cb_eventReceiver(int fd, int events, void* data) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800142 MessageQueue* queue = reinterpret_cast<MessageQueue*>(data);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800143 return queue->eventReceiver(fd, events);
144}
145
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700146int MessageQueue::eventReceiver(int /*fd*/, int /*events*/) {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800147 ssize_t n;
148 DisplayEventReceiver::Event buffer[8];
Dan Stoza6b698e42017-04-03 13:09:08 -0700149 while ((n = DisplayEventReceiver::getEvents(&mEventTube, buffer, 8)) > 0) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800150 for (int i = 0; i < n; i++) {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800151 if (buffer[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700152 mHandler->dispatchInvalidate();
Mathias Agopian8aedd472012-01-24 16:39:14 -0800153 break;
154 }
155 }
156 }
157 return 1;
158}
159
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700160// ---------------------------------------------------------------------------
161
162}; // namespace android