MessageQueue.h revision 8aedd4737d6ce8548d2fd5def65b1e1737283821
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 <gui/DisplayEventReceiver.h> 29 30#include "Barrier.h" 31 32namespace android { 33 34class IDisplayEventConnection; 35class EventThread; 36 37// --------------------------------------------------------------------------- 38 39class MessageBase : public MessageHandler 40{ 41public: 42 MessageBase(); 43 44 // return true if message has a handler 45 virtual bool handler() = 0; 46 47 // waits for the handler to be processed 48 void wait() const { barrier.wait(); } 49 50protected: 51 virtual ~MessageBase(); 52 53private: 54 virtual void handleMessage(const Message& message); 55 56 mutable Barrier barrier; 57}; 58 59// --------------------------------------------------------------------------- 60 61class MessageQueue { 62 sp<Looper> mLooper; 63 sp<EventThread> mEventThread; 64 sp<IDisplayEventConnection> mEvents; 65 sp<BitTube> mEventTube; 66 int32_t mWorkPending; 67 68 static int cb_eventReceiver(int fd, int events, void* data); 69 int eventReceiver(int fd, int events); 70 ssize_t getEvents(DisplayEventReceiver::Event* events, size_t count); 71 void scheduleWorkASAP(); 72 73public: 74 MessageQueue(); 75 ~MessageQueue(); 76 void setEventThread(const sp<EventThread>& events); 77 78 void waitMessage(); 79 status_t postMessage(const sp<MessageBase>& message, nsecs_t reltime=0); 80 status_t invalidate(); 81}; 82 83// --------------------------------------------------------------------------- 84 85}; // namespace android 86 87#endif /* ANDROID_MESSAGE_QUEUE_H */ 88