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 <media/NdkImage.h>
21 #include <media/NdkImageReader.h>
22 
23 // ---------------------------------------------------------------------------
24 namespace android {
25 namespace renderscript {
26 
27 class Allocation;
28 class Context;
29 
30 /**
31  * CpuConsumer is a BufferQueue consumer endpoint that allows direct CPU
32  * access to the underlying gralloc buffers provided by BufferQueue. Multiple
33  * buffers may be acquired by it at once, to be used concurrently by the
34  * CpuConsumer owner. Sets gralloc usage flags to be software-read-only.
35  * This queue is synchronous by default.
36  */
37 class GrallocConsumer
38 {
39   public:
40     GrallocConsumer(const Context *, Allocation *, uint32_t numAlloc);
41 
42     virtual ~GrallocConsumer();
43     ANativeWindow* getNativeWindow();
44     media_status_t lockNextBuffer(uint32_t idx = 0);
45     media_status_t unlockBuffer(uint32_t idx = 0);
46     uint32_t getNextAvailableIdx(Allocation *a);
47     bool releaseIdx(uint32_t idx);
48     bool isActive();
49     uint32_t mNumAlloc;
50 
51     static void onFrameAvailable(void* obj, AImageReader* reader);
52 
53   private:
54     media_status_t releaseAcquiredBufferLocked(uint32_t idx);
55     // Boolean array to check if a position has been occupied or not.
56     bool *isIdxUsed;
57     Allocation **mAlloc;
58 
59     const Context *mCtx;
60     AImageReader* mImgReader;
61     ANativeWindow* mNativeWindow;
62     AImageReader_ImageListener mReaderCb;
63     // Tracking for buffers acquired by the user
64     struct AcquiredBuffer {
65         AImage *mImg;
66         uint8_t *mBufferPointer;
67 
AcquiredBufferAcquiredBuffer68         AcquiredBuffer() :
69                 mImg(nullptr),
70                 mBufferPointer(nullptr) {
71         }
72     };
73     AcquiredBuffer *mAcquiredBuffer;
74 };
75 
76 } // namespace renderscript
77 } // namespace android
78 
79 #endif // ANDROID_RS_GRALLOC_CONSUMER_H
80