blob: 881ffd44e907127b08ea3b49b1e4db95333ffb26 [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
87 // in the current implementation we only send a single rectangle
Mathias Agopian1100c8b2010-04-05 16:21:53 -070088 size_t count;
89 Rect const* r = dirty.getArray(&count);
Mathias Agopiancc08e682010-04-15 18:48:26 -070090 FlatRegion& reg(buffers[buffer].dirtyRegion);
Mathias Agopian1100c8b2010-04-05 16:21:53 -070091 if (count > FlatRegion::NUM_RECT_MAX) {
92 const Rect bounds(dirty.getBounds());
93 reg.count = 1;
Mathias Agopiancc08e682010-04-15 18:48:26 -070094 reg.rects[0].l = uint16_t(bounds.left);
95 reg.rects[0].t = uint16_t(bounds.top);
96 reg.rects[0].r = uint16_t(bounds.right);
97 reg.rects[0].b = uint16_t(bounds.bottom);
Mathias Agopian1100c8b2010-04-05 16:21:53 -070098 } else {
99 reg.count = count;
100 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancc08e682010-04-15 18:48:26 -0700101 reg.rects[i].l = uint16_t(r[i].left);
102 reg.rects[i].t = uint16_t(r[i].top);
103 reg.rects[i].r = uint16_t(r[i].right);
104 reg.rects[i].b = uint16_t(r[i].bottom);
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700105 }
106 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700107 return NO_ERROR;
108}
109
110Region SharedBufferStack::getDirtyRegion(int buffer) const
111{
112 Region res;
113 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
114 return res;
115
Mathias Agopiancc08e682010-04-15 18:48:26 -0700116 const FlatRegion& reg(buffers[buffer].dirtyRegion);
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700117 if (reg.count > FlatRegion::NUM_RECT_MAX)
118 return res;
119
120 if (reg.count == 1) {
Mathias Agopiancc08e682010-04-15 18:48:26 -0700121 const Rect r(
122 reg.rects[0].l,
123 reg.rects[0].t,
124 reg.rects[0].r,
125 reg.rects[0].b);
126 res.set(r);
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700127 } else {
128 for (size_t i=0 ; i<reg.count ; i++) {
129 const Rect r(
Mathias Agopiancc08e682010-04-15 18:48:26 -0700130 reg.rects[i].l,
131 reg.rects[i].t,
132 reg.rects[i].r,
133 reg.rects[i].b);
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700134 res.orSelf(r);
135 }
136 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700137 return res;
138}
139
140// ----------------------------------------------------------------------------
141
142SharedBufferBase::SharedBufferBase(SharedClient* sharedClient,
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700143 int surface, int num, int32_t identity)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700144 : mSharedClient(sharedClient),
145 mSharedStack(sharedClient->surfaces + surface),
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700146 mNumBuffers(num), mIdentity(identity)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700147{
148}
149
150SharedBufferBase::~SharedBufferBase()
151{
152}
153
154uint32_t SharedBufferBase::getIdentity()
155{
156 SharedBufferStack& stack( *mSharedStack );
157 return stack.identity;
158}
159
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700160status_t SharedBufferBase::getStatus() const
161{
162 SharedBufferStack& stack( *mSharedStack );
163 return stack.status;
164}
165
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700166size_t SharedBufferBase::getFrontBuffer() const
167{
168 SharedBufferStack& stack( *mSharedStack );
169 return size_t( stack.head );
170}
171
172String8 SharedBufferBase::dump(char const* prefix) const
173{
174 const size_t SIZE = 1024;
175 char buffer[SIZE];
176 String8 result;
177 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianc2e30de2010-03-08 19:23:26 -0800178 int tail = (mNumBuffers + stack.head - stack.available + 1) % mNumBuffers;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700179 snprintf(buffer, SIZE,
Mathias Agopianc2e30de2010-03-08 19:23:26 -0800180 "%s[ head=%2d, available=%2d, queued=%2d, tail=%2d ] "
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700181 "reallocMask=%08x, inUse=%2d, identity=%d, status=%d\n",
Mathias Agopianc2e30de2010-03-08 19:23:26 -0800182 prefix, stack.head, stack.available, stack.queued, tail,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700183 stack.reallocMask, stack.inUse, stack.identity, stack.status);
184 result.append(buffer);
185 return result;
186}
187
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700188// ============================================================================
189// conditions and updates
190// ============================================================================
191
192SharedBufferClient::DequeueCondition::DequeueCondition(
193 SharedBufferClient* sbc) : ConditionBase(sbc) {
194}
195bool SharedBufferClient::DequeueCondition::operator()() {
196 return stack.available > 0;
197}
198
199SharedBufferClient::LockCondition::LockCondition(
200 SharedBufferClient* sbc, int buf) : ConditionBase(sbc), buf(buf) {
201}
202bool SharedBufferClient::LockCondition::operator()() {
203 return (buf != stack.head ||
204 (stack.queued > 0 && stack.inUse != buf));
205}
206
207SharedBufferServer::ReallocateCondition::ReallocateCondition(
208 SharedBufferBase* sbb, int buf) : ConditionBase(sbb), buf(buf) {
209}
210bool SharedBufferServer::ReallocateCondition::operator()() {
211 // TODO: we should also check that buf has been dequeued
212 return (buf != stack.head);
213}
214
215// ----------------------------------------------------------------------------
216
217SharedBufferClient::QueueUpdate::QueueUpdate(SharedBufferBase* sbb)
218 : UpdateBase(sbb) {
219}
220ssize_t SharedBufferClient::QueueUpdate::operator()() {
221 android_atomic_inc(&stack.queued);
222 return NO_ERROR;
223}
224
225SharedBufferClient::UndoDequeueUpdate::UndoDequeueUpdate(SharedBufferBase* sbb)
226 : UpdateBase(sbb) {
227}
228ssize_t SharedBufferClient::UndoDequeueUpdate::operator()() {
229 android_atomic_inc(&stack.available);
230 return NO_ERROR;
231}
232
233SharedBufferServer::UnlockUpdate::UnlockUpdate(
234 SharedBufferBase* sbb, int lockedBuffer)
235 : UpdateBase(sbb), lockedBuffer(lockedBuffer) {
236}
237ssize_t SharedBufferServer::UnlockUpdate::operator()() {
238 if (stack.inUse != lockedBuffer) {
239 LOGE("unlocking %d, but currently locked buffer is %d",
240 lockedBuffer, stack.inUse);
241 return BAD_VALUE;
242 }
243 android_atomic_write(-1, &stack.inUse);
244 return NO_ERROR;
245}
246
247SharedBufferServer::RetireUpdate::RetireUpdate(
248 SharedBufferBase* sbb, int numBuffers)
249 : UpdateBase(sbb), numBuffers(numBuffers) {
250}
251ssize_t SharedBufferServer::RetireUpdate::operator()() {
252 // head is only written in this function, which is single-thread.
253 int32_t head = stack.head;
254
255 // Preventively lock the current buffer before updating queued.
256 android_atomic_write(head, &stack.inUse);
257
258 // Decrement the number of queued buffers
259 int32_t queued;
260 do {
261 queued = stack.queued;
262 if (queued == 0) {
263 return NOT_ENOUGH_DATA;
264 }
265 } while (android_atomic_cmpxchg(queued, queued-1, &stack.queued));
266
267 // update the head pointer
268 head = ((head+1 >= numBuffers) ? 0 : head+1);
269
270 // lock the buffer before advancing head, which automatically unlocks
271 // the buffer we preventively locked upon entering this function
272 android_atomic_write(head, &stack.inUse);
273
274 // advance head
275 android_atomic_write(head, &stack.head);
276
277 // now that head has moved, we can increment the number of available buffers
278 android_atomic_inc(&stack.available);
279 return head;
280}
281
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700282SharedBufferServer::StatusUpdate::StatusUpdate(
283 SharedBufferBase* sbb, status_t status)
284 : UpdateBase(sbb), status(status) {
285}
286
287ssize_t SharedBufferServer::StatusUpdate::operator()() {
288 android_atomic_write(status, &stack.status);
289 return NO_ERROR;
290}
291
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700292// ============================================================================
293
294SharedBufferClient::SharedBufferClient(SharedClient* sharedClient,
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700295 int surface, int num, int32_t identity)
296 : SharedBufferBase(sharedClient, surface, num, identity), tail(0)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700297{
Mathias Agopianc7d56012009-09-14 15:48:42 -0700298 tail = computeTail();
299}
300
301int32_t SharedBufferClient::computeTail() const
302{
Mathias Agopian40d57992009-09-11 19:18:20 -0700303 SharedBufferStack& stack( *mSharedStack );
Mathias Agopian40d57992009-09-11 19:18:20 -0700304 // we need to make sure we read available and head coherently,
305 // w.r.t RetireUpdate.
Mathias Agopianc7d56012009-09-14 15:48:42 -0700306 int32_t newTail;
307 int32_t avail;
308 int32_t head;
Mathias Agopian40d57992009-09-11 19:18:20 -0700309 do {
310 avail = stack.available;
311 head = stack.head;
312 } while (stack.available != avail);
Mathias Agopianc7d56012009-09-14 15:48:42 -0700313 newTail = head - avail + 1;
314 if (newTail < 0) {
315 newTail += mNumBuffers;
Mathias Agopianc2e30de2010-03-08 19:23:26 -0800316 } else if (newTail >= mNumBuffers) {
317 newTail -= mNumBuffers;
Mathias Agopian40d57992009-09-11 19:18:20 -0700318 }
Mathias Agopianc7d56012009-09-14 15:48:42 -0700319 return newTail;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700320}
321
322ssize_t SharedBufferClient::dequeue()
323{
Mathias Agopian40d57992009-09-11 19:18:20 -0700324 SharedBufferStack& stack( *mSharedStack );
325
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700326 if (stack.head == tail && stack.available == mNumBuffers) {
Mathias Agopian40d57992009-09-11 19:18:20 -0700327 LOGW("dequeue: tail=%d, head=%d, avail=%d, queued=%d",
328 tail, stack.head, stack.available, stack.queued);
329 }
Mathias Agopian86f73292009-09-17 01:35:28 -0700330
331 const nsecs_t dequeueTime = systemTime(SYSTEM_TIME_THREAD);
Mathias Agopian40d57992009-09-11 19:18:20 -0700332
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700333 //LOGD("[%d] about to dequeue a buffer",
334 // mSharedStack->identity);
335 DequeueCondition condition(this);
336 status_t err = waitForCondition(condition);
337 if (err != NO_ERROR)
338 return ssize_t(err);
339
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700340 // NOTE: 'stack.available' is part of the conditions, however
341 // decrementing it, never changes any conditions, so we don't need
342 // to do this as part of an update.
343 if (android_atomic_dec(&stack.available) == 0) {
344 LOGW("dequeue probably called from multiple threads!");
345 }
346
347 int dequeued = tail;
348 tail = ((tail+1 >= mNumBuffers) ? 0 : tail+1);
349 LOGD_IF(DEBUG_ATOMICS, "dequeued=%d, tail=%d, %s",
350 dequeued, tail, dump("").string());
Mathias Agopian40d57992009-09-11 19:18:20 -0700351
Mathias Agopian86f73292009-09-17 01:35:28 -0700352 mDequeueTime[dequeued] = dequeueTime;
353
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700354 return dequeued;
355}
356
357status_t SharedBufferClient::undoDequeue(int buf)
358{
359 UndoDequeueUpdate update(this);
360 status_t err = updateCondition( update );
Mathias Agopianc7d56012009-09-14 15:48:42 -0700361 if (err == NO_ERROR) {
362 tail = computeTail();
363 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700364 return err;
365}
366
367status_t SharedBufferClient::lock(int buf)
368{
369 LockCondition condition(this, buf);
Mathias Agopian86f73292009-09-17 01:35:28 -0700370 status_t err = waitForCondition(condition);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700371 return err;
372}
373
374status_t SharedBufferClient::queue(int buf)
375{
376 QueueUpdate update(this);
377 status_t err = updateCondition( update );
378 LOGD_IF(DEBUG_ATOMICS, "queued=%d, %s", buf, dump("").string());
Mathias Agopian86f73292009-09-17 01:35:28 -0700379 SharedBufferStack& stack( *mSharedStack );
380 const nsecs_t now = systemTime(SYSTEM_TIME_THREAD);
381 stack.stats.totalTime = ns2us(now - mDequeueTime[buf]);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700382 return err;
383}
384
385bool SharedBufferClient::needNewBuffer(int buffer) const
386{
387 SharedBufferStack& stack( *mSharedStack );
388 const uint32_t mask = 1<<buffer;
389 return (android_atomic_and(~mask, &stack.reallocMask) & mask) != 0;
390}
391
Mathias Agopiancc08e682010-04-15 18:48:26 -0700392status_t SharedBufferClient::setCrop(int buffer, const Rect& crop)
393{
394 SharedBufferStack& stack( *mSharedStack );
395 return stack.setCrop(buffer, crop);
396}
397
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700398status_t SharedBufferClient::setDirtyRegion(int buffer, const Region& reg)
399{
400 SharedBufferStack& stack( *mSharedStack );
401 return stack.setDirtyRegion(buffer, reg);
402}
403
404// ----------------------------------------------------------------------------
405
406SharedBufferServer::SharedBufferServer(SharedClient* sharedClient,
Mathias Agopian48d819a2009-09-10 19:41:18 -0700407 int surface, int num, int32_t identity)
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700408 : SharedBufferBase(sharedClient, surface, num, identity)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700409{
Mathias Agopian48d819a2009-09-10 19:41:18 -0700410 mSharedStack->init(identity);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700411 mSharedStack->head = num-1;
412 mSharedStack->available = num;
413 mSharedStack->queued = 0;
414 mSharedStack->reallocMask = 0;
Mathias Agopiancc08e682010-04-15 18:48:26 -0700415 memset(mSharedStack->buffers, 0, sizeof(mSharedStack->buffers));
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700416}
417
418ssize_t SharedBufferServer::retireAndLock()
419{
420 RetireUpdate update(this, mNumBuffers);
421 ssize_t buf = updateCondition( update );
Mathias Agopian40d57992009-09-11 19:18:20 -0700422 LOGD_IF(DEBUG_ATOMICS && buf>=0, "retire=%d, %s", int(buf), dump("").string());
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700423 return buf;
424}
425
426status_t SharedBufferServer::unlock(int buffer)
427{
428 UnlockUpdate update(this, buffer);
429 status_t err = updateCondition( update );
430 return err;
431}
432
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700433void SharedBufferServer::setStatus(status_t status)
434{
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700435 if (status < NO_ERROR) {
436 StatusUpdate update(this, status);
437 updateCondition( update );
438 }
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700439}
440
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700441status_t SharedBufferServer::reallocate()
442{
443 SharedBufferStack& stack( *mSharedStack );
444 uint32_t mask = (1<<mNumBuffers)-1;
445 android_atomic_or(mask, &stack.reallocMask);
446 return NO_ERROR;
447}
448
Mathias Agopiane7005012009-10-07 16:44:10 -0700449int32_t SharedBufferServer::getQueuedCount() const
450{
451 SharedBufferStack& stack( *mSharedStack );
452 return stack.queued;
453}
454
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700455status_t SharedBufferServer::assertReallocate(int buffer)
456{
457 ReallocateCondition condition(this, buffer);
458 status_t err = waitForCondition(condition);
459 return err;
460}
461
462Region SharedBufferServer::getDirtyRegion(int buffer) const
463{
464 SharedBufferStack& stack( *mSharedStack );
465 return stack.getDirtyRegion(buffer);
466}
467
Mathias Agopian86f73292009-09-17 01:35:28 -0700468SharedBufferStack::Statistics SharedBufferServer::getStats() const
469{
470 SharedBufferStack& stack( *mSharedStack );
471 return stack.stats;
472}
473
474
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700475// ---------------------------------------------------------------------------
476}; // namespace android