blob: 7789a3f8646fa3e4e35d2aa4135c697c85cc965a [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 Agopianc7d56012009-09-14 15:48:42 -0700249 tail = computeTail();
250}
251
252int32_t SharedBufferClient::computeTail() const
253{
Mathias Agopian40d57992009-09-11 19:18:20 -0700254 SharedBufferStack& stack( *mSharedStack );
Mathias Agopian40d57992009-09-11 19:18:20 -0700255 // we need to make sure we read available and head coherently,
256 // w.r.t RetireUpdate.
Mathias Agopianc7d56012009-09-14 15:48:42 -0700257 int32_t newTail;
258 int32_t avail;
259 int32_t head;
Mathias Agopian40d57992009-09-11 19:18:20 -0700260 do {
261 avail = stack.available;
262 head = stack.head;
263 } while (stack.available != avail);
Mathias Agopianc7d56012009-09-14 15:48:42 -0700264 newTail = head - avail + 1;
265 if (newTail < 0) {
266 newTail += mNumBuffers;
Mathias Agopian40d57992009-09-11 19:18:20 -0700267 }
Mathias Agopianc7d56012009-09-14 15:48:42 -0700268 return newTail;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700269}
270
271ssize_t SharedBufferClient::dequeue()
272{
Mathias Agopian40d57992009-09-11 19:18:20 -0700273 SharedBufferStack& stack( *mSharedStack );
274
275 if (stack.head == tail && stack.available == 2) {
276 LOGW("dequeue: tail=%d, head=%d, avail=%d, queued=%d",
277 tail, stack.head, stack.available, stack.queued);
278 }
279
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700280 //LOGD("[%d] about to dequeue a buffer",
281 // mSharedStack->identity);
282 DequeueCondition condition(this);
283 status_t err = waitForCondition(condition);
284 if (err != NO_ERROR)
285 return ssize_t(err);
286
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700287 // NOTE: 'stack.available' is part of the conditions, however
288 // decrementing it, never changes any conditions, so we don't need
289 // to do this as part of an update.
290 if (android_atomic_dec(&stack.available) == 0) {
291 LOGW("dequeue probably called from multiple threads!");
292 }
293
294 int dequeued = tail;
295 tail = ((tail+1 >= mNumBuffers) ? 0 : tail+1);
296 LOGD_IF(DEBUG_ATOMICS, "dequeued=%d, tail=%d, %s",
297 dequeued, tail, dump("").string());
Mathias Agopian40d57992009-09-11 19:18:20 -0700298
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700299 return dequeued;
300}
301
302status_t SharedBufferClient::undoDequeue(int buf)
303{
304 UndoDequeueUpdate update(this);
305 status_t err = updateCondition( update );
Mathias Agopianc7d56012009-09-14 15:48:42 -0700306 if (err == NO_ERROR) {
307 tail = computeTail();
308 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700309 return err;
310}
311
312status_t SharedBufferClient::lock(int buf)
313{
314 LockCondition condition(this, buf);
315 status_t err = waitForCondition(condition);
316 return err;
317}
318
319status_t SharedBufferClient::queue(int buf)
320{
321 QueueUpdate update(this);
322 status_t err = updateCondition( update );
323 LOGD_IF(DEBUG_ATOMICS, "queued=%d, %s", buf, dump("").string());
324 return err;
325}
326
327bool SharedBufferClient::needNewBuffer(int buffer) const
328{
329 SharedBufferStack& stack( *mSharedStack );
330 const uint32_t mask = 1<<buffer;
331 return (android_atomic_and(~mask, &stack.reallocMask) & mask) != 0;
332}
333
334status_t SharedBufferClient::setDirtyRegion(int buffer, const Region& reg)
335{
336 SharedBufferStack& stack( *mSharedStack );
337 return stack.setDirtyRegion(buffer, reg);
338}
339
340// ----------------------------------------------------------------------------
341
342SharedBufferServer::SharedBufferServer(SharedClient* sharedClient,
Mathias Agopian48d819a2009-09-10 19:41:18 -0700343 int surface, int num, int32_t identity)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700344 : SharedBufferBase(sharedClient, surface, num)
345{
Mathias Agopian48d819a2009-09-10 19:41:18 -0700346 mSharedStack->init(identity);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700347 mSharedStack->head = num-1;
348 mSharedStack->available = num;
349 mSharedStack->queued = 0;
350 mSharedStack->reallocMask = 0;
351 memset(mSharedStack->dirtyRegion, 0, sizeof(mSharedStack->dirtyRegion));
352}
353
354ssize_t SharedBufferServer::retireAndLock()
355{
356 RetireUpdate update(this, mNumBuffers);
357 ssize_t buf = updateCondition( update );
Mathias Agopian40d57992009-09-11 19:18:20 -0700358 LOGD_IF(DEBUG_ATOMICS && buf>=0, "retire=%d, %s", int(buf), dump("").string());
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700359 return buf;
360}
361
362status_t SharedBufferServer::unlock(int buffer)
363{
364 UnlockUpdate update(this, buffer);
365 status_t err = updateCondition( update );
366 return err;
367}
368
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700369void SharedBufferServer::setStatus(status_t status)
370{
371 StatusUpdate update(this, status);
372 updateCondition( update );
373}
374
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700375status_t SharedBufferServer::reallocate()
376{
377 SharedBufferStack& stack( *mSharedStack );
378 uint32_t mask = (1<<mNumBuffers)-1;
379 android_atomic_or(mask, &stack.reallocMask);
380 return NO_ERROR;
381}
382
383status_t SharedBufferServer::assertReallocate(int buffer)
384{
385 ReallocateCondition condition(this, buffer);
386 status_t err = waitForCondition(condition);
387 return err;
388}
389
390Region SharedBufferServer::getDirtyRegion(int buffer) const
391{
392 SharedBufferStack& stack( *mSharedStack );
393 return stack.getDirtyRegion(buffer);
394}
395
396// ---------------------------------------------------------------------------
397}; // namespace android