xref: /aosp_15_r20/frameworks/native/include/gui/DisplayEventReceiver.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
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 <ftl/flags.h>
24 
25 #include <utils/Errors.h>
26 #include <utils/RefBase.h>
27 #include <utils/Timers.h>
28 
29 #include <android/gui/ISurfaceComposer.h>
30 #include <binder/IInterface.h>
31 #include <gui/VsyncEventData.h>
32 
33 #include <ui/DisplayId.h>
34 
35 // ----------------------------------------------------------------------------
36 
37 namespace android {
38 
39 // ----------------------------------------------------------------------------
40 
41 using EventRegistrationFlags = ftl::Flags<gui::ISurfaceComposer::EventRegistration>;
42 
43 using gui::IDisplayEventConnection;
44 using gui::ParcelableVsyncEventData;
45 using gui::VsyncEventData;
46 
47 namespace gui {
48 class BitTube;
49 } // namespace gui
50 
fourcc(char c1,char c2,char c3,char c4)51 static 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 }
57 
58 // ----------------------------------------------------------------------------
59 class DisplayEventReceiver {
60 public:
61     enum {
62         DISPLAY_EVENT_VSYNC = fourcc('v', 's', 'y', 'n'),
63         DISPLAY_EVENT_HOTPLUG = fourcc('p', 'l', 'u', 'g'),
64         DISPLAY_EVENT_MODE_CHANGE = fourcc('m', 'o', 'd', 'e'),
65         DISPLAY_EVENT_MODE_REJECTION = fourcc('r', 'e', 'j', 'e'),
66         DISPLAY_EVENT_NULL = fourcc('n', 'u', 'l', 'l'),
67         DISPLAY_EVENT_FRAME_RATE_OVERRIDE = fourcc('r', 'a', 't', 'e'),
68         DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH = fourcc('f', 'l', 's', 'h'),
69         DISPLAY_EVENT_HDCP_LEVELS_CHANGE = fourcc('h', 'd', 'c', 'p'),
70     };
71 
72     struct Event {
73         // We add __attribute__((aligned(8))) for nsecs_t fields because
74         // we need to make sure all fields are aligned the same with x86
75         // and x64 (long long has different default alignment):
76         //
77         // https://en.wikipedia.org/wiki/Data_structure_alignment
78 
79         struct Header {
80             uint32_t type;
81             PhysicalDisplayId displayId __attribute__((aligned(8)));
82             nsecs_t timestamp __attribute__((aligned(8)));
83         };
84 
85         struct VSync {
86             uint32_t count;
87             VsyncEventData vsyncData;
88         };
89 
90         struct Hotplug {
91             bool connected;
92             int32_t connectionError __attribute__((aligned(4)));
93         };
94 
95         struct ModeChange {
96             int32_t modeId;
97             nsecs_t vsyncPeriod __attribute__((aligned(8)));
98         };
99 
100         struct ModeRejection {
101             int32_t modeId;
102         };
103 
104         struct FrameRateOverride {
105             uid_t uid __attribute__((aligned(8)));
106             float frameRateHz __attribute__((aligned(8)));
107         };
108 
109         /*
110          * The values are defined in aidl:
111          * hardware/interfaces/drm/aidl/android/hardware/drm/HdcpLevel.aidl
112          */
113         struct HdcpLevelsChange {
114             int32_t connectedLevel;
115             int32_t maxLevel;
116         };
117 
118         Header header;
119         union {
120             VSync vsync;
121             Hotplug hotplug;
122             ModeChange modeChange;
123             FrameRateOverride frameRateOverride;
124             HdcpLevelsChange hdcpLevelsChange;
125             ModeRejection modeRejection;
126         };
127     };
128     static_assert(sizeof(Event) == 224);
129 
130 public:
131     /*
132      * DisplayEventReceiver creates and registers an event connection with
133      * SurfaceFlinger. VSync events are disabled by default. Call setVSyncRate
134      * or requestNextVsync to receive them.
135      * To receive ModeChanged and/or FrameRateOverrides events specify this in
136      * the constructor. Other events start being delivered immediately.
137      */
138     explicit DisplayEventReceiver(gui::ISurfaceComposer::VsyncSource vsyncSource =
139                                           gui::ISurfaceComposer::VsyncSource::eVsyncSourceApp,
140                                   EventRegistrationFlags eventRegistration = {},
141                                   const sp<IBinder>& layerHandle = nullptr);
142 
143     /*
144      * ~DisplayEventReceiver severs the connection with SurfaceFlinger, new events
145      * stop being delivered immediately. Note that the queue could have
146      * some events pending. These will be delivered.
147      */
148     ~DisplayEventReceiver();
149 
150     /*
151      * initCheck returns the state of DisplayEventReceiver after construction.
152      */
153     status_t initCheck() const;
154 
155     /*
156      * getFd returns the file descriptor to use to receive events.
157      * OWNERSHIP IS RETAINED by DisplayEventReceiver. DO NOT CLOSE this
158      * file-descriptor.
159      */
160     int getFd() const;
161 
162     /*
163      * getEvents reads events from the queue and returns how many events were
164      * read. Returns 0 if there are no more events or a negative error code.
165      * If NOT_ENOUGH_DATA is returned, the object has become invalid forever, it
166      * should be destroyed and getEvents() shouldn't be called again.
167      */
168     ssize_t getEvents(Event* events, size_t count);
169     static ssize_t getEvents(gui::BitTube* dataChannel, Event* events, size_t count);
170 
171     /*
172      * sendEvents write events to the queue and returns how many events were
173      * written.
174      */
175     ssize_t sendEvents(Event const* events, size_t count);
176     static ssize_t sendEvents(gui::BitTube* dataChannel, Event const* events, size_t count);
177 
178     /*
179      * setVsyncRate() sets the Event::VSync delivery rate. A value of
180      * 1 returns every Event::VSync. A value of 2 returns every other event,
181      * etc... a value of 0 returns no event unless  requestNextVsync() has
182      * been called.
183      */
184     status_t setVsyncRate(uint32_t count);
185 
186     /*
187      * requestNextVsync() schedules the next Event::VSync. It has no effect
188      * if the vsync rate is > 0.
189      */
190     status_t requestNextVsync();
191 
192     /**
193      * getLatestVsyncEventData() gets the latest vsync event data.
194      */
195     status_t getLatestVsyncEventData(ParcelableVsyncEventData* outVsyncEventData) const;
196 
197 private:
198     sp<IDisplayEventConnection> mEventConnection;
199     std::unique_ptr<gui::BitTube> mDataChannel;
200     std::optional<status_t> mInitError;
201 };
202 
203 inline bool operator==(DisplayEventReceiver::Event::FrameRateOverride lhs,
204                        DisplayEventReceiver::Event::FrameRateOverride rhs) {
205     return (lhs.uid == rhs.uid) && std::abs(lhs.frameRateHz - rhs.frameRateHz) < 0.001f;
206 }
207 
208 // ----------------------------------------------------------------------------
209 }; // namespace android
210 
211 #endif // ANDROID_GUI_DISPLAY_EVENT_H
212