blob: 7fd6c35c5e247375d5029e6fe0c97301e696da49 [file] [log] [blame]
Mathias Agopiand0566bc2011-11-17 17:49:17 -08001/*
2 * Copyright (C) 2011 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_GUI_DISPLAY_EVENT_H
18#define ANDROID_GUI_DISPLAY_EVENT_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
Huihong Luo1b0c49f2022-03-15 19:18:21 -070023#include <ftl/flags.h>
24
Mathias Agopiand0566bc2011-11-17 17:49:17 -080025#include <utils/Errors.h>
26#include <utils/RefBase.h>
27#include <utils/Timers.h>
28
Huihong Luo1b0c49f2022-03-15 19:18:21 -070029#include <android/gui/ISurfaceComposer.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080030#include <binder/IInterface.h>
Rachel Lee18c34372022-01-20 13:57:18 -080031#include <gui/VsyncEventData.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080032
Huihong Luo1b0c49f2022-03-15 19:18:21 -070033#include <ui/DisplayId.h>
34
Mathias Agopiand0566bc2011-11-17 17:49:17 -080035// ----------------------------------------------------------------------------
36
37namespace android {
38
39// ----------------------------------------------------------------------------
40
Huihong Luo1b0c49f2022-03-15 19:18:21 -070041using EventRegistrationFlags = ftl::Flags<gui::ISurfaceComposer::EventRegistration>;
42
Huihong Luo6fac5232021-11-22 16:05:23 -080043using gui::IDisplayEventConnection;
Rachel Leeb9c5a772022-02-04 21:17:37 -080044using gui::ParcelableVsyncEventData;
Rachel Lee18c34372022-01-20 13:57:18 -080045using gui::VsyncEventData;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080046
Dan Stoza27c81152017-03-31 16:30:42 -070047namespace gui {
48class BitTube;
49} // namespace gui
50
Colin Crossc2b90172016-09-26 18:11:51 -070051static inline constexpr uint32_t fourcc(char c1, char c2, char c3, char c4) {
52 return static_cast<uint32_t>(c1) << 24 |
53 static_cast<uint32_t>(c2) << 16 |
54 static_cast<uint32_t>(c3) << 8 |
55 static_cast<uint32_t>(c4);
56}
Mathias Agopiand0566bc2011-11-17 17:49:17 -080057
Colin Crossc2b90172016-09-26 18:11:51 -070058// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -080059class DisplayEventReceiver {
60public:
Rachel Lee3f028662021-11-04 19:32:24 +000061
Mathias Agopiand0566bc2011-11-17 17:49:17 -080062 enum {
Colin Crossc2b90172016-09-26 18:11:51 -070063 DISPLAY_EVENT_VSYNC = fourcc('v', 's', 'y', 'n'),
64 DISPLAY_EVENT_HOTPLUG = fourcc('p', 'l', 'u', 'g'),
Marin Shalamanova7fe3042021-01-29 21:02:08 +010065 DISPLAY_EVENT_MODE_CHANGE = fourcc('m', 'o', 'd', 'e'),
Alec Mouri967d5d72020-08-05 12:50:03 -070066 DISPLAY_EVENT_NULL = fourcc('n', 'u', 'l', 'l'),
Ady Abraham62f216c2020-10-13 19:07:23 -070067 DISPLAY_EVENT_FRAME_RATE_OVERRIDE = fourcc('r', 'a', 't', 'e'),
68 DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH = fourcc('f', 'l', 's', 'h'),
Mathias Agopiand0566bc2011-11-17 17:49:17 -080069 };
70
71 struct Event {
Yahan Zhou3cbda592020-08-03 15:53:27 -070072 // We add __attribute__((aligned(8))) for nsecs_t fields because
73 // we need to make sure all fields are aligned the same with x86
74 // and x64 (long long has different default alignment):
75 //
76 // https://en.wikipedia.org/wiki/Data_structure_alignment
Mathias Agopiand0566bc2011-11-17 17:49:17 -080077
78 struct Header {
79 uint32_t type;
Yahan Zhou3cbda592020-08-03 15:53:27 -070080 PhysicalDisplayId displayId __attribute__((aligned(8)));
Fengwei Yin0c7c81f2014-03-20 16:45:05 +080081 nsecs_t timestamp __attribute__((aligned(8)));
Mathias Agopiand0566bc2011-11-17 17:49:17 -080082 };
83
84 struct VSync {
85 uint32_t count;
Rachel Leeb9c5a772022-02-04 21:17:37 -080086 VsyncEventData vsyncData;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080087 };
88
Mathias Agopian148994e2012-09-19 17:31:36 -070089 struct Hotplug {
Mathias Agopian148994e2012-09-19 17:31:36 -070090 bool connected;
91 };
92
Marin Shalamanova7fe3042021-01-29 21:02:08 +010093 struct ModeChange {
94 int32_t modeId;
Yahan Zhou3cbda592020-08-03 15:53:27 -070095 nsecs_t vsyncPeriod __attribute__((aligned(8)));
Ady Abraham447052e2019-02-13 16:07:27 -080096 };
97
Ady Abraham62f216c2020-10-13 19:07:23 -070098 struct FrameRateOverride {
99 uid_t uid __attribute__((aligned(8)));
100 float frameRateHz __attribute__((aligned(8)));
101 };
102
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800103 Header header;
104 union {
105 VSync vsync;
Mathias Agopian148994e2012-09-19 17:31:36 -0700106 Hotplug hotplug;
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100107 ModeChange modeChange;
Ady Abraham62f216c2020-10-13 19:07:23 -0700108 FrameRateOverride frameRateOverride;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800109 };
110 };
111
112public:
113 /*
114 * DisplayEventReceiver creates and registers an event connection with
Mathias Agopian3cf199a2012-01-31 16:42:54 -0800115 * SurfaceFlinger. VSync events are disabled by default. Call setVSyncRate
116 * or requestNextVsync to receive them.
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100117 * To receive ModeChanged and/or FrameRateOverrides events specify this in
Ady Abraham62f216c2020-10-13 19:07:23 -0700118 * the constructor. Other events start being delivered immediately.
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800119 */
Huihong Luo1b0c49f2022-03-15 19:18:21 -0700120 explicit DisplayEventReceiver(gui::ISurfaceComposer::VsyncSource vsyncSource =
121 gui::ISurfaceComposer::VsyncSource::eVsyncSourceApp,
Rachel Lee2248f522023-01-27 16:45:23 -0800122 EventRegistrationFlags eventRegistration = {},
123 const sp<IBinder>& layerHandle = nullptr);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800124
125 /*
126 * ~DisplayEventReceiver severs the connection with SurfaceFlinger, new events
127 * stop being delivered immediately. Note that the queue could have
128 * some events pending. These will be delivered.
129 */
130 ~DisplayEventReceiver();
131
132 /*
133 * initCheck returns the state of DisplayEventReceiver after construction.
134 */
135 status_t initCheck() const;
136
137 /*
138 * getFd returns the file descriptor to use to receive events.
139 * OWNERSHIP IS RETAINED by DisplayEventReceiver. DO NOT CLOSE this
140 * file-descriptor.
141 */
142 int getFd() const;
143
144 /*
Mathias Agopian7b5be952012-04-02 17:02:19 -0700145 * getEvents reads events from the queue and returns how many events were
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800146 * read. Returns 0 if there are no more events or a negative error code.
147 * If NOT_ENOUGH_DATA is returned, the object has become invalid forever, it
148 * should be destroyed and getEvents() shouldn't be called again.
149 */
150 ssize_t getEvents(Event* events, size_t count);
Dan Stoza6b698e42017-04-03 13:09:08 -0700151 static ssize_t getEvents(gui::BitTube* dataChannel, Event* events, size_t count);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800152
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800153 /*
Mathias Agopian7b5be952012-04-02 17:02:19 -0700154 * sendEvents write events to the queue and returns how many events were
155 * written.
156 */
Alec Mouri967d5d72020-08-05 12:50:03 -0700157 ssize_t sendEvents(Event const* events, size_t count);
Dan Stoza6b698e42017-04-03 13:09:08 -0700158 static ssize_t sendEvents(gui::BitTube* dataChannel, Event const* events, size_t count);
Mathias Agopian7b5be952012-04-02 17:02:19 -0700159
160 /*
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800161 * setVsyncRate() sets the Event::VSync delivery rate. A value of
162 * 1 returns every Event::VSync. A value of 2 returns every other event,
163 * etc... a value of 0 returns no event unless requestNextVsync() has
164 * been called.
165 */
166 status_t setVsyncRate(uint32_t count);
167
168 /*
169 * requestNextVsync() schedules the next Event::VSync. It has no effect
170 * if the vsync rate is > 0.
171 */
172 status_t requestNextVsync();
173
Rachel Leeef2e21f2022-02-01 14:51:34 -0800174 /**
175 * getLatestVsyncEventData() gets the latest vsync event data.
176 */
Rachel Leeb9c5a772022-02-04 21:17:37 -0800177 status_t getLatestVsyncEventData(ParcelableVsyncEventData* outVsyncEventData) const;
Rachel Leeef2e21f2022-02-01 14:51:34 -0800178
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800179private:
180 sp<IDisplayEventConnection> mEventConnection;
Dan Stoza6b698e42017-04-03 13:09:08 -0700181 std::unique_ptr<gui::BitTube> mDataChannel;
Ady Abraham745f7b52022-02-08 19:36:37 -0800182 std::optional<status_t> mInitError;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800183};
184
Andy Yud6a36202022-01-26 04:08:22 -0800185inline bool operator==(DisplayEventReceiver::Event::FrameRateOverride lhs,
186 DisplayEventReceiver::Event::FrameRateOverride rhs) {
187 return (lhs.uid == rhs.uid) && std::abs(lhs.frameRateHz - rhs.frameRateHz) < 0.001f;
188}
189
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800190// ----------------------------------------------------------------------------
191}; // namespace android
192
193#endif // ANDROID_GUI_DISPLAY_EVENT_H