Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 1 | /* |
| 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 Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 20 | #include <errno.h> |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 21 | #include <stdint.h> |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 24 | #include <utils/Looper.h> |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 25 | #include <utils/Timers.h> |
| 26 | #include <utils/threads.h> |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 27 | |
Lloyd Pique | 9123ae5 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 28 | #include <gui/IDisplayEventConnection.h> |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 29 | #include <private/gui/BitTube.h> |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 30 | |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 31 | #include "Barrier.h" |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 32 | |
Alex Sakhartchouk | 117698b | 2017-06-07 11:36:32 -0400 | [diff] [blame] | 33 | #include <functional> |
| 34 | |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 35 | namespace android { |
| 36 | |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 37 | class EventThread; |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 38 | class SurfaceFlinger; |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 39 | |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 40 | // --------------------------------------------------------------------------- |
| 41 | |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 42 | class MessageBase : public MessageHandler { |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 43 | public: |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 44 | MessageBase(); |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 45 | |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 46 | // return true if message has a handler |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 47 | virtual bool handler() = 0; |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 48 | |
| 49 | // waits for the handler to be processed |
| 50 | void wait() const { barrier.wait(); } |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 51 | |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 52 | protected: |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 53 | virtual ~MessageBase(); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 54 | |
| 55 | private: |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 56 | virtual void handleMessage(const Message& message); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 57 | |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 58 | mutable Barrier barrier; |
| 59 | }; |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 60 | |
Alex Sakhartchouk | 117698b | 2017-06-07 11:36:32 -0400 | [diff] [blame] | 61 | class LambdaMessage : public MessageBase { |
| 62 | public: |
| 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 | |
| 72 | private: |
| 73 | const std::function<void()> mHandler; |
| 74 | }; |
| 75 | |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 76 | // --------------------------------------------------------------------------- |
| 77 | |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 78 | class MessageQueue { |
Lloyd Pique | 9123ae5 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 79 | public: |
| 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 | |
| 97 | namespace impl { |
| 98 | |
| 99 | class MessageQueue final : public android::MessageQueue { |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 100 | class Handler : public MessageHandler { |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 101 | enum { eventMaskInvalidate = 0x1, eventMaskRefresh = 0x2, eventMaskTransaction = 0x4 }; |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 102 | MessageQueue& mQueue; |
| 103 | int32_t mEventMask; |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 104 | |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 105 | public: |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 106 | explicit Handler(MessageQueue& queue) : mQueue(queue), mEventMask(0) {} |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 107 | virtual void handleMessage(const Message& message); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 108 | void dispatchRefresh(); |
| 109 | void dispatchInvalidate(); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | friend class Handler; |
| 113 | |
| 114 | sp<SurfaceFlinger> mFlinger; |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 115 | sp<Looper> mLooper; |
Lloyd Pique | 9123ae5 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 116 | android::EventThread* mEventThread; |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 117 | sp<IDisplayEventConnection> mEvents; |
Dan Stoza | 6b698e4 | 2017-04-03 13:09:08 -0700 | [diff] [blame] | 118 | gui::BitTube mEventTube; |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 119 | sp<Handler> mHandler; |
| 120 | |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 121 | static int cb_eventReceiver(int fd, int events, void* data); |
| 122 | int eventReceiver(int fd, int events); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 123 | |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 124 | public: |
Lloyd Pique | 9123ae5 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 125 | ~MessageQueue() override = default; |
| 126 | void init(const sp<SurfaceFlinger>& flinger) override; |
| 127 | void setEventThread(android::EventThread* events) override; |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 128 | |
Lloyd Pique | 9123ae5 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 129 | void waitMessage() override; |
| 130 | status_t postMessage(const sp<MessageBase>& message, nsecs_t reltime = 0) override; |
Mathias Agopian | 9eb1f05 | 2013-04-10 16:27:17 -0700 | [diff] [blame] | 131 | |
| 132 | // sends INVALIDATE message at next VSYNC |
Lloyd Pique | 9123ae5 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 133 | void invalidate() override; |
Mathias Agopian | 9eb1f05 | 2013-04-10 16:27:17 -0700 | [diff] [blame] | 134 | // sends REFRESH message at next VSYNC |
Lloyd Pique | 9123ae5 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 135 | void refresh() override; |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | // --------------------------------------------------------------------------- |
| 139 | |
Lloyd Pique | 9123ae5 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 140 | } // namespace impl |
| 141 | } // namespace android |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 142 | |
| 143 | #endif /* ANDROID_MESSAGE_QUEUE_H */ |