MessageQueue.h revision 8aedd4737d6ce8548d2fd5def65b1e1737283821
1f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian/*
2f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian * Copyright (C) 2009 The Android Open Source Project
3f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian *
4f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
5f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian * you may not use this file except in compliance with the License.
6f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian * You may obtain a copy of the License at
7f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian *
8f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
9f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian *
10f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian * Unless required by applicable law or agreed to in writing, software
11f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
12f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian * See the License for the specific language governing permissions and
14f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian * limitations under the License.
15f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian */
16f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian
17f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian#ifndef ANDROID_MESSAGE_QUEUE_H
18f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian#define ANDROID_MESSAGE_QUEUE_H
19f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian
20f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian#include <stdint.h>
21f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian#include <errno.h>
22f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian#include <sys/types.h>
23f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian
24f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian#include <utils/threads.h>
25f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian#include <utils/Timers.h>
26f61c57fe2e955e1c195bb0ca2dd7bcdaa922d5a9Mathias Agopian#include <utils/Looper.h>
27f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian
288aedd4737d6ce8548d2fd5def65b1e1737283821Mathias Agopian#include <gui/DisplayEventReceiver.h>
298aedd4737d6ce8548d2fd5def65b1e1737283821Mathias Agopian
30bb641244d7d73312dc65b8e338df18b22e335107Mathias Agopian#include "Barrier.h"
31f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian
32f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopiannamespace android {
33f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian
348aedd4737d6ce8548d2fd5def65b1e1737283821Mathias Agopianclass IDisplayEventConnection;
358aedd4737d6ce8548d2fd5def65b1e1737283821Mathias Agopianclass EventThread;
368aedd4737d6ce8548d2fd5def65b1e1737283821Mathias Agopian
37f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian// ---------------------------------------------------------------------------
38f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian
39f61c57fe2e955e1c195bb0ca2dd7bcdaa922d5a9Mathias Agopianclass MessageBase : public MessageHandler
40f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian{
41f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopianpublic:
42f61c57fe2e955e1c195bb0ca2dd7bcdaa922d5a9Mathias Agopian    MessageBase();
43f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian
44f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian    // return true if message has a handler
45f61c57fe2e955e1c195bb0ca2dd7bcdaa922d5a9Mathias Agopian    virtual bool handler() = 0;
46bb641244d7d73312dc65b8e338df18b22e335107Mathias Agopian
47bb641244d7d73312dc65b8e338df18b22e335107Mathias Agopian    // waits for the handler to be processed
48bb641244d7d73312dc65b8e338df18b22e335107Mathias Agopian    void wait() const { barrier.wait(); }
49bb641244d7d73312dc65b8e338df18b22e335107Mathias Agopian
50f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopianprotected:
51f61c57fe2e955e1c195bb0ca2dd7bcdaa922d5a9Mathias Agopian    virtual ~MessageBase();
52f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian
53f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopianprivate:
54f61c57fe2e955e1c195bb0ca2dd7bcdaa922d5a9Mathias Agopian    virtual void handleMessage(const Message& message);
55f61c57fe2e955e1c195bb0ca2dd7bcdaa922d5a9Mathias Agopian
56bb641244d7d73312dc65b8e338df18b22e335107Mathias Agopian    mutable Barrier barrier;
57f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian};
58f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian
59f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian// ---------------------------------------------------------------------------
60f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian
61f61c57fe2e955e1c195bb0ca2dd7bcdaa922d5a9Mathias Agopianclass MessageQueue {
62f61c57fe2e955e1c195bb0ca2dd7bcdaa922d5a9Mathias Agopian    sp<Looper> mLooper;
638aedd4737d6ce8548d2fd5def65b1e1737283821Mathias Agopian    sp<EventThread> mEventThread;
648aedd4737d6ce8548d2fd5def65b1e1737283821Mathias Agopian    sp<IDisplayEventConnection> mEvents;
658aedd4737d6ce8548d2fd5def65b1e1737283821Mathias Agopian    sp<BitTube> mEventTube;
668aedd4737d6ce8548d2fd5def65b1e1737283821Mathias Agopian    int32_t mWorkPending;
678aedd4737d6ce8548d2fd5def65b1e1737283821Mathias Agopian
688aedd4737d6ce8548d2fd5def65b1e1737283821Mathias Agopian    static int cb_eventReceiver(int fd, int events, void* data);
698aedd4737d6ce8548d2fd5def65b1e1737283821Mathias Agopian    int eventReceiver(int fd, int events);
708aedd4737d6ce8548d2fd5def65b1e1737283821Mathias Agopian    ssize_t getEvents(DisplayEventReceiver::Event* events, size_t count);
718aedd4737d6ce8548d2fd5def65b1e1737283821Mathias Agopian    void scheduleWorkASAP();
72f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian
73f61c57fe2e955e1c195bb0ca2dd7bcdaa922d5a9Mathias Agopianpublic:
74f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian    MessageQueue();
75f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian    ~MessageQueue();
768aedd4737d6ce8548d2fd5def65b1e1737283821Mathias Agopian    void setEventThread(const sp<EventThread>& events);
77f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian
78f61c57fe2e955e1c195bb0ca2dd7bcdaa922d5a9Mathias Agopian    void waitMessage();
79f61c57fe2e955e1c195bb0ca2dd7bcdaa922d5a9Mathias Agopian    status_t postMessage(const sp<MessageBase>& message, nsecs_t reltime=0);
80f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian    status_t invalidate();
81f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian};
82f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian
83f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian// ---------------------------------------------------------------------------
84f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian
85f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian}; // namespace android
86f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian
87f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian#endif /* ANDROID_MESSAGE_QUEUE_H */
88