blob: f1aec8ab7aeda3d77af56d1dd8daaf7d2621eb1f [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 Agopianc2e30de2010-03-08 19:23:26 -0800182 int tail = (mNumBuffers + stack.head - stack.available + 1) % mNumBuffers;
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 Agopiancbb288b2009-09-07 16:32:45 -0700192// ============================================================================
193// conditions and updates
194// ============================================================================
195
196SharedBufferClient::DequeueCondition::DequeueCondition(
197 SharedBufferClient* sbc) : ConditionBase(sbc) {
198}
199bool SharedBufferClient::DequeueCondition::operator()() {
200 return stack.available > 0;
201}
202
203SharedBufferClient::LockCondition::LockCondition(
204 SharedBufferClient* sbc, int buf) : ConditionBase(sbc), buf(buf) {
205}
206bool SharedBufferClient::LockCondition::operator()() {
207 return (buf != stack.head ||
208 (stack.queued > 0 && stack.inUse != buf));
209}
210
211SharedBufferServer::ReallocateCondition::ReallocateCondition(
212 SharedBufferBase* sbb, int buf) : ConditionBase(sbb), buf(buf) {
213}
214bool SharedBufferServer::ReallocateCondition::operator()() {
215 // TODO: we should also check that buf has been dequeued
216 return (buf != stack.head);
217}
218
219// ----------------------------------------------------------------------------
220
221SharedBufferClient::QueueUpdate::QueueUpdate(SharedBufferBase* sbb)
222 : UpdateBase(sbb) {
223}
224ssize_t SharedBufferClient::QueueUpdate::operator()() {
225 android_atomic_inc(&stack.queued);
226 return NO_ERROR;
227}
228
229SharedBufferClient::UndoDequeueUpdate::UndoDequeueUpdate(SharedBufferBase* sbb)
230 : UpdateBase(sbb) {
231}
232ssize_t SharedBufferClient::UndoDequeueUpdate::operator()() {
233 android_atomic_inc(&stack.available);
234 return NO_ERROR;
235}
236
237SharedBufferServer::UnlockUpdate::UnlockUpdate(
238 SharedBufferBase* sbb, int lockedBuffer)
239 : UpdateBase(sbb), lockedBuffer(lockedBuffer) {
240}
241ssize_t SharedBufferServer::UnlockUpdate::operator()() {
242 if (stack.inUse != lockedBuffer) {
243 LOGE("unlocking %d, but currently locked buffer is %d",
244 lockedBuffer, stack.inUse);
245 return BAD_VALUE;
246 }
247 android_atomic_write(-1, &stack.inUse);
248 return NO_ERROR;
249}
250
251SharedBufferServer::RetireUpdate::RetireUpdate(
252 SharedBufferBase* sbb, int numBuffers)
253 : UpdateBase(sbb), numBuffers(numBuffers) {
254}
255ssize_t SharedBufferServer::RetireUpdate::operator()() {
256 // head is only written in this function, which is single-thread.
257 int32_t head = stack.head;
258
259 // Preventively lock the current buffer before updating queued.
260 android_atomic_write(head, &stack.inUse);
261
262 // Decrement the number of queued buffers
263 int32_t queued;
264 do {
265 queued = stack.queued;
266 if (queued == 0) {
267 return NOT_ENOUGH_DATA;
268 }
269 } while (android_atomic_cmpxchg(queued, queued-1, &stack.queued));
270
271 // update the head pointer
272 head = ((head+1 >= numBuffers) ? 0 : head+1);
273
274 // lock the buffer before advancing head, which automatically unlocks
275 // the buffer we preventively locked upon entering this function
276 android_atomic_write(head, &stack.inUse);
277
278 // advance head
279 android_atomic_write(head, &stack.head);
280
281 // now that head has moved, we can increment the number of available buffers
282 android_atomic_inc(&stack.available);
283 return head;
284}
285
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700286SharedBufferServer::StatusUpdate::StatusUpdate(
287 SharedBufferBase* sbb, status_t status)
288 : UpdateBase(sbb), status(status) {
289}
290
291ssize_t SharedBufferServer::StatusUpdate::operator()() {
292 android_atomic_write(status, &stack.status);
293 return NO_ERROR;
294}
295
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700296// ============================================================================
297
298SharedBufferClient::SharedBufferClient(SharedClient* sharedClient,
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700299 int surface, int num, int32_t identity)
300 : SharedBufferBase(sharedClient, surface, num, identity), tail(0)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700301{
Mathias Agopianc7d56012009-09-14 15:48:42 -0700302 tail = computeTail();
303}
304
305int32_t SharedBufferClient::computeTail() const
306{
Mathias Agopian40d57992009-09-11 19:18:20 -0700307 SharedBufferStack& stack( *mSharedStack );
Mathias Agopian40d57992009-09-11 19:18:20 -0700308 // we need to make sure we read available and head coherently,
309 // w.r.t RetireUpdate.
Mathias Agopianc7d56012009-09-14 15:48:42 -0700310 int32_t newTail;
311 int32_t avail;
312 int32_t head;
Mathias Agopian40d57992009-09-11 19:18:20 -0700313 do {
314 avail = stack.available;
315 head = stack.head;
316 } while (stack.available != avail);
Mathias Agopianc7d56012009-09-14 15:48:42 -0700317 newTail = head - avail + 1;
318 if (newTail < 0) {
319 newTail += mNumBuffers;
Mathias Agopianc2e30de2010-03-08 19:23:26 -0800320 } else if (newTail >= mNumBuffers) {
321 newTail -= mNumBuffers;
Mathias Agopian40d57992009-09-11 19:18:20 -0700322 }
Mathias Agopianc7d56012009-09-14 15:48:42 -0700323 return newTail;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700324}
325
326ssize_t SharedBufferClient::dequeue()
327{
Mathias Agopian40d57992009-09-11 19:18:20 -0700328 SharedBufferStack& stack( *mSharedStack );
329
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700330 if (stack.head == tail && stack.available == mNumBuffers) {
Mathias Agopian40d57992009-09-11 19:18:20 -0700331 LOGW("dequeue: tail=%d, head=%d, avail=%d, queued=%d",
332 tail, stack.head, stack.available, stack.queued);
333 }
Mathias Agopian86f73292009-09-17 01:35:28 -0700334
335 const nsecs_t dequeueTime = systemTime(SYSTEM_TIME_THREAD);
Mathias Agopian40d57992009-09-11 19:18:20 -0700336
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700337 //LOGD("[%d] about to dequeue a buffer",
338 // mSharedStack->identity);
339 DequeueCondition condition(this);
340 status_t err = waitForCondition(condition);
341 if (err != NO_ERROR)
342 return ssize_t(err);
343
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700344 // NOTE: 'stack.available' is part of the conditions, however
345 // decrementing it, never changes any conditions, so we don't need
346 // to do this as part of an update.
347 if (android_atomic_dec(&stack.available) == 0) {
348 LOGW("dequeue probably called from multiple threads!");
349 }
350
351 int dequeued = tail;
352 tail = ((tail+1 >= mNumBuffers) ? 0 : tail+1);
353 LOGD_IF(DEBUG_ATOMICS, "dequeued=%d, tail=%d, %s",
354 dequeued, tail, dump("").string());
Mathias Agopian40d57992009-09-11 19:18:20 -0700355
Mathias Agopian86f73292009-09-17 01:35:28 -0700356 mDequeueTime[dequeued] = dequeueTime;
357
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700358 return dequeued;
359}
360
361status_t SharedBufferClient::undoDequeue(int buf)
362{
363 UndoDequeueUpdate update(this);
364 status_t err = updateCondition( update );
Mathias Agopianc7d56012009-09-14 15:48:42 -0700365 if (err == NO_ERROR) {
366 tail = computeTail();
367 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700368 return err;
369}
370
371status_t SharedBufferClient::lock(int buf)
372{
373 LockCondition condition(this, buf);
Mathias Agopian86f73292009-09-17 01:35:28 -0700374 status_t err = waitForCondition(condition);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700375 return err;
376}
377
378status_t SharedBufferClient::queue(int buf)
379{
380 QueueUpdate update(this);
381 status_t err = updateCondition( update );
382 LOGD_IF(DEBUG_ATOMICS, "queued=%d, %s", buf, dump("").string());
Mathias Agopian86f73292009-09-17 01:35:28 -0700383 SharedBufferStack& stack( *mSharedStack );
384 const nsecs_t now = systemTime(SYSTEM_TIME_THREAD);
385 stack.stats.totalTime = ns2us(now - mDequeueTime[buf]);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700386 return err;
387}
388
389bool SharedBufferClient::needNewBuffer(int buffer) const
390{
391 SharedBufferStack& stack( *mSharedStack );
392 const uint32_t mask = 1<<buffer;
393 return (android_atomic_and(~mask, &stack.reallocMask) & mask) != 0;
394}
395
Mathias Agopiancc08e682010-04-15 18:48:26 -0700396status_t SharedBufferClient::setCrop(int buffer, const Rect& crop)
397{
398 SharedBufferStack& stack( *mSharedStack );
399 return stack.setCrop(buffer, crop);
400}
401
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700402status_t SharedBufferClient::setDirtyRegion(int buffer, const Region& reg)
403{
404 SharedBufferStack& stack( *mSharedStack );
405 return stack.setDirtyRegion(buffer, reg);
406}
407
408// ----------------------------------------------------------------------------
409
410SharedBufferServer::SharedBufferServer(SharedClient* sharedClient,
Mathias Agopian48d819a2009-09-10 19:41:18 -0700411 int surface, int num, int32_t identity)
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700412 : SharedBufferBase(sharedClient, surface, num, identity)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700413{
Mathias Agopian48d819a2009-09-10 19:41:18 -0700414 mSharedStack->init(identity);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700415 mSharedStack->head = num-1;
416 mSharedStack->available = num;
417 mSharedStack->queued = 0;
418 mSharedStack->reallocMask = 0;
Mathias Agopiancc08e682010-04-15 18:48:26 -0700419 memset(mSharedStack->buffers, 0, sizeof(mSharedStack->buffers));
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700420}
421
422ssize_t SharedBufferServer::retireAndLock()
423{
424 RetireUpdate update(this, mNumBuffers);
425 ssize_t buf = updateCondition( update );
Mathias Agopian40d57992009-09-11 19:18:20 -0700426 LOGD_IF(DEBUG_ATOMICS && buf>=0, "retire=%d, %s", int(buf), dump("").string());
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700427 return buf;
428}
429
430status_t SharedBufferServer::unlock(int buffer)
431{
432 UnlockUpdate update(this, buffer);
433 status_t err = updateCondition( update );
434 return err;
435}
436
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700437void SharedBufferServer::setStatus(status_t status)
438{
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700439 if (status < NO_ERROR) {
440 StatusUpdate update(this, status);
441 updateCondition( update );
442 }
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700443}
444
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700445status_t SharedBufferServer::reallocate()
446{
447 SharedBufferStack& stack( *mSharedStack );
448 uint32_t mask = (1<<mNumBuffers)-1;
449 android_atomic_or(mask, &stack.reallocMask);
450 return NO_ERROR;
451}
452
Mathias Agopiane7005012009-10-07 16:44:10 -0700453int32_t SharedBufferServer::getQueuedCount() const
454{
455 SharedBufferStack& stack( *mSharedStack );
456 return stack.queued;
457}
458
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700459status_t SharedBufferServer::assertReallocate(int buffer)
460{
461 ReallocateCondition condition(this, buffer);
462 status_t err = waitForCondition(condition);
463 return err;
464}
465
466Region SharedBufferServer::getDirtyRegion(int buffer) const
467{
468 SharedBufferStack& stack( *mSharedStack );
469 return stack.getDirtyRegion(buffer);
470}
471
Mathias Agopian86f73292009-09-17 01:35:28 -0700472SharedBufferStack::Statistics SharedBufferServer::getStats() const
473{
474 SharedBufferStack& stack( *mSharedStack );
475 return stack.stats;
476}
477
478
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700479// ---------------------------------------------------------------------------
480}; // namespace android