blob: af0da0bdb22cb87a1166bff7c6ff3f1401a85bf6 [file] [log] [blame]
Mathias Agopiand0566bc2011-11-17 17:49:17 -08001/*
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
32namespace android {
33
34// ---------------------------------------------------------------------------
35
36EventThread::EventThread(const sp<SurfaceFlinger>& flinger)
37 : mFlinger(flinger),
38 mHw(flinger->graphicPlane(0).displayHardware()),
Mathias Agopian8aedd472012-01-24 16:39:14 -080039 mLastVSyncTimestamp(0),
Mathias Agopiand0566bc2011-11-17 17:49:17 -080040 mDeliveredEvents(0)
41{
42}
43
44void EventThread::onFirstRef() {
45 run("EventThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
46}
47
Mathias Agopian8aedd472012-01-24 16:39:14 -080048sp<DisplayEventConnection> EventThread::createEventConnection() const {
49 return new DisplayEventConnection(const_cast<EventThread*>(this));
50}
51
52nsecs_t EventThread::getLastVSyncTimestamp() const {
53 Mutex::Autolock _l(mLock);
54 return mLastVSyncTimestamp;
55}
56
57nsecs_t EventThread::getVSyncPeriod() const {
58 return mHw.getRefreshPeriod();
59
60}
61
Mathias Agopiand0566bc2011-11-17 17:49:17 -080062status_t EventThread::registerDisplayEventConnection(
63 const sp<DisplayEventConnection>& connection) {
64 Mutex::Autolock _l(mLock);
Mathias Agopian478ae5e2011-12-06 17:22:19 -080065 ConnectionInfo info;
66 mDisplayEventConnections.add(connection, info);
Mathias Agopiand0566bc2011-11-17 17:49:17 -080067 mCondition.signal();
68 return NO_ERROR;
69}
70
71status_t EventThread::unregisterDisplayEventConnection(
72 const wp<DisplayEventConnection>& connection) {
73 Mutex::Autolock _l(mLock);
Mathias Agopian478ae5e2011-12-06 17:22:19 -080074 mDisplayEventConnections.removeItem(connection);
Mathias Agopiand0566bc2011-11-17 17:49:17 -080075 mCondition.signal();
76 return NO_ERROR;
77}
78
Mathias Agopian478ae5e2011-12-06 17:22:19 -080079void EventThread::removeDisplayEventConnection(
Mathias Agopian23748662011-12-05 14:33:34 -080080 const wp<DisplayEventConnection>& connection) {
81 Mutex::Autolock _l(mLock);
Mathias Agopian478ae5e2011-12-06 17:22:19 -080082 mDisplayEventConnections.removeItem(connection);
83}
84
85EventThread::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
92void 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 Agopian8aedd472012-01-24 16:39:14 -080098 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 Agopian478ae5e2011-12-06 17:22:19 -0800103 }
104 }
105}
106
107void EventThread::requestNextVsync(
108 const wp<DisplayEventConnection>& connection) {
109 Mutex::Autolock _l(mLock);
110 ConnectionInfo* info = getConnectionInfoLocked(connection);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800111 if (info && info->count < 0) {
112 info->count = 0;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800113 mCondition.signal();
114 }
Mathias Agopian23748662011-12-05 14:33:34 -0800115}
116
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800117bool EventThread::threadLoop() {
118
119 nsecs_t timestamp;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800120 DisplayEventReceiver::Event vsync;
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800121 Vector< wp<DisplayEventConnection> > displayEventConnections;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800122
Mathias Agopian23748662011-12-05 14:33:34 -0800123 { // scope for the lock
124 Mutex::Autolock _l(mLock);
125 do {
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800126 // see if we need to wait for the VSYNC at all
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800127 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 Agopiana72d0db2012-01-09 18:19:18 -0800133 if (info.count >= 0) {
134 // at least one continuous mode or active one-shot event
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800135 waitForNextVsync = true;
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800136 break;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800137 }
138 }
139
140 if (waitForNextVsync)
141 break;
142
Mathias Agopian23748662011-12-05 14:33:34 -0800143 mCondition.wait(mLock);
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800144 } while(true);
Mathias Agopian23748662011-12-05 14:33:34 -0800145
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800146 // at least one listener requested VSYNC
Mathias Agopian23748662011-12-05 14:33:34 -0800147 mLock.unlock();
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800148 timestamp = mHw.waitForRefresh();
Mathias Agopian23748662011-12-05 14:33:34 -0800149 mLock.lock();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800150 mDeliveredEvents++;
Mathias Agopian8aedd472012-01-24 16:39:14 -0800151 mLastVSyncTimestamp = timestamp;
Mathias Agopian23748662011-12-05 14:33:34 -0800152
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800153 // now see if we still need to report this VSYNC event
Mathias Agopian3cf199a2012-01-31 16:42:54 -0800154 const size_t count = mDisplayEventConnections.size();
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800155 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian3cf199a2012-01-31 16:42:54 -0800156 bool reportVsync = false;
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800157 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 Agopian616c0cd2012-01-12 16:13:54 -0800173 if (reportVsync) {
174 displayEventConnections.add(mDisplayEventConnections.keyAt(i));
175 }
Mathias Agopiana72d0db2012-01-09 18:19:18 -0800176 }
Mathias Agopian3cf199a2012-01-31 16:42:54 -0800177 } while (!displayEventConnections.size());
Mathias Agopian23748662011-12-05 14:33:34 -0800178
Mathias Agopian23748662011-12-05 14:33:34 -0800179 // dispatch vsync events to listeners...
Mathias Agopian23748662011-12-05 14:33:34 -0800180 vsync.header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
181 vsync.header.timestamp = timestamp;
182 vsync.vsync.count = mDeliveredEvents;
Mathias Agopian23748662011-12-05 14:33:34 -0800183 }
184
185 const size_t count = displayEventConnections.size();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800186 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800187 sp<DisplayEventConnection> conn(displayEventConnections[i].promote());
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800188 // make sure the connection didn't die
189 if (conn != NULL) {
190 status_t err = conn->postEvent(vsync);
191 if (err == -EAGAIN || err == -EWOULDBLOCK) {
192 // The destination doesn't accept events anymore, it's probably
193 // full. For now, we just drop the events on the floor.
194 // Note that some events cannot be dropped and would have to be
195 // re-sent later. Right-now we don't have the ability to do
196 // this, but it doesn't matter for VSYNC.
197 } else if (err < 0) {
198 // handle any other error on the pipe as fatal. the only
199 // reasonable thing to do is to clean-up this connection.
200 // The most common error we'll get here is -EPIPE.
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800201 removeDisplayEventConnection(displayEventConnections[i]);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800202 }
Mathias Agopian23748662011-12-05 14:33:34 -0800203 } else {
204 // somehow the connection is dead, but we still have it in our list
205 // just clean the list.
Mathias Agopian616c0cd2012-01-12 16:13:54 -0800206 removeDisplayEventConnection(displayEventConnections[i]);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800207 }
208 }
209
Mathias Agopian23748662011-12-05 14:33:34 -0800210 // clear all our references without holding mLock
211 displayEventConnections.clear();
212
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800213 return true;
214}
215
216status_t EventThread::readyToRun() {
Steve Blocka19954a2012-01-04 20:05:49 +0000217 ALOGI("EventThread ready to run.");
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800218 return NO_ERROR;
219}
220
221void EventThread::dump(String8& result, char* buffer, size_t SIZE) const {
222 Mutex::Autolock _l(mLock);
223 result.append("VSYNC state:\n");
224 snprintf(buffer, SIZE, " numListeners=%u, events-delivered: %u\n",
225 mDisplayEventConnections.size(), mDeliveredEvents);
226 result.append(buffer);
227}
228
229// ---------------------------------------------------------------------------
230
231}; // namespace android