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