blob: 2ec697ea3541d45175878ba94ae84b38e6a2b337 [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
Lloyd Pique3fcdef12018-01-22 17:14:00 -080028#include <gui/IDisplayEventConnection.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 EventThread;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080038class SurfaceFlinger;
Mathias Agopian8aedd472012-01-24 16:39:14 -080039
Mathias Agopianf1d8e872009-04-20 19:39:12 -070040// ---------------------------------------------------------------------------
41
Lloyd Pique78ce4182018-01-31 16:39:51 -080042class MessageBase : public MessageHandler {
Mathias Agopianf1d8e872009-04-20 19:39:12 -070043public:
Mathias Agopianf61c57f2011-11-23 16:49:10 -080044 MessageBase();
Lloyd Pique78ce4182018-01-31 16:39:51 -080045
Mathias Agopianf1d8e872009-04-20 19:39:12 -070046 // 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
Alex Sakhartchouk117698b2017-06-07 11:36:32 -040061class LambdaMessage : public MessageBase {
62public:
63 explicit LambdaMessage(std::function<void()> handler)
64 : MessageBase(), mHandler(std::move(handler)) {}
65
66 bool handler() override {
67 mHandler();
68 // This return value is no longer checked, so it's always safe to return true
69 return true;
70 }
71
72private:
73 const std::function<void()> mHandler;
74};
75
Mathias Agopianf1d8e872009-04-20 19:39:12 -070076// ---------------------------------------------------------------------------
77
Mathias Agopianf61c57f2011-11-23 16:49:10 -080078class MessageQueue {
Lloyd Pique3fcdef12018-01-22 17:14:00 -080079public:
80 enum {
81 INVALIDATE = 0,
82 REFRESH = 1,
83 };
84
85 virtual ~MessageQueue();
86
87 virtual void init(const sp<SurfaceFlinger>& flinger) = 0;
Ana Krulec98b5b242018-08-10 15:03:23 -070088 // TODO(akrulec): Remove this function once everything is migrated to Scheduler.
Lloyd Pique3fcdef12018-01-22 17:14:00 -080089 virtual void setEventThread(EventThread* events) = 0;
Ana Krulec98b5b242018-08-10 15:03:23 -070090 virtual void setEventConnection(const sp<BnDisplayEventConnection>& connection) = 0;
Lloyd Pique3fcdef12018-01-22 17:14:00 -080091 virtual void waitMessage() = 0;
92 virtual status_t postMessage(const sp<MessageBase>& message, nsecs_t reltime = 0) = 0;
93 virtual void invalidate() = 0;
94 virtual void refresh() = 0;
95};
96
97// ---------------------------------------------------------------------------
98
99namespace impl {
100
101class MessageQueue final : public android::MessageQueue {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800102 class Handler : public MessageHandler {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800103 enum { eventMaskInvalidate = 0x1, eventMaskRefresh = 0x2, eventMaskTransaction = 0x4 };
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800104 MessageQueue& mQueue;
105 int32_t mEventMask;
Lloyd Pique78ce4182018-01-31 16:39:51 -0800106
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800107 public:
Lloyd Pique78ce4182018-01-31 16:39:51 -0800108 explicit Handler(MessageQueue& queue) : mQueue(queue), mEventMask(0) {}
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800109 virtual void handleMessage(const Message& message);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700110 void dispatchRefresh();
111 void dispatchInvalidate();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800112 };
113
114 friend class Handler;
115
116 sp<SurfaceFlinger> mFlinger;
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800117 sp<Looper> mLooper;
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800118 android::EventThread* mEventThread;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800119 sp<IDisplayEventConnection> mEvents;
Dan Stoza6b698e42017-04-03 13:09:08 -0700120 gui::BitTube mEventTube;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800121 sp<Handler> mHandler;
122
Mathias Agopian8aedd472012-01-24 16:39:14 -0800123 static int cb_eventReceiver(int fd, int events, void* data);
124 int eventReceiver(int fd, int events);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700125
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800126public:
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800127 ~MessageQueue() override = default;
128 void init(const sp<SurfaceFlinger>& flinger) override;
129 void setEventThread(android::EventThread* events) override;
Ana Krulec98b5b242018-08-10 15:03:23 -0700130 void setEventConnection(const sp<BnDisplayEventConnection>& connection) override;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800131
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800132 void waitMessage() override;
133 status_t postMessage(const sp<MessageBase>& message, nsecs_t reltime = 0) override;
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700134
135 // sends INVALIDATE message at next VSYNC
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800136 void invalidate() override;
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700137 // sends REFRESH message at next VSYNC
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800138 void refresh() override;
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700139};
140
141// ---------------------------------------------------------------------------
142
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800143} // namespace impl
144} // namespace android
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700145
146#endif /* ANDROID_MESSAGE_QUEUE_H */