blob: 5c4f9207c7c1b110feb4515792c8b6718787164a [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
Mathias Agopian1100c8b2010-04-05 16:21:53 -070076 size_t count;
77 Rect const* r = dirty.getArray(&count);
Mathias Agopiancbb288b2009-09-07 16:32:45 -070078 FlatRegion& reg(dirtyRegion[buffer]);
Mathias Agopian1100c8b2010-04-05 16:21:53 -070079 if (count > FlatRegion::NUM_RECT_MAX) {
80 const Rect bounds(dirty.getBounds());
81 reg.count = 1;
82 reg.rects[0] = uint16_t(bounds.left);
83 reg.rects[1] = uint16_t(bounds.top);
84 reg.rects[2] = uint16_t(bounds.right);
85 reg.rects[3] = uint16_t(bounds.bottom);
86 } else {
87 reg.count = count;
88 for (size_t i=0 ; i<count ; i++) {
89 reg.rects[i*4 + 0] = uint16_t(r[i].left);
90 reg.rects[i*4 + 1] = uint16_t(r[i].top);
91 reg.rects[i*4 + 2] = uint16_t(r[i].right);
92 reg.rects[i*4 + 3] = uint16_t(r[i].bottom);
93 }
94 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -070095 return NO_ERROR;
96}
97
98Region SharedBufferStack::getDirtyRegion(int buffer) const
99{
100 Region res;
101 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
102 return res;
103
104 const FlatRegion& reg(dirtyRegion[buffer]);
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700105 if (reg.count > FlatRegion::NUM_RECT_MAX)
106 return res;
107
108 if (reg.count == 1) {
109 res.set(Rect(reg.rects[0], reg.rects[1], reg.rects[2], reg.rects[3]));
110 } else {
111 for (size_t i=0 ; i<reg.count ; i++) {
112 const Rect r(
113 reg.rects[i*4 + 0],
114 reg.rects[i*4 + 1],
115 reg.rects[i*4 + 2],
116 reg.rects[i*4 + 3]);
117 res.orSelf(r);
118 }
119 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700120 return res;
121}
122
123// ----------------------------------------------------------------------------
124
125SharedBufferBase::SharedBufferBase(SharedClient* sharedClient,
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700126 int surface, int num, int32_t identity)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700127 : mSharedClient(sharedClient),
128 mSharedStack(sharedClient->surfaces + surface),
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700129 mNumBuffers(num), mIdentity(identity)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700130{
131}
132
133SharedBufferBase::~SharedBufferBase()
134{
135}
136
137uint32_t SharedBufferBase::getIdentity()
138{
139 SharedBufferStack& stack( *mSharedStack );
140 return stack.identity;
141}
142
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700143status_t SharedBufferBase::getStatus() const
144{
145 SharedBufferStack& stack( *mSharedStack );
146 return stack.status;
147}
148
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700149size_t SharedBufferBase::getFrontBuffer() const
150{
151 SharedBufferStack& stack( *mSharedStack );
152 return size_t( stack.head );
153}
154
155String8 SharedBufferBase::dump(char const* prefix) const
156{
157 const size_t SIZE = 1024;
158 char buffer[SIZE];
159 String8 result;
160 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianc2e30de2010-03-08 19:23:26 -0800161 int tail = (mNumBuffers + stack.head - stack.available + 1) % mNumBuffers;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700162 snprintf(buffer, SIZE,
Mathias Agopianc2e30de2010-03-08 19:23:26 -0800163 "%s[ head=%2d, available=%2d, queued=%2d, tail=%2d ] "
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700164 "reallocMask=%08x, inUse=%2d, identity=%d, status=%d\n",
Mathias Agopianc2e30de2010-03-08 19:23:26 -0800165 prefix, stack.head, stack.available, stack.queued, tail,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700166 stack.reallocMask, stack.inUse, stack.identity, stack.status);
167 result.append(buffer);
168 return result;
169}
170
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700171// ============================================================================
172// conditions and updates
173// ============================================================================
174
175SharedBufferClient::DequeueCondition::DequeueCondition(
176 SharedBufferClient* sbc) : ConditionBase(sbc) {
177}
178bool SharedBufferClient::DequeueCondition::operator()() {
179 return stack.available > 0;
180}
181
182SharedBufferClient::LockCondition::LockCondition(
183 SharedBufferClient* sbc, int buf) : ConditionBase(sbc), buf(buf) {
184}
185bool SharedBufferClient::LockCondition::operator()() {
186 return (buf != stack.head ||
187 (stack.queued > 0 && stack.inUse != buf));
188}
189
190SharedBufferServer::ReallocateCondition::ReallocateCondition(
191 SharedBufferBase* sbb, int buf) : ConditionBase(sbb), buf(buf) {
192}
193bool SharedBufferServer::ReallocateCondition::operator()() {
194 // TODO: we should also check that buf has been dequeued
195 return (buf != stack.head);
196}
197
198// ----------------------------------------------------------------------------
199
200SharedBufferClient::QueueUpdate::QueueUpdate(SharedBufferBase* sbb)
201 : UpdateBase(sbb) {
202}
203ssize_t SharedBufferClient::QueueUpdate::operator()() {
204 android_atomic_inc(&stack.queued);
205 return NO_ERROR;
206}
207
208SharedBufferClient::UndoDequeueUpdate::UndoDequeueUpdate(SharedBufferBase* sbb)
209 : UpdateBase(sbb) {
210}
211ssize_t SharedBufferClient::UndoDequeueUpdate::operator()() {
212 android_atomic_inc(&stack.available);
213 return NO_ERROR;
214}
215
216SharedBufferServer::UnlockUpdate::UnlockUpdate(
217 SharedBufferBase* sbb, int lockedBuffer)
218 : UpdateBase(sbb), lockedBuffer(lockedBuffer) {
219}
220ssize_t SharedBufferServer::UnlockUpdate::operator()() {
221 if (stack.inUse != lockedBuffer) {
222 LOGE("unlocking %d, but currently locked buffer is %d",
223 lockedBuffer, stack.inUse);
224 return BAD_VALUE;
225 }
226 android_atomic_write(-1, &stack.inUse);
227 return NO_ERROR;
228}
229
230SharedBufferServer::RetireUpdate::RetireUpdate(
231 SharedBufferBase* sbb, int numBuffers)
232 : UpdateBase(sbb), numBuffers(numBuffers) {
233}
234ssize_t SharedBufferServer::RetireUpdate::operator()() {
235 // head is only written in this function, which is single-thread.
236 int32_t head = stack.head;
237
238 // Preventively lock the current buffer before updating queued.
239 android_atomic_write(head, &stack.inUse);
240
241 // Decrement the number of queued buffers
242 int32_t queued;
243 do {
244 queued = stack.queued;
245 if (queued == 0) {
246 return NOT_ENOUGH_DATA;
247 }
248 } while (android_atomic_cmpxchg(queued, queued-1, &stack.queued));
249
250 // update the head pointer
251 head = ((head+1 >= numBuffers) ? 0 : head+1);
252
253 // lock the buffer before advancing head, which automatically unlocks
254 // the buffer we preventively locked upon entering this function
255 android_atomic_write(head, &stack.inUse);
256
257 // advance head
258 android_atomic_write(head, &stack.head);
259
260 // now that head has moved, we can increment the number of available buffers
261 android_atomic_inc(&stack.available);
262 return head;
263}
264
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700265SharedBufferServer::StatusUpdate::StatusUpdate(
266 SharedBufferBase* sbb, status_t status)
267 : UpdateBase(sbb), status(status) {
268}
269
270ssize_t SharedBufferServer::StatusUpdate::operator()() {
271 android_atomic_write(status, &stack.status);
272 return NO_ERROR;
273}
274
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700275// ============================================================================
276
277SharedBufferClient::SharedBufferClient(SharedClient* sharedClient,
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700278 int surface, int num, int32_t identity)
279 : SharedBufferBase(sharedClient, surface, num, identity), tail(0)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700280{
Mathias Agopianc7d56012009-09-14 15:48:42 -0700281 tail = computeTail();
282}
283
284int32_t SharedBufferClient::computeTail() const
285{
Mathias Agopian40d57992009-09-11 19:18:20 -0700286 SharedBufferStack& stack( *mSharedStack );
Mathias Agopian40d57992009-09-11 19:18:20 -0700287 // we need to make sure we read available and head coherently,
288 // w.r.t RetireUpdate.
Mathias Agopianc7d56012009-09-14 15:48:42 -0700289 int32_t newTail;
290 int32_t avail;
291 int32_t head;
Mathias Agopian40d57992009-09-11 19:18:20 -0700292 do {
293 avail = stack.available;
294 head = stack.head;
295 } while (stack.available != avail);
Mathias Agopianc7d56012009-09-14 15:48:42 -0700296 newTail = head - avail + 1;
297 if (newTail < 0) {
298 newTail += mNumBuffers;
Mathias Agopianc2e30de2010-03-08 19:23:26 -0800299 } else if (newTail >= mNumBuffers) {
300 newTail -= mNumBuffers;
Mathias Agopian40d57992009-09-11 19:18:20 -0700301 }
Mathias Agopianc7d56012009-09-14 15:48:42 -0700302 return newTail;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700303}
304
305ssize_t SharedBufferClient::dequeue()
306{
Mathias Agopian40d57992009-09-11 19:18:20 -0700307 SharedBufferStack& stack( *mSharedStack );
308
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700309 if (stack.head == tail && stack.available == mNumBuffers) {
Mathias Agopian40d57992009-09-11 19:18:20 -0700310 LOGW("dequeue: tail=%d, head=%d, avail=%d, queued=%d",
311 tail, stack.head, stack.available, stack.queued);
312 }
Mathias Agopian86f73292009-09-17 01:35:28 -0700313
314 const nsecs_t dequeueTime = systemTime(SYSTEM_TIME_THREAD);
Mathias Agopian40d57992009-09-11 19:18:20 -0700315
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700316 //LOGD("[%d] about to dequeue a buffer",
317 // mSharedStack->identity);
318 DequeueCondition condition(this);
319 status_t err = waitForCondition(condition);
320 if (err != NO_ERROR)
321 return ssize_t(err);
322
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700323 // NOTE: 'stack.available' is part of the conditions, however
324 // decrementing it, never changes any conditions, so we don't need
325 // to do this as part of an update.
326 if (android_atomic_dec(&stack.available) == 0) {
327 LOGW("dequeue probably called from multiple threads!");
328 }
329
330 int dequeued = tail;
331 tail = ((tail+1 >= mNumBuffers) ? 0 : tail+1);
332 LOGD_IF(DEBUG_ATOMICS, "dequeued=%d, tail=%d, %s",
333 dequeued, tail, dump("").string());
Mathias Agopian40d57992009-09-11 19:18:20 -0700334
Mathias Agopian86f73292009-09-17 01:35:28 -0700335 mDequeueTime[dequeued] = dequeueTime;
336
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700337 return dequeued;
338}
339
340status_t SharedBufferClient::undoDequeue(int buf)
341{
342 UndoDequeueUpdate update(this);
343 status_t err = updateCondition( update );
Mathias Agopianc7d56012009-09-14 15:48:42 -0700344 if (err == NO_ERROR) {
345 tail = computeTail();
346 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700347 return err;
348}
349
350status_t SharedBufferClient::lock(int buf)
351{
352 LockCondition condition(this, buf);
Mathias Agopian86f73292009-09-17 01:35:28 -0700353 status_t err = waitForCondition(condition);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700354 return err;
355}
356
357status_t SharedBufferClient::queue(int buf)
358{
359 QueueUpdate update(this);
360 status_t err = updateCondition( update );
361 LOGD_IF(DEBUG_ATOMICS, "queued=%d, %s", buf, dump("").string());
Mathias Agopian86f73292009-09-17 01:35:28 -0700362 SharedBufferStack& stack( *mSharedStack );
363 const nsecs_t now = systemTime(SYSTEM_TIME_THREAD);
364 stack.stats.totalTime = ns2us(now - mDequeueTime[buf]);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700365 return err;
366}
367
368bool SharedBufferClient::needNewBuffer(int buffer) const
369{
370 SharedBufferStack& stack( *mSharedStack );
371 const uint32_t mask = 1<<buffer;
372 return (android_atomic_and(~mask, &stack.reallocMask) & mask) != 0;
373}
374
375status_t SharedBufferClient::setDirtyRegion(int buffer, const Region& reg)
376{
377 SharedBufferStack& stack( *mSharedStack );
378 return stack.setDirtyRegion(buffer, reg);
379}
380
381// ----------------------------------------------------------------------------
382
383SharedBufferServer::SharedBufferServer(SharedClient* sharedClient,
Mathias Agopian48d819a2009-09-10 19:41:18 -0700384 int surface, int num, int32_t identity)
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700385 : SharedBufferBase(sharedClient, surface, num, identity)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700386{
Mathias Agopian48d819a2009-09-10 19:41:18 -0700387 mSharedStack->init(identity);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700388 mSharedStack->head = num-1;
389 mSharedStack->available = num;
390 mSharedStack->queued = 0;
391 mSharedStack->reallocMask = 0;
392 memset(mSharedStack->dirtyRegion, 0, sizeof(mSharedStack->dirtyRegion));
393}
394
395ssize_t SharedBufferServer::retireAndLock()
396{
397 RetireUpdate update(this, mNumBuffers);
398 ssize_t buf = updateCondition( update );
Mathias Agopian40d57992009-09-11 19:18:20 -0700399 LOGD_IF(DEBUG_ATOMICS && buf>=0, "retire=%d, %s", int(buf), dump("").string());
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700400 return buf;
401}
402
403status_t SharedBufferServer::unlock(int buffer)
404{
405 UnlockUpdate update(this, buffer);
406 status_t err = updateCondition( update );
407 return err;
408}
409
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700410void SharedBufferServer::setStatus(status_t status)
411{
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700412 if (status < NO_ERROR) {
413 StatusUpdate update(this, status);
414 updateCondition( update );
415 }
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700416}
417
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700418status_t SharedBufferServer::reallocate()
419{
420 SharedBufferStack& stack( *mSharedStack );
421 uint32_t mask = (1<<mNumBuffers)-1;
422 android_atomic_or(mask, &stack.reallocMask);
423 return NO_ERROR;
424}
425
Mathias Agopiane7005012009-10-07 16:44:10 -0700426int32_t SharedBufferServer::getQueuedCount() const
427{
428 SharedBufferStack& stack( *mSharedStack );
429 return stack.queued;
430}
431
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700432status_t SharedBufferServer::assertReallocate(int buffer)
433{
434 ReallocateCondition condition(this, buffer);
435 status_t err = waitForCondition(condition);
436 return err;
437}
438
439Region SharedBufferServer::getDirtyRegion(int buffer) const
440{
441 SharedBufferStack& stack( *mSharedStack );
442 return stack.getDirtyRegion(buffer);
443}
444
Mathias Agopian86f73292009-09-17 01:35:28 -0700445SharedBufferStack::Statistics SharedBufferServer::getStats() const
446{
447 SharedBufferStack& stack( *mSharedStack );
448 return stack.stats;
449}
450
451
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700452// ---------------------------------------------------------------------------
453}; // namespace android