blob: b77e08e7cc7d337bca06fa8bd2cb646a16e2857b [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;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080036class SurfaceFlinger;
Mathias Agopian8aedd472012-01-24 16:39:14 -080037
Mathias Agopianf1d8e872009-04-20 19:39:12 -070038// ---------------------------------------------------------------------------
39
Mathias Agopianf61c57f2011-11-23 16:49:10 -080040class MessageBase : public MessageHandler
Mathias Agopianf1d8e872009-04-20 19:39:12 -070041{
42public:
Mathias Agopianf61c57f2011-11-23 16:49:10 -080043 MessageBase();
Mathias Agopianf1d8e872009-04-20 19:39:12 -070044
45 // return true if message has a handler
Mathias Agopianf61c57f2011-11-23 16:49:10 -080046 virtual bool handler() = 0;
Mathias Agopianbb641242010-05-18 17:06:55 -070047
48 // waits for the handler to be processed
49 void wait() const { barrier.wait(); }
Mathias Agopianbb641242010-05-18 17:06:55 -070050
Mathias Agopianf1d8e872009-04-20 19:39:12 -070051protected:
Mathias Agopianf61c57f2011-11-23 16:49:10 -080052 virtual ~MessageBase();
Mathias Agopianf1d8e872009-04-20 19:39:12 -070053
54private:
Mathias Agopianf61c57f2011-11-23 16:49:10 -080055 virtual void handleMessage(const Message& message);
Mathias Agopianf1d8e872009-04-20 19:39:12 -070056
Mathias Agopianf61c57f2011-11-23 16:49:10 -080057 mutable Barrier barrier;
58};
Mathias Agopianf1d8e872009-04-20 19:39:12 -070059
60// ---------------------------------------------------------------------------
61
Mathias Agopianf61c57f2011-11-23 16:49:10 -080062class MessageQueue {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080063 class Handler : public MessageHandler {
64 enum {
Mathias Agopian9eb1f052013-04-10 16:27:17 -070065 eventMaskInvalidate = 0x1,
66 eventMaskRefresh = 0x2,
67 eventMaskTransaction = 0x4
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080068 };
69 MessageQueue& mQueue;
70 int32_t mEventMask;
71 public:
72 Handler(MessageQueue& queue) : mQueue(queue), mEventMask(0) { }
73 virtual void handleMessage(const Message& message);
Mathias Agopian4fec8732012-06-29 14:12:52 -070074 void dispatchRefresh();
75 void dispatchInvalidate();
Mathias Agopian9eb1f052013-04-10 16:27:17 -070076 void dispatchTransaction();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080077 };
78
79 friend class Handler;
80
81 sp<SurfaceFlinger> mFlinger;
Mathias Agopianf61c57f2011-11-23 16:49:10 -080082 sp<Looper> mLooper;
Mathias Agopian8aedd472012-01-24 16:39:14 -080083 sp<EventThread> mEventThread;
84 sp<IDisplayEventConnection> mEvents;
85 sp<BitTube> mEventTube;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080086 sp<Handler> mHandler;
87
Mathias Agopian8aedd472012-01-24 16:39:14 -080088
89 static int cb_eventReceiver(int fd, int events, void* data);
90 int eventReceiver(int fd, int events);
Mathias Agopianf1d8e872009-04-20 19:39:12 -070091
Mathias Agopianf61c57f2011-11-23 16:49:10 -080092public:
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080093 enum {
Mathias Agopian9eb1f052013-04-10 16:27:17 -070094 INVALIDATE = 0,
95 REFRESH = 1,
96 TRANSACTION = 2
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080097 };
98
Mathias Agopianf1d8e872009-04-20 19:39:12 -070099 MessageQueue();
100 ~MessageQueue();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800101 void init(const sp<SurfaceFlinger>& flinger);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800102 void setEventThread(const sp<EventThread>& events);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700103
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800104 void waitMessage();
105 status_t postMessage(const sp<MessageBase>& message, nsecs_t reltime=0);
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700106
107 // sends INVALIDATE message at next VSYNC
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800108 void invalidate();
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700109 // sends REFRESH message at next VSYNC
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800110 void refresh();
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700111 // sends TRANSACTION message immediately
112 void invalidateTransactionNow();
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700113};
114
115// ---------------------------------------------------------------------------
116
117}; // namespace android
118
119#endif /* ANDROID_MESSAGE_QUEUE_H */