SharedBufferStack.cpp revision cd30f4f849bb215509bd2645726048271b5db01e
1/*
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/surfaceflinger/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), cv(Condition::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
56// ----------------------------------------------------------------------------
57
58
59SharedBufferStack::SharedBufferStack()
60{
61}
62
63void SharedBufferStack::init(int32_t i)
64{
65    inUse = -1;
66    status = NO_ERROR;
67    identity = i;
68}
69
70status_t SharedBufferStack::setCrop(int buffer, const Rect& crop)
71{
72    if (uint32_t(buffer) >= NUM_BUFFER_MAX)
73        return BAD_INDEX;
74
75    buffers[buffer].crop.l = uint16_t(crop.left);
76    buffers[buffer].crop.t = uint16_t(crop.top);
77    buffers[buffer].crop.r = uint16_t(crop.right);
78    buffers[buffer].crop.b = uint16_t(crop.bottom);
79    return NO_ERROR;
80}
81
82status_t SharedBufferStack::setDirtyRegion(int buffer, const Region& dirty)
83{
84    if (uint32_t(buffer) >= NUM_BUFFER_MAX)
85        return BAD_INDEX;
86
87    FlatRegion& reg(buffers[buffer].dirtyRegion);
88    if (dirty.isEmpty()) {
89        reg.count = 0;
90        return NO_ERROR;
91    }
92
93    size_t count;
94    Rect const* r = dirty.getArray(&count);
95    if (count > FlatRegion::NUM_RECT_MAX) {
96        const Rect bounds(dirty.getBounds());
97        reg.count = 1;
98        reg.rects[0].l = uint16_t(bounds.left);
99        reg.rects[0].t = uint16_t(bounds.top);
100        reg.rects[0].r = uint16_t(bounds.right);
101        reg.rects[0].b = uint16_t(bounds.bottom);
102    } else {
103        reg.count = count;
104        for (size_t i=0 ; i<count ; i++) {
105            reg.rects[i].l = uint16_t(r[i].left);
106            reg.rects[i].t = uint16_t(r[i].top);
107            reg.rects[i].r = uint16_t(r[i].right);
108            reg.rects[i].b = uint16_t(r[i].bottom);
109        }
110    }
111    return NO_ERROR;
112}
113
114Region SharedBufferStack::getDirtyRegion(int buffer) const
115{
116    Region res;
117    if (uint32_t(buffer) >= NUM_BUFFER_MAX)
118        return res;
119
120    const FlatRegion& reg(buffers[buffer].dirtyRegion);
121    if (reg.count > FlatRegion::NUM_RECT_MAX)
122        return res;
123
124    if (reg.count == 1) {
125        const Rect r(
126                reg.rects[0].l,
127                reg.rects[0].t,
128                reg.rects[0].r,
129                reg.rects[0].b);
130        res.set(r);
131    } else {
132        for (size_t i=0 ; i<reg.count ; i++) {
133            const Rect r(
134                    reg.rects[i].l,
135                    reg.rects[i].t,
136                    reg.rects[i].r,
137                    reg.rects[i].b);
138            res.orSelf(r);
139        }
140    }
141    return res;
142}
143
144// ----------------------------------------------------------------------------
145
146SharedBufferBase::SharedBufferBase(SharedClient* sharedClient,
147        int surface, int num, int32_t identity)
148    : mSharedClient(sharedClient),
149      mSharedStack(sharedClient->surfaces + surface),
150      mNumBuffers(num), mIdentity(identity)
151{
152}
153
154SharedBufferBase::~SharedBufferBase()
155{
156}
157
158uint32_t SharedBufferBase::getIdentity()
159{
160    SharedBufferStack& stack( *mSharedStack );
161    return stack.identity;
162}
163
164status_t SharedBufferBase::getStatus() const
165{
166    SharedBufferStack& stack( *mSharedStack );
167    return stack.status;
168}
169
170size_t SharedBufferBase::getFrontBuffer() const
171{
172    SharedBufferStack& stack( *mSharedStack );
173    return size_t( stack.head );
174}
175
176String8 SharedBufferBase::dump(char const* prefix) const
177{
178    const size_t SIZE = 1024;
179    char buffer[SIZE];
180    String8 result;
181    SharedBufferStack& stack( *mSharedStack );
182    int tail = computeTail();
183    snprintf(buffer, SIZE,
184            "%s[ head=%2d, available=%2d, queued=%2d, tail=%2d ] "
185            "reallocMask=%08x, inUse=%2d, identity=%d, status=%d\n",
186            prefix, stack.head, stack.available, stack.queued, tail,
187            stack.reallocMask, stack.inUse, stack.identity, stack.status);
188    result.append(buffer);
189    return result;
190}
191
192int32_t SharedBufferBase::computeTail() const
193{
194    SharedBufferStack& stack( *mSharedStack );
195    return (mNumBuffers + stack.head - stack.available + 1) % mNumBuffers;
196}
197
198status_t SharedBufferBase::waitForCondition(const ConditionBase& condition)
199{
200    const SharedBufferStack& stack( *mSharedStack );
201    SharedClient& client( *mSharedClient );
202    const nsecs_t TIMEOUT = s2ns(1);
203    const int identity = mIdentity;
204
205    Mutex::Autolock _l(client.lock);
206    while ((condition()==false) &&
207            (stack.identity == identity) &&
208            (stack.status == NO_ERROR))
209    {
210        status_t err = client.cv.waitRelative(client.lock, TIMEOUT);
211        // handle errors and timeouts
212        if (CC_UNLIKELY(err != NO_ERROR)) {
213            if (err == TIMED_OUT) {
214                if (condition()) {
215                    LOGE("waitForCondition(%s) timed out (identity=%d), "
216                        "but condition is true! We recovered but it "
217                        "shouldn't happen." , condition.name(), stack.identity);
218                    break;
219                } else {
220                    LOGW("waitForCondition(%s) timed out "
221                        "(identity=%d, status=%d). "
222                        "CPU may be pegged. trying again.", condition.name(),
223                        stack.identity, stack.status);
224                }
225            } else {
226                LOGE("waitForCondition(%s) error (%s) ",
227                        condition.name(), strerror(-err));
228                return err;
229            }
230        }
231    }
232    return (stack.identity != mIdentity) ? status_t(BAD_INDEX) : stack.status;
233}
234// ============================================================================
235// conditions and updates
236// ============================================================================
237
238SharedBufferClient::DequeueCondition::DequeueCondition(
239        SharedBufferClient* sbc) : ConditionBase(sbc)  {
240}
241bool SharedBufferClient::DequeueCondition::operator()() const {
242    return stack.available > 0;
243}
244
245SharedBufferClient::LockCondition::LockCondition(
246        SharedBufferClient* sbc, int buf) : ConditionBase(sbc), buf(buf) {
247}
248bool SharedBufferClient::LockCondition::operator()() const {
249    // NOTE: if stack.head is messed up, we could crash the client
250    // or cause some drawing artifacts. This is okay, as long as it is
251    // limited to the client.
252    return (buf != stack.index[stack.head] ||
253            (stack.queued > 0 && stack.inUse != buf));
254}
255
256SharedBufferServer::ReallocateCondition::ReallocateCondition(
257        SharedBufferBase* sbb, int buf) : ConditionBase(sbb), buf(buf) {
258}
259bool SharedBufferServer::ReallocateCondition::operator()() const {
260    int32_t head = stack.head;
261    if (uint32_t(head) >= NUM_BUFFER_MAX) {
262        // if stack.head is messed up, we cannot allow the server to
263        // crash (since stack.head is mapped on the client side)
264        stack.status = BAD_VALUE;
265        return false;
266    }
267    // TODO: we should also check that buf has been dequeued
268    return (buf != stack.index[head]);
269}
270
271// ----------------------------------------------------------------------------
272
273SharedBufferClient::QueueUpdate::QueueUpdate(SharedBufferBase* sbb)
274    : UpdateBase(sbb) {
275}
276ssize_t SharedBufferClient::QueueUpdate::operator()() {
277    android_atomic_inc(&stack.queued);
278    return NO_ERROR;
279}
280
281SharedBufferClient::UndoDequeueUpdate::UndoDequeueUpdate(SharedBufferBase* sbb)
282    : UpdateBase(sbb) {
283}
284ssize_t SharedBufferClient::UndoDequeueUpdate::operator()() {
285    android_atomic_inc(&stack.available);
286    return NO_ERROR;
287}
288
289SharedBufferServer::UnlockUpdate::UnlockUpdate(
290        SharedBufferBase* sbb, int lockedBuffer)
291    : UpdateBase(sbb), lockedBuffer(lockedBuffer) {
292}
293ssize_t SharedBufferServer::UnlockUpdate::operator()() {
294    if (stack.inUse != lockedBuffer) {
295        LOGE("unlocking %d, but currently locked buffer is %d",
296                lockedBuffer, stack.inUse);
297        return BAD_VALUE;
298    }
299    android_atomic_write(-1, &stack.inUse);
300    return NO_ERROR;
301}
302
303SharedBufferServer::RetireUpdate::RetireUpdate(
304        SharedBufferBase* sbb, int numBuffers)
305    : UpdateBase(sbb), numBuffers(numBuffers) {
306}
307ssize_t SharedBufferServer::RetireUpdate::operator()() {
308    int32_t head = stack.head;
309    if (uint32_t(head) >= NUM_BUFFER_MAX)
310        return BAD_VALUE;
311
312    // Preventively lock the current buffer before updating queued.
313    android_atomic_write(stack.index[head], &stack.inUse);
314
315    // Decrement the number of queued buffers
316    int32_t queued;
317    do {
318        queued = stack.queued;
319        if (queued == 0) {
320            return NOT_ENOUGH_DATA;
321        }
322    } while (android_atomic_cmpxchg(queued, queued-1, &stack.queued));
323
324    // lock the buffer before advancing head, which automatically unlocks
325    // the buffer we preventively locked upon entering this function
326    head = (head + 1) % numBuffers;
327    android_atomic_write(stack.index[head], &stack.inUse);
328
329    // head is only modified here, so we don't need to use cmpxchg
330    android_atomic_write(head, &stack.head);
331
332    // now that head has moved, we can increment the number of available buffers
333    android_atomic_inc(&stack.available);
334    return head;
335}
336
337SharedBufferServer::StatusUpdate::StatusUpdate(
338        SharedBufferBase* sbb, status_t status)
339    : UpdateBase(sbb), status(status) {
340}
341
342ssize_t SharedBufferServer::StatusUpdate::operator()() {
343    android_atomic_write(status, &stack.status);
344    return NO_ERROR;
345}
346
347// ============================================================================
348
349SharedBufferClient::SharedBufferClient(SharedClient* sharedClient,
350        int surface, int num, int32_t identity)
351    : SharedBufferBase(sharedClient, surface, num, identity),
352      tail(0), undoDequeueTail(0)
353{
354    SharedBufferStack& stack( *mSharedStack );
355    tail = computeTail();
356    queued_head = stack.head;
357}
358
359ssize_t SharedBufferClient::dequeue()
360{
361    SharedBufferStack& stack( *mSharedStack );
362
363    if (stack.head == tail && stack.available == mNumBuffers) {
364        LOGW("dequeue: tail=%d, head=%d, avail=%d, queued=%d",
365                tail, stack.head, stack.available, stack.queued);
366    }
367
368    const nsecs_t dequeueTime = systemTime(SYSTEM_TIME_THREAD);
369
370    //LOGD("[%d] about to dequeue a buffer",
371    //        mSharedStack->identity);
372    DequeueCondition condition(this);
373    status_t err = waitForCondition(condition);
374    if (err != NO_ERROR)
375        return ssize_t(err);
376
377    // NOTE: 'stack.available' is part of the conditions, however
378    // decrementing it, never changes any conditions, so we don't need
379    // to do this as part of an update.
380    if (android_atomic_dec(&stack.available) == 0) {
381        LOGW("dequeue probably called from multiple threads!");
382    }
383
384    undoDequeueTail = tail;
385    int dequeued = stack.index[tail];
386    tail = ((tail+1 >= mNumBuffers) ? 0 : tail+1);
387    LOGD_IF(DEBUG_ATOMICS, "dequeued=%d, tail++=%d, %s",
388            dequeued, tail, dump("").string());
389
390    mDequeueTime[dequeued] = dequeueTime;
391
392    return dequeued;
393}
394
395status_t SharedBufferClient::undoDequeue(int buf)
396{
397    // TODO: we can only undo the previous dequeue, we should
398    // enforce that in the api
399    UndoDequeueUpdate update(this);
400    status_t err = updateCondition( update );
401    if (err == NO_ERROR) {
402        tail = undoDequeueTail;
403    }
404    return err;
405}
406
407status_t SharedBufferClient::lock(int buf)
408{
409    SharedBufferStack& stack( *mSharedStack );
410    LockCondition condition(this, buf);
411    status_t err = waitForCondition(condition);
412    return err;
413}
414
415status_t SharedBufferClient::queue(int buf)
416{
417    SharedBufferStack& stack( *mSharedStack );
418
419    queued_head = ((queued_head+1 >= mNumBuffers) ? 0 : queued_head+1);
420    stack.index[queued_head] = buf;
421
422    QueueUpdate update(this);
423    status_t err = updateCondition( update );
424    LOGD_IF(DEBUG_ATOMICS, "queued=%d, %s", buf, dump("").string());
425
426    const nsecs_t now = systemTime(SYSTEM_TIME_THREAD);
427    stack.stats.totalTime = ns2us(now - mDequeueTime[buf]);
428    return err;
429}
430
431bool SharedBufferClient::needNewBuffer(int buf) const
432{
433    SharedBufferStack& stack( *mSharedStack );
434    const uint32_t mask = 1<<buf;
435    return (android_atomic_and(~mask, &stack.reallocMask) & mask) != 0;
436}
437
438status_t SharedBufferClient::setCrop(int buf, const Rect& crop)
439{
440    SharedBufferStack& stack( *mSharedStack );
441    return stack.setCrop(buf, crop);
442}
443
444status_t SharedBufferClient::setDirtyRegion(int buf, const Region& reg)
445{
446    SharedBufferStack& stack( *mSharedStack );
447    return stack.setDirtyRegion(buf, reg);
448}
449
450status_t SharedBufferClient::setBufferCount(int bufferCount)
451{
452    if (uint32_t(bufferCount) >= NUM_BUFFER_MAX)
453        return BAD_VALUE;
454    mNumBuffers = bufferCount;
455    return NO_ERROR;
456}
457
458// ----------------------------------------------------------------------------
459
460SharedBufferServer::SharedBufferServer(SharedClient* sharedClient,
461        int surface, int num, int32_t identity)
462    : SharedBufferBase(sharedClient, surface, num, identity)
463{
464    mSharedStack->init(identity);
465    mSharedStack->head = num-1;
466    mSharedStack->available = num;
467    mSharedStack->queued = 0;
468    mSharedStack->reallocMask = 0;
469    memset(mSharedStack->buffers, 0, sizeof(mSharedStack->buffers));
470    for (int i=0 ; i<num ; i++) {
471        mBufferList.add(i);
472        mSharedStack->index[i] = i;
473    }
474}
475
476ssize_t SharedBufferServer::retireAndLock()
477{
478    RetireUpdate update(this, mNumBuffers);
479    ssize_t buf = updateCondition( update );
480    if (buf >= 0) {
481        if (uint32_t(buf) >= NUM_BUFFER_MAX)
482            return BAD_VALUE;
483        SharedBufferStack& stack( *mSharedStack );
484        buf = stack.index[buf];
485        LOGD_IF(DEBUG_ATOMICS && buf>=0, "retire=%d, %s",
486                int(buf), dump("").string());
487    }
488    return buf;
489}
490
491status_t SharedBufferServer::unlock(int buf)
492{
493    UnlockUpdate update(this, buf);
494    status_t err = updateCondition( update );
495    return err;
496}
497
498void SharedBufferServer::setStatus(status_t status)
499{
500    if (status < NO_ERROR) {
501        StatusUpdate update(this, status);
502        updateCondition( update );
503    }
504}
505
506status_t SharedBufferServer::reallocate()
507{
508    SharedBufferStack& stack( *mSharedStack );
509    uint32_t mask = (1<<mNumBuffers)-1;
510    android_atomic_or(mask, &stack.reallocMask);
511    return NO_ERROR;
512}
513
514int32_t SharedBufferServer::getQueuedCount() const
515{
516    SharedBufferStack& stack( *mSharedStack );
517    return stack.queued;
518}
519
520status_t SharedBufferServer::assertReallocate(int buf)
521{
522    // TODO: need to validate "buf"
523    ReallocateCondition condition(this, buf);
524    status_t err = waitForCondition(condition);
525    return err;
526}
527
528Region SharedBufferServer::getDirtyRegion(int buf) const
529{
530    SharedBufferStack& stack( *mSharedStack );
531    return stack.getDirtyRegion(buf);
532}
533
534
535/*
536 *
537 * NOTE: this is not thread-safe on the server-side, meaning
538 * 'head' cannot move during this operation. The client-side
539 * can safely operate an usual.
540 *
541 */
542status_t SharedBufferServer::resize(int newNumBuffers)
543{
544    if (uint32_t(newNumBuffers) >= NUM_BUFFER_MAX)
545        return BAD_VALUE;
546
547    // for now we're not supporting shrinking
548    const int numBuffers = mNumBuffers;
549    if (newNumBuffers < numBuffers)
550        return BAD_VALUE;
551
552    SharedBufferStack& stack( *mSharedStack );
553    const int extra = newNumBuffers - numBuffers;
554
555    // read the head, make sure it's valid
556    int32_t head = stack.head;
557    if (uint32_t(head) >= NUM_BUFFER_MAX)
558        return BAD_VALUE;
559
560    int base = numBuffers;
561    int32_t avail = stack.available;
562    int tail = head - avail + 1;
563
564    if (tail >= 0) {
565        int8_t* const index = const_cast<int8_t*>(stack.index);
566        const int nb = numBuffers - head;
567        memmove(&index[head + extra], &index[head], nb);
568        base = head;
569        // move head 'extra' ahead, this doesn't impact stack.index[head];
570        stack.head = head + extra;
571    }
572    stack.available += extra;
573
574    // fill the new free space with unused buffers
575    BufferList::const_iterator curr(mBufferList.free_begin());
576    for (int i=0 ; i<extra ; i++) {
577        stack.index[base+i] = *curr;
578        mBufferList.add(*curr);
579        ++curr;
580    }
581
582    mNumBuffers = newNumBuffers;
583    return NO_ERROR;
584}
585
586SharedBufferStack::Statistics SharedBufferServer::getStats() const
587{
588    SharedBufferStack& stack( *mSharedStack );
589    return stack.stats;
590}
591
592// ---------------------------------------------------------------------------
593status_t SharedBufferServer::BufferList::add(int value)
594{
595    if (uint32_t(value) >= mCapacity)
596        return BAD_VALUE;
597    uint32_t mask = 1<<(31-value);
598    if (mList & mask)
599        return ALREADY_EXISTS;
600    mList |= mask;
601    return NO_ERROR;
602}
603
604status_t SharedBufferServer::BufferList::remove(int value)
605{
606    if (uint32_t(value) >= mCapacity)
607        return BAD_VALUE;
608    uint32_t mask = 1<<(31-value);
609    if (!(mList & mask))
610        return NAME_NOT_FOUND;
611    mList &= ~mask;
612    return NO_ERROR;
613}
614
615
616// ---------------------------------------------------------------------------
617}; // namespace android
618