blob: ebc4315ec9ceb8c962b2aaa905e1f748ca661d91 [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
17#ifndef ANDROID_MESSAGE_QUEUE_H
18#define ANDROID_MESSAGE_QUEUE_H
19
Mathias Agopianf1d8e872009-04-20 19:39:12 -070020#include <errno.h>
Lloyd Pique78ce4182018-01-31 16:39:51 -080021#include <stdint.h>
Mathias Agopianf1d8e872009-04-20 19:39:12 -070022#include <sys/types.h>
23
Mathias Agopianf61c57f2011-11-23 16:49:10 -080024#include <utils/Looper.h>
Lloyd Pique78ce4182018-01-31 16:39:51 -080025#include <utils/Timers.h>
26#include <utils/threads.h>
Mathias Agopianf1d8e872009-04-20 19:39:12 -070027
Lloyd Pique3fcdef12018-01-22 17:14:00 -080028#include <gui/IDisplayEventConnection.h>
Lloyd Pique78ce4182018-01-31 16:39:51 -080029#include <private/gui/BitTube.h>
Mathias Agopian8aedd472012-01-24 16:39:14 -080030
Mathias Agopianbb641242010-05-18 17:06:55 -070031#include "Barrier.h"
Dominik Laskowskif654d572018-12-20 11:03:06 -080032#include "EventThread.h"
Mathias Agopianf1d8e872009-04-20 19:39:12 -070033
Alex Sakhartchouk117698b2017-06-07 11:36:32 -040034#include <functional>
35
Mathias Agopianf1d8e872009-04-20 19:39:12 -070036namespace android {
37
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080038class SurfaceFlinger;
Mathias Agopian8aedd472012-01-24 16:39:14 -080039
Mathias Agopianf1d8e872009-04-20 19:39:12 -070040// ---------------------------------------------------------------------------
41
Lloyd Pique78ce4182018-01-31 16:39:51 -080042class MessageBase : public MessageHandler {
Mathias Agopianf1d8e872009-04-20 19:39:12 -070043public:
Mathias Agopianf61c57f2011-11-23 16:49:10 -080044 MessageBase();
Lloyd Pique78ce4182018-01-31 16:39:51 -080045
Mathias Agopianf1d8e872009-04-20 19:39:12 -070046 // return true if message has a handler
Mathias Agopianf61c57f2011-11-23 16:49:10 -080047 virtual bool handler() = 0;
Mathias Agopianbb641242010-05-18 17:06:55 -070048
49 // waits for the handler to be processed
50 void wait() const { barrier.wait(); }
Mathias Agopianbb641242010-05-18 17:06:55 -070051
Mathias Agopianf1d8e872009-04-20 19:39:12 -070052protected:
Mathias Agopianf61c57f2011-11-23 16:49:10 -080053 virtual ~MessageBase();
Mathias Agopianf1d8e872009-04-20 19:39:12 -070054
55private:
Mathias Agopianf61c57f2011-11-23 16:49:10 -080056 virtual void handleMessage(const Message& message);
Mathias Agopianf1d8e872009-04-20 19:39:12 -070057
Mathias Agopianf61c57f2011-11-23 16:49:10 -080058 mutable Barrier barrier;
59};
Mathias Agopianf1d8e872009-04-20 19:39:12 -070060
Alex Sakhartchouk117698b2017-06-07 11:36:32 -040061class LambdaMessage : public MessageBase {
62public:
63 explicit LambdaMessage(std::function<void()> handler)
64 : MessageBase(), mHandler(std::move(handler)) {}
65
66 bool handler() override {
67 mHandler();
68 // This return value is no longer checked, so it's always safe to return true
69 return true;
70 }
71
72private:
73 const std::function<void()> mHandler;
74};
75
Mathias Agopianf1d8e872009-04-20 19:39:12 -070076// ---------------------------------------------------------------------------
77
Mathias Agopianf61c57f2011-11-23 16:49:10 -080078class MessageQueue {
Lloyd Pique3fcdef12018-01-22 17:14:00 -080079public:
80 enum {
81 INVALIDATE = 0,
82 REFRESH = 1,
83 };
84
85 virtual ~MessageQueue();
86
87 virtual void init(const sp<SurfaceFlinger>& flinger) = 0;
Ana Krulec85c39af2018-12-26 17:29:57 -080088 virtual void setEventConnection(const sp<EventThreadConnection>& connection) = 0;
Lloyd Pique3fcdef12018-01-22 17:14:00 -080089 virtual void waitMessage() = 0;
90 virtual status_t postMessage(const sp<MessageBase>& message, nsecs_t reltime = 0) = 0;
91 virtual void invalidate() = 0;
92 virtual void refresh() = 0;
93};
94
95// ---------------------------------------------------------------------------
96
97namespace impl {
98
99class MessageQueue final : public android::MessageQueue {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800100 class Handler : public MessageHandler {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800101 enum { eventMaskInvalidate = 0x1, eventMaskRefresh = 0x2, eventMaskTransaction = 0x4 };
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800102 MessageQueue& mQueue;
103 int32_t mEventMask;
Ady Abraham5facfb12020-04-22 15:18:31 -0700104 std::atomic<nsecs_t> mExpectedVSyncTime;
Lloyd Pique78ce4182018-01-31 16:39:51 -0800105
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800106 public:
Lloyd Pique78ce4182018-01-31 16:39:51 -0800107 explicit Handler(MessageQueue& queue) : mQueue(queue), mEventMask(0) {}
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800108 virtual void handleMessage(const Message& message);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700109 void dispatchRefresh();
Ady Abraham5facfb12020-04-22 15:18:31 -0700110 void dispatchInvalidate(nsecs_t expectedVSyncTimestamp);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800111 };
112
113 friend class Handler;
114
115 sp<SurfaceFlinger> mFlinger;
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800116 sp<Looper> mLooper;
Ana Krulec85c39af2018-12-26 17:29:57 -0800117 sp<EventThreadConnection> mEvents;
Dan Stoza6b698e42017-04-03 13:09:08 -0700118 gui::BitTube mEventTube;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800119 sp<Handler> mHandler;
120
Mathias Agopian8aedd472012-01-24 16:39:14 -0800121 static int cb_eventReceiver(int fd, int events, void* data);
122 int eventReceiver(int fd, int events);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700123
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800124public:
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800125 ~MessageQueue() override = default;
126 void init(const sp<SurfaceFlinger>& flinger) override;
Ana Krulec85c39af2018-12-26 17:29:57 -0800127 void setEventConnection(const sp<EventThreadConnection>& connection) override;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800128
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800129 void waitMessage() override;
130 status_t postMessage(const sp<MessageBase>& message, nsecs_t reltime = 0) override;
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700131
132 // sends INVALIDATE message at next VSYNC
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800133 void invalidate() override;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800134
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700135 // sends REFRESH message at next VSYNC
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800136 void refresh() override;
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700137};
138
139// ---------------------------------------------------------------------------
140
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800141} // namespace impl
142} // namespace android
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700143
144#endif /* ANDROID_MESSAGE_QUEUE_H */