blob: 3654f891b9ec260aa2789fb161eeb2f24e9162b3 [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
26#include "hardware/camera3.h"
27
Shuzhen Wang686f6442017-06-20 16:16:04 -070028#include "utils/LatencyHistogram.h"
Igor Murashkin2fba5842013-04-22 14:03:54 -070029#include "Camera3StreamBufferListener.h"
30#include "Camera3StreamInterface.h"
31
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080032namespace android {
33
34namespace camera3 {
35
36/**
37 * A class for managing a single stream of input or output data from the camera
38 * device.
39 *
40 * The stream has an internal state machine to track whether it's
41 * connected/configured/etc.
42 *
43 * States:
44 *
45 * STATE_ERROR: A serious error has occurred, stream is unusable. Outstanding
46 * buffers may still be returned.
47 *
48 * STATE_CONSTRUCTED: The stream is ready for configuration, but buffers cannot
49 * be gotten yet. Not connected to any endpoint, no buffers are registered
50 * with the HAL.
51 *
52 * STATE_IN_CONFIG: Configuration has started, but not yet concluded. During this
53 * time, the usage, max_buffers, and priv fields of camera3_stream returned by
54 * startConfiguration() may be modified.
55 *
56 * STATE_IN_RE_CONFIG: Configuration has started, and the stream has been
57 * configured before. Need to track separately from IN_CONFIG to avoid
58 * re-registering buffers with HAL.
59 *
60 * STATE_CONFIGURED: Stream is configured, and has registered buffers with the
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -070061 * HAL (if necessary). The stream's getBuffer/returnBuffer work. The priv
62 * pointer may still be modified.
63 *
64 * STATE_PREPARING: The stream's buffers are being pre-allocated for use. On
65 * older HALs, this is done as part of configuration, but in newer HALs
66 * buffers may be allocated at time of first use. But some use cases require
67 * buffer allocation upfront, to minmize disruption due to lengthy allocation
68 * duration. In this state, only prepareNextBuffer() and cancelPrepare()
69 * may be called.
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080070 *
Emilian Peevac3ce6c2017-12-12 15:27:02 +000071 * STATE_IN_IDLE: This is a temporary state only intended to be used for input
72 * streams and only for the case where we need to re-configure the camera device
73 * while the input stream has an outstanding buffer. All other streams should not
74 * be able to switch to this state. For them this is invalid and should be handled
75 * as an unknown state.
76 *
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080077 * Transition table:
78 *
79 * <none> => STATE_CONSTRUCTED:
80 * When constructed with valid arguments
81 * <none> => STATE_ERROR:
82 * When constructed with invalid arguments
83 * STATE_CONSTRUCTED => STATE_IN_CONFIG:
84 * When startConfiguration() is called
85 * STATE_IN_CONFIG => STATE_CONFIGURED:
86 * When finishConfiguration() is called
87 * STATE_IN_CONFIG => STATE_ERROR:
88 * When finishConfiguration() fails to allocate or register buffers.
89 * STATE_CONFIGURED => STATE_IN_RE_CONFIG: *
90 * When startConfiguration() is called again, after making sure stream is
91 * idle with waitUntilIdle().
92 * STATE_IN_RE_CONFIG => STATE_CONFIGURED:
93 * When finishConfiguration() is called.
94 * STATE_IN_RE_CONFIG => STATE_ERROR:
95 * When finishConfiguration() fails to allocate or register buffers.
96 * STATE_CONFIGURED => STATE_CONSTRUCTED:
97 * When disconnect() is called after making sure stream is idle with
98 * waitUntilIdle().
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -070099 * STATE_CONFIGURED => STATE_PREPARING:
100 * When startPrepare is called before the stream has a buffer
101 * queued back into it for the first time.
102 * STATE_PREPARING => STATE_CONFIGURED:
103 * When sufficient prepareNextBuffer calls have been made to allocate
104 * all stream buffers, or cancelPrepare is called.
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700105 * STATE_CONFIGURED => STATE_ABANDONED:
106 * When the buffer queue of the stream is abandoned.
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000107 * STATE_CONFIGURED => STATE_IN_IDLE:
108 * Only for an input stream which has an outstanding buffer.
109 * STATE_IN_IDLE => STATE_CONFIGURED:
110 * After the internal re-configuration, the input should revert back to
111 * the configured state.
Igor Murashkin13d315e2014-04-03 18:09:04 -0700112 *
113 * Status Tracking:
114 * Each stream is tracked by StatusTracker as a separate component,
115 * depending on the handed out buffer count. The state must be STATE_CONFIGURED
116 * in order for the component to be marked.
117 *
118 * It's marked in one of two ways:
119 *
120 * - ACTIVE: One or more buffers have been handed out (with #getBuffer).
121 * - IDLE: All buffers have been returned (with #returnBuffer), and their
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000122 * respective release_fence(s) have been signaled. The only exception to this
123 * rule is an input stream that moves to "STATE_IN_IDLE" during internal
124 * re-configuration.
Igor Murashkin13d315e2014-04-03 18:09:04 -0700125 *
126 * A typical use case is output streams. When the HAL has any buffers
127 * dequeued, the stream is marked ACTIVE. When the HAL returns all buffers
128 * (e.g. if no capture requests are active), the stream is marked IDLE.
129 * In this use case, the app consumer does not affect the component status.
130 *
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800131 */
132class Camera3Stream :
133 protected camera3_stream,
Igor Murashkin2fba5842013-04-22 14:03:54 -0700134 public virtual Camera3StreamInterface,
135 public virtual RefBase {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800136 public:
137
138 virtual ~Camera3Stream();
139
140 static Camera3Stream* cast(camera3_stream *stream);
141 static const Camera3Stream* cast(const camera3_stream *stream);
142
143 /**
144 * Get the stream's ID
145 */
146 int getId() const;
147
148 /**
Zhijun He125684a2015-12-26 15:07:30 -0800149 * Get the output stream set id.
150 */
151 int getStreamSetId() const;
152
153 /**
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800154 * Get the stream's dimensions and format
155 */
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800156 uint32_t getWidth() const;
157 uint32_t getHeight() const;
158 int getFormat() const;
159 android_dataspace getDataSpace() const;
Emilian Peev050f5dc2017-05-18 14:43:56 +0100160 uint64_t getUsage() const;
161 void setUsage(uint64_t usage);
Emilian Peev710c1422017-08-30 11:19:38 +0100162 void setFormatOverride(bool formatOverriden);
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700163 bool isFormatOverridden() const;
164 int getOriginalFormat() const;
165 void setDataSpaceOverride(bool dataSpaceOverriden);
166 bool isDataSpaceOverridden() const;
167 android_dataspace getOriginalDataSpace() const;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700168 int getMaxHalBuffers() const;
Shuzhen Wang5c22c152017-12-31 17:12:25 -0800169 const String8& physicalCameraId() const;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800170
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800171 void setOfflineProcessingSupport(bool) override;
172 bool getOfflineProcessingSupport() const override;
173
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800174 camera3_stream* asHalStream() override {
175 return this;
176 }
177
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800178 /**
179 * Start the stream configuration process. Returns a handle to the stream's
180 * information to be passed into the HAL device's configure_streams call.
181 *
182 * Until finishConfiguration() is called, no other methods on the stream may be
183 * called. The usage and max_buffers fields of camera3_stream may be modified
184 * between start/finishConfiguration, but may not be changed after that.
185 * The priv field of camera3_stream may be modified at any time after
186 * startConfiguration.
187 *
188 * Returns NULL in case of error starting configuration.
189 */
190 camera3_stream* startConfiguration();
191
192 /**
193 * Check if the stream is mid-configuration (start has been called, but not
194 * finish). Used for lazy completion of configuration.
195 */
196 bool isConfiguring() const;
197
198 /**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800199 * Completes the stream configuration process. The stream information
200 * structure returned by startConfiguration() may no longer be modified
201 * after this call, but can still be read until the destruction of the
202 * stream.
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800203 *
Yin-Chia Yeh573a2702019-04-17 10:08:55 -0700204 * streamReconfigured: set to true when a stream is being reconfigured.
205 *
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800206 * Returns:
207 * OK on a successful configuration
208 * NO_INIT in case of a serious error from the HAL device
209 * NO_MEMORY in case of an error registering buffers
Zhijun He5d677d12016-05-29 16:52:39 -0700210 * INVALID_OPERATION in case connecting to the consumer failed or consumer
211 * doesn't exist yet.
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800212 */
Yin-Chia Yeh573a2702019-04-17 10:08:55 -0700213 status_t finishConfiguration(/*out*/bool* streamReconfigured = nullptr);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800214
215 /**
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700216 * Cancels the stream configuration process. This returns the stream to the
217 * initial state, allowing it to be configured again later.
218 * This is done if the HAL rejects the proposed combined stream configuration
219 */
220 status_t cancelConfiguration();
221
222 /**
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700223 * Determine whether the stream has already become in-use (has received
224 * a valid filled buffer), which determines if a stream can still have
225 * prepareNextBuffer called on it.
226 */
227 bool isUnpreparable();
228
229 /**
230 * Start stream preparation. May only be called in the CONFIGURED state,
Ruben Brunkc78ac262015-08-13 17:58:46 -0700231 * when no valid buffers have yet been returned to this stream. Prepares
232 * up to maxCount buffers, or the maximum number of buffers needed by the
233 * pipeline if maxCount is ALLOCATE_PIPELINE_MAX.
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700234 *
235 * If no prepartion is necessary, returns OK and does not transition to
236 * PREPARING state. Otherwise, returns NOT_ENOUGH_DATA and transitions
237 * to PREPARING.
238 *
239 * This call performs no allocation, so is quick to call.
240 *
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700241 * blockRequest specifies whether prepare will block upcoming capture
242 * request. This flag should only be set to false if the caller guarantees
243 * the whole buffer preparation process is done before capture request
244 * comes in.
245 *
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700246 * Returns:
247 * OK if no more buffers need to be preallocated
248 * NOT_ENOUGH_DATA if calls to prepareNextBuffer are needed to finish
249 * buffer pre-allocation, and transitions to the PREPARING state.
250 * NO_INIT in case of a serious error from the HAL device
251 * INVALID_OPERATION if called when not in CONFIGURED state, or a
252 * valid buffer has already been returned to this stream.
253 */
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700254 status_t startPrepare(int maxCount, bool blockRequest);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700255
256 /**
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700257 * Check if the request on a stream is blocked by prepare.
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700258 */
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700259 bool isBlockedByPrepare() const;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700260
261 /**
262 * Continue stream buffer preparation by allocating the next
263 * buffer for this stream. May only be called in the PREPARED state.
264 *
265 * Returns OK and transitions to the CONFIGURED state if all buffers
266 * are allocated after the call concludes. Otherwise returns NOT_ENOUGH_DATA.
267 *
268 * This call allocates one buffer, which may take several milliseconds for
269 * large buffers.
270 *
271 * Returns:
272 * OK if no more buffers need to be preallocated, and transitions
273 * to the CONFIGURED state.
274 * NOT_ENOUGH_DATA if more calls to prepareNextBuffer are needed to finish
275 * buffer pre-allocation.
276 * NO_INIT in case of a serious error from the HAL device
277 * INVALID_OPERATION if called when not in CONFIGURED state, or a
278 * valid buffer has already been returned to this stream.
279 */
280 status_t prepareNextBuffer();
281
282 /**
283 * Cancel stream preparation early. In case allocation needs to be
284 * stopped, this method transitions the stream back to the CONFIGURED state.
285 * Buffers that have been allocated with prepareNextBuffer remain that way,
286 * but a later use of prepareNextBuffer will require just as many
287 * calls as if the earlier prepare attempt had not existed.
288 *
289 * Returns:
290 * OK if cancellation succeeded, and transitions to the CONFIGURED state
291 * INVALID_OPERATION if not in the PREPARING state
292 * NO_INIT in case of a serious error from the HAL device
293 */
294 status_t cancelPrepare();
295
296 /**
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700297 * Tear down memory for this stream. This frees all unused gralloc buffers
298 * allocated for this stream, but leaves it ready for operation afterward.
299 *
300 * May only be called in the CONFIGURED state, and keeps the stream in
301 * the CONFIGURED state.
302 *
303 * Returns:
304 * OK if teardown succeeded.
305 * INVALID_OPERATION if not in the CONFIGURED state
306 * NO_INIT in case of a serious error from the HAL device
307 */
308 status_t tearDown();
309
310 /**
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800311 * Fill in the camera3_stream_buffer with the next valid buffer for this
312 * stream, to hand over to the HAL.
313 *
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800314 * Multiple surfaces could share the same HAL stream, but a request may
315 * be only for a subset of surfaces. In this case, the
316 * Camera3StreamInterface object needs the surface ID information to acquire
317 * buffers for those surfaces.
318 *
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800319 * This method may only be called once finishConfiguration has been called.
320 * For bidirectional streams, this method applies to the output-side
321 * buffers.
322 *
323 */
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800324 status_t getBuffer(camera3_stream_buffer *buffer,
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -0700325 nsecs_t waitBufferTimeout,
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800326 const std::vector<size_t>& surface_ids = std::vector<size_t>());
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800327
328 /**
329 * Return a buffer to the stream after use by the HAL.
330 *
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700331 * Multiple surfaces could share the same HAL stream, but a request may
332 * be only for a subset of surfaces. In this case, the
333 * Camera3StreamInterface object needs the surface ID information to attach
334 * buffers for those surfaces.
335 *
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800336 * This method may only be called for buffers provided by getBuffer().
337 * For bidirectional streams, this method applies to the output-side buffers
338 */
339 status_t returnBuffer(const camera3_stream_buffer &buffer,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700340 nsecs_t timestamp, bool timestampIncreasing,
Emilian Peev538c90e2018-12-17 18:03:19 +0000341 const std::vector<size_t>& surface_ids = std::vector<size_t>(),
342 uint64_t frameNumber = 0);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800343
344 /**
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700345 * Fill in the camera3_stream_buffer with the next valid buffer for this
346 * stream, to hand over to the HAL.
347 *
348 * This method may only be called once finishConfiguration has been called.
349 * For bidirectional streams, this method applies to the input-side
350 * buffers.
351 *
Eino-Ville Talvalaba435252017-06-21 16:07:25 -0700352 * Normally this call will block until the handed out buffer count is less than the stream
353 * max buffer count; if respectHalLimit is set to false, this is ignored.
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700354 */
Eino-Ville Talvalaba435252017-06-21 16:07:25 -0700355 status_t getInputBuffer(camera3_stream_buffer *buffer, bool respectHalLimit = true);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700356
357 /**
358 * Return a buffer to the stream after use by the HAL.
359 *
360 * This method may only be called for buffers provided by getBuffer().
361 * For bidirectional streams, this method applies to the input-side buffers
362 */
363 status_t returnInputBuffer(const camera3_stream_buffer &buffer);
364
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700365 // get the buffer producer of the input buffer queue.
366 // only apply to input streams.
367 status_t getInputBufferProducer(sp<IGraphicBufferProducer> *producer);
368
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700369 /**
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800370 * Whether any of the stream's buffers are currently in use by the HAL,
371 * including buffers that have been returned but not yet had their
372 * release fence signaled.
373 */
374 bool hasOutstandingBuffers() const;
375
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -0700376 /**
377 * Get number of buffers currently handed out to HAL
378 */
379 size_t getOutstandingBuffersCount() const;
380
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800381 enum {
382 TIMEOUT_NEVER = -1
383 };
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700384
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800385 /**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700386 * Set the status tracker to notify about idle transitions
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800387 */
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700388 virtual status_t setStatusTracker(sp<StatusTracker> statusTracker);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800389
390 /**
391 * Disconnect stream from its non-HAL endpoint. After this,
392 * start/finishConfiguration must be called before the stream can be used
393 * again. This cannot be called if the stream has outstanding dequeued
394 * buffers.
395 */
396 status_t disconnect();
397
398 /**
399 * Debug dump of the stream's state.
400 */
Shuzhen Wang686f6442017-06-20 16:16:04 -0700401 virtual void dump(int fd, const Vector<String16> &args) const;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800402
Zhijun Hef0d962a2014-06-30 10:24:11 -0700403 /**
404 * Add a camera3 buffer listener. Adding the same listener twice has
405 * no effect.
406 */
Igor Murashkin2fba5842013-04-22 14:03:54 -0700407 void addBufferListener(
408 wp<Camera3StreamBufferListener> listener);
Zhijun Hef0d962a2014-06-30 10:24:11 -0700409
410 /**
411 * Remove a camera3 buffer listener. Removing the same listener twice
412 * or the listener that was never added has no effect.
413 */
Igor Murashkin2fba5842013-04-22 14:03:54 -0700414 void removeBufferListener(
415 const sp<Camera3StreamBufferListener>& listener);
416
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700417
418 // Setting listener will remove previous listener (if exists)
419 virtual void setBufferFreedListener(
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -0700420 wp<Camera3StreamBufferFreedListener> listener) override;
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700421
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700422 /**
423 * Return if the buffer queue of the stream is abandoned.
424 */
425 bool isAbandoned() const;
426
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000427 /**
428 * Switch a configured stream with possibly outstanding buffers in idle
429 * state. Configuration for such streams will be skipped assuming there
430 * are no changes to the stream parameters.
431 */
432 status_t forceToIdle();
433
434 /**
435 * Restore a forced idle stream to configured state, marking it active
436 * in case it contains outstanding buffers.
437 */
438 status_t restoreConfiguredState();
439
Emilian Peev538c90e2018-12-17 18:03:19 +0000440 /**
441 * Notify buffer stream listeners about incoming request with particular frame number.
442 */
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800443 void fireBufferRequestForFrameNumber(uint64_t frameNumber,
444 const CameraMetadata& settings) override;
Emilian Peev538c90e2018-12-17 18:03:19 +0000445
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800446 protected:
447 const int mId;
Zhijun He125684a2015-12-26 15:07:30 -0800448 /**
449 * Stream set id, used to indicate which group of this stream belongs to for buffer sharing
450 * across multiple streams.
451 *
452 * The default value is set to CAMERA3_STREAM_SET_ID_INVALID, which indicates that this stream
453 * doesn't intend to share buffers with any other streams, and this stream will fall back to
454 * the existing BufferQueue mechanism to manage the buffer allocations and buffer circulation.
455 * When a valid stream set id is set, this stream intends to use the Camera3BufferManager to
456 * manage the buffer allocations; the BufferQueue will only handle the buffer transaction
457 * between the producer and consumer. For this case, upon successfully registration, the streams
458 * with the same stream set id will potentially share the buffers allocated by
459 * Camera3BufferManager.
460 */
461 const int mSetId;
462
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800463 const String8 mName;
464 // Zero for formats with fixed buffer size for given dimensions.
465 const size_t mMaxSize;
466
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -0700467 enum StreamState {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800468 STATE_ERROR,
469 STATE_CONSTRUCTED,
470 STATE_IN_CONFIG,
471 STATE_IN_RECONFIG,
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700472 STATE_CONFIGURED,
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700473 STATE_PREPARING,
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000474 STATE_ABANDONED,
475 STATE_IN_IDLE
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800476 } mState;
477
478 mutable Mutex mLock;
479
480 Camera3Stream(int id, camera3_stream_type type,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800481 uint32_t width, uint32_t height, size_t maxSize, int format,
Zhijun He125684a2015-12-26 15:07:30 -0800482 android_dataspace dataSpace, camera3_stream_rotation_t rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800483 const String8& physicalCameraId, int setId);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800484
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -0700485 wp<Camera3StreamBufferFreedListener> mBufferFreedListener;
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700486
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800487 /**
488 * Interface to be implemented by derived classes
489 */
490
491 // getBuffer / returnBuffer implementations
492
493 // Since camera3_stream_buffer includes a raw pointer to the stream,
494 // cast to camera3_stream*, implementations must increment the
495 // refcount of the stream manually in getBufferLocked, and decrement it in
496 // returnBufferLocked.
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800497 virtual status_t getBufferLocked(camera3_stream_buffer *buffer,
498 const std::vector<size_t>& surface_ids = std::vector<size_t>());
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800499 virtual status_t returnBufferLocked(const camera3_stream_buffer &buffer,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700500 nsecs_t timestamp,
501 const std::vector<size_t>& surface_ids = std::vector<size_t>());
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700502 virtual status_t getInputBufferLocked(camera3_stream_buffer *buffer);
503 virtual status_t returnInputBufferLocked(
504 const camera3_stream_buffer &buffer);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800505 virtual bool hasOutstandingBuffersLocked() const = 0;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700506 // Get the buffer producer of the input buffer queue. Only apply to input streams.
507 virtual status_t getInputBufferProducerLocked(sp<IGraphicBufferProducer> *producer);
508
Igor Murashkine2172be2013-05-28 15:31:39 -0700509 // Can return -ENOTCONN when we are already disconnected (not an error)
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800510 virtual status_t disconnectLocked() = 0;
511
512 // Configure the buffer queue interface to the other end of the stream,
513 // after the HAL has provided usage and max_buffers values. After this call,
514 // the stream must be ready to produce all buffers for registration with
515 // HAL.
Shuzhen Wang210ba5c2018-07-25 16:47:40 -0700516 // Returns NO_INIT or DEAD_OBJECT if the queue has been abandoned.
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800517 virtual status_t configureQueueLocked() = 0;
518
519 // Get the total number of buffers in the queue
520 virtual size_t getBufferCountLocked() = 0;
521
Zhijun He6adc9cc2014-04-15 14:09:55 -0700522 // Get handout output buffer count.
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -0700523 virtual size_t getHandoutOutputBufferCountLocked() const = 0;
Zhijun He6adc9cc2014-04-15 14:09:55 -0700524
525 // Get handout input buffer count.
526 virtual size_t getHandoutInputBufferCountLocked() = 0;
527
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700528 // Get the usage flags for the other endpoint, or return
529 // INVALID_OPERATION if they cannot be obtained.
Emilian Peev050f5dc2017-05-18 14:43:56 +0100530 virtual status_t getEndpointUsage(uint64_t *usage) const = 0;
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700531
Emilian Peev889234d2017-07-18 18:21:26 -0700532 // Return whether the buffer is in the list of outstanding buffers.
533 bool isOutstandingBuffer(const camera3_stream_buffer& buffer) const;
534
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700535 // Tracking for idle state
536 wp<StatusTracker> mStatusTracker;
537 // Status tracker component ID
538 int mStatusId;
539
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700540 // Tracking for stream prepare - whether this stream can still have
541 // prepareNextBuffer called on it.
542 bool mStreamUnpreparable;
543
Emilian Peev050f5dc2017-05-18 14:43:56 +0100544 uint64_t mUsage;
545
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800546 private:
Shuzhen Wangf9b4eb92019-06-10 11:06:01 -0700547 // Previously configured stream properties (post HAL override)
Emilian Peev050f5dc2017-05-18 14:43:56 +0100548 uint64_t mOldUsage;
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800549 uint32_t mOldMaxBuffers;
Shuzhen Wangf9b4eb92019-06-10 11:06:01 -0700550 int mOldFormat;
551 android_dataspace mOldDataSpace;
552
Zhijun He6adc9cc2014-04-15 14:09:55 -0700553 Condition mOutputBufferReturnedSignal;
554 Condition mInputBufferReturnedSignal;
555 static const nsecs_t kWaitForBufferDuration = 3000000000LL; // 3000 ms
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800556
Igor Murashkin2fba5842013-04-22 14:03:54 -0700557 void fireBufferListenersLocked(const camera3_stream_buffer& buffer,
Emilian Peev538c90e2018-12-17 18:03:19 +0000558 bool acquired, bool output, nsecs_t timestamp = 0, uint64_t frameNumber = 0);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700559 List<wp<Camera3StreamBufferListener> > mBufferListenerList;
560
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700561 status_t cancelPrepareLocked();
562
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700563 // Remove the buffer from the list of outstanding buffers.
564 void removeOutstandingBuffer(const camera3_stream_buffer& buffer);
565
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700566 // Tracking for PREPARING state
567
568 // State of buffer preallocation. Only true if either prepareNextBuffer
569 // has been called sufficient number of times, or stream configuration
570 // had to register buffers with the HAL
571 bool mPrepared;
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700572 bool mPrepareBlockRequest;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700573
574 Vector<camera3_stream_buffer_t> mPreparedBuffers;
575 size_t mPreparedBufferIdx;
576
Ruben Brunkc78ac262015-08-13 17:58:46 -0700577 // Number of buffers allocated on last prepare call.
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800578 size_t mLastMaxCount;
Ruben Brunkc78ac262015-08-13 17:58:46 -0700579
Emilian Peev889234d2017-07-18 18:21:26 -0700580 mutable Mutex mOutstandingBuffersLock;
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700581 // Outstanding buffers dequeued from the stream's buffer queue.
582 List<buffer_handle_t> mOutstandingBuffers;
583
Shuzhen Wang686f6442017-06-20 16:16:04 -0700584 // Latency histogram of the wait time for handout buffer count to drop below
585 // max_buffers.
586 static const int32_t kBufferLimitLatencyBinSize = 33; //in ms
587 CameraLatencyHistogram mBufferLimitLatency;
Emilian Peev710c1422017-08-30 11:19:38 +0100588
Yin-Chia Yeh90667662019-07-01 15:45:00 -0700589 //Keep track of original format when the stream is created in case it gets overridden
Emilian Peev710c1422017-08-30 11:19:38 +0100590 bool mFormatOverridden;
Yin-Chia Yeh90667662019-07-01 15:45:00 -0700591 const int mOriginalFormat;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700592
593 //Keep track of original dataSpace in case it gets overridden
594 bool mDataSpaceOverridden;
Shuzhen Wang2f5010d2019-08-22 12:41:12 -0700595 const android_dataspace mOriginalDataSpace;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700596
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800597 String8 mPhysicalCameraId;
Shuzhen Wang26abaf42018-08-28 15:41:20 -0700598 nsecs_t mLastTimestamp;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800599
600 bool mSupportOfflineProcessing = false;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800601}; // class Camera3Stream
602
603}; // namespace camera3
604
605}; // namespace android
606
607#endif