blob: 70711e73ee46b2fa829cc6de263353d3f94dcb19 [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 Agopianf1d8e872009-04-20 19:39:12 -070032
33namespace android {
34
35// ---------------------------------------------------------------------------
36
Mathias Agopianf61c57f2011-11-23 16:49:10 -080037MessageBase::MessageBase()
38 : MessageHandler() {
Mathias Agopianb6683b52009-04-28 03:17:50 -070039}
40
Mathias Agopianf61c57f2011-11-23 16:49:10 -080041MessageBase::~MessageBase() {
Mathias Agopianb6683b52009-04-28 03:17:50 -070042}
43
Mathias Agopianf61c57f2011-11-23 16:49:10 -080044void MessageBase::handleMessage(const Message&) {
45 this->handler();
46 barrier.open();
47};
48
Mathias Agopianb6683b52009-04-28 03:17:50 -070049// ---------------------------------------------------------------------------
50
Mathias Agopianf1d8e872009-04-20 19:39:12 -070051MessageQueue::MessageQueue()
Mathias Agopianbe42aef2011-12-03 14:47:29 -080052 : mLooper(new Looper(true)), mWorkPending(0)
Mathias Agopianf1d8e872009-04-20 19:39:12 -070053{
54}
55
Mathias Agopianf61c57f2011-11-23 16:49:10 -080056MessageQueue::~MessageQueue() {
57}
Mathias Agopianb6683b52009-04-28 03:17:50 -070058
Mathias Agopian8aedd472012-01-24 16:39:14 -080059void MessageQueue::setEventThread(const sp<EventThread>& eventThread)
60{
61 mEventThread = eventThread;
62 mEvents = eventThread->createEventConnection();
63 mEventTube = mEvents->getDataChannel();
64 mLooper->addFd(mEventTube->getFd(), 0, ALOOPER_EVENT_INPUT,
65 MessageQueue::cb_eventReceiver, this);
66}
67
Mathias Agopianf61c57f2011-11-23 16:49:10 -080068void MessageQueue::waitMessage() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -070069 do {
Mathias Agopianf61c57f2011-11-23 16:49:10 -080070 IPCThreadState::self()->flushCommands();
Mathias Agopianb6683b52009-04-28 03:17:50 -070071
Mathias Agopianf61c57f2011-11-23 16:49:10 -080072 int32_t ret = mLooper->pollOnce(-1);
73 switch (ret) {
74 case ALOOPER_POLL_WAKE:
Mathias Agopianf61c57f2011-11-23 16:49:10 -080075 case ALOOPER_POLL_CALLBACK:
Mathias Agopianbe42aef2011-12-03 14:47:29 -080076 // callback and/or wake
77 if (android_atomic_and(0, &mWorkPending)) {
78 return;
79 }
Mathias Agopianf61c57f2011-11-23 16:49:10 -080080 continue;
81
82 case ALOOPER_POLL_TIMEOUT:
83 // timeout (should not happen)
84 continue;
85
86 case ALOOPER_POLL_ERROR:
Steve Blocke6f43dd2012-01-06 19:20:56 +000087 ALOGE("ALOOPER_POLL_ERROR");
Mathias Agopianf61c57f2011-11-23 16:49:10 -080088 continue;
89
90 default:
91 // should not happen
Steve Blocke6f43dd2012-01-06 19:20:56 +000092 ALOGE("Looper::pollOnce() returned unknown status %d", ret);
Mathias Agopianf61c57f2011-11-23 16:49:10 -080093 continue;
94 }
95 } while (true);
Mathias Agopianf1d8e872009-04-20 19:39:12 -070096}
97
98status_t MessageQueue::postMessage(
Mathias Agopianf61c57f2011-11-23 16:49:10 -080099 const sp<MessageBase>& messageHandler, nsecs_t relTime)
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700100{
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800101 const Message dummyMessage;
102 if (relTime > 0) {
103 mLooper->sendMessageDelayed(relTime, messageHandler, dummyMessage);
104 } else {
105 mLooper->sendMessage(messageHandler, dummyMessage);
106 }
107 return NO_ERROR;
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700108}
109
Mathias Agopian8aedd472012-01-24 16:39:14 -0800110void MessageQueue::scheduleWorkASAP() {
Mathias Agopianbe42aef2011-12-03 14:47:29 -0800111 if (android_atomic_or(1, &mWorkPending) == 0) {
112 mLooper->wake();
Mathias Agopian8aedd472012-01-24 16:39:14 -0800113 }
114}
115
116status_t MessageQueue::invalidate() {
117 mEvents->requestNextVsync();
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700118 return NO_ERROR;
119}
120
Mathias Agopian8aedd472012-01-24 16:39:14 -0800121int MessageQueue::cb_eventReceiver(int fd, int events, void* data) {
122 MessageQueue* queue = reinterpret_cast<MessageQueue *>(data);
123 return queue->eventReceiver(fd, events);
124}
125
126int MessageQueue::eventReceiver(int fd, int events) {
127 ssize_t n;
128 DisplayEventReceiver::Event buffer[8];
129 while ((n = getEvents(buffer, 8)) > 0) {
130 for (int i=0 ; i<n ; i++) {
131 if (buffer[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
132 scheduleWorkASAP();
133 break;
134 }
135 }
136 }
137 return 1;
138}
139
140ssize_t MessageQueue::getEvents(
141 DisplayEventReceiver::Event* events, size_t count)
142{
143 ssize_t size = mEventTube->read(events, sizeof(events[0])*count);
144 ALOGE_IF(size<0, "MessageQueue::getEvents error (%s)", strerror(-size));
145 if (size >= 0) {
146 // Note: if (size % sizeof(events[0])) != 0, we've got a
147 // partial read. This can happen if the queue filed up (ie: if we
148 // didn't pull from it fast enough).
149 // We discard the partial event and rely on the sender to
150 // re-send the event if appropriate (some events, like VSYNC
151 // can be lost forever).
152 // returns number of events read
153 size /= sizeof(events[0]);
154 }
155 return size;
156}
157
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700158// ---------------------------------------------------------------------------
159
160}; // namespace android