Mathias Agopian | 79f39eb | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 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 | #include <stdint.h> |
| 18 | #include <sys/types.h> |
| 19 | |
| 20 | #include <gui/IDisplayEventConnection.h> |
| 21 | #include <gui/DisplayEventReceiver.h> |
| 22 | |
| 23 | #include <utils/Errors.h> |
| 24 | |
| 25 | #include "DisplayHardware/DisplayHardware.h" |
| 26 | #include "DisplayEventConnection.h" |
| 27 | #include "EventThread.h" |
| 28 | #include "SurfaceFlinger.h" |
| 29 | |
| 30 | // --------------------------------------------------------------------------- |
| 31 | |
| 32 | namespace android { |
| 33 | |
| 34 | // --------------------------------------------------------------------------- |
| 35 | |
| 36 | EventThread::EventThread(const sp<SurfaceFlinger>& flinger) |
| 37 | : mFlinger(flinger), |
| 38 | mHw(flinger->graphicPlane(0).displayHardware()), |
Mathias Agopian | e02f7b3 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 39 | mLastVSyncTimestamp(0), |
Mathias Agopian | 79f39eb | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 40 | mDeliveredEvents(0) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | void EventThread::onFirstRef() { |
| 45 | run("EventThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE); |
| 46 | } |
| 47 | |
Mathias Agopian | e02f7b3 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 48 | sp<DisplayEventConnection> EventThread::createEventConnection() const { |
| 49 | return new DisplayEventConnection(const_cast<EventThread*>(this)); |
| 50 | } |
| 51 | |
| 52 | nsecs_t EventThread::getLastVSyncTimestamp() const { |
| 53 | Mutex::Autolock _l(mLock); |
| 54 | return mLastVSyncTimestamp; |
| 55 | } |
| 56 | |
| 57 | nsecs_t EventThread::getVSyncPeriod() const { |
| 58 | return mHw.getRefreshPeriod(); |
| 59 | |
| 60 | } |
| 61 | |
Mathias Agopian | 79f39eb | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 62 | status_t EventThread::registerDisplayEventConnection( |
| 63 | const sp<DisplayEventConnection>& connection) { |
| 64 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 6779df2 | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 65 | ConnectionInfo info; |
| 66 | mDisplayEventConnections.add(connection, info); |
Mathias Agopian | 79f39eb | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 67 | mCondition.signal(); |
| 68 | return NO_ERROR; |
| 69 | } |
| 70 | |
| 71 | status_t EventThread::unregisterDisplayEventConnection( |
| 72 | const wp<DisplayEventConnection>& connection) { |
| 73 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 6779df2 | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 74 | mDisplayEventConnections.removeItem(connection); |
Mathias Agopian | 79f39eb | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 75 | mCondition.signal(); |
| 76 | return NO_ERROR; |
| 77 | } |
| 78 | |
Mathias Agopian | 6779df2 | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 79 | void EventThread::removeDisplayEventConnection( |
Mathias Agopian | 2871ce9 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 80 | const wp<DisplayEventConnection>& connection) { |
| 81 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 6779df2 | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 82 | mDisplayEventConnections.removeItem(connection); |
| 83 | } |
| 84 | |
| 85 | EventThread::ConnectionInfo* EventThread::getConnectionInfoLocked( |
| 86 | const wp<DisplayEventConnection>& connection) { |
| 87 | ssize_t index = mDisplayEventConnections.indexOfKey(connection); |
| 88 | if (index < 0) return NULL; |
| 89 | return &mDisplayEventConnections.editValueAt(index); |
| 90 | } |
| 91 | |
| 92 | void EventThread::setVsyncRate(uint32_t count, |
| 93 | const wp<DisplayEventConnection>& connection) { |
| 94 | if (int32_t(count) >= 0) { // server must protect against bad params |
| 95 | Mutex::Autolock _l(mLock); |
| 96 | ConnectionInfo* info = getConnectionInfoLocked(connection); |
| 97 | if (info) { |
Mathias Agopian | e02f7b3 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 98 | const int32_t new_count = (count == 0) ? -1 : count; |
| 99 | if (info->count != new_count) { |
| 100 | info->count = new_count; |
| 101 | mCondition.signal(); |
| 102 | } |
Mathias Agopian | 6779df2 | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void EventThread::requestNextVsync( |
| 108 | const wp<DisplayEventConnection>& connection) { |
| 109 | Mutex::Autolock _l(mLock); |
| 110 | ConnectionInfo* info = getConnectionInfoLocked(connection); |
Mathias Agopian | e02f7b3 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 111 | if (info && info->count < 0) { |
| 112 | info->count = 0; |
Mathias Agopian | 6779df2 | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 113 | mCondition.signal(); |
| 114 | } |
Mathias Agopian | 2871ce9 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Mathias Agopian | 79f39eb | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 117 | bool EventThread::threadLoop() { |
| 118 | |
| 119 | nsecs_t timestamp; |
Mathias Agopian | 79f39eb | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 120 | DisplayEventReceiver::Event vsync; |
Mathias Agopian | 87c6ea6 | 2012-01-12 16:13:54 -0800 | [diff] [blame] | 121 | Vector< wp<DisplayEventConnection> > displayEventConnections; |
Mathias Agopian | 79f39eb | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 122 | |
Mathias Agopian | 2871ce9 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 123 | { // scope for the lock |
| 124 | Mutex::Autolock _l(mLock); |
| 125 | do { |
Mathias Agopian | 7e9a370 | 2012-01-09 18:19:18 -0800 | [diff] [blame] | 126 | // see if we need to wait for the VSYNC at all |
Mathias Agopian | 6779df2 | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 127 | do { |
| 128 | bool waitForNextVsync = false; |
| 129 | size_t count = mDisplayEventConnections.size(); |
| 130 | for (size_t i=0 ; i<count ; i++) { |
| 131 | const ConnectionInfo& info( |
| 132 | mDisplayEventConnections.valueAt(i)); |
Mathias Agopian | 7e9a370 | 2012-01-09 18:19:18 -0800 | [diff] [blame] | 133 | if (info.count >= 0) { |
| 134 | // at least one continuous mode or active one-shot event |
Mathias Agopian | 6779df2 | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 135 | waitForNextVsync = true; |
Mathias Agopian | 7e9a370 | 2012-01-09 18:19:18 -0800 | [diff] [blame] | 136 | break; |
Mathias Agopian | 6779df2 | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | if (waitForNextVsync) |
| 141 | break; |
| 142 | |
Mathias Agopian | 2871ce9 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 143 | mCondition.wait(mLock); |
Mathias Agopian | 6779df2 | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 144 | } while(true); |
Mathias Agopian | 2871ce9 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 145 | |
Mathias Agopian | 7e9a370 | 2012-01-09 18:19:18 -0800 | [diff] [blame] | 146 | // at least one listener requested VSYNC |
Mathias Agopian | 2871ce9 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 147 | mLock.unlock(); |
Mathias Agopian | 1d99795 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 148 | timestamp = mHw.waitForRefresh(); |
Mathias Agopian | 2871ce9 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 149 | mLock.lock(); |
Mathias Agopian | 6779df2 | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 150 | mDeliveredEvents++; |
Mathias Agopian | e02f7b3 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 151 | mLastVSyncTimestamp = timestamp; |
Mathias Agopian | 2871ce9 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 152 | |
Mathias Agopian | 7e9a370 | 2012-01-09 18:19:18 -0800 | [diff] [blame] | 153 | // now see if we still need to report this VSYNC event |
| 154 | bool reportVsync = false; |
| 155 | size_t count = mDisplayEventConnections.size(); |
| 156 | for (size_t i=0 ; i<count ; i++) { |
| 157 | const ConnectionInfo& info( |
| 158 | mDisplayEventConnections.valueAt(i)); |
| 159 | if (info.count >= 1) { |
| 160 | if (info.count==1 || (mDeliveredEvents % info.count) == 0) { |
| 161 | // continuous event, and time to report it |
| 162 | reportVsync = true; |
| 163 | } |
| 164 | } else if (info.count >= -1) { |
| 165 | ConnectionInfo& info( |
| 166 | mDisplayEventConnections.editValueAt(i)); |
| 167 | if (info.count == 0) { |
| 168 | // fired this time around |
| 169 | reportVsync = true; |
| 170 | } |
| 171 | info.count--; |
| 172 | } |
Mathias Agopian | 87c6ea6 | 2012-01-12 16:13:54 -0800 | [diff] [blame] | 173 | if (reportVsync) { |
| 174 | displayEventConnections.add(mDisplayEventConnections.keyAt(i)); |
| 175 | } |
Mathias Agopian | 7e9a370 | 2012-01-09 18:19:18 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | if (reportVsync) { |
| 179 | break; |
| 180 | } |
| 181 | } while (true); |
Mathias Agopian | 2871ce9 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 182 | |
Mathias Agopian | 2871ce9 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 183 | // dispatch vsync events to listeners... |
Mathias Agopian | 2871ce9 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 184 | vsync.header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC; |
| 185 | vsync.header.timestamp = timestamp; |
| 186 | vsync.vsync.count = mDeliveredEvents; |
Mathias Agopian | 2871ce9 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | const size_t count = displayEventConnections.size(); |
Mathias Agopian | 79f39eb | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 190 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | 87c6ea6 | 2012-01-12 16:13:54 -0800 | [diff] [blame] | 191 | sp<DisplayEventConnection> conn(displayEventConnections[i].promote()); |
Mathias Agopian | 79f39eb | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 192 | // make sure the connection didn't die |
| 193 | if (conn != NULL) { |
| 194 | status_t err = conn->postEvent(vsync); |
| 195 | if (err == -EAGAIN || err == -EWOULDBLOCK) { |
| 196 | // The destination doesn't accept events anymore, it's probably |
| 197 | // full. For now, we just drop the events on the floor. |
| 198 | // Note that some events cannot be dropped and would have to be |
| 199 | // re-sent later. Right-now we don't have the ability to do |
| 200 | // this, but it doesn't matter for VSYNC. |
| 201 | } else if (err < 0) { |
| 202 | // handle any other error on the pipe as fatal. the only |
| 203 | // reasonable thing to do is to clean-up this connection. |
| 204 | // The most common error we'll get here is -EPIPE. |
Mathias Agopian | 87c6ea6 | 2012-01-12 16:13:54 -0800 | [diff] [blame] | 205 | removeDisplayEventConnection(displayEventConnections[i]); |
Mathias Agopian | 79f39eb | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 206 | } |
Mathias Agopian | 2871ce9 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 207 | } else { |
| 208 | // somehow the connection is dead, but we still have it in our list |
| 209 | // just clean the list. |
Mathias Agopian | 87c6ea6 | 2012-01-12 16:13:54 -0800 | [diff] [blame] | 210 | removeDisplayEventConnection(displayEventConnections[i]); |
Mathias Agopian | 79f39eb | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Mathias Agopian | 2871ce9 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 214 | // clear all our references without holding mLock |
| 215 | displayEventConnections.clear(); |
| 216 | |
Mathias Agopian | 79f39eb | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 217 | return true; |
| 218 | } |
| 219 | |
| 220 | status_t EventThread::readyToRun() { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 221 | ALOGI("EventThread ready to run."); |
Mathias Agopian | 79f39eb | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 222 | return NO_ERROR; |
| 223 | } |
| 224 | |
| 225 | void EventThread::dump(String8& result, char* buffer, size_t SIZE) const { |
| 226 | Mutex::Autolock _l(mLock); |
| 227 | result.append("VSYNC state:\n"); |
| 228 | snprintf(buffer, SIZE, " numListeners=%u, events-delivered: %u\n", |
| 229 | mDisplayEventConnections.size(), mDeliveredEvents); |
| 230 | result.append(buffer); |
| 231 | } |
| 232 | |
| 233 | // --------------------------------------------------------------------------- |
| 234 | |
| 235 | }; // namespace android |