blob: 641a0a34d883ec86c86822cb1ef642da15e30b5d [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
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070042void MessageQueue::Handler::dispatchInvalidate(int64_t vsyncId, nsecs_t expectedVSyncTimestamp) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080043 if ((android_atomic_or(eventMaskInvalidate, &mEventMask) & eventMaskInvalidate) == 0) {
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070044 mVsyncId = vsyncId;
Ady Abraham5facfb12020-04-22 15:18:31 -070045 mExpectedVSyncTime = expectedVSyncTimestamp;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080046 mQueue.mLooper->sendMessage(this, Message(MessageQueue::INVALIDATE));
47 }
48}
49
50void MessageQueue::Handler::handleMessage(const Message& message) {
51 switch (message.what) {
52 case INVALIDATE:
53 android_atomic_and(~eventMaskInvalidate, &mEventMask);
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070054 mQueue.mFlinger->onMessageReceived(message.what, mVsyncId, mExpectedVSyncTime);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080055 break;
56 case REFRESH:
57 android_atomic_and(~eventMaskRefresh, &mEventMask);
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070058 mQueue.mFlinger->onMessageReceived(message.what, mVsyncId, mExpectedVSyncTime);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080059 break;
60 }
61}
62
63// ---------------------------------------------------------------------------
64
Lloyd Pique78ce4182018-01-31 16:39:51 -080065void MessageQueue::init(const sp<SurfaceFlinger>& flinger) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080066 mFlinger = flinger;
67 mLooper = new Looper(true);
68 mHandler = new Handler(*this);
69}
70
Ana Krulec85c39af2018-12-26 17:29:57 -080071void MessageQueue::setEventConnection(const sp<EventThreadConnection>& connection) {
Ana Krulec98b5b242018-08-10 15:03:23 -070072 if (mEventTube.getFd() >= 0) {
73 mLooper->removeFd(mEventTube.getFd());
74 }
75
76 mEvents = connection;
77 mEvents->stealReceiveChannel(&mEventTube);
78 mLooper->addFd(mEventTube.getFd(), 0, Looper::EVENT_INPUT, MessageQueue::cb_eventReceiver,
79 this);
80}
81
Mathias Agopianf61c57f2011-11-23 16:49:10 -080082void MessageQueue::waitMessage() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -070083 do {
Mathias Agopianf61c57f2011-11-23 16:49:10 -080084 IPCThreadState::self()->flushCommands();
Mathias Agopianf61c57f2011-11-23 16:49:10 -080085 int32_t ret = mLooper->pollOnce(-1);
86 switch (ret) {
Brian Carlstromfe761ab2013-12-12 23:13:18 -080087 case Looper::POLL_WAKE:
88 case Looper::POLL_CALLBACK:
Mathias Agopianf61c57f2011-11-23 16:49:10 -080089 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -080090 case Looper::POLL_ERROR:
91 ALOGE("Looper::POLL_ERROR");
Pablo Ceballos53390e12015-08-04 11:25:59 -070092 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -080093 case Looper::POLL_TIMEOUT:
Mathias Agopianf61c57f2011-11-23 16:49:10 -080094 // timeout (should not happen)
95 continue;
Mathias Agopianf61c57f2011-11-23 16:49:10 -080096 default:
97 // should not happen
Steve Blocke6f43dd2012-01-06 19:20:56 +000098 ALOGE("Looper::pollOnce() returned unknown status %d", ret);
Mathias Agopianf61c57f2011-11-23 16:49:10 -080099 continue;
100 }
101 } while (true);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700102}
103
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700104void MessageQueue::postMessage(sp<MessageHandler>&& handler) {
105 mLooper->sendMessage(handler, Message());
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700106}
107
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800108void MessageQueue::invalidate() {
Mathias Agopian69a655c2012-04-11 20:43:19 -0700109 mEvents->requestNextVsync();
Mathias Agopian8aedd472012-01-24 16:39:14 -0800110}
111
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800112void MessageQueue::refresh() {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700113 mHandler->dispatchRefresh();
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700114}
115
Mathias Agopian8aedd472012-01-24 16:39:14 -0800116int MessageQueue::cb_eventReceiver(int fd, int events, void* data) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800117 MessageQueue* queue = reinterpret_cast<MessageQueue*>(data);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800118 return queue->eventReceiver(fd, events);
119}
120
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700121int MessageQueue::eventReceiver(int /*fd*/, int /*events*/) {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800122 ssize_t n;
123 DisplayEventReceiver::Event buffer[8];
Dan Stoza6b698e42017-04-03 13:09:08 -0700124 while ((n = DisplayEventReceiver::getEvents(&mEventTube, buffer, 8)) > 0) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800125 for (int i = 0; i < n; i++) {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800126 if (buffer[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700127 mHandler->dispatchInvalidate(buffer[i].vsync.vsyncId,
128 buffer[i].vsync.expectedVSyncTimestamp);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800129 break;
130 }
131 }
132 }
133 return 1;
134}
135
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700136} // namespace android::impl
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800137
138// TODO(b/129481165): remove the #pragma below and fix conversion issues
139#pragma clang diagnostic pop // ignored "-Wconversion"