blob: 1ad86a6acaf57d7d0ff31d3acd4f4b8a91b3eddd [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
17#include <stdint.h>
18#include <errno.h>
19#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/threads.h>
24#include <utils/Timers.h>
25#include <utils/Log.h>
Mathias Agopian8aedd472012-01-24 16:39:14 -080026
27#include <gui/IDisplayEventConnection.h>
28#include <gui/BitTube.h>
Mathias Agopianf1d8e872009-04-20 19:39:12 -070029
30#include "MessageQueue.h"
Mathias Agopian8aedd472012-01-24 16:39:14 -080031#include "EventThread.h"
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080032#include "SurfaceFlinger.h"
Mathias Agopianf1d8e872009-04-20 19:39:12 -070033
34namespace android {
35
36// ---------------------------------------------------------------------------
37
Mathias Agopianf61c57f2011-11-23 16:49:10 -080038MessageBase::MessageBase()
39 : MessageHandler() {
Mathias Agopianb6683b52009-04-28 03:17:50 -070040}
41
Mathias Agopianf61c57f2011-11-23 16:49:10 -080042MessageBase::~MessageBase() {
Mathias Agopianb6683b52009-04-28 03:17:50 -070043}
44
Mathias Agopianf61c57f2011-11-23 16:49:10 -080045void MessageBase::handleMessage(const Message&) {
46 this->handler();
47 barrier.open();
48};
49
Mathias Agopianb6683b52009-04-28 03:17:50 -070050// ---------------------------------------------------------------------------
51
Mathias Agopian4fec8732012-06-29 14:12:52 -070052void MessageQueue::Handler::dispatchRefresh() {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080053 if ((android_atomic_or(eventMaskRefresh, &mEventMask) & eventMaskRefresh) == 0) {
54 mQueue.mLooper->sendMessage(this, Message(MessageQueue::REFRESH));
55 }
56}
57
Mathias Agopian4fec8732012-06-29 14:12:52 -070058void MessageQueue::Handler::dispatchInvalidate() {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080059 if ((android_atomic_or(eventMaskInvalidate, &mEventMask) & eventMaskInvalidate) == 0) {
60 mQueue.mLooper->sendMessage(this, Message(MessageQueue::INVALIDATE));
61 }
62}
63
Mathias Agopian9eb1f052013-04-10 16:27:17 -070064void MessageQueue::Handler::dispatchTransaction() {
65 if ((android_atomic_or(eventMaskTransaction, &mEventMask) & eventMaskTransaction) == 0) {
66 mQueue.mLooper->sendMessage(this, Message(MessageQueue::TRANSACTION));
67 }
68}
69
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080070void MessageQueue::Handler::handleMessage(const Message& message) {
71 switch (message.what) {
72 case INVALIDATE:
73 android_atomic_and(~eventMaskInvalidate, &mEventMask);
74 mQueue.mFlinger->onMessageReceived(message.what);
75 break;
76 case REFRESH:
77 android_atomic_and(~eventMaskRefresh, &mEventMask);
78 mQueue.mFlinger->onMessageReceived(message.what);
79 break;
Mathias Agopian9eb1f052013-04-10 16:27:17 -070080 case TRANSACTION:
81 android_atomic_and(~eventMaskTransaction, &mEventMask);
82 mQueue.mFlinger->onMessageReceived(message.what);
83 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080084 }
85}
86
87// ---------------------------------------------------------------------------
88
Mathias Agopianf1d8e872009-04-20 19:39:12 -070089MessageQueue::MessageQueue()
Mathias Agopianf1d8e872009-04-20 19:39:12 -070090{
91}
92
Mathias Agopianf61c57f2011-11-23 16:49:10 -080093MessageQueue::~MessageQueue() {
94}
Mathias Agopianb6683b52009-04-28 03:17:50 -070095
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080096void MessageQueue::init(const sp<SurfaceFlinger>& flinger)
97{
98 mFlinger = flinger;
99 mLooper = new Looper(true);
100 mHandler = new Handler(*this);
101}
102
Mathias Agopian8aedd472012-01-24 16:39:14 -0800103void MessageQueue::setEventThread(const sp<EventThread>& eventThread)
104{
105 mEventThread = eventThread;
106 mEvents = eventThread->createEventConnection();
107 mEventTube = mEvents->getDataChannel();
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800108 mLooper->addFd(mEventTube->getFd(), 0, Looper::EVENT_INPUT,
Mathias Agopian8aedd472012-01-24 16:39:14 -0800109 MessageQueue::cb_eventReceiver, this);
110}
111
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800112void MessageQueue::waitMessage() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700113 do {
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800114 IPCThreadState::self()->flushCommands();
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800115 int32_t ret = mLooper->pollOnce(-1);
116 switch (ret) {
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800117 case Looper::POLL_WAKE:
118 case Looper::POLL_CALLBACK:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800119 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800120 case Looper::POLL_ERROR:
121 ALOGE("Looper::POLL_ERROR");
122 case Looper::POLL_TIMEOUT:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800123 // timeout (should not happen)
124 continue;
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800125 default:
126 // should not happen
Steve Blocke6f43dd2012-01-06 19:20:56 +0000127 ALOGE("Looper::pollOnce() returned unknown status %d", ret);
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800128 continue;
129 }
130 } while (true);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700131}
132
133status_t MessageQueue::postMessage(
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800134 const sp<MessageBase>& messageHandler, nsecs_t relTime)
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700135{
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800136 const Message dummyMessage;
137 if (relTime > 0) {
138 mLooper->sendMessageDelayed(relTime, messageHandler, dummyMessage);
139 } else {
140 mLooper->sendMessage(messageHandler, dummyMessage);
141 }
142 return NO_ERROR;
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700143}
144
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700145
Mathias Agopian4fec8732012-06-29 14:12:52 -0700146/* when INVALIDATE_ON_VSYNC is set SF only processes
147 * buffer updates on VSYNC and performs a refresh immediately
148 * after.
149 *
150 * when INVALIDATE_ON_VSYNC is set to false, SF will instead
151 * perform the buffer updates immediately, but the refresh only
152 * at the next VSYNC.
153 * THIS MODE IS BUGGY ON GALAXY NEXUS AND WILL CAUSE HANGS
154 */
155#define INVALIDATE_ON_VSYNC 1
156
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700157void MessageQueue::invalidateTransactionNow() {
158 mHandler->dispatchTransaction();
159}
160
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800161void MessageQueue::invalidate() {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700162#if INVALIDATE_ON_VSYNC
Mathias Agopian69a655c2012-04-11 20:43:19 -0700163 mEvents->requestNextVsync();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700164#else
165 mHandler->dispatchInvalidate();
166#endif
Mathias Agopian8aedd472012-01-24 16:39:14 -0800167}
168
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800169void MessageQueue::refresh() {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700170#if INVALIDATE_ON_VSYNC
171 mHandler->dispatchRefresh();
172#else
Mathias Agopian8aedd472012-01-24 16:39:14 -0800173 mEvents->requestNextVsync();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700174#endif
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700175}
176
Mathias Agopian8aedd472012-01-24 16:39:14 -0800177int MessageQueue::cb_eventReceiver(int fd, int events, void* data) {
178 MessageQueue* queue = reinterpret_cast<MessageQueue *>(data);
179 return queue->eventReceiver(fd, events);
180}
181
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700182int MessageQueue::eventReceiver(int /*fd*/, int /*events*/) {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800183 ssize_t n;
184 DisplayEventReceiver::Event buffer[8];
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800185 while ((n = DisplayEventReceiver::getEvents(mEventTube, buffer, 8)) > 0) {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800186 for (int i=0 ; i<n ; i++) {
187 if (buffer[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700188#if INVALIDATE_ON_VSYNC
189 mHandler->dispatchInvalidate();
190#else
191 mHandler->dispatchRefresh();
192#endif
Mathias Agopian8aedd472012-01-24 16:39:14 -0800193 break;
194 }
195 }
196 }
197 return 1;
198}
199
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700200// ---------------------------------------------------------------------------
201
202}; // namespace android