| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1 | #include "vsync_service.h" | 
|  | 2 |  | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 3 | #include <hardware/hwcomposer.h> | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 4 | #include <log/log.h> | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 5 | #include <poll.h> | 
|  | 6 | #include <sys/prctl.h> | 
|  | 7 | #include <time.h> | 
|  | 8 | #include <utils/Trace.h> | 
|  | 9 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 10 | #include <dvr/dvr_display_types.h> | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 11 | #include <pdx/default_transport/service_endpoint.h> | 
|  | 12 | #include <private/dvr/clock_ns.h> | 
| Corey Tabaka | 3f82d31 | 2017-04-20 14:42:08 -0700 | [diff] [blame] | 13 | #include <private/dvr/display_protocol.h> | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 14 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 15 | using android::dvr::display::VSyncProtocol; | 
|  | 16 | using android::dvr::display::VSyncSchedInfo; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 17 | using android::pdx::Channel; | 
|  | 18 | using android::pdx::Message; | 
|  | 19 | using android::pdx::MessageInfo; | 
|  | 20 | using android::pdx::default_transport::Endpoint; | 
|  | 21 | using android::pdx::rpc::DispatchRemoteMethod; | 
|  | 22 |  | 
|  | 23 | namespace android { | 
|  | 24 | namespace dvr { | 
|  | 25 |  | 
|  | 26 | VSyncService::VSyncService() | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 27 | : BASE("VSyncService", Endpoint::Create(VSyncProtocol::kClientPath)), | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 28 | last_vsync_(0), | 
|  | 29 | current_vsync_(0), | 
|  | 30 | compositor_time_ns_(0), | 
|  | 31 | current_vsync_count_(0) {} | 
|  | 32 |  | 
|  | 33 | VSyncService::~VSyncService() {} | 
|  | 34 |  | 
|  | 35 | void VSyncService::VSyncEvent(int display, int64_t timestamp_ns, | 
|  | 36 | int64_t compositor_time_ns, | 
|  | 37 | uint32_t vsync_count) { | 
|  | 38 | ATRACE_NAME("VSyncService::VSyncEvent"); | 
|  | 39 | std::lock_guard<std::mutex> autolock(mutex_); | 
|  | 40 |  | 
|  | 41 | if (display == HWC_DISPLAY_PRIMARY) { | 
|  | 42 | last_vsync_ = current_vsync_; | 
|  | 43 | current_vsync_ = timestamp_ns; | 
|  | 44 | compositor_time_ns_ = compositor_time_ns; | 
|  | 45 | current_vsync_count_ = vsync_count; | 
|  | 46 |  | 
|  | 47 | NotifyWaiters(); | 
|  | 48 | UpdateClients(); | 
|  | 49 | } | 
|  | 50 | } | 
|  | 51 |  | 
|  | 52 | std::shared_ptr<Channel> VSyncService::OnChannelOpen(pdx::Message& message) { | 
|  | 53 | const MessageInfo& info = message.GetInfo(); | 
|  | 54 |  | 
|  | 55 | auto client = std::make_shared<VSyncChannel>(*this, info.pid, info.cid); | 
|  | 56 | AddClient(client); | 
|  | 57 |  | 
|  | 58 | return client; | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | void VSyncService::OnChannelClose(pdx::Message& /*message*/, | 
|  | 62 | const std::shared_ptr<Channel>& channel) { | 
|  | 63 | auto client = std::static_pointer_cast<VSyncChannel>(channel); | 
|  | 64 | if (!client) { | 
|  | 65 | ALOGW("WARNING: VSyncChannel was NULL!!!\n"); | 
|  | 66 | return; | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | RemoveClient(client); | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | void VSyncService::AddWaiter(pdx::Message& message) { | 
|  | 73 | std::lock_guard<std::mutex> autolock(mutex_); | 
|  | 74 | std::unique_ptr<VSyncWaiter> waiter(new VSyncWaiter(message)); | 
|  | 75 | waiters_.push_back(std::move(waiter)); | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | void VSyncService::AddClient(const std::shared_ptr<VSyncChannel>& client) { | 
|  | 79 | std::lock_guard<std::mutex> autolock(mutex_); | 
|  | 80 | clients_.push_back(client); | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | void VSyncService::RemoveClient(const std::shared_ptr<VSyncChannel>& client) { | 
|  | 84 | std::lock_guard<std::mutex> autolock(mutex_); | 
|  | 85 | clients_.remove(client); | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | // Private. Assumes mutex is held. | 
|  | 89 | void VSyncService::NotifyWaiters() { | 
|  | 90 | ATRACE_NAME("VSyncService::NotifyWaiters"); | 
|  | 91 | auto first = waiters_.begin(); | 
|  | 92 | auto last = waiters_.end(); | 
|  | 93 |  | 
|  | 94 | while (first != last) { | 
|  | 95 | (*first)->Notify(current_vsync_); | 
|  | 96 | waiters_.erase(first++); | 
|  | 97 | } | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | // Private. Assumes mutex is held. | 
|  | 101 | void VSyncService::UpdateClients() { | 
|  | 102 | ATRACE_NAME("VSyncService::UpdateClients"); | 
|  | 103 | auto first = clients_.begin(); | 
|  | 104 | auto last = clients_.end(); | 
|  | 105 |  | 
|  | 106 | while (first != last) { | 
|  | 107 | (*first)->Signal(); | 
|  | 108 | first++; | 
|  | 109 | } | 
|  | 110 | } | 
|  | 111 |  | 
| Alex Vakulenko | f0a7bd0 | 2017-03-31 18:06:19 -0700 | [diff] [blame] | 112 | pdx::Status<void> VSyncService::HandleMessage(pdx::Message& message) { | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 113 | switch (message.GetOp()) { | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 114 | case VSyncProtocol::Wait::Opcode: | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 115 | AddWaiter(message); | 
| Alex Vakulenko | f0a7bd0 | 2017-03-31 18:06:19 -0700 | [diff] [blame] | 116 | return {}; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 117 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 118 | case VSyncProtocol::GetLastTimestamp::Opcode: | 
|  | 119 | DispatchRemoteMethod<VSyncProtocol::GetLastTimestamp>( | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 120 | *this, &VSyncService::OnGetLastTimestamp, message); | 
| Alex Vakulenko | f0a7bd0 | 2017-03-31 18:06:19 -0700 | [diff] [blame] | 121 | return {}; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 122 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 123 | case VSyncProtocol::GetSchedInfo::Opcode: | 
|  | 124 | DispatchRemoteMethod<VSyncProtocol::GetSchedInfo>( | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 125 | *this, &VSyncService::OnGetSchedInfo, message); | 
| Alex Vakulenko | f0a7bd0 | 2017-03-31 18:06:19 -0700 | [diff] [blame] | 126 | return {}; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 127 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 128 | case VSyncProtocol::Acknowledge::Opcode: | 
|  | 129 | DispatchRemoteMethod<VSyncProtocol::Acknowledge>( | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 130 | *this, &VSyncService::OnAcknowledge, message); | 
| Alex Vakulenko | f0a7bd0 | 2017-03-31 18:06:19 -0700 | [diff] [blame] | 131 | return {}; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 132 |  | 
|  | 133 | default: | 
|  | 134 | return Service::HandleMessage(message); | 
|  | 135 | } | 
|  | 136 | } | 
|  | 137 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 138 | pdx::Status<int64_t> VSyncService::OnGetLastTimestamp(pdx::Message& message) { | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 139 | auto client = std::static_pointer_cast<VSyncChannel>(message.GetChannel()); | 
|  | 140 | std::lock_guard<std::mutex> autolock(mutex_); | 
|  | 141 |  | 
|  | 142 | // Getting the timestamp has the side effect of ACKing. | 
|  | 143 | client->Ack(); | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 144 | return {current_vsync_}; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 145 | } | 
|  | 146 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 147 | pdx::Status<VSyncSchedInfo> VSyncService::OnGetSchedInfo( | 
|  | 148 | pdx::Message& message) { | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 149 | auto client = std::static_pointer_cast<VSyncChannel>(message.GetChannel()); | 
|  | 150 | std::lock_guard<std::mutex> autolock(mutex_); | 
|  | 151 |  | 
|  | 152 | // Getting the timestamp has the side effect of ACKing. | 
|  | 153 | client->Ack(); | 
|  | 154 |  | 
|  | 155 | uint32_t next_vsync_count = current_vsync_count_ + 1; | 
|  | 156 | int64_t current_time = GetSystemClockNs(); | 
|  | 157 | int64_t vsync_period_ns = 0; | 
|  | 158 | int64_t next_warp; | 
|  | 159 | if (current_vsync_ == 0 || last_vsync_ == 0) { | 
|  | 160 | // Handle startup when current_vsync_ or last_vsync_ are 0. | 
|  | 161 | // Normally should not happen because vsync_service is running before | 
|  | 162 | // applications, but in case it does a sane time prevents applications | 
|  | 163 | // from malfunctioning. | 
|  | 164 | vsync_period_ns = 20000000; | 
|  | 165 | next_warp = current_time; | 
|  | 166 | } else { | 
|  | 167 | // TODO(jbates) When we have an accurate reading of the true vsync | 
|  | 168 | // period, use that instead of this estimated value. | 
|  | 169 | vsync_period_ns = current_vsync_ - last_vsync_; | 
|  | 170 | // Clamp the period, because when there are no surfaces the last_vsync_ | 
|  | 171 | // value will get stale. Note this is temporary and goes away as soon | 
|  | 172 | // as we have an accurate vsync period reported by the system. | 
|  | 173 | vsync_period_ns = std::min(vsync_period_ns, INT64_C(20000000)); | 
|  | 174 | next_warp = current_vsync_ + vsync_period_ns - compositor_time_ns_; | 
|  | 175 | // If the request missed the present window, move up to the next vsync. | 
|  | 176 | if (current_time > next_warp) { | 
|  | 177 | next_warp += vsync_period_ns; | 
|  | 178 | ++next_vsync_count; | 
|  | 179 | } | 
|  | 180 | } | 
|  | 181 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 182 | return {{vsync_period_ns, next_warp, next_vsync_count}}; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 183 | } | 
|  | 184 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 185 | pdx::Status<void> VSyncService::OnAcknowledge(pdx::Message& message) { | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 186 | auto client = std::static_pointer_cast<VSyncChannel>(message.GetChannel()); | 
|  | 187 | std::lock_guard<std::mutex> autolock(mutex_); | 
|  | 188 | client->Ack(); | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 189 | return {}; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 190 | } | 
|  | 191 |  | 
|  | 192 | void VSyncWaiter::Notify(int64_t timestamp) { | 
|  | 193 | timestamp_ = timestamp; | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 194 | DispatchRemoteMethod<VSyncProtocol::Wait>(*this, &VSyncWaiter::OnWait, | 
|  | 195 | message_); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 196 | } | 
|  | 197 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 198 | pdx::Status<int64_t> VSyncWaiter::OnWait(pdx::Message& /*message*/) { | 
|  | 199 | return {timestamp_}; | 
|  | 200 | } | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 201 |  | 
|  | 202 | void VSyncChannel::Ack() { | 
| Corey Tabaka | 89bbefc | 2017-06-06 16:14:21 -0700 | [diff] [blame] | 203 | ALOGD_IF(TRACE > 1, "VSyncChannel::Ack: pid=%d cid=%d\n", pid_, cid_); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 204 | service_.ModifyChannelEvents(cid_, POLLPRI, 0); | 
|  | 205 | } | 
|  | 206 |  | 
|  | 207 | void VSyncChannel::Signal() { | 
| Corey Tabaka | 89bbefc | 2017-06-06 16:14:21 -0700 | [diff] [blame] | 208 | ALOGD_IF(TRACE > 1, "VSyncChannel::Signal: pid=%d cid=%d\n", pid_, cid_); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 209 | service_.ModifyChannelEvents(cid_, 0, POLLPRI); | 
|  | 210 | } | 
|  | 211 |  | 
|  | 212 | }  // namespace dvr | 
|  | 213 | }  // namespace android |