• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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_GUI_CPUCONSUMER_H
18 #define ANDROID_GUI_CPUCONSUMER_H
19 
20 #include <system/window.h>
21 
22 #include <com_android_graphics_libgui_flags.h>
23 #include <gui/BufferQueue.h>
24 #include <gui/ConsumerBase.h>
25 
26 #include <utils/Vector.h>
27 
28 
29 namespace android {
30 
31 class BufferQueue;
32 class GraphicBuffer;
33 class String8;
34 class Surface;
35 
36 /**
37  * CpuConsumer is a BufferQueue consumer endpoint that allows direct CPU
38  * access to the underlying gralloc buffers provided by BufferQueue. Multiple
39  * buffers may be acquired by it at once, to be used concurrently by the
40  * CpuConsumer owner. Sets gralloc usage flags to be software-read-only.
41  * This queue is synchronous by default.
42  */
43 
44 class CpuConsumer : public ConsumerBase
45 {
46   public:
47     typedef ConsumerBase::FrameAvailableListener FrameAvailableListener;
48 
49     struct LockedBuffer {
50         uint8_t    *data;
51         uint32_t    width;
52         uint32_t    height;
53         PixelFormat format;
54         uint32_t    stride;
55         Rect        crop;
56         uint32_t    transform;
57         uint32_t    scalingMode;
58         int64_t     timestamp;
59         android_dataspace dataSpace;
60         uint64_t    frameNumber;
61         // this is the same as format, except for formats that are compatible with
62         // a flexible format (e.g. HAL_PIXEL_FORMAT_YCbCr_420_888). In the latter
63         // case this contains that flexible format
64         PixelFormat flexFormat;
65         // Values below are only valid when using HAL_PIXEL_FORMAT_YCbCr_420_888
66         // or compatible format, in which case LockedBuffer::data
67         // contains the Y channel, and stride is the Y channel stride. For other
68         // formats, these will all be 0.
69         uint8_t    *dataCb;
70         uint8_t    *dataCr;
71         uint32_t    chromaStride;
72         uint32_t    chromaStep;
73 
LockedBufferLockedBuffer74         LockedBuffer() :
75             data(nullptr),
76             width(0),
77             height(0),
78             format(PIXEL_FORMAT_NONE),
79             stride(0),
80             crop(Rect::EMPTY_RECT),
81             transform(0),
82             scalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
83             timestamp(0),
84             dataSpace(HAL_DATASPACE_UNKNOWN),
85             frameNumber(0),
86             flexFormat(PIXEL_FORMAT_NONE),
87             dataCb(nullptr),
88             dataCr(nullptr),
89             chromaStride(0),
90             chromaStep(0)
91         {}
92     };
93 
94     // Create a new CPU consumer. The maxLockedBuffers parameter specifies
95     // how many buffers can be locked for user access at the same time.
96     static std::tuple<sp<CpuConsumer>, sp<Surface>> create(size_t maxLockedBuffers,
97                                                            bool controlledByApp = false,
98                                                            bool isConsumerSurfaceFlinger = false);
99     static sp<CpuConsumer> create(const sp<IGraphicBufferConsumer>& bq, size_t maxLockedBuffers,
100                                   bool controlledByApp = false)
101             __attribute((deprecated("Prefer ctors that create their own surface and consumer.")));
102 
103 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
104     CpuConsumer(size_t maxLockedBuffers, bool controlledByApp = false,
105                 bool isConsumerSurfaceFlinger = false);
106 
107     CpuConsumer(const sp<IGraphicBufferConsumer>& bq, size_t maxLockedBuffers,
108                 bool controlledByApp = false)
109             __attribute((deprecated("Prefer ctors that create their own surface and consumer.")));
110 #else
111     CpuConsumer(const sp<IGraphicBufferConsumer>& bq, size_t maxLockedBuffers,
112                 bool controlledByApp = false);
113 #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
114 
115     // Gets the next graphics buffer from the producer and locks it for CPU use,
116     // filling out the passed-in locked buffer structure with the native pointer
117     // and metadata. Returns BAD_VALUE if no new buffer is available, and
118     // NOT_ENOUGH_DATA if the maximum number of buffers is already locked.
119     //
120     // Only a fixed number of buffers can be locked at a time, determined by the
121     // construction-time maxLockedBuffers parameter. If INVALID_OPERATION is
122     // returned by lockNextBuffer, then old buffers must be returned to the queue
123     // by calling unlockBuffer before more buffers can be acquired.
124     status_t lockNextBuffer(LockedBuffer *nativeBuffer);
125 
126     // Returns a locked buffer to the queue, allowing it to be reused. Since
127     // only a fixed number of buffers may be locked at a time, old buffers must
128     // be released by calling unlockBuffer to ensure new buffers can be acquired by
129     // lockNextBuffer.
130     status_t unlockBuffer(const LockedBuffer &nativeBuffer);
131 
132   private:
133     // Maximum number of buffers that can be locked at a time
134     const size_t mMaxLockedBuffers;
135 
136     // Tracking for buffers acquired by the user
137     struct AcquiredBuffer {
138         static constexpr uintptr_t kUnusedId = 0;
139 
140         // Need to track the original mSlot index and the buffer itself because
141         // the mSlot entry may be freed/reused before the acquired buffer is
142         // released.
143         int mSlot;
144         sp<GraphicBuffer> mGraphicBuffer;
145         uintptr_t mLockedBufferId;
146 
AcquiredBufferAcquiredBuffer147         AcquiredBuffer() :
148                 mSlot(BufferQueue::INVALID_BUFFER_SLOT),
149                 mLockedBufferId(kUnusedId) {
150         }
151 
resetAcquiredBuffer152         void reset() {
153             mSlot = BufferQueue::INVALID_BUFFER_SLOT;
154             mGraphicBuffer.clear();
155             mLockedBufferId = kUnusedId;
156         }
157     };
158 
159     size_t findAcquiredBufferLocked(uintptr_t id) const;
160 
161     status_t lockBufferItem(const BufferItem& item, LockedBuffer* outBuffer) const;
162 
163     Vector<AcquiredBuffer> mAcquiredBuffers;
164 
165     // Count of currently locked buffers
166     size_t mCurrentLockedBuffers;
167 };
168 
169 } // namespace android
170 
171 #endif // ANDROID_GUI_CPUCONSUMER_H
172