blob: 0dffbde88aff26d6595698fbff7c56c9e0ebebac [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
Mathias Agopiand0566bc2011-11-17 17:49:17 -080036class IDisplayEventConnection;
37
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:
52 enum {
Colin Crossc2b90172016-09-26 18:11:51 -070053 DISPLAY_EVENT_VSYNC = fourcc('v', 's', 'y', 'n'),
54 DISPLAY_EVENT_HOTPLUG = fourcc('p', 'l', 'u', 'g'),
Marin Shalamanova7fe3042021-01-29 21:02:08 +010055 DISPLAY_EVENT_MODE_CHANGE = fourcc('m', 'o', 'd', 'e'),
Alec Mouri967d5d72020-08-05 12:50:03 -070056 DISPLAY_EVENT_NULL = fourcc('n', 'u', 'l', 'l'),
Ady Abraham62f216c2020-10-13 19:07:23 -070057 DISPLAY_EVENT_FRAME_RATE_OVERRIDE = fourcc('r', 'a', 't', 'e'),
58 DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH = fourcc('f', 'l', 's', 'h'),
Mathias Agopiand0566bc2011-11-17 17:49:17 -080059 };
60
61 struct Event {
Yahan Zhou3cbda592020-08-03 15:53:27 -070062 // We add __attribute__((aligned(8))) for nsecs_t fields because
63 // we need to make sure all fields are aligned the same with x86
64 // and x64 (long long has different default alignment):
65 //
66 // https://en.wikipedia.org/wiki/Data_structure_alignment
Mathias Agopiand0566bc2011-11-17 17:49:17 -080067
68 struct Header {
69 uint32_t type;
Yahan Zhou3cbda592020-08-03 15:53:27 -070070 PhysicalDisplayId displayId __attribute__((aligned(8)));
Fengwei Yin0c7c81f2014-03-20 16:45:05 +080071 nsecs_t timestamp __attribute__((aligned(8)));
Mathias Agopiand0566bc2011-11-17 17:49:17 -080072 };
73
74 struct VSync {
75 uint32_t count;
Yahan Zhou3cbda592020-08-03 15:53:27 -070076 nsecs_t expectedVSyncTimestamp __attribute__((aligned(8)));
Ady Abraham8cb21882020-08-26 18:22:05 -070077 nsecs_t deadlineTimestamp __attribute__((aligned(8)));
Jorim Jaggic0086af2021-02-12 18:18:11 +010078 nsecs_t frameInterval __attribute__((aligned(8)));
Ady Abraham74e17562020-08-24 18:18:19 -070079 int64_t vsyncId;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080080 };
81
Mathias Agopian148994e2012-09-19 17:31:36 -070082 struct Hotplug {
Mathias Agopian148994e2012-09-19 17:31:36 -070083 bool connected;
84 };
85
Marin Shalamanova7fe3042021-01-29 21:02:08 +010086 struct ModeChange {
87 int32_t modeId;
Yahan Zhou3cbda592020-08-03 15:53:27 -070088 nsecs_t vsyncPeriod __attribute__((aligned(8)));
Ady Abraham447052e2019-02-13 16:07:27 -080089 };
90
Ady Abraham62f216c2020-10-13 19:07:23 -070091 struct FrameRateOverride {
92 uid_t uid __attribute__((aligned(8)));
93 float frameRateHz __attribute__((aligned(8)));
94 };
95
Mathias Agopiand0566bc2011-11-17 17:49:17 -080096 Header header;
97 union {
98 VSync vsync;
Mathias Agopian148994e2012-09-19 17:31:36 -070099 Hotplug hotplug;
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100100 ModeChange modeChange;
Ady Abraham62f216c2020-10-13 19:07:23 -0700101 FrameRateOverride frameRateOverride;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800102 };
103 };
104
105public:
106 /*
107 * DisplayEventReceiver creates and registers an event connection with
Mathias Agopian3cf199a2012-01-31 16:42:54 -0800108 * SurfaceFlinger. VSync events are disabled by default. Call setVSyncRate
109 * or requestNextVsync to receive them.
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100110 * To receive ModeChanged and/or FrameRateOverrides events specify this in
Ady Abraham62f216c2020-10-13 19:07:23 -0700111 * the constructor. Other events start being delivered immediately.
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800112 */
Chih-Hung Hsiehaaf62162018-12-20 15:45:04 -0800113 explicit DisplayEventReceiver(
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700114 ISurfaceComposer::VsyncSource vsyncSource = ISurfaceComposer::eVsyncSourceApp,
Ady Abraham62f216c2020-10-13 19:07:23 -0700115 ISurfaceComposer::EventRegistrationFlags eventRegistration = {});
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800116
117 /*
118 * ~DisplayEventReceiver severs the connection with SurfaceFlinger, new events
119 * stop being delivered immediately. Note that the queue could have
120 * some events pending. These will be delivered.
121 */
122 ~DisplayEventReceiver();
123
124 /*
125 * initCheck returns the state of DisplayEventReceiver after construction.
126 */
127 status_t initCheck() const;
128
129 /*
130 * getFd returns the file descriptor to use to receive events.
131 * OWNERSHIP IS RETAINED by DisplayEventReceiver. DO NOT CLOSE this
132 * file-descriptor.
133 */
134 int getFd() const;
135
136 /*
Mathias Agopian7b5be952012-04-02 17:02:19 -0700137 * getEvents reads events from the queue and returns how many events were
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800138 * read. Returns 0 if there are no more events or a negative error code.
139 * If NOT_ENOUGH_DATA is returned, the object has become invalid forever, it
140 * should be destroyed and getEvents() shouldn't be called again.
141 */
142 ssize_t getEvents(Event* events, size_t count);
Dan Stoza6b698e42017-04-03 13:09:08 -0700143 static ssize_t getEvents(gui::BitTube* dataChannel, Event* events, size_t count);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800144
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800145 /*
Mathias Agopian7b5be952012-04-02 17:02:19 -0700146 * sendEvents write events to the queue and returns how many events were
147 * written.
148 */
Alec Mouri967d5d72020-08-05 12:50:03 -0700149 ssize_t sendEvents(Event const* events, size_t count);
Dan Stoza6b698e42017-04-03 13:09:08 -0700150 static ssize_t sendEvents(gui::BitTube* dataChannel, Event const* events, size_t count);
Mathias Agopian7b5be952012-04-02 17:02:19 -0700151
152 /*
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800153 * setVsyncRate() sets the Event::VSync delivery rate. A value of
154 * 1 returns every Event::VSync. A value of 2 returns every other event,
155 * etc... a value of 0 returns no event unless requestNextVsync() has
156 * been called.
157 */
158 status_t setVsyncRate(uint32_t count);
159
160 /*
161 * requestNextVsync() schedules the next Event::VSync. It has no effect
162 * if the vsync rate is > 0.
163 */
164 status_t requestNextVsync();
165
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800166private:
167 sp<IDisplayEventConnection> mEventConnection;
Dan Stoza6b698e42017-04-03 13:09:08 -0700168 std::unique_ptr<gui::BitTube> mDataChannel;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800169};
170
171// ----------------------------------------------------------------------------
172}; // namespace android
173
174#endif // ANDROID_GUI_DISPLAY_EVENT_H