blob: 156a7db3c19005e7cdb3921b92a262a424fc5813 [file] [log] [blame]
Mathias Agopian9779b2212009-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 Agopian000479f2010-02-09 17:46:37 -080026#include <private/surfaceflinger/SharedBufferStack.h>
Mathias Agopian9779b2212009-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 Agopiana729f972010-03-19 16:14:13 -070037 : lock(Mutex::SHARED), cv(Condition::SHARED)
Mathias Agopian9779b2212009-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 Agopian898c4c92010-05-18 17:06:55 -070047 if (uint32_t(i) >= uint32_t(SharedBufferStack::NUM_LAYERS_MAX))
Mathias Agopian9779b2212009-09-07 16:32:45 -070048 return BAD_INDEX;
49 return surfaces[i].status;
50}
51
Mathias Agopian9779b2212009-09-07 16:32:45 -070052// ----------------------------------------------------------------------------
53
54
55SharedBufferStack::SharedBufferStack()
Mathias Agopian9779b2212009-09-07 16:32:45 -070056{
57}
58
Mathias Agopian248b5bd2009-09-10 19:41:18 -070059void SharedBufferStack::init(int32_t i)
60{
Mathias Agopian7623da42010-06-01 15:12:58 -070061 inUse = -2;
Mathias Agopian248b5bd2009-09-10 19:41:18 -070062 status = NO_ERROR;
63 identity = i;
64}
65
Mathias Agopian16a86ee2010-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 Agopian9779b2212009-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 Agopiana8a0aa82010-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 Agopian6bb5eba2010-04-05 16:21:53 -070089 size_t count;
90 Rect const* r = dirty.getArray(&count);
Mathias Agopian6bb5eba2010-04-05 16:21:53 -070091 if (count > FlatRegion::NUM_RECT_MAX) {
92 const Rect bounds(dirty.getBounds());
93 reg.count = 1;
Mathias Agopian16a86ee2010-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 Agopian6bb5eba2010-04-05 16:21:53 -070098 } else {
99 reg.count = count;
100 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian16a86ee2010-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 Agopian6bb5eba2010-04-05 16:21:53 -0700105 }
106 }
Mathias Agopian9779b2212009-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 Agopian16a86ee2010-04-15 18:48:26 -0700116 const FlatRegion& reg(buffers[buffer].dirtyRegion);
Mathias Agopian6bb5eba2010-04-05 16:21:53 -0700117 if (reg.count > FlatRegion::NUM_RECT_MAX)
118 return res;
119
120 if (reg.count == 1) {
Mathias Agopian16a86ee2010-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 Agopian6bb5eba2010-04-05 16:21:53 -0700127 } else {
128 for (size_t i=0 ; i<reg.count ; i++) {
129 const Rect r(
Mathias Agopian16a86ee2010-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 Agopian6bb5eba2010-04-05 16:21:53 -0700134 res.orSelf(r);
135 }
136 }
Mathias Agopian9779b2212009-09-07 16:32:45 -0700137 return res;
138}
139
140// ----------------------------------------------------------------------------
141
142SharedBufferBase::SharedBufferBase(SharedClient* sharedClient,
Mathias Agopian898c4c92010-05-18 17:06:55 -0700143 int surface, int32_t identity)
Mathias Agopian9779b2212009-09-07 16:32:45 -0700144 : mSharedClient(sharedClient),
145 mSharedStack(sharedClient->surfaces + surface),
Mathias Agopian898c4c92010-05-18 17:06:55 -0700146 mIdentity(identity)
Mathias Agopian9779b2212009-09-07 16:32:45 -0700147{
148}
149
150SharedBufferBase::~SharedBufferBase()
151{
152}
153
Mathias Agopian0c4cec72009-10-02 18:12:30 -0700154status_t SharedBufferBase::getStatus() const
155{
156 SharedBufferStack& stack( *mSharedStack );
157 return stack.status;
158}
159
Mathias Agopian770492c2010-05-28 14:22:23 -0700160int32_t SharedBufferBase::getIdentity() const
161{
162 SharedBufferStack& stack( *mSharedStack );
163 return stack.identity;
164}
165
Mathias Agopian9779b2212009-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 Agopian898c4c92010-05-18 17:06:55 -0700179 "%s[ head=%2d, available=%2d, queued=%2d ] "
Mathias Agopianbe6c8fc2010-05-17 18:54:19 -0700180 "reallocMask=%08x, inUse=%2d, identity=%d, status=%d",
Mathias Agopian898c4c92010-05-18 17:06:55 -0700181 prefix, stack.head, stack.available, stack.queued,
Mathias Agopian9779b2212009-09-07 16:32:45 -0700182 stack.reallocMask, stack.inUse, stack.identity, stack.status);
183 result.append(buffer);
Mathias Agopian898c4c92010-05-18 17:06:55 -0700184 result.append("\n");
Mathias Agopian9779b2212009-09-07 16:32:45 -0700185 return result;
186}
187
Mathias Agopianf590f702010-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 Agopian9779b2212009-09-07 16:32:45 -0700224// ============================================================================
225// conditions and updates
226// ============================================================================
227
228SharedBufferClient::DequeueCondition::DequeueCondition(
229 SharedBufferClient* sbc) : ConditionBase(sbc) {
230}
Mathias Agopianf590f702010-04-27 16:41:19 -0700231bool SharedBufferClient::DequeueCondition::operator()() const {
Mathias Agopian9779b2212009-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 Agopianf590f702010-04-27 16:41:19 -0700238bool SharedBufferClient::LockCondition::operator()() const {
Mathias Agopian3b91e13e2010-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 Agopianbfe7f0b12010-04-27 21:08:20 -0700242 return (buf != stack.index[stack.head] ||
Mathias Agopian9779b2212009-09-07 16:32:45 -0700243 (stack.queued > 0 && stack.inUse != buf));
244}
245
Mathias Agopian9779b2212009-09-07 16:32:45 -0700246// ----------------------------------------------------------------------------
247
248SharedBufferClient::QueueUpdate::QueueUpdate(SharedBufferBase* sbb)
249 : UpdateBase(sbb) {
250}
251ssize_t SharedBufferClient::QueueUpdate::operator()() {
252 android_atomic_inc(&stack.queued);
253 return NO_ERROR;
254}
255
256SharedBufferClient::UndoDequeueUpdate::UndoDequeueUpdate(SharedBufferBase* sbb)
257 : UpdateBase(sbb) {
258}
259ssize_t SharedBufferClient::UndoDequeueUpdate::operator()() {
260 android_atomic_inc(&stack.available);
261 return NO_ERROR;
262}
263
264SharedBufferServer::UnlockUpdate::UnlockUpdate(
265 SharedBufferBase* sbb, int lockedBuffer)
266 : UpdateBase(sbb), lockedBuffer(lockedBuffer) {
267}
268ssize_t SharedBufferServer::UnlockUpdate::operator()() {
269 if (stack.inUse != lockedBuffer) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700270 LOGE("unlocking %d, but currently locked buffer is %d "
271 "(identity=%d, token=%d)",
272 lockedBuffer, stack.inUse,
273 stack.identity, stack.token);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700274 return BAD_VALUE;
275 }
276 android_atomic_write(-1, &stack.inUse);
277 return NO_ERROR;
278}
279
280SharedBufferServer::RetireUpdate::RetireUpdate(
281 SharedBufferBase* sbb, int numBuffers)
282 : UpdateBase(sbb), numBuffers(numBuffers) {
283}
284ssize_t SharedBufferServer::RetireUpdate::operator()() {
Mathias Agopian9779b2212009-09-07 16:32:45 -0700285 int32_t head = stack.head;
Mathias Agopian898c4c92010-05-18 17:06:55 -0700286 if (uint32_t(head) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopian3b91e13e2010-04-30 12:59:21 -0700287 return BAD_VALUE;
Mathias Agopian9779b2212009-09-07 16:32:45 -0700288
289 // Preventively lock the current buffer before updating queued.
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700290 android_atomic_write(stack.index[head], &stack.inUse);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700291
292 // Decrement the number of queued buffers
293 int32_t queued;
294 do {
295 queued = stack.queued;
296 if (queued == 0) {
297 return NOT_ENOUGH_DATA;
298 }
299 } while (android_atomic_cmpxchg(queued, queued-1, &stack.queued));
300
Mathias Agopian9779b2212009-09-07 16:32:45 -0700301 // lock the buffer before advancing head, which automatically unlocks
302 // the buffer we preventively locked upon entering this function
Mathias Agopianbe6c8fc2010-05-17 18:54:19 -0700303
Mathias Agopian59751db2010-05-07 15:58:44 -0700304 head = (head + 1) % numBuffers;
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700305 android_atomic_write(stack.index[head], &stack.inUse);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700306
Mathias Agopian59751db2010-05-07 15:58:44 -0700307 // head is only modified here, so we don't need to use cmpxchg
Mathias Agopian9779b2212009-09-07 16:32:45 -0700308 android_atomic_write(head, &stack.head);
Mathias Agopian59751db2010-05-07 15:58:44 -0700309
Mathias Agopian9779b2212009-09-07 16:32:45 -0700310 // now that head has moved, we can increment the number of available buffers
311 android_atomic_inc(&stack.available);
312 return head;
313}
314
Mathias Agopian436c6272009-09-10 16:55:13 -0700315SharedBufferServer::StatusUpdate::StatusUpdate(
316 SharedBufferBase* sbb, status_t status)
317 : UpdateBase(sbb), status(status) {
318}
319
320ssize_t SharedBufferServer::StatusUpdate::operator()() {
321 android_atomic_write(status, &stack.status);
322 return NO_ERROR;
323}
324
Mathias Agopian9779b2212009-09-07 16:32:45 -0700325// ============================================================================
326
327SharedBufferClient::SharedBufferClient(SharedClient* sharedClient,
Mathias Agopian4961c952009-10-06 19:00:57 -0700328 int surface, int num, int32_t identity)
Mathias Agopian898c4c92010-05-18 17:06:55 -0700329 : SharedBufferBase(sharedClient, surface, identity),
330 mNumBuffers(num), tail(0), undoDequeueTail(0)
Mathias Agopian9779b2212009-09-07 16:32:45 -0700331{
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700332 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianbd852712009-09-14 15:48:42 -0700333 tail = computeTail();
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700334 queued_head = stack.head;
Mathias Agopianbd852712009-09-14 15:48:42 -0700335}
336
Mathias Agopian898c4c92010-05-18 17:06:55 -0700337int32_t SharedBufferClient::computeTail() const
338{
339 SharedBufferStack& stack( *mSharedStack );
340 return (mNumBuffers + stack.head - stack.available + 1) % mNumBuffers;
341}
342
Mathias Agopian9779b2212009-09-07 16:32:45 -0700343ssize_t SharedBufferClient::dequeue()
344{
Mathias Agopian3e63f912009-09-11 19:18:20 -0700345 SharedBufferStack& stack( *mSharedStack );
346
Mathias Agopian6bb5eba2010-04-05 16:21:53 -0700347 if (stack.head == tail && stack.available == mNumBuffers) {
Mathias Agopian3e63f912009-09-11 19:18:20 -0700348 LOGW("dequeue: tail=%d, head=%d, avail=%d, queued=%d",
349 tail, stack.head, stack.available, stack.queued);
350 }
Mathias Agopian898c4c92010-05-18 17:06:55 -0700351
352 RWLock::AutoRLock _rd(mLock);
353
Mathias Agopianbcef9ac2009-09-17 01:35:28 -0700354 const nsecs_t dequeueTime = systemTime(SYSTEM_TIME_THREAD);
Mathias Agopian3e63f912009-09-11 19:18:20 -0700355
Mathias Agopian9779b2212009-09-07 16:32:45 -0700356 //LOGD("[%d] about to dequeue a buffer",
357 // mSharedStack->identity);
358 DequeueCondition condition(this);
359 status_t err = waitForCondition(condition);
360 if (err != NO_ERROR)
361 return ssize_t(err);
362
Mathias Agopian9779b2212009-09-07 16:32:45 -0700363 // NOTE: 'stack.available' is part of the conditions, however
364 // decrementing it, never changes any conditions, so we don't need
365 // to do this as part of an update.
366 if (android_atomic_dec(&stack.available) == 0) {
367 LOGW("dequeue probably called from multiple threads!");
368 }
369
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700370 undoDequeueTail = tail;
371 int dequeued = stack.index[tail];
Mathias Agopian9779b2212009-09-07 16:32:45 -0700372 tail = ((tail+1 >= mNumBuffers) ? 0 : tail+1);
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700373 LOGD_IF(DEBUG_ATOMICS, "dequeued=%d, tail++=%d, %s",
Mathias Agopian9779b2212009-09-07 16:32:45 -0700374 dequeued, tail, dump("").string());
Mathias Agopian3e63f912009-09-11 19:18:20 -0700375
Mathias Agopianbcef9ac2009-09-17 01:35:28 -0700376 mDequeueTime[dequeued] = dequeueTime;
377
Mathias Agopian9779b2212009-09-07 16:32:45 -0700378 return dequeued;
379}
380
381status_t SharedBufferClient::undoDequeue(int buf)
382{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700383 RWLock::AutoRLock _rd(mLock);
384
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700385 // TODO: we can only undo the previous dequeue, we should
386 // enforce that in the api
Mathias Agopian9779b2212009-09-07 16:32:45 -0700387 UndoDequeueUpdate update(this);
388 status_t err = updateCondition( update );
Mathias Agopianbd852712009-09-14 15:48:42 -0700389 if (err == NO_ERROR) {
Mathias Agopianc54c1272010-04-27 16:11:38 -0700390 tail = undoDequeueTail;
Mathias Agopianbd852712009-09-14 15:48:42 -0700391 }
Mathias Agopian9779b2212009-09-07 16:32:45 -0700392 return err;
393}
394
395status_t SharedBufferClient::lock(int buf)
396{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700397 RWLock::AutoRLock _rd(mLock);
398
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700399 SharedBufferStack& stack( *mSharedStack );
Mathias Agopian9779b2212009-09-07 16:32:45 -0700400 LockCondition condition(this, buf);
Mathias Agopianbcef9ac2009-09-17 01:35:28 -0700401 status_t err = waitForCondition(condition);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700402 return err;
403}
404
405status_t SharedBufferClient::queue(int buf)
406{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700407 RWLock::AutoRLock _rd(mLock);
408
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700409 SharedBufferStack& stack( *mSharedStack );
410
Mathias Agopianbe6c8fc2010-05-17 18:54:19 -0700411 queued_head = (queued_head + 1) % mNumBuffers;
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700412 stack.index[queued_head] = buf;
413
Mathias Agopian9779b2212009-09-07 16:32:45 -0700414 QueueUpdate update(this);
415 status_t err = updateCondition( update );
416 LOGD_IF(DEBUG_ATOMICS, "queued=%d, %s", buf, dump("").string());
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700417
Mathias Agopianbcef9ac2009-09-17 01:35:28 -0700418 const nsecs_t now = systemTime(SYSTEM_TIME_THREAD);
419 stack.stats.totalTime = ns2us(now - mDequeueTime[buf]);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700420 return err;
421}
422
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700423bool SharedBufferClient::needNewBuffer(int buf) const
Mathias Agopian9779b2212009-09-07 16:32:45 -0700424{
425 SharedBufferStack& stack( *mSharedStack );
Mathias Agopian57d89892010-05-21 14:51:33 -0700426 const uint32_t mask = 1<<(31-buf);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700427 return (android_atomic_and(~mask, &stack.reallocMask) & mask) != 0;
428}
429
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700430status_t SharedBufferClient::setCrop(int buf, const Rect& crop)
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700431{
432 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700433 return stack.setCrop(buf, crop);
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700434}
435
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700436status_t SharedBufferClient::setDirtyRegion(int buf, const Region& reg)
Mathias Agopian9779b2212009-09-07 16:32:45 -0700437{
438 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700439 return stack.setDirtyRegion(buf, reg);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700440}
441
Mathias Agopian898c4c92010-05-18 17:06:55 -0700442status_t SharedBufferClient::setBufferCount(
443 int bufferCount, const SetBufferCountCallback& ipc)
Mathias Agopian59751db2010-05-07 15:58:44 -0700444{
Mathias Agopianbe6c8fc2010-05-17 18:54:19 -0700445 SharedBufferStack& stack( *mSharedStack );
Mathias Agopian898c4c92010-05-18 17:06:55 -0700446 if (uint32_t(bufferCount) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopian59751db2010-05-07 15:58:44 -0700447 return BAD_VALUE;
Mathias Agopian898c4c92010-05-18 17:06:55 -0700448
Mathias Agopian25f0bda2010-05-21 14:19:50 -0700449 if (uint32_t(bufferCount) < SharedBufferStack::NUM_BUFFER_MIN)
450 return BAD_VALUE;
451
Mathias Agopian898c4c92010-05-18 17:06:55 -0700452 RWLock::AutoWLock _wr(mLock);
453
454 status_t err = ipc(bufferCount);
455 if (err == NO_ERROR) {
456 mNumBuffers = bufferCount;
457 queued_head = (stack.head + stack.queued) % mNumBuffers;
458 }
459 return err;
Mathias Agopian59751db2010-05-07 15:58:44 -0700460}
461
Mathias Agopian9779b2212009-09-07 16:32:45 -0700462// ----------------------------------------------------------------------------
463
464SharedBufferServer::SharedBufferServer(SharedClient* sharedClient,
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700465 int surface, int num, int32_t identity)
Mathias Agopian898c4c92010-05-18 17:06:55 -0700466 : SharedBufferBase(sharedClient, surface, identity),
467 mNumBuffers(num)
Mathias Agopian9779b2212009-09-07 16:32:45 -0700468{
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700469 mSharedStack->init(identity);
Mathias Agopian7623da42010-06-01 15:12:58 -0700470 mSharedStack->token = surface;
Mathias Agopian9779b2212009-09-07 16:32:45 -0700471 mSharedStack->head = num-1;
472 mSharedStack->available = num;
473 mSharedStack->queued = 0;
474 mSharedStack->reallocMask = 0;
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700475 memset(mSharedStack->buffers, 0, sizeof(mSharedStack->buffers));
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700476 for (int i=0 ; i<num ; i++) {
Mathias Agopian59751db2010-05-07 15:58:44 -0700477 mBufferList.add(i);
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700478 mSharedStack->index[i] = i;
479 }
Mathias Agopian9779b2212009-09-07 16:32:45 -0700480}
481
Mathias Agopian5e140102010-06-08 19:54:15 -0700482SharedBufferServer::~SharedBufferServer()
483{
484}
485
Mathias Agopian9779b2212009-09-07 16:32:45 -0700486ssize_t SharedBufferServer::retireAndLock()
487{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700488 RWLock::AutoRLock _l(mLock);
489
Mathias Agopian9779b2212009-09-07 16:32:45 -0700490 RetireUpdate update(this, mNumBuffers);
491 ssize_t buf = updateCondition( update );
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700492 if (buf >= 0) {
Mathias Agopian898c4c92010-05-18 17:06:55 -0700493 if (uint32_t(buf) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopian3b91e13e2010-04-30 12:59:21 -0700494 return BAD_VALUE;
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700495 SharedBufferStack& stack( *mSharedStack );
496 buf = stack.index[buf];
497 LOGD_IF(DEBUG_ATOMICS && buf>=0, "retire=%d, %s",
498 int(buf), dump("").string());
499 }
Mathias Agopian9779b2212009-09-07 16:32:45 -0700500 return buf;
501}
502
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700503status_t SharedBufferServer::unlock(int buf)
Mathias Agopian9779b2212009-09-07 16:32:45 -0700504{
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700505 UnlockUpdate update(this, buf);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700506 status_t err = updateCondition( update );
507 return err;
508}
509
Mathias Agopian436c6272009-09-10 16:55:13 -0700510void SharedBufferServer::setStatus(status_t status)
511{
Mathias Agopian0c4cec72009-10-02 18:12:30 -0700512 if (status < NO_ERROR) {
513 StatusUpdate update(this, status);
514 updateCondition( update );
515 }
Mathias Agopian436c6272009-09-10 16:55:13 -0700516}
517
Mathias Agopian2be352a2010-05-21 17:24:35 -0700518status_t SharedBufferServer::reallocateAll()
Mathias Agopian9779b2212009-09-07 16:32:45 -0700519{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700520 RWLock::AutoRLock _l(mLock);
521
Mathias Agopian9779b2212009-09-07 16:32:45 -0700522 SharedBufferStack& stack( *mSharedStack );
Mathias Agopian57d89892010-05-21 14:51:33 -0700523 uint32_t mask = mBufferList.getMask();
Mathias Agopian2be352a2010-05-21 17:24:35 -0700524 android_atomic_or(mask, &stack.reallocMask);
525 return NO_ERROR;
526}
527
528status_t SharedBufferServer::reallocateAllExcept(int buffer)
529{
530 RWLock::AutoRLock _l(mLock);
531
532 SharedBufferStack& stack( *mSharedStack );
533 BufferList temp(mBufferList);
534 temp.remove(buffer);
535 uint32_t mask = temp.getMask();
536 android_atomic_or(mask, &stack.reallocMask);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700537 return NO_ERROR;
538}
539
Mathias Agopiane05f07d2009-10-07 16:44:10 -0700540int32_t SharedBufferServer::getQueuedCount() const
541{
542 SharedBufferStack& stack( *mSharedStack );
543 return stack.queued;
544}
545
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700546Region SharedBufferServer::getDirtyRegion(int buf) const
Mathias Agopian9779b2212009-09-07 16:32:45 -0700547{
548 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700549 return stack.getDirtyRegion(buf);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700550}
551
Mathias Agopian59751db2010-05-07 15:58:44 -0700552/*
Mathias Agopian59751db2010-05-07 15:58:44 -0700553 * NOTE: this is not thread-safe on the server-side, meaning
554 * 'head' cannot move during this operation. The client-side
555 * can safely operate an usual.
556 *
557 */
558status_t SharedBufferServer::resize(int newNumBuffers)
559{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700560 if (uint32_t(newNumBuffers) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopian59751db2010-05-07 15:58:44 -0700561 return BAD_VALUE;
562
Mathias Agopian898c4c92010-05-18 17:06:55 -0700563 RWLock::AutoWLock _l(mLock);
564
Mathias Agopian59751db2010-05-07 15:58:44 -0700565 // for now we're not supporting shrinking
566 const int numBuffers = mNumBuffers;
567 if (newNumBuffers < numBuffers)
568 return BAD_VALUE;
569
570 SharedBufferStack& stack( *mSharedStack );
571 const int extra = newNumBuffers - numBuffers;
572
573 // read the head, make sure it's valid
574 int32_t head = stack.head;
Mathias Agopian898c4c92010-05-18 17:06:55 -0700575 if (uint32_t(head) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopian59751db2010-05-07 15:58:44 -0700576 return BAD_VALUE;
577
578 int base = numBuffers;
579 int32_t avail = stack.available;
580 int tail = head - avail + 1;
Mathias Agopiancd30f4f2010-05-17 17:27:26 -0700581
Mathias Agopian59751db2010-05-07 15:58:44 -0700582 if (tail >= 0) {
583 int8_t* const index = const_cast<int8_t*>(stack.index);
584 const int nb = numBuffers - head;
585 memmove(&index[head + extra], &index[head], nb);
586 base = head;
587 // move head 'extra' ahead, this doesn't impact stack.index[head];
588 stack.head = head + extra;
589 }
590 stack.available += extra;
591
592 // fill the new free space with unused buffers
593 BufferList::const_iterator curr(mBufferList.free_begin());
594 for (int i=0 ; i<extra ; i++) {
Mathias Agopiancd30f4f2010-05-17 17:27:26 -0700595 stack.index[base+i] = *curr;
596 mBufferList.add(*curr);
597 ++curr;
Mathias Agopian59751db2010-05-07 15:58:44 -0700598 }
599
600 mNumBuffers = newNumBuffers;
601 return NO_ERROR;
602}
603
Mathias Agopianbcef9ac2009-09-17 01:35:28 -0700604SharedBufferStack::Statistics SharedBufferServer::getStats() const
605{
606 SharedBufferStack& stack( *mSharedStack );
607 return stack.stats;
608}
609
Mathias Agopian59751db2010-05-07 15:58:44 -0700610// ---------------------------------------------------------------------------
611status_t SharedBufferServer::BufferList::add(int value)
612{
613 if (uint32_t(value) >= mCapacity)
614 return BAD_VALUE;
615 uint32_t mask = 1<<(31-value);
616 if (mList & mask)
617 return ALREADY_EXISTS;
618 mList |= mask;
619 return NO_ERROR;
620}
621
622status_t SharedBufferServer::BufferList::remove(int value)
623{
624 if (uint32_t(value) >= mCapacity)
625 return BAD_VALUE;
626 uint32_t mask = 1<<(31-value);
627 if (!(mList & mask))
628 return NAME_NOT_FOUND;
629 mList &= ~mask;
630 return NO_ERROR;
631}
632
Mathias Agopianbcef9ac2009-09-17 01:35:28 -0700633
Mathias Agopian9779b2212009-09-07 16:32:45 -0700634// ---------------------------------------------------------------------------
635}; // namespace android