Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 1 | /* |
Shuzhen Wang | c28189a | 2017-11-27 23:05:10 -0800 | [diff] [blame^] | 2 | * Copyright (C) 2013-2018 The Android Open Source Project |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 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_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 Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 24 | #include <utils/List.h> |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 25 | |
| 26 | #include "hardware/camera3.h" |
| 27 | |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 28 | #include "utils/LatencyHistogram.h" |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 29 | #include "Camera3StreamBufferListener.h" |
| 30 | #include "Camera3StreamInterface.h" |
| 31 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 32 | namespace android { |
| 33 | |
| 34 | namespace 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 Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 61 | * 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 Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 70 | * |
Emilian Peev | ac3ce6c | 2017-12-12 15:27:02 +0000 | [diff] [blame] | 71 | * 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 Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 77 | * 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 Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 99 | * 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 Chen | e8c535e | 2016-04-14 12:18:26 -0700 | [diff] [blame] | 105 | * STATE_CONFIGURED => STATE_ABANDONED: |
| 106 | * When the buffer queue of the stream is abandoned. |
Emilian Peev | ac3ce6c | 2017-12-12 15:27:02 +0000 | [diff] [blame] | 107 | * 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 Murashkin | 13d315e | 2014-04-03 18:09:04 -0700 | [diff] [blame] | 112 | * |
| 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 Peev | ac3ce6c | 2017-12-12 15:27:02 +0000 | [diff] [blame] | 122 | * 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 Murashkin | 13d315e | 2014-04-03 18:09:04 -0700 | [diff] [blame] | 125 | * |
| 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 Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 131 | */ |
| 132 | class Camera3Stream : |
| 133 | protected camera3_stream, |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 134 | public virtual Camera3StreamInterface, |
| 135 | public virtual RefBase { |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 136 | 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 He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 149 | * Get the output stream set id. |
| 150 | */ |
| 151 | int getStreamSetId() const; |
| 152 | |
| 153 | /** |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 154 | * Get the stream's dimensions and format |
| 155 | */ |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 156 | uint32_t getWidth() const; |
| 157 | uint32_t getHeight() const; |
| 158 | int getFormat() const; |
| 159 | android_dataspace getDataSpace() const; |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 160 | uint64_t getUsage() const; |
| 161 | void setUsage(uint64_t usage); |
Emilian Peev | 710c142 | 2017-08-30 11:19:38 +0100 | [diff] [blame] | 162 | void setFormatOverride(bool formatOverriden); |
Eino-Ville Talvala | 91cd3f8 | 2017-08-21 16:12:50 -0700 | [diff] [blame] | 163 | bool isFormatOverridden() const; |
| 164 | int getOriginalFormat() const; |
| 165 | void setDataSpaceOverride(bool dataSpaceOverriden); |
| 166 | bool isDataSpaceOverridden() const; |
| 167 | android_dataspace getOriginalDataSpace() const; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 168 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 169 | camera3_stream* asHalStream() override { |
| 170 | return this; |
| 171 | } |
| 172 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 173 | /** |
| 174 | * Start the stream configuration process. Returns a handle to the stream's |
| 175 | * information to be passed into the HAL device's configure_streams call. |
| 176 | * |
| 177 | * Until finishConfiguration() is called, no other methods on the stream may be |
| 178 | * called. The usage and max_buffers fields of camera3_stream may be modified |
| 179 | * between start/finishConfiguration, but may not be changed after that. |
| 180 | * The priv field of camera3_stream may be modified at any time after |
| 181 | * startConfiguration. |
| 182 | * |
| 183 | * Returns NULL in case of error starting configuration. |
| 184 | */ |
| 185 | camera3_stream* startConfiguration(); |
| 186 | |
| 187 | /** |
| 188 | * Check if the stream is mid-configuration (start has been called, but not |
| 189 | * finish). Used for lazy completion of configuration. |
| 190 | */ |
| 191 | bool isConfiguring() const; |
| 192 | |
| 193 | /** |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 194 | * Completes the stream configuration process. The stream information |
| 195 | * structure returned by startConfiguration() may no longer be modified |
| 196 | * after this call, but can still be read until the destruction of the |
| 197 | * stream. |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 198 | * |
| 199 | * Returns: |
| 200 | * OK on a successful configuration |
| 201 | * NO_INIT in case of a serious error from the HAL device |
| 202 | * NO_MEMORY in case of an error registering buffers |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 203 | * INVALID_OPERATION in case connecting to the consumer failed or consumer |
| 204 | * doesn't exist yet. |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 205 | */ |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 206 | status_t finishConfiguration(); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 207 | |
| 208 | /** |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 209 | * Cancels the stream configuration process. This returns the stream to the |
| 210 | * initial state, allowing it to be configured again later. |
| 211 | * This is done if the HAL rejects the proposed combined stream configuration |
| 212 | */ |
| 213 | status_t cancelConfiguration(); |
| 214 | |
| 215 | /** |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 216 | * Determine whether the stream has already become in-use (has received |
| 217 | * a valid filled buffer), which determines if a stream can still have |
| 218 | * prepareNextBuffer called on it. |
| 219 | */ |
| 220 | bool isUnpreparable(); |
| 221 | |
| 222 | /** |
| 223 | * Start stream preparation. May only be called in the CONFIGURED state, |
Ruben Brunk | c78ac26 | 2015-08-13 17:58:46 -0700 | [diff] [blame] | 224 | * when no valid buffers have yet been returned to this stream. Prepares |
| 225 | * up to maxCount buffers, or the maximum number of buffers needed by the |
| 226 | * pipeline if maxCount is ALLOCATE_PIPELINE_MAX. |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 227 | * |
| 228 | * If no prepartion is necessary, returns OK and does not transition to |
| 229 | * PREPARING state. Otherwise, returns NOT_ENOUGH_DATA and transitions |
| 230 | * to PREPARING. |
| 231 | * |
| 232 | * This call performs no allocation, so is quick to call. |
| 233 | * |
| 234 | * Returns: |
| 235 | * OK if no more buffers need to be preallocated |
| 236 | * NOT_ENOUGH_DATA if calls to prepareNextBuffer are needed to finish |
| 237 | * buffer pre-allocation, and transitions to the PREPARING state. |
| 238 | * NO_INIT in case of a serious error from the HAL device |
| 239 | * INVALID_OPERATION if called when not in CONFIGURED state, or a |
| 240 | * valid buffer has already been returned to this stream. |
| 241 | */ |
Ruben Brunk | c78ac26 | 2015-08-13 17:58:46 -0700 | [diff] [blame] | 242 | status_t startPrepare(int maxCount); |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 243 | |
| 244 | /** |
| 245 | * Check if the stream is mid-preparing. |
| 246 | */ |
| 247 | bool isPreparing() const; |
| 248 | |
| 249 | /** |
| 250 | * Continue stream buffer preparation by allocating the next |
| 251 | * buffer for this stream. May only be called in the PREPARED state. |
| 252 | * |
| 253 | * Returns OK and transitions to the CONFIGURED state if all buffers |
| 254 | * are allocated after the call concludes. Otherwise returns NOT_ENOUGH_DATA. |
| 255 | * |
| 256 | * This call allocates one buffer, which may take several milliseconds for |
| 257 | * large buffers. |
| 258 | * |
| 259 | * Returns: |
| 260 | * OK if no more buffers need to be preallocated, and transitions |
| 261 | * to the CONFIGURED state. |
| 262 | * NOT_ENOUGH_DATA if more calls to prepareNextBuffer are needed to finish |
| 263 | * buffer pre-allocation. |
| 264 | * NO_INIT in case of a serious error from the HAL device |
| 265 | * INVALID_OPERATION if called when not in CONFIGURED state, or a |
| 266 | * valid buffer has already been returned to this stream. |
| 267 | */ |
| 268 | status_t prepareNextBuffer(); |
| 269 | |
| 270 | /** |
| 271 | * Cancel stream preparation early. In case allocation needs to be |
| 272 | * stopped, this method transitions the stream back to the CONFIGURED state. |
| 273 | * Buffers that have been allocated with prepareNextBuffer remain that way, |
| 274 | * but a later use of prepareNextBuffer will require just as many |
| 275 | * calls as if the earlier prepare attempt had not existed. |
| 276 | * |
| 277 | * Returns: |
| 278 | * OK if cancellation succeeded, and transitions to the CONFIGURED state |
| 279 | * INVALID_OPERATION if not in the PREPARING state |
| 280 | * NO_INIT in case of a serious error from the HAL device |
| 281 | */ |
| 282 | status_t cancelPrepare(); |
| 283 | |
| 284 | /** |
Eino-Ville Talvala | b25e3c8 | 2015-07-15 16:04:27 -0700 | [diff] [blame] | 285 | * Tear down memory for this stream. This frees all unused gralloc buffers |
| 286 | * allocated for this stream, but leaves it ready for operation afterward. |
| 287 | * |
| 288 | * May only be called in the CONFIGURED state, and keeps the stream in |
| 289 | * the CONFIGURED state. |
| 290 | * |
| 291 | * Returns: |
| 292 | * OK if teardown succeeded. |
| 293 | * INVALID_OPERATION if not in the CONFIGURED state |
| 294 | * NO_INIT in case of a serious error from the HAL device |
| 295 | */ |
| 296 | status_t tearDown(); |
| 297 | |
| 298 | /** |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 299 | * Fill in the camera3_stream_buffer with the next valid buffer for this |
| 300 | * stream, to hand over to the HAL. |
| 301 | * |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 302 | * Multiple surfaces could share the same HAL stream, but a request may |
| 303 | * be only for a subset of surfaces. In this case, the |
| 304 | * Camera3StreamInterface object needs the surface ID information to acquire |
| 305 | * buffers for those surfaces. |
| 306 | * |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 307 | * This method may only be called once finishConfiguration has been called. |
| 308 | * For bidirectional streams, this method applies to the output-side |
| 309 | * buffers. |
| 310 | * |
| 311 | */ |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 312 | status_t getBuffer(camera3_stream_buffer *buffer, |
| 313 | const std::vector<size_t>& surface_ids = std::vector<size_t>()); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 314 | |
| 315 | /** |
| 316 | * Return a buffer to the stream after use by the HAL. |
| 317 | * |
| 318 | * This method may only be called for buffers provided by getBuffer(). |
| 319 | * For bidirectional streams, this method applies to the output-side buffers |
| 320 | */ |
| 321 | status_t returnBuffer(const camera3_stream_buffer &buffer, |
| 322 | nsecs_t timestamp); |
| 323 | |
| 324 | /** |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 325 | * Fill in the camera3_stream_buffer with the next valid buffer for this |
| 326 | * stream, to hand over to the HAL. |
| 327 | * |
| 328 | * This method may only be called once finishConfiguration has been called. |
| 329 | * For bidirectional streams, this method applies to the input-side |
| 330 | * buffers. |
| 331 | * |
Eino-Ville Talvala | ba43525 | 2017-06-21 16:07:25 -0700 | [diff] [blame] | 332 | * Normally this call will block until the handed out buffer count is less than the stream |
| 333 | * max buffer count; if respectHalLimit is set to false, this is ignored. |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 334 | */ |
Eino-Ville Talvala | ba43525 | 2017-06-21 16:07:25 -0700 | [diff] [blame] | 335 | status_t getInputBuffer(camera3_stream_buffer *buffer, bool respectHalLimit = true); |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 336 | |
| 337 | /** |
| 338 | * Return a buffer to the stream after use by the HAL. |
| 339 | * |
| 340 | * This method may only be called for buffers provided by getBuffer(). |
| 341 | * For bidirectional streams, this method applies to the input-side buffers |
| 342 | */ |
| 343 | status_t returnInputBuffer(const camera3_stream_buffer &buffer); |
| 344 | |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 345 | // get the buffer producer of the input buffer queue. |
| 346 | // only apply to input streams. |
| 347 | status_t getInputBufferProducer(sp<IGraphicBufferProducer> *producer); |
| 348 | |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 349 | /** |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 350 | * Whether any of the stream's buffers are currently in use by the HAL, |
| 351 | * including buffers that have been returned but not yet had their |
| 352 | * release fence signaled. |
| 353 | */ |
| 354 | bool hasOutstandingBuffers() const; |
| 355 | |
| 356 | enum { |
| 357 | TIMEOUT_NEVER = -1 |
| 358 | }; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 359 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 360 | /** |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 361 | * Set the status tracker to notify about idle transitions |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 362 | */ |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 363 | virtual status_t setStatusTracker(sp<StatusTracker> statusTracker); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 364 | |
| 365 | /** |
| 366 | * Disconnect stream from its non-HAL endpoint. After this, |
| 367 | * start/finishConfiguration must be called before the stream can be used |
| 368 | * again. This cannot be called if the stream has outstanding dequeued |
| 369 | * buffers. |
| 370 | */ |
| 371 | status_t disconnect(); |
| 372 | |
| 373 | /** |
| 374 | * Debug dump of the stream's state. |
| 375 | */ |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 376 | virtual void dump(int fd, const Vector<String16> &args) const; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 377 | |
Zhijun He | f0d962a | 2014-06-30 10:24:11 -0700 | [diff] [blame] | 378 | /** |
| 379 | * Add a camera3 buffer listener. Adding the same listener twice has |
| 380 | * no effect. |
| 381 | */ |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 382 | void addBufferListener( |
| 383 | wp<Camera3StreamBufferListener> listener); |
Zhijun He | f0d962a | 2014-06-30 10:24:11 -0700 | [diff] [blame] | 384 | |
| 385 | /** |
| 386 | * Remove a camera3 buffer listener. Removing the same listener twice |
| 387 | * or the listener that was never added has no effect. |
| 388 | */ |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 389 | void removeBufferListener( |
| 390 | const sp<Camera3StreamBufferListener>& listener); |
| 391 | |
Yin-Chia Yeh | be83fa7 | 2017-03-30 13:35:36 -0700 | [diff] [blame] | 392 | |
| 393 | // Setting listener will remove previous listener (if exists) |
| 394 | virtual void setBufferFreedListener( |
Yin-Chia Yeh | db1e864 | 2017-07-14 15:19:30 -0700 | [diff] [blame] | 395 | wp<Camera3StreamBufferFreedListener> listener) override; |
Yin-Chia Yeh | be83fa7 | 2017-03-30 13:35:36 -0700 | [diff] [blame] | 396 | |
Chien-Yu Chen | e8c535e | 2016-04-14 12:18:26 -0700 | [diff] [blame] | 397 | /** |
| 398 | * Return if the buffer queue of the stream is abandoned. |
| 399 | */ |
| 400 | bool isAbandoned() const; |
| 401 | |
Emilian Peev | ac3ce6c | 2017-12-12 15:27:02 +0000 | [diff] [blame] | 402 | /** |
| 403 | * Switch a configured stream with possibly outstanding buffers in idle |
| 404 | * state. Configuration for such streams will be skipped assuming there |
| 405 | * are no changes to the stream parameters. |
| 406 | */ |
| 407 | status_t forceToIdle(); |
| 408 | |
| 409 | /** |
| 410 | * Restore a forced idle stream to configured state, marking it active |
| 411 | * in case it contains outstanding buffers. |
| 412 | */ |
| 413 | status_t restoreConfiguredState(); |
| 414 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 415 | protected: |
| 416 | const int mId; |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 417 | /** |
| 418 | * Stream set id, used to indicate which group of this stream belongs to for buffer sharing |
| 419 | * across multiple streams. |
| 420 | * |
| 421 | * The default value is set to CAMERA3_STREAM_SET_ID_INVALID, which indicates that this stream |
| 422 | * doesn't intend to share buffers with any other streams, and this stream will fall back to |
| 423 | * the existing BufferQueue mechanism to manage the buffer allocations and buffer circulation. |
| 424 | * When a valid stream set id is set, this stream intends to use the Camera3BufferManager to |
| 425 | * manage the buffer allocations; the BufferQueue will only handle the buffer transaction |
| 426 | * between the producer and consumer. For this case, upon successfully registration, the streams |
| 427 | * with the same stream set id will potentially share the buffers allocated by |
| 428 | * Camera3BufferManager. |
| 429 | */ |
| 430 | const int mSetId; |
| 431 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 432 | const String8 mName; |
| 433 | // Zero for formats with fixed buffer size for given dimensions. |
| 434 | const size_t mMaxSize; |
| 435 | |
| 436 | enum { |
| 437 | STATE_ERROR, |
| 438 | STATE_CONSTRUCTED, |
| 439 | STATE_IN_CONFIG, |
| 440 | STATE_IN_RECONFIG, |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 441 | STATE_CONFIGURED, |
Chien-Yu Chen | e8c535e | 2016-04-14 12:18:26 -0700 | [diff] [blame] | 442 | STATE_PREPARING, |
Emilian Peev | ac3ce6c | 2017-12-12 15:27:02 +0000 | [diff] [blame] | 443 | STATE_ABANDONED, |
| 444 | STATE_IN_IDLE |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 445 | } mState; |
| 446 | |
| 447 | mutable Mutex mLock; |
| 448 | |
| 449 | Camera3Stream(int id, camera3_stream_type type, |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 450 | uint32_t width, uint32_t height, size_t maxSize, int format, |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 451 | android_dataspace dataSpace, camera3_stream_rotation_t rotation, |
Shuzhen Wang | c28189a | 2017-11-27 23:05:10 -0800 | [diff] [blame^] | 452 | const String8& physicalCameraId, int setId); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 453 | |
Yin-Chia Yeh | db1e864 | 2017-07-14 15:19:30 -0700 | [diff] [blame] | 454 | wp<Camera3StreamBufferFreedListener> mBufferFreedListener; |
Yin-Chia Yeh | be83fa7 | 2017-03-30 13:35:36 -0700 | [diff] [blame] | 455 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 456 | /** |
| 457 | * Interface to be implemented by derived classes |
| 458 | */ |
| 459 | |
| 460 | // getBuffer / returnBuffer implementations |
| 461 | |
| 462 | // Since camera3_stream_buffer includes a raw pointer to the stream, |
| 463 | // cast to camera3_stream*, implementations must increment the |
| 464 | // refcount of the stream manually in getBufferLocked, and decrement it in |
| 465 | // returnBufferLocked. |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 466 | virtual status_t getBufferLocked(camera3_stream_buffer *buffer, |
| 467 | const std::vector<size_t>& surface_ids = std::vector<size_t>()); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 468 | virtual status_t returnBufferLocked(const camera3_stream_buffer &buffer, |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 469 | nsecs_t timestamp); |
| 470 | virtual status_t getInputBufferLocked(camera3_stream_buffer *buffer); |
| 471 | virtual status_t returnInputBufferLocked( |
| 472 | const camera3_stream_buffer &buffer); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 473 | virtual bool hasOutstandingBuffersLocked() const = 0; |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 474 | // Get the buffer producer of the input buffer queue. Only apply to input streams. |
| 475 | virtual status_t getInputBufferProducerLocked(sp<IGraphicBufferProducer> *producer); |
| 476 | |
Igor Murashkin | e2172be | 2013-05-28 15:31:39 -0700 | [diff] [blame] | 477 | // Can return -ENOTCONN when we are already disconnected (not an error) |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 478 | virtual status_t disconnectLocked() = 0; |
| 479 | |
| 480 | // Configure the buffer queue interface to the other end of the stream, |
| 481 | // after the HAL has provided usage and max_buffers values. After this call, |
| 482 | // the stream must be ready to produce all buffers for registration with |
| 483 | // HAL. |
| 484 | virtual status_t configureQueueLocked() = 0; |
| 485 | |
| 486 | // Get the total number of buffers in the queue |
| 487 | virtual size_t getBufferCountLocked() = 0; |
| 488 | |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 489 | // Get handout output buffer count. |
| 490 | virtual size_t getHandoutOutputBufferCountLocked() = 0; |
| 491 | |
| 492 | // Get handout input buffer count. |
| 493 | virtual size_t getHandoutInputBufferCountLocked() = 0; |
| 494 | |
Eino-Ville Talvala | b2f5b19 | 2013-07-30 14:36:03 -0700 | [diff] [blame] | 495 | // Get the usage flags for the other endpoint, or return |
| 496 | // INVALID_OPERATION if they cannot be obtained. |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 497 | virtual status_t getEndpointUsage(uint64_t *usage) const = 0; |
Eino-Ville Talvala | b2f5b19 | 2013-07-30 14:36:03 -0700 | [diff] [blame] | 498 | |
Emilian Peev | 889234d | 2017-07-18 18:21:26 -0700 | [diff] [blame] | 499 | // Return whether the buffer is in the list of outstanding buffers. |
| 500 | bool isOutstandingBuffer(const camera3_stream_buffer& buffer) const; |
| 501 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 502 | // Tracking for idle state |
| 503 | wp<StatusTracker> mStatusTracker; |
| 504 | // Status tracker component ID |
| 505 | int mStatusId; |
| 506 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 507 | // Tracking for stream prepare - whether this stream can still have |
| 508 | // prepareNextBuffer called on it. |
| 509 | bool mStreamUnpreparable; |
| 510 | |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 511 | uint64_t mUsage; |
| 512 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 513 | private: |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 514 | uint64_t mOldUsage; |
Eino-Ville Talvala | 02bf032 | 2016-02-18 12:41:10 -0800 | [diff] [blame] | 515 | uint32_t mOldMaxBuffers; |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 516 | Condition mOutputBufferReturnedSignal; |
| 517 | Condition mInputBufferReturnedSignal; |
| 518 | static const nsecs_t kWaitForBufferDuration = 3000000000LL; // 3000 ms |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 519 | |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 520 | void fireBufferListenersLocked(const camera3_stream_buffer& buffer, |
| 521 | bool acquired, bool output); |
| 522 | List<wp<Camera3StreamBufferListener> > mBufferListenerList; |
| 523 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 524 | status_t cancelPrepareLocked(); |
| 525 | |
Chien-Yu Chen | e02e932 | 2016-04-11 16:59:33 -0700 | [diff] [blame] | 526 | // Remove the buffer from the list of outstanding buffers. |
| 527 | void removeOutstandingBuffer(const camera3_stream_buffer& buffer); |
| 528 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 529 | // Tracking for PREPARING state |
| 530 | |
| 531 | // State of buffer preallocation. Only true if either prepareNextBuffer |
| 532 | // has been called sufficient number of times, or stream configuration |
| 533 | // had to register buffers with the HAL |
| 534 | bool mPrepared; |
| 535 | |
| 536 | Vector<camera3_stream_buffer_t> mPreparedBuffers; |
| 537 | size_t mPreparedBufferIdx; |
| 538 | |
Ruben Brunk | c78ac26 | 2015-08-13 17:58:46 -0700 | [diff] [blame] | 539 | // Number of buffers allocated on last prepare call. |
Eino-Ville Talvala | 02bf032 | 2016-02-18 12:41:10 -0800 | [diff] [blame] | 540 | size_t mLastMaxCount; |
Ruben Brunk | c78ac26 | 2015-08-13 17:58:46 -0700 | [diff] [blame] | 541 | |
Emilian Peev | 889234d | 2017-07-18 18:21:26 -0700 | [diff] [blame] | 542 | mutable Mutex mOutstandingBuffersLock; |
Chien-Yu Chen | e02e932 | 2016-04-11 16:59:33 -0700 | [diff] [blame] | 543 | // Outstanding buffers dequeued from the stream's buffer queue. |
| 544 | List<buffer_handle_t> mOutstandingBuffers; |
| 545 | |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 546 | // Latency histogram of the wait time for handout buffer count to drop below |
| 547 | // max_buffers. |
| 548 | static const int32_t kBufferLimitLatencyBinSize = 33; //in ms |
| 549 | CameraLatencyHistogram mBufferLimitLatency; |
Emilian Peev | 710c142 | 2017-08-30 11:19:38 +0100 | [diff] [blame] | 550 | |
| 551 | //Keep track of original format in case it gets overridden |
| 552 | bool mFormatOverridden; |
| 553 | int mOriginalFormat; |
Eino-Ville Talvala | 91cd3f8 | 2017-08-21 16:12:50 -0700 | [diff] [blame] | 554 | |
| 555 | //Keep track of original dataSpace in case it gets overridden |
| 556 | bool mDataSpaceOverridden; |
| 557 | android_dataspace mOriginalDataSpace; |
| 558 | |
Shuzhen Wang | c28189a | 2017-11-27 23:05:10 -0800 | [diff] [blame^] | 559 | String8 mPhysicalCameraId; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 560 | }; // class Camera3Stream |
| 561 | |
| 562 | }; // namespace camera3 |
| 563 | |
| 564 | }; // namespace android |
| 565 | |
| 566 | #endif |