1 /*
2  * Copyright (C) 2013-2018 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_SERVERS_CAMERA3_OUTPUT_STREAM_H
18 #define ANDROID_SERVERS_CAMERA3_OUTPUT_STREAM_H
19 
20 #include <utils/RefBase.h>
21 #include <gui/IProducerListener.h>
22 #include <gui/Surface.h>
23 
24 #include "utils/LatencyHistogram.h"
25 #include "Camera3Stream.h"
26 #include "Camera3IOStreamBase.h"
27 #include "Camera3OutputStreamInterface.h"
28 #include "Camera3BufferManager.h"
29 
30 namespace android {
31 
32 namespace camera3 {
33 
34 class Camera3BufferManager;
35 
36 /**
37  * Stream info structure that holds the necessary stream info for buffer manager to use for
38  * buffer allocation and management.
39  */
40 struct StreamInfo {
41     int streamId;
42     int streamSetId;
43     uint32_t width;
44     uint32_t height;
45     uint32_t format;
46     android_dataspace dataSpace;
47     uint64_t combinedUsage;
48     size_t totalBufferCount;
49     bool isConfigured;
50     explicit StreamInfo(int id = CAMERA3_STREAM_ID_INVALID,
51             int setId = CAMERA3_STREAM_SET_ID_INVALID,
52             uint32_t w = 0,
53             uint32_t h = 0,
54             uint32_t fmt = 0,
55             android_dataspace ds = HAL_DATASPACE_UNKNOWN,
56             uint64_t usage = 0,
57             size_t bufferCount = 0,
58             bool configured = false) :
streamIdStreamInfo59                 streamId(id),
60                 streamSetId(setId),
61                 width(w),
62                 height(h),
63                 format(fmt),
64                 dataSpace(ds),
65                 combinedUsage(usage),
66                 totalBufferCount(bufferCount),
67                 isConfigured(configured){}
68 };
69 
70 /**
71  * A class for managing a single stream of output data from the camera device.
72  */
73 class Camera3OutputStream :
74         public Camera3IOStreamBase,
75         public Camera3OutputStreamInterface {
76   public:
77     /**
78      * Set up a stream for formats that have 2 dimensions, such as RAW and YUV.
79      * A valid stream set id needs to be set to support buffer sharing between multiple
80      * streams.
81      */
82     Camera3OutputStream(int id, sp<Surface> consumer,
83             uint32_t width, uint32_t height, int format,
84             android_dataspace dataSpace, camera3_stream_rotation_t rotation,
85             nsecs_t timestampOffset, const String8& physicalCameraId,
86             int setId = CAMERA3_STREAM_SET_ID_INVALID);
87 
88     /**
89      * Set up a stream for formats that have a variable buffer size for the same
90      * dimensions, such as compressed JPEG.
91      * A valid stream set id needs to be set to support buffer sharing between multiple
92      * streams.
93      */
94     Camera3OutputStream(int id, sp<Surface> consumer,
95             uint32_t width, uint32_t height, size_t maxSize, int format,
96             android_dataspace dataSpace, camera3_stream_rotation_t rotation,
97             nsecs_t timestampOffset, const String8& physicalCameraId,
98             int setId = CAMERA3_STREAM_SET_ID_INVALID);
99 
100     /**
101      * Set up a stream with deferred consumer for formats that have 2 dimensions, such as
102      * RAW and YUV. The consumer must be set before using this stream for output. A valid
103      * stream set id needs to be set to support buffer sharing between multiple streams.
104      */
105     Camera3OutputStream(int id, uint32_t width, uint32_t height, int format,
106             uint64_t consumerUsage, android_dataspace dataSpace,
107             camera3_stream_rotation_t rotation, nsecs_t timestampOffset,
108             const String8& physicalCameraId,
109             int setId = CAMERA3_STREAM_SET_ID_INVALID);
110 
111     virtual ~Camera3OutputStream();
112 
113     /**
114      * Camera3Stream interface
115      */
116 
117     virtual void     dump(int fd, const Vector<String16> &args) const;
118 
119     /**
120      * Set the transform on the output stream; one of the
121      * HAL_TRANSFORM_* / NATIVE_WINDOW_TRANSFORM_* constants.
122      */
123     status_t         setTransform(int transform);
124 
125     /**
126      * Return if this output stream is for video encoding.
127      */
128     bool isVideoStream() const;
129     /**
130      * Return if this output stream is consumed by hardware composer.
131      */
132     bool isConsumedByHWComposer() const;
133 
134     /**
135      * Return if this output stream is consumed by hardware texture.
136      */
137     bool isConsumedByHWTexture() const;
138 
139     /**
140      * Return if the consumer configuration of this stream is deferred.
141      */
142     virtual bool isConsumerConfigurationDeferred(size_t surface_id) const;
143 
144     /**
145      * Set the consumer surfaces to the output stream.
146      */
147     virtual status_t setConsumers(const std::vector<sp<Surface>>& consumers);
148 
149     class BufferProducerListener : public SurfaceListener {
150         public:
BufferProducerListener(wp<Camera3OutputStream> parent,bool needsReleaseNotify)151             BufferProducerListener(wp<Camera3OutputStream> parent, bool needsReleaseNotify)
152                     : mParent(parent), mNeedsReleaseNotify(needsReleaseNotify) {}
153 
154             /**
155             * Implementation of IProducerListener, used to notify this stream that the consumer
156             * has returned a buffer and it is ready to return to Camera3BufferManager for reuse.
157             */
158             virtual void onBufferReleased();
needsReleaseNotify()159             virtual bool needsReleaseNotify() { return mNeedsReleaseNotify; }
160             virtual void onBuffersDiscarded(const std::vector<sp<GraphicBuffer>>& buffers);
161 
162         private:
163             wp<Camera3OutputStream> mParent;
164             bool mNeedsReleaseNotify;
165     };
166 
167     virtual status_t detachBuffer(sp<GraphicBuffer>* buffer, int* fenceFd);
168 
169     /**
170      * Notify that the buffer is being released to the buffer queue instead of
171      * being queued to the consumer.
172      */
173     virtual status_t notifyBufferReleased(ANativeWindowBuffer *anwBuffer);
174 
175     /**
176      * Drop buffers if dropping is true. If dropping is false, do not drop buffers.
177      */
178     virtual status_t dropBuffers(bool dropping) override;
179 
180     /**
181      * Query the physical camera id for the output stream.
182      */
183     virtual const String8& getPhysicalCameraId() const override;
184 
185     /**
186      * Set the graphic buffer manager to get/return the stream buffers.
187      *
188      * It is only legal to call this method when stream is in STATE_CONSTRUCTED state.
189      */
190     status_t setBufferManager(sp<Camera3BufferManager> bufferManager);
191 
192     /**
193      * Query the ouput surface id.
194      */
getSurfaceId(const sp<Surface> &)195     virtual ssize_t getSurfaceId(const sp<Surface> &/*surface*/) { return 0; }
196 
getUniqueSurfaceIds(const std::vector<size_t> &,std::vector<size_t> *)197     virtual status_t getUniqueSurfaceIds(const std::vector<size_t>&,
198             /*out*/std::vector<size_t>*) { return INVALID_OPERATION; };
199 
200     /**
201      * Update the stream output surfaces.
202      */
203     virtual status_t updateStream(const std::vector<sp<Surface>> &outputSurfaces,
204             const std::vector<OutputStreamInfo> &outputInfo,
205             const std::vector<size_t> &removedSurfaceIds,
206             KeyedVector<sp<Surface>, size_t> *outputMap/*out*/);
207 
208     /**
209      * Apply ZSL related consumer usage quirk.
210      */
211     static void applyZSLUsageQuirk(int format, uint64_t *consumerUsage /*inout*/);
212 
213   protected:
214     Camera3OutputStream(int id, camera3_stream_type_t type,
215             uint32_t width, uint32_t height, int format,
216             android_dataspace dataSpace, camera3_stream_rotation_t rotation,
217             const String8& physicalCameraId,
218             uint64_t consumerUsage = 0, nsecs_t timestampOffset = 0,
219             int setId = CAMERA3_STREAM_SET_ID_INVALID);
220 
221     /**
222      * Note that we release the lock briefly in this function
223      */
224     virtual status_t returnBufferCheckedLocked(
225             const camera3_stream_buffer &buffer,
226             nsecs_t timestamp,
227             bool output,
228             const std::vector<size_t>& surface_ids,
229             /*out*/
230             sp<Fence> *releaseFenceOut);
231 
232     virtual status_t disconnectLocked();
233 
234     status_t getEndpointUsageForSurface(uint64_t *usage,
235             const sp<Surface>& surface) const;
236     status_t configureConsumerQueueLocked();
237 
238     // Consumer as the output of camera HAL
239     sp<Surface> mConsumer;
240 
getPresetConsumerUsage()241     uint64_t getPresetConsumerUsage() const { return mConsumerUsage; }
242 
243     static const nsecs_t       kDequeueBufferTimeout   = 1000000000; // 1 sec
244 
245     status_t getBufferLockedCommon(ANativeWindowBuffer** anb, int* fenceFd);
246 
247 
248   private:
249 
250     int               mTransform;
251 
252     virtual status_t  setTransformLocked(int transform);
253 
254     bool mTraceFirstBuffer;
255 
256     // Name of Surface consumer
257     String8           mConsumerName;
258 
259     // Whether consumer assumes MONOTONIC timestamp
260     bool mUseMonoTimestamp;
261 
262     /**
263      * GraphicBuffer manager this stream is registered to. Used to replace the buffer
264      * allocation/deallocation role of BufferQueue.
265      */
266     sp<Camera3BufferManager> mBufferManager;
267 
268     /**
269      * Buffer producer listener, used to handle notification when a buffer is released
270      * from consumer side, or a set of buffers are discarded by the consumer.
271      */
272     sp<BufferProducerListener> mBufferProducerListener;
273 
274     /**
275      * Flag indicating if the buffer manager is used to allocate the stream buffers
276      */
277     bool mUseBufferManager;
278 
279     /**
280      * Timestamp offset for video and hardware composer consumed streams
281      */
282     nsecs_t mTimestampOffset;
283 
284     /**
285      * Consumer end point usage flag set by the constructor for the deferred
286      * consumer case.
287      */
288     uint64_t    mConsumerUsage;
289 
290     // Whether to drop valid buffers.
291     bool mDropBuffers;
292 
293     /**
294      * Internal Camera3Stream interface
295      */
296     virtual status_t getBufferLocked(camera3_stream_buffer *buffer,
297             const std::vector<size_t>& surface_ids);
298 
299     virtual status_t returnBufferLocked(
300             const camera3_stream_buffer &buffer,
301             nsecs_t timestamp, const std::vector<size_t>& surface_ids);
302 
303     virtual status_t queueBufferToConsumer(sp<ANativeWindow>& consumer,
304             ANativeWindowBuffer* buffer, int anwReleaseFence,
305             const std::vector<size_t>& surface_ids);
306 
307     virtual status_t configureQueueLocked();
308 
309     virtual status_t getEndpointUsage(uint64_t *usage) const;
310 
311     /**
312      * Private methods
313      */
314     void onBuffersRemovedLocked(const std::vector<sp<GraphicBuffer>>&);
315     status_t detachBufferLocked(sp<GraphicBuffer>* buffer, int* fenceFd);
316     // Call this after each dequeueBuffer/attachBuffer/detachNextBuffer call to get update on
317     // removed buffers. Set notifyBufferManager to false when the call is initiated by buffer
318     // manager so buffer manager doesn't need to be notified.
319     void checkRemovedBuffersLocked(bool notifyBufferManager = true);
320 
321     // Check return status of IGBP calls and set abandoned state accordingly
322     void checkRetAndSetAbandonedLocked(status_t res);
323 
324     // If the status indicates abandonded stream, only log when state hasn't been updated to
325     // STATE_ABANDONED
326     static bool shouldLogError(status_t res, StreamState state);
327 
328     static const int32_t kDequeueLatencyBinSize = 5; // in ms
329     CameraLatencyHistogram mDequeueBufferLatency;
330 
331 }; // class Camera3OutputStream
332 
333 } // namespace camera3
334 
335 } // namespace android
336 
337 #endif
338