blob: 0b2206d2af5db780f25f2571356196c9518bbdfe [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;
Ady Abraham46e2f3e2019-03-18 16:40:15 -070088 // TODO(b/128863962): Remove this function once everything is migrated to Scheduler.
Dominik Laskowskif654d572018-12-20 11:03:06 -080089 virtual void setEventThread(EventThread* events, ResyncCallback resyncCallback) = 0;
Ana Krulec85c39af2018-12-26 17:29:57 -080090 virtual void setEventConnection(const sp<EventThreadConnection>& connection) = 0;
Lloyd Pique3fcdef12018-01-22 17:14:00 -080091 virtual void waitMessage() = 0;
92 virtual status_t postMessage(const sp<MessageBase>& message, nsecs_t reltime = 0) = 0;
93 virtual void invalidate() = 0;
94 virtual void refresh() = 0;
95};
96
97// ---------------------------------------------------------------------------
98
99namespace impl {
100
101class MessageQueue final : public android::MessageQueue {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800102 class Handler : public MessageHandler {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800103 enum { eventMaskInvalidate = 0x1, eventMaskRefresh = 0x2, eventMaskTransaction = 0x4 };
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800104 MessageQueue& mQueue;
105 int32_t mEventMask;
Lloyd Pique78ce4182018-01-31 16:39:51 -0800106
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800107 public:
Lloyd Pique78ce4182018-01-31 16:39:51 -0800108 explicit Handler(MessageQueue& queue) : mQueue(queue), mEventMask(0) {}
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800109 virtual void handleMessage(const Message& message);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700110 void dispatchRefresh();
111 void dispatchInvalidate();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800112 };
113
114 friend class Handler;
115
116 sp<SurfaceFlinger> mFlinger;
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800117 sp<Looper> mLooper;
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800118 android::EventThread* mEventThread;
Ana Krulec85c39af2018-12-26 17:29:57 -0800119 sp<EventThreadConnection> mEvents;
Dan Stoza6b698e42017-04-03 13:09:08 -0700120 gui::BitTube mEventTube;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800121 sp<Handler> mHandler;
122
Mathias Agopian8aedd472012-01-24 16:39:14 -0800123 static int cb_eventReceiver(int fd, int events, void* data);
124 int eventReceiver(int fd, int events);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700125
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800126public:
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800127 ~MessageQueue() override = default;
128 void init(const sp<SurfaceFlinger>& flinger) override;
Dominik Laskowskif654d572018-12-20 11:03:06 -0800129 void setEventThread(android::EventThread* events, ResyncCallback resyncCallback) override;
Ana Krulec85c39af2018-12-26 17:29:57 -0800130 void setEventConnection(const sp<EventThreadConnection>& connection) override;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800131
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800132 void waitMessage() override;
133 status_t postMessage(const sp<MessageBase>& message, nsecs_t reltime = 0) override;
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700134
135 // sends INVALIDATE message at next VSYNC
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800136 void invalidate() override;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800137
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700138 // sends REFRESH message at next VSYNC
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800139 void refresh() override;
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700140};
141
142// ---------------------------------------------------------------------------
143
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800144} // namespace impl
145} // namespace android
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700146
147#endif /* ANDROID_MESSAGE_QUEUE_H */