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> 26 #include <utils/Looper.h> 27 28 #include <private/gui/BitTube.h> 29 #include <gui/DisplayEventReceiver.h> 30 31 #include "Barrier.h" 32 33 #include <functional> 34 35 namespace android { 36 37 class IDisplayEventConnection; 38 class EventThread; 39 class SurfaceFlinger; 40 41 // --------------------------------------------------------------------------- 42 43 class MessageBase : public MessageHandler 44 { 45 public: 46 MessageBase(); 47 48 // return true if message has a handler 49 virtual bool handler() = 0; 50 51 // waits for the handler to be processed wait()52 void wait() const { barrier.wait(); } 53 54 protected: 55 virtual ~MessageBase(); 56 57 private: 58 virtual void handleMessage(const Message& message); 59 60 mutable Barrier barrier; 61 }; 62 63 class LambdaMessage : public MessageBase { 64 public: LambdaMessage(std::function<void ()> handler)65 explicit LambdaMessage(std::function<void()> handler) 66 : MessageBase(), mHandler(std::move(handler)) {} 67 handler()68 bool handler() override { 69 mHandler(); 70 // This return value is no longer checked, so it's always safe to return true 71 return true; 72 } 73 74 private: 75 const std::function<void()> mHandler; 76 }; 77 78 // --------------------------------------------------------------------------- 79 80 class MessageQueue { 81 class Handler : public MessageHandler { 82 enum { 83 eventMaskInvalidate = 0x1, 84 eventMaskRefresh = 0x2, 85 eventMaskTransaction = 0x4 86 }; 87 MessageQueue& mQueue; 88 int32_t mEventMask; 89 public: Handler(MessageQueue & queue)90 explicit Handler(MessageQueue& queue) : mQueue(queue), mEventMask(0) { } 91 virtual void handleMessage(const Message& message); 92 void dispatchRefresh(); 93 void dispatchInvalidate(); 94 }; 95 96 friend class Handler; 97 98 sp<SurfaceFlinger> mFlinger; 99 sp<Looper> mLooper; 100 sp<EventThread> mEventThread; 101 sp<IDisplayEventConnection> mEvents; 102 gui::BitTube mEventTube; 103 sp<Handler> mHandler; 104 105 106 static int cb_eventReceiver(int fd, int events, void* data); 107 int eventReceiver(int fd, int events); 108 109 public: 110 enum { 111 INVALIDATE = 0, 112 REFRESH = 1, 113 }; 114 115 MessageQueue(); 116 ~MessageQueue(); 117 void init(const sp<SurfaceFlinger>& flinger); 118 void setEventThread(const sp<EventThread>& events); 119 120 void waitMessage(); 121 status_t postMessage(const sp<MessageBase>& message, nsecs_t reltime=0); 122 123 // sends INVALIDATE message at next VSYNC 124 void invalidate(); 125 // sends REFRESH message at next VSYNC 126 void refresh(); 127 }; 128 129 // --------------------------------------------------------------------------- 130 131 }; // namespace android 132 133 #endif /* ANDROID_MESSAGE_QUEUE_H */ 134