blob: 85a33c868c53d5ac858945b59988ebc48991e6b7 [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
Dan Stoza6b698e42017-04-03 13:09:08 -070028#include <private/gui/BitTube.h>
Mathias Agopian8aedd472012-01-24 16:39:14 -080029#include <gui/DisplayEventReceiver.h>
30
Mathias Agopianbb641242010-05-18 17:06:55 -070031#include "Barrier.h"
Mathias Agopianf1d8e872009-04-20 19:39:12 -070032
33namespace android {
34
Mathias Agopian8aedd472012-01-24 16:39:14 -080035class IDisplayEventConnection;
36class EventThread;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080037class SurfaceFlinger;
Mathias Agopian8aedd472012-01-24 16:39:14 -080038
Mathias Agopianf1d8e872009-04-20 19:39:12 -070039// ---------------------------------------------------------------------------
40
Mathias Agopianf61c57f2011-11-23 16:49:10 -080041class MessageBase : public MessageHandler
Mathias Agopianf1d8e872009-04-20 19:39:12 -070042{
43public:
Mathias Agopianf61c57f2011-11-23 16:49:10 -080044 MessageBase();
Mathias Agopianf1d8e872009-04-20 19:39:12 -070045
46 // return true if message has a handler
Mathias Agopianf61c57f2011-11-23 16:49:10 -080047 virtual bool handler() = 0;
Mathias Agopianbb641242010-05-18 17:06:55 -070048
49 // waits for the handler to be processed
50 void wait() const { barrier.wait(); }
Mathias Agopianbb641242010-05-18 17:06:55 -070051
Mathias Agopianf1d8e872009-04-20 19:39:12 -070052protected:
Mathias Agopianf61c57f2011-11-23 16:49:10 -080053 virtual ~MessageBase();
Mathias Agopianf1d8e872009-04-20 19:39:12 -070054
55private:
Mathias Agopianf61c57f2011-11-23 16:49:10 -080056 virtual void handleMessage(const Message& message);
Mathias Agopianf1d8e872009-04-20 19:39:12 -070057
Mathias Agopianf61c57f2011-11-23 16:49:10 -080058 mutable Barrier barrier;
59};
Mathias Agopianf1d8e872009-04-20 19:39:12 -070060
61// ---------------------------------------------------------------------------
62
Mathias Agopianf61c57f2011-11-23 16:49:10 -080063class MessageQueue {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080064 class Handler : public MessageHandler {
65 enum {
Mathias Agopian9eb1f052013-04-10 16:27:17 -070066 eventMaskInvalidate = 0x1,
67 eventMaskRefresh = 0x2,
68 eventMaskTransaction = 0x4
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080069 };
70 MessageQueue& mQueue;
71 int32_t mEventMask;
72 public:
Chih-Hung Hsieh342b7602016-09-01 11:34:16 -070073 explicit Handler(MessageQueue& queue) : mQueue(queue), mEventMask(0) { }
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080074 virtual void handleMessage(const Message& message);
Mathias Agopian4fec8732012-06-29 14:12:52 -070075 void dispatchRefresh();
76 void dispatchInvalidate();
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;
Dan Stoza6b698e42017-04-03 13:09:08 -070085 gui::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,
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080096 };
97
Mathias Agopianf1d8e872009-04-20 19:39:12 -070098 MessageQueue();
99 ~MessageQueue();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800100 void init(const sp<SurfaceFlinger>& flinger);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800101 void setEventThread(const sp<EventThread>& events);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700102
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800103 void waitMessage();
104 status_t postMessage(const sp<MessageBase>& message, nsecs_t reltime=0);
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700105
106 // sends INVALIDATE message at next VSYNC
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800107 void invalidate();
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700108 // sends REFRESH message at next VSYNC
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800109 void refresh();
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700110};
111
112// ---------------------------------------------------------------------------
113
114}; // namespace android
115
116#endif /* ANDROID_MESSAGE_QUEUE_H */