blob: f32053b912c36388822650b55f666232321523c6 [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_STREAM_H
18#define ANDROID_SERVERS_CAMERA3_STREAM_H
19
20#include <gui/Surface.h>
21#include <utils/RefBase.h>
22#include <utils/String8.h>
23#include <utils/String16.h>
Igor Murashkin2fba5842013-04-22 14:03:54 -070024#include <utils/List.h>
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080025
Shuzhen Wang686f6442017-06-20 16:16:04 -070026#include "utils/LatencyHistogram.h"
Igor Murashkin2fba5842013-04-22 14:03:54 -070027#include "Camera3StreamBufferListener.h"
28#include "Camera3StreamInterface.h"
29
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080030namespace android {
31
32namespace camera3 {
33
34/**
35 * A class for managing a single stream of input or output data from the camera
36 * device.
37 *
38 * The stream has an internal state machine to track whether it's
39 * connected/configured/etc.
40 *
41 * States:
42 *
43 * STATE_ERROR: A serious error has occurred, stream is unusable. Outstanding
44 * buffers may still be returned.
45 *
46 * STATE_CONSTRUCTED: The stream is ready for configuration, but buffers cannot
47 * be gotten yet. Not connected to any endpoint, no buffers are registered
48 * with the HAL.
49 *
50 * STATE_IN_CONFIG: Configuration has started, but not yet concluded. During this
Emilian Peevf4816702020-04-03 15:44:51 -070051 * time, the usage, max_buffers, and priv fields of camera_stream returned by
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080052 * startConfiguration() may be modified.
53 *
54 * STATE_IN_RE_CONFIG: Configuration has started, and the stream has been
55 * configured before. Need to track separately from IN_CONFIG to avoid
56 * re-registering buffers with HAL.
57 *
58 * STATE_CONFIGURED: Stream is configured, and has registered buffers with the
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -070059 * HAL (if necessary). The stream's getBuffer/returnBuffer work. The priv
60 * pointer may still be modified.
61 *
62 * STATE_PREPARING: The stream's buffers are being pre-allocated for use. On
63 * older HALs, this is done as part of configuration, but in newer HALs
64 * buffers may be allocated at time of first use. But some use cases require
65 * buffer allocation upfront, to minmize disruption due to lengthy allocation
66 * duration. In this state, only prepareNextBuffer() and cancelPrepare()
67 * may be called.
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080068 *
Emilian Peevac3ce6c2017-12-12 15:27:02 +000069 * STATE_IN_IDLE: This is a temporary state only intended to be used for input
70 * streams and only for the case where we need to re-configure the camera device
71 * while the input stream has an outstanding buffer. All other streams should not
72 * be able to switch to this state. For them this is invalid and should be handled
73 * as an unknown state.
74 *
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080075 * Transition table:
76 *
77 * <none> => STATE_CONSTRUCTED:
78 * When constructed with valid arguments
79 * <none> => STATE_ERROR:
80 * When constructed with invalid arguments
81 * STATE_CONSTRUCTED => STATE_IN_CONFIG:
82 * When startConfiguration() is called
83 * STATE_IN_CONFIG => STATE_CONFIGURED:
84 * When finishConfiguration() is called
85 * STATE_IN_CONFIG => STATE_ERROR:
86 * When finishConfiguration() fails to allocate or register buffers.
87 * STATE_CONFIGURED => STATE_IN_RE_CONFIG: *
88 * When startConfiguration() is called again, after making sure stream is
89 * idle with waitUntilIdle().
90 * STATE_IN_RE_CONFIG => STATE_CONFIGURED:
91 * When finishConfiguration() is called.
92 * STATE_IN_RE_CONFIG => STATE_ERROR:
93 * When finishConfiguration() fails to allocate or register buffers.
94 * STATE_CONFIGURED => STATE_CONSTRUCTED:
95 * When disconnect() is called after making sure stream is idle with
96 * waitUntilIdle().
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -070097 * STATE_CONFIGURED => STATE_PREPARING:
98 * When startPrepare is called before the stream has a buffer
99 * queued back into it for the first time.
100 * STATE_PREPARING => STATE_CONFIGURED:
101 * When sufficient prepareNextBuffer calls have been made to allocate
102 * all stream buffers, or cancelPrepare is called.
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700103 * STATE_CONFIGURED => STATE_ABANDONED:
104 * When the buffer queue of the stream is abandoned.
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000105 * STATE_CONFIGURED => STATE_IN_IDLE:
106 * Only for an input stream which has an outstanding buffer.
107 * STATE_IN_IDLE => STATE_CONFIGURED:
108 * After the internal re-configuration, the input should revert back to
109 * the configured state.
Igor Murashkin13d315e2014-04-03 18:09:04 -0700110 *
111 * Status Tracking:
112 * Each stream is tracked by StatusTracker as a separate component,
113 * depending on the handed out buffer count. The state must be STATE_CONFIGURED
114 * in order for the component to be marked.
115 *
116 * It's marked in one of two ways:
117 *
118 * - ACTIVE: One or more buffers have been handed out (with #getBuffer).
119 * - IDLE: All buffers have been returned (with #returnBuffer), and their
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000120 * respective release_fence(s) have been signaled. The only exception to this
121 * rule is an input stream that moves to "STATE_IN_IDLE" during internal
122 * re-configuration.
Igor Murashkin13d315e2014-04-03 18:09:04 -0700123 *
124 * A typical use case is output streams. When the HAL has any buffers
125 * dequeued, the stream is marked ACTIVE. When the HAL returns all buffers
126 * (e.g. if no capture requests are active), the stream is marked IDLE.
127 * In this use case, the app consumer does not affect the component status.
128 *
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800129 */
130class Camera3Stream :
Emilian Peevf4816702020-04-03 15:44:51 -0700131 protected camera_stream,
Igor Murashkin2fba5842013-04-22 14:03:54 -0700132 public virtual Camera3StreamInterface,
133 public virtual RefBase {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800134 public:
135
136 virtual ~Camera3Stream();
137
Emilian Peevf4816702020-04-03 15:44:51 -0700138 static Camera3Stream* cast(camera_stream *stream);
139 static const Camera3Stream* cast(const camera_stream *stream);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800140
Emilian Peev2295df72021-11-12 18:14:10 -0800141 // Queue corresponding HDR metadata to given native window.
142 static void queueHDRMetadata(buffer_handle_t buffer, sp<ANativeWindow>& anw,
Emilian Peevc81a7592022-02-14 17:38:18 -0800143 int64_t dynamicRangeProfile);
Emilian Peev2295df72021-11-12 18:14:10 -0800144
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800145 /**
146 * Get the stream's ID
147 */
148 int getId() const;
149
150 /**
Zhijun He125684a2015-12-26 15:07:30 -0800151 * Get the output stream set id.
152 */
153 int getStreamSetId() const;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800154 /**
155 * Is this stream part of a multi-resolution stream set
156 */
157 bool isMultiResolution() const;
158 /**
159 * Get the HAL stream group id for a multi-resolution stream set
160 */
161 int getHalStreamGroupId() const;
Zhijun He125684a2015-12-26 15:07:30 -0800162
163 /**
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800164 * Get the stream's dimensions and format
165 */
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800166 uint32_t getWidth() const;
167 uint32_t getHeight() const;
168 int getFormat() const;
169 android_dataspace getDataSpace() const;
Austin Borger9e2b27c2022-07-15 11:27:24 -0700170 int32_t getColorSpace() const;
Emilian Peev050f5dc2017-05-18 14:43:56 +0100171 uint64_t getUsage() const;
172 void setUsage(uint64_t usage);
Emilian Peev710c1422017-08-30 11:19:38 +0100173 void setFormatOverride(bool formatOverriden);
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700174 bool isFormatOverridden() const;
175 int getOriginalFormat() const;
Emilian Peevc81a7592022-02-14 17:38:18 -0800176 int64_t getDynamicRangeProfile() const;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700177 void setDataSpaceOverride(bool dataSpaceOverriden);
178 bool isDataSpaceOverridden() const;
179 android_dataspace getOriginalDataSpace() const;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700180 int getMaxHalBuffers() const;
Shuzhen Wang5c22c152017-12-31 17:12:25 -0800181 const String8& physicalCameraId() const;
Shuzhen Wang8ed1e872022-03-08 16:34:33 -0800182 int64_t getStreamUseCase() const;
Shuzhen Wange4208922022-02-01 16:52:48 -0800183 int getTimestampBase() const;
184 bool isDeviceTimeBaseRealtime() const;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800185
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800186 void setOfflineProcessingSupport(bool) override;
187 bool getOfflineProcessingSupport() const override;
188
Emilian Peevf4816702020-04-03 15:44:51 -0700189 camera_stream* asHalStream() override {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800190 return this;
191 }
192
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800193 /**
194 * Start the stream configuration process. Returns a handle to the stream's
195 * information to be passed into the HAL device's configure_streams call.
196 *
197 * Until finishConfiguration() is called, no other methods on the stream may be
Emilian Peevf4816702020-04-03 15:44:51 -0700198 * called. The usage and max_buffers fields of camera_stream may be modified
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800199 * between start/finishConfiguration, but may not be changed after that.
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800200 *
201 * Returns NULL in case of error starting configuration.
202 */
Emilian Peevf4816702020-04-03 15:44:51 -0700203 camera_stream* startConfiguration();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800204
205 /**
206 * Check if the stream is mid-configuration (start has been called, but not
207 * finish). Used for lazy completion of configuration.
208 */
209 bool isConfiguring() const;
210
211 /**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800212 * Completes the stream configuration process. The stream information
213 * structure returned by startConfiguration() may no longer be modified
214 * after this call, but can still be read until the destruction of the
215 * stream.
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800216 *
Yin-Chia Yeh573a2702019-04-17 10:08:55 -0700217 * streamReconfigured: set to true when a stream is being reconfigured.
218 *
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800219 * Returns:
220 * OK on a successful configuration
221 * NO_INIT in case of a serious error from the HAL device
222 * NO_MEMORY in case of an error registering buffers
Zhijun He5d677d12016-05-29 16:52:39 -0700223 * INVALID_OPERATION in case connecting to the consumer failed or consumer
224 * doesn't exist yet.
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800225 */
Yin-Chia Yeh573a2702019-04-17 10:08:55 -0700226 status_t finishConfiguration(/*out*/bool* streamReconfigured = nullptr);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800227
228 /**
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700229 * Cancels the stream configuration process. This returns the stream to the
230 * initial state, allowing it to be configured again later.
231 * This is done if the HAL rejects the proposed combined stream configuration
232 */
233 status_t cancelConfiguration();
234
235 /**
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700236 * Determine whether the stream has already become in-use (has received
237 * a valid filled buffer), which determines if a stream can still have
238 * prepareNextBuffer called on it.
239 */
240 bool isUnpreparable();
241
242 /**
Emilian Peevf0348ae2021-01-13 13:39:45 -0800243 * Mark the stream as unpreparable.
244 */
245 void markUnpreparable() override;
246
247 /**
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700248 * Start stream preparation. May only be called in the CONFIGURED state,
Ruben Brunkc78ac262015-08-13 17:58:46 -0700249 * when no valid buffers have yet been returned to this stream. Prepares
250 * up to maxCount buffers, or the maximum number of buffers needed by the
251 * pipeline if maxCount is ALLOCATE_PIPELINE_MAX.
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700252 *
253 * If no prepartion is necessary, returns OK and does not transition to
254 * PREPARING state. Otherwise, returns NOT_ENOUGH_DATA and transitions
255 * to PREPARING.
256 *
257 * This call performs no allocation, so is quick to call.
258 *
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700259 * blockRequest specifies whether prepare will block upcoming capture
260 * request. This flag should only be set to false if the caller guarantees
261 * the whole buffer preparation process is done before capture request
262 * comes in.
263 *
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700264 * Returns:
265 * OK if no more buffers need to be preallocated
266 * NOT_ENOUGH_DATA if calls to prepareNextBuffer are needed to finish
267 * buffer pre-allocation, and transitions to the PREPARING state.
268 * NO_INIT in case of a serious error from the HAL device
269 * INVALID_OPERATION if called when not in CONFIGURED state, or a
270 * valid buffer has already been returned to this stream.
271 */
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700272 status_t startPrepare(int maxCount, bool blockRequest);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700273
274 /**
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700275 * Check if the request on a stream is blocked by prepare.
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700276 */
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700277 bool isBlockedByPrepare() const;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700278
279 /**
280 * Continue stream buffer preparation by allocating the next
281 * buffer for this stream. May only be called in the PREPARED state.
282 *
283 * Returns OK and transitions to the CONFIGURED state if all buffers
284 * are allocated after the call concludes. Otherwise returns NOT_ENOUGH_DATA.
285 *
286 * This call allocates one buffer, which may take several milliseconds for
287 * large buffers.
288 *
289 * Returns:
290 * OK if no more buffers need to be preallocated, and transitions
291 * to the CONFIGURED state.
292 * NOT_ENOUGH_DATA if more calls to prepareNextBuffer are needed to finish
293 * buffer pre-allocation.
294 * NO_INIT in case of a serious error from the HAL device
295 * INVALID_OPERATION if called when not in CONFIGURED state, or a
296 * valid buffer has already been returned to this stream.
297 */
298 status_t prepareNextBuffer();
299
300 /**
301 * Cancel stream preparation early. In case allocation needs to be
302 * stopped, this method transitions the stream back to the CONFIGURED state.
303 * Buffers that have been allocated with prepareNextBuffer remain that way,
304 * but a later use of prepareNextBuffer will require just as many
305 * calls as if the earlier prepare attempt had not existed.
306 *
307 * Returns:
308 * OK if cancellation succeeded, and transitions to the CONFIGURED state
309 * INVALID_OPERATION if not in the PREPARING state
310 * NO_INIT in case of a serious error from the HAL device
311 */
312 status_t cancelPrepare();
313
314 /**
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700315 * Tear down memory for this stream. This frees all unused gralloc buffers
316 * allocated for this stream, but leaves it ready for operation afterward.
317 *
318 * May only be called in the CONFIGURED state, and keeps the stream in
319 * the CONFIGURED state.
320 *
321 * Returns:
322 * OK if teardown succeeded.
323 * INVALID_OPERATION if not in the CONFIGURED state
324 * NO_INIT in case of a serious error from the HAL device
325 */
326 status_t tearDown();
327
328 /**
Emilian Peevf4816702020-04-03 15:44:51 -0700329 * Fill in the camera_stream_buffer with the next valid buffer for this
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800330 * stream, to hand over to the HAL.
331 *
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800332 * Multiple surfaces could share the same HAL stream, but a request may
333 * be only for a subset of surfaces. In this case, the
334 * Camera3StreamInterface object needs the surface ID information to acquire
335 * buffers for those surfaces.
336 *
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800337 * This method may only be called once finishConfiguration has been called.
338 * For bidirectional streams, this method applies to the output-side
339 * buffers.
340 *
341 */
Emilian Peevf4816702020-04-03 15:44:51 -0700342 status_t getBuffer(camera_stream_buffer *buffer,
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -0700343 nsecs_t waitBufferTimeout,
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800344 const std::vector<size_t>& surface_ids = std::vector<size_t>());
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800345
346 /**
Yin-Chia Yeh14ef48d2020-02-10 15:06:37 -0800347 * Similar to getBuffer() except this method fills multiple buffers.
348 */
349 status_t getBuffers(std::vector<OutstandingBuffer>* buffers,
350 nsecs_t waitBufferTimeout);
351
352 /**
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800353 * Return a buffer to the stream after use by the HAL.
354 *
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700355 * Multiple surfaces could share the same HAL stream, but a request may
356 * be only for a subset of surfaces. In this case, the
357 * Camera3StreamInterface object needs the surface ID information to attach
358 * buffers for those surfaces.
359 *
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800360 * This method may only be called for buffers provided by getBuffer().
361 * For bidirectional streams, this method applies to the output-side buffers
362 */
Emilian Peevf4816702020-04-03 15:44:51 -0700363 status_t returnBuffer(const camera_stream_buffer &buffer,
Shuzhen Wang90708ea2021-11-04 11:40:49 -0700364 nsecs_t timestamp, nsecs_t readoutTimestamp, bool timestampIncreasing,
Emilian Peev538c90e2018-12-17 18:03:19 +0000365 const std::vector<size_t>& surface_ids = std::vector<size_t>(),
Emilian Peev5104fe92021-10-21 14:27:09 -0700366 uint64_t frameNumber = 0, int32_t transform = -1);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800367
368 /**
Emilian Peevf4816702020-04-03 15:44:51 -0700369 * Fill in the camera_stream_buffer with the next valid buffer for this
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700370 * stream, to hand over to the HAL.
371 *
372 * This method may only be called once finishConfiguration has been called.
373 * For bidirectional streams, this method applies to the input-side
374 * buffers.
375 *
Shuzhen Wang83bff122020-11-20 15:51:39 -0800376 * This method also returns the size of the returned input buffer.
377 *
Eino-Ville Talvalaba435252017-06-21 16:07:25 -0700378 * Normally this call will block until the handed out buffer count is less than the stream
379 * max buffer count; if respectHalLimit is set to false, this is ignored.
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700380 */
Shuzhen Wang83bff122020-11-20 15:51:39 -0800381 status_t getInputBuffer(camera_stream_buffer *buffer,
382 Size* size, bool respectHalLimit = true);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700383
384 /**
385 * Return a buffer to the stream after use by the HAL.
386 *
387 * This method may only be called for buffers provided by getBuffer().
388 * For bidirectional streams, this method applies to the input-side buffers
389 */
Emilian Peevf4816702020-04-03 15:44:51 -0700390 status_t returnInputBuffer(const camera_stream_buffer &buffer);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700391
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700392 // get the buffer producer of the input buffer queue.
393 // only apply to input streams.
394 status_t getInputBufferProducer(sp<IGraphicBufferProducer> *producer);
395
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700396 /**
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800397 * Whether any of the stream's buffers are currently in use by the HAL,
398 * including buffers that have been returned but not yet had their
399 * release fence signaled.
400 */
401 bool hasOutstandingBuffers() const;
402
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -0700403 /**
404 * Get number of buffers currently handed out to HAL
405 */
406 size_t getOutstandingBuffersCount() const;
407
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800408 enum {
409 TIMEOUT_NEVER = -1
410 };
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700411
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800412 /**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700413 * Set the status tracker to notify about idle transitions
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800414 */
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700415 virtual status_t setStatusTracker(sp<StatusTracker> statusTracker);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800416
417 /**
418 * Disconnect stream from its non-HAL endpoint. After this,
419 * start/finishConfiguration must be called before the stream can be used
420 * again. This cannot be called if the stream has outstanding dequeued
421 * buffers.
422 */
423 status_t disconnect();
424
425 /**
426 * Debug dump of the stream's state.
427 */
Shuzhen Wang686f6442017-06-20 16:16:04 -0700428 virtual void dump(int fd, const Vector<String16> &args) const;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800429
Zhijun Hef0d962a2014-06-30 10:24:11 -0700430 /**
431 * Add a camera3 buffer listener. Adding the same listener twice has
432 * no effect.
433 */
Igor Murashkin2fba5842013-04-22 14:03:54 -0700434 void addBufferListener(
435 wp<Camera3StreamBufferListener> listener);
Zhijun Hef0d962a2014-06-30 10:24:11 -0700436
437 /**
438 * Remove a camera3 buffer listener. Removing the same listener twice
439 * or the listener that was never added has no effect.
440 */
Igor Murashkin2fba5842013-04-22 14:03:54 -0700441 void removeBufferListener(
442 const sp<Camera3StreamBufferListener>& listener);
443
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700444
445 // Setting listener will remove previous listener (if exists)
446 virtual void setBufferFreedListener(
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -0700447 wp<Camera3StreamBufferFreedListener> listener) override;
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700448
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700449 /**
450 * Return if the buffer queue of the stream is abandoned.
451 */
452 bool isAbandoned() const;
453
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000454 /**
455 * Switch a configured stream with possibly outstanding buffers in idle
456 * state. Configuration for such streams will be skipped assuming there
457 * are no changes to the stream parameters.
458 */
459 status_t forceToIdle();
460
461 /**
462 * Restore a forced idle stream to configured state, marking it active
463 * in case it contains outstanding buffers.
464 */
465 status_t restoreConfiguredState();
466
Emilian Peev538c90e2018-12-17 18:03:19 +0000467 /**
468 * Notify buffer stream listeners about incoming request with particular frame number.
469 */
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800470 void fireBufferRequestForFrameNumber(uint64_t frameNumber,
471 const CameraMetadata& settings) override;
Emilian Peev538c90e2018-12-17 18:03:19 +0000472
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800473 protected:
474 const int mId;
Zhijun He125684a2015-12-26 15:07:30 -0800475 /**
476 * Stream set id, used to indicate which group of this stream belongs to for buffer sharing
477 * across multiple streams.
478 *
479 * The default value is set to CAMERA3_STREAM_SET_ID_INVALID, which indicates that this stream
480 * doesn't intend to share buffers with any other streams, and this stream will fall back to
481 * the existing BufferQueue mechanism to manage the buffer allocations and buffer circulation.
482 * When a valid stream set id is set, this stream intends to use the Camera3BufferManager to
483 * manage the buffer allocations; the BufferQueue will only handle the buffer transaction
484 * between the producer and consumer. For this case, upon successfully registration, the streams
485 * with the same stream set id will potentially share the buffers allocated by
486 * Camera3BufferManager.
487 */
488 const int mSetId;
489
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800490 const String8 mName;
491 // Zero for formats with fixed buffer size for given dimensions.
492 const size_t mMaxSize;
493
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -0700494 enum StreamState {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800495 STATE_ERROR,
496 STATE_CONSTRUCTED,
497 STATE_IN_CONFIG,
498 STATE_IN_RECONFIG,
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700499 STATE_CONFIGURED,
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700500 STATE_PREPARING,
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000501 STATE_ABANDONED,
502 STATE_IN_IDLE
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800503 } mState;
504
505 mutable Mutex mLock;
506
Emilian Peevf4816702020-04-03 15:44:51 -0700507 Camera3Stream(int id, camera_stream_type type,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800508 uint32_t width, uint32_t height, size_t maxSize, int format,
Emilian Peevf4816702020-04-03 15:44:51 -0700509 android_dataspace dataSpace, camera_stream_rotation_t rotation,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800510 const String8& physicalCameraId,
511 const std::unordered_set<int32_t> &sensorPixelModesUsed,
Emilian Peevc81a7592022-02-14 17:38:18 -0800512 int setId, bool isMultiResolution, int64_t dynamicRangeProfile,
Austin Borger9e2b27c2022-07-15 11:27:24 -0700513 int64_t streamUseCase, bool deviceTimeBaseIsRealtime, int timestampBase,
514 int32_t colorSpace);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800515
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -0700516 wp<Camera3StreamBufferFreedListener> mBufferFreedListener;
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700517
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800518 /**
519 * Interface to be implemented by derived classes
520 */
521
522 // getBuffer / returnBuffer implementations
523
Emilian Peevf4816702020-04-03 15:44:51 -0700524 // Since camera_stream_buffer includes a raw pointer to the stream,
525 // cast to camera_stream*, implementations must increment the
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800526 // refcount of the stream manually in getBufferLocked, and decrement it in
527 // returnBufferLocked.
Emilian Peevf4816702020-04-03 15:44:51 -0700528 virtual status_t getBufferLocked(camera_stream_buffer *buffer,
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800529 const std::vector<size_t>& surface_ids = std::vector<size_t>());
Emilian Peevf4816702020-04-03 15:44:51 -0700530 virtual status_t returnBufferLocked(const camera_stream_buffer &buffer,
Shuzhen Wang90708ea2021-11-04 11:40:49 -0700531 nsecs_t timestamp, nsecs_t readoutTimestamp, int32_t transform,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700532 const std::vector<size_t>& surface_ids = std::vector<size_t>());
Yin-Chia Yeh14ef48d2020-02-10 15:06:37 -0800533
534 virtual status_t getBuffersLocked(std::vector<OutstandingBuffer>*);
535
Shuzhen Wang83bff122020-11-20 15:51:39 -0800536 virtual status_t getInputBufferLocked(camera_stream_buffer *buffer, Size* size);
Yin-Chia Yeh14ef48d2020-02-10 15:06:37 -0800537
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700538 virtual status_t returnInputBufferLocked(
Emilian Peevf4816702020-04-03 15:44:51 -0700539 const camera_stream_buffer &buffer);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800540 virtual bool hasOutstandingBuffersLocked() const = 0;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700541 // Get the buffer producer of the input buffer queue. Only apply to input streams.
542 virtual status_t getInputBufferProducerLocked(sp<IGraphicBufferProducer> *producer);
543
Igor Murashkine2172be2013-05-28 15:31:39 -0700544 // Can return -ENOTCONN when we are already disconnected (not an error)
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800545 virtual status_t disconnectLocked() = 0;
546
547 // Configure the buffer queue interface to the other end of the stream,
548 // after the HAL has provided usage and max_buffers values. After this call,
549 // the stream must be ready to produce all buffers for registration with
550 // HAL.
Shuzhen Wang210ba5c2018-07-25 16:47:40 -0700551 // Returns NO_INIT or DEAD_OBJECT if the queue has been abandoned.
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800552 virtual status_t configureQueueLocked() = 0;
553
554 // Get the total number of buffers in the queue
555 virtual size_t getBufferCountLocked() = 0;
556
Zhijun He6adc9cc2014-04-15 14:09:55 -0700557 // Get handout output buffer count.
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -0700558 virtual size_t getHandoutOutputBufferCountLocked() const = 0;
Zhijun He6adc9cc2014-04-15 14:09:55 -0700559
560 // Get handout input buffer count.
561 virtual size_t getHandoutInputBufferCountLocked() = 0;
562
Shuzhen Wangc2352702022-09-06 18:36:31 -0700563 // Get cached output buffer count.
564 virtual size_t getCachedOutputBufferCountLocked() const = 0;
565 virtual size_t getMaxCachedOutputBuffersLocked() const = 0;
566
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700567 // Get the usage flags for the other endpoint, or return
568 // INVALID_OPERATION if they cannot be obtained.
Emilian Peev050f5dc2017-05-18 14:43:56 +0100569 virtual status_t getEndpointUsage(uint64_t *usage) const = 0;
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700570
Emilian Peev889234d2017-07-18 18:21:26 -0700571 // Return whether the buffer is in the list of outstanding buffers.
Emilian Peevf4816702020-04-03 15:44:51 -0700572 bool isOutstandingBuffer(const camera_stream_buffer& buffer) const;
Emilian Peev889234d2017-07-18 18:21:26 -0700573
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700574 // Tracking for idle state
575 wp<StatusTracker> mStatusTracker;
576 // Status tracker component ID
577 int mStatusId;
578
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700579 // Tracking for stream prepare - whether this stream can still have
580 // prepareNextBuffer called on it.
581 bool mStreamUnpreparable;
582
Emilian Peev050f5dc2017-05-18 14:43:56 +0100583 uint64_t mUsage;
584
Shuzhen Wangc2352702022-09-06 18:36:31 -0700585 Condition mOutputBufferReturnedSignal;
586
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800587 private:
Shuzhen Wangf9b4eb92019-06-10 11:06:01 -0700588 // Previously configured stream properties (post HAL override)
Emilian Peev050f5dc2017-05-18 14:43:56 +0100589 uint64_t mOldUsage;
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800590 uint32_t mOldMaxBuffers;
Shuzhen Wangf9b4eb92019-06-10 11:06:01 -0700591 int mOldFormat;
592 android_dataspace mOldDataSpace;
593
Zhijun He6adc9cc2014-04-15 14:09:55 -0700594 Condition mInputBufferReturnedSignal;
595 static const nsecs_t kWaitForBufferDuration = 3000000000LL; // 3000 ms
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800596
Emilian Peevf4816702020-04-03 15:44:51 -0700597 void fireBufferListenersLocked(const camera_stream_buffer& buffer,
Emilian Peev538c90e2018-12-17 18:03:19 +0000598 bool acquired, bool output, nsecs_t timestamp = 0, uint64_t frameNumber = 0);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700599 List<wp<Camera3StreamBufferListener> > mBufferListenerList;
600
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700601 status_t cancelPrepareLocked();
602
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700603 // Remove the buffer from the list of outstanding buffers.
Emilian Peevf4816702020-04-03 15:44:51 -0700604 void removeOutstandingBuffer(const camera_stream_buffer& buffer);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700605
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700606 // Tracking for PREPARING state
607
608 // State of buffer preallocation. Only true if either prepareNextBuffer
609 // has been called sufficient number of times, or stream configuration
610 // had to register buffers with the HAL
611 bool mPrepared;
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700612 bool mPrepareBlockRequest;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700613
Emilian Peevf4816702020-04-03 15:44:51 -0700614 Vector<camera_stream_buffer_t> mPreparedBuffers;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700615 size_t mPreparedBufferIdx;
616
Ruben Brunkc78ac262015-08-13 17:58:46 -0700617 // Number of buffers allocated on last prepare call.
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800618 size_t mLastMaxCount;
Ruben Brunkc78ac262015-08-13 17:58:46 -0700619
Emilian Peev889234d2017-07-18 18:21:26 -0700620 mutable Mutex mOutstandingBuffersLock;
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700621 // Outstanding buffers dequeued from the stream's buffer queue.
622 List<buffer_handle_t> mOutstandingBuffers;
623
Shuzhen Wang686f6442017-06-20 16:16:04 -0700624 // Latency histogram of the wait time for handout buffer count to drop below
625 // max_buffers.
626 static const int32_t kBufferLimitLatencyBinSize = 33; //in ms
627 CameraLatencyHistogram mBufferLimitLatency;
Emilian Peev710c1422017-08-30 11:19:38 +0100628
Yin-Chia Yeh90667662019-07-01 15:45:00 -0700629 //Keep track of original format when the stream is created in case it gets overridden
Emilian Peev710c1422017-08-30 11:19:38 +0100630 bool mFormatOverridden;
Yin-Chia Yeh90667662019-07-01 15:45:00 -0700631 const int mOriginalFormat;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700632
633 //Keep track of original dataSpace in case it gets overridden
634 bool mDataSpaceOverridden;
Shuzhen Wang2f5010d2019-08-22 12:41:12 -0700635 const android_dataspace mOriginalDataSpace;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700636
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800637 String8 mPhysicalCameraId;
Shuzhen Wang26abaf42018-08-28 15:41:20 -0700638 nsecs_t mLastTimestamp;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800639
Shuzhen Wang83bff122020-11-20 15:51:39 -0800640 bool mIsMultiResolution = false;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800641 bool mSupportOfflineProcessing = false;
Shuzhen Wange4208922022-02-01 16:52:48 -0800642
643 bool mDeviceTimeBaseIsRealtime;
644 int mTimestampBase;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800645}; // class Camera3Stream
646
647}; // namespace camera3
648
649}; // namespace android
650
651#endif