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; 3699ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopianclass SurfaceFlinger; 378aedd4737d6ce8548d2fd5def65b1e1737283821Mathias Agopian 38f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian// --------------------------------------------------------------------------- 39f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian 40f61c57fe2e955e1c195bb0ca2dd7bcdaa922d5a9Mathias Agopianclass MessageBase : public MessageHandler 41f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian{ 42f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopianpublic: 43f61c57fe2e955e1c195bb0ca2dd7bcdaa922d5a9Mathias Agopian MessageBase(); 44f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian 45f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian // return true if message has a handler 46f61c57fe2e955e1c195bb0ca2dd7bcdaa922d5a9Mathias Agopian virtual bool handler() = 0; 47bb641244d7d73312dc65b8e338df18b22e335107Mathias Agopian 48bb641244d7d73312dc65b8e338df18b22e335107Mathias Agopian // waits for the handler to be processed 49bb641244d7d73312dc65b8e338df18b22e335107Mathias Agopian void wait() const { barrier.wait(); } 50bb641244d7d73312dc65b8e338df18b22e335107Mathias Agopian 51f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopianprotected: 52f61c57fe2e955e1c195bb0ca2dd7bcdaa922d5a9Mathias Agopian virtual ~MessageBase(); 53f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian 54f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopianprivate: 55f61c57fe2e955e1c195bb0ca2dd7bcdaa922d5a9Mathias Agopian virtual void handleMessage(const Message& message); 56f61c57fe2e955e1c195bb0ca2dd7bcdaa922d5a9Mathias Agopian 57bb641244d7d73312dc65b8e338df18b22e335107Mathias Agopian mutable Barrier barrier; 58f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian}; 59f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian 60f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian// --------------------------------------------------------------------------- 61f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian 62f61c57fe2e955e1c195bb0ca2dd7bcdaa922d5a9Mathias Agopianclass MessageQueue { 6399ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian class Handler : public MessageHandler { 6499ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian enum { 659eb1f0558b5fc78920602afe7bcfa3115bb1f6beMathias Agopian eventMaskInvalidate = 0x1, 669eb1f0558b5fc78920602afe7bcfa3115bb1f6beMathias Agopian eventMaskRefresh = 0x2, 679eb1f0558b5fc78920602afe7bcfa3115bb1f6beMathias Agopian eventMaskTransaction = 0x4 6899ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian }; 6999ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian MessageQueue& mQueue; 7099ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian int32_t mEventMask; 7199ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian public: 7299ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian Handler(MessageQueue& queue) : mQueue(queue), mEventMask(0) { } 7399ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian virtual void handleMessage(const Message& message); 744fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian void dispatchRefresh(); 754fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian void dispatchInvalidate(); 769eb1f0558b5fc78920602afe7bcfa3115bb1f6beMathias Agopian void dispatchTransaction(); 7799ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian }; 7899ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian 7999ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian friend class Handler; 8099ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian 8199ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian sp<SurfaceFlinger> mFlinger; 82f61c57fe2e955e1c195bb0ca2dd7bcdaa922d5a9Mathias Agopian sp<Looper> mLooper; 838aedd4737d6ce8548d2fd5def65b1e1737283821Mathias Agopian sp<EventThread> mEventThread; 848aedd4737d6ce8548d2fd5def65b1e1737283821Mathias Agopian sp<IDisplayEventConnection> mEvents; 858aedd4737d6ce8548d2fd5def65b1e1737283821Mathias Agopian sp<BitTube> mEventTube; 8699ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian sp<Handler> mHandler; 8799ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian 888aedd4737d6ce8548d2fd5def65b1e1737283821Mathias Agopian 898aedd4737d6ce8548d2fd5def65b1e1737283821Mathias Agopian static int cb_eventReceiver(int fd, int events, void* data); 908aedd4737d6ce8548d2fd5def65b1e1737283821Mathias Agopian int eventReceiver(int fd, int events); 91f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian 92f61c57fe2e955e1c195bb0ca2dd7bcdaa922d5a9Mathias Agopianpublic: 9399ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian enum { 949eb1f0558b5fc78920602afe7bcfa3115bb1f6beMathias Agopian INVALIDATE = 0, 959eb1f0558b5fc78920602afe7bcfa3115bb1f6beMathias Agopian REFRESH = 1, 969eb1f0558b5fc78920602afe7bcfa3115bb1f6beMathias Agopian TRANSACTION = 2 9799ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian }; 9899ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian 99f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian MessageQueue(); 100f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian ~MessageQueue(); 10199ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian void init(const sp<SurfaceFlinger>& flinger); 1028aedd4737d6ce8548d2fd5def65b1e1737283821Mathias Agopian void setEventThread(const sp<EventThread>& events); 103f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian 104f61c57fe2e955e1c195bb0ca2dd7bcdaa922d5a9Mathias Agopian void waitMessage(); 105f61c57fe2e955e1c195bb0ca2dd7bcdaa922d5a9Mathias Agopian status_t postMessage(const sp<MessageBase>& message, nsecs_t reltime=0); 1069eb1f0558b5fc78920602afe7bcfa3115bb1f6beMathias Agopian 1079eb1f0558b5fc78920602afe7bcfa3115bb1f6beMathias Agopian // sends INVALIDATE message at next VSYNC 10899ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian void invalidate(); 1099eb1f0558b5fc78920602afe7bcfa3115bb1f6beMathias Agopian // sends REFRESH message at next VSYNC 11099ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian void refresh(); 1119eb1f0558b5fc78920602afe7bcfa3115bb1f6beMathias Agopian // sends TRANSACTION message immediately 1129eb1f0558b5fc78920602afe7bcfa3115bb1f6beMathias Agopian void invalidateTransactionNow(); 113f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian}; 114f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian 115f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian// --------------------------------------------------------------------------- 116f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian 117f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian}; // namespace android 118f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian 119f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian#endif /* ANDROID_MESSAGE_QUEUE_H */ 120