blob: 73fb582f9c3e646430b29b0a1cf6c5e4cbc636fe [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
26#include <private/ui/SharedBufferStack.h>
27
28#include <ui/Rect.h>
29#include <ui/Region.h>
30
31#define DEBUG_ATOMICS 0
32
33namespace android {
34// ----------------------------------------------------------------------------
35
36SharedClient::SharedClient()
37 : lock(Mutex::SHARED)
38{
39}
40
41SharedClient::~SharedClient() {
42}
43
44
45// these functions are used by the clients
46status_t SharedClient::validate(size_t i) const {
47 if (uint32_t(i) >= uint32_t(NUM_LAYERS_MAX))
48 return BAD_INDEX;
49 return surfaces[i].status;
50}
51
52uint32_t SharedClient::getIdentity(size_t token) const {
53 return uint32_t(surfaces[token].identity);
54}
55
56status_t SharedClient::setIdentity(size_t token, uint32_t identity) {
57 if (token >= NUM_LAYERS_MAX)
58 return BAD_INDEX;
59 surfaces[token].identity = identity;
60 return NO_ERROR;
61}
62
63// ----------------------------------------------------------------------------
64
65
66SharedBufferStack::SharedBufferStack()
Mathias Agopianb58b5d72009-09-10 16:55:13 -070067 : inUse(-1), status(NO_ERROR), identity(-1)
Mathias Agopiancbb288b2009-09-07 16:32:45 -070068{
69}
70
71status_t SharedBufferStack::setDirtyRegion(int buffer, const Region& dirty)
72{
73 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
74 return BAD_INDEX;
75
76 // in the current implementation we only send a single rectangle
77 const Rect bounds(dirty.getBounds());
78 FlatRegion& reg(dirtyRegion[buffer]);
79 reg.count = 1;
80 reg.rects[0] = uint16_t(bounds.left);
81 reg.rects[1] = uint16_t(bounds.top);
82 reg.rects[2] = uint16_t(bounds.right);
83 reg.rects[3] = uint16_t(bounds.bottom);
84 return NO_ERROR;
85}
86
87Region SharedBufferStack::getDirtyRegion(int buffer) const
88{
89 Region res;
90 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
91 return res;
92
93 const FlatRegion& reg(dirtyRegion[buffer]);
94 res.set(Rect(reg.rects[0], reg.rects[1], reg.rects[2], reg.rects[3]));
95 return res;
96}
97
98// ----------------------------------------------------------------------------
99
100SharedBufferBase::SharedBufferBase(SharedClient* sharedClient,
101 int surface, int num)
102 : mSharedClient(sharedClient),
103 mSharedStack(sharedClient->surfaces + surface),
104 mNumBuffers(num)
105{
106}
107
108SharedBufferBase::~SharedBufferBase()
109{
110}
111
112uint32_t SharedBufferBase::getIdentity()
113{
114 SharedBufferStack& stack( *mSharedStack );
115 return stack.identity;
116}
117
118size_t SharedBufferBase::getFrontBuffer() const
119{
120 SharedBufferStack& stack( *mSharedStack );
121 return size_t( stack.head );
122}
123
124String8 SharedBufferBase::dump(char const* prefix) const
125{
126 const size_t SIZE = 1024;
127 char buffer[SIZE];
128 String8 result;
129 SharedBufferStack& stack( *mSharedStack );
130 snprintf(buffer, SIZE,
131 "%s[ head=%2d, available=%2d, queued=%2d ] "
132 "reallocMask=%08x, inUse=%2d, identity=%d, status=%d\n",
133 prefix, stack.head, stack.available, stack.queued,
134 stack.reallocMask, stack.inUse, stack.identity, stack.status);
135 result.append(buffer);
136 return result;
137}
138
139
140// ============================================================================
141// conditions and updates
142// ============================================================================
143
144SharedBufferClient::DequeueCondition::DequeueCondition(
145 SharedBufferClient* sbc) : ConditionBase(sbc) {
146}
147bool SharedBufferClient::DequeueCondition::operator()() {
148 return stack.available > 0;
149}
150
151SharedBufferClient::LockCondition::LockCondition(
152 SharedBufferClient* sbc, int buf) : ConditionBase(sbc), buf(buf) {
153}
154bool SharedBufferClient::LockCondition::operator()() {
155 return (buf != stack.head ||
156 (stack.queued > 0 && stack.inUse != buf));
157}
158
159SharedBufferServer::ReallocateCondition::ReallocateCondition(
160 SharedBufferBase* sbb, int buf) : ConditionBase(sbb), buf(buf) {
161}
162bool SharedBufferServer::ReallocateCondition::operator()() {
163 // TODO: we should also check that buf has been dequeued
164 return (buf != stack.head);
165}
166
167// ----------------------------------------------------------------------------
168
169SharedBufferClient::QueueUpdate::QueueUpdate(SharedBufferBase* sbb)
170 : UpdateBase(sbb) {
171}
172ssize_t SharedBufferClient::QueueUpdate::operator()() {
173 android_atomic_inc(&stack.queued);
174 return NO_ERROR;
175}
176
177SharedBufferClient::UndoDequeueUpdate::UndoDequeueUpdate(SharedBufferBase* sbb)
178 : UpdateBase(sbb) {
179}
180ssize_t SharedBufferClient::UndoDequeueUpdate::operator()() {
181 android_atomic_inc(&stack.available);
182 return NO_ERROR;
183}
184
185SharedBufferServer::UnlockUpdate::UnlockUpdate(
186 SharedBufferBase* sbb, int lockedBuffer)
187 : UpdateBase(sbb), lockedBuffer(lockedBuffer) {
188}
189ssize_t SharedBufferServer::UnlockUpdate::operator()() {
190 if (stack.inUse != lockedBuffer) {
191 LOGE("unlocking %d, but currently locked buffer is %d",
192 lockedBuffer, stack.inUse);
193 return BAD_VALUE;
194 }
195 android_atomic_write(-1, &stack.inUse);
196 return NO_ERROR;
197}
198
199SharedBufferServer::RetireUpdate::RetireUpdate(
200 SharedBufferBase* sbb, int numBuffers)
201 : UpdateBase(sbb), numBuffers(numBuffers) {
202}
203ssize_t SharedBufferServer::RetireUpdate::operator()() {
204 // head is only written in this function, which is single-thread.
205 int32_t head = stack.head;
206
207 // Preventively lock the current buffer before updating queued.
208 android_atomic_write(head, &stack.inUse);
209
210 // Decrement the number of queued buffers
211 int32_t queued;
212 do {
213 queued = stack.queued;
214 if (queued == 0) {
215 return NOT_ENOUGH_DATA;
216 }
217 } while (android_atomic_cmpxchg(queued, queued-1, &stack.queued));
218
219 // update the head pointer
220 head = ((head+1 >= numBuffers) ? 0 : head+1);
221
222 // lock the buffer before advancing head, which automatically unlocks
223 // the buffer we preventively locked upon entering this function
224 android_atomic_write(head, &stack.inUse);
225
226 // advance head
227 android_atomic_write(head, &stack.head);
228
229 // now that head has moved, we can increment the number of available buffers
230 android_atomic_inc(&stack.available);
231 return head;
232}
233
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700234SharedBufferServer::StatusUpdate::StatusUpdate(
235 SharedBufferBase* sbb, status_t status)
236 : UpdateBase(sbb), status(status) {
237}
238
239ssize_t SharedBufferServer::StatusUpdate::operator()() {
240 android_atomic_write(status, &stack.status);
241 return NO_ERROR;
242}
243
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700244// ============================================================================
245
246SharedBufferClient::SharedBufferClient(SharedClient* sharedClient,
247 int surface, int num)
248 : SharedBufferBase(sharedClient, surface, num), tail(0)
249{
250}
251
252ssize_t SharedBufferClient::dequeue()
253{
254 //LOGD("[%d] about to dequeue a buffer",
255 // mSharedStack->identity);
256 DequeueCondition condition(this);
257 status_t err = waitForCondition(condition);
258 if (err != NO_ERROR)
259 return ssize_t(err);
260
261
262 SharedBufferStack& stack( *mSharedStack );
263 // NOTE: 'stack.available' is part of the conditions, however
264 // decrementing it, never changes any conditions, so we don't need
265 // to do this as part of an update.
266 if (android_atomic_dec(&stack.available) == 0) {
267 LOGW("dequeue probably called from multiple threads!");
268 }
269
270 int dequeued = tail;
271 tail = ((tail+1 >= mNumBuffers) ? 0 : tail+1);
272 LOGD_IF(DEBUG_ATOMICS, "dequeued=%d, tail=%d, %s",
273 dequeued, tail, dump("").string());
274 return dequeued;
275}
276
277status_t SharedBufferClient::undoDequeue(int buf)
278{
279 UndoDequeueUpdate update(this);
280 status_t err = updateCondition( update );
281 return err;
282}
283
284status_t SharedBufferClient::lock(int buf)
285{
286 LockCondition condition(this, buf);
287 status_t err = waitForCondition(condition);
288 return err;
289}
290
291status_t SharedBufferClient::queue(int buf)
292{
293 QueueUpdate update(this);
294 status_t err = updateCondition( update );
295 LOGD_IF(DEBUG_ATOMICS, "queued=%d, %s", buf, dump("").string());
296 return err;
297}
298
299bool SharedBufferClient::needNewBuffer(int buffer) const
300{
301 SharedBufferStack& stack( *mSharedStack );
302 const uint32_t mask = 1<<buffer;
303 return (android_atomic_and(~mask, &stack.reallocMask) & mask) != 0;
304}
305
306status_t SharedBufferClient::setDirtyRegion(int buffer, const Region& reg)
307{
308 SharedBufferStack& stack( *mSharedStack );
309 return stack.setDirtyRegion(buffer, reg);
310}
311
312// ----------------------------------------------------------------------------
313
314SharedBufferServer::SharedBufferServer(SharedClient* sharedClient,
315 int surface, int num)
316 : SharedBufferBase(sharedClient, surface, num)
317{
318 mSharedStack->head = num-1;
319 mSharedStack->available = num;
320 mSharedStack->queued = 0;
321 mSharedStack->reallocMask = 0;
322 memset(mSharedStack->dirtyRegion, 0, sizeof(mSharedStack->dirtyRegion));
323}
324
325ssize_t SharedBufferServer::retireAndLock()
326{
327 RetireUpdate update(this, mNumBuffers);
328 ssize_t buf = updateCondition( update );
329 LOGD_IF(DEBUG_ATOMICS, "retire=%d, %s", int(buf), dump("").string());
330 return buf;
331}
332
333status_t SharedBufferServer::unlock(int buffer)
334{
335 UnlockUpdate update(this, buffer);
336 status_t err = updateCondition( update );
337 return err;
338}
339
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700340void SharedBufferServer::setStatus(status_t status)
341{
342 StatusUpdate update(this, status);
343 updateCondition( update );
344}
345
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700346status_t SharedBufferServer::reallocate()
347{
348 SharedBufferStack& stack( *mSharedStack );
349 uint32_t mask = (1<<mNumBuffers)-1;
350 android_atomic_or(mask, &stack.reallocMask);
351 return NO_ERROR;
352}
353
354status_t SharedBufferServer::assertReallocate(int buffer)
355{
356 ReallocateCondition condition(this, buffer);
357 status_t err = waitForCondition(condition);
358 return err;
359}
360
361Region SharedBufferServer::getDirtyRegion(int buffer) const
362{
363 SharedBufferStack& stack( *mSharedStack );
364 return stack.getDirtyRegion(buffer);
365}
366
367// ---------------------------------------------------------------------------
368}; // namespace android