blob: 21a40db4e3cb5611305062c80d9993662956c71c [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 Agopiancc08e682010-04-15 18:48:26 -070070status_t SharedBufferStack::setCrop(int buffer, const Rect& crop)
71{
72 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
73 return BAD_INDEX;
74
75 buffers[buffer].crop.l = uint16_t(crop.left);
76 buffers[buffer].crop.t = uint16_t(crop.top);
77 buffers[buffer].crop.r = uint16_t(crop.right);
78 buffers[buffer].crop.b = uint16_t(crop.bottom);
79 return NO_ERROR;
80}
81
Mathias Agopiancbb288b2009-09-07 16:32:45 -070082status_t SharedBufferStack::setDirtyRegion(int buffer, const Region& dirty)
83{
84 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
85 return BAD_INDEX;
86
Mathias Agopian245e4d72010-04-21 15:24:11 -070087 FlatRegion& reg(buffers[buffer].dirtyRegion);
88 if (dirty.isEmpty()) {
89 reg.count = 0;
90 return NO_ERROR;
91 }
92
Mathias Agopian1100c8b2010-04-05 16:21:53 -070093 size_t count;
94 Rect const* r = dirty.getArray(&count);
Mathias Agopian1100c8b2010-04-05 16:21:53 -070095 if (count > FlatRegion::NUM_RECT_MAX) {
96 const Rect bounds(dirty.getBounds());
97 reg.count = 1;
Mathias Agopiancc08e682010-04-15 18:48:26 -070098 reg.rects[0].l = uint16_t(bounds.left);
99 reg.rects[0].t = uint16_t(bounds.top);
100 reg.rects[0].r = uint16_t(bounds.right);
101 reg.rects[0].b = uint16_t(bounds.bottom);
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700102 } else {
103 reg.count = count;
104 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancc08e682010-04-15 18:48:26 -0700105 reg.rects[i].l = uint16_t(r[i].left);
106 reg.rects[i].t = uint16_t(r[i].top);
107 reg.rects[i].r = uint16_t(r[i].right);
108 reg.rects[i].b = uint16_t(r[i].bottom);
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700109 }
110 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700111 return NO_ERROR;
112}
113
114Region SharedBufferStack::getDirtyRegion(int buffer) const
115{
116 Region res;
117 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
118 return res;
119
Mathias Agopiancc08e682010-04-15 18:48:26 -0700120 const FlatRegion& reg(buffers[buffer].dirtyRegion);
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700121 if (reg.count > FlatRegion::NUM_RECT_MAX)
122 return res;
123
124 if (reg.count == 1) {
Mathias Agopiancc08e682010-04-15 18:48:26 -0700125 const Rect r(
126 reg.rects[0].l,
127 reg.rects[0].t,
128 reg.rects[0].r,
129 reg.rects[0].b);
130 res.set(r);
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700131 } else {
132 for (size_t i=0 ; i<reg.count ; i++) {
133 const Rect r(
Mathias Agopiancc08e682010-04-15 18:48:26 -0700134 reg.rects[i].l,
135 reg.rects[i].t,
136 reg.rects[i].r,
137 reg.rects[i].b);
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700138 res.orSelf(r);
139 }
140 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700141 return res;
142}
143
144// ----------------------------------------------------------------------------
145
146SharedBufferBase::SharedBufferBase(SharedClient* sharedClient,
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700147 int surface, int num, int32_t identity)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700148 : mSharedClient(sharedClient),
149 mSharedStack(sharedClient->surfaces + surface),
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700150 mNumBuffers(num), mIdentity(identity)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700151{
152}
153
154SharedBufferBase::~SharedBufferBase()
155{
156}
157
158uint32_t SharedBufferBase::getIdentity()
159{
160 SharedBufferStack& stack( *mSharedStack );
161 return stack.identity;
162}
163
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700164status_t SharedBufferBase::getStatus() const
165{
166 SharedBufferStack& stack( *mSharedStack );
167 return stack.status;
168}
169
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700170size_t SharedBufferBase::getFrontBuffer() const
171{
172 SharedBufferStack& stack( *mSharedStack );
173 return size_t( stack.head );
174}
175
176String8 SharedBufferBase::dump(char const* prefix) const
177{
178 const size_t SIZE = 1024;
179 char buffer[SIZE];
180 String8 result;
181 SharedBufferStack& stack( *mSharedStack );
Mathias Agopian0a8cd062010-04-27 16:11:38 -0700182 int tail = computeTail();
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700183 snprintf(buffer, SIZE,
Mathias Agopianc2e30de2010-03-08 19:23:26 -0800184 "%s[ head=%2d, available=%2d, queued=%2d, tail=%2d ] "
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700185 "reallocMask=%08x, inUse=%2d, identity=%d, status=%d\n",
Mathias Agopianc2e30de2010-03-08 19:23:26 -0800186 prefix, stack.head, stack.available, stack.queued, tail,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700187 stack.reallocMask, stack.inUse, stack.identity, stack.status);
188 result.append(buffer);
189 return result;
190}
191
Mathias Agopian0a8cd062010-04-27 16:11:38 -0700192int32_t SharedBufferBase::computeTail() const
193{
194 SharedBufferStack& stack( *mSharedStack );
195 return (mNumBuffers + stack.head - stack.available + 1) % mNumBuffers;
196}
197
Mathias Agopianb2965332010-04-27 16:41:19 -0700198status_t SharedBufferBase::waitForCondition(const ConditionBase& condition)
199{
200 const SharedBufferStack& stack( *mSharedStack );
201 SharedClient& client( *mSharedClient );
202 const nsecs_t TIMEOUT = s2ns(1);
203 const int identity = mIdentity;
204
205 Mutex::Autolock _l(client.lock);
206 while ((condition()==false) &&
207 (stack.identity == identity) &&
208 (stack.status == NO_ERROR))
209 {
210 status_t err = client.cv.waitRelative(client.lock, TIMEOUT);
211 // handle errors and timeouts
212 if (CC_UNLIKELY(err != NO_ERROR)) {
213 if (err == TIMED_OUT) {
214 if (condition()) {
215 LOGE("waitForCondition(%s) timed out (identity=%d), "
216 "but condition is true! We recovered but it "
217 "shouldn't happen." , condition.name(), stack.identity);
218 break;
219 } else {
220 LOGW("waitForCondition(%s) timed out "
221 "(identity=%d, status=%d). "
222 "CPU may be pegged. trying again.", condition.name(),
223 stack.identity, stack.status);
224 }
225 } else {
226 LOGE("waitForCondition(%s) error (%s) ",
227 condition.name(), strerror(-err));
228 return err;
229 }
230 }
231 }
232 return (stack.identity != mIdentity) ? status_t(BAD_INDEX) : stack.status;
233}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700234// ============================================================================
235// conditions and updates
236// ============================================================================
237
238SharedBufferClient::DequeueCondition::DequeueCondition(
239 SharedBufferClient* sbc) : ConditionBase(sbc) {
240}
Mathias Agopianb2965332010-04-27 16:41:19 -0700241bool SharedBufferClient::DequeueCondition::operator()() const {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700242 return stack.available > 0;
243}
244
245SharedBufferClient::LockCondition::LockCondition(
246 SharedBufferClient* sbc, int buf) : ConditionBase(sbc), buf(buf) {
247}
Mathias Agopianb2965332010-04-27 16:41:19 -0700248bool SharedBufferClient::LockCondition::operator()() const {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700249 return (buf != stack.head ||
250 (stack.queued > 0 && stack.inUse != buf));
251}
252
253SharedBufferServer::ReallocateCondition::ReallocateCondition(
254 SharedBufferBase* sbb, int buf) : ConditionBase(sbb), buf(buf) {
255}
Mathias Agopianb2965332010-04-27 16:41:19 -0700256bool SharedBufferServer::ReallocateCondition::operator()() const {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700257 // TODO: we should also check that buf has been dequeued
258 return (buf != stack.head);
259}
260
261// ----------------------------------------------------------------------------
262
263SharedBufferClient::QueueUpdate::QueueUpdate(SharedBufferBase* sbb)
264 : UpdateBase(sbb) {
265}
266ssize_t SharedBufferClient::QueueUpdate::operator()() {
267 android_atomic_inc(&stack.queued);
268 return NO_ERROR;
269}
270
271SharedBufferClient::UndoDequeueUpdate::UndoDequeueUpdate(SharedBufferBase* sbb)
272 : UpdateBase(sbb) {
273}
274ssize_t SharedBufferClient::UndoDequeueUpdate::operator()() {
275 android_atomic_inc(&stack.available);
276 return NO_ERROR;
277}
278
279SharedBufferServer::UnlockUpdate::UnlockUpdate(
280 SharedBufferBase* sbb, int lockedBuffer)
281 : UpdateBase(sbb), lockedBuffer(lockedBuffer) {
282}
283ssize_t SharedBufferServer::UnlockUpdate::operator()() {
284 if (stack.inUse != lockedBuffer) {
285 LOGE("unlocking %d, but currently locked buffer is %d",
286 lockedBuffer, stack.inUse);
287 return BAD_VALUE;
288 }
289 android_atomic_write(-1, &stack.inUse);
290 return NO_ERROR;
291}
292
293SharedBufferServer::RetireUpdate::RetireUpdate(
294 SharedBufferBase* sbb, int numBuffers)
295 : UpdateBase(sbb), numBuffers(numBuffers) {
296}
297ssize_t SharedBufferServer::RetireUpdate::operator()() {
298 // head is only written in this function, which is single-thread.
299 int32_t head = stack.head;
300
301 // Preventively lock the current buffer before updating queued.
302 android_atomic_write(head, &stack.inUse);
303
304 // Decrement the number of queued buffers
305 int32_t queued;
306 do {
307 queued = stack.queued;
308 if (queued == 0) {
309 return NOT_ENOUGH_DATA;
310 }
311 } while (android_atomic_cmpxchg(queued, queued-1, &stack.queued));
312
313 // update the head pointer
314 head = ((head+1 >= numBuffers) ? 0 : head+1);
315
316 // lock the buffer before advancing head, which automatically unlocks
317 // the buffer we preventively locked upon entering this function
318 android_atomic_write(head, &stack.inUse);
319
320 // advance head
321 android_atomic_write(head, &stack.head);
322
323 // now that head has moved, we can increment the number of available buffers
324 android_atomic_inc(&stack.available);
325 return head;
326}
327
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700328SharedBufferServer::StatusUpdate::StatusUpdate(
329 SharedBufferBase* sbb, status_t status)
330 : UpdateBase(sbb), status(status) {
331}
332
333ssize_t SharedBufferServer::StatusUpdate::operator()() {
334 android_atomic_write(status, &stack.status);
335 return NO_ERROR;
336}
337
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700338// ============================================================================
339
340SharedBufferClient::SharedBufferClient(SharedClient* sharedClient,
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700341 int surface, int num, int32_t identity)
Mathias Agopian0a8cd062010-04-27 16:11:38 -0700342 : SharedBufferBase(sharedClient, surface, num, identity),
343 tail(0), undoDequeueTail(0)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700344{
Mathias Agopianc7d56012009-09-14 15:48:42 -0700345 tail = computeTail();
346}
347
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700348ssize_t SharedBufferClient::dequeue()
349{
Mathias Agopian40d57992009-09-11 19:18:20 -0700350 SharedBufferStack& stack( *mSharedStack );
351
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700352 if (stack.head == tail && stack.available == mNumBuffers) {
Mathias Agopian40d57992009-09-11 19:18:20 -0700353 LOGW("dequeue: tail=%d, head=%d, avail=%d, queued=%d",
354 tail, stack.head, stack.available, stack.queued);
355 }
Mathias Agopian86f73292009-09-17 01:35:28 -0700356
357 const nsecs_t dequeueTime = systemTime(SYSTEM_TIME_THREAD);
Mathias Agopian40d57992009-09-11 19:18:20 -0700358
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700359 //LOGD("[%d] about to dequeue a buffer",
360 // mSharedStack->identity);
361 DequeueCondition condition(this);
362 status_t err = waitForCondition(condition);
363 if (err != NO_ERROR)
364 return ssize_t(err);
365
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700366 // NOTE: 'stack.available' is part of the conditions, however
367 // decrementing it, never changes any conditions, so we don't need
368 // to do this as part of an update.
369 if (android_atomic_dec(&stack.available) == 0) {
370 LOGW("dequeue probably called from multiple threads!");
371 }
372
373 int dequeued = tail;
374 tail = ((tail+1 >= mNumBuffers) ? 0 : tail+1);
Mathias Agopian0a8cd062010-04-27 16:11:38 -0700375 undoDequeueTail = dequeued;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700376 LOGD_IF(DEBUG_ATOMICS, "dequeued=%d, tail=%d, %s",
377 dequeued, tail, dump("").string());
Mathias Agopian40d57992009-09-11 19:18:20 -0700378
Mathias Agopian86f73292009-09-17 01:35:28 -0700379 mDequeueTime[dequeued] = dequeueTime;
380
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700381 return dequeued;
382}
383
384status_t SharedBufferClient::undoDequeue(int buf)
385{
386 UndoDequeueUpdate update(this);
387 status_t err = updateCondition( update );
Mathias Agopianc7d56012009-09-14 15:48:42 -0700388 if (err == NO_ERROR) {
Mathias Agopian0a8cd062010-04-27 16:11:38 -0700389 tail = undoDequeueTail;
Mathias Agopianc7d56012009-09-14 15:48:42 -0700390 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700391 return err;
392}
393
394status_t SharedBufferClient::lock(int buf)
395{
396 LockCondition condition(this, buf);
Mathias Agopian86f73292009-09-17 01:35:28 -0700397 status_t err = waitForCondition(condition);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700398 return err;
399}
400
401status_t SharedBufferClient::queue(int buf)
402{
403 QueueUpdate update(this);
404 status_t err = updateCondition( update );
405 LOGD_IF(DEBUG_ATOMICS, "queued=%d, %s", buf, dump("").string());
Mathias Agopian86f73292009-09-17 01:35:28 -0700406 SharedBufferStack& stack( *mSharedStack );
407 const nsecs_t now = systemTime(SYSTEM_TIME_THREAD);
408 stack.stats.totalTime = ns2us(now - mDequeueTime[buf]);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700409 return err;
410}
411
412bool SharedBufferClient::needNewBuffer(int buffer) const
413{
414 SharedBufferStack& stack( *mSharedStack );
415 const uint32_t mask = 1<<buffer;
416 return (android_atomic_and(~mask, &stack.reallocMask) & mask) != 0;
417}
418
Mathias Agopiancc08e682010-04-15 18:48:26 -0700419status_t SharedBufferClient::setCrop(int buffer, const Rect& crop)
420{
421 SharedBufferStack& stack( *mSharedStack );
422 return stack.setCrop(buffer, crop);
423}
424
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700425status_t SharedBufferClient::setDirtyRegion(int buffer, const Region& reg)
426{
427 SharedBufferStack& stack( *mSharedStack );
428 return stack.setDirtyRegion(buffer, reg);
429}
430
431// ----------------------------------------------------------------------------
432
433SharedBufferServer::SharedBufferServer(SharedClient* sharedClient,
Mathias Agopian48d819a2009-09-10 19:41:18 -0700434 int surface, int num, int32_t identity)
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700435 : SharedBufferBase(sharedClient, surface, num, identity)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700436{
Mathias Agopian48d819a2009-09-10 19:41:18 -0700437 mSharedStack->init(identity);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700438 mSharedStack->head = num-1;
439 mSharedStack->available = num;
440 mSharedStack->queued = 0;
441 mSharedStack->reallocMask = 0;
Mathias Agopiancc08e682010-04-15 18:48:26 -0700442 memset(mSharedStack->buffers, 0, sizeof(mSharedStack->buffers));
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700443}
444
445ssize_t SharedBufferServer::retireAndLock()
446{
447 RetireUpdate update(this, mNumBuffers);
448 ssize_t buf = updateCondition( update );
Mathias Agopian40d57992009-09-11 19:18:20 -0700449 LOGD_IF(DEBUG_ATOMICS && buf>=0, "retire=%d, %s", int(buf), dump("").string());
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700450 return buf;
451}
452
453status_t SharedBufferServer::unlock(int buffer)
454{
455 UnlockUpdate update(this, buffer);
456 status_t err = updateCondition( update );
457 return err;
458}
459
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700460void SharedBufferServer::setStatus(status_t status)
461{
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700462 if (status < NO_ERROR) {
463 StatusUpdate update(this, status);
464 updateCondition( update );
465 }
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700466}
467
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700468status_t SharedBufferServer::reallocate()
469{
470 SharedBufferStack& stack( *mSharedStack );
471 uint32_t mask = (1<<mNumBuffers)-1;
472 android_atomic_or(mask, &stack.reallocMask);
473 return NO_ERROR;
474}
475
Mathias Agopiane7005012009-10-07 16:44:10 -0700476int32_t SharedBufferServer::getQueuedCount() const
477{
478 SharedBufferStack& stack( *mSharedStack );
479 return stack.queued;
480}
481
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700482status_t SharedBufferServer::assertReallocate(int buffer)
483{
484 ReallocateCondition condition(this, buffer);
485 status_t err = waitForCondition(condition);
486 return err;
487}
488
489Region SharedBufferServer::getDirtyRegion(int buffer) const
490{
491 SharedBufferStack& stack( *mSharedStack );
492 return stack.getDirtyRegion(buffer);
493}
494
Mathias Agopian86f73292009-09-17 01:35:28 -0700495SharedBufferStack::Statistics SharedBufferServer::getStats() const
496{
497 SharedBufferStack& stack( *mSharedStack );
498 return stack.stats;
499}
500
501
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700502// ---------------------------------------------------------------------------
503}; // namespace android