blob: 5ea197dc70c951a46dc0485b104e65be52ddc955 [file] [log] [blame]
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001/*
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 Agopianf61c57f2011-11-23 16:49:10 -080026#include <utils/Looper.h>
Mathias Agopianf1d8e872009-04-20 19:39:12 -070027
Mathias Agopian8aedd472012-01-24 16:39:14 -080028#include <gui/DisplayEventReceiver.h>
29
Mathias Agopianbb641242010-05-18 17:06:55 -070030#include "Barrier.h"
Mathias Agopianf1d8e872009-04-20 19:39:12 -070031
32namespace android {
33
Mathias Agopian8aedd472012-01-24 16:39:14 -080034class IDisplayEventConnection;
35class EventThread;
36
Mathias Agopianf1d8e872009-04-20 19:39:12 -070037// ---------------------------------------------------------------------------
38
Mathias Agopianf61c57f2011-11-23 16:49:10 -080039class MessageBase : public MessageHandler
Mathias Agopianf1d8e872009-04-20 19:39:12 -070040{
41public:
Mathias Agopianf61c57f2011-11-23 16:49:10 -080042 MessageBase();
Mathias Agopianf1d8e872009-04-20 19:39:12 -070043
44 // return true if message has a handler
Mathias Agopianf61c57f2011-11-23 16:49:10 -080045 virtual bool handler() = 0;
Mathias Agopianbb641242010-05-18 17:06:55 -070046
47 // waits for the handler to be processed
48 void wait() const { barrier.wait(); }
Mathias Agopianbb641242010-05-18 17:06:55 -070049
Mathias Agopianf1d8e872009-04-20 19:39:12 -070050protected:
Mathias Agopianf61c57f2011-11-23 16:49:10 -080051 virtual ~MessageBase();
Mathias Agopianf1d8e872009-04-20 19:39:12 -070052
53private:
Mathias Agopianf61c57f2011-11-23 16:49:10 -080054 virtual void handleMessage(const Message& message);
Mathias Agopianf1d8e872009-04-20 19:39:12 -070055
Mathias Agopianf61c57f2011-11-23 16:49:10 -080056 mutable Barrier barrier;
57};
Mathias Agopianf1d8e872009-04-20 19:39:12 -070058
59// ---------------------------------------------------------------------------
60
Mathias Agopianf61c57f2011-11-23 16:49:10 -080061class MessageQueue {
62 sp<Looper> mLooper;
Mathias Agopian8aedd472012-01-24 16:39:14 -080063 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();
Mathias Agopianf1d8e872009-04-20 19:39:12 -070072
Mathias Agopianf61c57f2011-11-23 16:49:10 -080073public:
Mathias Agopianf1d8e872009-04-20 19:39:12 -070074 MessageQueue();
75 ~MessageQueue();
Mathias Agopian8aedd472012-01-24 16:39:14 -080076 void setEventThread(const sp<EventThread>& events);
Mathias Agopianf1d8e872009-04-20 19:39:12 -070077
Mathias Agopianf61c57f2011-11-23 16:49:10 -080078 void waitMessage();
79 status_t postMessage(const sp<MessageBase>& message, nsecs_t reltime=0);
Mathias Agopianf1d8e872009-04-20 19:39:12 -070080 status_t invalidate();
Mathias Agopianf1d8e872009-04-20 19:39:12 -070081};
82
83// ---------------------------------------------------------------------------
84
85}; // namespace android
86
87#endif /* ANDROID_MESSAGE_QUEUE_H */