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 | |
| 20 | #include <stdint.h> |
| 21 | #include <errno.h> |
| 22 | #include <sys/types.h> |
| 23 | |
| 24 | #include <utils/threads.h> |
| 25 | #include <utils/Timers.h> |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 26 | #include <utils/Looper.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> |
| 29 | |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 30 | #include "Barrier.h" |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 34 | class IDisplayEventConnection; |
| 35 | class EventThread; |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 36 | class SurfaceFlinger; |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 37 | |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 38 | // --------------------------------------------------------------------------- |
| 39 | |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 40 | class MessageBase : public MessageHandler |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 41 | { |
| 42 | public: |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 43 | MessageBase(); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 44 | |
| 45 | // return true if message has a handler |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 46 | virtual bool handler() = 0; |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 47 | |
| 48 | // waits for the handler to be processed |
| 49 | void wait() const { barrier.wait(); } |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 50 | |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 51 | protected: |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 52 | virtual ~MessageBase(); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 53 | |
| 54 | private: |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 55 | virtual void handleMessage(const Message& message); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 56 | |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 57 | mutable Barrier barrier; |
| 58 | }; |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 59 | |
| 60 | // --------------------------------------------------------------------------- |
| 61 | |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 62 | class MessageQueue { |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 63 | class Handler : public MessageHandler { |
| 64 | enum { |
Mathias Agopian | 9eb1f05 | 2013-04-10 16:27:17 -0700 | [diff] [blame] | 65 | eventMaskInvalidate = 0x1, |
| 66 | eventMaskRefresh = 0x2, |
| 67 | eventMaskTransaction = 0x4 |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 68 | }; |
| 69 | MessageQueue& mQueue; |
| 70 | int32_t mEventMask; |
| 71 | public: |
| 72 | Handler(MessageQueue& queue) : mQueue(queue), mEventMask(0) { } |
| 73 | virtual void handleMessage(const Message& message); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 74 | void dispatchRefresh(); |
| 75 | void dispatchInvalidate(); |
Mathias Agopian | 9eb1f05 | 2013-04-10 16:27:17 -0700 | [diff] [blame] | 76 | void dispatchTransaction(); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | friend class Handler; |
| 80 | |
| 81 | sp<SurfaceFlinger> mFlinger; |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 82 | sp<Looper> mLooper; |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 83 | sp<EventThread> mEventThread; |
| 84 | sp<IDisplayEventConnection> mEvents; |
| 85 | sp<BitTube> mEventTube; |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 86 | sp<Handler> mHandler; |
| 87 | |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 88 | |
| 89 | static int cb_eventReceiver(int fd, int events, void* data); |
| 90 | int eventReceiver(int fd, int events); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 91 | |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame] | 92 | public: |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 93 | enum { |
Mathias Agopian | 9eb1f05 | 2013-04-10 16:27:17 -0700 | [diff] [blame] | 94 | INVALIDATE = 0, |
| 95 | REFRESH = 1, |
| 96 | TRANSACTION = 2 |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 97 | }; |
| 98 | |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 99 | MessageQueue(); |
| 100 | ~MessageQueue(); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 101 | void init(const sp<SurfaceFlinger>& flinger); |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 102 | void setEventThread(const sp<EventThread>& 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 | void waitMessage(); |
| 105 | status_t postMessage(const sp<MessageBase>& message, nsecs_t reltime=0); |
Mathias Agopian | 9eb1f05 | 2013-04-10 16:27:17 -0700 | [diff] [blame] | 106 | |
| 107 | // sends INVALIDATE message at next VSYNC |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 108 | void invalidate(); |
Mathias Agopian | 9eb1f05 | 2013-04-10 16:27:17 -0700 | [diff] [blame] | 109 | // sends REFRESH message at next VSYNC |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 110 | void refresh(); |
Mathias Agopian | 9eb1f05 | 2013-04-10 16:27:17 -0700 | [diff] [blame] | 111 | // sends TRANSACTION message immediately |
| 112 | void invalidateTransactionNow(); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | // --------------------------------------------------------------------------- |
| 116 | |
| 117 | }; // namespace android |
| 118 | |
| 119 | #endif /* ANDROID_MESSAGE_QUEUE_H */ |