blob: 90d1c724509b8717f32094185f83722c9783ff44 [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 Pique9123ae52018-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"
Mathias Agopianf1d8e872009-04-20 19:39:12 -070032
Alex Sakhartchouk117698b2017-06-07 11:36:32 -040033#include <functional>
34
Mathias Agopianf1d8e872009-04-20 19:39:12 -070035namespace android {
36
Mathias Agopian8aedd472012-01-24 16:39:14 -080037class EventThread;
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 Pique9123ae52018-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;
88 virtual void setEventThread(EventThread* events) = 0;
89 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;
Lloyd Pique78ce4182018-01-31 16:39:51 -0800104
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800105 public:
Lloyd Pique78ce4182018-01-31 16:39:51 -0800106 explicit Handler(MessageQueue& queue) : mQueue(queue), mEventMask(0) {}
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800107 virtual void handleMessage(const Message& message);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700108 void dispatchRefresh();
109 void dispatchInvalidate();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800110 };
111
112 friend class Handler;
113
114 sp<SurfaceFlinger> mFlinger;
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800115 sp<Looper> mLooper;
Lloyd Pique9123ae52018-01-22 17:14:00 -0800116 android::EventThread* mEventThread;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800117 sp<IDisplayEventConnection> 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 Pique9123ae52018-01-22 17:14:00 -0800125 ~MessageQueue() override = default;
126 void init(const sp<SurfaceFlinger>& flinger) override;
127 void setEventThread(android::EventThread* events) override;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800128
Lloyd Pique9123ae52018-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 Pique9123ae52018-01-22 17:14:00 -0800133 void invalidate() override;
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700134 // sends REFRESH message at next VSYNC
Lloyd Pique9123ae52018-01-22 17:14:00 -0800135 void refresh() override;
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700136};
137
138// ---------------------------------------------------------------------------
139
Lloyd Pique9123ae52018-01-22 17:14:00 -0800140} // namespace impl
141} // namespace android
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700142
143#endif /* ANDROID_MESSAGE_QUEUE_H */