blob: 3b2ef848b38f76af5d2e319411fbbb4ef6dda905 [file] [log] [blame]
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "SharedBufferStack"
18
19#include <stdint.h>
20#include <sys/types.h>
21
22#include <utils/Debug.h>
23#include <utils/Log.h>
24#include <utils/threads.h>
25
Mathias Agopian9cce3252010-02-09 17:46:37 -080026#include <private/surfaceflinger/SharedBufferStack.h>
Mathias Agopiancbb288b2009-09-07 16:32:45 -070027
28#include <ui/Rect.h>
29#include <ui/Region.h>
30
31#define DEBUG_ATOMICS 0
32
33namespace android {
34// ----------------------------------------------------------------------------
35
36SharedClient::SharedClient()
Mathias Agopian26d24422010-03-19 16:14:13 -070037 : lock(Mutex::SHARED), cv(Condition::SHARED)
Mathias Agopiancbb288b2009-09-07 16:32:45 -070038{
39}
40
41SharedClient::~SharedClient() {
42}
43
44
45// these functions are used by the clients
46status_t SharedClient::validate(size_t i) const {
Mathias Agopianbb641242010-05-18 17:06:55 -070047 if (uint32_t(i) >= uint32_t(SharedBufferStack::NUM_LAYERS_MAX))
Mathias Agopiancbb288b2009-09-07 16:32:45 -070048 return BAD_INDEX;
49 return surfaces[i].status;
50}
51
Mathias Agopiancbb288b2009-09-07 16:32:45 -070052// ----------------------------------------------------------------------------
53
54
55SharedBufferStack::SharedBufferStack()
Mathias Agopiancbb288b2009-09-07 16:32:45 -070056{
57}
58
Mathias Agopian48d819a2009-09-10 19:41:18 -070059void SharedBufferStack::init(int32_t i)
60{
Mathias Agopianb7e930d2010-06-01 15:12:58 -070061 inUse = -2;
Mathias Agopian48d819a2009-09-10 19:41:18 -070062 status = NO_ERROR;
63 identity = i;
64}
65
Mathias Agopiancc08e682010-04-15 18:48:26 -070066status_t SharedBufferStack::setCrop(int buffer, const Rect& crop)
67{
68 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
69 return BAD_INDEX;
70
71 buffers[buffer].crop.l = uint16_t(crop.left);
72 buffers[buffer].crop.t = uint16_t(crop.top);
73 buffers[buffer].crop.r = uint16_t(crop.right);
74 buffers[buffer].crop.b = uint16_t(crop.bottom);
75 return NO_ERROR;
76}
77
Mathias Agopianb661d662010-08-19 17:01:19 -070078status_t SharedBufferStack::setTransform(int buffer, uint8_t transform)
79{
80 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
81 return BAD_INDEX;
82 buffers[buffer].transform = transform;
83 return NO_ERROR;
84}
85
Mathias Agopiancbb288b2009-09-07 16:32:45 -070086status_t SharedBufferStack::setDirtyRegion(int buffer, const Region& dirty)
87{
88 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
89 return BAD_INDEX;
90
Mathias Agopian245e4d72010-04-21 15:24:11 -070091 FlatRegion& reg(buffers[buffer].dirtyRegion);
92 if (dirty.isEmpty()) {
93 reg.count = 0;
94 return NO_ERROR;
95 }
96
Mathias Agopian1100c8b2010-04-05 16:21:53 -070097 size_t count;
98 Rect const* r = dirty.getArray(&count);
Mathias Agopian1100c8b2010-04-05 16:21:53 -070099 if (count > FlatRegion::NUM_RECT_MAX) {
100 const Rect bounds(dirty.getBounds());
101 reg.count = 1;
Mathias Agopiancc08e682010-04-15 18:48:26 -0700102 reg.rects[0].l = uint16_t(bounds.left);
103 reg.rects[0].t = uint16_t(bounds.top);
104 reg.rects[0].r = uint16_t(bounds.right);
105 reg.rects[0].b = uint16_t(bounds.bottom);
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700106 } else {
107 reg.count = count;
108 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancc08e682010-04-15 18:48:26 -0700109 reg.rects[i].l = uint16_t(r[i].left);
110 reg.rects[i].t = uint16_t(r[i].top);
111 reg.rects[i].r = uint16_t(r[i].right);
112 reg.rects[i].b = uint16_t(r[i].bottom);
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700113 }
114 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700115 return NO_ERROR;
116}
117
118Region SharedBufferStack::getDirtyRegion(int buffer) const
119{
120 Region res;
121 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
122 return res;
123
Mathias Agopiancc08e682010-04-15 18:48:26 -0700124 const FlatRegion& reg(buffers[buffer].dirtyRegion);
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700125 if (reg.count > FlatRegion::NUM_RECT_MAX)
126 return res;
127
128 if (reg.count == 1) {
Mathias Agopiancc08e682010-04-15 18:48:26 -0700129 const Rect r(
130 reg.rects[0].l,
131 reg.rects[0].t,
132 reg.rects[0].r,
133 reg.rects[0].b);
134 res.set(r);
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700135 } else {
136 for (size_t i=0 ; i<reg.count ; i++) {
137 const Rect r(
Mathias Agopiancc08e682010-04-15 18:48:26 -0700138 reg.rects[i].l,
139 reg.rects[i].t,
140 reg.rects[i].r,
141 reg.rects[i].b);
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700142 res.orSelf(r);
143 }
144 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700145 return res;
146}
147
Mathias Agopianb661d662010-08-19 17:01:19 -0700148Rect SharedBufferStack::getCrop(int buffer) const
149{
150 Rect res(-1, -1);
151 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
152 return res;
153 res.left = buffers[buffer].crop.l;
154 res.top = buffers[buffer].crop.t;
155 res.right = buffers[buffer].crop.r;
156 res.bottom = buffers[buffer].crop.b;
157 return res;
158}
159
160uint32_t SharedBufferStack::getTransform(int buffer) const
161{
162 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
163 return 0;
164 return buffers[buffer].transform;
165}
166
167
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700168// ----------------------------------------------------------------------------
169
170SharedBufferBase::SharedBufferBase(SharedClient* sharedClient,
Mathias Agopianbb641242010-05-18 17:06:55 -0700171 int surface, int32_t identity)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700172 : mSharedClient(sharedClient),
173 mSharedStack(sharedClient->surfaces + surface),
Mathias Agopianbb641242010-05-18 17:06:55 -0700174 mIdentity(identity)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700175{
176}
177
178SharedBufferBase::~SharedBufferBase()
179{
180}
181
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700182status_t SharedBufferBase::getStatus() const
183{
184 SharedBufferStack& stack( *mSharedStack );
185 return stack.status;
186}
187
Mathias Agopian7e27f052010-05-28 14:22:23 -0700188int32_t SharedBufferBase::getIdentity() const
189{
190 SharedBufferStack& stack( *mSharedStack );
191 return stack.identity;
192}
193
Mathias Agopiancbb288b2009-09-07 16:32:45 -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 Agopianbb641242010-05-18 17:06:55 -0700201 "%s[ head=%2d, available=%2d, queued=%2d ] "
Mathias Agopianb5c45772010-05-17 18:54:19 -0700202 "reallocMask=%08x, inUse=%2d, identity=%d, status=%d",
Mathias Agopianbb641242010-05-18 17:06:55 -0700203 prefix, stack.head, stack.available, stack.queued,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700204 stack.reallocMask, stack.inUse, stack.identity, stack.status);
205 result.append(buffer);
Mathias Agopianbb641242010-05-18 17:06:55 -0700206 result.append("\n");
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700207 return result;
208}
209
Mathias Agopianb2965332010-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 Agopiancbb288b2009-09-07 16:32:45 -0700246// ============================================================================
247// conditions and updates
248// ============================================================================
249
250SharedBufferClient::DequeueCondition::DequeueCondition(
251 SharedBufferClient* sbc) : ConditionBase(sbc) {
252}
Mathias Agopianb2965332010-04-27 16:41:19 -0700253bool SharedBufferClient::DequeueCondition::operator()() const {
Mathias Agopiancbb288b2009-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 Agopianb2965332010-04-27 16:41:19 -0700260bool SharedBufferClient::LockCondition::operator()() const {
Mathias Agopiand5212872010-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 Agopianc0a91642010-04-27 21:08:20 -0700264 return (buf != stack.index[stack.head] ||
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700265 (stack.queued > 0 && stack.inUse != buf));
266}
267
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700268SharedBufferServer::BuffersAvailableCondition::BuffersAvailableCondition(
269 SharedBufferServer* sbs, int numBuffers) : ConditionBase(sbs),
270 mNumBuffers(numBuffers) {
271}
272bool SharedBufferServer::BuffersAvailableCondition::operator()() const {
273 return stack.available == mNumBuffers;
274}
275
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700276// ----------------------------------------------------------------------------
277
278SharedBufferClient::QueueUpdate::QueueUpdate(SharedBufferBase* sbb)
279 : UpdateBase(sbb) {
280}
281ssize_t SharedBufferClient::QueueUpdate::operator()() {
282 android_atomic_inc(&stack.queued);
283 return NO_ERROR;
284}
285
Mathias Agopian0e7f4292010-08-26 17:42:27 -0700286SharedBufferClient::DequeueUpdate::DequeueUpdate(SharedBufferBase* sbb)
287 : UpdateBase(sbb) {
288}
289ssize_t SharedBufferClient::DequeueUpdate::operator()() {
290 if (android_atomic_dec(&stack.available) == 0) {
291 LOGW("dequeue probably called from multiple threads!");
292 }
293 return NO_ERROR;
294}
295
Mathias Agopian19957552010-10-01 16:22:41 -0700296SharedBufferClient::CancelUpdate::CancelUpdate(SharedBufferBase* sbb,
297 int tail, int buf)
298 : UpdateBase(sbb), tail(tail), buf(buf) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700299}
Mathias Agopian19957552010-10-01 16:22:41 -0700300ssize_t SharedBufferClient::CancelUpdate::operator()() {
301 stack.index[tail] = buf;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700302 android_atomic_inc(&stack.available);
303 return NO_ERROR;
304}
305
306SharedBufferServer::UnlockUpdate::UnlockUpdate(
307 SharedBufferBase* sbb, int lockedBuffer)
308 : UpdateBase(sbb), lockedBuffer(lockedBuffer) {
309}
310ssize_t SharedBufferServer::UnlockUpdate::operator()() {
311 if (stack.inUse != lockedBuffer) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700312 LOGE("unlocking %d, but currently locked buffer is %d "
313 "(identity=%d, token=%d)",
314 lockedBuffer, stack.inUse,
315 stack.identity, stack.token);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700316 return BAD_VALUE;
317 }
318 android_atomic_write(-1, &stack.inUse);
319 return NO_ERROR;
320}
321
322SharedBufferServer::RetireUpdate::RetireUpdate(
323 SharedBufferBase* sbb, int numBuffers)
324 : UpdateBase(sbb), numBuffers(numBuffers) {
325}
326ssize_t SharedBufferServer::RetireUpdate::operator()() {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700327 int32_t head = stack.head;
Mathias Agopianbb641242010-05-18 17:06:55 -0700328 if (uint32_t(head) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopiand5212872010-04-30 12:59:21 -0700329 return BAD_VALUE;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700330
331 // Preventively lock the current buffer before updating queued.
Mathias Agopian19957552010-10-01 16:22:41 -0700332 android_atomic_write(stack.headBuf, &stack.inUse);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700333
334 // Decrement the number of queued buffers
335 int32_t queued;
336 do {
337 queued = stack.queued;
338 if (queued == 0) {
339 return NOT_ENOUGH_DATA;
340 }
341 } while (android_atomic_cmpxchg(queued, queued-1, &stack.queued));
342
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700343 // lock the buffer before advancing head, which automatically unlocks
344 // the buffer we preventively locked upon entering this function
Mathias Agopianb5c45772010-05-17 18:54:19 -0700345
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700346 head = (head + 1) % numBuffers;
Mathias Agopian19957552010-10-01 16:22:41 -0700347 const int8_t headBuf = stack.index[head];
348 stack.headBuf = headBuf;
349 android_atomic_write(headBuf, &stack.inUse);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700350
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700351 // head is only modified here, so we don't need to use cmpxchg
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700352 android_atomic_write(head, &stack.head);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700353
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700354 // now that head has moved, we can increment the number of available buffers
355 android_atomic_inc(&stack.available);
356 return head;
357}
358
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700359SharedBufferServer::StatusUpdate::StatusUpdate(
360 SharedBufferBase* sbb, status_t status)
361 : UpdateBase(sbb), status(status) {
362}
363
364ssize_t SharedBufferServer::StatusUpdate::operator()() {
365 android_atomic_write(status, &stack.status);
366 return NO_ERROR;
367}
368
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700369// ============================================================================
370
371SharedBufferClient::SharedBufferClient(SharedClient* sharedClient,
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700372 int surface, int num, int32_t identity)
Mathias Agopianbb641242010-05-18 17:06:55 -0700373 : SharedBufferBase(sharedClient, surface, identity),
Mathias Agopian19957552010-10-01 16:22:41 -0700374 mNumBuffers(num), tail(0)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700375{
Mathias Agopianc0a91642010-04-27 21:08:20 -0700376 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianc7d56012009-09-14 15:48:42 -0700377 tail = computeTail();
Mathias Agopianc0a91642010-04-27 21:08:20 -0700378 queued_head = stack.head;
Mathias Agopianc7d56012009-09-14 15:48:42 -0700379}
380
Mathias Agopianbb641242010-05-18 17:06:55 -0700381int32_t SharedBufferClient::computeTail() const
382{
383 SharedBufferStack& stack( *mSharedStack );
384 return (mNumBuffers + stack.head - stack.available + 1) % mNumBuffers;
385}
386
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700387ssize_t SharedBufferClient::dequeue()
388{
Mathias Agopian40d57992009-09-11 19:18:20 -0700389 SharedBufferStack& stack( *mSharedStack );
390
Mathias Agopianbb641242010-05-18 17:06:55 -0700391 RWLock::AutoRLock _rd(mLock);
392
Mathias Agopian86f73292009-09-17 01:35:28 -0700393 const nsecs_t dequeueTime = systemTime(SYSTEM_TIME_THREAD);
Mathias Agopian40d57992009-09-11 19:18:20 -0700394
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700395 //LOGD("[%d] about to dequeue a buffer",
396 // mSharedStack->identity);
397 DequeueCondition condition(this);
398 status_t err = waitForCondition(condition);
399 if (err != NO_ERROR)
400 return ssize_t(err);
401
Mathias Agopian0e7f4292010-08-26 17:42:27 -0700402 DequeueUpdate update(this);
403 updateCondition( update );
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700404
Mathias Agopianc0a91642010-04-27 21:08:20 -0700405 int dequeued = stack.index[tail];
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700406 tail = ((tail+1 >= mNumBuffers) ? 0 : tail+1);
Mathias Agopianc0a91642010-04-27 21:08:20 -0700407 LOGD_IF(DEBUG_ATOMICS, "dequeued=%d, tail++=%d, %s",
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700408 dequeued, tail, dump("").string());
Mathias Agopian40d57992009-09-11 19:18:20 -0700409
Mathias Agopian86f73292009-09-17 01:35:28 -0700410 mDequeueTime[dequeued] = dequeueTime;
411
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700412 return dequeued;
413}
414
415status_t SharedBufferClient::undoDequeue(int buf)
416{
Mathias Agopian19957552010-10-01 16:22:41 -0700417 return cancel(buf);
418}
419
420status_t SharedBufferClient::cancel(int buf)
421{
Mathias Agopianbb641242010-05-18 17:06:55 -0700422 RWLock::AutoRLock _rd(mLock);
423
Mathias Agopian19957552010-10-01 16:22:41 -0700424 // calculate the new position of the tail index (essentially tail--)
425 int localTail = (tail + mNumBuffers - 1) % mNumBuffers;
426 CancelUpdate update(this, localTail, buf);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700427 status_t err = updateCondition( update );
Mathias Agopianc7d56012009-09-14 15:48:42 -0700428 if (err == NO_ERROR) {
Mathias Agopian19957552010-10-01 16:22:41 -0700429 tail = localTail;
Mathias Agopianc7d56012009-09-14 15:48:42 -0700430 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700431 return err;
432}
433
434status_t SharedBufferClient::lock(int buf)
435{
Mathias Agopianbb641242010-05-18 17:06:55 -0700436 RWLock::AutoRLock _rd(mLock);
437
Mathias Agopianc0a91642010-04-27 21:08:20 -0700438 SharedBufferStack& stack( *mSharedStack );
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700439 LockCondition condition(this, buf);
Mathias Agopian86f73292009-09-17 01:35:28 -0700440 status_t err = waitForCondition(condition);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700441 return err;
442}
443
444status_t SharedBufferClient::queue(int buf)
445{
Mathias Agopianbb641242010-05-18 17:06:55 -0700446 RWLock::AutoRLock _rd(mLock);
447
Mathias Agopianc0a91642010-04-27 21:08:20 -0700448 SharedBufferStack& stack( *mSharedStack );
449
Mathias Agopianb5c45772010-05-17 18:54:19 -0700450 queued_head = (queued_head + 1) % mNumBuffers;
Mathias Agopianc0a91642010-04-27 21:08:20 -0700451 stack.index[queued_head] = buf;
452
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700453 QueueUpdate update(this);
454 status_t err = updateCondition( update );
455 LOGD_IF(DEBUG_ATOMICS, "queued=%d, %s", buf, dump("").string());
Mathias Agopianc0a91642010-04-27 21:08:20 -0700456
Mathias Agopian86f73292009-09-17 01:35:28 -0700457 const nsecs_t now = systemTime(SYSTEM_TIME_THREAD);
458 stack.stats.totalTime = ns2us(now - mDequeueTime[buf]);
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700459
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700460 return err;
461}
462
Mathias Agopianc0a91642010-04-27 21:08:20 -0700463bool SharedBufferClient::needNewBuffer(int buf) const
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700464{
465 SharedBufferStack& stack( *mSharedStack );
Mathias Agopiana0b3f1d2010-05-21 14:51:33 -0700466 const uint32_t mask = 1<<(31-buf);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700467 return (android_atomic_and(~mask, &stack.reallocMask) & mask) != 0;
468}
469
Mathias Agopianc0a91642010-04-27 21:08:20 -0700470status_t SharedBufferClient::setCrop(int buf, const Rect& crop)
Mathias Agopiancc08e682010-04-15 18:48:26 -0700471{
472 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianc0a91642010-04-27 21:08:20 -0700473 return stack.setCrop(buf, crop);
Mathias Agopiancc08e682010-04-15 18:48:26 -0700474}
475
Mathias Agopianb661d662010-08-19 17:01:19 -0700476status_t SharedBufferClient::setTransform(int buf, uint32_t transform)
477{
478 SharedBufferStack& stack( *mSharedStack );
479 return stack.setTransform(buf, uint8_t(transform));
480}
481
Mathias Agopianc0a91642010-04-27 21:08:20 -0700482status_t SharedBufferClient::setDirtyRegion(int buf, const Region& reg)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700483{
484 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianc0a91642010-04-27 21:08:20 -0700485 return stack.setDirtyRegion(buf, reg);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700486}
487
Mathias Agopianbb641242010-05-18 17:06:55 -0700488status_t SharedBufferClient::setBufferCount(
489 int bufferCount, const SetBufferCountCallback& ipc)
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700490{
Mathias Agopianb5c45772010-05-17 18:54:19 -0700491 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianbb641242010-05-18 17:06:55 -0700492 if (uint32_t(bufferCount) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700493 return BAD_VALUE;
Mathias Agopianbb641242010-05-18 17:06:55 -0700494
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700495 if (uint32_t(bufferCount) < SharedBufferStack::NUM_BUFFER_MIN)
496 return BAD_VALUE;
497
Mathias Agopianbb641242010-05-18 17:06:55 -0700498 RWLock::AutoWLock _wr(mLock);
499
500 status_t err = ipc(bufferCount);
501 if (err == NO_ERROR) {
502 mNumBuffers = bufferCount;
503 queued_head = (stack.head + stack.queued) % mNumBuffers;
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700504 tail = computeTail();
Mathias Agopianbb641242010-05-18 17:06:55 -0700505 }
506 return err;
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700507}
508
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700509// ----------------------------------------------------------------------------
510
511SharedBufferServer::SharedBufferServer(SharedClient* sharedClient,
Mathias Agopian48d819a2009-09-10 19:41:18 -0700512 int surface, int num, int32_t identity)
Mathias Agopianbb641242010-05-18 17:06:55 -0700513 : SharedBufferBase(sharedClient, surface, identity),
514 mNumBuffers(num)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700515{
Mathias Agopian48d819a2009-09-10 19:41:18 -0700516 mSharedStack->init(identity);
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700517 mSharedStack->token = surface;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700518 mSharedStack->head = num-1;
519 mSharedStack->available = num;
520 mSharedStack->queued = 0;
521 mSharedStack->reallocMask = 0;
Mathias Agopiancc08e682010-04-15 18:48:26 -0700522 memset(mSharedStack->buffers, 0, sizeof(mSharedStack->buffers));
Mathias Agopianc0a91642010-04-27 21:08:20 -0700523 for (int i=0 ; i<num ; i++) {
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700524 mBufferList.add(i);
Mathias Agopianc0a91642010-04-27 21:08:20 -0700525 mSharedStack->index[i] = i;
526 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700527}
528
Mathias Agopian579b3f82010-06-08 19:54:15 -0700529SharedBufferServer::~SharedBufferServer()
530{
531}
532
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700533ssize_t SharedBufferServer::retireAndLock()
534{
Mathias Agopianbb641242010-05-18 17:06:55 -0700535 RWLock::AutoRLock _l(mLock);
536
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700537 RetireUpdate update(this, mNumBuffers);
538 ssize_t buf = updateCondition( update );
Mathias Agopianc0a91642010-04-27 21:08:20 -0700539 if (buf >= 0) {
Mathias Agopianbb641242010-05-18 17:06:55 -0700540 if (uint32_t(buf) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopiand5212872010-04-30 12:59:21 -0700541 return BAD_VALUE;
Mathias Agopianc0a91642010-04-27 21:08:20 -0700542 SharedBufferStack& stack( *mSharedStack );
543 buf = stack.index[buf];
544 LOGD_IF(DEBUG_ATOMICS && buf>=0, "retire=%d, %s",
545 int(buf), dump("").string());
546 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700547 return buf;
548}
549
Mathias Agopianc0a91642010-04-27 21:08:20 -0700550status_t SharedBufferServer::unlock(int buf)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700551{
Mathias Agopianc0a91642010-04-27 21:08:20 -0700552 UnlockUpdate update(this, buf);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700553 status_t err = updateCondition( update );
554 return err;
555}
556
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700557void SharedBufferServer::setStatus(status_t status)
558{
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700559 if (status < NO_ERROR) {
560 StatusUpdate update(this, status);
561 updateCondition( update );
562 }
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700563}
564
Mathias Agopiana138f892010-05-21 17:24:35 -0700565status_t SharedBufferServer::reallocateAll()
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700566{
Mathias Agopianbb641242010-05-18 17:06:55 -0700567 RWLock::AutoRLock _l(mLock);
568
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700569 SharedBufferStack& stack( *mSharedStack );
Mathias Agopiana0b3f1d2010-05-21 14:51:33 -0700570 uint32_t mask = mBufferList.getMask();
Mathias Agopiana138f892010-05-21 17:24:35 -0700571 android_atomic_or(mask, &stack.reallocMask);
572 return NO_ERROR;
573}
574
575status_t SharedBufferServer::reallocateAllExcept(int buffer)
576{
577 RWLock::AutoRLock _l(mLock);
578
579 SharedBufferStack& stack( *mSharedStack );
580 BufferList temp(mBufferList);
581 temp.remove(buffer);
582 uint32_t mask = temp.getMask();
583 android_atomic_or(mask, &stack.reallocMask);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700584 return NO_ERROR;
585}
586
Mathias Agopiane7005012009-10-07 16:44:10 -0700587int32_t SharedBufferServer::getQueuedCount() const
588{
589 SharedBufferStack& stack( *mSharedStack );
590 return stack.queued;
591}
592
Mathias Agopianc0a91642010-04-27 21:08:20 -0700593Region SharedBufferServer::getDirtyRegion(int buf) const
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700594{
595 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianc0a91642010-04-27 21:08:20 -0700596 return stack.getDirtyRegion(buf);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700597}
598
Mathias Agopianb661d662010-08-19 17:01:19 -0700599Rect SharedBufferServer::getCrop(int buf) const
600{
601 SharedBufferStack& stack( *mSharedStack );
602 return stack.getCrop(buf);
603}
604
605uint32_t SharedBufferServer::getTransform(int buf) const
606{
607 SharedBufferStack& stack( *mSharedStack );
608 return stack.getTransform(buf);
609}
610
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700611/*
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700612 * NOTE: this is not thread-safe on the server-side, meaning
613 * 'head' cannot move during this operation. The client-side
614 * can safely operate an usual.
615 *
616 */
617status_t SharedBufferServer::resize(int newNumBuffers)
618{
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700619 if ((unsigned int)(newNumBuffers) < SharedBufferStack::NUM_BUFFER_MIN ||
620 (unsigned int)(newNumBuffers) > SharedBufferStack::NUM_BUFFER_MAX) {
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700621 return BAD_VALUE;
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700622 }
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700623
Mathias Agopianbb641242010-05-18 17:06:55 -0700624 RWLock::AutoWLock _l(mLock);
625
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700626 if (newNumBuffers < mNumBuffers) {
627 return shrink(newNumBuffers);
628 } else {
629 return grow(newNumBuffers);
630 }
631}
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700632
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700633status_t SharedBufferServer::grow(int newNumBuffers)
634{
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700635 SharedBufferStack& stack( *mSharedStack );
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700636 const int numBuffers = mNumBuffers;
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700637 const int extra = newNumBuffers - numBuffers;
638
639 // read the head, make sure it's valid
640 int32_t head = stack.head;
Mathias Agopianbb641242010-05-18 17:06:55 -0700641 if (uint32_t(head) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700642 return BAD_VALUE;
643
644 int base = numBuffers;
645 int32_t avail = stack.available;
646 int tail = head - avail + 1;
Mathias Agopiand6297f72010-05-17 17:27:26 -0700647
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700648 if (tail >= 0) {
649 int8_t* const index = const_cast<int8_t*>(stack.index);
650 const int nb = numBuffers - head;
651 memmove(&index[head + extra], &index[head], nb);
652 base = head;
653 // move head 'extra' ahead, this doesn't impact stack.index[head];
654 stack.head = head + extra;
655 }
656 stack.available += extra;
657
658 // fill the new free space with unused buffers
659 BufferList::const_iterator curr(mBufferList.free_begin());
660 for (int i=0 ; i<extra ; i++) {
Mathias Agopiand6297f72010-05-17 17:27:26 -0700661 stack.index[base+i] = *curr;
662 mBufferList.add(*curr);
663 ++curr;
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700664 }
665
666 mNumBuffers = newNumBuffers;
667 return NO_ERROR;
668}
669
Jamie Gennis54cc83e2010-11-02 11:51:32 -0700670status_t SharedBufferServer::shrink(int newNumBuffers)
671{
672 SharedBufferStack& stack( *mSharedStack );
673
674 // Shrinking is only supported if there are no buffers currently dequeued.
675 int32_t avail = stack.available;
676 int32_t queued = stack.queued;
677 if (avail + queued != mNumBuffers) {
678 return INVALID_OPERATION;
679 }
680
681 // Wait for any queued buffers to be displayed.
682 BuffersAvailableCondition condition(this, mNumBuffers);
683 status_t err = waitForCondition(condition);
684 if (err < 0) {
685 return err;
686 }
687
688 // Reset head to index 0 and make it refer to buffer 0. The same renaming
689 // (head -> 0) is done in the BufferManager.
690 int32_t head = stack.head;
691 int8_t* index = const_cast<int8_t*>(stack.index);
692 for (int8_t i = 0; i < newNumBuffers; i++) {
693 index[i] = i;
694 }
695 stack.head = 0;
696 stack.headBuf = 0;
697
698 // If one of the buffers is in use it must be the head buffer, which we are
699 // renaming to buffer 0.
700 if (stack.inUse > 0) {
701 stack.inUse = 0;
702 }
703
704 // Free the buffers from the end of the list that are no longer needed.
705 for (int i = newNumBuffers; i < mNumBuffers; i++) {
706 mBufferList.remove(i);
707 }
708
709 // Tell the client to reallocate all the buffers.
710 reallocateAll();
711
712 mNumBuffers = newNumBuffers;
713 stack.available = newNumBuffers;
714
715 return NO_ERROR;
716}
717
Mathias Agopian86f73292009-09-17 01:35:28 -0700718SharedBufferStack::Statistics SharedBufferServer::getStats() const
719{
720 SharedBufferStack& stack( *mSharedStack );
721 return stack.stats;
722}
723
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700724// ---------------------------------------------------------------------------
725status_t SharedBufferServer::BufferList::add(int value)
726{
727 if (uint32_t(value) >= mCapacity)
728 return BAD_VALUE;
729 uint32_t mask = 1<<(31-value);
730 if (mList & mask)
731 return ALREADY_EXISTS;
732 mList |= mask;
733 return NO_ERROR;
734}
735
736status_t SharedBufferServer::BufferList::remove(int value)
737{
738 if (uint32_t(value) >= mCapacity)
739 return BAD_VALUE;
740 uint32_t mask = 1<<(31-value);
741 if (!(mList & mask))
742 return NAME_NOT_FOUND;
743 mList &= ~mask;
744 return NO_ERROR;
745}
746
Mathias Agopian86f73292009-09-17 01:35:28 -0700747
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700748// ---------------------------------------------------------------------------
749}; // namespace android