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