blob: a43b44021e5475874ec4b1a0c2d58869f17779a0 [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 Agopiane96aa3e2010-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 Agopian9779b2212009-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 Agopiana8a0aa82010-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 Agopian6bb5eba2010-04-05 16:21:53 -070097 size_t count;
98 Rect const* r = dirty.getArray(&count);
Mathias Agopian6bb5eba2010-04-05 16:21:53 -070099 if (count > FlatRegion::NUM_RECT_MAX) {
100 const Rect bounds(dirty.getBounds());
101 reg.count = 1;
Mathias Agopian16a86ee2010-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 Agopian6bb5eba2010-04-05 16:21:53 -0700106 } else {
107 reg.count = count;
108 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian16a86ee2010-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 Agopian6bb5eba2010-04-05 16:21:53 -0700113 }
114 }
Mathias Agopian9779b2212009-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 Agopian16a86ee2010-04-15 18:48:26 -0700124 const FlatRegion& reg(buffers[buffer].dirtyRegion);
Mathias Agopian6bb5eba2010-04-05 16:21:53 -0700125 if (reg.count > FlatRegion::NUM_RECT_MAX)
126 return res;
127
128 if (reg.count == 1) {
Mathias Agopian16a86ee2010-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 Agopian6bb5eba2010-04-05 16:21:53 -0700135 } else {
136 for (size_t i=0 ; i<reg.count ; i++) {
137 const Rect r(
Mathias Agopian16a86ee2010-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 Agopian6bb5eba2010-04-05 16:21:53 -0700142 res.orSelf(r);
143 }
144 }
Mathias Agopian9779b2212009-09-07 16:32:45 -0700145 return res;
146}
147
Mathias Agopiane96aa3e2010-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 Agopian9779b2212009-09-07 16:32:45 -0700168// ----------------------------------------------------------------------------
169
170SharedBufferBase::SharedBufferBase(SharedClient* sharedClient,
Mathias Agopian898c4c92010-05-18 17:06:55 -0700171 int surface, int32_t identity)
Mathias Agopian9779b2212009-09-07 16:32:45 -0700172 : mSharedClient(sharedClient),
173 mSharedStack(sharedClient->surfaces + surface),
Mathias Agopian898c4c92010-05-18 17:06:55 -0700174 mIdentity(identity)
Mathias Agopian9779b2212009-09-07 16:32:45 -0700175{
176}
177
178SharedBufferBase::~SharedBufferBase()
179{
180}
181
Mathias Agopian0c4cec72009-10-02 18:12:30 -0700182status_t SharedBufferBase::getStatus() const
183{
184 SharedBufferStack& stack( *mSharedStack );
185 return stack.status;
186}
187
Mathias Agopian770492c2010-05-28 14:22:23 -0700188int32_t SharedBufferBase::getIdentity() const
189{
190 SharedBufferStack& stack( *mSharedStack );
191 return stack.identity;
192}
193
Mathias Agopian9779b2212009-09-07 16:32:45 -0700194String8 SharedBufferBase::dump(char const* prefix) const
195{
196 const size_t SIZE = 1024;
197 char buffer[SIZE];
198 String8 result;
199 SharedBufferStack& stack( *mSharedStack );
200 snprintf(buffer, SIZE,
Mathias Agopian898c4c92010-05-18 17:06:55 -0700201 "%s[ head=%2d, available=%2d, queued=%2d ] "
Mathias Agopianbe6c8fc2010-05-17 18:54:19 -0700202 "reallocMask=%08x, inUse=%2d, identity=%d, status=%d",
Mathias Agopian898c4c92010-05-18 17:06:55 -0700203 prefix, stack.head, stack.available, stack.queued,
Mathias Agopian9779b2212009-09-07 16:32:45 -0700204 stack.reallocMask, stack.inUse, stack.identity, stack.status);
205 result.append(buffer);
Mathias Agopian898c4c92010-05-18 17:06:55 -0700206 result.append("\n");
Mathias Agopian9779b2212009-09-07 16:32:45 -0700207 return result;
208}
209
Mathias Agopianf590f702010-04-27 16:41:19 -0700210status_t SharedBufferBase::waitForCondition(const ConditionBase& condition)
211{
212 const SharedBufferStack& stack( *mSharedStack );
213 SharedClient& client( *mSharedClient );
214 const nsecs_t TIMEOUT = s2ns(1);
215 const int identity = mIdentity;
216
217 Mutex::Autolock _l(client.lock);
218 while ((condition()==false) &&
219 (stack.identity == identity) &&
220 (stack.status == NO_ERROR))
221 {
222 status_t err = client.cv.waitRelative(client.lock, TIMEOUT);
223 // handle errors and timeouts
224 if (CC_UNLIKELY(err != NO_ERROR)) {
225 if (err == TIMED_OUT) {
226 if (condition()) {
227 LOGE("waitForCondition(%s) timed out (identity=%d), "
228 "but condition is true! We recovered but it "
229 "shouldn't happen." , condition.name(), stack.identity);
230 break;
231 } else {
232 LOGW("waitForCondition(%s) timed out "
233 "(identity=%d, status=%d). "
234 "CPU may be pegged. trying again.", condition.name(),
235 stack.identity, stack.status);
236 }
237 } else {
238 LOGE("waitForCondition(%s) error (%s) ",
239 condition.name(), strerror(-err));
240 return err;
241 }
242 }
243 }
244 return (stack.identity != mIdentity) ? status_t(BAD_INDEX) : stack.status;
245}
Mathias Agopian9779b2212009-09-07 16:32:45 -0700246// ============================================================================
247// conditions and updates
248// ============================================================================
249
250SharedBufferClient::DequeueCondition::DequeueCondition(
251 SharedBufferClient* sbc) : ConditionBase(sbc) {
252}
Mathias Agopianf590f702010-04-27 16:41:19 -0700253bool SharedBufferClient::DequeueCondition::operator()() const {
Mathias Agopian9779b2212009-09-07 16:32:45 -0700254 return stack.available > 0;
255}
256
257SharedBufferClient::LockCondition::LockCondition(
258 SharedBufferClient* sbc, int buf) : ConditionBase(sbc), buf(buf) {
259}
Mathias Agopianf590f702010-04-27 16:41:19 -0700260bool SharedBufferClient::LockCondition::operator()() const {
Mathias Agopian3b91e132010-04-30 12:59:21 -0700261 // NOTE: if stack.head is messed up, we could crash the client
262 // or cause some drawing artifacts. This is okay, as long as it is
263 // limited to the client.
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700264 return (buf != stack.index[stack.head] ||
Mathias Agopian9779b2212009-09-07 16:32:45 -0700265 (stack.queued > 0 && stack.inUse != buf));
266}
267
Mathias Agopian9779b2212009-09-07 16:32:45 -0700268// ----------------------------------------------------------------------------
269
270SharedBufferClient::QueueUpdate::QueueUpdate(SharedBufferBase* sbb)
271 : UpdateBase(sbb) {
272}
273ssize_t SharedBufferClient::QueueUpdate::operator()() {
274 android_atomic_inc(&stack.queued);
275 return NO_ERROR;
276}
277
Mathias Agopianc9289fa2010-08-26 17:42:27 -0700278SharedBufferClient::DequeueUpdate::DequeueUpdate(SharedBufferBase* sbb)
279 : UpdateBase(sbb) {
280}
281ssize_t SharedBufferClient::DequeueUpdate::operator()() {
282 if (android_atomic_dec(&stack.available) == 0) {
283 LOGW("dequeue probably called from multiple threads!");
284 }
285 return NO_ERROR;
286}
287
Mathias Agopian9779b2212009-09-07 16:32:45 -0700288SharedBufferClient::UndoDequeueUpdate::UndoDequeueUpdate(SharedBufferBase* sbb)
289 : UpdateBase(sbb) {
290}
291ssize_t SharedBufferClient::UndoDequeueUpdate::operator()() {
292 android_atomic_inc(&stack.available);
293 return NO_ERROR;
294}
295
296SharedBufferServer::UnlockUpdate::UnlockUpdate(
297 SharedBufferBase* sbb, int lockedBuffer)
298 : UpdateBase(sbb), lockedBuffer(lockedBuffer) {
299}
300ssize_t SharedBufferServer::UnlockUpdate::operator()() {
301 if (stack.inUse != lockedBuffer) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700302 LOGE("unlocking %d, but currently locked buffer is %d "
303 "(identity=%d, token=%d)",
304 lockedBuffer, stack.inUse,
305 stack.identity, stack.token);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700306 return BAD_VALUE;
307 }
308 android_atomic_write(-1, &stack.inUse);
309 return NO_ERROR;
310}
311
312SharedBufferServer::RetireUpdate::RetireUpdate(
313 SharedBufferBase* sbb, int numBuffers)
314 : UpdateBase(sbb), numBuffers(numBuffers) {
315}
316ssize_t SharedBufferServer::RetireUpdate::operator()() {
Mathias Agopian9779b2212009-09-07 16:32:45 -0700317 int32_t head = stack.head;
Mathias Agopian898c4c92010-05-18 17:06:55 -0700318 if (uint32_t(head) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopian3b91e132010-04-30 12:59:21 -0700319 return BAD_VALUE;
Mathias Agopian9779b2212009-09-07 16:32:45 -0700320
321 // Preventively lock the current buffer before updating queued.
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700322 android_atomic_write(stack.index[head], &stack.inUse);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700323
324 // Decrement the number of queued buffers
325 int32_t queued;
326 do {
327 queued = stack.queued;
328 if (queued == 0) {
329 return NOT_ENOUGH_DATA;
330 }
331 } while (android_atomic_cmpxchg(queued, queued-1, &stack.queued));
332
Mathias Agopian9779b2212009-09-07 16:32:45 -0700333 // lock the buffer before advancing head, which automatically unlocks
334 // the buffer we preventively locked upon entering this function
Mathias Agopianbe6c8fc2010-05-17 18:54:19 -0700335
Mathias Agopian59751db2010-05-07 15:58:44 -0700336 head = (head + 1) % numBuffers;
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700337 android_atomic_write(stack.index[head], &stack.inUse);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700338
Mathias Agopian59751db2010-05-07 15:58:44 -0700339 // head is only modified here, so we don't need to use cmpxchg
Mathias Agopian9779b2212009-09-07 16:32:45 -0700340 android_atomic_write(head, &stack.head);
Mathias Agopian59751db2010-05-07 15:58:44 -0700341
Mathias Agopian9779b2212009-09-07 16:32:45 -0700342 // now that head has moved, we can increment the number of available buffers
343 android_atomic_inc(&stack.available);
344 return head;
345}
346
Mathias Agopian436c6272009-09-10 16:55:13 -0700347SharedBufferServer::StatusUpdate::StatusUpdate(
348 SharedBufferBase* sbb, status_t status)
349 : UpdateBase(sbb), status(status) {
350}
351
352ssize_t SharedBufferServer::StatusUpdate::operator()() {
353 android_atomic_write(status, &stack.status);
354 return NO_ERROR;
355}
356
Mathias Agopian9779b2212009-09-07 16:32:45 -0700357// ============================================================================
358
359SharedBufferClient::SharedBufferClient(SharedClient* sharedClient,
Mathias Agopian4961c952009-10-06 19:00:57 -0700360 int surface, int num, int32_t identity)
Mathias Agopian898c4c92010-05-18 17:06:55 -0700361 : SharedBufferBase(sharedClient, surface, identity),
362 mNumBuffers(num), tail(0), undoDequeueTail(0)
Mathias Agopian9779b2212009-09-07 16:32:45 -0700363{
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700364 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianbd852712009-09-14 15:48:42 -0700365 tail = computeTail();
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700366 queued_head = stack.head;
Mathias Agopianbd852712009-09-14 15:48:42 -0700367}
368
Mathias Agopian898c4c92010-05-18 17:06:55 -0700369int32_t SharedBufferClient::computeTail() const
370{
371 SharedBufferStack& stack( *mSharedStack );
372 return (mNumBuffers + stack.head - stack.available + 1) % mNumBuffers;
373}
374
Mathias Agopian9779b2212009-09-07 16:32:45 -0700375ssize_t SharedBufferClient::dequeue()
376{
Mathias Agopian3e63f912009-09-11 19:18:20 -0700377 SharedBufferStack& stack( *mSharedStack );
378
Mathias Agopian898c4c92010-05-18 17:06:55 -0700379 RWLock::AutoRLock _rd(mLock);
380
Mathias Agopianbcef9ac2009-09-17 01:35:28 -0700381 const nsecs_t dequeueTime = systemTime(SYSTEM_TIME_THREAD);
Mathias Agopian3e63f912009-09-11 19:18:20 -0700382
Mathias Agopian9779b2212009-09-07 16:32:45 -0700383 //LOGD("[%d] about to dequeue a buffer",
384 // mSharedStack->identity);
385 DequeueCondition condition(this);
386 status_t err = waitForCondition(condition);
387 if (err != NO_ERROR)
388 return ssize_t(err);
389
Mathias Agopianc9289fa2010-08-26 17:42:27 -0700390 DequeueUpdate update(this);
391 updateCondition( update );
Mathias Agopian9779b2212009-09-07 16:32:45 -0700392
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700393 undoDequeueTail = tail;
394 int dequeued = stack.index[tail];
Mathias Agopian9779b2212009-09-07 16:32:45 -0700395 tail = ((tail+1 >= mNumBuffers) ? 0 : tail+1);
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700396 LOGD_IF(DEBUG_ATOMICS, "dequeued=%d, tail++=%d, %s",
Mathias Agopian9779b2212009-09-07 16:32:45 -0700397 dequeued, tail, dump("").string());
Mathias Agopian3e63f912009-09-11 19:18:20 -0700398
Mathias Agopianbcef9ac2009-09-17 01:35:28 -0700399 mDequeueTime[dequeued] = dequeueTime;
400
Mathias Agopian9779b2212009-09-07 16:32:45 -0700401 return dequeued;
402}
403
404status_t SharedBufferClient::undoDequeue(int buf)
405{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700406 RWLock::AutoRLock _rd(mLock);
407
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700408 // TODO: we can only undo the previous dequeue, we should
409 // enforce that in the api
Mathias Agopian9779b2212009-09-07 16:32:45 -0700410 UndoDequeueUpdate update(this);
411 status_t err = updateCondition( update );
Mathias Agopianbd852712009-09-14 15:48:42 -0700412 if (err == NO_ERROR) {
Mathias Agopianc54c1272010-04-27 16:11:38 -0700413 tail = undoDequeueTail;
Mathias Agopianbd852712009-09-14 15:48:42 -0700414 }
Mathias Agopian9779b2212009-09-07 16:32:45 -0700415 return err;
416}
417
418status_t SharedBufferClient::lock(int buf)
419{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700420 RWLock::AutoRLock _rd(mLock);
421
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700422 SharedBufferStack& stack( *mSharedStack );
Mathias Agopian9779b2212009-09-07 16:32:45 -0700423 LockCondition condition(this, buf);
Mathias Agopianbcef9ac2009-09-17 01:35:28 -0700424 status_t err = waitForCondition(condition);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700425 return err;
426}
427
428status_t SharedBufferClient::queue(int buf)
429{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700430 RWLock::AutoRLock _rd(mLock);
431
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700432 SharedBufferStack& stack( *mSharedStack );
433
Mathias Agopianbe6c8fc2010-05-17 18:54:19 -0700434 queued_head = (queued_head + 1) % mNumBuffers;
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700435 stack.index[queued_head] = buf;
436
Mathias Agopian9779b2212009-09-07 16:32:45 -0700437 QueueUpdate update(this);
438 status_t err = updateCondition( update );
439 LOGD_IF(DEBUG_ATOMICS, "queued=%d, %s", buf, dump("").string());
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700440
Mathias Agopianbcef9ac2009-09-17 01:35:28 -0700441 const nsecs_t now = systemTime(SYSTEM_TIME_THREAD);
442 stack.stats.totalTime = ns2us(now - mDequeueTime[buf]);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700443 return err;
444}
445
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700446bool SharedBufferClient::needNewBuffer(int buf) const
Mathias Agopian9779b2212009-09-07 16:32:45 -0700447{
448 SharedBufferStack& stack( *mSharedStack );
Mathias Agopian57d89892010-05-21 14:51:33 -0700449 const uint32_t mask = 1<<(31-buf);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700450 return (android_atomic_and(~mask, &stack.reallocMask) & mask) != 0;
451}
452
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700453status_t SharedBufferClient::setCrop(int buf, const Rect& crop)
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700454{
455 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700456 return stack.setCrop(buf, crop);
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700457}
458
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700459status_t SharedBufferClient::setTransform(int buf, uint32_t transform)
460{
461 SharedBufferStack& stack( *mSharedStack );
462 return stack.setTransform(buf, uint8_t(transform));
463}
464
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700465status_t SharedBufferClient::setDirtyRegion(int buf, const Region& reg)
Mathias Agopian9779b2212009-09-07 16:32:45 -0700466{
467 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700468 return stack.setDirtyRegion(buf, reg);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700469}
470
Mathias Agopian898c4c92010-05-18 17:06:55 -0700471status_t SharedBufferClient::setBufferCount(
472 int bufferCount, const SetBufferCountCallback& ipc)
Mathias Agopian59751db2010-05-07 15:58:44 -0700473{
Mathias Agopianbe6c8fc2010-05-17 18:54:19 -0700474 SharedBufferStack& stack( *mSharedStack );
Mathias Agopian898c4c92010-05-18 17:06:55 -0700475 if (uint32_t(bufferCount) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopian59751db2010-05-07 15:58:44 -0700476 return BAD_VALUE;
Mathias Agopian898c4c92010-05-18 17:06:55 -0700477
Mathias Agopian25f0bda2010-05-21 14:19:50 -0700478 if (uint32_t(bufferCount) < SharedBufferStack::NUM_BUFFER_MIN)
479 return BAD_VALUE;
480
Mathias Agopian898c4c92010-05-18 17:06:55 -0700481 RWLock::AutoWLock _wr(mLock);
482
483 status_t err = ipc(bufferCount);
484 if (err == NO_ERROR) {
485 mNumBuffers = bufferCount;
486 queued_head = (stack.head + stack.queued) % mNumBuffers;
487 }
488 return err;
Mathias Agopian59751db2010-05-07 15:58:44 -0700489}
490
Mathias Agopian9779b2212009-09-07 16:32:45 -0700491// ----------------------------------------------------------------------------
492
493SharedBufferServer::SharedBufferServer(SharedClient* sharedClient,
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700494 int surface, int num, int32_t identity)
Mathias Agopian898c4c92010-05-18 17:06:55 -0700495 : SharedBufferBase(sharedClient, surface, identity),
496 mNumBuffers(num)
Mathias Agopian9779b2212009-09-07 16:32:45 -0700497{
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700498 mSharedStack->init(identity);
Mathias Agopian7623da42010-06-01 15:12:58 -0700499 mSharedStack->token = surface;
Mathias Agopian9779b2212009-09-07 16:32:45 -0700500 mSharedStack->head = num-1;
501 mSharedStack->available = num;
502 mSharedStack->queued = 0;
503 mSharedStack->reallocMask = 0;
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700504 memset(mSharedStack->buffers, 0, sizeof(mSharedStack->buffers));
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700505 for (int i=0 ; i<num ; i++) {
Mathias Agopian59751db2010-05-07 15:58:44 -0700506 mBufferList.add(i);
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700507 mSharedStack->index[i] = i;
508 }
Mathias Agopian9779b2212009-09-07 16:32:45 -0700509}
510
Mathias Agopian5e140102010-06-08 19:54:15 -0700511SharedBufferServer::~SharedBufferServer()
512{
513}
514
Mathias Agopian9779b2212009-09-07 16:32:45 -0700515ssize_t SharedBufferServer::retireAndLock()
516{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700517 RWLock::AutoRLock _l(mLock);
518
Mathias Agopian9779b2212009-09-07 16:32:45 -0700519 RetireUpdate update(this, mNumBuffers);
520 ssize_t buf = updateCondition( update );
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700521 if (buf >= 0) {
Mathias Agopian898c4c92010-05-18 17:06:55 -0700522 if (uint32_t(buf) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopian3b91e132010-04-30 12:59:21 -0700523 return BAD_VALUE;
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700524 SharedBufferStack& stack( *mSharedStack );
525 buf = stack.index[buf];
526 LOGD_IF(DEBUG_ATOMICS && buf>=0, "retire=%d, %s",
527 int(buf), dump("").string());
528 }
Mathias Agopian9779b2212009-09-07 16:32:45 -0700529 return buf;
530}
531
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700532status_t SharedBufferServer::unlock(int buf)
Mathias Agopian9779b2212009-09-07 16:32:45 -0700533{
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700534 UnlockUpdate update(this, buf);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700535 status_t err = updateCondition( update );
536 return err;
537}
538
Mathias Agopian436c6272009-09-10 16:55:13 -0700539void SharedBufferServer::setStatus(status_t status)
540{
Mathias Agopian0c4cec72009-10-02 18:12:30 -0700541 if (status < NO_ERROR) {
542 StatusUpdate update(this, status);
543 updateCondition( update );
544 }
Mathias Agopian436c6272009-09-10 16:55:13 -0700545}
546
Mathias Agopian2be352a2010-05-21 17:24:35 -0700547status_t SharedBufferServer::reallocateAll()
Mathias Agopian9779b2212009-09-07 16:32:45 -0700548{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700549 RWLock::AutoRLock _l(mLock);
550
Mathias Agopian9779b2212009-09-07 16:32:45 -0700551 SharedBufferStack& stack( *mSharedStack );
Mathias Agopian57d89892010-05-21 14:51:33 -0700552 uint32_t mask = mBufferList.getMask();
Mathias Agopian2be352a2010-05-21 17:24:35 -0700553 android_atomic_or(mask, &stack.reallocMask);
554 return NO_ERROR;
555}
556
557status_t SharedBufferServer::reallocateAllExcept(int buffer)
558{
559 RWLock::AutoRLock _l(mLock);
560
561 SharedBufferStack& stack( *mSharedStack );
562 BufferList temp(mBufferList);
563 temp.remove(buffer);
564 uint32_t mask = temp.getMask();
565 android_atomic_or(mask, &stack.reallocMask);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700566 return NO_ERROR;
567}
568
Mathias Agopiane05f07d2009-10-07 16:44:10 -0700569int32_t SharedBufferServer::getQueuedCount() const
570{
571 SharedBufferStack& stack( *mSharedStack );
572 return stack.queued;
573}
574
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700575Region SharedBufferServer::getDirtyRegion(int buf) const
Mathias Agopian9779b2212009-09-07 16:32:45 -0700576{
577 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700578 return stack.getDirtyRegion(buf);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700579}
580
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700581Rect SharedBufferServer::getCrop(int buf) const
582{
583 SharedBufferStack& stack( *mSharedStack );
584 return stack.getCrop(buf);
585}
586
587uint32_t SharedBufferServer::getTransform(int buf) const
588{
589 SharedBufferStack& stack( *mSharedStack );
590 return stack.getTransform(buf);
591}
592
Mathias Agopian59751db2010-05-07 15:58:44 -0700593/*
Mathias Agopian59751db2010-05-07 15:58:44 -0700594 * NOTE: this is not thread-safe on the server-side, meaning
595 * 'head' cannot move during this operation. The client-side
596 * can safely operate an usual.
597 *
598 */
599status_t SharedBufferServer::resize(int newNumBuffers)
600{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700601 if (uint32_t(newNumBuffers) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopian59751db2010-05-07 15:58:44 -0700602 return BAD_VALUE;
603
Mathias Agopian898c4c92010-05-18 17:06:55 -0700604 RWLock::AutoWLock _l(mLock);
605
Mathias Agopian59751db2010-05-07 15:58:44 -0700606 // for now we're not supporting shrinking
607 const int numBuffers = mNumBuffers;
608 if (newNumBuffers < numBuffers)
609 return BAD_VALUE;
610
611 SharedBufferStack& stack( *mSharedStack );
612 const int extra = newNumBuffers - numBuffers;
613
614 // read the head, make sure it's valid
615 int32_t head = stack.head;
Mathias Agopian898c4c92010-05-18 17:06:55 -0700616 if (uint32_t(head) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopian59751db2010-05-07 15:58:44 -0700617 return BAD_VALUE;
618
619 int base = numBuffers;
620 int32_t avail = stack.available;
621 int tail = head - avail + 1;
Mathias Agopiancd30f4f2010-05-17 17:27:26 -0700622
Mathias Agopian59751db2010-05-07 15:58:44 -0700623 if (tail >= 0) {
624 int8_t* const index = const_cast<int8_t*>(stack.index);
625 const int nb = numBuffers - head;
626 memmove(&index[head + extra], &index[head], nb);
627 base = head;
628 // move head 'extra' ahead, this doesn't impact stack.index[head];
629 stack.head = head + extra;
630 }
631 stack.available += extra;
632
633 // fill the new free space with unused buffers
634 BufferList::const_iterator curr(mBufferList.free_begin());
635 for (int i=0 ; i<extra ; i++) {
Mathias Agopiancd30f4f2010-05-17 17:27:26 -0700636 stack.index[base+i] = *curr;
637 mBufferList.add(*curr);
638 ++curr;
Mathias Agopian59751db2010-05-07 15:58:44 -0700639 }
640
641 mNumBuffers = newNumBuffers;
642 return NO_ERROR;
643}
644
Mathias Agopianbcef9ac2009-09-17 01:35:28 -0700645SharedBufferStack::Statistics SharedBufferServer::getStats() const
646{
647 SharedBufferStack& stack( *mSharedStack );
648 return stack.stats;
649}
650
Mathias Agopian59751db2010-05-07 15:58:44 -0700651// ---------------------------------------------------------------------------
652status_t SharedBufferServer::BufferList::add(int value)
653{
654 if (uint32_t(value) >= mCapacity)
655 return BAD_VALUE;
656 uint32_t mask = 1<<(31-value);
657 if (mList & mask)
658 return ALREADY_EXISTS;
659 mList |= mask;
660 return NO_ERROR;
661}
662
663status_t SharedBufferServer::BufferList::remove(int value)
664{
665 if (uint32_t(value) >= mCapacity)
666 return BAD_VALUE;
667 uint32_t mask = 1<<(31-value);
668 if (!(mList & mask))
669 return NAME_NOT_FOUND;
670 mList &= ~mask;
671 return NO_ERROR;
672}
673
Mathias Agopianbcef9ac2009-09-17 01:35:28 -0700674
Mathias Agopian9779b2212009-09-07 16:32:45 -0700675// ---------------------------------------------------------------------------
676}; // namespace android