blob: 6067e69ea054e3453dc4c016285218e18a96dab6 [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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Mathias Agopian8aedd472012-01-24 16:39:14 -080021#include <binder/IPCThreadState.h>
22
Mathias Agopianf1d8e872009-04-20 19:39:12 -070023#include <utils/Log.h>
Lloyd Pique78ce4182018-01-31 16:39:51 -080024#include <utils/Timers.h>
25#include <utils/threads.h>
Mathias Agopian8aedd472012-01-24 16:39:14 -080026
Lloyd Pique3fcdef12018-01-22 17:14:00 -080027#include <gui/DisplayEventReceiver.h>
Mathias Agopian8aedd472012-01-24 16:39:14 -080028#include <gui/IDisplayEventConnection.h>
Mathias Agopianf1d8e872009-04-20 19:39:12 -070029
Mathias Agopian8aedd472012-01-24 16:39:14 -080030#include "EventThread.h"
Lloyd Pique78ce4182018-01-31 16:39:51 -080031#include "MessageQueue.h"
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080032#include "SurfaceFlinger.h"
Mathias Agopianf1d8e872009-04-20 19:39:12 -070033
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070034namespace android::impl {
Lloyd Pique3fcdef12018-01-22 17:14:00 -080035
Mathias Agopian4fec8732012-06-29 14:12:52 -070036void MessageQueue::Handler::dispatchRefresh() {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080037 if ((android_atomic_or(eventMaskRefresh, &mEventMask) & eventMaskRefresh) == 0) {
38 mQueue.mLooper->sendMessage(this, Message(MessageQueue::REFRESH));
39 }
40}
41
Ady Abraham5facfb12020-04-22 15:18:31 -070042void MessageQueue::Handler::dispatchInvalidate(nsecs_t expectedVSyncTimestamp) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080043 if ((android_atomic_or(eventMaskInvalidate, &mEventMask) & eventMaskInvalidate) == 0) {
Ady Abraham5facfb12020-04-22 15:18:31 -070044 mExpectedVSyncTime = expectedVSyncTimestamp;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080045 mQueue.mLooper->sendMessage(this, Message(MessageQueue::INVALIDATE));
46 }
47}
48
49void MessageQueue::Handler::handleMessage(const Message& message) {
50 switch (message.what) {
51 case INVALIDATE:
52 android_atomic_and(~eventMaskInvalidate, &mEventMask);
Ady Abraham5facfb12020-04-22 15:18:31 -070053 mQueue.mFlinger->onMessageReceived(message.what, mExpectedVSyncTime);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080054 break;
55 case REFRESH:
56 android_atomic_and(~eventMaskRefresh, &mEventMask);
Ady Abraham5facfb12020-04-22 15:18:31 -070057 mQueue.mFlinger->onMessageReceived(message.what, mExpectedVSyncTime);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080058 break;
59 }
60}
61
62// ---------------------------------------------------------------------------
63
Lloyd Pique78ce4182018-01-31 16:39:51 -080064void MessageQueue::init(const sp<SurfaceFlinger>& flinger) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080065 mFlinger = flinger;
66 mLooper = new Looper(true);
67 mHandler = new Handler(*this);
68}
69
Ana Krulec85c39af2018-12-26 17:29:57 -080070void MessageQueue::setEventConnection(const sp<EventThreadConnection>& connection) {
Ana Krulec98b5b242018-08-10 15:03:23 -070071 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
Mathias Agopianf61c57f2011-11-23 16:49:10 -080081void MessageQueue::waitMessage() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -070082 do {
Mathias Agopianf61c57f2011-11-23 16:49:10 -080083 IPCThreadState::self()->flushCommands();
Mathias Agopianf61c57f2011-11-23 16:49:10 -080084 int32_t ret = mLooper->pollOnce(-1);
85 switch (ret) {
Brian Carlstromfe761ab2013-12-12 23:13:18 -080086 case Looper::POLL_WAKE:
87 case Looper::POLL_CALLBACK:
Mathias Agopianf61c57f2011-11-23 16:49:10 -080088 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -080089 case Looper::POLL_ERROR:
90 ALOGE("Looper::POLL_ERROR");
Pablo Ceballos53390e12015-08-04 11:25:59 -070091 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -080092 case Looper::POLL_TIMEOUT:
Mathias Agopianf61c57f2011-11-23 16:49:10 -080093 // timeout (should not happen)
94 continue;
Mathias Agopianf61c57f2011-11-23 16:49:10 -080095 default:
96 // should not happen
Steve Blocke6f43dd2012-01-06 19:20:56 +000097 ALOGE("Looper::pollOnce() returned unknown status %d", ret);
Mathias Agopianf61c57f2011-11-23 16:49:10 -080098 continue;
99 }
100 } while (true);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700101}
102
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700103void MessageQueue::postMessage(sp<MessageHandler>&& handler) {
104 mLooper->sendMessage(handler, Message());
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700105}
106
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800107void MessageQueue::invalidate() {
Mathias Agopian69a655c2012-04-11 20:43:19 -0700108 mEvents->requestNextVsync();
Mathias Agopian8aedd472012-01-24 16:39:14 -0800109}
110
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800111void MessageQueue::refresh() {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700112 mHandler->dispatchRefresh();
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700113}
114
Mathias Agopian8aedd472012-01-24 16:39:14 -0800115int MessageQueue::cb_eventReceiver(int fd, int events, void* data) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800116 MessageQueue* queue = reinterpret_cast<MessageQueue*>(data);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800117 return queue->eventReceiver(fd, events);
118}
119
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700120int MessageQueue::eventReceiver(int /*fd*/, int /*events*/) {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800121 ssize_t n;
122 DisplayEventReceiver::Event buffer[8];
Dan Stoza6b698e42017-04-03 13:09:08 -0700123 while ((n = DisplayEventReceiver::getEvents(&mEventTube, buffer, 8)) > 0) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800124 for (int i = 0; i < n; i++) {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800125 if (buffer[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
Ady Abraham5facfb12020-04-22 15:18:31 -0700126 mHandler->dispatchInvalidate(buffer[i].vsync.expectedVSyncTimestamp);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800127 break;
128 }
129 }
130 }
131 return 1;
132}
133
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700134} // namespace android::impl
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800135
136// TODO(b/129481165): remove the #pragma below and fix conversion issues
137#pragma clang diagnostic pop // ignored "-Wconversion"