blob: 436d79320e9cac908b999117f3de1f3a23ba73ed [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
Mathias Agopiancbb288b2009-09-07 16:32:45 -070056// ----------------------------------------------------------------------------
57
58
59SharedBufferStack::SharedBufferStack()
Mathias Agopiancbb288b2009-09-07 16:32:45 -070060{
61}
62
Mathias Agopian48d819a2009-09-10 19:41:18 -070063void SharedBufferStack::init(int32_t i)
64{
65 inUse = -1;
66 status = NO_ERROR;
67 identity = i;
68}
69
Mathias Agopiancbb288b2009-09-07 16:32:45 -070070status_t SharedBufferStack::setDirtyRegion(int buffer, const Region& dirty)
71{
72 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
73 return BAD_INDEX;
74
75 // in the current implementation we only send a single rectangle
76 const Rect bounds(dirty.getBounds());
77 FlatRegion& reg(dirtyRegion[buffer]);
78 reg.count = 1;
79 reg.rects[0] = uint16_t(bounds.left);
80 reg.rects[1] = uint16_t(bounds.top);
81 reg.rects[2] = uint16_t(bounds.right);
82 reg.rects[3] = uint16_t(bounds.bottom);
83 return NO_ERROR;
84}
85
86Region SharedBufferStack::getDirtyRegion(int buffer) const
87{
88 Region res;
89 if (uint32_t(buffer) >= NUM_BUFFER_MAX)
90 return res;
91
92 const FlatRegion& reg(dirtyRegion[buffer]);
93 res.set(Rect(reg.rects[0], reg.rects[1], reg.rects[2], reg.rects[3]));
94 return res;
95}
96
97// ----------------------------------------------------------------------------
98
99SharedBufferBase::SharedBufferBase(SharedClient* sharedClient,
100 int surface, int num)
101 : mSharedClient(sharedClient),
102 mSharedStack(sharedClient->surfaces + surface),
103 mNumBuffers(num)
104{
105}
106
107SharedBufferBase::~SharedBufferBase()
108{
109}
110
111uint32_t SharedBufferBase::getIdentity()
112{
113 SharedBufferStack& stack( *mSharedStack );
114 return stack.identity;
115}
116
117size_t SharedBufferBase::getFrontBuffer() const
118{
119 SharedBufferStack& stack( *mSharedStack );
120 return size_t( stack.head );
121}
122
123String8 SharedBufferBase::dump(char const* prefix) const
124{
125 const size_t SIZE = 1024;
126 char buffer[SIZE];
127 String8 result;
128 SharedBufferStack& stack( *mSharedStack );
129 snprintf(buffer, SIZE,
130 "%s[ head=%2d, available=%2d, queued=%2d ] "
131 "reallocMask=%08x, inUse=%2d, identity=%d, status=%d\n",
132 prefix, stack.head, stack.available, stack.queued,
133 stack.reallocMask, stack.inUse, stack.identity, stack.status);
134 result.append(buffer);
135 return result;
136}
137
138
139// ============================================================================
140// conditions and updates
141// ============================================================================
142
143SharedBufferClient::DequeueCondition::DequeueCondition(
144 SharedBufferClient* sbc) : ConditionBase(sbc) {
145}
146bool SharedBufferClient::DequeueCondition::operator()() {
147 return stack.available > 0;
148}
149
150SharedBufferClient::LockCondition::LockCondition(
151 SharedBufferClient* sbc, int buf) : ConditionBase(sbc), buf(buf) {
152}
153bool SharedBufferClient::LockCondition::operator()() {
154 return (buf != stack.head ||
155 (stack.queued > 0 && stack.inUse != buf));
156}
157
158SharedBufferServer::ReallocateCondition::ReallocateCondition(
159 SharedBufferBase* sbb, int buf) : ConditionBase(sbb), buf(buf) {
160}
161bool SharedBufferServer::ReallocateCondition::operator()() {
162 // TODO: we should also check that buf has been dequeued
163 return (buf != stack.head);
164}
165
166// ----------------------------------------------------------------------------
167
168SharedBufferClient::QueueUpdate::QueueUpdate(SharedBufferBase* sbb)
169 : UpdateBase(sbb) {
170}
171ssize_t SharedBufferClient::QueueUpdate::operator()() {
172 android_atomic_inc(&stack.queued);
173 return NO_ERROR;
174}
175
176SharedBufferClient::UndoDequeueUpdate::UndoDequeueUpdate(SharedBufferBase* sbb)
177 : UpdateBase(sbb) {
178}
179ssize_t SharedBufferClient::UndoDequeueUpdate::operator()() {
180 android_atomic_inc(&stack.available);
181 return NO_ERROR;
182}
183
184SharedBufferServer::UnlockUpdate::UnlockUpdate(
185 SharedBufferBase* sbb, int lockedBuffer)
186 : UpdateBase(sbb), lockedBuffer(lockedBuffer) {
187}
188ssize_t SharedBufferServer::UnlockUpdate::operator()() {
189 if (stack.inUse != lockedBuffer) {
190 LOGE("unlocking %d, but currently locked buffer is %d",
191 lockedBuffer, stack.inUse);
192 return BAD_VALUE;
193 }
194 android_atomic_write(-1, &stack.inUse);
195 return NO_ERROR;
196}
197
198SharedBufferServer::RetireUpdate::RetireUpdate(
199 SharedBufferBase* sbb, int numBuffers)
200 : UpdateBase(sbb), numBuffers(numBuffers) {
201}
202ssize_t SharedBufferServer::RetireUpdate::operator()() {
203 // head is only written in this function, which is single-thread.
204 int32_t head = stack.head;
205
206 // Preventively lock the current buffer before updating queued.
207 android_atomic_write(head, &stack.inUse);
208
209 // Decrement the number of queued buffers
210 int32_t queued;
211 do {
212 queued = stack.queued;
213 if (queued == 0) {
214 return NOT_ENOUGH_DATA;
215 }
216 } while (android_atomic_cmpxchg(queued, queued-1, &stack.queued));
217
218 // update the head pointer
219 head = ((head+1 >= numBuffers) ? 0 : head+1);
220
221 // lock the buffer before advancing head, which automatically unlocks
222 // the buffer we preventively locked upon entering this function
223 android_atomic_write(head, &stack.inUse);
224
225 // advance head
226 android_atomic_write(head, &stack.head);
227
228 // now that head has moved, we can increment the number of available buffers
229 android_atomic_inc(&stack.available);
230 return head;
231}
232
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700233SharedBufferServer::StatusUpdate::StatusUpdate(
234 SharedBufferBase* sbb, status_t status)
235 : UpdateBase(sbb), status(status) {
236}
237
238ssize_t SharedBufferServer::StatusUpdate::operator()() {
239 android_atomic_write(status, &stack.status);
240 return NO_ERROR;
241}
242
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700243// ============================================================================
244
245SharedBufferClient::SharedBufferClient(SharedClient* sharedClient,
246 int surface, int num)
247 : SharedBufferBase(sharedClient, surface, num), tail(0)
248{
Mathias Agopian40d57992009-09-11 19:18:20 -0700249 SharedBufferStack& stack( *mSharedStack );
250 int32_t avail;
251 int32_t head;
252 // we need to make sure we read available and head coherently,
253 // w.r.t RetireUpdate.
254 do {
255 avail = stack.available;
256 head = stack.head;
257 } while (stack.available != avail);
258 tail = head - avail + 1;
259 if (tail < 0) {
260 tail += num;
261 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700262}
263
264ssize_t SharedBufferClient::dequeue()
265{
Mathias Agopian40d57992009-09-11 19:18:20 -0700266 SharedBufferStack& stack( *mSharedStack );
267
268 if (stack.head == tail && stack.available == 2) {
269 LOGW("dequeue: tail=%d, head=%d, avail=%d, queued=%d",
270 tail, stack.head, stack.available, stack.queued);
271 }
272
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700273 //LOGD("[%d] about to dequeue a buffer",
274 // mSharedStack->identity);
275 DequeueCondition condition(this);
276 status_t err = waitForCondition(condition);
277 if (err != NO_ERROR)
278 return ssize_t(err);
279
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700280 // NOTE: 'stack.available' is part of the conditions, however
281 // decrementing it, never changes any conditions, so we don't need
282 // to do this as part of an update.
283 if (android_atomic_dec(&stack.available) == 0) {
284 LOGW("dequeue probably called from multiple threads!");
285 }
286
287 int dequeued = tail;
288 tail = ((tail+1 >= mNumBuffers) ? 0 : tail+1);
289 LOGD_IF(DEBUG_ATOMICS, "dequeued=%d, tail=%d, %s",
290 dequeued, tail, dump("").string());
Mathias Agopian40d57992009-09-11 19:18:20 -0700291
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700292 return dequeued;
293}
294
295status_t SharedBufferClient::undoDequeue(int buf)
296{
297 UndoDequeueUpdate update(this);
298 status_t err = updateCondition( update );
299 return err;
300}
301
302status_t SharedBufferClient::lock(int buf)
303{
304 LockCondition condition(this, buf);
305 status_t err = waitForCondition(condition);
306 return err;
307}
308
309status_t SharedBufferClient::queue(int buf)
310{
311 QueueUpdate update(this);
312 status_t err = updateCondition( update );
313 LOGD_IF(DEBUG_ATOMICS, "queued=%d, %s", buf, dump("").string());
314 return err;
315}
316
317bool SharedBufferClient::needNewBuffer(int buffer) const
318{
319 SharedBufferStack& stack( *mSharedStack );
320 const uint32_t mask = 1<<buffer;
321 return (android_atomic_and(~mask, &stack.reallocMask) & mask) != 0;
322}
323
324status_t SharedBufferClient::setDirtyRegion(int buffer, const Region& reg)
325{
326 SharedBufferStack& stack( *mSharedStack );
327 return stack.setDirtyRegion(buffer, reg);
328}
329
330// ----------------------------------------------------------------------------
331
332SharedBufferServer::SharedBufferServer(SharedClient* sharedClient,
Mathias Agopian48d819a2009-09-10 19:41:18 -0700333 int surface, int num, int32_t identity)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700334 : SharedBufferBase(sharedClient, surface, num)
335{
Mathias Agopian48d819a2009-09-10 19:41:18 -0700336 mSharedStack->init(identity);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700337 mSharedStack->head = num-1;
338 mSharedStack->available = num;
339 mSharedStack->queued = 0;
340 mSharedStack->reallocMask = 0;
341 memset(mSharedStack->dirtyRegion, 0, sizeof(mSharedStack->dirtyRegion));
342}
343
344ssize_t SharedBufferServer::retireAndLock()
345{
346 RetireUpdate update(this, mNumBuffers);
347 ssize_t buf = updateCondition( update );
Mathias Agopian40d57992009-09-11 19:18:20 -0700348 LOGD_IF(DEBUG_ATOMICS && buf>=0, "retire=%d, %s", int(buf), dump("").string());
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700349 return buf;
350}
351
352status_t SharedBufferServer::unlock(int buffer)
353{
354 UnlockUpdate update(this, buffer);
355 status_t err = updateCondition( update );
356 return err;
357}
358
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700359void SharedBufferServer::setStatus(status_t status)
360{
361 StatusUpdate update(this, status);
362 updateCondition( update );
363}
364
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700365status_t SharedBufferServer::reallocate()
366{
367 SharedBufferStack& stack( *mSharedStack );
368 uint32_t mask = (1<<mNumBuffers)-1;
369 android_atomic_or(mask, &stack.reallocMask);
370 return NO_ERROR;
371}
372
373status_t SharedBufferServer::assertReallocate(int buffer)
374{
375 ReallocateCondition condition(this, buffer);
376 status_t err = waitForCondition(condition);
377 return err;
378}
379
380Region SharedBufferServer::getDirtyRegion(int buffer) const
381{
382 SharedBufferStack& stack( *mSharedStack );
383 return stack.getDirtyRegion(buffer);
384}
385
386// ---------------------------------------------------------------------------
387}; // namespace android