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 | |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 28 | #include <gui/DisplayEventReceiver.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 IDisplayEventConnection; |
| 38 | class EventThread; |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 39 | class SurfaceFlinger; |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 40 | |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 41 | // --------------------------------------------------------------------------- |
| 42 | |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame^] | 43 | class MessageBase : public MessageHandler { |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 44 | public: |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 45 | MessageBase(); |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame^] | 46 | |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 47 | // return true if message has a handler |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 48 | virtual bool handler() = 0; |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 49 | |
| 50 | // waits for the handler to be processed |
| 51 | void wait() const { barrier.wait(); } |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 52 | |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 53 | protected: |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 54 | virtual ~MessageBase(); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 55 | |
| 56 | private: |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 57 | virtual void handleMessage(const Message& message); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 58 | |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 59 | mutable Barrier barrier; |
| 60 | }; |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 61 | |
Alex Sakhartchouk | 117698b | 2017-06-07 11:36:32 -0400 | [diff] [blame] | 62 | class LambdaMessage : public MessageBase { |
| 63 | public: |
| 64 | explicit LambdaMessage(std::function<void()> handler) |
| 65 | : MessageBase(), mHandler(std::move(handler)) {} |
| 66 | |
| 67 | bool handler() override { |
| 68 | mHandler(); |
| 69 | // This return value is no longer checked, so it's always safe to return true |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | private: |
| 74 | const std::function<void()> mHandler; |
| 75 | }; |
| 76 | |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 77 | // --------------------------------------------------------------------------- |
| 78 | |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 79 | class MessageQueue { |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 80 | class Handler : public MessageHandler { |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame^] | 81 | enum { eventMaskInvalidate = 0x1, eventMaskRefresh = 0x2, eventMaskTransaction = 0x4 }; |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 82 | MessageQueue& mQueue; |
| 83 | int32_t mEventMask; |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame^] | 84 | |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 85 | public: |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame^] | 86 | explicit Handler(MessageQueue& queue) : mQueue(queue), mEventMask(0) {} |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 87 | virtual void handleMessage(const Message& message); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 88 | void dispatchRefresh(); |
| 89 | void dispatchInvalidate(); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | friend class Handler; |
| 93 | |
| 94 | sp<SurfaceFlinger> mFlinger; |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 95 | sp<Looper> mLooper; |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 96 | sp<EventThread> mEventThread; |
| 97 | sp<IDisplayEventConnection> mEvents; |
Dan Stoza | 6b698e4 | 2017-04-03 13:09:08 -0700 | [diff] [blame] | 98 | gui::BitTube mEventTube; |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 99 | sp<Handler> mHandler; |
| 100 | |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 101 | static int cb_eventReceiver(int fd, int events, void* data); |
| 102 | int eventReceiver(int fd, int events); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 103 | |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 104 | public: |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 105 | enum { |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame^] | 106 | INVALIDATE = 0, |
| 107 | REFRESH = 1, |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 110 | MessageQueue(); |
| 111 | ~MessageQueue(); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 112 | void init(const sp<SurfaceFlinger>& flinger); |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 113 | void setEventThread(const sp<EventThread>& events); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 114 | |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 115 | void waitMessage(); |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame^] | 116 | status_t postMessage(const sp<MessageBase>& message, nsecs_t reltime = 0); |
Mathias Agopian | 9eb1f05 | 2013-04-10 16:27:17 -0700 | [diff] [blame] | 117 | |
| 118 | // sends INVALIDATE message at next VSYNC |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 119 | void invalidate(); |
Mathias Agopian | 9eb1f05 | 2013-04-10 16:27:17 -0700 | [diff] [blame] | 120 | // sends REFRESH message at next VSYNC |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 121 | void refresh(); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | // --------------------------------------------------------------------------- |
| 125 | |
| 126 | }; // namespace android |
| 127 | |
| 128 | #endif /* ANDROID_MESSAGE_QUEUE_H */ |