1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef ANDROID_SERVERS_CAMERA3_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> 24 #include <utils/List.h> 25 26 #include "hardware/camera3.h" 27 28 #include "Camera3StreamBufferListener.h" 29 #include "Camera3StreamInterface.h" 30 31 namespace android { 32 33 namespace camera3 { 34 35 /** 36 * A class for managing a single stream of input or output data from the camera 37 * device. 38 * 39 * The stream has an internal state machine to track whether it's 40 * connected/configured/etc. 41 * 42 * States: 43 * 44 * STATE_ERROR: A serious error has occurred, stream is unusable. Outstanding 45 * buffers may still be returned. 46 * 47 * STATE_CONSTRUCTED: The stream is ready for configuration, but buffers cannot 48 * be gotten yet. Not connected to any endpoint, no buffers are registered 49 * with the HAL. 50 * 51 * STATE_IN_CONFIG: Configuration has started, but not yet concluded. During this 52 * time, the usage, max_buffers, and priv fields of camera3_stream returned by 53 * startConfiguration() may be modified. 54 * 55 * STATE_IN_RE_CONFIG: Configuration has started, and the stream has been 56 * configured before. Need to track separately from IN_CONFIG to avoid 57 * re-registering buffers with HAL. 58 * 59 * STATE_CONFIGURED: Stream is configured, and has registered buffers with the 60 * HAL (if necessary). The stream's getBuffer/returnBuffer work. The priv 61 * pointer may still be modified. 62 * 63 * STATE_PREPARING: The stream's buffers are being pre-allocated for use. On 64 * older HALs, this is done as part of configuration, but in newer HALs 65 * buffers may be allocated at time of first use. But some use cases require 66 * buffer allocation upfront, to minmize disruption due to lengthy allocation 67 * duration. In this state, only prepareNextBuffer() and cancelPrepare() 68 * may be called. 69 * 70 * Transition table: 71 * 72 * <none> => STATE_CONSTRUCTED: 73 * When constructed with valid arguments 74 * <none> => STATE_ERROR: 75 * When constructed with invalid arguments 76 * STATE_CONSTRUCTED => STATE_IN_CONFIG: 77 * When startConfiguration() is called 78 * STATE_IN_CONFIG => STATE_CONFIGURED: 79 * When finishConfiguration() is called 80 * STATE_IN_CONFIG => STATE_ERROR: 81 * When finishConfiguration() fails to allocate or register buffers. 82 * STATE_CONFIGURED => STATE_IN_RE_CONFIG: * 83 * When startConfiguration() is called again, after making sure stream is 84 * idle with waitUntilIdle(). 85 * STATE_IN_RE_CONFIG => STATE_CONFIGURED: 86 * When finishConfiguration() is called. 87 * STATE_IN_RE_CONFIG => STATE_ERROR: 88 * When finishConfiguration() fails to allocate or register buffers. 89 * STATE_CONFIGURED => STATE_CONSTRUCTED: 90 * When disconnect() is called after making sure stream is idle with 91 * waitUntilIdle(). 92 * STATE_CONFIGURED => STATE_PREPARING: 93 * When startPrepare is called before the stream has a buffer 94 * queued back into it for the first time. 95 * STATE_PREPARING => STATE_CONFIGURED: 96 * When sufficient prepareNextBuffer calls have been made to allocate 97 * all stream buffers, or cancelPrepare is called. 98 * STATE_CONFIGURED => STATE_ABANDONED: 99 * When the buffer queue of the stream is abandoned. 100 * 101 * Status Tracking: 102 * Each stream is tracked by StatusTracker as a separate component, 103 * depending on the handed out buffer count. The state must be STATE_CONFIGURED 104 * in order for the component to be marked. 105 * 106 * It's marked in one of two ways: 107 * 108 * - ACTIVE: One or more buffers have been handed out (with #getBuffer). 109 * - IDLE: All buffers have been returned (with #returnBuffer), and their 110 * respective release_fence(s) have been signaled. 111 * 112 * A typical use case is output streams. When the HAL has any buffers 113 * dequeued, the stream is marked ACTIVE. When the HAL returns all buffers 114 * (e.g. if no capture requests are active), the stream is marked IDLE. 115 * In this use case, the app consumer does not affect the component status. 116 * 117 */ 118 class Camera3Stream : 119 protected camera3_stream, 120 public virtual Camera3StreamInterface, 121 public virtual RefBase { 122 public: 123 124 virtual ~Camera3Stream(); 125 126 static Camera3Stream* cast(camera3_stream *stream); 127 static const Camera3Stream* cast(const camera3_stream *stream); 128 129 /** 130 * Get the stream's ID 131 */ 132 int getId() const; 133 134 /** 135 * Get the output stream set id. 136 */ 137 int getStreamSetId() const; 138 139 /** 140 * Get the stream's dimensions and format 141 */ 142 uint32_t getWidth() const; 143 uint32_t getHeight() const; 144 int getFormat() const; 145 android_dataspace getDataSpace() const; 146 147 /** 148 * Start the stream configuration process. Returns a handle to the stream's 149 * information to be passed into the HAL device's configure_streams call. 150 * 151 * Until finishConfiguration() is called, no other methods on the stream may be 152 * called. The usage and max_buffers fields of camera3_stream may be modified 153 * between start/finishConfiguration, but may not be changed after that. 154 * The priv field of camera3_stream may be modified at any time after 155 * startConfiguration. 156 * 157 * Returns NULL in case of error starting configuration. 158 */ 159 camera3_stream* startConfiguration(); 160 161 /** 162 * Check if the stream is mid-configuration (start has been called, but not 163 * finish). Used for lazy completion of configuration. 164 */ 165 bool isConfiguring() const; 166 167 /** 168 * Completes the stream configuration process. During this call, the stream 169 * may call the device's register_stream_buffers() method. The stream 170 * information structure returned by startConfiguration() may no longer be 171 * modified after this call, but can still be read until the destruction of 172 * the stream. 173 * 174 * Returns: 175 * OK on a successful configuration 176 * NO_INIT in case of a serious error from the HAL device 177 * NO_MEMORY in case of an error registering buffers 178 * INVALID_OPERATION in case connecting to the consumer failed 179 */ 180 status_t finishConfiguration(camera3_device *hal3Device); 181 182 /** 183 * Cancels the stream configuration process. This returns the stream to the 184 * initial state, allowing it to be configured again later. 185 * This is done if the HAL rejects the proposed combined stream configuration 186 */ 187 status_t cancelConfiguration(); 188 189 /** 190 * Determine whether the stream has already become in-use (has received 191 * a valid filled buffer), which determines if a stream can still have 192 * prepareNextBuffer called on it. 193 */ 194 bool isUnpreparable(); 195 196 /** 197 * Start stream preparation. May only be called in the CONFIGURED state, 198 * when no valid buffers have yet been returned to this stream. Prepares 199 * up to maxCount buffers, or the maximum number of buffers needed by the 200 * pipeline if maxCount is ALLOCATE_PIPELINE_MAX. 201 * 202 * If no prepartion is necessary, returns OK and does not transition to 203 * PREPARING state. Otherwise, returns NOT_ENOUGH_DATA and transitions 204 * to PREPARING. 205 * 206 * This call performs no allocation, so is quick to call. 207 * 208 * Returns: 209 * OK if no more buffers need to be preallocated 210 * NOT_ENOUGH_DATA if calls to prepareNextBuffer are needed to finish 211 * buffer pre-allocation, and transitions to the PREPARING state. 212 * NO_INIT in case of a serious error from the HAL device 213 * INVALID_OPERATION if called when not in CONFIGURED state, or a 214 * valid buffer has already been returned to this stream. 215 */ 216 status_t startPrepare(int maxCount); 217 218 /** 219 * Check if the stream is mid-preparing. 220 */ 221 bool isPreparing() const; 222 223 /** 224 * Continue stream buffer preparation by allocating the next 225 * buffer for this stream. May only be called in the PREPARED state. 226 * 227 * Returns OK and transitions to the CONFIGURED state if all buffers 228 * are allocated after the call concludes. Otherwise returns NOT_ENOUGH_DATA. 229 * 230 * This call allocates one buffer, which may take several milliseconds for 231 * large buffers. 232 * 233 * Returns: 234 * OK if no more buffers need to be preallocated, and transitions 235 * to the CONFIGURED state. 236 * NOT_ENOUGH_DATA if more calls to prepareNextBuffer are needed to finish 237 * buffer pre-allocation. 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 */ 242 status_t prepareNextBuffer(); 243 244 /** 245 * Cancel stream preparation early. In case allocation needs to be 246 * stopped, this method transitions the stream back to the CONFIGURED state. 247 * Buffers that have been allocated with prepareNextBuffer remain that way, 248 * but a later use of prepareNextBuffer will require just as many 249 * calls as if the earlier prepare attempt had not existed. 250 * 251 * Returns: 252 * OK if cancellation succeeded, and transitions to the CONFIGURED state 253 * INVALID_OPERATION if not in the PREPARING state 254 * NO_INIT in case of a serious error from the HAL device 255 */ 256 status_t cancelPrepare(); 257 258 /** 259 * Tear down memory for this stream. This frees all unused gralloc buffers 260 * allocated for this stream, but leaves it ready for operation afterward. 261 * 262 * May only be called in the CONFIGURED state, and keeps the stream in 263 * the CONFIGURED state. 264 * 265 * Returns: 266 * OK if teardown succeeded. 267 * INVALID_OPERATION if not in the CONFIGURED state 268 * NO_INIT in case of a serious error from the HAL device 269 */ 270 status_t tearDown(); 271 272 /** 273 * Fill in the camera3_stream_buffer with the next valid buffer for this 274 * stream, to hand over to the HAL. 275 * 276 * This method may only be called once finishConfiguration has been called. 277 * For bidirectional streams, this method applies to the output-side 278 * buffers. 279 * 280 */ 281 status_t getBuffer(camera3_stream_buffer *buffer); 282 283 /** 284 * Return a buffer to the stream after use by the HAL. 285 * 286 * This method may only be called for buffers provided by getBuffer(). 287 * For bidirectional streams, this method applies to the output-side buffers 288 */ 289 status_t returnBuffer(const camera3_stream_buffer &buffer, 290 nsecs_t timestamp); 291 292 /** 293 * Fill in the camera3_stream_buffer with the next valid buffer for this 294 * stream, to hand over to the HAL. 295 * 296 * This method may only be called once finishConfiguration has been called. 297 * For bidirectional streams, this method applies to the input-side 298 * buffers. 299 * 300 */ 301 status_t getInputBuffer(camera3_stream_buffer *buffer); 302 303 /** 304 * Return a buffer to the stream after use by the HAL. 305 * 306 * This method may only be called for buffers provided by getBuffer(). 307 * For bidirectional streams, this method applies to the input-side buffers 308 */ 309 status_t returnInputBuffer(const camera3_stream_buffer &buffer); 310 311 // get the buffer producer of the input buffer queue. 312 // only apply to input streams. 313 status_t getInputBufferProducer(sp<IGraphicBufferProducer> *producer); 314 315 /** 316 * Whether any of the stream's buffers are currently in use by the HAL, 317 * including buffers that have been returned but not yet had their 318 * release fence signaled. 319 */ 320 bool hasOutstandingBuffers() const; 321 322 enum { 323 TIMEOUT_NEVER = -1 324 }; 325 326 /** 327 * Set the status tracker to notify about idle transitions 328 */ 329 virtual status_t setStatusTracker(sp<StatusTracker> statusTracker); 330 331 /** 332 * Disconnect stream from its non-HAL endpoint. After this, 333 * start/finishConfiguration must be called before the stream can be used 334 * again. This cannot be called if the stream has outstanding dequeued 335 * buffers. 336 */ 337 status_t disconnect(); 338 339 /** 340 * Debug dump of the stream's state. 341 */ 342 virtual void dump(int fd, const Vector<String16> &args) const = 0; 343 344 /** 345 * Add a camera3 buffer listener. Adding the same listener twice has 346 * no effect. 347 */ 348 void addBufferListener( 349 wp<Camera3StreamBufferListener> listener); 350 351 /** 352 * Remove a camera3 buffer listener. Removing the same listener twice 353 * or the listener that was never added has no effect. 354 */ 355 void removeBufferListener( 356 const sp<Camera3StreamBufferListener>& listener); 357 358 /** 359 * Return if the buffer queue of the stream is abandoned. 360 */ 361 bool isAbandoned() const; 362 363 protected: 364 const int mId; 365 /** 366 * Stream set id, used to indicate which group of this stream belongs to for buffer sharing 367 * across multiple streams. 368 * 369 * The default value is set to CAMERA3_STREAM_SET_ID_INVALID, which indicates that this stream 370 * doesn't intend to share buffers with any other streams, and this stream will fall back to 371 * the existing BufferQueue mechanism to manage the buffer allocations and buffer circulation. 372 * When a valid stream set id is set, this stream intends to use the Camera3BufferManager to 373 * manage the buffer allocations; the BufferQueue will only handle the buffer transaction 374 * between the producer and consumer. For this case, upon successfully registration, the streams 375 * with the same stream set id will potentially share the buffers allocated by 376 * Camera3BufferManager. 377 */ 378 const int mSetId; 379 380 const String8 mName; 381 // Zero for formats with fixed buffer size for given dimensions. 382 const size_t mMaxSize; 383 384 enum { 385 STATE_ERROR, 386 STATE_CONSTRUCTED, 387 STATE_IN_CONFIG, 388 STATE_IN_RECONFIG, 389 STATE_CONFIGURED, 390 STATE_PREPARING, 391 STATE_ABANDONED 392 } mState; 393 394 mutable Mutex mLock; 395 396 Camera3Stream(int id, camera3_stream_type type, 397 uint32_t width, uint32_t height, size_t maxSize, int format, 398 android_dataspace dataSpace, camera3_stream_rotation_t rotation, 399 int setId); 400 401 /** 402 * Interface to be implemented by derived classes 403 */ 404 405 // getBuffer / returnBuffer implementations 406 407 // Since camera3_stream_buffer includes a raw pointer to the stream, 408 // cast to camera3_stream*, implementations must increment the 409 // refcount of the stream manually in getBufferLocked, and decrement it in 410 // returnBufferLocked. 411 virtual status_t getBufferLocked(camera3_stream_buffer *buffer); 412 virtual status_t returnBufferLocked(const camera3_stream_buffer &buffer, 413 nsecs_t timestamp); 414 virtual status_t getInputBufferLocked(camera3_stream_buffer *buffer); 415 virtual status_t returnInputBufferLocked( 416 const camera3_stream_buffer &buffer); 417 virtual bool hasOutstandingBuffersLocked() const = 0; 418 // Get the buffer producer of the input buffer queue. Only apply to input streams. 419 virtual status_t getInputBufferProducerLocked(sp<IGraphicBufferProducer> *producer); 420 421 // Can return -ENOTCONN when we are already disconnected (not an error) 422 virtual status_t disconnectLocked() = 0; 423 424 // Configure the buffer queue interface to the other end of the stream, 425 // after the HAL has provided usage and max_buffers values. After this call, 426 // the stream must be ready to produce all buffers for registration with 427 // HAL. 428 virtual status_t configureQueueLocked() = 0; 429 430 // Get the total number of buffers in the queue 431 virtual size_t getBufferCountLocked() = 0; 432 433 // Get handout output buffer count. 434 virtual size_t getHandoutOutputBufferCountLocked() = 0; 435 436 // Get handout input buffer count. 437 virtual size_t getHandoutInputBufferCountLocked() = 0; 438 439 // Get the usage flags for the other endpoint, or return 440 // INVALID_OPERATION if they cannot be obtained. 441 virtual status_t getEndpointUsage(uint32_t *usage) const = 0; 442 443 // Tracking for idle state 444 wp<StatusTracker> mStatusTracker; 445 // Status tracker component ID 446 int mStatusId; 447 448 // Tracking for stream prepare - whether this stream can still have 449 // prepareNextBuffer called on it. 450 bool mStreamUnpreparable; 451 452 private: 453 uint32_t mOldUsage; 454 uint32_t mOldMaxBuffers; 455 Condition mOutputBufferReturnedSignal; 456 Condition mInputBufferReturnedSignal; 457 static const nsecs_t kWaitForBufferDuration = 3000000000LL; // 3000 ms 458 459 // Gets all buffers from endpoint and registers them with the HAL. 460 status_t registerBuffersLocked(camera3_device *hal3Device); 461 462 void fireBufferListenersLocked(const camera3_stream_buffer& buffer, 463 bool acquired, bool output); 464 List<wp<Camera3StreamBufferListener> > mBufferListenerList; 465 466 status_t cancelPrepareLocked(); 467 468 // Return whether the buffer is in the list of outstanding buffers. 469 bool isOutstandingBuffer(const camera3_stream_buffer& buffer); 470 471 // Remove the buffer from the list of outstanding buffers. 472 void removeOutstandingBuffer(const camera3_stream_buffer& buffer); 473 474 // Tracking for PREPARING state 475 476 // State of buffer preallocation. Only true if either prepareNextBuffer 477 // has been called sufficient number of times, or stream configuration 478 // had to register buffers with the HAL 479 bool mPrepared; 480 481 Vector<camera3_stream_buffer_t> mPreparedBuffers; 482 size_t mPreparedBufferIdx; 483 484 // Number of buffers allocated on last prepare call. 485 size_t mLastMaxCount; 486 487 // Outstanding buffers dequeued from the stream's buffer queue. 488 List<buffer_handle_t> mOutstandingBuffers; 489 490 }; // class Camera3Stream 491 492 }; // namespace camera3 493 494 }; // namespace android 495 496 #endif 497