blob: 366d22af1c991b120d0559cb79aa8709c1d8f290 [file] [log] [blame]
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -08001/*
Shuzhen Wangc28189a2017-11-27 23:05:10 -08002 * Copyright (C) 2013-2018 The Android Open Source Project
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -08003 *
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
Yin-Chia Yeh14ef48d2020-02-10 15:06:37 -080020#include <mutex>
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080021#include <utils/RefBase.h>
Zhijun He125684a2015-12-26 15:07:30 -080022#include <gui/IProducerListener.h>
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080023#include <gui/Surface.h>
24
Shuzhen Wang686f6442017-06-20 16:16:04 -070025#include "utils/LatencyHistogram.h"
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080026#include "Camera3Stream.h"
Igor Murashkine3a9f962013-05-08 18:03:15 -070027#include "Camera3IOStreamBase.h"
Igor Murashkin2fba5842013-04-22 14:03:54 -070028#include "Camera3OutputStreamInterface.h"
Zhijun He125684a2015-12-26 15:07:30 -080029#include "Camera3BufferManager.h"
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080030
31namespace android {
32
33namespace camera3 {
34
Zhijun He125684a2015-12-26 15:07:30 -080035class 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 */
41struct StreamInfo {
42 int streamId;
43 int streamSetId;
44 uint32_t width;
45 uint32_t height;
46 uint32_t format;
47 android_dataspace dataSpace;
Emilian Peev050f5dc2017-05-18 14:43:56 +010048 uint64_t combinedUsage;
Zhijun He125684a2015-12-26 15:07:30 -080049 size_t totalBufferCount;
50 bool isConfigured;
Chih-Hung Hsiehd19d9942016-08-29 14:21:14 -070051 explicit StreamInfo(int id = CAMERA3_STREAM_ID_INVALID,
Zhijun He125684a2015-12-26 15:07:30 -080052 int setId = CAMERA3_STREAM_SET_ID_INVALID,
53 uint32_t w = 0,
54 uint32_t h = 0,
55 uint32_t fmt = 0,
56 android_dataspace ds = HAL_DATASPACE_UNKNOWN,
Emilian Peev050f5dc2017-05-18 14:43:56 +010057 uint64_t usage = 0,
Zhijun He125684a2015-12-26 15:07:30 -080058 size_t bufferCount = 0,
59 bool configured = false) :
60 streamId(id),
61 streamSetId(setId),
62 width(w),
63 height(h),
64 format(fmt),
65 dataSpace(ds),
66 combinedUsage(usage),
67 totalBufferCount(bufferCount),
68 isConfigured(configured){}
69};
70
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080071/**
72 * A class for managing a single stream of output data from the camera device.
73 */
Igor Murashkin2fba5842013-04-22 14:03:54 -070074class Camera3OutputStream :
Igor Murashkine3a9f962013-05-08 18:03:15 -070075 public Camera3IOStreamBase,
Igor Murashkin2fba5842013-04-22 14:03:54 -070076 public Camera3OutputStreamInterface {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080077 public:
78 /**
79 * Set up a stream for formats that have 2 dimensions, such as RAW and YUV.
Zhijun He125684a2015-12-26 15:07:30 -080080 * A valid stream set id needs to be set to support buffer sharing between multiple
81 * streams.
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080082 */
Eino-Ville Talvala727d1722015-06-09 13:44:19 -070083 Camera3OutputStream(int id, sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080084 uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -070085 android_dataspace dataSpace, camera_stream_rotation_t rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -080086 nsecs_t timestampOffset, const String8& physicalCameraId,
87 int setId = CAMERA3_STREAM_SET_ID_INVALID);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080088
89 /**
90 * Set up a stream for formats that have a variable buffer size for the same
91 * dimensions, such as compressed JPEG.
Zhijun He125684a2015-12-26 15:07:30 -080092 * A valid stream set id needs to be set to support buffer sharing between multiple
93 * streams.
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080094 */
Eino-Ville Talvala727d1722015-06-09 13:44:19 -070095 Camera3OutputStream(int id, sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080096 uint32_t width, uint32_t height, size_t maxSize, int format,
Emilian Peevf4816702020-04-03 15:44:51 -070097 android_dataspace dataSpace, camera_stream_rotation_t rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -080098 nsecs_t timestampOffset, const String8& physicalCameraId,
99 int setId = CAMERA3_STREAM_SET_ID_INVALID);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800100
Zhijun He5d677d12016-05-29 16:52:39 -0700101 /**
102 * Set up a stream with deferred consumer for formats that have 2 dimensions, such as
103 * RAW and YUV. The consumer must be set before using this stream for output. A valid
104 * stream set id needs to be set to support buffer sharing between multiple streams.
105 */
106 Camera3OutputStream(int id, uint32_t width, uint32_t height, int format,
Emilian Peev050f5dc2017-05-18 14:43:56 +0100107 uint64_t consumerUsage, android_dataspace dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -0700108 camera_stream_rotation_t rotation, nsecs_t timestampOffset,
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800109 const String8& physicalCameraId,
Zhijun He5d677d12016-05-29 16:52:39 -0700110 int setId = CAMERA3_STREAM_SET_ID_INVALID);
111
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800112 virtual ~Camera3OutputStream();
113
114 /**
115 * Camera3Stream interface
116 */
117
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800118 virtual void dump(int fd, const Vector<String16> &args) const;
119
120 /**
121 * Set the transform on the output stream; one of the
122 * HAL_TRANSFORM_* / NATIVE_WINDOW_TRANSFORM_* constants.
123 */
124 status_t setTransform(int transform);
125
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700126 /**
127 * Return if this output stream is for video encoding.
128 */
129 bool isVideoStream() const;
Shuzhen Wang13a69632016-01-26 09:51:07 -0800130 /**
131 * Return if this output stream is consumed by hardware composer.
132 */
133 bool isConsumedByHWComposer() const;
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700134
Zhijun He5d677d12016-05-29 16:52:39 -0700135 /**
Zhijun Hef0645c12016-08-02 00:58:11 -0700136 * Return if this output stream is consumed by hardware texture.
137 */
138 bool isConsumedByHWTexture() const;
139
140 /**
Zhijun He5d677d12016-05-29 16:52:39 -0700141 * Return if the consumer configuration of this stream is deferred.
142 */
Shuzhen Wang0129d522016-10-30 22:43:41 -0700143 virtual bool isConsumerConfigurationDeferred(size_t surface_id) const;
Zhijun He5d677d12016-05-29 16:52:39 -0700144
145 /**
Shuzhen Wang758c2152017-01-10 18:26:18 -0800146 * Set the consumer surfaces to the output stream.
Zhijun He5d677d12016-05-29 16:52:39 -0700147 */
Shuzhen Wang758c2152017-01-10 18:26:18 -0800148 virtual status_t setConsumers(const std::vector<sp<Surface>>& consumers);
Zhijun He5d677d12016-05-29 16:52:39 -0700149
Shuzhen Wang0160ddd2019-08-15 09:11:56 -0700150 class BufferProducerListener : public SurfaceListener {
Zhijun He125684a2015-12-26 15:07:30 -0800151 public:
Shuzhen Wang0160ddd2019-08-15 09:11:56 -0700152 BufferProducerListener(wp<Camera3OutputStream> parent, bool needsReleaseNotify)
153 : mParent(parent), mNeedsReleaseNotify(needsReleaseNotify) {}
Zhijun He125684a2015-12-26 15:07:30 -0800154
Shuzhen Wang0160ddd2019-08-15 09:11:56 -0700155 /**
156 * Implementation of IProducerListener, used to notify this stream that the consumer
157 * has returned a buffer and it is ready to return to Camera3BufferManager for reuse.
158 */
159 virtual void onBufferReleased();
160 virtual bool needsReleaseNotify() { return mNeedsReleaseNotify; }
161 virtual void onBuffersDiscarded(const std::vector<sp<GraphicBuffer>>& buffers);
Zhijun He125684a2015-12-26 15:07:30 -0800162
163 private:
Shuzhen Wang0160ddd2019-08-15 09:11:56 -0700164 wp<Camera3OutputStream> mParent;
165 bool mNeedsReleaseNotify;
Zhijun He125684a2015-12-26 15:07:30 -0800166 };
167
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700168 virtual status_t detachBuffer(sp<GraphicBuffer>* buffer, int* fenceFd);
169
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800170 /**
171 * Notify that the buffer is being released to the buffer queue instead of
172 * being queued to the consumer.
173 */
174 virtual status_t notifyBufferReleased(ANativeWindowBuffer *anwBuffer);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700175
Zhijun He125684a2015-12-26 15:07:30 -0800176 /**
Chien-Yu Chena936ac22017-10-23 15:59:49 -0700177 * Drop buffers if dropping is true. If dropping is false, do not drop buffers.
178 */
179 virtual status_t dropBuffers(bool dropping) override;
180
181 /**
Shuzhen Wang5c22c152017-12-31 17:12:25 -0800182 * Query the physical camera id for the output stream.
183 */
184 virtual const String8& getPhysicalCameraId() const override;
185
186 /**
Zhijun He125684a2015-12-26 15:07:30 -0800187 * Set the graphic buffer manager to get/return the stream buffers.
188 *
189 * It is only legal to call this method when stream is in STATE_CONSTRUCTED state.
190 */
191 status_t setBufferManager(sp<Camera3BufferManager> bufferManager);
192
Emilian Peev40ead602017-09-26 15:46:36 +0100193 /**
194 * Query the ouput surface id.
195 */
196 virtual ssize_t getSurfaceId(const sp<Surface> &/*surface*/) { return 0; }
197
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700198 virtual status_t getUniqueSurfaceIds(const std::vector<size_t>&,
199 /*out*/std::vector<size_t>*) { return INVALID_OPERATION; };
200
Emilian Peev40ead602017-09-26 15:46:36 +0100201 /**
202 * Update the stream output surfaces.
203 */
204 virtual status_t updateStream(const std::vector<sp<Surface>> &outputSurfaces,
205 const std::vector<OutputStreamInfo> &outputInfo,
206 const std::vector<size_t> &removedSurfaceIds,
207 KeyedVector<sp<Surface>, size_t> *outputMap/*out*/);
208
Emilian Peev35ae8262018-11-08 13:11:32 +0000209 /**
Yin-Chia Yeh14ef48d2020-02-10 15:06:37 -0800210 * Set the batch size for buffer operations. The output stream will request
211 * buffers from buffer queue on a batch basis. Currently only video streams
212 * are allowed to set the batch size. Also if the stream is managed by
213 * buffer manager (Surface group in Java API) then batching is also not
214 * supported. Changing batch size on the fly while there is already batched
215 * buffers in the stream is also not supported.
216 * If the batch size is larger than the max dequeue count set
217 * by the camera HAL, the batch size will be set to the max dequeue count
218 * instead.
219 */
220 virtual status_t setBatchSize(size_t batchSize = 1) override;
221
222 /**
Emilian Peev35ae8262018-11-08 13:11:32 +0000223 * Apply ZSL related consumer usage quirk.
224 */
225 static void applyZSLUsageQuirk(int format, uint64_t *consumerUsage /*inout*/);
226
Shuzhen Wangabbcb6b2020-12-09 22:32:44 -0800227 void setImageDumpMask(int mask) { mImageDumpMask = mask; }
228
Igor Murashkine3a9f962013-05-08 18:03:15 -0700229 protected:
Emilian Peevf4816702020-04-03 15:44:51 -0700230 Camera3OutputStream(int id, camera_stream_type_t type,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800231 uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -0700232 android_dataspace dataSpace, camera_stream_rotation_t rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800233 const String8& physicalCameraId,
Emilian Peev050f5dc2017-05-18 14:43:56 +0100234 uint64_t consumerUsage = 0, nsecs_t timestampOffset = 0,
Zhijun He125684a2015-12-26 15:07:30 -0800235 int setId = CAMERA3_STREAM_SET_ID_INVALID);
Igor Murashkine3a9f962013-05-08 18:03:15 -0700236
Zhijun He124ccf42013-05-22 14:01:30 -0700237 /**
238 * Note that we release the lock briefly in this function
239 */
Igor Murashkine3a9f962013-05-08 18:03:15 -0700240 virtual status_t returnBufferCheckedLocked(
Emilian Peevf4816702020-04-03 15:44:51 -0700241 const camera_stream_buffer &buffer,
Igor Murashkine3a9f962013-05-08 18:03:15 -0700242 nsecs_t timestamp,
243 bool output,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700244 const std::vector<size_t>& surface_ids,
Igor Murashkine3a9f962013-05-08 18:03:15 -0700245 /*out*/
246 sp<Fence> *releaseFenceOut);
247
Zhijun He0a210512014-07-24 13:45:15 -0700248 virtual status_t disconnectLocked();
249
Emilian Peev050f5dc2017-05-18 14:43:56 +0100250 status_t getEndpointUsageForSurface(uint64_t *usage,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700251 const sp<Surface>& surface) const;
252 status_t configureConsumerQueueLocked();
253
254 // Consumer as the output of camera HAL
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700255 sp<Surface> mConsumer;
Zhijun He5d677d12016-05-29 16:52:39 -0700256
Emilian Peev050f5dc2017-05-18 14:43:56 +0100257 uint64_t getPresetConsumerUsage() const { return mConsumerUsage; }
Zhijun Hef0645c12016-08-02 00:58:11 -0700258
259 static const nsecs_t kDequeueBufferTimeout = 1000000000; // 1 sec
260
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800261 status_t getBufferLockedCommon(ANativeWindowBuffer** anb, int* fenceFd);
262
263
Shuzhen Wang0129d522016-10-30 22:43:41 -0700264 private:
265
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800266 int mTransform;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800267
Igor Murashkine3a9f962013-05-08 18:03:15 -0700268 virtual status_t setTransformLocked(int transform);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800269
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400270 bool mTraceFirstBuffer;
271
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700272 // Name of Surface consumer
273 String8 mConsumerName;
274
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800275 // Whether consumer assumes MONOTONIC timestamp
276 bool mUseMonoTimestamp;
Shuzhen Wang13a69632016-01-26 09:51:07 -0800277
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800278 /**
Zhijun He125684a2015-12-26 15:07:30 -0800279 * GraphicBuffer manager this stream is registered to. Used to replace the buffer
280 * allocation/deallocation role of BufferQueue.
281 */
282 sp<Camera3BufferManager> mBufferManager;
283
284 /**
Shuzhen Wang0160ddd2019-08-15 09:11:56 -0700285 * Buffer producer listener, used to handle notification when a buffer is released
286 * from consumer side, or a set of buffers are discarded by the consumer.
Zhijun He125684a2015-12-26 15:07:30 -0800287 */
Shuzhen Wang0160ddd2019-08-15 09:11:56 -0700288 sp<BufferProducerListener> mBufferProducerListener;
Zhijun He125684a2015-12-26 15:07:30 -0800289
290 /**
291 * Flag indicating if the buffer manager is used to allocate the stream buffers
292 */
293 bool mUseBufferManager;
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800294
295 /**
296 * Timestamp offset for video and hardware composer consumed streams
297 */
298 nsecs_t mTimestampOffset;
299
Zhijun He125684a2015-12-26 15:07:30 -0800300 /**
Zhijun He5d677d12016-05-29 16:52:39 -0700301 * Consumer end point usage flag set by the constructor for the deferred
302 * consumer case.
303 */
Emilian Peev050f5dc2017-05-18 14:43:56 +0100304 uint64_t mConsumerUsage;
Zhijun He5d677d12016-05-29 16:52:39 -0700305
Chien-Yu Chena936ac22017-10-23 15:59:49 -0700306 // Whether to drop valid buffers.
307 bool mDropBuffers;
308
Yin-Chia Yeh14ef48d2020-02-10 15:06:37 -0800309
310 // Protecting batch states below, must be acquired after mLock
311 std::mutex mBatchLock;
312
313 // The batch size for buffer operation
314 size_t mBatchSize = 1;
315
316 // Prefetched buffers (ready to be handed to client)
317 std::vector<Surface::BatchBuffer> mBatchedBuffers;
318
319 // ---- End of mBatchLock protected scope ----
320
Zhijun He5d677d12016-05-29 16:52:39 -0700321 /**
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800322 * Internal Camera3Stream interface
323 */
Emilian Peevf4816702020-04-03 15:44:51 -0700324 virtual status_t getBufferLocked(camera_stream_buffer *buffer,
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800325 const std::vector<size_t>& surface_ids);
326
Yin-Chia Yeh14ef48d2020-02-10 15:06:37 -0800327 virtual status_t getBuffersLocked(/*out*/std::vector<OutstandingBuffer>* buffers) override;
328
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800329 virtual status_t returnBufferLocked(
Emilian Peevf4816702020-04-03 15:44:51 -0700330 const camera_stream_buffer &buffer,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700331 nsecs_t timestamp, const std::vector<size_t>& surface_ids);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800332
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800333 virtual status_t queueBufferToConsumer(sp<ANativeWindow>& consumer,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700334 ANativeWindowBuffer* buffer, int anwReleaseFence,
335 const std::vector<size_t>& surface_ids);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800336
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800337 virtual status_t configureQueueLocked();
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700338
Emilian Peev050f5dc2017-05-18 14:43:56 +0100339 virtual status_t getEndpointUsage(uint64_t *usage) const;
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700340
Yin-Chia Yeh89954d92017-05-21 17:28:53 -0700341 /**
342 * Private methods
343 */
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700344 void onBuffersRemovedLocked(const std::vector<sp<GraphicBuffer>>&);
Yin-Chia Yeh89954d92017-05-21 17:28:53 -0700345 status_t detachBufferLocked(sp<GraphicBuffer>* buffer, int* fenceFd);
Yin-Chia Yehbf1b8b92019-03-06 14:56:08 -0800346 // Call this after each dequeueBuffer/attachBuffer/detachNextBuffer call to get update on
347 // removed buffers. Set notifyBufferManager to false when the call is initiated by buffer
348 // manager so buffer manager doesn't need to be notified.
349 void checkRemovedBuffersLocked(bool notifyBufferManager = true);
350
351 // Check return status of IGBP calls and set abandoned state accordingly
352 void checkRetAndSetAbandonedLocked(status_t res);
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700353
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -0700354 // If the status indicates abandonded stream, only log when state hasn't been updated to
355 // STATE_ABANDONED
356 static bool shouldLogError(status_t res, StreamState state);
357
Shuzhen Wangabbcb6b2020-12-09 22:32:44 -0800358 // Dump images to disk before returning to consumer
359 void dumpImageToDisk(nsecs_t timestamp, ANativeWindowBuffer* anwBuffer, int fence);
360
Yin-Chia Yeh14ef48d2020-02-10 15:06:37 -0800361 void returnPrefetchedBuffersLocked();
362
Shuzhen Wang686f6442017-06-20 16:16:04 -0700363 static const int32_t kDequeueLatencyBinSize = 5; // in ms
364 CameraLatencyHistogram mDequeueBufferLatency;
365
Shuzhen Wangabbcb6b2020-12-09 22:32:44 -0800366 int mImageDumpMask = 0;
367
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800368}; // class Camera3OutputStream
369
370} // namespace camera3
371
372} // namespace android
373
374#endif