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 // TODO(b/129481165): remove the #pragma below and fix conversion issues
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wconversion"
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::impl {
35 
dispatchRefresh()36 void MessageQueue::Handler::dispatchRefresh() {
37     if ((android_atomic_or(eventMaskRefresh, &mEventMask) & eventMaskRefresh) == 0) {
38         mQueue.mLooper->sendMessage(this, Message(MessageQueue::REFRESH));
39     }
40 }
41 
dispatchInvalidate(nsecs_t expectedVSyncTimestamp)42 void MessageQueue::Handler::dispatchInvalidate(nsecs_t expectedVSyncTimestamp) {
43     if ((android_atomic_or(eventMaskInvalidate, &mEventMask) & eventMaskInvalidate) == 0) {
44         mExpectedVSyncTime = expectedVSyncTimestamp;
45         mQueue.mLooper->sendMessage(this, Message(MessageQueue::INVALIDATE));
46     }
47 }
48 
handleMessage(const Message & message)49 void MessageQueue::Handler::handleMessage(const Message& message) {
50     switch (message.what) {
51         case INVALIDATE:
52             android_atomic_and(~eventMaskInvalidate, &mEventMask);
53             mQueue.mFlinger->onMessageReceived(message.what, mExpectedVSyncTime);
54             break;
55         case REFRESH:
56             android_atomic_and(~eventMaskRefresh, &mEventMask);
57             mQueue.mFlinger->onMessageReceived(message.what, mExpectedVSyncTime);
58             break;
59     }
60 }
61 
62 // ---------------------------------------------------------------------------
63 
init(const sp<SurfaceFlinger> & flinger)64 void MessageQueue::init(const sp<SurfaceFlinger>& flinger) {
65     mFlinger = flinger;
66     mLooper = new Looper(true);
67     mHandler = new Handler(*this);
68 }
69 
setEventConnection(const sp<EventThreadConnection> & connection)70 void MessageQueue::setEventConnection(const sp<EventThreadConnection>& connection) {
71     if (mEventTube.getFd() >= 0) {
72         mLooper->removeFd(mEventTube.getFd());
73     }
74 
75     mEvents = connection;
76     mEvents->stealReceiveChannel(&mEventTube);
77     mLooper->addFd(mEventTube.getFd(), 0, Looper::EVENT_INPUT, MessageQueue::cb_eventReceiver,
78                    this);
79 }
80 
waitMessage()81 void MessageQueue::waitMessage() {
82     do {
83         IPCThreadState::self()->flushCommands();
84         int32_t ret = mLooper->pollOnce(-1);
85         switch (ret) {
86             case Looper::POLL_WAKE:
87             case Looper::POLL_CALLBACK:
88                 continue;
89             case Looper::POLL_ERROR:
90                 ALOGE("Looper::POLL_ERROR");
91                 continue;
92             case Looper::POLL_TIMEOUT:
93                 // timeout (should not happen)
94                 continue;
95             default:
96                 // should not happen
97                 ALOGE("Looper::pollOnce() returned unknown status %d", ret);
98                 continue;
99         }
100     } while (true);
101 }
102 
postMessage(sp<MessageHandler> && handler)103 void MessageQueue::postMessage(sp<MessageHandler>&& handler) {
104     mLooper->sendMessage(handler, Message());
105 }
106 
invalidate()107 void MessageQueue::invalidate() {
108     mEvents->requestNextVsync();
109 }
110 
refresh()111 void MessageQueue::refresh() {
112     mHandler->dispatchRefresh();
113 }
114 
cb_eventReceiver(int fd,int events,void * data)115 int MessageQueue::cb_eventReceiver(int fd, int events, void* data) {
116     MessageQueue* queue = reinterpret_cast<MessageQueue*>(data);
117     return queue->eventReceiver(fd, events);
118 }
119 
eventReceiver(int,int)120 int MessageQueue::eventReceiver(int /*fd*/, int /*events*/) {
121     ssize_t n;
122     DisplayEventReceiver::Event buffer[8];
123     while ((n = DisplayEventReceiver::getEvents(&mEventTube, buffer, 8)) > 0) {
124         for (int i = 0; i < n; i++) {
125             if (buffer[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
126                 mHandler->dispatchInvalidate(buffer[i].vsync.expectedVSyncTimestamp);
127                 break;
128             }
129         }
130     }
131     return 1;
132 }
133 
134 } // namespace android::impl
135 
136 // TODO(b/129481165): remove the #pragma below and fix conversion issues
137 #pragma clang diagnostic pop // ignored "-Wconversion"
138