blob: 7505d530e0c5b3172bea5652a8ad982ef36ba9bd [file] [log] [blame]
Mathias Agopian9779b222009-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 Agopian9779b222009-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 Agopian9779b222009-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 Agopian9779b222009-09-07 16:32:45 -070048 return BAD_INDEX;
49 return surfaces[i].status;
50}
51
Mathias Agopian9779b222009-09-07 16:32:45 -070052// ----------------------------------------------------------------------------
53
54
55SharedBufferStack::SharedBufferStack()
Mathias Agopian9779b222009-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 Agopian9779b222009-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 Agopian9779b222009-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 Agopian9779b222009-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 Agopian9779b222009-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 Agopian9779b222009-09-07 16:32:45 -0700171 : mSharedClient(sharedClient),
172 mSharedStack(sharedClient->surfaces + surface),
Mathias Agopian898c4c92010-05-18 17:06:55 -0700173 mIdentity(identity)
Mathias Agopian9779b222009-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 Agopian9779b222009-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 Agopian1d4549a2011-01-19 18:02:20 -0800201 "reallocMask=%08x, identity=%d, status=%d",
Mathias Agopian898c4c92010-05-18 17:06:55 -0700202 prefix, stack.head, stack.available, stack.queued,
Mathias Agopian1d4549a2011-01-19 18:02:20 -0800203 stack.reallocMask, stack.identity, stack.status);
Mathias Agopian9779b222009-09-07 16:32:45 -0700204 result.append(buffer);
Mathias Agopian898c4c92010-05-18 17:06:55 -0700205 result.append("\n");
Mathias Agopian9779b222009-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 Agopian9779b222009-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 Agopian9779b222009-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 Agopian3b91e132010-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 Agopian2e102a02011-01-18 15:51:30 -0800263 return (buf != stack.index[stack.head]);
Mathias Agopian9779b222009-09-07 16:32:45 -0700264}
265
Jamie Gennis6c925d02010-11-02 11:51:32 -0700266SharedBufferServer::BuffersAvailableCondition::BuffersAvailableCondition(
267 SharedBufferServer* sbs, int numBuffers) : ConditionBase(sbs),
268 mNumBuffers(numBuffers) {
269}
270bool SharedBufferServer::BuffersAvailableCondition::operator()() const {
271 return stack.available == mNumBuffers;
272}
273
Mathias Agopian9779b222009-09-07 16:32:45 -0700274// ----------------------------------------------------------------------------
275
276SharedBufferClient::QueueUpdate::QueueUpdate(SharedBufferBase* sbb)
277 : UpdateBase(sbb) {
278}
279ssize_t SharedBufferClient::QueueUpdate::operator()() {
280 android_atomic_inc(&stack.queued);
281 return NO_ERROR;
282}
283
Mathias Agopianc9289fa2010-08-26 17:42:27 -0700284SharedBufferClient::DequeueUpdate::DequeueUpdate(SharedBufferBase* sbb)
285 : UpdateBase(sbb) {
286}
287ssize_t SharedBufferClient::DequeueUpdate::operator()() {
288 if (android_atomic_dec(&stack.available) == 0) {
289 LOGW("dequeue probably called from multiple threads!");
290 }
291 return NO_ERROR;
292}
293
Mathias Agopian8ddd2c72010-10-01 16:22:41 -0700294SharedBufferClient::CancelUpdate::CancelUpdate(SharedBufferBase* sbb,
295 int tail, int buf)
296 : UpdateBase(sbb), tail(tail), buf(buf) {
Mathias Agopian9779b222009-09-07 16:32:45 -0700297}
Mathias Agopian8ddd2c72010-10-01 16:22:41 -0700298ssize_t SharedBufferClient::CancelUpdate::operator()() {
299 stack.index[tail] = buf;
Mathias Agopian9779b222009-09-07 16:32:45 -0700300 android_atomic_inc(&stack.available);
301 return NO_ERROR;
302}
303
Mathias Agopian9779b222009-09-07 16:32:45 -0700304SharedBufferServer::RetireUpdate::RetireUpdate(
305 SharedBufferBase* sbb, int numBuffers)
306 : UpdateBase(sbb), numBuffers(numBuffers) {
307}
308ssize_t SharedBufferServer::RetireUpdate::operator()() {
Mathias Agopian9779b222009-09-07 16:32:45 -0700309 int32_t head = stack.head;
Mathias Agopian898c4c92010-05-18 17:06:55 -0700310 if (uint32_t(head) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopian3b91e132010-04-30 12:59:21 -0700311 return BAD_VALUE;
Mathias Agopian9779b222009-09-07 16:32:45 -0700312
Mathias Agopian9779b222009-09-07 16:32:45 -0700313 // Decrement the number of queued buffers
314 int32_t queued;
315 do {
316 queued = stack.queued;
317 if (queued == 0) {
318 return NOT_ENOUGH_DATA;
319 }
320 } while (android_atomic_cmpxchg(queued, queued-1, &stack.queued));
321
Mathias Agopian9779b222009-09-07 16:32:45 -0700322 // lock the buffer before advancing head, which automatically unlocks
323 // the buffer we preventively locked upon entering this function
Mathias Agopianbe6c8fc2010-05-17 18:54:19 -0700324
Mathias Agopian59751db2010-05-07 15:58:44 -0700325 head = (head + 1) % numBuffers;
Mathias Agopian8ddd2c72010-10-01 16:22:41 -0700326 const int8_t headBuf = stack.index[head];
327 stack.headBuf = headBuf;
Mathias Agopian9779b222009-09-07 16:32:45 -0700328
Mathias Agopian59751db2010-05-07 15:58:44 -0700329 // head is only modified here, so we don't need to use cmpxchg
Mathias Agopian9779b222009-09-07 16:32:45 -0700330 android_atomic_write(head, &stack.head);
Mathias Agopian59751db2010-05-07 15:58:44 -0700331
Mathias Agopian9779b222009-09-07 16:32:45 -0700332 // now that head has moved, we can increment the number of available buffers
333 android_atomic_inc(&stack.available);
334 return head;
335}
336
Mathias Agopian436c6272009-09-10 16:55:13 -0700337SharedBufferServer::StatusUpdate::StatusUpdate(
338 SharedBufferBase* sbb, status_t status)
339 : UpdateBase(sbb), status(status) {
340}
341
342ssize_t SharedBufferServer::StatusUpdate::operator()() {
343 android_atomic_write(status, &stack.status);
344 return NO_ERROR;
345}
346
Mathias Agopian9779b222009-09-07 16:32:45 -0700347// ============================================================================
348
349SharedBufferClient::SharedBufferClient(SharedClient* sharedClient,
Mathias Agopian4961c952009-10-06 19:00:57 -0700350 int surface, int num, int32_t identity)
Mathias Agopian898c4c92010-05-18 17:06:55 -0700351 : SharedBufferBase(sharedClient, surface, identity),
Mathias Agopian8ddd2c72010-10-01 16:22:41 -0700352 mNumBuffers(num), tail(0)
Mathias Agopian9779b222009-09-07 16:32:45 -0700353{
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700354 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianbd852712009-09-14 15:48:42 -0700355 tail = computeTail();
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700356 queued_head = stack.head;
Mathias Agopianbd852712009-09-14 15:48:42 -0700357}
358
Mathias Agopian898c4c92010-05-18 17:06:55 -0700359int32_t SharedBufferClient::computeTail() const
360{
361 SharedBufferStack& stack( *mSharedStack );
362 return (mNumBuffers + stack.head - stack.available + 1) % mNumBuffers;
363}
364
Mathias Agopian9779b222009-09-07 16:32:45 -0700365ssize_t SharedBufferClient::dequeue()
366{
Mathias Agopian3e63f912009-09-11 19:18:20 -0700367 SharedBufferStack& stack( *mSharedStack );
368
Mathias Agopian898c4c92010-05-18 17:06:55 -0700369 RWLock::AutoRLock _rd(mLock);
370
Mathias Agopianbcef9ac2009-09-17 01:35:28 -0700371 const nsecs_t dequeueTime = systemTime(SYSTEM_TIME_THREAD);
Mathias Agopian3e63f912009-09-11 19:18:20 -0700372
Mathias Agopian9779b222009-09-07 16:32:45 -0700373 //LOGD("[%d] about to dequeue a buffer",
374 // mSharedStack->identity);
375 DequeueCondition condition(this);
376 status_t err = waitForCondition(condition);
377 if (err != NO_ERROR)
378 return ssize_t(err);
379
Mathias Agopianc9289fa2010-08-26 17:42:27 -0700380 DequeueUpdate update(this);
381 updateCondition( update );
Mathias Agopian9779b222009-09-07 16:32:45 -0700382
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700383 int dequeued = stack.index[tail];
Mathias Agopian9779b222009-09-07 16:32:45 -0700384 tail = ((tail+1 >= mNumBuffers) ? 0 : tail+1);
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700385 LOGD_IF(DEBUG_ATOMICS, "dequeued=%d, tail++=%d, %s",
Mathias Agopian9779b222009-09-07 16:32:45 -0700386 dequeued, tail, dump("").string());
Mathias Agopian3e63f912009-09-11 19:18:20 -0700387
Mathias Agopianbcef9ac2009-09-17 01:35:28 -0700388 mDequeueTime[dequeued] = dequeueTime;
389
Mathias Agopian9779b222009-09-07 16:32:45 -0700390 return dequeued;
391}
392
393status_t SharedBufferClient::undoDequeue(int buf)
394{
Mathias Agopian8ddd2c72010-10-01 16:22:41 -0700395 return cancel(buf);
396}
397
398status_t SharedBufferClient::cancel(int buf)
399{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700400 RWLock::AutoRLock _rd(mLock);
401
Mathias Agopian8ddd2c72010-10-01 16:22:41 -0700402 // calculate the new position of the tail index (essentially tail--)
403 int localTail = (tail + mNumBuffers - 1) % mNumBuffers;
404 CancelUpdate update(this, localTail, buf);
Mathias Agopian9779b222009-09-07 16:32:45 -0700405 status_t err = updateCondition( update );
Mathias Agopianbd852712009-09-14 15:48:42 -0700406 if (err == NO_ERROR) {
Mathias Agopian8ddd2c72010-10-01 16:22:41 -0700407 tail = localTail;
Mathias Agopianbd852712009-09-14 15:48:42 -0700408 }
Mathias Agopian9779b222009-09-07 16:32:45 -0700409 return err;
410}
411
412status_t SharedBufferClient::lock(int buf)
413{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700414 RWLock::AutoRLock _rd(mLock);
415
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700416 SharedBufferStack& stack( *mSharedStack );
Mathias Agopian9779b222009-09-07 16:32:45 -0700417 LockCondition condition(this, buf);
Mathias Agopianbcef9ac2009-09-17 01:35:28 -0700418 status_t err = waitForCondition(condition);
Mathias Agopian9779b222009-09-07 16:32:45 -0700419 return err;
420}
421
422status_t SharedBufferClient::queue(int buf)
423{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700424 RWLock::AutoRLock _rd(mLock);
425
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700426 SharedBufferStack& stack( *mSharedStack );
427
Mathias Agopianbe6c8fc2010-05-17 18:54:19 -0700428 queued_head = (queued_head + 1) % mNumBuffers;
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700429 stack.index[queued_head] = buf;
430
Mathias Agopian9779b222009-09-07 16:32:45 -0700431 QueueUpdate update(this);
432 status_t err = updateCondition( update );
433 LOGD_IF(DEBUG_ATOMICS, "queued=%d, %s", buf, dump("").string());
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700434
Mathias Agopianbcef9ac2009-09-17 01:35:28 -0700435 const nsecs_t now = systemTime(SYSTEM_TIME_THREAD);
436 stack.stats.totalTime = ns2us(now - mDequeueTime[buf]);
Jamie Gennis6c925d02010-11-02 11:51:32 -0700437
Mathias Agopian9779b222009-09-07 16:32:45 -0700438 return err;
439}
440
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700441bool SharedBufferClient::needNewBuffer(int buf) const
Mathias Agopian9779b222009-09-07 16:32:45 -0700442{
443 SharedBufferStack& stack( *mSharedStack );
Mathias Agopian57d89892010-05-21 14:51:33 -0700444 const uint32_t mask = 1<<(31-buf);
Mathias Agopian9779b222009-09-07 16:32:45 -0700445 return (android_atomic_and(~mask, &stack.reallocMask) & mask) != 0;
446}
447
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700448status_t SharedBufferClient::setCrop(int buf, const Rect& crop)
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700449{
450 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700451 return stack.setCrop(buf, crop);
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700452}
453
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700454status_t SharedBufferClient::setTransform(int buf, uint32_t transform)
455{
456 SharedBufferStack& stack( *mSharedStack );
457 return stack.setTransform(buf, uint8_t(transform));
458}
459
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700460status_t SharedBufferClient::setDirtyRegion(int buf, const Region& reg)
Mathias Agopian9779b222009-09-07 16:32:45 -0700461{
462 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700463 return stack.setDirtyRegion(buf, reg);
Mathias Agopian9779b222009-09-07 16:32:45 -0700464}
465
Mathias Agopian898c4c92010-05-18 17:06:55 -0700466status_t SharedBufferClient::setBufferCount(
467 int bufferCount, const SetBufferCountCallback& ipc)
Mathias Agopian59751db2010-05-07 15:58:44 -0700468{
Mathias Agopianbe6c8fc2010-05-17 18:54:19 -0700469 SharedBufferStack& stack( *mSharedStack );
Mathias Agopian898c4c92010-05-18 17:06:55 -0700470 if (uint32_t(bufferCount) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopian59751db2010-05-07 15:58:44 -0700471 return BAD_VALUE;
Mathias Agopian898c4c92010-05-18 17:06:55 -0700472
Mathias Agopian25f0bda2010-05-21 14:19:50 -0700473 if (uint32_t(bufferCount) < SharedBufferStack::NUM_BUFFER_MIN)
474 return BAD_VALUE;
475
Mathias Agopian898c4c92010-05-18 17:06:55 -0700476 RWLock::AutoWLock _wr(mLock);
477
478 status_t err = ipc(bufferCount);
479 if (err == NO_ERROR) {
480 mNumBuffers = bufferCount;
481 queued_head = (stack.head + stack.queued) % mNumBuffers;
Jamie Gennis6c925d02010-11-02 11:51:32 -0700482 tail = computeTail();
Mathias Agopian898c4c92010-05-18 17:06:55 -0700483 }
484 return err;
Mathias Agopian59751db2010-05-07 15:58:44 -0700485}
486
Mathias Agopian9779b222009-09-07 16:32:45 -0700487// ----------------------------------------------------------------------------
488
489SharedBufferServer::SharedBufferServer(SharedClient* sharedClient,
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700490 int surface, int num, int32_t identity)
Mathias Agopian898c4c92010-05-18 17:06:55 -0700491 : SharedBufferBase(sharedClient, surface, identity),
492 mNumBuffers(num)
Mathias Agopian9779b222009-09-07 16:32:45 -0700493{
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700494 mSharedStack->init(identity);
Mathias Agopian7623da42010-06-01 15:12:58 -0700495 mSharedStack->token = surface;
Mathias Agopian9779b222009-09-07 16:32:45 -0700496 mSharedStack->head = num-1;
497 mSharedStack->available = num;
498 mSharedStack->queued = 0;
499 mSharedStack->reallocMask = 0;
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700500 memset(mSharedStack->buffers, 0, sizeof(mSharedStack->buffers));
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700501 for (int i=0 ; i<num ; i++) {
Mathias Agopian59751db2010-05-07 15:58:44 -0700502 mBufferList.add(i);
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700503 mSharedStack->index[i] = i;
504 }
Mathias Agopian9779b222009-09-07 16:32:45 -0700505}
506
Mathias Agopian5e140102010-06-08 19:54:15 -0700507SharedBufferServer::~SharedBufferServer()
508{
509}
510
Mathias Agopian9779b222009-09-07 16:32:45 -0700511ssize_t SharedBufferServer::retireAndLock()
512{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700513 RWLock::AutoRLock _l(mLock);
514
Mathias Agopian9779b222009-09-07 16:32:45 -0700515 RetireUpdate update(this, mNumBuffers);
516 ssize_t buf = updateCondition( update );
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700517 if (buf >= 0) {
Mathias Agopian898c4c92010-05-18 17:06:55 -0700518 if (uint32_t(buf) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopian3b91e132010-04-30 12:59:21 -0700519 return BAD_VALUE;
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700520 SharedBufferStack& stack( *mSharedStack );
521 buf = stack.index[buf];
522 LOGD_IF(DEBUG_ATOMICS && buf>=0, "retire=%d, %s",
523 int(buf), dump("").string());
524 }
Mathias Agopian9779b222009-09-07 16:32:45 -0700525 return buf;
526}
527
Mathias Agopian436c6272009-09-10 16:55:13 -0700528void SharedBufferServer::setStatus(status_t status)
529{
Mathias Agopian0c4cec72009-10-02 18:12:30 -0700530 if (status < NO_ERROR) {
531 StatusUpdate update(this, status);
532 updateCondition( update );
533 }
Mathias Agopian436c6272009-09-10 16:55:13 -0700534}
535
Mathias Agopian2be352a2010-05-21 17:24:35 -0700536status_t SharedBufferServer::reallocateAll()
Mathias Agopian9779b222009-09-07 16:32:45 -0700537{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700538 RWLock::AutoRLock _l(mLock);
539
Mathias Agopian9779b222009-09-07 16:32:45 -0700540 SharedBufferStack& stack( *mSharedStack );
Mathias Agopian57d89892010-05-21 14:51:33 -0700541 uint32_t mask = mBufferList.getMask();
Mathias Agopian2be352a2010-05-21 17:24:35 -0700542 android_atomic_or(mask, &stack.reallocMask);
543 return NO_ERROR;
544}
545
546status_t SharedBufferServer::reallocateAllExcept(int buffer)
547{
548 RWLock::AutoRLock _l(mLock);
549
550 SharedBufferStack& stack( *mSharedStack );
551 BufferList temp(mBufferList);
552 temp.remove(buffer);
553 uint32_t mask = temp.getMask();
554 android_atomic_or(mask, &stack.reallocMask);
Mathias Agopian9779b222009-09-07 16:32:45 -0700555 return NO_ERROR;
556}
557
Mathias Agopiane05f07d2009-10-07 16:44:10 -0700558int32_t SharedBufferServer::getQueuedCount() const
559{
560 SharedBufferStack& stack( *mSharedStack );
561 return stack.queued;
562}
563
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700564Region SharedBufferServer::getDirtyRegion(int buf) const
Mathias Agopian9779b222009-09-07 16:32:45 -0700565{
566 SharedBufferStack& stack( *mSharedStack );
Mathias Agopianbfe7f0b12010-04-27 21:08:20 -0700567 return stack.getDirtyRegion(buf);
Mathias Agopian9779b222009-09-07 16:32:45 -0700568}
569
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700570Rect SharedBufferServer::getCrop(int buf) const
571{
572 SharedBufferStack& stack( *mSharedStack );
573 return stack.getCrop(buf);
574}
575
576uint32_t SharedBufferServer::getTransform(int buf) const
577{
578 SharedBufferStack& stack( *mSharedStack );
579 return stack.getTransform(buf);
580}
581
Mathias Agopian59751db2010-05-07 15:58:44 -0700582/*
Mathias Agopian59751db2010-05-07 15:58:44 -0700583 * NOTE: this is not thread-safe on the server-side, meaning
584 * 'head' cannot move during this operation. The client-side
585 * can safely operate an usual.
586 *
587 */
588status_t SharedBufferServer::resize(int newNumBuffers)
589{
Jamie Gennis6c925d02010-11-02 11:51:32 -0700590 if ((unsigned int)(newNumBuffers) < SharedBufferStack::NUM_BUFFER_MIN ||
591 (unsigned int)(newNumBuffers) > SharedBufferStack::NUM_BUFFER_MAX) {
Mathias Agopian59751db2010-05-07 15:58:44 -0700592 return BAD_VALUE;
Jamie Gennis6c925d02010-11-02 11:51:32 -0700593 }
Mathias Agopian59751db2010-05-07 15:58:44 -0700594
Mathias Agopian898c4c92010-05-18 17:06:55 -0700595 RWLock::AutoWLock _l(mLock);
596
Jamie Gennis6c925d02010-11-02 11:51:32 -0700597 if (newNumBuffers < mNumBuffers) {
598 return shrink(newNumBuffers);
599 } else {
600 return grow(newNumBuffers);
601 }
602}
Mathias Agopian59751db2010-05-07 15:58:44 -0700603
Jamie Gennis6c925d02010-11-02 11:51:32 -0700604status_t SharedBufferServer::grow(int newNumBuffers)
605{
Mathias Agopian59751db2010-05-07 15:58:44 -0700606 SharedBufferStack& stack( *mSharedStack );
Jamie Gennis6c925d02010-11-02 11:51:32 -0700607 const int numBuffers = mNumBuffers;
Mathias Agopian59751db2010-05-07 15:58:44 -0700608 const int extra = newNumBuffers - numBuffers;
609
610 // read the head, make sure it's valid
611 int32_t head = stack.head;
Mathias Agopian898c4c92010-05-18 17:06:55 -0700612 if (uint32_t(head) >= SharedBufferStack::NUM_BUFFER_MAX)
Mathias Agopian59751db2010-05-07 15:58:44 -0700613 return BAD_VALUE;
614
615 int base = numBuffers;
616 int32_t avail = stack.available;
617 int tail = head - avail + 1;
Mathias Agopiancd30f4f2010-05-17 17:27:26 -0700618
Mathias Agopian59751db2010-05-07 15:58:44 -0700619 if (tail >= 0) {
620 int8_t* const index = const_cast<int8_t*>(stack.index);
621 const int nb = numBuffers - head;
622 memmove(&index[head + extra], &index[head], nb);
623 base = head;
624 // move head 'extra' ahead, this doesn't impact stack.index[head];
625 stack.head = head + extra;
626 }
627 stack.available += extra;
628
629 // fill the new free space with unused buffers
630 BufferList::const_iterator curr(mBufferList.free_begin());
631 for (int i=0 ; i<extra ; i++) {
Mathias Agopiancd30f4f2010-05-17 17:27:26 -0700632 stack.index[base+i] = *curr;
633 mBufferList.add(*curr);
634 ++curr;
Mathias Agopian59751db2010-05-07 15:58:44 -0700635 }
636
637 mNumBuffers = newNumBuffers;
638 return NO_ERROR;
639}
640
Jamie Gennis6c925d02010-11-02 11:51:32 -0700641status_t SharedBufferServer::shrink(int newNumBuffers)
642{
643 SharedBufferStack& stack( *mSharedStack );
644
645 // Shrinking is only supported if there are no buffers currently dequeued.
646 int32_t avail = stack.available;
647 int32_t queued = stack.queued;
648 if (avail + queued != mNumBuffers) {
649 return INVALID_OPERATION;
650 }
651
652 // Wait for any queued buffers to be displayed.
653 BuffersAvailableCondition condition(this, mNumBuffers);
654 status_t err = waitForCondition(condition);
655 if (err < 0) {
656 return err;
657 }
658
659 // Reset head to index 0 and make it refer to buffer 0. The same renaming
660 // (head -> 0) is done in the BufferManager.
661 int32_t head = stack.head;
662 int8_t* index = const_cast<int8_t*>(stack.index);
663 for (int8_t i = 0; i < newNumBuffers; i++) {
664 index[i] = i;
665 }
666 stack.head = 0;
667 stack.headBuf = 0;
668
Jamie Gennis6c925d02010-11-02 11:51:32 -0700669 // Free the buffers from the end of the list that are no longer needed.
670 for (int i = newNumBuffers; i < mNumBuffers; i++) {
671 mBufferList.remove(i);
672 }
673
674 // Tell the client to reallocate all the buffers.
675 reallocateAll();
676
677 mNumBuffers = newNumBuffers;
678 stack.available = newNumBuffers;
679
680 return NO_ERROR;
681}
682
Mathias Agopianbcef9ac2009-09-17 01:35:28 -0700683SharedBufferStack::Statistics SharedBufferServer::getStats() const
684{
685 SharedBufferStack& stack( *mSharedStack );
686 return stack.stats;
687}
688
Mathias Agopian59751db2010-05-07 15:58:44 -0700689// ---------------------------------------------------------------------------
690status_t SharedBufferServer::BufferList::add(int value)
691{
692 if (uint32_t(value) >= mCapacity)
693 return BAD_VALUE;
694 uint32_t mask = 1<<(31-value);
695 if (mList & mask)
696 return ALREADY_EXISTS;
697 mList |= mask;
698 return NO_ERROR;
699}
700
701status_t SharedBufferServer::BufferList::remove(int value)
702{
703 if (uint32_t(value) >= mCapacity)
704 return BAD_VALUE;
705 uint32_t mask = 1<<(31-value);
706 if (!(mList & mask))
707 return NAME_NOT_FOUND;
708 mList &= ~mask;
709 return NO_ERROR;
710}
711
Mathias Agopianbcef9ac2009-09-17 01:35:28 -0700712
Mathias Agopian9779b222009-09-07 16:32:45 -0700713// ---------------------------------------------------------------------------
714}; // namespace android