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 <errno.h>
18 #include <stdint.h>
19 #include <sys/types.h>
20
21 #include <binder/IPCThreadState.h>
22
23 #include <utils/Log.h>
24 #include <utils/Timers.h>
25 #include <utils/threads.h>
26
27 #include <gui/DisplayEventReceiver.h>
28 #include <gui/IDisplayEventConnection.h>
29
30 #include "EventThread.h"
31 #include "MessageQueue.h"
32 #include "SurfaceFlinger.h"
33
34 namespace android {
35
36 // ---------------------------------------------------------------------------
37
MessageBase()38 MessageBase::MessageBase() : MessageHandler() {}
39
~MessageBase()40 MessageBase::~MessageBase() {}
41
handleMessage(const Message &)42 void MessageBase::handleMessage(const Message&) {
43 this->handler();
44 barrier.open();
45 };
46
47 // ---------------------------------------------------------------------------
48
49 MessageQueue::~MessageQueue() = default;
50
51 // ---------------------------------------------------------------------------
52
53 namespace impl {
54
dispatchRefresh()55 void MessageQueue::Handler::dispatchRefresh() {
56 if ((android_atomic_or(eventMaskRefresh, &mEventMask) & eventMaskRefresh) == 0) {
57 mQueue.mLooper->sendMessage(this, Message(MessageQueue::REFRESH));
58 }
59 }
60
dispatchInvalidate()61 void MessageQueue::Handler::dispatchInvalidate() {
62 if ((android_atomic_or(eventMaskInvalidate, &mEventMask) & eventMaskInvalidate) == 0) {
63 mQueue.mLooper->sendMessage(this, Message(MessageQueue::INVALIDATE));
64 }
65 }
66
handleMessage(const Message & message)67 void MessageQueue::Handler::handleMessage(const Message& message) {
68 switch (message.what) {
69 case INVALIDATE:
70 android_atomic_and(~eventMaskInvalidate, &mEventMask);
71 mQueue.mFlinger->onMessageReceived(message.what);
72 break;
73 case REFRESH:
74 android_atomic_and(~eventMaskRefresh, &mEventMask);
75 mQueue.mFlinger->onMessageReceived(message.what);
76 break;
77 }
78 }
79
80 // ---------------------------------------------------------------------------
81
init(const sp<SurfaceFlinger> & flinger)82 void MessageQueue::init(const sp<SurfaceFlinger>& flinger) {
83 mFlinger = flinger;
84 mLooper = new Looper(true);
85 mHandler = new Handler(*this);
86 }
87
setEventThread(android::EventThread * eventThread,ResyncCallback resyncCallback)88 void MessageQueue::setEventThread(android::EventThread* eventThread,
89 ResyncCallback resyncCallback) {
90 if (mEventThread == eventThread) {
91 return;
92 }
93
94 if (mEventTube.getFd() >= 0) {
95 mLooper->removeFd(mEventTube.getFd());
96 }
97
98 mEventThread = eventThread;
99 mEvents = eventThread->createEventConnection(std::move(resyncCallback),
100 ISurfaceComposer::eConfigChangedSuppress);
101 mEvents->stealReceiveChannel(&mEventTube);
102 mLooper->addFd(mEventTube.getFd(), 0, Looper::EVENT_INPUT, MessageQueue::cb_eventReceiver,
103 this);
104 }
105
setEventConnection(const sp<EventThreadConnection> & connection)106 void MessageQueue::setEventConnection(const sp<EventThreadConnection>& connection) {
107 if (mEventTube.getFd() >= 0) {
108 mLooper->removeFd(mEventTube.getFd());
109 }
110
111 mEvents = connection;
112 mEvents->stealReceiveChannel(&mEventTube);
113 mLooper->addFd(mEventTube.getFd(), 0, Looper::EVENT_INPUT, MessageQueue::cb_eventReceiver,
114 this);
115 }
116
waitMessage()117 void MessageQueue::waitMessage() {
118 do {
119 IPCThreadState::self()->flushCommands();
120 int32_t ret = mLooper->pollOnce(-1);
121 switch (ret) {
122 case Looper::POLL_WAKE:
123 case Looper::POLL_CALLBACK:
124 continue;
125 case Looper::POLL_ERROR:
126 ALOGE("Looper::POLL_ERROR");
127 continue;
128 case Looper::POLL_TIMEOUT:
129 // timeout (should not happen)
130 continue;
131 default:
132 // should not happen
133 ALOGE("Looper::pollOnce() returned unknown status %d", ret);
134 continue;
135 }
136 } while (true);
137 }
138
postMessage(const sp<MessageBase> & messageHandler,nsecs_t relTime)139 status_t MessageQueue::postMessage(const sp<MessageBase>& messageHandler, nsecs_t relTime) {
140 const Message dummyMessage;
141 if (relTime > 0) {
142 mLooper->sendMessageDelayed(relTime, messageHandler, dummyMessage);
143 } else {
144 mLooper->sendMessage(messageHandler, dummyMessage);
145 }
146 return NO_ERROR;
147 }
148
invalidate()149 void MessageQueue::invalidate() {
150 mEvents->requestNextVsync();
151 }
152
refresh()153 void MessageQueue::refresh() {
154 mHandler->dispatchRefresh();
155 }
156
cb_eventReceiver(int fd,int events,void * data)157 int MessageQueue::cb_eventReceiver(int fd, int events, void* data) {
158 MessageQueue* queue = reinterpret_cast<MessageQueue*>(data);
159 return queue->eventReceiver(fd, events);
160 }
161
eventReceiver(int,int)162 int MessageQueue::eventReceiver(int /*fd*/, int /*events*/) {
163 ssize_t n;
164 DisplayEventReceiver::Event buffer[8];
165 while ((n = DisplayEventReceiver::getEvents(&mEventTube, buffer, 8)) > 0) {
166 for (int i = 0; i < n; i++) {
167 if (buffer[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
168 mHandler->dispatchInvalidate();
169 break;
170 }
171 }
172 }
173 return 1;
174 }
175
176 // ---------------------------------------------------------------------------
177
178 } // namespace impl
179 } // namespace android
180