blob: 14f50bbdce4afc8472eb66322c165961edf9e711 [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
Alex Sakhartchouk117698b2017-06-07 11:36:32 -040033#include <functional>
34
Mathias Agopianf1d8e872009-04-20 19:39:12 -070035namespace android {
36
Mathias Agopian8aedd472012-01-24 16:39:14 -080037class IDisplayEventConnection;
38class EventThread;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080039class SurfaceFlinger;
Mathias Agopian8aedd472012-01-24 16:39:14 -080040
Mathias Agopianf1d8e872009-04-20 19:39:12 -070041// ---------------------------------------------------------------------------
42
Mathias Agopianf61c57f2011-11-23 16:49:10 -080043class MessageBase : public MessageHandler
Mathias Agopianf1d8e872009-04-20 19:39:12 -070044{
45public:
Mathias Agopianf61c57f2011-11-23 16:49:10 -080046 MessageBase();
Mathias Agopianf1d8e872009-04-20 19:39:12 -070047
48 // return true if message has a handler
Mathias Agopianf61c57f2011-11-23 16:49:10 -080049 virtual bool handler() = 0;
Mathias Agopianbb641242010-05-18 17:06:55 -070050
51 // waits for the handler to be processed
52 void wait() const { barrier.wait(); }
Mathias Agopianbb641242010-05-18 17:06:55 -070053
Mathias Agopianf1d8e872009-04-20 19:39:12 -070054protected:
Mathias Agopianf61c57f2011-11-23 16:49:10 -080055 virtual ~MessageBase();
Mathias Agopianf1d8e872009-04-20 19:39:12 -070056
57private:
Mathias Agopianf61c57f2011-11-23 16:49:10 -080058 virtual void handleMessage(const Message& message);
Mathias Agopianf1d8e872009-04-20 19:39:12 -070059
Mathias Agopianf61c57f2011-11-23 16:49:10 -080060 mutable Barrier barrier;
61};
Mathias Agopianf1d8e872009-04-20 19:39:12 -070062
Alex Sakhartchouk117698b2017-06-07 11:36:32 -040063class LambdaMessage : public MessageBase {
64public:
65 explicit LambdaMessage(std::function<void()> handler)
66 : MessageBase(), mHandler(std::move(handler)) {}
67
68 bool handler() override {
69 mHandler();
70 // This return value is no longer checked, so it's always safe to return true
71 return true;
72 }
73
74private:
75 const std::function<void()> mHandler;
76};
77
Mathias Agopianf1d8e872009-04-20 19:39:12 -070078// ---------------------------------------------------------------------------
79
Mathias Agopianf61c57f2011-11-23 16:49:10 -080080class MessageQueue {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080081 class Handler : public MessageHandler {
82 enum {
Mathias Agopian9eb1f052013-04-10 16:27:17 -070083 eventMaskInvalidate = 0x1,
84 eventMaskRefresh = 0x2,
85 eventMaskTransaction = 0x4
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080086 };
87 MessageQueue& mQueue;
88 int32_t mEventMask;
89 public:
Chih-Hung Hsieh342b7602016-09-01 11:34:16 -070090 explicit Handler(MessageQueue& queue) : mQueue(queue), mEventMask(0) { }
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080091 virtual void handleMessage(const Message& message);
Mathias Agopian4fec8732012-06-29 14:12:52 -070092 void dispatchRefresh();
93 void dispatchInvalidate();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080094 };
95
96 friend class Handler;
97
98 sp<SurfaceFlinger> mFlinger;
Mathias Agopianf61c57f2011-11-23 16:49:10 -080099 sp<Looper> mLooper;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800100 sp<EventThread> mEventThread;
101 sp<IDisplayEventConnection> mEvents;
Dan Stoza6b698e42017-04-03 13:09:08 -0700102 gui::BitTube mEventTube;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800103 sp<Handler> mHandler;
104
Mathias Agopian8aedd472012-01-24 16:39:14 -0800105
106 static int cb_eventReceiver(int fd, int events, void* data);
107 int eventReceiver(int fd, int events);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700108
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800109public:
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800110 enum {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700111 INVALIDATE = 0,
112 REFRESH = 1,
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800113 };
114
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700115 MessageQueue();
116 ~MessageQueue();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800117 void init(const sp<SurfaceFlinger>& flinger);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800118 void setEventThread(const sp<EventThread>& events);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700119
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800120 void waitMessage();
121 status_t postMessage(const sp<MessageBase>& message, nsecs_t reltime=0);
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700122
123 // sends INVALIDATE message at next VSYNC
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800124 void invalidate();
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700125 // sends REFRESH message at next VSYNC
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800126 void refresh();
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700127};
128
129// ---------------------------------------------------------------------------
130
131}; // namespace android
132
133#endif /* ANDROID_MESSAGE_QUEUE_H */