blob: 4ad9f861caba8c49c5810bf1e2150620d3912298 [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 {
Mathias Agopianbb641242010-05-18 17:06:55 -070047 if (uint32_t(i) >= uint32_t(SharedBufferStack::NUM_LAYERS_MAX))
Mathias Agopiancbb288b2009-09-07 16:32:45 -070048 return BAD_INDEX;
49 return surfaces[i].status;
50}
51
Mathias Agopiancbb288b2009-09-07 16:32:45 -070052// ----------------------------------------------------------------------------
53
54
55SharedBufferStack::SharedBufferStack()
Mathias Agopiancbb288b2009-09-07 16:32:45 -070056{
57}
58
Mathias Agopian48d819a2009-09-10 19:41:18 -070059void SharedBufferStack::init(int32_t i)
60{
Mathias Agopianb7e930d2010-06-01 15:12:58 -070061 inUse = -2;
Mathias Agopian48d819a2009-09-10 19:41:18 -070062 status = NO_ERROR;
63 identity = i;
64}
65
Mathias Agopiancc08e682010-04-15 18:48:26 -070066status_t SharedBufferStack::setCrop(int buffer, const Rect& crop)
67{
68 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
69 return BAD_INDEX;
70
71 buffers[buffer].crop.l = uint16_t(crop.left);
72 buffers[buffer].crop.t = uint16_t(crop.top);
73 buffers[buffer].crop.r = uint16_t(crop.right);
74 buffers[buffer].crop.b = uint16_t(crop.bottom);
75 return NO_ERROR;
76}
77
Mathias Agopianb661d662010-08-19 17:01:19 -070078status_t SharedBufferStack::setTransform(int buffer, uint8_t transform)
79{
80 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
81 return BAD_INDEX;
82 buffers[buffer].transform = transform;
83 return NO_ERROR;
84}
85
Mathias Agopiancbb288b2009-09-07 16:32:45 -070086status_t SharedBufferStack::setDirtyRegion(int buffer, const Region& dirty)
87{
88 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
89 return BAD_INDEX;
90
Mathias Agopian245e4d72010-04-21 15:24:11 -070091 FlatRegion& reg(buffers[buffer].dirtyRegion);
92 if (dirty.isEmpty()) {
93 reg.count = 0;
94 return NO_ERROR;
95 }
96
Mathias Agopian1100c8b2010-04-05 16:21:53 -070097 size_t count;
98 Rect const* r = dirty.getArray(&count);
Mathias Agopian1100c8b2010-04-05 16:21:53 -070099 if (count > FlatRegion::NUM_RECT_MAX) {
100 const Rect bounds(dirty.getBounds());
101 reg.count = 1;
Mathias Agopiancc08e682010-04-15 18:48:26 -0700102 reg.rects[0].l = uint16_t(bounds.left);
103 reg.rects[0].t = uint16_t(bounds.top);
104 reg.rects[0].r = uint16_t(bounds.right);
105 reg.rects[0].b = uint16_t(bounds.bottom);
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700106 } else {
107 reg.count = count;
108 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancc08e682010-04-15 18:48:26 -0700109 reg.rects[i].l = uint16_t(r[i].left);
110 reg.rects[i].t = uint16_t(r[i].top);
111 reg.rects[i].r = uint16_t(r[i].right);
112 reg.rects[i].b = uint16_t(r[i].bottom);
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700113 }
114 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700115 return NO_ERROR;
116}
117
118Region SharedBufferStack::getDirtyRegion(int buffer) const
119{
120 Region res;
121 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
122 return res;
123
Mathias Agopiancc08e682010-04-15 18:48:26 -0700124 const FlatRegion& reg(buffers[buffer].dirtyRegion);
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700125 if (reg.count > FlatRegion::NUM_RECT_MAX)
126 return res;
127
128 if (reg.count == 1) {
Mathias Agopiancc08e682010-04-15 18:48:26 -0700129 const Rect r(
130 reg.rects[0].l,
131 reg.rects[0].t,
132 reg.rects[0].r,
133 reg.rects[0].b);
134 res.set(r);
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700135 } else {
136 for (size_t i=0 ; i<reg.count ; i++) {
137 const Rect r(
Mathias Agopiancc08e682010-04-15 18:48:26 -0700138 reg.rects[i].l,
139 reg.rects[i].t,
140 reg.rects[i].r,
141 reg.rects[i].b);
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700142 res.orSelf(r);
143 }
144 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700145 return res;
146}
147
Mathias Agopianb661d662010-08-19 17:01:19 -0700148Rect SharedBufferStack::getCrop(int buffer) const
149{
150 Rect res(-1, -1);
151 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
152 return res;
153 res.left = buffers[buffer].crop.l;
154 res.top = buffers[buffer].crop.t;
155 res.right = buffers[buffer].crop.r;
156 res.bottom = buffers[buffer].crop.b;
157 return res;
158}
159
160uint32_t SharedBufferStack::getTransform(int buffer) const
161{
162 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
163 return 0;
164 return buffers[buffer].transform;
165}
166
167
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700168// ----------------------------------------------------------------------------
169
170SharedBufferBase::SharedBufferBase(SharedClient* sharedClient,
Mathias Agopianbb641242010-05-18 17:06:55 -0700171 int surface, int32_t identity)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700172 : mSharedClient(sharedClient),
173 mSharedStack(sharedClient->surfaces + surface),
Mathias Agopianbb641242010-05-18 17:06:55 -0700174 mIdentity(identity)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700175{
176}
177
178SharedBufferBase::~SharedBufferBase()
179{
180}
181
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700182status_t SharedBufferBase::getStatus() const
183{
184 SharedBufferStack& stack( *mSharedStack );
185 return stack.status;
186}
187
Mathias Agopian7e27f052010-05-28 14:22:23 -0700188int32_t SharedBufferBase::getIdentity() const
189{
190 SharedBufferStack& stack( *mSharedStack );
191 return stack.identity;
192}
193
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700194size_t SharedBufferBase::getFrontBuffer() const
195{
196 SharedBufferStack& stack( *mSharedStack );
197 return size_t( stack.head );
198}
199
200String8 SharedBufferBase::dump(char const* prefix) const
201{
202 const size_t SIZE = 1024;
203 char buffer[SIZE];
204 String8 result;
205 SharedBufferStack& stack( *mSharedStack );
206 snprintf(buffer, SIZE,
Mathias Agopianbb641242010-05-18 17:06:55 -0700207 "%s[ head=%2d, available=%2d, queued=%2d ] "
Mathias Agopianb5c45772010-05-17 18:54:19 -0700208 "reallocMask=%08x, inUse=%2d, identity=%d, status=%d",
Mathias Agopianbb641242010-05-18 17:06:55 -0700209 prefix, stack.head, stack.available, stack.queued,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700210 stack.reallocMask, stack.inUse, stack.identity, stack.status);
211 result.append(buffer);
Mathias Agopianbb641242010-05-18 17:06:55 -0700212 result.append("\n");
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700213 return result;
214}
215
Mathias Agopianb2965332010-04-27 16:41:19 -0700216status_t SharedBufferBase::waitForCondition(const ConditionBase& condition)
217{
218 const SharedBufferStack& stack( *mSharedStack );
219 SharedClient& client( *mSharedClient );
220 const nsecs_t TIMEOUT = s2ns(1);
221 const int identity = mIdentity;
222
223 Mutex::Autolock _l(client.lock);
224 while ((condition()==false) &&
225 (stack.identity == identity) &&
226 (stack.status == NO_ERROR))
227 {
228 status_t err = client.cv.waitRelative(client.lock, TIMEOUT);
229 // handle errors and timeouts
230 if (CC_UNLIKELY(err != NO_ERROR)) {
231 if (err == TIMED_OUT) {
232 if (condition()) {
233 LOGE("waitForCondition(%s) timed out (identity=%d), "
234 "but condition is true! We recovered but it "
235 "shouldn't happen." , condition.name(), stack.identity);
236 break;
237 } else {
238 LOGW("waitForCondition(%s) timed out "
239 "(identity=%d, status=%d). "
240 "CPU may be pegged. trying again.", condition.name(),
241 stack.identity, stack.status);
242 }
243 } else {
244 LOGE("waitForCondition(%s) error (%s) ",
245 condition.name(), strerror(-err));
246 return err;
247 }
248 }
249 }
250 return (stack.identity != mIdentity) ? status_t(BAD_INDEX) : stack.status;
251}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700252// ============================================================================
253// conditions and updates
254// ============================================================================
255
256SharedBufferClient::DequeueCondition::DequeueCondition(
257 SharedBufferClient* sbc) : ConditionBase(sbc) {
258}
Mathias Agopianb2965332010-04-27 16:41:19 -0700259bool SharedBufferClient::DequeueCondition::operator()() const {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700260 return stack.available > 0;
261}
262
263SharedBufferClient::LockCondition::LockCondition(
264 SharedBufferClient* sbc, int buf) : ConditionBase(sbc), buf(buf) {
265}
Mathias Agopianb2965332010-04-27 16:41:19 -0700266bool SharedBufferClient::LockCondition::operator()() const {
Mathias Agopiand5212872010-04-30 12:59:21 -0700267 // NOTE: if stack.head is messed up, we could crash the client
268 // or cause some drawing artifacts. This is okay, as long as it is
269 // limited to the client.
Mathias Agopianc0a91642010-04-27 21:08:20 -0700270 return (buf != stack.index[stack.head] ||
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700271 (stack.queued > 0 && stack.inUse != buf));
272}
273
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700274// ----------------------------------------------------------------------------
275
276SharedBufferClient::QueueUpdate::QueueUpdate(SharedBufferBase* sbb)
277 : UpdateBase(sbb) {
278}
279ssize_t SharedBufferClient::QueueUpdate::operator()() {
280 android_atomic_inc(&stack.queued);
281 return NO_ERROR;
282}
283
284SharedBufferClient::UndoDequeueUpdate::UndoDequeueUpdate(SharedBufferBase* sbb)
285 : UpdateBase(sbb) {
286}
287ssize_t SharedBufferClient::UndoDequeueUpdate::operator()() {
288 android_atomic_inc(&stack.available);
289 return NO_ERROR;
290}
291
292SharedBufferServer::UnlockUpdate::UnlockUpdate(
293 SharedBufferBase* sbb, int lockedBuffer)
294 : UpdateBase(sbb), lockedBuffer(lockedBuffer) {
295}
296ssize_t SharedBufferServer::UnlockUpdate::operator()() {
297 if (stack.inUse != lockedBuffer) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700298 LOGE("unlocking %d, but currently locked buffer is %d "
299 "(identity=%d, token=%d)",
300 lockedBuffer, stack.inUse,
301 stack.identity, stack.token);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700302 return BAD_VALUE;
303 }
304 android_atomic_write(-1, &stack.inUse);
305 return NO_ERROR;
306}
307
308SharedBufferServer::RetireUpdate::RetireUpdate(
309 SharedBufferBase* sbb, int numBuffers)
310 : UpdateBase(sbb), numBuffers(numBuffers) {
311}
312ssize_t SharedBufferServer::RetireUpdate::operator()() {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700313 int32_t head = stack.head;
Mathias Agopianbb641242010-05-18 17:06:55 -0700314 if (uint32_t(head) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopiand5212872010-04-30 12:59:21 -0700315 return BAD_VALUE;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700316
317 // Preventively lock the current buffer before updating queued.
Mathias Agopianc0a91642010-04-27 21:08:20 -0700318 android_atomic_write(stack.index[head], &stack.inUse);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700319
320 // Decrement the number of queued buffers
321 int32_t queued;
322 do {
323 queued = stack.queued;
324 if (queued == 0) {
325 return NOT_ENOUGH_DATA;
326 }
327 } while (android_atomic_cmpxchg(queued, queued-1, &stack.queued));
328
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700329 // lock the buffer before advancing head, which automatically unlocks
330 // the buffer we preventively locked upon entering this function
Mathias Agopianb5c45772010-05-17 18:54:19 -0700331
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700332 head = (head + 1) % numBuffers;
Mathias Agopianc0a91642010-04-27 21:08:20 -0700333 android_atomic_write(stack.index[head], &stack.inUse);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700334
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700335 // head is only modified here, so we don't need to use cmpxchg
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700336 android_atomic_write(head, &stack.head);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700337
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700338 // now that head has moved, we can increment the number of available buffers
339 android_atomic_inc(&stack.available);
340 return head;
341}
342
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700343SharedBufferServer::StatusUpdate::StatusUpdate(
344 SharedBufferBase* sbb, status_t status)
345 : UpdateBase(sbb), status(status) {
346}
347
348ssize_t SharedBufferServer::StatusUpdate::operator()() {
349 android_atomic_write(status, &stack.status);
350 return NO_ERROR;
351}
352
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700353// ============================================================================
354
355SharedBufferClient::SharedBufferClient(SharedClient* sharedClient,
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700356 int surface, int num, int32_t identity)
Mathias Agopianbb641242010-05-18 17:06:55 -0700357 : SharedBufferBase(sharedClient, surface, identity),
358 mNumBuffers(num), tail(0), undoDequeueTail(0)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700359{
Mathias Agopianc0a91642010-04-27 21:08:20 -0700360 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianc7d56012009-09-14 15:48:42 -0700361 tail = computeTail();
Mathias Agopianc0a91642010-04-27 21:08:20 -0700362 queued_head = stack.head;
Mathias Agopianc7d56012009-09-14 15:48:42 -0700363}
364
Mathias Agopianbb641242010-05-18 17:06:55 -0700365int32_t SharedBufferClient::computeTail() const
366{
367 SharedBufferStack& stack( *mSharedStack );
368 return (mNumBuffers + stack.head - stack.available + 1) % mNumBuffers;
369}
370
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700371ssize_t SharedBufferClient::dequeue()
372{
Mathias Agopian40d57992009-09-11 19:18:20 -0700373 SharedBufferStack& stack( *mSharedStack );
374
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700375 if (stack.head == tail && stack.available == mNumBuffers) {
Mathias Agopian40d57992009-09-11 19:18:20 -0700376 LOGW("dequeue: tail=%d, head=%d, avail=%d, queued=%d",
377 tail, stack.head, stack.available, stack.queued);
378 }
Mathias Agopianbb641242010-05-18 17:06:55 -0700379
380 RWLock::AutoRLock _rd(mLock);
381
Mathias Agopian86f73292009-09-17 01:35:28 -0700382 const nsecs_t dequeueTime = systemTime(SYSTEM_TIME_THREAD);
Mathias Agopian40d57992009-09-11 19:18:20 -0700383
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700384 //LOGD("[%d] about to dequeue a buffer",
385 // mSharedStack->identity);
386 DequeueCondition condition(this);
387 status_t err = waitForCondition(condition);
388 if (err != NO_ERROR)
389 return ssize_t(err);
390
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700391 // NOTE: 'stack.available' is part of the conditions, however
392 // decrementing it, never changes any conditions, so we don't need
393 // to do this as part of an update.
394 if (android_atomic_dec(&stack.available) == 0) {
395 LOGW("dequeue probably called from multiple threads!");
396 }
397
Mathias Agopianc0a91642010-04-27 21:08:20 -0700398 undoDequeueTail = tail;
399 int dequeued = stack.index[tail];
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700400 tail = ((tail+1 >= mNumBuffers) ? 0 : tail+1);
Mathias Agopianc0a91642010-04-27 21:08:20 -0700401 LOGD_IF(DEBUG_ATOMICS, "dequeued=%d, tail++=%d, %s",
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700402 dequeued, tail, dump("").string());
Mathias Agopian40d57992009-09-11 19:18:20 -0700403
Mathias Agopian86f73292009-09-17 01:35:28 -0700404 mDequeueTime[dequeued] = dequeueTime;
405
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700406 return dequeued;
407}
408
409status_t SharedBufferClient::undoDequeue(int buf)
410{
Mathias Agopianbb641242010-05-18 17:06:55 -0700411 RWLock::AutoRLock _rd(mLock);
412
Mathias Agopianc0a91642010-04-27 21:08:20 -0700413 // TODO: we can only undo the previous dequeue, we should
414 // enforce that in the api
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700415 UndoDequeueUpdate update(this);
416 status_t err = updateCondition( update );
Mathias Agopianc7d56012009-09-14 15:48:42 -0700417 if (err == NO_ERROR) {
Mathias Agopian0a8cd062010-04-27 16:11:38 -0700418 tail = undoDequeueTail;
Mathias Agopianc7d56012009-09-14 15:48:42 -0700419 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700420 return err;
421}
422
423status_t SharedBufferClient::lock(int buf)
424{
Mathias Agopianbb641242010-05-18 17:06:55 -0700425 RWLock::AutoRLock _rd(mLock);
426
Mathias Agopianc0a91642010-04-27 21:08:20 -0700427 SharedBufferStack& stack( *mSharedStack );
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700428 LockCondition condition(this, buf);
Mathias Agopian86f73292009-09-17 01:35:28 -0700429 status_t err = waitForCondition(condition);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700430 return err;
431}
432
433status_t SharedBufferClient::queue(int buf)
434{
Mathias Agopianbb641242010-05-18 17:06:55 -0700435 RWLock::AutoRLock _rd(mLock);
436
Mathias Agopianc0a91642010-04-27 21:08:20 -0700437 SharedBufferStack& stack( *mSharedStack );
438
Mathias Agopianb5c45772010-05-17 18:54:19 -0700439 queued_head = (queued_head + 1) % mNumBuffers;
Mathias Agopianc0a91642010-04-27 21:08:20 -0700440 stack.index[queued_head] = buf;
441
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700442 QueueUpdate update(this);
443 status_t err = updateCondition( update );
444 LOGD_IF(DEBUG_ATOMICS, "queued=%d, %s", buf, dump("").string());
Mathias Agopianc0a91642010-04-27 21:08:20 -0700445
Mathias Agopian86f73292009-09-17 01:35:28 -0700446 const nsecs_t now = systemTime(SYSTEM_TIME_THREAD);
447 stack.stats.totalTime = ns2us(now - mDequeueTime[buf]);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700448 return err;
449}
450
Mathias Agopianc0a91642010-04-27 21:08:20 -0700451bool SharedBufferClient::needNewBuffer(int buf) const
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700452{
453 SharedBufferStack& stack( *mSharedStack );
Mathias Agopiana0b3f1d2010-05-21 14:51:33 -0700454 const uint32_t mask = 1<<(31-buf);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700455 return (android_atomic_and(~mask, &stack.reallocMask) & mask) != 0;
456}
457
Mathias Agopianc0a91642010-04-27 21:08:20 -0700458status_t SharedBufferClient::setCrop(int buf, const Rect& crop)
Mathias Agopiancc08e682010-04-15 18:48:26 -0700459{
460 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianc0a91642010-04-27 21:08:20 -0700461 return stack.setCrop(buf, crop);
Mathias Agopiancc08e682010-04-15 18:48:26 -0700462}
463
Mathias Agopianb661d662010-08-19 17:01:19 -0700464status_t SharedBufferClient::setTransform(int buf, uint32_t transform)
465{
466 SharedBufferStack& stack( *mSharedStack );
467 return stack.setTransform(buf, uint8_t(transform));
468}
469
Mathias Agopianc0a91642010-04-27 21:08:20 -0700470status_t SharedBufferClient::setDirtyRegion(int buf, const Region& reg)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700471{
472 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianc0a91642010-04-27 21:08:20 -0700473 return stack.setDirtyRegion(buf, reg);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700474}
475
Mathias Agopianbb641242010-05-18 17:06:55 -0700476status_t SharedBufferClient::setBufferCount(
477 int bufferCount, const SetBufferCountCallback& ipc)
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700478{
Mathias Agopianb5c45772010-05-17 18:54:19 -0700479 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianbb641242010-05-18 17:06:55 -0700480 if (uint32_t(bufferCount) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700481 return BAD_VALUE;
Mathias Agopianbb641242010-05-18 17:06:55 -0700482
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700483 if (uint32_t(bufferCount) < SharedBufferStack::NUM_BUFFER_MIN)
484 return BAD_VALUE;
485
Mathias Agopianbb641242010-05-18 17:06:55 -0700486 RWLock::AutoWLock _wr(mLock);
487
488 status_t err = ipc(bufferCount);
489 if (err == NO_ERROR) {
490 mNumBuffers = bufferCount;
491 queued_head = (stack.head + stack.queued) % mNumBuffers;
492 }
493 return err;
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700494}
495
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700496// ----------------------------------------------------------------------------
497
498SharedBufferServer::SharedBufferServer(SharedClient* sharedClient,
Mathias Agopian48d819a2009-09-10 19:41:18 -0700499 int surface, int num, int32_t identity)
Mathias Agopianbb641242010-05-18 17:06:55 -0700500 : SharedBufferBase(sharedClient, surface, identity),
501 mNumBuffers(num)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700502{
Mathias Agopian48d819a2009-09-10 19:41:18 -0700503 mSharedStack->init(identity);
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700504 mSharedStack->token = surface;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700505 mSharedStack->head = num-1;
506 mSharedStack->available = num;
507 mSharedStack->queued = 0;
508 mSharedStack->reallocMask = 0;
Mathias Agopiancc08e682010-04-15 18:48:26 -0700509 memset(mSharedStack->buffers, 0, sizeof(mSharedStack->buffers));
Mathias Agopianc0a91642010-04-27 21:08:20 -0700510 for (int i=0 ; i<num ; i++) {
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700511 mBufferList.add(i);
Mathias Agopianc0a91642010-04-27 21:08:20 -0700512 mSharedStack->index[i] = i;
513 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700514}
515
Mathias Agopian579b3f82010-06-08 19:54:15 -0700516SharedBufferServer::~SharedBufferServer()
517{
518}
519
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700520ssize_t SharedBufferServer::retireAndLock()
521{
Mathias Agopianbb641242010-05-18 17:06:55 -0700522 RWLock::AutoRLock _l(mLock);
523
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700524 RetireUpdate update(this, mNumBuffers);
525 ssize_t buf = updateCondition( update );
Mathias Agopianc0a91642010-04-27 21:08:20 -0700526 if (buf >= 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700527 if (uint32_t(buf) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopiand5212872010-04-30 12:59:21 -0700528 return BAD_VALUE;
Mathias Agopianc0a91642010-04-27 21:08:20 -0700529 SharedBufferStack& stack( *mSharedStack );
530 buf = stack.index[buf];
531 LOGD_IF(DEBUG_ATOMICS && buf>=0, "retire=%d, %s",
532 int(buf), dump("").string());
533 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700534 return buf;
535}
536
Mathias Agopianc0a91642010-04-27 21:08:20 -0700537status_t SharedBufferServer::unlock(int buf)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700538{
Mathias Agopianc0a91642010-04-27 21:08:20 -0700539 UnlockUpdate update(this, buf);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700540 status_t err = updateCondition( update );
541 return err;
542}
543
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700544void SharedBufferServer::setStatus(status_t status)
545{
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700546 if (status < NO_ERROR) {
547 StatusUpdate update(this, status);
548 updateCondition( update );
549 }
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700550}
551
Mathias Agopiana138f892010-05-21 17:24:35 -0700552status_t SharedBufferServer::reallocateAll()
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700553{
Mathias Agopianbb641242010-05-18 17:06:55 -0700554 RWLock::AutoRLock _l(mLock);
555
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700556 SharedBufferStack& stack( *mSharedStack );
Mathias Agopiana0b3f1d2010-05-21 14:51:33 -0700557 uint32_t mask = mBufferList.getMask();
Mathias Agopiana138f892010-05-21 17:24:35 -0700558 android_atomic_or(mask, &stack.reallocMask);
559 return NO_ERROR;
560}
561
562status_t SharedBufferServer::reallocateAllExcept(int buffer)
563{
564 RWLock::AutoRLock _l(mLock);
565
566 SharedBufferStack& stack( *mSharedStack );
567 BufferList temp(mBufferList);
568 temp.remove(buffer);
569 uint32_t mask = temp.getMask();
570 android_atomic_or(mask, &stack.reallocMask);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700571 return NO_ERROR;
572}
573
Mathias Agopiane7005012009-10-07 16:44:10 -0700574int32_t SharedBufferServer::getQueuedCount() const
575{
576 SharedBufferStack& stack( *mSharedStack );
577 return stack.queued;
578}
579
Mathias Agopianc0a91642010-04-27 21:08:20 -0700580Region SharedBufferServer::getDirtyRegion(int buf) const
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700581{
582 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianc0a91642010-04-27 21:08:20 -0700583 return stack.getDirtyRegion(buf);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700584}
585
Mathias Agopianb661d662010-08-19 17:01:19 -0700586Rect SharedBufferServer::getCrop(int buf) const
587{
588 SharedBufferStack& stack( *mSharedStack );
589 return stack.getCrop(buf);
590}
591
592uint32_t SharedBufferServer::getTransform(int buf) const
593{
594 SharedBufferStack& stack( *mSharedStack );
595 return stack.getTransform(buf);
596}
597
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700598/*
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700599 * NOTE: this is not thread-safe on the server-side, meaning
600 * 'head' cannot move during this operation. The client-side
601 * can safely operate an usual.
602 *
603 */
604status_t SharedBufferServer::resize(int newNumBuffers)
605{
Mathias Agopianbb641242010-05-18 17:06:55 -0700606 if (uint32_t(newNumBuffers) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700607 return BAD_VALUE;
608
Mathias Agopianbb641242010-05-18 17:06:55 -0700609 RWLock::AutoWLock _l(mLock);
610
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700611 // for now we're not supporting shrinking
612 const int numBuffers = mNumBuffers;
613 if (newNumBuffers < numBuffers)
614 return BAD_VALUE;
615
616 SharedBufferStack& stack( *mSharedStack );
617 const int extra = newNumBuffers - numBuffers;
618
619 // read the head, make sure it's valid
620 int32_t head = stack.head;
Mathias Agopianbb641242010-05-18 17:06:55 -0700621 if (uint32_t(head) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700622 return BAD_VALUE;
623
624 int base = numBuffers;
625 int32_t avail = stack.available;
626 int tail = head - avail + 1;
Mathias Agopiand6297f72010-05-17 17:27:26 -0700627
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700628 if (tail >= 0) {
629 int8_t* const index = const_cast<int8_t*>(stack.index);
630 const int nb = numBuffers - head;
631 memmove(&index[head + extra], &index[head], nb);
632 base = head;
633 // move head 'extra' ahead, this doesn't impact stack.index[head];
634 stack.head = head + extra;
635 }
636 stack.available += extra;
637
638 // fill the new free space with unused buffers
639 BufferList::const_iterator curr(mBufferList.free_begin());
640 for (int i=0 ; i<extra ; i++) {
Mathias Agopiand6297f72010-05-17 17:27:26 -0700641 stack.index[base+i] = *curr;
642 mBufferList.add(*curr);
643 ++curr;
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700644 }
645
646 mNumBuffers = newNumBuffers;
647 return NO_ERROR;
648}
649
Mathias Agopian86f73292009-09-17 01:35:28 -0700650SharedBufferStack::Statistics SharedBufferServer::getStats() const
651{
652 SharedBufferStack& stack( *mSharedStack );
653 return stack.stats;
654}
655
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700656// ---------------------------------------------------------------------------
657status_t SharedBufferServer::BufferList::add(int value)
658{
659 if (uint32_t(value) >= mCapacity)
660 return BAD_VALUE;
661 uint32_t mask = 1<<(31-value);
662 if (mList & mask)
663 return ALREADY_EXISTS;
664 mList |= mask;
665 return NO_ERROR;
666}
667
668status_t SharedBufferServer::BufferList::remove(int value)
669{
670 if (uint32_t(value) >= mCapacity)
671 return BAD_VALUE;
672 uint32_t mask = 1<<(31-value);
673 if (!(mList & mask))
674 return NAME_NOT_FOUND;
675 mList &= ~mask;
676 return NO_ERROR;
677}
678
Mathias Agopian86f73292009-09-17 01:35:28 -0700679
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700680// ---------------------------------------------------------------------------
681}; // namespace android