blob: 5995af5de7555c168427dbae89cb31de0e366497 [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()
67 : inUse(-1), identity(-1), status(NO_ERROR)
68{
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
234// ============================================================================
235
236SharedBufferClient::SharedBufferClient(SharedClient* sharedClient,
237 int surface, int num)
238 : SharedBufferBase(sharedClient, surface, num), tail(0)
239{
240}
241
242ssize_t SharedBufferClient::dequeue()
243{
244 //LOGD("[%d] about to dequeue a buffer",
245 // mSharedStack->identity);
246 DequeueCondition condition(this);
247 status_t err = waitForCondition(condition);
248 if (err != NO_ERROR)
249 return ssize_t(err);
250
251
252 SharedBufferStack& stack( *mSharedStack );
253 // NOTE: 'stack.available' is part of the conditions, however
254 // decrementing it, never changes any conditions, so we don't need
255 // to do this as part of an update.
256 if (android_atomic_dec(&stack.available) == 0) {
257 LOGW("dequeue probably called from multiple threads!");
258 }
259
260 int dequeued = tail;
261 tail = ((tail+1 >= mNumBuffers) ? 0 : tail+1);
262 LOGD_IF(DEBUG_ATOMICS, "dequeued=%d, tail=%d, %s",
263 dequeued, tail, dump("").string());
264 return dequeued;
265}
266
267status_t SharedBufferClient::undoDequeue(int buf)
268{
269 UndoDequeueUpdate update(this);
270 status_t err = updateCondition( update );
271 return err;
272}
273
274status_t SharedBufferClient::lock(int buf)
275{
276 LockCondition condition(this, buf);
277 status_t err = waitForCondition(condition);
278 return err;
279}
280
281status_t SharedBufferClient::queue(int buf)
282{
283 QueueUpdate update(this);
284 status_t err = updateCondition( update );
285 LOGD_IF(DEBUG_ATOMICS, "queued=%d, %s", buf, dump("").string());
286 return err;
287}
288
289bool SharedBufferClient::needNewBuffer(int buffer) const
290{
291 SharedBufferStack& stack( *mSharedStack );
292 const uint32_t mask = 1<<buffer;
293 return (android_atomic_and(~mask, &stack.reallocMask) & mask) != 0;
294}
295
296status_t SharedBufferClient::setDirtyRegion(int buffer, const Region& reg)
297{
298 SharedBufferStack& stack( *mSharedStack );
299 return stack.setDirtyRegion(buffer, reg);
300}
301
302// ----------------------------------------------------------------------------
303
304SharedBufferServer::SharedBufferServer(SharedClient* sharedClient,
305 int surface, int num)
306 : SharedBufferBase(sharedClient, surface, num)
307{
308 mSharedStack->head = num-1;
309 mSharedStack->available = num;
310 mSharedStack->queued = 0;
311 mSharedStack->reallocMask = 0;
312 memset(mSharedStack->dirtyRegion, 0, sizeof(mSharedStack->dirtyRegion));
313}
314
315ssize_t SharedBufferServer::retireAndLock()
316{
317 RetireUpdate update(this, mNumBuffers);
318 ssize_t buf = updateCondition( update );
319 LOGD_IF(DEBUG_ATOMICS, "retire=%d, %s", int(buf), dump("").string());
320 return buf;
321}
322
323status_t SharedBufferServer::unlock(int buffer)
324{
325 UnlockUpdate update(this, buffer);
326 status_t err = updateCondition( update );
327 return err;
328}
329
330status_t SharedBufferServer::reallocate()
331{
332 SharedBufferStack& stack( *mSharedStack );
333 uint32_t mask = (1<<mNumBuffers)-1;
334 android_atomic_or(mask, &stack.reallocMask);
335 return NO_ERROR;
336}
337
338status_t SharedBufferServer::assertReallocate(int buffer)
339{
340 ReallocateCondition condition(this, buffer);
341 status_t err = waitForCondition(condition);
342 return err;
343}
344
345Region SharedBufferServer::getDirtyRegion(int buffer) const
346{
347 SharedBufferStack& stack( *mSharedStack );
348 return stack.getDirtyRegion(buffer);
349}
350
351// ---------------------------------------------------------------------------
352}; // namespace android