1/*
2 * Copyright (C) 2013 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#ifndef ANDROID_RS_GRALLOC_CONSUMER_H
18#define ANDROID_RS_GRALLOC_CONSUMER_H
19
20#include <gui/ConsumerBase.h>
21
22#include <ui/GraphicBuffer.h>
23
24#include <utils/String8.h>
25#include <utils/Vector.h>
26#include <utils/threads.h>
27
28
29// ---------------------------------------------------------------------------
30namespace android {
31namespace renderscript {
32
33class Allocation;
34
35/**
36 * CpuConsumer is a BufferQueue consumer endpoint that allows direct CPU
37 * access to the underlying gralloc buffers provided by BufferQueue. Multiple
38 * buffers may be acquired by it at once, to be used concurrently by the
39 * CpuConsumer owner. Sets gralloc usage flags to be software-read-only.
40 * This queue is synchronous by default.
41 */
42class GrallocConsumer : public ConsumerBase
43{
44  public:
45    typedef ConsumerBase::FrameAvailableListener FrameAvailableListener;
46
47    GrallocConsumer(Allocation *, const sp<IGraphicBufferConsumer>& bq, int flags, uint32_t numAlloc);
48
49    virtual ~GrallocConsumer();
50    status_t lockNextBuffer(uint32_t idx = 0);
51    status_t unlockBuffer(uint32_t idx = 0);
52    uint32_t getNextAvailableIdx(Allocation *a);
53    bool releaseIdx(uint32_t idx);
54    uint32_t mNumAlloc;
55
56
57  private:
58    status_t releaseAcquiredBufferLocked(uint32_t idx);
59    // Boolean array to check if a position has been occupied or not.
60    bool *isIdxUsed;
61    Allocation **mAlloc;
62
63    // Tracking for buffers acquired by the user
64    struct AcquiredBuffer {
65        // Need to track the original mSlot index and the buffer itself because
66        // the mSlot entry may be freed/reused before the acquired buffer is
67        // released.
68        int mSlot;
69        sp<GraphicBuffer> mGraphicBuffer;
70        void *mBufferPointer;
71
72        AcquiredBuffer() :
73                mSlot(BufferQueue::INVALID_BUFFER_SLOT),
74                mBufferPointer(nullptr) {
75        }
76    };
77    AcquiredBuffer *mAcquiredBuffer;
78};
79
80} // namespace renderscript
81} // namespace android
82
83#endif // ANDROID_RS_GRALLOC_CONSUMER_H
84