Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 1 | /* |
| 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 Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 17 | #include <errno.h> |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 18 | #include <stdint.h> |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 19 | #include <sys/types.h> |
| 20 | |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 21 | #include <binder/IPCThreadState.h> |
| 22 | |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 23 | #include <utils/Log.h> |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 24 | #include <utils/Timers.h> |
| 25 | #include <utils/threads.h> |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 26 | |
Lloyd Pique | 3fcdef1 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 27 | #include <gui/DisplayEventReceiver.h> |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 28 | #include <gui/IDisplayEventConnection.h> |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 29 | |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 30 | #include "EventThread.h" |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 31 | #include "MessageQueue.h" |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 32 | #include "SurfaceFlinger.h" |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 33 | |
| 34 | namespace android { |
| 35 | |
| 36 | // --------------------------------------------------------------------------- |
| 37 | |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 38 | MessageBase::MessageBase() : MessageHandler() {} |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 39 | |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 40 | MessageBase::~MessageBase() {} |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 41 | |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 42 | void MessageBase::handleMessage(const Message&) { |
| 43 | this->handler(); |
| 44 | barrier.open(); |
| 45 | }; |
| 46 | |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 47 | // --------------------------------------------------------------------------- |
| 48 | |
Lloyd Pique | 3fcdef1 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 49 | MessageQueue::~MessageQueue() = default; |
| 50 | |
| 51 | // --------------------------------------------------------------------------- |
| 52 | |
| 53 | namespace impl { |
| 54 | |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 55 | void MessageQueue::Handler::dispatchRefresh() { |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 56 | if ((android_atomic_or(eventMaskRefresh, &mEventMask) & eventMaskRefresh) == 0) { |
| 57 | mQueue.mLooper->sendMessage(this, Message(MessageQueue::REFRESH)); |
| 58 | } |
| 59 | } |
| 60 | |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 61 | void MessageQueue::Handler::dispatchInvalidate() { |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 62 | if ((android_atomic_or(eventMaskInvalidate, &mEventMask) & eventMaskInvalidate) == 0) { |
| 63 | mQueue.mLooper->sendMessage(this, Message(MessageQueue::INVALIDATE)); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void MessageQueue::Handler::handleMessage(const Message& message) { |
| 68 | switch (message.what) { |
| 69 | case INVALIDATE: |
| 70 | android_atomic_and(~eventMaskInvalidate, &mEventMask); |
| 71 | mQueue.mFlinger->onMessageReceived(message.what); |
| 72 | break; |
| 73 | case REFRESH: |
| 74 | android_atomic_and(~eventMaskRefresh, &mEventMask); |
| 75 | mQueue.mFlinger->onMessageReceived(message.what); |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // --------------------------------------------------------------------------- |
| 81 | |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 82 | void MessageQueue::init(const sp<SurfaceFlinger>& flinger) { |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 83 | mFlinger = flinger; |
| 84 | mLooper = new Looper(true); |
| 85 | mHandler = new Handler(*this); |
| 86 | } |
| 87 | |
Lloyd Pique | 3fcdef1 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 88 | void MessageQueue::setEventThread(android::EventThread* eventThread) { |
Chia-I Wu | 90f669f | 2017-10-05 14:24:41 -0700 | [diff] [blame] | 89 | if (mEventThread == eventThread) { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | if (mEventTube.getFd() >= 0) { |
| 94 | mLooper->removeFd(mEventTube.getFd()); |
| 95 | } |
| 96 | |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 97 | mEventThread = eventThread; |
| 98 | mEvents = eventThread->createEventConnection(); |
Dan Stoza | 6b698e4 | 2017-04-03 13:09:08 -0700 | [diff] [blame] | 99 | mEvents->stealReceiveChannel(&mEventTube); |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 100 | mLooper->addFd(mEventTube.getFd(), 0, Looper::EVENT_INPUT, MessageQueue::cb_eventReceiver, |
| 101 | this); |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 104 | void MessageQueue::setEventConnection(const sp<EventThreadConnection>& connection) { |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 105 | if (mEventTube.getFd() >= 0) { |
| 106 | mLooper->removeFd(mEventTube.getFd()); |
| 107 | } |
| 108 | |
| 109 | mEvents = connection; |
| 110 | mEvents->stealReceiveChannel(&mEventTube); |
| 111 | mLooper->addFd(mEventTube.getFd(), 0, Looper::EVENT_INPUT, MessageQueue::cb_eventReceiver, |
| 112 | this); |
| 113 | } |
| 114 | |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 115 | void MessageQueue::waitMessage() { |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 116 | do { |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 117 | IPCThreadState::self()->flushCommands(); |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 118 | int32_t ret = mLooper->pollOnce(-1); |
| 119 | switch (ret) { |
Brian Carlstrom | fe761ab | 2013-12-12 23:13:18 -0800 | [diff] [blame] | 120 | case Looper::POLL_WAKE: |
| 121 | case Looper::POLL_CALLBACK: |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 122 | continue; |
Brian Carlstrom | fe761ab | 2013-12-12 23:13:18 -0800 | [diff] [blame] | 123 | case Looper::POLL_ERROR: |
| 124 | ALOGE("Looper::POLL_ERROR"); |
Pablo Ceballos | 53390e1 | 2015-08-04 11:25:59 -0700 | [diff] [blame] | 125 | continue; |
Brian Carlstrom | fe761ab | 2013-12-12 23:13:18 -0800 | [diff] [blame] | 126 | case Looper::POLL_TIMEOUT: |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 127 | // timeout (should not happen) |
| 128 | continue; |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 129 | default: |
| 130 | // should not happen |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 131 | ALOGE("Looper::pollOnce() returned unknown status %d", ret); |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 132 | continue; |
| 133 | } |
| 134 | } while (true); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 137 | status_t MessageQueue::postMessage(const sp<MessageBase>& messageHandler, nsecs_t relTime) { |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 138 | const Message dummyMessage; |
| 139 | if (relTime > 0) { |
| 140 | mLooper->sendMessageDelayed(relTime, messageHandler, dummyMessage); |
| 141 | } else { |
| 142 | mLooper->sendMessage(messageHandler, dummyMessage); |
| 143 | } |
| 144 | return NO_ERROR; |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 147 | void MessageQueue::invalidate() { |
Mathias Agopian | 69a655c | 2012-04-11 20:43:19 -0700 | [diff] [blame] | 148 | mEvents->requestNextVsync(); |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 151 | void MessageQueue::refresh() { |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 152 | mHandler->dispatchRefresh(); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 155 | int MessageQueue::cb_eventReceiver(int fd, int events, void* data) { |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 156 | MessageQueue* queue = reinterpret_cast<MessageQueue*>(data); |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 157 | return queue->eventReceiver(fd, events); |
| 158 | } |
| 159 | |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 160 | int MessageQueue::eventReceiver(int /*fd*/, int /*events*/) { |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 161 | ssize_t n; |
| 162 | DisplayEventReceiver::Event buffer[8]; |
Dan Stoza | 6b698e4 | 2017-04-03 13:09:08 -0700 | [diff] [blame] | 163 | while ((n = DisplayEventReceiver::getEvents(&mEventTube, buffer, 8)) > 0) { |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 164 | for (int i = 0; i < n; i++) { |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 165 | if (buffer[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) { |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 166 | mHandler->dispatchInvalidate(); |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 167 | break; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | return 1; |
| 172 | } |
| 173 | |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 174 | // --------------------------------------------------------------------------- |
| 175 | |
Lloyd Pique | 3fcdef1 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 176 | } // namespace impl |
| 177 | } // namespace android |