1 /*
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
21 #include <binder/IPCThreadState.h>
22
23 #include <utils/threads.h>
24 #include <utils/Timers.h>
25 #include <utils/Log.h>
26
27 #include <gui/IDisplayEventConnection.h>
28 #include <gui/BitTube.h>
29
30 #include "MessageQueue.h"
31 #include "EventThread.h"
32 #include "SurfaceFlinger.h"
33
34 namespace android {
35
36 // ---------------------------------------------------------------------------
37
MessageBase()38 MessageBase::MessageBase()
39 : MessageHandler() {
40 }
41
~MessageBase()42 MessageBase::~MessageBase() {
43 }
44
handleMessage(const Message &)45 void MessageBase::handleMessage(const Message&) {
46 this->handler();
47 barrier.open();
48 };
49
50 // ---------------------------------------------------------------------------
51
dispatchRefresh()52 void MessageQueue::Handler::dispatchRefresh() {
53 if ((android_atomic_or(eventMaskRefresh, &mEventMask) & eventMaskRefresh) == 0) {
54 mQueue.mLooper->sendMessage(this, Message(MessageQueue::REFRESH));
55 }
56 }
57
dispatchInvalidate()58 void MessageQueue::Handler::dispatchInvalidate() {
59 if ((android_atomic_or(eventMaskInvalidate, &mEventMask) & eventMaskInvalidate) == 0) {
60 mQueue.mLooper->sendMessage(this, Message(MessageQueue::INVALIDATE));
61 }
62 }
63
handleMessage(const Message & message)64 void 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
MessageQueue()79 MessageQueue::MessageQueue()
80 {
81 }
82
~MessageQueue()83 MessageQueue::~MessageQueue() {
84 }
85
init(const sp<SurfaceFlinger> & flinger)86 void MessageQueue::init(const sp<SurfaceFlinger>& flinger)
87 {
88 mFlinger = flinger;
89 mLooper = new Looper(true);
90 mHandler = new Handler(*this);
91 }
92
setEventThread(const sp<EventThread> & eventThread)93 void MessageQueue::setEventThread(const sp<EventThread>& eventThread)
94 {
95 mEventThread = eventThread;
96 mEvents = eventThread->createEventConnection();
97 mEventTube = mEvents->getDataChannel();
98 mLooper->addFd(mEventTube->getFd(), 0, ALOOPER_EVENT_INPUT,
99 MessageQueue::cb_eventReceiver, this);
100 }
101
waitMessage()102 void MessageQueue::waitMessage() {
103 do {
104 IPCThreadState::self()->flushCommands();
105 int32_t ret = mLooper->pollOnce(-1);
106 switch (ret) {
107 case ALOOPER_POLL_WAKE:
108 case ALOOPER_POLL_CALLBACK:
109 continue;
110 case ALOOPER_POLL_ERROR:
111 ALOGE("ALOOPER_POLL_ERROR");
112 case ALOOPER_POLL_TIMEOUT:
113 // timeout (should not happen)
114 continue;
115 default:
116 // should not happen
117 ALOGE("Looper::pollOnce() returned unknown status %d", ret);
118 continue;
119 }
120 } while (true);
121 }
122
postMessage(const sp<MessageBase> & messageHandler,nsecs_t relTime)123 status_t MessageQueue::postMessage(
124 const sp<MessageBase>& messageHandler, nsecs_t relTime)
125 {
126 const Message dummyMessage;
127 if (relTime > 0) {
128 mLooper->sendMessageDelayed(relTime, messageHandler, dummyMessage);
129 } else {
130 mLooper->sendMessage(messageHandler, dummyMessage);
131 }
132 return NO_ERROR;
133 }
134
135 /* when INVALIDATE_ON_VSYNC is set SF only processes
136 * buffer updates on VSYNC and performs a refresh immediately
137 * after.
138 *
139 * when INVALIDATE_ON_VSYNC is set to false, SF will instead
140 * perform the buffer updates immediately, but the refresh only
141 * at the next VSYNC.
142 * THIS MODE IS BUGGY ON GALAXY NEXUS AND WILL CAUSE HANGS
143 */
144 #define INVALIDATE_ON_VSYNC 1
145
invalidate()146 void MessageQueue::invalidate() {
147 #if INVALIDATE_ON_VSYNC
148 mEvents->requestNextVsync();
149 #else
150 mHandler->dispatchInvalidate();
151 #endif
152 }
153
refresh()154 void MessageQueue::refresh() {
155 #if INVALIDATE_ON_VSYNC
156 mHandler->dispatchRefresh();
157 #else
158 mEvents->requestNextVsync();
159 #endif
160 }
161
cb_eventReceiver(int fd,int events,void * data)162 int MessageQueue::cb_eventReceiver(int fd, int events, void* data) {
163 MessageQueue* queue = reinterpret_cast<MessageQueue *>(data);
164 return queue->eventReceiver(fd, events);
165 }
166
eventReceiver(int fd,int events)167 int MessageQueue::eventReceiver(int fd, int events) {
168 ssize_t n;
169 DisplayEventReceiver::Event buffer[8];
170 while ((n = DisplayEventReceiver::getEvents(mEventTube, buffer, 8)) > 0) {
171 for (int i=0 ; i<n ; i++) {
172 if (buffer[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
173 #if INVALIDATE_ON_VSYNC
174 mHandler->dispatchInvalidate();
175 #else
176 mHandler->dispatchRefresh();
177 #endif
178 break;
179 }
180 }
181 }
182 return 1;
183 }
184
185 // ---------------------------------------------------------------------------
186
187 }; // namespace android
188