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