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_NULL = fourcc('n', 'u', 'l', 'l'),
66         DISPLAY_EVENT_FRAME_RATE_OVERRIDE = fourcc('r', 'a', 't', 'e'),
67         DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH = fourcc('f', 'l', 's', 'h'),
68         DISPLAY_EVENT_HDCP_LEVELS_CHANGE = fourcc('h', 'd', 'c', 'p'),
69     };
70 
71     struct Event {
72         // 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
77 
78         struct Header {
79             uint32_t type;
80             PhysicalDisplayId displayId __attribute__((aligned(8)));
81             nsecs_t timestamp __attribute__((aligned(8)));
82         };
83 
84         struct VSync {
85             uint32_t count;
86             VsyncEventData vsyncData;
87         };
88 
89         struct Hotplug {
90             bool connected;
91             int32_t connectionError __attribute__((aligned(4)));
92         };
93 
94         struct ModeChange {
95             int32_t modeId;
96             nsecs_t vsyncPeriod __attribute__((aligned(8)));
97         };
98 
99         struct FrameRateOverride {
100             uid_t uid __attribute__((aligned(8)));
101             float frameRateHz __attribute__((aligned(8)));
102         };
103 
104         /*
105          * The values are defined in aidl:
106          * hardware/interfaces/drm/aidl/android/hardware/drm/HdcpLevel.aidl
107          */
108         struct HdcpLevelsChange {
109             int32_t connectedLevel;
110             int32_t maxLevel;
111         };
112 
113         Header header;
114         union {
115             VSync vsync;
116             Hotplug hotplug;
117             ModeChange modeChange;
118             FrameRateOverride frameRateOverride;
119             HdcpLevelsChange hdcpLevelsChange;
120         };
121     };
122     static_assert(sizeof(Event) == 216);
123 
124 public:
125     /*
126      * DisplayEventReceiver creates and registers an event connection with
127      * SurfaceFlinger. VSync events are disabled by default. Call setVSyncRate
128      * or requestNextVsync to receive them.
129      * To receive ModeChanged and/or FrameRateOverrides events specify this in
130      * the constructor. Other events start being delivered immediately.
131      */
132     explicit DisplayEventReceiver(gui::ISurfaceComposer::VsyncSource vsyncSource =
133                                           gui::ISurfaceComposer::VsyncSource::eVsyncSourceApp,
134                                   EventRegistrationFlags eventRegistration = {},
135                                   const sp<IBinder>& layerHandle = nullptr);
136 
137     /*
138      * ~DisplayEventReceiver severs the connection with SurfaceFlinger, new events
139      * stop being delivered immediately. Note that the queue could have
140      * some events pending. These will be delivered.
141      */
142     ~DisplayEventReceiver();
143 
144     /*
145      * initCheck returns the state of DisplayEventReceiver after construction.
146      */
147     status_t initCheck() const;
148 
149     /*
150      * getFd returns the file descriptor to use to receive events.
151      * OWNERSHIP IS RETAINED by DisplayEventReceiver. DO NOT CLOSE this
152      * file-descriptor.
153      */
154     int getFd() const;
155 
156     /*
157      * getEvents reads events from the queue and returns how many events were
158      * read. Returns 0 if there are no more events or a negative error code.
159      * If NOT_ENOUGH_DATA is returned, the object has become invalid forever, it
160      * should be destroyed and getEvents() shouldn't be called again.
161      */
162     ssize_t getEvents(Event* events, size_t count);
163     static ssize_t getEvents(gui::BitTube* dataChannel, Event* events, size_t count);
164 
165     /*
166      * sendEvents write events to the queue and returns how many events were
167      * written.
168      */
169     ssize_t sendEvents(Event const* events, size_t count);
170     static ssize_t sendEvents(gui::BitTube* dataChannel, Event const* events, size_t count);
171 
172     /*
173      * setVsyncRate() sets the Event::VSync delivery rate. A value of
174      * 1 returns every Event::VSync. A value of 2 returns every other event,
175      * etc... a value of 0 returns no event unless  requestNextVsync() has
176      * been called.
177      */
178     status_t setVsyncRate(uint32_t count);
179 
180     /*
181      * requestNextVsync() schedules the next Event::VSync. It has no effect
182      * if the vsync rate is > 0.
183      */
184     status_t requestNextVsync();
185 
186     /**
187      * getLatestVsyncEventData() gets the latest vsync event data.
188      */
189     status_t getLatestVsyncEventData(ParcelableVsyncEventData* outVsyncEventData) const;
190 
191 private:
192     sp<IDisplayEventConnection> mEventConnection;
193     std::unique_ptr<gui::BitTube> mDataChannel;
194     std::optional<status_t> mInitError;
195 };
196 
197 inline bool operator==(DisplayEventReceiver::Event::FrameRateOverride lhs,
198                        DisplayEventReceiver::Event::FrameRateOverride rhs) {
199     return (lhs.uid == rhs.uid) && std::abs(lhs.frameRateHz - rhs.frameRateHz) < 0.001f;
200 }
201 
202 // ----------------------------------------------------------------------------
203 }; // namespace android
204 
205 #endif // ANDROID_GUI_DISPLAY_EVENT_H
206