blob: 34dc24b1f35c4d4df73e727ac3eab4f21a6d6a5c [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
64void MessageQueue::Handler::handleMessage(const Message& message) {
65 switch (message.what) {
66 case INVALIDATE:
67 android_atomic_and(~eventMaskInvalidate, &mEventMask);
68 mQueue.mFlinger->onMessageReceived(message.what);
69 break;
70 case REFRESH:
71 android_atomic_and(~eventMaskRefresh, &mEventMask);
72 mQueue.mFlinger->onMessageReceived(message.what);
73 break;
74 }
75}
76
77// ---------------------------------------------------------------------------
78
Mathias Agopianf1d8e872009-04-20 19:39:12 -070079MessageQueue::MessageQueue()
Mathias Agopianf1d8e872009-04-20 19:39:12 -070080{
81}
82
Mathias Agopianf61c57f2011-11-23 16:49:10 -080083MessageQueue::~MessageQueue() {
84}
Mathias Agopianb6683b52009-04-28 03:17:50 -070085
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080086void MessageQueue::init(const sp<SurfaceFlinger>& flinger)
87{
88 mFlinger = flinger;
89 mLooper = new Looper(true);
90 mHandler = new Handler(*this);
91}
92
Mathias Agopian8aedd472012-01-24 16:39:14 -080093void MessageQueue::setEventThread(const sp<EventThread>& eventThread)
94{
95 mEventThread = eventThread;
96 mEvents = eventThread->createEventConnection();
97 mEventTube = mEvents->getDataChannel();
Brian Carlstromfe761ab2013-12-12 23:13:18 -080098 mLooper->addFd(mEventTube->getFd(), 0, Looper::EVENT_INPUT,
Mathias Agopian8aedd472012-01-24 16:39:14 -080099 MessageQueue::cb_eventReceiver, this);
100}
101
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800102void MessageQueue::waitMessage() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700103 do {
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800104 IPCThreadState::self()->flushCommands();
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800105 int32_t ret = mLooper->pollOnce(-1);
106 switch (ret) {
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800107 case Looper::POLL_WAKE:
108 case Looper::POLL_CALLBACK:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800109 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800110 case Looper::POLL_ERROR:
111 ALOGE("Looper::POLL_ERROR");
Pablo Ceballos53390e12015-08-04 11:25:59 -0700112 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800113 case Looper::POLL_TIMEOUT:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800114 // timeout (should not happen)
115 continue;
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800116 default:
117 // should not happen
Steve Blocke6f43dd2012-01-06 19:20:56 +0000118 ALOGE("Looper::pollOnce() returned unknown status %d", ret);
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800119 continue;
120 }
121 } while (true);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700122}
123
124status_t MessageQueue::postMessage(
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800125 const sp<MessageBase>& messageHandler, nsecs_t relTime)
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700126{
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 Agopian9eb1f052013-04-10 16:27:17 -0700136
Mathias Agopian4fec8732012-06-29 14:12:52 -0700137/* when INVALIDATE_ON_VSYNC is set SF only processes
138 * buffer updates on VSYNC and performs a refresh immediately
139 * after.
140 *
141 * when INVALIDATE_ON_VSYNC is set to false, SF will instead
142 * perform the buffer updates immediately, but the refresh only
143 * at the next VSYNC.
144 * THIS MODE IS BUGGY ON GALAXY NEXUS AND WILL CAUSE HANGS
145 */
146#define INVALIDATE_ON_VSYNC 1
147
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800148void MessageQueue::invalidate() {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700149#if INVALIDATE_ON_VSYNC
Mathias Agopian69a655c2012-04-11 20:43:19 -0700150 mEvents->requestNextVsync();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700151#else
152 mHandler->dispatchInvalidate();
153#endif
Mathias Agopian8aedd472012-01-24 16:39:14 -0800154}
155
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800156void MessageQueue::refresh() {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700157#if INVALIDATE_ON_VSYNC
158 mHandler->dispatchRefresh();
159#else
Mathias Agopian8aedd472012-01-24 16:39:14 -0800160 mEvents->requestNextVsync();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700161#endif
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700162}
163
Mathias Agopian8aedd472012-01-24 16:39:14 -0800164int MessageQueue::cb_eventReceiver(int fd, int events, void* data) {
165 MessageQueue* queue = reinterpret_cast<MessageQueue *>(data);
166 return queue->eventReceiver(fd, events);
167}
168
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700169int MessageQueue::eventReceiver(int /*fd*/, int /*events*/) {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800170 ssize_t n;
171 DisplayEventReceiver::Event buffer[8];
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800172 while ((n = DisplayEventReceiver::getEvents(mEventTube, buffer, 8)) > 0) {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800173 for (int i=0 ; i<n ; i++) {
174 if (buffer[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700175#if INVALIDATE_ON_VSYNC
176 mHandler->dispatchInvalidate();
177#else
178 mHandler->dispatchRefresh();
179#endif
Mathias Agopian8aedd472012-01-24 16:39:14 -0800180 break;
181 }
182 }
183 }
184 return 1;
185}
186
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700187// ---------------------------------------------------------------------------
188
189}; // namespace android