blob: 456bbfb6116c96d0681326f6d4be2aa6b086b919 [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
23#include <utils/Errors.h>
24#include <utils/RefBase.h>
25#include <utils/Timers.h>
26
27#include <binder/IInterface.h>
Jorim Jaggib1e2f8d2017-06-08 15:43:59 -070028#include <gui/ISurfaceComposer.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080029
30// ----------------------------------------------------------------------------
31
32namespace android {
33
34// ----------------------------------------------------------------------------
35
Huihong Luo6fac5232021-11-22 16:05:23 -080036using gui::IDisplayEventConnection;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080037
Dan Stoza27c81152017-03-31 16:30:42 -070038namespace gui {
39class BitTube;
40} // namespace gui
41
Colin Crossc2b90172016-09-26 18:11:51 -070042static inline constexpr uint32_t fourcc(char c1, char c2, char c3, char c4) {
43 return static_cast<uint32_t>(c1) << 24 |
44 static_cast<uint32_t>(c2) << 16 |
45 static_cast<uint32_t>(c3) << 8 |
46 static_cast<uint32_t>(c4);
47}
Mathias Agopiand0566bc2011-11-17 17:49:17 -080048
Colin Crossc2b90172016-09-26 18:11:51 -070049// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -080050class DisplayEventReceiver {
51public:
Rachel Lee3f028662021-11-04 19:32:24 +000052 // Max amount of frame timelines is arbitrarily set to be reasonable.
53 static constexpr int64_t kFrameTimelinesLength = 7;
54
Mathias Agopiand0566bc2011-11-17 17:49:17 -080055 enum {
Colin Crossc2b90172016-09-26 18:11:51 -070056 DISPLAY_EVENT_VSYNC = fourcc('v', 's', 'y', 'n'),
57 DISPLAY_EVENT_HOTPLUG = fourcc('p', 'l', 'u', 'g'),
Marin Shalamanova7fe3042021-01-29 21:02:08 +010058 DISPLAY_EVENT_MODE_CHANGE = fourcc('m', 'o', 'd', 'e'),
Alec Mouri967d5d72020-08-05 12:50:03 -070059 DISPLAY_EVENT_NULL = fourcc('n', 'u', 'l', 'l'),
Ady Abraham62f216c2020-10-13 19:07:23 -070060 DISPLAY_EVENT_FRAME_RATE_OVERRIDE = fourcc('r', 'a', 't', 'e'),
61 DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH = fourcc('f', 'l', 's', 'h'),
Mathias Agopiand0566bc2011-11-17 17:49:17 -080062 };
63
64 struct Event {
Yahan Zhou3cbda592020-08-03 15:53:27 -070065 // We add __attribute__((aligned(8))) for nsecs_t fields because
66 // we need to make sure all fields are aligned the same with x86
67 // and x64 (long long has different default alignment):
68 //
69 // https://en.wikipedia.org/wiki/Data_structure_alignment
Mathias Agopiand0566bc2011-11-17 17:49:17 -080070
71 struct Header {
72 uint32_t type;
Yahan Zhou3cbda592020-08-03 15:53:27 -070073 PhysicalDisplayId displayId __attribute__((aligned(8)));
Fengwei Yin0c7c81f2014-03-20 16:45:05 +080074 nsecs_t timestamp __attribute__((aligned(8)));
Mathias Agopiand0566bc2011-11-17 17:49:17 -080075 };
76
77 struct VSync {
78 uint32_t count;
Yahan Zhou3cbda592020-08-03 15:53:27 -070079 nsecs_t expectedVSyncTimestamp __attribute__((aligned(8)));
Ady Abraham8cb21882020-08-26 18:22:05 -070080 nsecs_t deadlineTimestamp __attribute__((aligned(8)));
Jorim Jaggic0086af2021-02-12 18:18:11 +010081 nsecs_t frameInterval __attribute__((aligned(8)));
Ady Abraham74e17562020-08-24 18:18:19 -070082 int64_t vsyncId;
Rachel Lee3f028662021-11-04 19:32:24 +000083 size_t preferredFrameTimelineIndex __attribute__((aligned(8)));
84 struct FrameTimeline {
85 nsecs_t expectedVSyncTimestamp __attribute__((aligned(8)));
86 nsecs_t deadlineTimestamp __attribute__((aligned(8)));
87 int64_t vsyncId;
88 } frameTimelines[kFrameTimelinesLength];
Mathias Agopiand0566bc2011-11-17 17:49:17 -080089 };
90
Mathias Agopian148994e2012-09-19 17:31:36 -070091 struct Hotplug {
Mathias Agopian148994e2012-09-19 17:31:36 -070092 bool connected;
93 };
94
Marin Shalamanova7fe3042021-01-29 21:02:08 +010095 struct ModeChange {
96 int32_t modeId;
Yahan Zhou3cbda592020-08-03 15:53:27 -070097 nsecs_t vsyncPeriod __attribute__((aligned(8)));
Ady Abraham447052e2019-02-13 16:07:27 -080098 };
99
Ady Abraham62f216c2020-10-13 19:07:23 -0700100 struct FrameRateOverride {
101 uid_t uid __attribute__((aligned(8)));
102 float frameRateHz __attribute__((aligned(8)));
103 };
104
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800105 Header header;
106 union {
107 VSync vsync;
Mathias Agopian148994e2012-09-19 17:31:36 -0700108 Hotplug hotplug;
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100109 ModeChange modeChange;
Ady Abraham62f216c2020-10-13 19:07:23 -0700110 FrameRateOverride frameRateOverride;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800111 };
112 };
113
114public:
115 /*
116 * DisplayEventReceiver creates and registers an event connection with
Mathias Agopian3cf199a2012-01-31 16:42:54 -0800117 * SurfaceFlinger. VSync events are disabled by default. Call setVSyncRate
118 * or requestNextVsync to receive them.
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100119 * To receive ModeChanged and/or FrameRateOverrides events specify this in
Ady Abraham62f216c2020-10-13 19:07:23 -0700120 * the constructor. Other events start being delivered immediately.
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800121 */
Chih-Hung Hsiehaaf62162018-12-20 15:45:04 -0800122 explicit DisplayEventReceiver(
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700123 ISurfaceComposer::VsyncSource vsyncSource = ISurfaceComposer::eVsyncSourceApp,
Ady Abraham62f216c2020-10-13 19:07:23 -0700124 ISurfaceComposer::EventRegistrationFlags eventRegistration = {});
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800125
126 /*
127 * ~DisplayEventReceiver severs the connection with SurfaceFlinger, new events
128 * stop being delivered immediately. Note that the queue could have
129 * some events pending. These will be delivered.
130 */
131 ~DisplayEventReceiver();
132
133 /*
134 * initCheck returns the state of DisplayEventReceiver after construction.
135 */
136 status_t initCheck() const;
137
138 /*
139 * getFd returns the file descriptor to use to receive events.
140 * OWNERSHIP IS RETAINED by DisplayEventReceiver. DO NOT CLOSE this
141 * file-descriptor.
142 */
143 int getFd() const;
144
145 /*
Mathias Agopian7b5be952012-04-02 17:02:19 -0700146 * getEvents reads events from the queue and returns how many events were
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800147 * read. Returns 0 if there are no more events or a negative error code.
148 * If NOT_ENOUGH_DATA is returned, the object has become invalid forever, it
149 * should be destroyed and getEvents() shouldn't be called again.
150 */
151 ssize_t getEvents(Event* events, size_t count);
Dan Stoza6b698e42017-04-03 13:09:08 -0700152 static ssize_t getEvents(gui::BitTube* dataChannel, Event* events, size_t count);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800153
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800154 /*
Mathias Agopian7b5be952012-04-02 17:02:19 -0700155 * sendEvents write events to the queue and returns how many events were
156 * written.
157 */
Alec Mouri967d5d72020-08-05 12:50:03 -0700158 ssize_t sendEvents(Event const* events, size_t count);
Dan Stoza6b698e42017-04-03 13:09:08 -0700159 static ssize_t sendEvents(gui::BitTube* dataChannel, Event const* events, size_t count);
Mathias Agopian7b5be952012-04-02 17:02:19 -0700160
161 /*
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800162 * setVsyncRate() sets the Event::VSync delivery rate. A value of
163 * 1 returns every Event::VSync. A value of 2 returns every other event,
164 * etc... a value of 0 returns no event unless requestNextVsync() has
165 * been called.
166 */
167 status_t setVsyncRate(uint32_t count);
168
169 /*
170 * requestNextVsync() schedules the next Event::VSync. It has no effect
171 * if the vsync rate is > 0.
172 */
173 status_t requestNextVsync();
174
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800175private:
176 sp<IDisplayEventConnection> mEventConnection;
Dan Stoza6b698e42017-04-03 13:09:08 -0700177 std::unique_ptr<gui::BitTube> mDataChannel;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800178};
179
180// ----------------------------------------------------------------------------
181}; // namespace android
182
183#endif // ANDROID_GUI_DISPLAY_EVENT_H