blob: a17e8acc577cf45a7aa231285a5ac97a07390e9f [file] [log] [blame]
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001/*
2 * Copyright (C) 2007 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#define LOG_TAG "SharedBufferStack"
18
19#include <stdint.h>
20#include <sys/types.h>
21
22#include <utils/Debug.h>
23#include <utils/Log.h>
24#include <utils/threads.h>
25
Mathias Agopian9cce3252010-02-09 17:46:37 -080026#include <private/surfaceflinger/SharedBufferStack.h>
Mathias Agopiancbb288b2009-09-07 16:32:45 -070027
28#include <ui/Rect.h>
29#include <ui/Region.h>
30
31#define DEBUG_ATOMICS 0
32
33namespace android {
34// ----------------------------------------------------------------------------
35
36SharedClient::SharedClient()
Mathias Agopian26d24422010-03-19 16:14:13 -070037 : lock(Mutex::SHARED), cv(Condition::SHARED)
Mathias Agopiancbb288b2009-09-07 16:32:45 -070038{
39}
40
41SharedClient::~SharedClient() {
42}
43
44
45// these functions are used by the clients
46status_t SharedClient::validate(size_t i) const {
47 if (uint32_t(i) >= uint32_t(NUM_LAYERS_MAX))
48 return BAD_INDEX;
49 return surfaces[i].status;
50}
51
52uint32_t SharedClient::getIdentity(size_t token) const {
53 return uint32_t(surfaces[token].identity);
54}
55
Mathias Agopiancbb288b2009-09-07 16:32:45 -070056// ----------------------------------------------------------------------------
57
58
59SharedBufferStack::SharedBufferStack()
Mathias Agopiancbb288b2009-09-07 16:32:45 -070060{
61}
62
Mathias Agopian48d819a2009-09-10 19:41:18 -070063void SharedBufferStack::init(int32_t i)
64{
65 inUse = -1;
66 status = NO_ERROR;
67 identity = i;
68}
69
Mathias Agopiancbb288b2009-09-07 16:32:45 -070070status_t SharedBufferStack::setDirtyRegion(int buffer, const Region& dirty)
71{
72 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
73 return BAD_INDEX;
74
75 // in the current implementation we only send a single rectangle
76 const Rect bounds(dirty.getBounds());
77 FlatRegion& reg(dirtyRegion[buffer]);
78 reg.count = 1;
79 reg.rects[0] = uint16_t(bounds.left);
80 reg.rects[1] = uint16_t(bounds.top);
81 reg.rects[2] = uint16_t(bounds.right);
82 reg.rects[3] = uint16_t(bounds.bottom);
83 return NO_ERROR;
84}
85
86Region SharedBufferStack::getDirtyRegion(int buffer) const
87{
88 Region res;
89 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
90 return res;
91
92 const FlatRegion& reg(dirtyRegion[buffer]);
93 res.set(Rect(reg.rects[0], reg.rects[1], reg.rects[2], reg.rects[3]));
94 return res;
95}
96
97// ----------------------------------------------------------------------------
98
99SharedBufferBase::SharedBufferBase(SharedClient* sharedClient,
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700100 int surface, int num, int32_t identity)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700101 : mSharedClient(sharedClient),
102 mSharedStack(sharedClient->surfaces + surface),
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700103 mNumBuffers(num), mIdentity(identity)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700104{
105}
106
107SharedBufferBase::~SharedBufferBase()
108{
109}
110
111uint32_t SharedBufferBase::getIdentity()
112{
113 SharedBufferStack& stack( *mSharedStack );
114 return stack.identity;
115}
116
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700117status_t SharedBufferBase::getStatus() const
118{
119 SharedBufferStack& stack( *mSharedStack );
120 return stack.status;
121}
122
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700123size_t SharedBufferBase::getFrontBuffer() const
124{
125 SharedBufferStack& stack( *mSharedStack );
126 return size_t( stack.head );
127}
128
129String8 SharedBufferBase::dump(char const* prefix) const
130{
131 const size_t SIZE = 1024;
132 char buffer[SIZE];
133 String8 result;
134 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianc2e30de2010-03-08 19:23:26 -0800135 int tail = (mNumBuffers + stack.head - stack.available + 1) % mNumBuffers;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700136 snprintf(buffer, SIZE,
Mathias Agopianc2e30de2010-03-08 19:23:26 -0800137 "%s[ head=%2d, available=%2d, queued=%2d, tail=%2d ] "
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700138 "reallocMask=%08x, inUse=%2d, identity=%d, status=%d\n",
Mathias Agopianc2e30de2010-03-08 19:23:26 -0800139 prefix, stack.head, stack.available, stack.queued, tail,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700140 stack.reallocMask, stack.inUse, stack.identity, stack.status);
141 result.append(buffer);
142 return result;
143}
144
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700145// ============================================================================
146// conditions and updates
147// ============================================================================
148
149SharedBufferClient::DequeueCondition::DequeueCondition(
150 SharedBufferClient* sbc) : ConditionBase(sbc) {
151}
152bool SharedBufferClient::DequeueCondition::operator()() {
153 return stack.available > 0;
154}
155
156SharedBufferClient::LockCondition::LockCondition(
157 SharedBufferClient* sbc, int buf) : ConditionBase(sbc), buf(buf) {
158}
159bool SharedBufferClient::LockCondition::operator()() {
160 return (buf != stack.head ||
161 (stack.queued > 0 && stack.inUse != buf));
162}
163
164SharedBufferServer::ReallocateCondition::ReallocateCondition(
165 SharedBufferBase* sbb, int buf) : ConditionBase(sbb), buf(buf) {
166}
167bool SharedBufferServer::ReallocateCondition::operator()() {
168 // TODO: we should also check that buf has been dequeued
169 return (buf != stack.head);
170}
171
172// ----------------------------------------------------------------------------
173
174SharedBufferClient::QueueUpdate::QueueUpdate(SharedBufferBase* sbb)
175 : UpdateBase(sbb) {
176}
177ssize_t SharedBufferClient::QueueUpdate::operator()() {
178 android_atomic_inc(&stack.queued);
179 return NO_ERROR;
180}
181
182SharedBufferClient::UndoDequeueUpdate::UndoDequeueUpdate(SharedBufferBase* sbb)
183 : UpdateBase(sbb) {
184}
185ssize_t SharedBufferClient::UndoDequeueUpdate::operator()() {
186 android_atomic_inc(&stack.available);
187 return NO_ERROR;
188}
189
190SharedBufferServer::UnlockUpdate::UnlockUpdate(
191 SharedBufferBase* sbb, int lockedBuffer)
192 : UpdateBase(sbb), lockedBuffer(lockedBuffer) {
193}
194ssize_t SharedBufferServer::UnlockUpdate::operator()() {
195 if (stack.inUse != lockedBuffer) {
196 LOGE("unlocking %d, but currently locked buffer is %d",
197 lockedBuffer, stack.inUse);
198 return BAD_VALUE;
199 }
200 android_atomic_write(-1, &stack.inUse);
201 return NO_ERROR;
202}
203
204SharedBufferServer::RetireUpdate::RetireUpdate(
205 SharedBufferBase* sbb, int numBuffers)
206 : UpdateBase(sbb), numBuffers(numBuffers) {
207}
208ssize_t SharedBufferServer::RetireUpdate::operator()() {
209 // head is only written in this function, which is single-thread.
210 int32_t head = stack.head;
211
212 // Preventively lock the current buffer before updating queued.
213 android_atomic_write(head, &stack.inUse);
214
215 // Decrement the number of queued buffers
216 int32_t queued;
217 do {
218 queued = stack.queued;
219 if (queued == 0) {
220 return NOT_ENOUGH_DATA;
221 }
222 } while (android_atomic_cmpxchg(queued, queued-1, &stack.queued));
223
224 // update the head pointer
225 head = ((head+1 >= numBuffers) ? 0 : head+1);
226
227 // lock the buffer before advancing head, which automatically unlocks
228 // the buffer we preventively locked upon entering this function
229 android_atomic_write(head, &stack.inUse);
230
231 // advance head
232 android_atomic_write(head, &stack.head);
233
234 // now that head has moved, we can increment the number of available buffers
235 android_atomic_inc(&stack.available);
236 return head;
237}
238
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700239SharedBufferServer::StatusUpdate::StatusUpdate(
240 SharedBufferBase* sbb, status_t status)
241 : UpdateBase(sbb), status(status) {
242}
243
244ssize_t SharedBufferServer::StatusUpdate::operator()() {
245 android_atomic_write(status, &stack.status);
246 return NO_ERROR;
247}
248
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700249// ============================================================================
250
251SharedBufferClient::SharedBufferClient(SharedClient* sharedClient,
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700252 int surface, int num, int32_t identity)
253 : SharedBufferBase(sharedClient, surface, num, identity), tail(0)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700254{
Mathias Agopianc7d56012009-09-14 15:48:42 -0700255 tail = computeTail();
256}
257
258int32_t SharedBufferClient::computeTail() const
259{
Mathias Agopian40d57992009-09-11 19:18:20 -0700260 SharedBufferStack& stack( *mSharedStack );
Mathias Agopian40d57992009-09-11 19:18:20 -0700261 // we need to make sure we read available and head coherently,
262 // w.r.t RetireUpdate.
Mathias Agopianc7d56012009-09-14 15:48:42 -0700263 int32_t newTail;
264 int32_t avail;
265 int32_t head;
Mathias Agopian40d57992009-09-11 19:18:20 -0700266 do {
267 avail = stack.available;
268 head = stack.head;
269 } while (stack.available != avail);
Mathias Agopianc7d56012009-09-14 15:48:42 -0700270 newTail = head - avail + 1;
271 if (newTail < 0) {
272 newTail += mNumBuffers;
Mathias Agopianc2e30de2010-03-08 19:23:26 -0800273 } else if (newTail >= mNumBuffers) {
274 newTail -= mNumBuffers;
Mathias Agopian40d57992009-09-11 19:18:20 -0700275 }
Mathias Agopianc7d56012009-09-14 15:48:42 -0700276 return newTail;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700277}
278
279ssize_t SharedBufferClient::dequeue()
280{
Mathias Agopian40d57992009-09-11 19:18:20 -0700281 SharedBufferStack& stack( *mSharedStack );
282
283 if (stack.head == tail && stack.available == 2) {
284 LOGW("dequeue: tail=%d, head=%d, avail=%d, queued=%d",
285 tail, stack.head, stack.available, stack.queued);
286 }
Mathias Agopian86f73292009-09-17 01:35:28 -0700287
288 const nsecs_t dequeueTime = systemTime(SYSTEM_TIME_THREAD);
Mathias Agopian40d57992009-09-11 19:18:20 -0700289
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700290 //LOGD("[%d] about to dequeue a buffer",
291 // mSharedStack->identity);
292 DequeueCondition condition(this);
293 status_t err = waitForCondition(condition);
294 if (err != NO_ERROR)
295 return ssize_t(err);
296
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700297 // NOTE: 'stack.available' is part of the conditions, however
298 // decrementing it, never changes any conditions, so we don't need
299 // to do this as part of an update.
300 if (android_atomic_dec(&stack.available) == 0) {
301 LOGW("dequeue probably called from multiple threads!");
302 }
303
304 int dequeued = tail;
305 tail = ((tail+1 >= mNumBuffers) ? 0 : tail+1);
306 LOGD_IF(DEBUG_ATOMICS, "dequeued=%d, tail=%d, %s",
307 dequeued, tail, dump("").string());
Mathias Agopian40d57992009-09-11 19:18:20 -0700308
Mathias Agopian86f73292009-09-17 01:35:28 -0700309 mDequeueTime[dequeued] = dequeueTime;
310
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700311 return dequeued;
312}
313
314status_t SharedBufferClient::undoDequeue(int buf)
315{
316 UndoDequeueUpdate update(this);
317 status_t err = updateCondition( update );
Mathias Agopianc7d56012009-09-14 15:48:42 -0700318 if (err == NO_ERROR) {
319 tail = computeTail();
320 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700321 return err;
322}
323
324status_t SharedBufferClient::lock(int buf)
325{
326 LockCondition condition(this, buf);
Mathias Agopian86f73292009-09-17 01:35:28 -0700327 status_t err = waitForCondition(condition);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700328 return err;
329}
330
331status_t SharedBufferClient::queue(int buf)
332{
333 QueueUpdate update(this);
334 status_t err = updateCondition( update );
335 LOGD_IF(DEBUG_ATOMICS, "queued=%d, %s", buf, dump("").string());
Mathias Agopian86f73292009-09-17 01:35:28 -0700336 SharedBufferStack& stack( *mSharedStack );
337 const nsecs_t now = systemTime(SYSTEM_TIME_THREAD);
338 stack.stats.totalTime = ns2us(now - mDequeueTime[buf]);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700339 return err;
340}
341
342bool SharedBufferClient::needNewBuffer(int buffer) const
343{
344 SharedBufferStack& stack( *mSharedStack );
345 const uint32_t mask = 1<<buffer;
346 return (android_atomic_and(~mask, &stack.reallocMask) & mask) != 0;
347}
348
349status_t SharedBufferClient::setDirtyRegion(int buffer, const Region& reg)
350{
351 SharedBufferStack& stack( *mSharedStack );
352 return stack.setDirtyRegion(buffer, reg);
353}
354
355// ----------------------------------------------------------------------------
356
357SharedBufferServer::SharedBufferServer(SharedClient* sharedClient,
Mathias Agopian48d819a2009-09-10 19:41:18 -0700358 int surface, int num, int32_t identity)
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700359 : SharedBufferBase(sharedClient, surface, num, identity)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700360{
Mathias Agopian48d819a2009-09-10 19:41:18 -0700361 mSharedStack->init(identity);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700362 mSharedStack->head = num-1;
363 mSharedStack->available = num;
364 mSharedStack->queued = 0;
365 mSharedStack->reallocMask = 0;
366 memset(mSharedStack->dirtyRegion, 0, sizeof(mSharedStack->dirtyRegion));
367}
368
369ssize_t SharedBufferServer::retireAndLock()
370{
371 RetireUpdate update(this, mNumBuffers);
372 ssize_t buf = updateCondition( update );
Mathias Agopian40d57992009-09-11 19:18:20 -0700373 LOGD_IF(DEBUG_ATOMICS && buf>=0, "retire=%d, %s", int(buf), dump("").string());
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700374 return buf;
375}
376
377status_t SharedBufferServer::unlock(int buffer)
378{
379 UnlockUpdate update(this, buffer);
380 status_t err = updateCondition( update );
381 return err;
382}
383
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700384void SharedBufferServer::setStatus(status_t status)
385{
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700386 if (status < NO_ERROR) {
387 StatusUpdate update(this, status);
388 updateCondition( update );
389 }
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700390}
391
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700392status_t SharedBufferServer::reallocate()
393{
394 SharedBufferStack& stack( *mSharedStack );
395 uint32_t mask = (1<<mNumBuffers)-1;
396 android_atomic_or(mask, &stack.reallocMask);
397 return NO_ERROR;
398}
399
Mathias Agopiane7005012009-10-07 16:44:10 -0700400int32_t SharedBufferServer::getQueuedCount() const
401{
402 SharedBufferStack& stack( *mSharedStack );
403 return stack.queued;
404}
405
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700406status_t SharedBufferServer::assertReallocate(int buffer)
407{
408 ReallocateCondition condition(this, buffer);
409 status_t err = waitForCondition(condition);
410 return err;
411}
412
413Region SharedBufferServer::getDirtyRegion(int buffer) const
414{
415 SharedBufferStack& stack( *mSharedStack );
416 return stack.getDirtyRegion(buffer);
417}
418
Mathias Agopian86f73292009-09-17 01:35:28 -0700419SharedBufferStack::Statistics SharedBufferServer::getStats() const
420{
421 SharedBufferStack& stack( *mSharedStack );
422 return stack.stats;
423}
424
425
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700426// ---------------------------------------------------------------------------
427}; // namespace android