blob: 99efd3905f2c310a2d9dd9b68ed1d47b6f2c270f [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");
Pablo Ceballos53390e12015-08-04 11:25:59 -0700122 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800123 case Looper::POLL_TIMEOUT:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800124 // timeout (should not happen)
125 continue;
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800126 default:
127 // should not happen
Steve Blocke6f43dd2012-01-06 19:20:56 +0000128 ALOGE("Looper::pollOnce() returned unknown status %d", ret);
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800129 continue;
130 }
131 } while (true);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700132}
133
134status_t MessageQueue::postMessage(
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800135 const sp<MessageBase>& messageHandler, nsecs_t relTime)
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700136{
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800137 const Message dummyMessage;
138 if (relTime > 0) {
139 mLooper->sendMessageDelayed(relTime, messageHandler, dummyMessage);
140 } else {
141 mLooper->sendMessage(messageHandler, dummyMessage);
142 }
143 return NO_ERROR;
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700144}
145
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700146
Mathias Agopian4fec8732012-06-29 14:12:52 -0700147/* when INVALIDATE_ON_VSYNC is set SF only processes
148 * buffer updates on VSYNC and performs a refresh immediately
149 * after.
150 *
151 * when INVALIDATE_ON_VSYNC is set to false, SF will instead
152 * perform the buffer updates immediately, but the refresh only
153 * at the next VSYNC.
154 * THIS MODE IS BUGGY ON GALAXY NEXUS AND WILL CAUSE HANGS
155 */
156#define INVALIDATE_ON_VSYNC 1
157
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700158void MessageQueue::invalidateTransactionNow() {
159 mHandler->dispatchTransaction();
160}
161
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800162void MessageQueue::invalidate() {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700163#if INVALIDATE_ON_VSYNC
Mathias Agopian69a655c2012-04-11 20:43:19 -0700164 mEvents->requestNextVsync();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700165#else
166 mHandler->dispatchInvalidate();
167#endif
Mathias Agopian8aedd472012-01-24 16:39:14 -0800168}
169
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800170void MessageQueue::refresh() {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700171#if INVALIDATE_ON_VSYNC
172 mHandler->dispatchRefresh();
173#else
Mathias Agopian8aedd472012-01-24 16:39:14 -0800174 mEvents->requestNextVsync();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700175#endif
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700176}
177
Mathias Agopian8aedd472012-01-24 16:39:14 -0800178int MessageQueue::cb_eventReceiver(int fd, int events, void* data) {
179 MessageQueue* queue = reinterpret_cast<MessageQueue *>(data);
180 return queue->eventReceiver(fd, events);
181}
182
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700183int MessageQueue::eventReceiver(int /*fd*/, int /*events*/) {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800184 ssize_t n;
185 DisplayEventReceiver::Event buffer[8];
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800186 while ((n = DisplayEventReceiver::getEvents(mEventTube, buffer, 8)) > 0) {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800187 for (int i=0 ; i<n ; i++) {
188 if (buffer[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700189#if INVALIDATE_ON_VSYNC
190 mHandler->dispatchInvalidate();
191#else
192 mHandler->dispatchRefresh();
193#endif
Mathias Agopian8aedd472012-01-24 16:39:14 -0800194 break;
195 }
196 }
197 }
198 return 1;
199}
200
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700201// ---------------------------------------------------------------------------
202
203}; // namespace android