2009-04-21 02:39:12 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2012-01-25 00:39:14 +00:00
|
|
|
#include <binder/IPCThreadState.h>
|
|
|
|
|
2009-04-21 02:39:12 +00:00
|
|
|
#include <utils/threads.h>
|
|
|
|
#include <utils/Timers.h>
|
|
|
|
#include <utils/Log.h>
|
2012-01-25 00:39:14 +00:00
|
|
|
|
|
|
|
#include <gui/IDisplayEventConnection.h>
|
|
|
|
#include <gui/BitTube.h>
|
2009-04-21 02:39:12 +00:00
|
|
|
|
|
|
|
#include "MessageQueue.h"
|
2012-01-25 00:39:14 +00:00
|
|
|
#include "EventThread.h"
|
2012-02-01 02:24:27 +00:00
|
|
|
#include "SurfaceFlinger.h"
|
2009-04-21 02:39:12 +00:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2011-11-24 00:49:10 +00:00
|
|
|
MessageBase::MessageBase()
|
|
|
|
: MessageHandler() {
|
2009-04-28 10:17:50 +00:00
|
|
|
}
|
|
|
|
|
2011-11-24 00:49:10 +00:00
|
|
|
MessageBase::~MessageBase() {
|
2009-04-28 10:17:50 +00:00
|
|
|
}
|
|
|
|
|
2011-11-24 00:49:10 +00:00
|
|
|
void MessageBase::handleMessage(const Message&) {
|
|
|
|
this->handler();
|
|
|
|
barrier.open();
|
|
|
|
};
|
|
|
|
|
2009-04-28 10:17:50 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2012-06-29 21:12:52 +00:00
|
|
|
void MessageQueue::Handler::dispatchRefresh() {
|
2012-02-01 02:24:27 +00:00
|
|
|
if ((android_atomic_or(eventMaskRefresh, &mEventMask) & eventMaskRefresh) == 0) {
|
|
|
|
mQueue.mLooper->sendMessage(this, Message(MessageQueue::REFRESH));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-29 21:12:52 +00:00
|
|
|
void MessageQueue::Handler::dispatchInvalidate() {
|
2012-02-01 02:24:27 +00:00
|
|
|
if ((android_atomic_or(eventMaskInvalidate, &mEventMask) & eventMaskInvalidate) == 0) {
|
|
|
|
mQueue.mLooper->sendMessage(this, Message(MessageQueue::INVALIDATE));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-10 23:27:17 +00:00
|
|
|
void MessageQueue::Handler::dispatchTransaction() {
|
|
|
|
if ((android_atomic_or(eventMaskTransaction, &mEventMask) & eventMaskTransaction) == 0) {
|
|
|
|
mQueue.mLooper->sendMessage(this, Message(MessageQueue::TRANSACTION));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-01 02:24:27 +00:00
|
|
|
void MessageQueue::Handler::handleMessage(const Message& message) {
|
|
|
|
switch (message.what) {
|
|
|
|
case INVALIDATE:
|
|
|
|
android_atomic_and(~eventMaskInvalidate, &mEventMask);
|
|
|
|
mQueue.mFlinger->onMessageReceived(message.what);
|
|
|
|
break;
|
|
|
|
case REFRESH:
|
|
|
|
android_atomic_and(~eventMaskRefresh, &mEventMask);
|
|
|
|
mQueue.mFlinger->onMessageReceived(message.what);
|
|
|
|
break;
|
2013-04-10 23:27:17 +00:00
|
|
|
case TRANSACTION:
|
|
|
|
android_atomic_and(~eventMaskTransaction, &mEventMask);
|
|
|
|
mQueue.mFlinger->onMessageReceived(message.what);
|
|
|
|
break;
|
2012-02-01 02:24:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2009-04-21 02:39:12 +00:00
|
|
|
MessageQueue::MessageQueue()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-11-24 00:49:10 +00:00
|
|
|
MessageQueue::~MessageQueue() {
|
2009-04-21 02:39:12 +00:00
|
|
|
}
|
|
|
|
|
2012-02-01 02:24:27 +00:00
|
|
|
void MessageQueue::init(const sp<SurfaceFlinger>& flinger)
|
|
|
|
{
|
|
|
|
mFlinger = flinger;
|
|
|
|
mLooper = new Looper(true);
|
|
|
|
mHandler = new Handler(*this);
|
|
|
|
}
|
|
|
|
|
2012-01-25 00:39:14 +00:00
|
|
|
void MessageQueue::setEventThread(const sp<EventThread>& eventThread)
|
|
|
|
{
|
|
|
|
mEventThread = eventThread;
|
|
|
|
mEvents = eventThread->createEventConnection();
|
|
|
|
mEventTube = mEvents->getDataChannel();
|
2013-12-13 07:13:18 +00:00
|
|
|
mLooper->addFd(mEventTube->getFd(), 0, Looper::EVENT_INPUT,
|
2012-01-25 00:39:14 +00:00
|
|
|
MessageQueue::cb_eventReceiver, this);
|
|
|
|
}
|
|
|
|
|
2011-11-24 00:49:10 +00:00
|
|
|
void MessageQueue::waitMessage() {
|
2009-04-21 02:39:12 +00:00
|
|
|
do {
|
2011-11-24 00:49:10 +00:00
|
|
|
IPCThreadState::self()->flushCommands();
|
|
|
|
int32_t ret = mLooper->pollOnce(-1);
|
|
|
|
switch (ret) {
|
2013-12-13 07:13:18 +00:00
|
|
|
case Looper::POLL_WAKE:
|
|
|
|
case Looper::POLL_CALLBACK:
|
2011-11-24 00:49:10 +00:00
|
|
|
continue;
|
2013-12-13 07:13:18 +00:00
|
|
|
case Looper::POLL_ERROR:
|
|
|
|
ALOGE("Looper::POLL_ERROR");
|
|
|
|
case Looper::POLL_TIMEOUT:
|
2012-02-01 02:24:27 +00:00
|
|
|
// timeout (should not happen)
|
2011-11-24 00:49:10 +00:00
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
// should not happen
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("Looper::pollOnce() returned unknown status %d", ret);
|
2011-11-24 00:49:10 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} while (true);
|
2009-04-21 02:39:12 +00:00
|
|
|
}
|
|
|
|
|
2011-11-24 00:49:10 +00:00
|
|
|
status_t MessageQueue::postMessage(
|
|
|
|
const sp<MessageBase>& messageHandler, nsecs_t relTime)
|
2009-04-21 02:39:12 +00:00
|
|
|
{
|
2011-11-24 00:49:10 +00:00
|
|
|
const Message dummyMessage;
|
|
|
|
if (relTime > 0) {
|
|
|
|
mLooper->sendMessageDelayed(relTime, messageHandler, dummyMessage);
|
|
|
|
} else {
|
|
|
|
mLooper->sendMessage(messageHandler, dummyMessage);
|
2009-04-21 02:39:12 +00:00
|
|
|
}
|
2011-11-24 00:49:10 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2013-04-10 23:27:17 +00:00
|
|
|
|
2012-06-29 21:12:52 +00:00
|
|
|
/* when INVALIDATE_ON_VSYNC is set SF only processes
|
|
|
|
* buffer updates on VSYNC and performs a refresh immediately
|
|
|
|
* after.
|
|
|
|
*
|
|
|
|
* when INVALIDATE_ON_VSYNC is set to false, SF will instead
|
|
|
|
* perform the buffer updates immediately, but the refresh only
|
|
|
|
* at the next VSYNC.
|
|
|
|
* THIS MODE IS BUGGY ON GALAXY NEXUS AND WILL CAUSE HANGS
|
|
|
|
*/
|
|
|
|
#define INVALIDATE_ON_VSYNC 1
|
|
|
|
|
2013-04-10 23:27:17 +00:00
|
|
|
void MessageQueue::invalidateTransactionNow() {
|
|
|
|
mHandler->dispatchTransaction();
|
|
|
|
}
|
|
|
|
|
2012-02-01 02:24:27 +00:00
|
|
|
void MessageQueue::invalidate() {
|
2012-06-29 21:12:52 +00:00
|
|
|
#if INVALIDATE_ON_VSYNC
|
2012-04-12 03:43:19 +00:00
|
|
|
mEvents->requestNextVsync();
|
2012-06-29 21:12:52 +00:00
|
|
|
#else
|
|
|
|
mHandler->dispatchInvalidate();
|
|
|
|
#endif
|
2012-01-25 00:39:14 +00:00
|
|
|
}
|
|
|
|
|
2012-02-01 02:24:27 +00:00
|
|
|
void MessageQueue::refresh() {
|
2012-06-29 21:12:52 +00:00
|
|
|
#if INVALIDATE_ON_VSYNC
|
|
|
|
mHandler->dispatchRefresh();
|
|
|
|
#else
|
2012-01-25 00:39:14 +00:00
|
|
|
mEvents->requestNextVsync();
|
2012-06-29 21:12:52 +00:00
|
|
|
#endif
|
2009-04-21 02:39:12 +00:00
|
|
|
}
|
|
|
|
|
2012-01-25 00:39:14 +00:00
|
|
|
int MessageQueue::cb_eventReceiver(int fd, int events, void* data) {
|
|
|
|
MessageQueue* queue = reinterpret_cast<MessageQueue *>(data);
|
|
|
|
return queue->eventReceiver(fd, events);
|
|
|
|
}
|
|
|
|
|
|
|
|
int MessageQueue::eventReceiver(int fd, int events) {
|
|
|
|
ssize_t n;
|
|
|
|
DisplayEventReceiver::Event buffer[8];
|
2012-02-01 02:24:27 +00:00
|
|
|
while ((n = DisplayEventReceiver::getEvents(mEventTube, buffer, 8)) > 0) {
|
2012-01-25 00:39:14 +00:00
|
|
|
for (int i=0 ; i<n ; i++) {
|
|
|
|
if (buffer[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
|
2012-06-29 21:12:52 +00:00
|
|
|
#if INVALIDATE_ON_VSYNC
|
|
|
|
mHandler->dispatchInvalidate();
|
|
|
|
#else
|
|
|
|
mHandler->dispatchRefresh();
|
|
|
|
#endif
|
2012-01-25 00:39:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-04-21 02:39:12 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
}; // namespace android
|