blob: fe1bf081b82f2e5ba5061c717e4bb527955e8f86 [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
Mathias Agopianf1d8e872009-04-20 19:39:12 -070020#include <errno.h>
Lloyd Pique78ce4182018-01-31 16:39:51 -080021#include <stdint.h>
Mathias Agopianf1d8e872009-04-20 19:39:12 -070022#include <sys/types.h>
23
Mathias Agopianf61c57f2011-11-23 16:49:10 -080024#include <utils/Looper.h>
Lloyd Pique78ce4182018-01-31 16:39:51 -080025#include <utils/Timers.h>
26#include <utils/threads.h>
Mathias Agopianf1d8e872009-04-20 19:39:12 -070027
Mathias Agopian8aedd472012-01-24 16:39:14 -080028#include <gui/DisplayEventReceiver.h>
Lloyd Pique78ce4182018-01-31 16:39:51 -080029#include <private/gui/BitTube.h>
Mathias Agopian8aedd472012-01-24 16:39:14 -080030
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
Lloyd Pique78ce4182018-01-31 16:39:51 -080043class MessageBase : public MessageHandler {
Mathias Agopianf1d8e872009-04-20 19:39:12 -070044public:
Mathias Agopianf61c57f2011-11-23 16:49:10 -080045 MessageBase();
Lloyd Pique78ce4182018-01-31 16:39:51 -080046
Mathias Agopianf1d8e872009-04-20 19:39:12 -070047 // return true if message has a handler
Mathias Agopianf61c57f2011-11-23 16:49:10 -080048 virtual bool handler() = 0;
Mathias Agopianbb641242010-05-18 17:06:55 -070049
50 // waits for the handler to be processed
51 void wait() const { barrier.wait(); }
Mathias Agopianbb641242010-05-18 17:06:55 -070052
Mathias Agopianf1d8e872009-04-20 19:39:12 -070053protected:
Mathias Agopianf61c57f2011-11-23 16:49:10 -080054 virtual ~MessageBase();
Mathias Agopianf1d8e872009-04-20 19:39:12 -070055
56private:
Mathias Agopianf61c57f2011-11-23 16:49:10 -080057 virtual void handleMessage(const Message& message);
Mathias Agopianf1d8e872009-04-20 19:39:12 -070058
Mathias Agopianf61c57f2011-11-23 16:49:10 -080059 mutable Barrier barrier;
60};
Mathias Agopianf1d8e872009-04-20 19:39:12 -070061
Alex Sakhartchouk117698b2017-06-07 11:36:32 -040062class LambdaMessage : public MessageBase {
63public:
64 explicit LambdaMessage(std::function<void()> handler)
65 : MessageBase(), mHandler(std::move(handler)) {}
66
67 bool handler() override {
68 mHandler();
69 // This return value is no longer checked, so it's always safe to return true
70 return true;
71 }
72
73private:
74 const std::function<void()> mHandler;
75};
76
Mathias Agopianf1d8e872009-04-20 19:39:12 -070077// ---------------------------------------------------------------------------
78
Mathias Agopianf61c57f2011-11-23 16:49:10 -080079class MessageQueue {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080080 class Handler : public MessageHandler {
Lloyd Pique78ce4182018-01-31 16:39:51 -080081 enum { eventMaskInvalidate = 0x1, eventMaskRefresh = 0x2, eventMaskTransaction = 0x4 };
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080082 MessageQueue& mQueue;
83 int32_t mEventMask;
Lloyd Pique78ce4182018-01-31 16:39:51 -080084
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080085 public:
Lloyd Pique78ce4182018-01-31 16:39:51 -080086 explicit Handler(MessageQueue& queue) : mQueue(queue), mEventMask(0) {}
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080087 virtual void handleMessage(const Message& message);
Mathias Agopian4fec8732012-06-29 14:12:52 -070088 void dispatchRefresh();
89 void dispatchInvalidate();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080090 };
91
92 friend class Handler;
93
94 sp<SurfaceFlinger> mFlinger;
Mathias Agopianf61c57f2011-11-23 16:49:10 -080095 sp<Looper> mLooper;
Mathias Agopian8aedd472012-01-24 16:39:14 -080096 sp<EventThread> mEventThread;
97 sp<IDisplayEventConnection> mEvents;
Dan Stoza6b698e42017-04-03 13:09:08 -070098 gui::BitTube mEventTube;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080099 sp<Handler> mHandler;
100
Mathias Agopian8aedd472012-01-24 16:39:14 -0800101 static int cb_eventReceiver(int fd, int events, void* data);
102 int eventReceiver(int fd, int events);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700103
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800104public:
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800105 enum {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800106 INVALIDATE = 0,
107 REFRESH = 1,
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800108 };
109
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700110 MessageQueue();
111 ~MessageQueue();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800112 void init(const sp<SurfaceFlinger>& flinger);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800113 void setEventThread(const sp<EventThread>& events);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700114
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800115 void waitMessage();
Lloyd Pique78ce4182018-01-31 16:39:51 -0800116 status_t postMessage(const sp<MessageBase>& message, nsecs_t reltime = 0);
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700117
118 // sends INVALIDATE message at next VSYNC
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800119 void invalidate();
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700120 // sends REFRESH message at next VSYNC
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800121 void refresh();
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700122};
123
124// ---------------------------------------------------------------------------
125
126}; // namespace android
127
128#endif /* ANDROID_MESSAGE_QUEUE_H */