blob: b45e43fb3d858a85110404f4ddad430b9c7c447f [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 Agopian248b5bd2009-09-10 19:41:18 -070061 status = NO_ERROR;
62 identity = i;
63}
64
Mathias Agopian16a86ee2010-04-15 18:48:26 -070065status_t SharedBufferStack::setCrop(int buffer, const Rect& crop)
66{
67 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
68 return BAD_INDEX;
69
70 buffers[buffer].crop.l = uint16_t(crop.left);
71 buffers[buffer].crop.t = uint16_t(crop.top);
72 buffers[buffer].crop.r = uint16_t(crop.right);
73 buffers[buffer].crop.b = uint16_t(crop.bottom);
74 return NO_ERROR;
75}
76
Mathias Agopiane96aa3e2010-08-19 17:01:19 -070077status_t SharedBufferStack::setTransform(int buffer, uint8_t transform)
78{
79 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
80 return BAD_INDEX;
81 buffers[buffer].transform = transform;
82 return NO_ERROR;
83}
84
Mathias Agopian9779b2212009-09-07 16:32:45 -070085status_t SharedBufferStack::setDirtyRegion(int buffer, const Region& dirty)
86{
87 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
88 return BAD_INDEX;
89
Mathias Agopiana8a0aa82010-04-21 15:24:11 -070090 FlatRegion& reg(buffers[buffer].dirtyRegion);
91 if (dirty.isEmpty()) {
92 reg.count = 0;
93 return NO_ERROR;
94 }
95
Mathias Agopian6bb5eba2010-04-05 16:21:53 -070096 size_t count;
97 Rect const* r = dirty.getArray(&count);
Mathias Agopian6bb5eba2010-04-05 16:21:53 -070098 if (count > FlatRegion::NUM_RECT_MAX) {
99 const Rect bounds(dirty.getBounds());
100 reg.count = 1;
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700101 reg.rects[0].l = uint16_t(bounds.left);
102 reg.rects[0].t = uint16_t(bounds.top);
103 reg.rects[0].r = uint16_t(bounds.right);
104 reg.rects[0].b = uint16_t(bounds.bottom);
Mathias Agopian6bb5eba2010-04-05 16:21:53 -0700105 } else {
106 reg.count = count;
107 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700108 reg.rects[i].l = uint16_t(r[i].left);
109 reg.rects[i].t = uint16_t(r[i].top);
110 reg.rects[i].r = uint16_t(r[i].right);
111 reg.rects[i].b = uint16_t(r[i].bottom);
Mathias Agopian6bb5eba2010-04-05 16:21:53 -0700112 }
113 }
Mathias Agopian9779b2212009-09-07 16:32:45 -0700114 return NO_ERROR;
115}
116
117Region SharedBufferStack::getDirtyRegion(int buffer) const
118{
119 Region res;
120 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
121 return res;
122
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700123 const FlatRegion& reg(buffers[buffer].dirtyRegion);
Mathias Agopian6bb5eba2010-04-05 16:21:53 -0700124 if (reg.count > FlatRegion::NUM_RECT_MAX)
125 return res;
126
127 if (reg.count == 1) {
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700128 const Rect r(
129 reg.rects[0].l,
130 reg.rects[0].t,
131 reg.rects[0].r,
132 reg.rects[0].b);
133 res.set(r);
Mathias Agopian6bb5eba2010-04-05 16:21:53 -0700134 } else {
135 for (size_t i=0 ; i<reg.count ; i++) {
136 const Rect r(
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700137 reg.rects[i].l,
138 reg.rects[i].t,
139 reg.rects[i].r,
140 reg.rects[i].b);
Mathias Agopian6bb5eba2010-04-05 16:21:53 -0700141 res.orSelf(r);
142 }
143 }
Mathias Agopian9779b2212009-09-07 16:32:45 -0700144 return res;
145}
146
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700147Rect SharedBufferStack::getCrop(int buffer) const
148{
149 Rect res(-1, -1);
150 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
151 return res;
152 res.left = buffers[buffer].crop.l;
153 res.top = buffers[buffer].crop.t;
154 res.right = buffers[buffer].crop.r;
155 res.bottom = buffers[buffer].crop.b;
156 return res;
157}
158
159uint32_t SharedBufferStack::getTransform(int buffer) const
160{
161 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
162 return 0;
163 return buffers[buffer].transform;
164}
165
166
Mathias Agopian9779b2212009-09-07 16:32:45 -0700167// ----------------------------------------------------------------------------
168
169SharedBufferBase::SharedBufferBase(SharedClient* sharedClient,
Mathias Agopian898c4c92010-05-18 17:06:55 -0700170 int surface, int32_t identity)
Mathias Agopian9779b2212009-09-07 16:32:45 -0700171 : mSharedClient(sharedClient),
172 mSharedStack(sharedClient->surfaces + surface),
Mathias Agopian898c4c92010-05-18 17:06:55 -0700173 mIdentity(identity)
Mathias Agopian9779b2212009-09-07 16:32:45 -0700174{
175}
176
177SharedBufferBase::~SharedBufferBase()
178{
179}
180
Mathias Agopian0c4cec72009-10-02 18:12:30 -0700181status_t SharedBufferBase::getStatus() const
182{
183 SharedBufferStack& stack( *mSharedStack );
184 return stack.status;
185}
186
Mathias Agopian770492c2010-05-28 14:22:23 -0700187int32_t SharedBufferBase::getIdentity() const
188{
189 SharedBufferStack& stack( *mSharedStack );
190 return stack.identity;
191}
192
Mathias Agopian9779b2212009-09-07 16:32:45 -0700193String8 SharedBufferBase::dump(char const* prefix) const
194{
195 const size_t SIZE = 1024;
196 char buffer[SIZE];
197 String8 result;
198 SharedBufferStack& stack( *mSharedStack );
199 snprintf(buffer, SIZE,
Mathias Agopian898c4c92010-05-18 17:06:55 -0700200 "%s[ head=%2d, available=%2d, queued=%2d ] "
Mathias Agopian21956042011-01-18 15:51:30 -0800201 "reallocMask=%08x, identity=%d, status=%d",
Mathias Agopian898c4c92010-05-18 17:06:55 -0700202 prefix, stack.head, stack.available, stack.queued,
Mathias Agopian21956042011-01-18 15:51:30 -0800203 stack.reallocMask, stack.identity, stack.status);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700204 result.append(buffer);
Mathias Agopian898c4c92010-05-18 17:06:55 -0700205 result.append("\n");
Mathias Agopian9779b2212009-09-07 16:32:45 -0700206 return result;
207}
208
Mathias Agopianf590f702010-04-27 16:41:19 -0700209status_t SharedBufferBase::waitForCondition(const ConditionBase& condition)
210{
211 const SharedBufferStack& stack( *mSharedStack );
212 SharedClient& client( *mSharedClient );
213 const nsecs_t TIMEOUT = s2ns(1);
214 const int identity = mIdentity;
215
216 Mutex::Autolock _l(client.lock);
217 while ((condition()==false) &&
218 (stack.identity == identity) &&
219 (stack.status == NO_ERROR))
220 {
221 status_t err = client.cv.waitRelative(client.lock, TIMEOUT);
222 // handle errors and timeouts
223 if (CC_UNLIKELY(err != NO_ERROR)) {
224 if (err == TIMED_OUT) {
225 if (condition()) {
226 LOGE("waitForCondition(%s) timed out (identity=%d), "
227 "but condition is true! We recovered but it "
228 "shouldn't happen." , condition.name(), stack.identity);
229 break;
230 } else {
231 LOGW("waitForCondition(%s) timed out "
232 "(identity=%d, status=%d). "
233 "CPU may be pegged. trying again.", condition.name(),
234 stack.identity, stack.status);
235 }
236 } else {
237 LOGE("waitForCondition(%s) error (%s) ",
238 condition.name(), strerror(-err));
239 return err;
240 }
241 }
242 }
243 return (stack.identity != mIdentity) ? status_t(BAD_INDEX) : stack.status;
244}
Mathias Agopian9779b2212009-09-07 16:32:45 -0700245// ============================================================================
246// conditions and updates
247// ============================================================================
248
249SharedBufferClient::DequeueCondition::DequeueCondition(
250 SharedBufferClient* sbc) : ConditionBase(sbc) {
251}
Mathias Agopianf590f702010-04-27 16:41:19 -0700252bool SharedBufferClient::DequeueCondition::operator()() const {
Mathias Agopian9779b2212009-09-07 16:32:45 -0700253 return stack.available > 0;
254}
255
256SharedBufferClient::LockCondition::LockCondition(
257 SharedBufferClient* sbc, int buf) : ConditionBase(sbc), buf(buf) {
258}
Mathias Agopianf590f702010-04-27 16:41:19 -0700259bool SharedBufferClient::LockCondition::operator()() const {
Mathias Agopian3b91e13e2010-04-30 12:59:21 -0700260 // NOTE: if stack.head is messed up, we could crash the client
261 // or cause some drawing artifacts. This is okay, as long as it is
262 // limited to the client.
Mathias Agopian21956042011-01-18 15:51:30 -0800263 return (buf != stack.index[stack.head]);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700264}
265
Mathias Agopian9779b2212009-09-07 16:32:45 -0700266// ----------------------------------------------------------------------------
267
268SharedBufferClient::QueueUpdate::QueueUpdate(SharedBufferBase* sbb)
269 : UpdateBase(sbb) {
270}
271ssize_t SharedBufferClient::QueueUpdate::operator()() {
272 android_atomic_inc(&stack.queued);
273 return NO_ERROR;
274}
275
Mathias Agopianc9289fa2010-08-26 17:42:27 -0700276SharedBufferClient::DequeueUpdate::DequeueUpdate(SharedBufferBase* sbb)
277 : UpdateBase(sbb) {
278}
279ssize_t SharedBufferClient::DequeueUpdate::operator()() {
280 if (android_atomic_dec(&stack.available) == 0) {
281 LOGW("dequeue probably called from multiple threads!");
282 }
283 return NO_ERROR;
284}
285
Mathias Agopian8ddd2c72010-10-01 16:22:41 -0700286SharedBufferClient::CancelUpdate::CancelUpdate(SharedBufferBase* sbb,
287 int tail, int buf)
288 : UpdateBase(sbb), tail(tail), buf(buf) {
Mathias Agopian9779b2212009-09-07 16:32:45 -0700289}
Mathias Agopian8ddd2c72010-10-01 16:22:41 -0700290ssize_t SharedBufferClient::CancelUpdate::operator()() {
291 stack.index[tail] = buf;
Mathias Agopian9779b2212009-09-07 16:32:45 -0700292 android_atomic_inc(&stack.available);
293 return NO_ERROR;
294}
295
Mathias Agopian9779b2212009-09-07 16:32:45 -0700296SharedBufferServer::RetireUpdate::RetireUpdate(
297 SharedBufferBase* sbb, int numBuffers)
298 : UpdateBase(sbb), numBuffers(numBuffers) {
299}
300ssize_t SharedBufferServer::RetireUpdate::operator()() {
Mathias Agopian9779b2212009-09-07 16:32:45 -0700301 int32_t head = stack.head;
Mathias Agopian898c4c92010-05-18 17:06:55 -0700302 if (uint32_t(head) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopian3b91e13e2010-04-30 12:59:21 -0700303 return BAD_VALUE;
Mathias Agopian9779b2212009-09-07 16:32:45 -0700304
Mathias Agopian9779b2212009-09-07 16:32:45 -0700305 // Decrement the number of queued buffers
306 int32_t queued;
307 do {
308 queued = stack.queued;
309 if (queued == 0) {
310 return NOT_ENOUGH_DATA;
311 }
312 } while (android_atomic_cmpxchg(queued, queued-1, &stack.queued));
313
Mathias Agopian9779b2212009-09-07 16:32:45 -0700314 // lock the buffer before advancing head, which automatically unlocks
315 // the buffer we preventively locked upon entering this function
Mathias Agopianbe6c8fc2010-05-17 18:54:19 -0700316
Mathias Agopian59751db2010-05-07 15:58:44 -0700317 head = (head + 1) % numBuffers;
Mathias Agopian8ddd2c72010-10-01 16:22:41 -0700318 const int8_t headBuf = stack.index[head];
319 stack.headBuf = headBuf;
Mathias Agopian9779b2212009-09-07 16:32:45 -0700320
Mathias Agopian59751db2010-05-07 15:58:44 -0700321 // head is only modified here, so we don't need to use cmpxchg
Mathias Agopian9779b2212009-09-07 16:32:45 -0700322 android_atomic_write(head, &stack.head);
Mathias Agopian59751db2010-05-07 15:58:44 -0700323
Mathias Agopian9779b2212009-09-07 16:32:45 -0700324 // now that head has moved, we can increment the number of available buffers
325 android_atomic_inc(&stack.available);
326 return head;
327}
328
Mathias Agopian436c6272009-09-10 16:55:13 -0700329SharedBufferServer::StatusUpdate::StatusUpdate(
330 SharedBufferBase* sbb, status_t status)
331 : UpdateBase(sbb), status(status) {
332}
333
334ssize_t SharedBufferServer::StatusUpdate::operator()() {
335 android_atomic_write(status, &stack.status);
336 return NO_ERROR;
337}
338
Mathias Agopian9779b2212009-09-07 16:32:45 -0700339// ============================================================================
340
341SharedBufferClient::SharedBufferClient(SharedClient* sharedClient,
Mathias Agopian4961c952009-10-06 19:00:57 -0700342 int surface, int num, int32_t identity)
Mathias Agopian898c4c92010-05-18 17:06:55 -0700343 : SharedBufferBase(sharedClient, surface, identity),
Mathias Agopian8ddd2c72010-10-01 16:22:41 -0700344 mNumBuffers(num), tail(0)
Mathias Agopian9779b2212009-09-07 16:32:45 -0700345{
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700346 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianbd852712009-09-14 15:48:42 -0700347 tail = computeTail();
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700348 queued_head = stack.head;
Mathias Agopianbd852712009-09-14 15:48:42 -0700349}
350
Mathias Agopian898c4c92010-05-18 17:06:55 -0700351int32_t SharedBufferClient::computeTail() const
352{
353 SharedBufferStack& stack( *mSharedStack );
354 return (mNumBuffers + stack.head - stack.available + 1) % mNumBuffers;
355}
356
Mathias Agopian9779b2212009-09-07 16:32:45 -0700357ssize_t SharedBufferClient::dequeue()
358{
Mathias Agopian3e63f912009-09-11 19:18:20 -0700359 SharedBufferStack& stack( *mSharedStack );
360
Mathias Agopian6bb5eba2010-04-05 16:21:53 -0700361 if (stack.head == tail && stack.available == mNumBuffers) {
Mathias Agopian3e63f912009-09-11 19:18:20 -0700362 LOGW("dequeue: tail=%d, head=%d, avail=%d, queued=%d",
363 tail, stack.head, stack.available, stack.queued);
364 }
Mathias Agopian898c4c92010-05-18 17:06:55 -0700365
366 RWLock::AutoRLock _rd(mLock);
367
Mathias Agopianbcef9ac2009-09-17 01:35:28 -0700368 const nsecs_t dequeueTime = systemTime(SYSTEM_TIME_THREAD);
Mathias Agopian3e63f912009-09-11 19:18:20 -0700369
Mathias Agopian9779b2212009-09-07 16:32:45 -0700370 //LOGD("[%d] about to dequeue a buffer",
371 // mSharedStack->identity);
372 DequeueCondition condition(this);
373 status_t err = waitForCondition(condition);
374 if (err != NO_ERROR)
375 return ssize_t(err);
376
Mathias Agopianc9289fa2010-08-26 17:42:27 -0700377 DequeueUpdate update(this);
378 updateCondition( update );
Mathias Agopian9779b2212009-09-07 16:32:45 -0700379
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700380 int dequeued = stack.index[tail];
Mathias Agopian9779b2212009-09-07 16:32:45 -0700381 tail = ((tail+1 >= mNumBuffers) ? 0 : tail+1);
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700382 LOGD_IF(DEBUG_ATOMICS, "dequeued=%d, tail++=%d, %s",
Mathias Agopian9779b2212009-09-07 16:32:45 -0700383 dequeued, tail, dump("").string());
Mathias Agopian3e63f912009-09-11 19:18:20 -0700384
Mathias Agopianbcef9ac2009-09-17 01:35:28 -0700385 mDequeueTime[dequeued] = dequeueTime;
386
Mathias Agopian9779b2212009-09-07 16:32:45 -0700387 return dequeued;
388}
389
390status_t SharedBufferClient::undoDequeue(int buf)
391{
Mathias Agopian8ddd2c72010-10-01 16:22:41 -0700392 return cancel(buf);
393}
394
395status_t SharedBufferClient::cancel(int buf)
396{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700397 RWLock::AutoRLock _rd(mLock);
398
Mathias Agopian8ddd2c72010-10-01 16:22:41 -0700399 // calculate the new position of the tail index (essentially tail--)
400 int localTail = (tail + mNumBuffers - 1) % mNumBuffers;
401 CancelUpdate update(this, localTail, buf);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700402 status_t err = updateCondition( update );
Mathias Agopianbd852712009-09-14 15:48:42 -0700403 if (err == NO_ERROR) {
Mathias Agopian8ddd2c72010-10-01 16:22:41 -0700404 tail = localTail;
Mathias Agopianbd852712009-09-14 15:48:42 -0700405 }
Mathias Agopian9779b2212009-09-07 16:32:45 -0700406 return err;
407}
408
409status_t SharedBufferClient::lock(int buf)
410{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700411 RWLock::AutoRLock _rd(mLock);
412
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700413 SharedBufferStack& stack( *mSharedStack );
Mathias Agopian9779b2212009-09-07 16:32:45 -0700414 LockCondition condition(this, buf);
Mathias Agopianbcef9ac2009-09-17 01:35:28 -0700415 status_t err = waitForCondition(condition);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700416 return err;
417}
418
419status_t SharedBufferClient::queue(int buf)
420{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700421 RWLock::AutoRLock _rd(mLock);
422
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700423 SharedBufferStack& stack( *mSharedStack );
424
Mathias Agopianbe6c8fc2010-05-17 18:54:19 -0700425 queued_head = (queued_head + 1) % mNumBuffers;
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700426 stack.index[queued_head] = buf;
427
Mathias Agopian9779b2212009-09-07 16:32:45 -0700428 QueueUpdate update(this);
429 status_t err = updateCondition( update );
430 LOGD_IF(DEBUG_ATOMICS, "queued=%d, %s", buf, dump("").string());
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700431
Mathias Agopianbcef9ac2009-09-17 01:35:28 -0700432 const nsecs_t now = systemTime(SYSTEM_TIME_THREAD);
433 stack.stats.totalTime = ns2us(now - mDequeueTime[buf]);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700434 return err;
435}
436
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700437bool SharedBufferClient::needNewBuffer(int buf) const
Mathias Agopian9779b2212009-09-07 16:32:45 -0700438{
439 SharedBufferStack& stack( *mSharedStack );
Mathias Agopian57d89892010-05-21 14:51:33 -0700440 const uint32_t mask = 1<<(31-buf);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700441 return (android_atomic_and(~mask, &stack.reallocMask) & mask) != 0;
442}
443
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700444status_t SharedBufferClient::setCrop(int buf, const Rect& crop)
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700445{
446 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700447 return stack.setCrop(buf, crop);
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700448}
449
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700450status_t SharedBufferClient::setTransform(int buf, uint32_t transform)
451{
452 SharedBufferStack& stack( *mSharedStack );
453 return stack.setTransform(buf, uint8_t(transform));
454}
455
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700456status_t SharedBufferClient::setDirtyRegion(int buf, const Region& reg)
Mathias Agopian9779b2212009-09-07 16:32:45 -0700457{
458 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700459 return stack.setDirtyRegion(buf, reg);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700460}
461
Mathias Agopian898c4c92010-05-18 17:06:55 -0700462status_t SharedBufferClient::setBufferCount(
463 int bufferCount, const SetBufferCountCallback& ipc)
Mathias Agopian59751db2010-05-07 15:58:44 -0700464{
Mathias Agopianbe6c8fc2010-05-17 18:54:19 -0700465 SharedBufferStack& stack( *mSharedStack );
Mathias Agopian898c4c92010-05-18 17:06:55 -0700466 if (uint32_t(bufferCount) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopian59751db2010-05-07 15:58:44 -0700467 return BAD_VALUE;
Mathias Agopian898c4c92010-05-18 17:06:55 -0700468
Mathias Agopian25f0bda2010-05-21 14:19:50 -0700469 if (uint32_t(bufferCount) < SharedBufferStack::NUM_BUFFER_MIN)
470 return BAD_VALUE;
471
Mathias Agopian898c4c92010-05-18 17:06:55 -0700472 RWLock::AutoWLock _wr(mLock);
473
474 status_t err = ipc(bufferCount);
475 if (err == NO_ERROR) {
476 mNumBuffers = bufferCount;
477 queued_head = (stack.head + stack.queued) % mNumBuffers;
478 }
479 return err;
Mathias Agopian59751db2010-05-07 15:58:44 -0700480}
481
Mathias Agopian9779b2212009-09-07 16:32:45 -0700482// ----------------------------------------------------------------------------
483
484SharedBufferServer::SharedBufferServer(SharedClient* sharedClient,
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700485 int surface, int num, int32_t identity)
Mathias Agopian898c4c92010-05-18 17:06:55 -0700486 : SharedBufferBase(sharedClient, surface, identity),
487 mNumBuffers(num)
Mathias Agopian9779b2212009-09-07 16:32:45 -0700488{
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700489 mSharedStack->init(identity);
Mathias Agopian7623da42010-06-01 15:12:58 -0700490 mSharedStack->token = surface;
Mathias Agopian9779b2212009-09-07 16:32:45 -0700491 mSharedStack->head = num-1;
492 mSharedStack->available = num;
493 mSharedStack->queued = 0;
494 mSharedStack->reallocMask = 0;
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700495 memset(mSharedStack->buffers, 0, sizeof(mSharedStack->buffers));
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700496 for (int i=0 ; i<num ; i++) {
Mathias Agopian59751db2010-05-07 15:58:44 -0700497 mBufferList.add(i);
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700498 mSharedStack->index[i] = i;
499 }
Mathias Agopian9779b2212009-09-07 16:32:45 -0700500}
501
Mathias Agopian5e140102010-06-08 19:54:15 -0700502SharedBufferServer::~SharedBufferServer()
503{
504}
505
Mathias Agopian9779b2212009-09-07 16:32:45 -0700506ssize_t SharedBufferServer::retireAndLock()
507{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700508 RWLock::AutoRLock _l(mLock);
509
Mathias Agopian9779b2212009-09-07 16:32:45 -0700510 RetireUpdate update(this, mNumBuffers);
511 ssize_t buf = updateCondition( update );
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700512 if (buf >= 0) {
Mathias Agopian898c4c92010-05-18 17:06:55 -0700513 if (uint32_t(buf) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopian3b91e13e2010-04-30 12:59:21 -0700514 return BAD_VALUE;
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700515 SharedBufferStack& stack( *mSharedStack );
516 buf = stack.index[buf];
517 LOGD_IF(DEBUG_ATOMICS && buf>=0, "retire=%d, %s",
518 int(buf), dump("").string());
519 }
Mathias Agopian9779b2212009-09-07 16:32:45 -0700520 return buf;
521}
522
Mathias Agopian436c6272009-09-10 16:55:13 -0700523void SharedBufferServer::setStatus(status_t status)
524{
Mathias Agopian0c4cec72009-10-02 18:12:30 -0700525 if (status < NO_ERROR) {
526 StatusUpdate update(this, status);
527 updateCondition( update );
528 }
Mathias Agopian436c6272009-09-10 16:55:13 -0700529}
530
Mathias Agopian2be352a2010-05-21 17:24:35 -0700531status_t SharedBufferServer::reallocateAll()
Mathias Agopian9779b2212009-09-07 16:32:45 -0700532{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700533 RWLock::AutoRLock _l(mLock);
534
Mathias Agopian9779b2212009-09-07 16:32:45 -0700535 SharedBufferStack& stack( *mSharedStack );
Mathias Agopian57d89892010-05-21 14:51:33 -0700536 uint32_t mask = mBufferList.getMask();
Mathias Agopian2be352a2010-05-21 17:24:35 -0700537 android_atomic_or(mask, &stack.reallocMask);
538 return NO_ERROR;
539}
540
541status_t SharedBufferServer::reallocateAllExcept(int buffer)
542{
543 RWLock::AutoRLock _l(mLock);
544
545 SharedBufferStack& stack( *mSharedStack );
546 BufferList temp(mBufferList);
547 temp.remove(buffer);
548 uint32_t mask = temp.getMask();
549 android_atomic_or(mask, &stack.reallocMask);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700550 return NO_ERROR;
551}
552
Mathias Agopiane05f07d2009-10-07 16:44:10 -0700553int32_t SharedBufferServer::getQueuedCount() const
554{
555 SharedBufferStack& stack( *mSharedStack );
556 return stack.queued;
557}
558
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700559Region SharedBufferServer::getDirtyRegion(int buf) const
Mathias Agopian9779b2212009-09-07 16:32:45 -0700560{
561 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700562 return stack.getDirtyRegion(buf);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700563}
564
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700565Rect SharedBufferServer::getCrop(int buf) const
566{
567 SharedBufferStack& stack( *mSharedStack );
568 return stack.getCrop(buf);
569}
570
571uint32_t SharedBufferServer::getTransform(int buf) const
572{
573 SharedBufferStack& stack( *mSharedStack );
574 return stack.getTransform(buf);
575}
576
Mathias Agopian59751db2010-05-07 15:58:44 -0700577/*
Mathias Agopian59751db2010-05-07 15:58:44 -0700578 * NOTE: this is not thread-safe on the server-side, meaning
579 * 'head' cannot move during this operation. The client-side
580 * can safely operate an usual.
581 *
582 */
583status_t SharedBufferServer::resize(int newNumBuffers)
584{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700585 if (uint32_t(newNumBuffers) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopian59751db2010-05-07 15:58:44 -0700586 return BAD_VALUE;
587
Mathias Agopian898c4c92010-05-18 17:06:55 -0700588 RWLock::AutoWLock _l(mLock);
589
Mathias Agopian59751db2010-05-07 15:58:44 -0700590 // for now we're not supporting shrinking
591 const int numBuffers = mNumBuffers;
592 if (newNumBuffers < numBuffers)
593 return BAD_VALUE;
594
595 SharedBufferStack& stack( *mSharedStack );
596 const int extra = newNumBuffers - numBuffers;
597
598 // read the head, make sure it's valid
599 int32_t head = stack.head;
Mathias Agopian898c4c92010-05-18 17:06:55 -0700600 if (uint32_t(head) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopian59751db2010-05-07 15:58:44 -0700601 return BAD_VALUE;
602
603 int base = numBuffers;
604 int32_t avail = stack.available;
605 int tail = head - avail + 1;
Mathias Agopiancd30f4f2010-05-17 17:27:26 -0700606
Mathias Agopian59751db2010-05-07 15:58:44 -0700607 if (tail >= 0) {
608 int8_t* const index = const_cast<int8_t*>(stack.index);
609 const int nb = numBuffers - head;
610 memmove(&index[head + extra], &index[head], nb);
611 base = head;
612 // move head 'extra' ahead, this doesn't impact stack.index[head];
613 stack.head = head + extra;
614 }
615 stack.available += extra;
616
617 // fill the new free space with unused buffers
618 BufferList::const_iterator curr(mBufferList.free_begin());
619 for (int i=0 ; i<extra ; i++) {
Mathias Agopiancd30f4f2010-05-17 17:27:26 -0700620 stack.index[base+i] = *curr;
621 mBufferList.add(*curr);
622 ++curr;
Mathias Agopian59751db2010-05-07 15:58:44 -0700623 }
624
625 mNumBuffers = newNumBuffers;
626 return NO_ERROR;
627}
628
Mathias Agopianbcef9ac2009-09-17 01:35:28 -0700629SharedBufferStack::Statistics SharedBufferServer::getStats() const
630{
631 SharedBufferStack& stack( *mSharedStack );
632 return stack.stats;
633}
634
Mathias Agopian59751db2010-05-07 15:58:44 -0700635// ---------------------------------------------------------------------------
636status_t SharedBufferServer::BufferList::add(int value)
637{
638 if (uint32_t(value) >= mCapacity)
639 return BAD_VALUE;
640 uint32_t mask = 1<<(31-value);
641 if (mList & mask)
642 return ALREADY_EXISTS;
643 mList |= mask;
644 return NO_ERROR;
645}
646
647status_t SharedBufferServer::BufferList::remove(int value)
648{
649 if (uint32_t(value) >= mCapacity)
650 return BAD_VALUE;
651 uint32_t mask = 1<<(31-value);
652 if (!(mList & mask))
653 return NAME_NOT_FOUND;
654 mList &= ~mask;
655 return NO_ERROR;
656}
657
Mathias Agopianbcef9ac2009-09-17 01:35:28 -0700658
Mathias Agopian9779b2212009-09-07 16:32:45 -0700659// ---------------------------------------------------------------------------
660}; // namespace android