DisplayEventReceiver.h revision c2b9017b595ed146aae181f0efc46c169ea86806
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 29// ---------------------------------------------------------------------------- 30 31namespace android { 32 33// ---------------------------------------------------------------------------- 34 35class BitTube; 36class IDisplayEventConnection; 37 38static inline constexpr uint32_t fourcc(char c1, char c2, char c3, char c4) { 39 return static_cast<uint32_t>(c1) << 24 | 40 static_cast<uint32_t>(c2) << 16 | 41 static_cast<uint32_t>(c3) << 8 | 42 static_cast<uint32_t>(c4); 43} 44 45// ---------------------------------------------------------------------------- 46class DisplayEventReceiver { 47public: 48 enum { 49 DISPLAY_EVENT_VSYNC = fourcc('v', 's', 'y', 'n'), 50 DISPLAY_EVENT_HOTPLUG = fourcc('p', 'l', 'u', 'g'), 51 }; 52 53 struct Event { 54 55 struct Header { 56 uint32_t type; 57 uint32_t id; 58 nsecs_t timestamp __attribute__((aligned(8))); 59 }; 60 61 struct VSync { 62 uint32_t count; 63 }; 64 65 struct Hotplug { 66 bool connected; 67 }; 68 69 Header header; 70 union { 71 VSync vsync; 72 Hotplug hotplug; 73 }; 74 }; 75 76public: 77 /* 78 * DisplayEventReceiver creates and registers an event connection with 79 * SurfaceFlinger. VSync events are disabled by default. Call setVSyncRate 80 * or requestNextVsync to receive them. 81 * Other events start being delivered immediately. 82 */ 83 DisplayEventReceiver(); 84 85 /* 86 * ~DisplayEventReceiver severs the connection with SurfaceFlinger, new events 87 * stop being delivered immediately. Note that the queue could have 88 * some events pending. These will be delivered. 89 */ 90 ~DisplayEventReceiver(); 91 92 /* 93 * initCheck returns the state of DisplayEventReceiver after construction. 94 */ 95 status_t initCheck() const; 96 97 /* 98 * getFd returns the file descriptor to use to receive events. 99 * OWNERSHIP IS RETAINED by DisplayEventReceiver. DO NOT CLOSE this 100 * file-descriptor. 101 */ 102 int getFd() const; 103 104 /* 105 * getEvents reads events from the queue and returns how many events were 106 * read. Returns 0 if there are no more events or a negative error code. 107 * If NOT_ENOUGH_DATA is returned, the object has become invalid forever, it 108 * should be destroyed and getEvents() shouldn't be called again. 109 */ 110 ssize_t getEvents(Event* events, size_t count); 111 static ssize_t getEvents(const sp<BitTube>& dataChannel, 112 Event* events, size_t count); 113 114 /* 115 * sendEvents write events to the queue and returns how many events were 116 * written. 117 */ 118 static ssize_t sendEvents(const sp<BitTube>& dataChannel, 119 Event const* events, size_t count); 120 121 /* 122 * setVsyncRate() sets the Event::VSync delivery rate. A value of 123 * 1 returns every Event::VSync. A value of 2 returns every other event, 124 * etc... a value of 0 returns no event unless requestNextVsync() has 125 * been called. 126 */ 127 status_t setVsyncRate(uint32_t count); 128 129 /* 130 * requestNextVsync() schedules the next Event::VSync. It has no effect 131 * if the vsync rate is > 0. 132 */ 133 status_t requestNextVsync(); 134 135private: 136 sp<IDisplayEventConnection> mEventConnection; 137 sp<BitTube> mDataChannel; 138}; 139 140// ---------------------------------------------------------------------------- 141}; // namespace android 142 143#endif // ANDROID_GUI_DISPLAY_EVENT_H 144