blob: 9ad4349da2993d25602b915268d2e1f0db1e17bc [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 }
Mathias Agopian86f73292009-09-17 01:35:28 -0700279
280 const nsecs_t dequeueTime = systemTime(SYSTEM_TIME_THREAD);
Mathias Agopian40d57992009-09-11 19:18:20 -0700281
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700282 //LOGD("[%d] about to dequeue a buffer",
283 // mSharedStack->identity);
284 DequeueCondition condition(this);
285 status_t err = waitForCondition(condition);
286 if (err != NO_ERROR)
287 return ssize_t(err);
288
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700289 // NOTE: 'stack.available' is part of the conditions, however
290 // decrementing it, never changes any conditions, so we don't need
291 // to do this as part of an update.
292 if (android_atomic_dec(&stack.available) == 0) {
293 LOGW("dequeue probably called from multiple threads!");
294 }
295
296 int dequeued = tail;
297 tail = ((tail+1 >= mNumBuffers) ? 0 : tail+1);
298 LOGD_IF(DEBUG_ATOMICS, "dequeued=%d, tail=%d, %s",
299 dequeued, tail, dump("").string());
Mathias Agopian40d57992009-09-11 19:18:20 -0700300
Mathias Agopian86f73292009-09-17 01:35:28 -0700301 mDequeueTime[dequeued] = dequeueTime;
302
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700303 return dequeued;
304}
305
306status_t SharedBufferClient::undoDequeue(int buf)
307{
308 UndoDequeueUpdate update(this);
309 status_t err = updateCondition( update );
Mathias Agopianc7d56012009-09-14 15:48:42 -0700310 if (err == NO_ERROR) {
311 tail = computeTail();
312 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700313 return err;
314}
315
316status_t SharedBufferClient::lock(int buf)
317{
318 LockCondition condition(this, buf);
Mathias Agopian86f73292009-09-17 01:35:28 -0700319 status_t err = waitForCondition(condition);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700320 return err;
321}
322
323status_t SharedBufferClient::queue(int buf)
324{
325 QueueUpdate update(this);
326 status_t err = updateCondition( update );
327 LOGD_IF(DEBUG_ATOMICS, "queued=%d, %s", buf, dump("").string());
Mathias Agopian86f73292009-09-17 01:35:28 -0700328 SharedBufferStack& stack( *mSharedStack );
329 const nsecs_t now = systemTime(SYSTEM_TIME_THREAD);
330 stack.stats.totalTime = ns2us(now - mDequeueTime[buf]);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700331 return err;
332}
333
334bool SharedBufferClient::needNewBuffer(int buffer) const
335{
336 SharedBufferStack& stack( *mSharedStack );
337 const uint32_t mask = 1<<buffer;
338 return (android_atomic_and(~mask, &stack.reallocMask) & mask) != 0;
339}
340
341status_t SharedBufferClient::setDirtyRegion(int buffer, const Region& reg)
342{
343 SharedBufferStack& stack( *mSharedStack );
344 return stack.setDirtyRegion(buffer, reg);
345}
346
347// ----------------------------------------------------------------------------
348
349SharedBufferServer::SharedBufferServer(SharedClient* sharedClient,
Mathias Agopian48d819a2009-09-10 19:41:18 -0700350 int surface, int num, int32_t identity)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700351 : SharedBufferBase(sharedClient, surface, num)
352{
Mathias Agopian48d819a2009-09-10 19:41:18 -0700353 mSharedStack->init(identity);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700354 mSharedStack->head = num-1;
355 mSharedStack->available = num;
356 mSharedStack->queued = 0;
357 mSharedStack->reallocMask = 0;
358 memset(mSharedStack->dirtyRegion, 0, sizeof(mSharedStack->dirtyRegion));
359}
360
361ssize_t SharedBufferServer::retireAndLock()
362{
363 RetireUpdate update(this, mNumBuffers);
364 ssize_t buf = updateCondition( update );
Mathias Agopian40d57992009-09-11 19:18:20 -0700365 LOGD_IF(DEBUG_ATOMICS && buf>=0, "retire=%d, %s", int(buf), dump("").string());
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700366 return buf;
367}
368
369status_t SharedBufferServer::unlock(int buffer)
370{
371 UnlockUpdate update(this, buffer);
372 status_t err = updateCondition( update );
373 return err;
374}
375
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700376void SharedBufferServer::setStatus(status_t status)
377{
378 StatusUpdate update(this, status);
379 updateCondition( update );
380}
381
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700382status_t SharedBufferServer::reallocate()
383{
384 SharedBufferStack& stack( *mSharedStack );
385 uint32_t mask = (1<<mNumBuffers)-1;
386 android_atomic_or(mask, &stack.reallocMask);
387 return NO_ERROR;
388}
389
390status_t SharedBufferServer::assertReallocate(int buffer)
391{
392 ReallocateCondition condition(this, buffer);
393 status_t err = waitForCondition(condition);
394 return err;
395}
396
397Region SharedBufferServer::getDirtyRegion(int buffer) const
398{
399 SharedBufferStack& stack( *mSharedStack );
400 return stack.getDirtyRegion(buffer);
401}
402
Mathias Agopian86f73292009-09-17 01:35:28 -0700403SharedBufferStack::Statistics SharedBufferServer::getStats() const
404{
405 SharedBufferStack& stack( *mSharedStack );
406 return stack.stats;
407}
408
409
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700410// ---------------------------------------------------------------------------
411}; // namespace android