blob: 1dd8642516b1e3adef340d98945569d1a25f82bb [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 Agopiancbb288b2009-09-07 16:32:45 -070078status_t SharedBufferStack::setDirtyRegion(int buffer, const Region& dirty)
79{
80 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
81 return BAD_INDEX;
82
Mathias Agopian245e4d72010-04-21 15:24:11 -070083 FlatRegion& reg(buffers[buffer].dirtyRegion);
84 if (dirty.isEmpty()) {
85 reg.count = 0;
86 return NO_ERROR;
87 }
88
Mathias Agopian1100c8b2010-04-05 16:21:53 -070089 size_t count;
90 Rect const* r = dirty.getArray(&count);
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 Agopianbb641242010-05-18 17:06:55 -0700143 int surface, int32_t identity)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700144 : mSharedClient(sharedClient),
145 mSharedStack(sharedClient->surfaces + surface),
Mathias Agopianbb641242010-05-18 17:06:55 -0700146 mIdentity(identity)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700147{
148}
149
150SharedBufferBase::~SharedBufferBase()
151{
152}
153
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700154status_t SharedBufferBase::getStatus() const
155{
156 SharedBufferStack& stack( *mSharedStack );
157 return stack.status;
158}
159
Mathias Agopian7e27f052010-05-28 14:22:23 -0700160int32_t SharedBufferBase::getIdentity() const
161{
162 SharedBufferStack& stack( *mSharedStack );
163 return stack.identity;
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 );
178 snprintf(buffer, SIZE,
Mathias Agopianbb641242010-05-18 17:06:55 -0700179 "%s[ head=%2d, available=%2d, queued=%2d ] "
Mathias Agopianb5c45772010-05-17 18:54:19 -0700180 "reallocMask=%08x, inUse=%2d, identity=%d, status=%d",
Mathias Agopianbb641242010-05-18 17:06:55 -0700181 prefix, stack.head, stack.available, stack.queued,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700182 stack.reallocMask, stack.inUse, stack.identity, stack.status);
183 result.append(buffer);
Mathias Agopianbb641242010-05-18 17:06:55 -0700184 result.append("\n");
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700185 return result;
186}
187
Mathias Agopianb2965332010-04-27 16:41:19 -0700188status_t SharedBufferBase::waitForCondition(const ConditionBase& condition)
189{
190 const SharedBufferStack& stack( *mSharedStack );
191 SharedClient& client( *mSharedClient );
192 const nsecs_t TIMEOUT = s2ns(1);
193 const int identity = mIdentity;
194
195 Mutex::Autolock _l(client.lock);
196 while ((condition()==false) &&
197 (stack.identity == identity) &&
198 (stack.status == NO_ERROR))
199 {
200 status_t err = client.cv.waitRelative(client.lock, TIMEOUT);
201 // handle errors and timeouts
202 if (CC_UNLIKELY(err != NO_ERROR)) {
203 if (err == TIMED_OUT) {
204 if (condition()) {
205 LOGE("waitForCondition(%s) timed out (identity=%d), "
206 "but condition is true! We recovered but it "
207 "shouldn't happen." , condition.name(), stack.identity);
208 break;
209 } else {
210 LOGW("waitForCondition(%s) timed out "
211 "(identity=%d, status=%d). "
212 "CPU may be pegged. trying again.", condition.name(),
213 stack.identity, stack.status);
214 }
215 } else {
216 LOGE("waitForCondition(%s) error (%s) ",
217 condition.name(), strerror(-err));
218 return err;
219 }
220 }
221 }
222 return (stack.identity != mIdentity) ? status_t(BAD_INDEX) : stack.status;
223}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700224// ============================================================================
225// conditions and updates
226// ============================================================================
227
228SharedBufferClient::DequeueCondition::DequeueCondition(
229 SharedBufferClient* sbc) : ConditionBase(sbc) {
230}
Mathias Agopianb2965332010-04-27 16:41:19 -0700231bool SharedBufferClient::DequeueCondition::operator()() const {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700232 return stack.available > 0;
233}
234
235SharedBufferClient::LockCondition::LockCondition(
236 SharedBufferClient* sbc, int buf) : ConditionBase(sbc), buf(buf) {
237}
Mathias Agopianb2965332010-04-27 16:41:19 -0700238bool SharedBufferClient::LockCondition::operator()() const {
Mathias Agopiand5212872010-04-30 12:59:21 -0700239 // NOTE: if stack.head is messed up, we could crash the client
240 // or cause some drawing artifacts. This is okay, as long as it is
241 // limited to the client.
Mathias Agopianc0a91642010-04-27 21:08:20 -0700242 return (buf != stack.index[stack.head] ||
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700243 (stack.queued > 0 && stack.inUse != buf));
244}
245
246SharedBufferServer::ReallocateCondition::ReallocateCondition(
247 SharedBufferBase* sbb, int buf) : ConditionBase(sbb), buf(buf) {
248}
Mathias Agopianb2965332010-04-27 16:41:19 -0700249bool SharedBufferServer::ReallocateCondition::operator()() const {
Mathias Agopiand5212872010-04-30 12:59:21 -0700250 int32_t head = stack.head;
Mathias Agopianbb641242010-05-18 17:06:55 -0700251 if (uint32_t(head) >= SharedBufferStack::NUM_BUFFER_MAX) {
Mathias Agopiand5212872010-04-30 12:59:21 -0700252 // if stack.head is messed up, we cannot allow the server to
253 // crash (since stack.head is mapped on the client side)
254 stack.status = BAD_VALUE;
255 return false;
256 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700257 // TODO: we should also check that buf has been dequeued
Mathias Agopiand5212872010-04-30 12:59:21 -0700258 return (buf != stack.index[head]);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700259}
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) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700285 LOGE("unlocking %d, but currently locked buffer is %d "
286 "(identity=%d, token=%d)",
287 lockedBuffer, stack.inUse,
288 stack.identity, stack.token);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700289 return BAD_VALUE;
290 }
291 android_atomic_write(-1, &stack.inUse);
292 return NO_ERROR;
293}
294
295SharedBufferServer::RetireUpdate::RetireUpdate(
296 SharedBufferBase* sbb, int numBuffers)
297 : UpdateBase(sbb), numBuffers(numBuffers) {
298}
299ssize_t SharedBufferServer::RetireUpdate::operator()() {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700300 int32_t head = stack.head;
Mathias Agopianbb641242010-05-18 17:06:55 -0700301 if (uint32_t(head) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopiand5212872010-04-30 12:59:21 -0700302 return BAD_VALUE;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700303
304 // Preventively lock the current buffer before updating queued.
Mathias Agopianc0a91642010-04-27 21:08:20 -0700305 android_atomic_write(stack.index[head], &stack.inUse);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700306
307 // Decrement the number of queued buffers
308 int32_t queued;
309 do {
310 queued = stack.queued;
311 if (queued == 0) {
312 return NOT_ENOUGH_DATA;
313 }
314 } while (android_atomic_cmpxchg(queued, queued-1, &stack.queued));
315
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700316 // lock the buffer before advancing head, which automatically unlocks
317 // the buffer we preventively locked upon entering this function
Mathias Agopianb5c45772010-05-17 18:54:19 -0700318
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700319 head = (head + 1) % numBuffers;
Mathias Agopianc0a91642010-04-27 21:08:20 -0700320 android_atomic_write(stack.index[head], &stack.inUse);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700321
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700322 // head is only modified here, so we don't need to use cmpxchg
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700323 android_atomic_write(head, &stack.head);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700324
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700325 // now that head has moved, we can increment the number of available buffers
326 android_atomic_inc(&stack.available);
327 return head;
328}
329
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700330SharedBufferServer::StatusUpdate::StatusUpdate(
331 SharedBufferBase* sbb, status_t status)
332 : UpdateBase(sbb), status(status) {
333}
334
335ssize_t SharedBufferServer::StatusUpdate::operator()() {
336 android_atomic_write(status, &stack.status);
337 return NO_ERROR;
338}
339
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700340// ============================================================================
341
342SharedBufferClient::SharedBufferClient(SharedClient* sharedClient,
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700343 int surface, int num, int32_t identity)
Mathias Agopianbb641242010-05-18 17:06:55 -0700344 : SharedBufferBase(sharedClient, surface, identity),
345 mNumBuffers(num), tail(0), undoDequeueTail(0)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700346{
Mathias Agopianc0a91642010-04-27 21:08:20 -0700347 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianc7d56012009-09-14 15:48:42 -0700348 tail = computeTail();
Mathias Agopianc0a91642010-04-27 21:08:20 -0700349 queued_head = stack.head;
Mathias Agopianc7d56012009-09-14 15:48:42 -0700350}
351
Mathias Agopianbb641242010-05-18 17:06:55 -0700352int32_t SharedBufferClient::computeTail() const
353{
354 SharedBufferStack& stack( *mSharedStack );
355 return (mNumBuffers + stack.head - stack.available + 1) % mNumBuffers;
356}
357
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700358ssize_t SharedBufferClient::dequeue()
359{
Mathias Agopian40d57992009-09-11 19:18:20 -0700360 SharedBufferStack& stack( *mSharedStack );
361
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700362 if (stack.head == tail && stack.available == mNumBuffers) {
Mathias Agopian40d57992009-09-11 19:18:20 -0700363 LOGW("dequeue: tail=%d, head=%d, avail=%d, queued=%d",
364 tail, stack.head, stack.available, stack.queued);
365 }
Mathias Agopianbb641242010-05-18 17:06:55 -0700366
367 RWLock::AutoRLock _rd(mLock);
368
Mathias Agopian86f73292009-09-17 01:35:28 -0700369 const nsecs_t dequeueTime = systemTime(SYSTEM_TIME_THREAD);
Mathias Agopian40d57992009-09-11 19:18:20 -0700370
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700371 //LOGD("[%d] about to dequeue a buffer",
372 // mSharedStack->identity);
373 DequeueCondition condition(this);
374 status_t err = waitForCondition(condition);
375 if (err != NO_ERROR)
376 return ssize_t(err);
377
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700378 // NOTE: 'stack.available' is part of the conditions, however
379 // decrementing it, never changes any conditions, so we don't need
380 // to do this as part of an update.
381 if (android_atomic_dec(&stack.available) == 0) {
382 LOGW("dequeue probably called from multiple threads!");
383 }
384
Mathias Agopianc0a91642010-04-27 21:08:20 -0700385 undoDequeueTail = tail;
386 int dequeued = stack.index[tail];
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700387 tail = ((tail+1 >= mNumBuffers) ? 0 : tail+1);
Mathias Agopianc0a91642010-04-27 21:08:20 -0700388 LOGD_IF(DEBUG_ATOMICS, "dequeued=%d, tail++=%d, %s",
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700389 dequeued, tail, dump("").string());
Mathias Agopian40d57992009-09-11 19:18:20 -0700390
Mathias Agopian86f73292009-09-17 01:35:28 -0700391 mDequeueTime[dequeued] = dequeueTime;
392
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700393 return dequeued;
394}
395
396status_t SharedBufferClient::undoDequeue(int buf)
397{
Mathias Agopianbb641242010-05-18 17:06:55 -0700398 RWLock::AutoRLock _rd(mLock);
399
Mathias Agopianc0a91642010-04-27 21:08:20 -0700400 // TODO: we can only undo the previous dequeue, we should
401 // enforce that in the api
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700402 UndoDequeueUpdate update(this);
403 status_t err = updateCondition( update );
Mathias Agopianc7d56012009-09-14 15:48:42 -0700404 if (err == NO_ERROR) {
Mathias Agopian0a8cd062010-04-27 16:11:38 -0700405 tail = undoDequeueTail;
Mathias Agopianc7d56012009-09-14 15:48:42 -0700406 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700407 return err;
408}
409
410status_t SharedBufferClient::lock(int buf)
411{
Mathias Agopianbb641242010-05-18 17:06:55 -0700412 RWLock::AutoRLock _rd(mLock);
413
Mathias Agopianc0a91642010-04-27 21:08:20 -0700414 SharedBufferStack& stack( *mSharedStack );
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700415 LockCondition condition(this, buf);
Mathias Agopian86f73292009-09-17 01:35:28 -0700416 status_t err = waitForCondition(condition);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700417 return err;
418}
419
420status_t SharedBufferClient::queue(int buf)
421{
Mathias Agopianbb641242010-05-18 17:06:55 -0700422 RWLock::AutoRLock _rd(mLock);
423
Mathias Agopianc0a91642010-04-27 21:08:20 -0700424 SharedBufferStack& stack( *mSharedStack );
425
Mathias Agopianb5c45772010-05-17 18:54:19 -0700426 queued_head = (queued_head + 1) % mNumBuffers;
Mathias Agopianc0a91642010-04-27 21:08:20 -0700427 stack.index[queued_head] = buf;
428
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700429 QueueUpdate update(this);
430 status_t err = updateCondition( update );
431 LOGD_IF(DEBUG_ATOMICS, "queued=%d, %s", buf, dump("").string());
Mathias Agopianc0a91642010-04-27 21:08:20 -0700432
Mathias Agopian86f73292009-09-17 01:35:28 -0700433 const nsecs_t now = systemTime(SYSTEM_TIME_THREAD);
434 stack.stats.totalTime = ns2us(now - mDequeueTime[buf]);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700435 return err;
436}
437
Mathias Agopianc0a91642010-04-27 21:08:20 -0700438bool SharedBufferClient::needNewBuffer(int buf) const
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700439{
440 SharedBufferStack& stack( *mSharedStack );
Mathias Agopiana0b3f1d2010-05-21 14:51:33 -0700441 const uint32_t mask = 1<<(31-buf);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700442 return (android_atomic_and(~mask, &stack.reallocMask) & mask) != 0;
443}
444
Mathias Agopianc0a91642010-04-27 21:08:20 -0700445status_t SharedBufferClient::setCrop(int buf, const Rect& crop)
Mathias Agopiancc08e682010-04-15 18:48:26 -0700446{
447 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianc0a91642010-04-27 21:08:20 -0700448 return stack.setCrop(buf, crop);
Mathias Agopiancc08e682010-04-15 18:48:26 -0700449}
450
Mathias Agopianc0a91642010-04-27 21:08:20 -0700451status_t SharedBufferClient::setDirtyRegion(int buf, const Region& reg)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700452{
453 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianc0a91642010-04-27 21:08:20 -0700454 return stack.setDirtyRegion(buf, reg);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700455}
456
Mathias Agopianbb641242010-05-18 17:06:55 -0700457status_t SharedBufferClient::setBufferCount(
458 int bufferCount, const SetBufferCountCallback& ipc)
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700459{
Mathias Agopianb5c45772010-05-17 18:54:19 -0700460 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianbb641242010-05-18 17:06:55 -0700461 if (uint32_t(bufferCount) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700462 return BAD_VALUE;
Mathias Agopianbb641242010-05-18 17:06:55 -0700463
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700464 if (uint32_t(bufferCount) < SharedBufferStack::NUM_BUFFER_MIN)
465 return BAD_VALUE;
466
Mathias Agopianbb641242010-05-18 17:06:55 -0700467 RWLock::AutoWLock _wr(mLock);
468
469 status_t err = ipc(bufferCount);
470 if (err == NO_ERROR) {
471 mNumBuffers = bufferCount;
472 queued_head = (stack.head + stack.queued) % mNumBuffers;
473 }
474 return err;
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700475}
476
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700477// ----------------------------------------------------------------------------
478
479SharedBufferServer::SharedBufferServer(SharedClient* sharedClient,
Mathias Agopian48d819a2009-09-10 19:41:18 -0700480 int surface, int num, int32_t identity)
Mathias Agopianbb641242010-05-18 17:06:55 -0700481 : SharedBufferBase(sharedClient, surface, identity),
482 mNumBuffers(num)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700483{
Mathias Agopian48d819a2009-09-10 19:41:18 -0700484 mSharedStack->init(identity);
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700485 mSharedStack->token = surface;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700486 mSharedStack->head = num-1;
487 mSharedStack->available = num;
488 mSharedStack->queued = 0;
489 mSharedStack->reallocMask = 0;
Mathias Agopiancc08e682010-04-15 18:48:26 -0700490 memset(mSharedStack->buffers, 0, sizeof(mSharedStack->buffers));
Mathias Agopianc0a91642010-04-27 21:08:20 -0700491 for (int i=0 ; i<num ; i++) {
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700492 mBufferList.add(i);
Mathias Agopianc0a91642010-04-27 21:08:20 -0700493 mSharedStack->index[i] = i;
494 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700495}
496
497ssize_t SharedBufferServer::retireAndLock()
498{
Mathias Agopianbb641242010-05-18 17:06:55 -0700499 RWLock::AutoRLock _l(mLock);
500
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700501 RetireUpdate update(this, mNumBuffers);
502 ssize_t buf = updateCondition( update );
Mathias Agopianc0a91642010-04-27 21:08:20 -0700503 if (buf >= 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700504 if (uint32_t(buf) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopiand5212872010-04-30 12:59:21 -0700505 return BAD_VALUE;
Mathias Agopianc0a91642010-04-27 21:08:20 -0700506 SharedBufferStack& stack( *mSharedStack );
507 buf = stack.index[buf];
508 LOGD_IF(DEBUG_ATOMICS && buf>=0, "retire=%d, %s",
509 int(buf), dump("").string());
510 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700511 return buf;
512}
513
Mathias Agopianc0a91642010-04-27 21:08:20 -0700514status_t SharedBufferServer::unlock(int buf)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700515{
Mathias Agopianc0a91642010-04-27 21:08:20 -0700516 UnlockUpdate update(this, buf);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700517 status_t err = updateCondition( update );
518 return err;
519}
520
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700521void SharedBufferServer::setStatus(status_t status)
522{
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700523 if (status < NO_ERROR) {
524 StatusUpdate update(this, status);
525 updateCondition( update );
526 }
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700527}
528
Mathias Agopiana138f892010-05-21 17:24:35 -0700529status_t SharedBufferServer::reallocateAll()
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700530{
Mathias Agopianbb641242010-05-18 17:06:55 -0700531 RWLock::AutoRLock _l(mLock);
532
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700533 SharedBufferStack& stack( *mSharedStack );
Mathias Agopiana0b3f1d2010-05-21 14:51:33 -0700534 uint32_t mask = mBufferList.getMask();
Mathias Agopiana138f892010-05-21 17:24:35 -0700535 android_atomic_or(mask, &stack.reallocMask);
536 return NO_ERROR;
537}
538
539status_t SharedBufferServer::reallocateAllExcept(int buffer)
540{
541 RWLock::AutoRLock _l(mLock);
542
543 SharedBufferStack& stack( *mSharedStack );
544 BufferList temp(mBufferList);
545 temp.remove(buffer);
546 uint32_t mask = temp.getMask();
547 android_atomic_or(mask, &stack.reallocMask);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700548 return NO_ERROR;
549}
550
Mathias Agopiane7005012009-10-07 16:44:10 -0700551int32_t SharedBufferServer::getQueuedCount() const
552{
553 SharedBufferStack& stack( *mSharedStack );
554 return stack.queued;
555}
556
Mathias Agopianc0a91642010-04-27 21:08:20 -0700557status_t SharedBufferServer::assertReallocate(int buf)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700558{
Mathias Agopianbb641242010-05-18 17:06:55 -0700559 /*
560 * NOTE: it's safe to hold mLock for read while waiting for
561 * the ReallocateCondition because that condition is not updated
562 * by the thread that holds mLock for write.
563 */
564 RWLock::AutoRLock _l(mLock);
565
Mathias Agopiand606de62010-05-10 20:06:11 -0700566 // TODO: need to validate "buf"
Mathias Agopianc0a91642010-04-27 21:08:20 -0700567 ReallocateCondition condition(this, buf);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700568 status_t err = waitForCondition(condition);
569 return err;
570}
571
Mathias Agopianc0a91642010-04-27 21:08:20 -0700572Region SharedBufferServer::getDirtyRegion(int buf) const
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700573{
574 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianc0a91642010-04-27 21:08:20 -0700575 return stack.getDirtyRegion(buf);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700576}
577
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700578/*
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700579 * NOTE: this is not thread-safe on the server-side, meaning
580 * 'head' cannot move during this operation. The client-side
581 * can safely operate an usual.
582 *
583 */
584status_t SharedBufferServer::resize(int newNumBuffers)
585{
Mathias Agopianbb641242010-05-18 17:06:55 -0700586 if (uint32_t(newNumBuffers) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700587 return BAD_VALUE;
588
Mathias Agopianbb641242010-05-18 17:06:55 -0700589 RWLock::AutoWLock _l(mLock);
590
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700591 // for now we're not supporting shrinking
592 const int numBuffers = mNumBuffers;
593 if (newNumBuffers < numBuffers)
594 return BAD_VALUE;
595
596 SharedBufferStack& stack( *mSharedStack );
597 const int extra = newNumBuffers - numBuffers;
598
599 // read the head, make sure it's valid
600 int32_t head = stack.head;
Mathias Agopianbb641242010-05-18 17:06:55 -0700601 if (uint32_t(head) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700602 return BAD_VALUE;
603
604 int base = numBuffers;
605 int32_t avail = stack.available;
606 int tail = head - avail + 1;
Mathias Agopiand6297f72010-05-17 17:27:26 -0700607
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700608 if (tail >= 0) {
609 int8_t* const index = const_cast<int8_t*>(stack.index);
610 const int nb = numBuffers - head;
611 memmove(&index[head + extra], &index[head], nb);
612 base = head;
613 // move head 'extra' ahead, this doesn't impact stack.index[head];
614 stack.head = head + extra;
615 }
616 stack.available += extra;
617
618 // fill the new free space with unused buffers
619 BufferList::const_iterator curr(mBufferList.free_begin());
620 for (int i=0 ; i<extra ; i++) {
Mathias Agopiand6297f72010-05-17 17:27:26 -0700621 stack.index[base+i] = *curr;
622 mBufferList.add(*curr);
623 ++curr;
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700624 }
625
626 mNumBuffers = newNumBuffers;
627 return NO_ERROR;
628}
629
Mathias Agopian86f73292009-09-17 01:35:28 -0700630SharedBufferStack::Statistics SharedBufferServer::getStats() const
631{
632 SharedBufferStack& stack( *mSharedStack );
633 return stack.stats;
634}
635
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700636// ---------------------------------------------------------------------------
637status_t SharedBufferServer::BufferList::add(int value)
638{
639 if (uint32_t(value) >= mCapacity)
640 return BAD_VALUE;
641 uint32_t mask = 1<<(31-value);
642 if (mList & mask)
643 return ALREADY_EXISTS;
644 mList |= mask;
645 return NO_ERROR;
646}
647
648status_t SharedBufferServer::BufferList::remove(int value)
649{
650 if (uint32_t(value) >= mCapacity)
651 return BAD_VALUE;
652 uint32_t mask = 1<<(31-value);
653 if (!(mList & mask))
654 return NAME_NOT_FOUND;
655 mList &= ~mask;
656 return NO_ERROR;
657}
658
Mathias Agopian86f73292009-09-17 01:35:28 -0700659
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700660// ---------------------------------------------------------------------------
661}; // namespace android