blob: 5deeabbe3b57466e2606da914dbf7429e1670f20 [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
52uint32_t SharedClient::getIdentity(size_t token) const {
53 return uint32_t(surfaces[token].identity);
54}
55
Mathias Agopiancbb288b2009-09-07 16:32:45 -070056// ----------------------------------------------------------------------------
57
58
59SharedBufferStack::SharedBufferStack()
Mathias Agopiancbb288b2009-09-07 16:32:45 -070060{
61}
62
Mathias Agopian48d819a2009-09-10 19:41:18 -070063void SharedBufferStack::init(int32_t i)
64{
65 inUse = -1;
66 status = NO_ERROR;
67 identity = i;
68}
69
Mathias Agopiancc08e682010-04-15 18:48:26 -070070status_t SharedBufferStack::setCrop(int buffer, const Rect& crop)
71{
72 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
73 return BAD_INDEX;
74
75 buffers[buffer].crop.l = uint16_t(crop.left);
76 buffers[buffer].crop.t = uint16_t(crop.top);
77 buffers[buffer].crop.r = uint16_t(crop.right);
78 buffers[buffer].crop.b = uint16_t(crop.bottom);
79 return NO_ERROR;
80}
81
Mathias Agopiancbb288b2009-09-07 16:32:45 -070082status_t SharedBufferStack::setDirtyRegion(int buffer, const Region& dirty)
83{
84 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
85 return BAD_INDEX;
86
Mathias Agopian245e4d72010-04-21 15:24:11 -070087 FlatRegion& reg(buffers[buffer].dirtyRegion);
88 if (dirty.isEmpty()) {
89 reg.count = 0;
90 return NO_ERROR;
91 }
92
Mathias Agopian1100c8b2010-04-05 16:21:53 -070093 size_t count;
94 Rect const* r = dirty.getArray(&count);
Mathias Agopian1100c8b2010-04-05 16:21:53 -070095 if (count > FlatRegion::NUM_RECT_MAX) {
96 const Rect bounds(dirty.getBounds());
97 reg.count = 1;
Mathias Agopiancc08e682010-04-15 18:48:26 -070098 reg.rects[0].l = uint16_t(bounds.left);
99 reg.rects[0].t = uint16_t(bounds.top);
100 reg.rects[0].r = uint16_t(bounds.right);
101 reg.rects[0].b = uint16_t(bounds.bottom);
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700102 } else {
103 reg.count = count;
104 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancc08e682010-04-15 18:48:26 -0700105 reg.rects[i].l = uint16_t(r[i].left);
106 reg.rects[i].t = uint16_t(r[i].top);
107 reg.rects[i].r = uint16_t(r[i].right);
108 reg.rects[i].b = uint16_t(r[i].bottom);
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700109 }
110 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700111 return NO_ERROR;
112}
113
114Region SharedBufferStack::getDirtyRegion(int buffer) const
115{
116 Region res;
117 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
118 return res;
119
Mathias Agopiancc08e682010-04-15 18:48:26 -0700120 const FlatRegion& reg(buffers[buffer].dirtyRegion);
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700121 if (reg.count > FlatRegion::NUM_RECT_MAX)
122 return res;
123
124 if (reg.count == 1) {
Mathias Agopiancc08e682010-04-15 18:48:26 -0700125 const Rect r(
126 reg.rects[0].l,
127 reg.rects[0].t,
128 reg.rects[0].r,
129 reg.rects[0].b);
130 res.set(r);
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700131 } else {
132 for (size_t i=0 ; i<reg.count ; i++) {
133 const Rect r(
Mathias Agopiancc08e682010-04-15 18:48:26 -0700134 reg.rects[i].l,
135 reg.rects[i].t,
136 reg.rects[i].r,
137 reg.rects[i].b);
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700138 res.orSelf(r);
139 }
140 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700141 return res;
142}
143
144// ----------------------------------------------------------------------------
145
146SharedBufferBase::SharedBufferBase(SharedClient* sharedClient,
Mathias Agopianbb641242010-05-18 17:06:55 -0700147 int surface, int32_t identity)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700148 : mSharedClient(sharedClient),
149 mSharedStack(sharedClient->surfaces + surface),
Mathias Agopianbb641242010-05-18 17:06:55 -0700150 mIdentity(identity)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700151{
152}
153
154SharedBufferBase::~SharedBufferBase()
155{
156}
157
158uint32_t SharedBufferBase::getIdentity()
159{
160 SharedBufferStack& stack( *mSharedStack );
161 return stack.identity;
162}
163
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700164status_t SharedBufferBase::getStatus() const
165{
166 SharedBufferStack& stack( *mSharedStack );
167 return stack.status;
168}
169
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700170size_t SharedBufferBase::getFrontBuffer() const
171{
172 SharedBufferStack& stack( *mSharedStack );
173 return size_t( stack.head );
174}
175
176String8 SharedBufferBase::dump(char const* prefix) const
177{
178 const size_t SIZE = 1024;
179 char buffer[SIZE];
180 String8 result;
181 SharedBufferStack& stack( *mSharedStack );
182 snprintf(buffer, SIZE,
Mathias Agopianbb641242010-05-18 17:06:55 -0700183 "%s[ head=%2d, available=%2d, queued=%2d ] "
Mathias Agopianb5c45772010-05-17 18:54:19 -0700184 "reallocMask=%08x, inUse=%2d, identity=%d, status=%d",
Mathias Agopianbb641242010-05-18 17:06:55 -0700185 prefix, stack.head, stack.available, stack.queued,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700186 stack.reallocMask, stack.inUse, stack.identity, stack.status);
187 result.append(buffer);
Mathias Agopianbb641242010-05-18 17:06:55 -0700188 result.append("\n");
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700189 return result;
190}
191
Mathias Agopianb2965332010-04-27 16:41:19 -0700192status_t SharedBufferBase::waitForCondition(const ConditionBase& condition)
193{
194 const SharedBufferStack& stack( *mSharedStack );
195 SharedClient& client( *mSharedClient );
196 const nsecs_t TIMEOUT = s2ns(1);
197 const int identity = mIdentity;
198
199 Mutex::Autolock _l(client.lock);
200 while ((condition()==false) &&
201 (stack.identity == identity) &&
202 (stack.status == NO_ERROR))
203 {
204 status_t err = client.cv.waitRelative(client.lock, TIMEOUT);
205 // handle errors and timeouts
206 if (CC_UNLIKELY(err != NO_ERROR)) {
207 if (err == TIMED_OUT) {
208 if (condition()) {
209 LOGE("waitForCondition(%s) timed out (identity=%d), "
210 "but condition is true! We recovered but it "
211 "shouldn't happen." , condition.name(), stack.identity);
212 break;
213 } else {
214 LOGW("waitForCondition(%s) timed out "
215 "(identity=%d, status=%d). "
216 "CPU may be pegged. trying again.", condition.name(),
217 stack.identity, stack.status);
218 }
219 } else {
220 LOGE("waitForCondition(%s) error (%s) ",
221 condition.name(), strerror(-err));
222 return err;
223 }
224 }
225 }
226 return (stack.identity != mIdentity) ? status_t(BAD_INDEX) : stack.status;
227}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700228// ============================================================================
229// conditions and updates
230// ============================================================================
231
232SharedBufferClient::DequeueCondition::DequeueCondition(
233 SharedBufferClient* sbc) : ConditionBase(sbc) {
234}
Mathias Agopianb2965332010-04-27 16:41:19 -0700235bool SharedBufferClient::DequeueCondition::operator()() const {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700236 return stack.available > 0;
237}
238
239SharedBufferClient::LockCondition::LockCondition(
240 SharedBufferClient* sbc, int buf) : ConditionBase(sbc), buf(buf) {
241}
Mathias Agopianb2965332010-04-27 16:41:19 -0700242bool SharedBufferClient::LockCondition::operator()() const {
Mathias Agopiand5212872010-04-30 12:59:21 -0700243 // NOTE: if stack.head is messed up, we could crash the client
244 // or cause some drawing artifacts. This is okay, as long as it is
245 // limited to the client.
Mathias Agopianc0a91642010-04-27 21:08:20 -0700246 return (buf != stack.index[stack.head] ||
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700247 (stack.queued > 0 && stack.inUse != buf));
248}
249
250SharedBufferServer::ReallocateCondition::ReallocateCondition(
251 SharedBufferBase* sbb, int buf) : ConditionBase(sbb), buf(buf) {
252}
Mathias Agopianb2965332010-04-27 16:41:19 -0700253bool SharedBufferServer::ReallocateCondition::operator()() const {
Mathias Agopiand5212872010-04-30 12:59:21 -0700254 int32_t head = stack.head;
Mathias Agopianbb641242010-05-18 17:06:55 -0700255 if (uint32_t(head) >= SharedBufferStack::NUM_BUFFER_MAX) {
Mathias Agopiand5212872010-04-30 12:59:21 -0700256 // if stack.head is messed up, we cannot allow the server to
257 // crash (since stack.head is mapped on the client side)
258 stack.status = BAD_VALUE;
259 return false;
260 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700261 // TODO: we should also check that buf has been dequeued
Mathias Agopiand5212872010-04-30 12:59:21 -0700262 return (buf != stack.index[head]);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700263}
264
265// ----------------------------------------------------------------------------
266
267SharedBufferClient::QueueUpdate::QueueUpdate(SharedBufferBase* sbb)
268 : UpdateBase(sbb) {
269}
270ssize_t SharedBufferClient::QueueUpdate::operator()() {
271 android_atomic_inc(&stack.queued);
272 return NO_ERROR;
273}
274
275SharedBufferClient::UndoDequeueUpdate::UndoDequeueUpdate(SharedBufferBase* sbb)
276 : UpdateBase(sbb) {
277}
278ssize_t SharedBufferClient::UndoDequeueUpdate::operator()() {
279 android_atomic_inc(&stack.available);
280 return NO_ERROR;
281}
282
283SharedBufferServer::UnlockUpdate::UnlockUpdate(
284 SharedBufferBase* sbb, int lockedBuffer)
285 : UpdateBase(sbb), lockedBuffer(lockedBuffer) {
286}
287ssize_t SharedBufferServer::UnlockUpdate::operator()() {
288 if (stack.inUse != lockedBuffer) {
289 LOGE("unlocking %d, but currently locked buffer is %d",
290 lockedBuffer, stack.inUse);
291 return BAD_VALUE;
292 }
293 android_atomic_write(-1, &stack.inUse);
294 return NO_ERROR;
295}
296
297SharedBufferServer::RetireUpdate::RetireUpdate(
298 SharedBufferBase* sbb, int numBuffers)
299 : UpdateBase(sbb), numBuffers(numBuffers) {
300}
301ssize_t SharedBufferServer::RetireUpdate::operator()() {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700302 int32_t head = stack.head;
Mathias Agopianbb641242010-05-18 17:06:55 -0700303 if (uint32_t(head) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopiand5212872010-04-30 12:59:21 -0700304 return BAD_VALUE;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700305
306 // Preventively lock the current buffer before updating queued.
Mathias Agopianc0a91642010-04-27 21:08:20 -0700307 android_atomic_write(stack.index[head], &stack.inUse);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700308
309 // Decrement the number of queued buffers
310 int32_t queued;
311 do {
312 queued = stack.queued;
313 if (queued == 0) {
314 return NOT_ENOUGH_DATA;
315 }
316 } while (android_atomic_cmpxchg(queued, queued-1, &stack.queued));
317
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700318 // lock the buffer before advancing head, which automatically unlocks
319 // the buffer we preventively locked upon entering this function
Mathias Agopianb5c45772010-05-17 18:54:19 -0700320
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700321 head = (head + 1) % numBuffers;
Mathias Agopianc0a91642010-04-27 21:08:20 -0700322 android_atomic_write(stack.index[head], &stack.inUse);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700323
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700324 // head is only modified here, so we don't need to use cmpxchg
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700325 android_atomic_write(head, &stack.head);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700326
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700327 // now that head has moved, we can increment the number of available buffers
328 android_atomic_inc(&stack.available);
329 return head;
330}
331
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700332SharedBufferServer::StatusUpdate::StatusUpdate(
333 SharedBufferBase* sbb, status_t status)
334 : UpdateBase(sbb), status(status) {
335}
336
337ssize_t SharedBufferServer::StatusUpdate::operator()() {
338 android_atomic_write(status, &stack.status);
339 return NO_ERROR;
340}
341
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700342// ============================================================================
343
344SharedBufferClient::SharedBufferClient(SharedClient* sharedClient,
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700345 int surface, int num, int32_t identity)
Mathias Agopianbb641242010-05-18 17:06:55 -0700346 : SharedBufferBase(sharedClient, surface, identity),
347 mNumBuffers(num), tail(0), undoDequeueTail(0)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700348{
Mathias Agopianc0a91642010-04-27 21:08:20 -0700349 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianc7d56012009-09-14 15:48:42 -0700350 tail = computeTail();
Mathias Agopianc0a91642010-04-27 21:08:20 -0700351 queued_head = stack.head;
Mathias Agopianc7d56012009-09-14 15:48:42 -0700352}
353
Mathias Agopianbb641242010-05-18 17:06:55 -0700354int32_t SharedBufferClient::computeTail() const
355{
356 SharedBufferStack& stack( *mSharedStack );
357 return (mNumBuffers + stack.head - stack.available + 1) % mNumBuffers;
358}
359
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700360ssize_t SharedBufferClient::dequeue()
361{
Mathias Agopian40d57992009-09-11 19:18:20 -0700362 SharedBufferStack& stack( *mSharedStack );
363
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700364 if (stack.head == tail && stack.available == mNumBuffers) {
Mathias Agopian40d57992009-09-11 19:18:20 -0700365 LOGW("dequeue: tail=%d, head=%d, avail=%d, queued=%d",
366 tail, stack.head, stack.available, stack.queued);
367 }
Mathias Agopianbb641242010-05-18 17:06:55 -0700368
369 RWLock::AutoRLock _rd(mLock);
370
Mathias Agopian86f73292009-09-17 01:35:28 -0700371 const nsecs_t dequeueTime = systemTime(SYSTEM_TIME_THREAD);
Mathias Agopian40d57992009-09-11 19:18:20 -0700372
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700373 //LOGD("[%d] about to dequeue a buffer",
374 // mSharedStack->identity);
375 DequeueCondition condition(this);
376 status_t err = waitForCondition(condition);
377 if (err != NO_ERROR)
378 return ssize_t(err);
379
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700380 // NOTE: 'stack.available' is part of the conditions, however
381 // decrementing it, never changes any conditions, so we don't need
382 // to do this as part of an update.
383 if (android_atomic_dec(&stack.available) == 0) {
384 LOGW("dequeue probably called from multiple threads!");
385 }
386
Mathias Agopianc0a91642010-04-27 21:08:20 -0700387 undoDequeueTail = tail;
388 int dequeued = stack.index[tail];
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700389 tail = ((tail+1 >= mNumBuffers) ? 0 : tail+1);
Mathias Agopianc0a91642010-04-27 21:08:20 -0700390 LOGD_IF(DEBUG_ATOMICS, "dequeued=%d, tail++=%d, %s",
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700391 dequeued, tail, dump("").string());
Mathias Agopian40d57992009-09-11 19:18:20 -0700392
Mathias Agopian86f73292009-09-17 01:35:28 -0700393 mDequeueTime[dequeued] = dequeueTime;
394
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700395 return dequeued;
396}
397
398status_t SharedBufferClient::undoDequeue(int buf)
399{
Mathias Agopianbb641242010-05-18 17:06:55 -0700400 RWLock::AutoRLock _rd(mLock);
401
Mathias Agopianc0a91642010-04-27 21:08:20 -0700402 // TODO: we can only undo the previous dequeue, we should
403 // enforce that in the api
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700404 UndoDequeueUpdate update(this);
405 status_t err = updateCondition( update );
Mathias Agopianc7d56012009-09-14 15:48:42 -0700406 if (err == NO_ERROR) {
Mathias Agopian0a8cd062010-04-27 16:11:38 -0700407 tail = undoDequeueTail;
Mathias Agopianc7d56012009-09-14 15:48:42 -0700408 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700409 return err;
410}
411
412status_t SharedBufferClient::lock(int buf)
413{
Mathias Agopianbb641242010-05-18 17:06:55 -0700414 RWLock::AutoRLock _rd(mLock);
415
Mathias Agopianc0a91642010-04-27 21:08:20 -0700416 SharedBufferStack& stack( *mSharedStack );
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700417 LockCondition condition(this, buf);
Mathias Agopian86f73292009-09-17 01:35:28 -0700418 status_t err = waitForCondition(condition);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700419 return err;
420}
421
422status_t SharedBufferClient::queue(int buf)
423{
Mathias Agopianbb641242010-05-18 17:06:55 -0700424 RWLock::AutoRLock _rd(mLock);
425
Mathias Agopianc0a91642010-04-27 21:08:20 -0700426 SharedBufferStack& stack( *mSharedStack );
427
Mathias Agopianb5c45772010-05-17 18:54:19 -0700428 queued_head = (queued_head + 1) % mNumBuffers;
Mathias Agopianc0a91642010-04-27 21:08:20 -0700429 stack.index[queued_head] = buf;
430
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700431 QueueUpdate update(this);
432 status_t err = updateCondition( update );
433 LOGD_IF(DEBUG_ATOMICS, "queued=%d, %s", buf, dump("").string());
Mathias Agopianc0a91642010-04-27 21:08:20 -0700434
Mathias Agopian86f73292009-09-17 01:35:28 -0700435 const nsecs_t now = systemTime(SYSTEM_TIME_THREAD);
436 stack.stats.totalTime = ns2us(now - mDequeueTime[buf]);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700437 return err;
438}
439
Mathias Agopianc0a91642010-04-27 21:08:20 -0700440bool SharedBufferClient::needNewBuffer(int buf) const
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700441{
442 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianc0a91642010-04-27 21:08:20 -0700443 const uint32_t mask = 1<<buf;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700444 return (android_atomic_and(~mask, &stack.reallocMask) & mask) != 0;
445}
446
Mathias Agopianc0a91642010-04-27 21:08:20 -0700447status_t SharedBufferClient::setCrop(int buf, const Rect& crop)
Mathias Agopiancc08e682010-04-15 18:48:26 -0700448{
449 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianc0a91642010-04-27 21:08:20 -0700450 return stack.setCrop(buf, crop);
Mathias Agopiancc08e682010-04-15 18:48:26 -0700451}
452
Mathias Agopianc0a91642010-04-27 21:08:20 -0700453status_t SharedBufferClient::setDirtyRegion(int buf, const Region& reg)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700454{
455 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianc0a91642010-04-27 21:08:20 -0700456 return stack.setDirtyRegion(buf, reg);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700457}
458
Mathias Agopianbb641242010-05-18 17:06:55 -0700459status_t SharedBufferClient::setBufferCount(
460 int bufferCount, const SetBufferCountCallback& ipc)
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700461{
Mathias Agopianb5c45772010-05-17 18:54:19 -0700462 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianbb641242010-05-18 17:06:55 -0700463 if (uint32_t(bufferCount) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700464 return BAD_VALUE;
Mathias Agopianbb641242010-05-18 17:06:55 -0700465
466 RWLock::AutoWLock _wr(mLock);
467
468 status_t err = ipc(bufferCount);
469 if (err == NO_ERROR) {
470 mNumBuffers = bufferCount;
471 queued_head = (stack.head + stack.queued) % mNumBuffers;
472 }
473 return err;
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700474}
475
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700476// ----------------------------------------------------------------------------
477
478SharedBufferServer::SharedBufferServer(SharedClient* sharedClient,
Mathias Agopian48d819a2009-09-10 19:41:18 -0700479 int surface, int num, int32_t identity)
Mathias Agopianbb641242010-05-18 17:06:55 -0700480 : SharedBufferBase(sharedClient, surface, identity),
481 mNumBuffers(num)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700482{
Mathias Agopian48d819a2009-09-10 19:41:18 -0700483 mSharedStack->init(identity);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700484 mSharedStack->head = num-1;
485 mSharedStack->available = num;
486 mSharedStack->queued = 0;
487 mSharedStack->reallocMask = 0;
Mathias Agopiancc08e682010-04-15 18:48:26 -0700488 memset(mSharedStack->buffers, 0, sizeof(mSharedStack->buffers));
Mathias Agopianc0a91642010-04-27 21:08:20 -0700489 for (int i=0 ; i<num ; i++) {
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700490 mBufferList.add(i);
Mathias Agopianc0a91642010-04-27 21:08:20 -0700491 mSharedStack->index[i] = i;
492 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700493}
494
495ssize_t SharedBufferServer::retireAndLock()
496{
Mathias Agopianbb641242010-05-18 17:06:55 -0700497 RWLock::AutoRLock _l(mLock);
498
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700499 RetireUpdate update(this, mNumBuffers);
500 ssize_t buf = updateCondition( update );
Mathias Agopianc0a91642010-04-27 21:08:20 -0700501 if (buf >= 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700502 if (uint32_t(buf) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopiand5212872010-04-30 12:59:21 -0700503 return BAD_VALUE;
Mathias Agopianc0a91642010-04-27 21:08:20 -0700504 SharedBufferStack& stack( *mSharedStack );
505 buf = stack.index[buf];
506 LOGD_IF(DEBUG_ATOMICS && buf>=0, "retire=%d, %s",
507 int(buf), dump("").string());
508 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700509 return buf;
510}
511
Mathias Agopianc0a91642010-04-27 21:08:20 -0700512status_t SharedBufferServer::unlock(int buf)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700513{
Mathias Agopianc0a91642010-04-27 21:08:20 -0700514 UnlockUpdate update(this, buf);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700515 status_t err = updateCondition( update );
516 return err;
517}
518
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700519void SharedBufferServer::setStatus(status_t status)
520{
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700521 if (status < NO_ERROR) {
522 StatusUpdate update(this, status);
523 updateCondition( update );
524 }
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700525}
526
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700527status_t SharedBufferServer::reallocate()
528{
Mathias Agopianbb641242010-05-18 17:06:55 -0700529 RWLock::AutoRLock _l(mLock);
530
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700531 SharedBufferStack& stack( *mSharedStack );
532 uint32_t mask = (1<<mNumBuffers)-1;
533 android_atomic_or(mask, &stack.reallocMask);
534 return NO_ERROR;
535}
536
Mathias Agopiane7005012009-10-07 16:44:10 -0700537int32_t SharedBufferServer::getQueuedCount() const
538{
539 SharedBufferStack& stack( *mSharedStack );
540 return stack.queued;
541}
542
Mathias Agopianc0a91642010-04-27 21:08:20 -0700543status_t SharedBufferServer::assertReallocate(int buf)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700544{
Mathias Agopianbb641242010-05-18 17:06:55 -0700545 /*
546 * NOTE: it's safe to hold mLock for read while waiting for
547 * the ReallocateCondition because that condition is not updated
548 * by the thread that holds mLock for write.
549 */
550 RWLock::AutoRLock _l(mLock);
551
Mathias Agopiand606de62010-05-10 20:06:11 -0700552 // TODO: need to validate "buf"
Mathias Agopianc0a91642010-04-27 21:08:20 -0700553 ReallocateCondition condition(this, buf);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700554 status_t err = waitForCondition(condition);
555 return err;
556}
557
Mathias Agopianc0a91642010-04-27 21:08:20 -0700558Region SharedBufferServer::getDirtyRegion(int buf) const
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700559{
560 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianc0a91642010-04-27 21:08:20 -0700561 return stack.getDirtyRegion(buf);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700562}
563
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700564/*
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700565 * NOTE: this is not thread-safe on the server-side, meaning
566 * 'head' cannot move during this operation. The client-side
567 * can safely operate an usual.
568 *
569 */
570status_t SharedBufferServer::resize(int newNumBuffers)
571{
Mathias Agopianbb641242010-05-18 17:06:55 -0700572 if (uint32_t(newNumBuffers) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700573 return BAD_VALUE;
574
Mathias Agopianbb641242010-05-18 17:06:55 -0700575 RWLock::AutoWLock _l(mLock);
576
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700577 // for now we're not supporting shrinking
578 const int numBuffers = mNumBuffers;
579 if (newNumBuffers < numBuffers)
580 return BAD_VALUE;
581
582 SharedBufferStack& stack( *mSharedStack );
583 const int extra = newNumBuffers - numBuffers;
584
585 // read the head, make sure it's valid
586 int32_t head = stack.head;
Mathias Agopianbb641242010-05-18 17:06:55 -0700587 if (uint32_t(head) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700588 return BAD_VALUE;
589
590 int base = numBuffers;
591 int32_t avail = stack.available;
592 int tail = head - avail + 1;
Mathias Agopiand6297f72010-05-17 17:27:26 -0700593
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700594 if (tail >= 0) {
595 int8_t* const index = const_cast<int8_t*>(stack.index);
596 const int nb = numBuffers - head;
597 memmove(&index[head + extra], &index[head], nb);
598 base = head;
599 // move head 'extra' ahead, this doesn't impact stack.index[head];
600 stack.head = head + extra;
601 }
602 stack.available += extra;
603
604 // fill the new free space with unused buffers
605 BufferList::const_iterator curr(mBufferList.free_begin());
606 for (int i=0 ; i<extra ; i++) {
Mathias Agopiand6297f72010-05-17 17:27:26 -0700607 stack.index[base+i] = *curr;
608 mBufferList.add(*curr);
609 ++curr;
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700610 }
611
612 mNumBuffers = newNumBuffers;
613 return NO_ERROR;
614}
615
Mathias Agopian86f73292009-09-17 01:35:28 -0700616SharedBufferStack::Statistics SharedBufferServer::getStats() const
617{
618 SharedBufferStack& stack( *mSharedStack );
619 return stack.stats;
620}
621
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700622// ---------------------------------------------------------------------------
623status_t SharedBufferServer::BufferList::add(int value)
624{
625 if (uint32_t(value) >= mCapacity)
626 return BAD_VALUE;
627 uint32_t mask = 1<<(31-value);
628 if (mList & mask)
629 return ALREADY_EXISTS;
630 mList |= mask;
631 return NO_ERROR;
632}
633
634status_t SharedBufferServer::BufferList::remove(int value)
635{
636 if (uint32_t(value) >= mCapacity)
637 return BAD_VALUE;
638 uint32_t mask = 1<<(31-value);
639 if (!(mList & mask))
640 return NAME_NOT_FOUND;
641 mList &= ~mask;
642 return NO_ERROR;
643}
644
Mathias Agopian86f73292009-09-17 01:35:28 -0700645
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700646// ---------------------------------------------------------------------------
647}; // namespace android