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_INTERFACE_H 18 #define ANDROID_SERVERS_CAMERA3_STREAM_INTERFACE_H 19 20 #include <gui/Flags.h> 21 #include <utils/RefBase.h> 22 23 #include <camera/camera2/OutputConfiguration.h> 24 #include <camera/CameraMetadata.h> 25 #include "Camera3StreamBufferListener.h" 26 #include "Camera3StreamBufferFreedListener.h" 27 28 namespace android { 29 30 namespace camera3 { 31 32 typedef enum camera_buffer_status { 33 CAMERA_BUFFER_STATUS_OK = 0, 34 CAMERA_BUFFER_STATUS_ERROR = 1 35 } camera_buffer_status_t; 36 37 typedef enum camera_stream_type { 38 CAMERA_STREAM_OUTPUT = 0, 39 CAMERA_STREAM_INPUT = 1, 40 CAMERA_NUM_STREAM_TYPES 41 } camera_stream_type_t; 42 43 typedef enum camera_stream_rotation { 44 /* No rotation */ 45 CAMERA_STREAM_ROTATION_0 = 0, 46 47 /* Rotate by 90 degree counterclockwise */ 48 CAMERA_STREAM_ROTATION_90 = 1, 49 50 /* Rotate by 180 degree counterclockwise */ 51 CAMERA_STREAM_ROTATION_180 = 2, 52 53 /* Rotate by 270 degree counterclockwise */ 54 CAMERA_STREAM_ROTATION_270 = 3 55 } camera_stream_rotation_t; 56 57 typedef struct camera_stream { 58 camera_stream_type_t stream_type; 59 uint32_t width; 60 uint32_t height; 61 int format; 62 uint32_t usage; 63 uint32_t max_buffers; 64 android_dataspace_t data_space; 65 camera_stream_rotation_t rotation; 66 std::string physical_camera_id; 67 68 std::unordered_set<int32_t> sensor_pixel_modes_used; 69 int64_t dynamic_range_profile; 70 int64_t use_case; 71 int32_t color_space; 72 } camera_stream_t; 73 74 typedef struct camera_stream_buffer { 75 camera_stream_t *stream; 76 buffer_handle_t *buffer; 77 camera_buffer_status_t status; 78 int acquire_fence; 79 int release_fence; 80 } camera_stream_buffer_t; 81 82 struct Size { 83 uint32_t width; 84 uint32_t height; widthSize85 explicit Size(uint32_t w = 0, uint32_t h = 0) : width(w), height(h){} 86 }; 87 88 enum { 89 /** 90 * This stream set ID indicates that the set ID is invalid, and this stream doesn't intend to 91 * share buffers with any other stream. It is illegal to register this kind of stream to 92 * Camera3BufferManager. 93 */ 94 CAMERA3_STREAM_SET_ID_INVALID = -1, 95 96 /** 97 * Invalid output stream ID. 98 */ 99 CAMERA3_STREAM_ID_INVALID = -1, 100 }; 101 102 class StatusTracker; 103 104 // OutputStreamInfo describes the property of a camera stream. 105 class OutputStreamInfo { 106 public: 107 int width; 108 int height; 109 int format; 110 android_dataspace dataSpace; 111 uint64_t consumerUsage; 112 bool finalized = false; 113 bool supportsOffline = false; 114 std::unordered_set<int32_t> sensorPixelModesUsed; 115 int64_t dynamicRangeProfile; 116 int64_t streamUseCase; 117 int timestampBase; 118 int32_t colorSpace; OutputStreamInfo()119 OutputStreamInfo() : 120 width(-1), height(-1), format(-1), dataSpace(HAL_DATASPACE_UNKNOWN), 121 consumerUsage(0), 122 dynamicRangeProfile(ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD), 123 streamUseCase(ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT), 124 timestampBase(OutputConfiguration::TIMESTAMP_BASE_DEFAULT), 125 colorSpace(ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_UNSPECIFIED) {} OutputStreamInfo(int _width,int _height,int _format,android_dataspace _dataSpace,uint64_t _consumerUsage,const std::unordered_set<int32_t> & _sensorPixelModesUsed,int64_t _dynamicRangeProfile,int _streamUseCase,int _timestampBase,int32_t _colorSpace)126 OutputStreamInfo(int _width, int _height, int _format, android_dataspace _dataSpace, 127 uint64_t _consumerUsage, const std::unordered_set<int32_t>& _sensorPixelModesUsed, 128 int64_t _dynamicRangeProfile, int _streamUseCase, int _timestampBase, 129 int32_t _colorSpace) : 130 width(_width), height(_height), format(_format), 131 dataSpace(_dataSpace), consumerUsage(_consumerUsage), 132 sensorPixelModesUsed(_sensorPixelModesUsed), dynamicRangeProfile(_dynamicRangeProfile), 133 streamUseCase(_streamUseCase), timestampBase(_timestampBase), colorSpace(_colorSpace) {} 134 bool operator == (const OutputStreamInfo& other) const { 135 return (width == other.width && 136 height == other.height && 137 format == other.format && 138 (other.format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED || 139 (dataSpace == other.dataSpace && consumerUsage == other.consumerUsage)) && 140 sensorPixelModesUsed == other.sensorPixelModesUsed && 141 dynamicRangeProfile == other.dynamicRangeProfile && 142 colorSpace == other.colorSpace && 143 streamUseCase == other.streamUseCase && 144 timestampBase == other.timestampBase); 145 } 146 }; 147 148 // A holder containing a surface and its corresponding mirroring mode 149 struct SurfaceHolder { 150 sp<Surface> mSurface; 151 int mMirrorMode = OutputConfiguration::MIRROR_MODE_AUTO; 152 }; 153 154 // Utility class to lock and unlock a GraphicBuffer 155 class GraphicBufferLocker { 156 public: GraphicBufferLocker(sp<GraphicBuffer> buffer)157 GraphicBufferLocker(sp<GraphicBuffer> buffer) : _buffer(buffer) {} 158 lockAsync(uint32_t usage,void ** dstBuffer,int fenceFd)159 status_t lockAsync(uint32_t usage, void** dstBuffer, int fenceFd) { 160 if (_buffer == nullptr) return BAD_VALUE; 161 162 status_t res = OK; 163 if (!_locked) { 164 status_t res = _buffer->lockAsync(usage, dstBuffer, fenceFd); 165 if (res == OK) { 166 _locked = true; 167 } 168 } 169 return res; 170 } 171 lockAsync(void ** dstBuffer,int fenceFd)172 status_t lockAsync(void** dstBuffer, int fenceFd) { 173 return lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, dstBuffer, fenceFd); 174 } 175 ~GraphicBufferLocker()176 ~GraphicBufferLocker() { 177 if (_locked && _buffer != nullptr) { 178 auto res = _buffer->unlock(); 179 if (res != OK) { 180 ALOGE("%s: Error trying to unlock buffer: %s (%d)", __FUNCTION__, 181 strerror(-res), res); 182 } 183 } 184 } 185 186 private: 187 sp<GraphicBuffer> _buffer; 188 bool _locked = false; 189 }; 190 191 /** 192 * An interface for managing a single stream of input and/or output data from 193 * the camera device. 194 */ 195 class Camera3StreamInterface : public virtual RefBase { 196 public: 197 198 enum { 199 ALLOCATE_PIPELINE_MAX = 0, // Allocate max buffers used by a given surface 200 }; 201 202 /** 203 * Get the stream's ID 204 */ 205 virtual int getId() const = 0; 206 207 /** 208 * Get the output stream set id. 209 */ 210 virtual int getStreamSetId() const = 0; 211 212 /** 213 * Is this stream part of a multi-resolution stream set 214 */ 215 virtual bool isMultiResolution() const = 0; 216 217 /** 218 * Get the HAL stream group id for a multi-resolution stream set 219 */ 220 virtual int getHalStreamGroupId() const = 0; 221 222 /** 223 * Get the stream's dimensions and format 224 */ 225 virtual uint32_t getWidth() const = 0; 226 virtual uint32_t getHeight() const = 0; 227 virtual int getFormat() const = 0; 228 virtual int64_t getDynamicRangeProfile() const = 0; 229 virtual android_dataspace getDataSpace() const = 0; 230 virtual int32_t getColorSpace() const = 0; 231 virtual void setFormatOverride(bool formatOverriden) = 0; 232 virtual bool isFormatOverridden() const = 0; 233 virtual int getOriginalFormat() const = 0; 234 virtual void setDataSpaceOverride(bool dataSpaceOverriden) = 0; 235 virtual bool isDataSpaceOverridden() const = 0; 236 virtual android_dataspace getOriginalDataSpace() const = 0; 237 virtual int getMaxHalBuffers() const = 0; 238 virtual int getMaxTotalBuffers() const = 0; 239 240 /** 241 * Offline processing 242 */ 243 virtual void setOfflineProcessingSupport(bool support) = 0; 244 virtual bool getOfflineProcessingSupport() const = 0; 245 246 /** 247 * Get a handle for the stream, without starting stream configuration. 248 */ 249 virtual camera_stream* asHalStream() = 0; 250 251 /** 252 * Start the stream configuration process. Returns a handle to the stream's 253 * information to be passed into the device's configure_streams call. 254 * 255 * Until finishConfiguration() is called, no other methods on the stream may 256 * be called. The usage and max_buffers fields of camera_stream may be 257 * modified between start/finishConfiguration, but may not be changed after 258 * that. The priv field of camera_stream may be modified at any time after 259 * startConfiguration. 260 * 261 * Returns NULL in case of error starting configuration. 262 */ 263 virtual camera_stream* startConfiguration() = 0; 264 265 /** 266 * Check if the stream is mid-configuration (start has been called, but not 267 * finish). Used for lazy completion of configuration. 268 */ 269 virtual bool isConfiguring() const = 0; 270 271 /** 272 * Completes the stream configuration process. During this call, the stream 273 * may call the device's register_stream_buffers() method. The stream 274 * information structure returned by startConfiguration() may no longer be 275 * modified after this call, but can still be read until the destruction of 276 * the stream. 277 * 278 * streamReconfigured: set to true when a stream is being reconfigured. 279 * 280 * Returns: 281 * OK on a successful configuration 282 * NO_INIT in case of a serious error from the HAL device 283 * NO_MEMORY in case of an error registering buffers 284 * INVALID_OPERATION in case connecting to the consumer failed 285 */ 286 virtual status_t finishConfiguration(/*out*/bool* streamReconfigured = nullptr) = 0; 287 288 /** 289 * Cancels the stream configuration process. This returns the stream to the 290 * initial state, allowing it to be configured again later. 291 * This is done if the HAL rejects the proposed combined stream configuration 292 */ 293 virtual status_t cancelConfiguration() = 0; 294 295 /** 296 * Determine whether the stream has already become in-use (has received 297 * a valid filled buffer), which determines if a stream can still have 298 * prepareNextBuffer called on it. 299 */ 300 virtual bool isUnpreparable() = 0; 301 302 /** 303 * Mark the stream as unpreparable. 304 */ 305 virtual void markUnpreparable() = 0; 306 307 /** 308 * Start stream preparation. May only be called in the CONFIGURED state, 309 * when no valid buffers have yet been returned to this stream. Prepares 310 * up to maxCount buffers, or the maximum number of buffers needed by the 311 * pipeline if maxCount is ALLOCATE_PIPELINE_MAX. 312 * 313 * If no prepartion is necessary, returns OK and does not transition to 314 * PREPARING state. Otherwise, returns NOT_ENOUGH_DATA and transitions 315 * to PREPARING. 316 * 317 * blockRequest specifies whether prepare will block upcoming capture 318 * request. This flag should only be set to false if the caller guarantees 319 * the whole buffer preparation process is done before capture request 320 * comes in. 321 * 322 * Returns: 323 * OK if no more buffers need to be preallocated 324 * NOT_ENOUGH_DATA if calls to prepareNextBuffer are needed to finish 325 * buffer pre-allocation, and transitions to the PREPARING state. 326 * NO_INIT in case of a serious error from the HAL device 327 * INVALID_OPERATION if called when not in CONFIGURED state, or a 328 * valid buffer has already been returned to this stream. 329 */ 330 virtual status_t startPrepare(int maxCount, bool blockRequest) = 0; 331 332 /** 333 * Check if the request on a stream is blocked by prepare. 334 */ 335 virtual bool isBlockedByPrepare() const = 0; 336 337 /** 338 * Continue stream buffer preparation by allocating the next 339 * buffer for this stream. May only be called in the PREPARED state. 340 * 341 * Returns OK and transitions to the CONFIGURED state if all buffers 342 * are allocated after the call concludes. Otherwise returns NOT_ENOUGH_DATA. 343 * 344 * Returns: 345 * OK if no more buffers need to be preallocated, and transitions 346 * to the CONFIGURED state. 347 * NOT_ENOUGH_DATA if more calls to prepareNextBuffer are needed to finish 348 * buffer pre-allocation. 349 * NO_INIT in case of a serious error from the HAL device 350 * INVALID_OPERATION if called when not in CONFIGURED state, or a 351 * valid buffer has already been returned to this stream. 352 */ 353 virtual status_t prepareNextBuffer() = 0; 354 355 /** 356 * Cancel stream preparation early. In case allocation needs to be 357 * stopped, this method transitions the stream back to the CONFIGURED state. 358 * Buffers that have been allocated with prepareNextBuffer remain that way, 359 * but a later use of prepareNextBuffer will require just as many 360 * calls as if the earlier prepare attempt had not existed. 361 * 362 * Returns: 363 * OK if cancellation succeeded, and transitions to the CONFIGURED state 364 * INVALID_OPERATION if not in the PREPARING state 365 * NO_INIT in case of a serious error from the HAL device 366 */ 367 virtual status_t cancelPrepare() = 0; 368 369 /** 370 * Tear down memory for this stream. This frees all unused gralloc buffers 371 * allocated for this stream, but leaves it ready for operation afterward. 372 * 373 * May only be called in the CONFIGURED state, and keeps the stream in 374 * the CONFIGURED state. 375 * 376 * Returns: 377 * OK if teardown succeeded. 378 * INVALID_OPERATION if not in the CONFIGURED state 379 * NO_INIT in case of a serious error from the HAL device 380 */ 381 virtual status_t tearDown() = 0; 382 383 /** 384 * Fill in the camera_stream_buffer with the next valid buffer for this 385 * stream, to hand over to the HAL. 386 * 387 * Multiple surfaces could share the same HAL stream, but a request may 388 * be only for a subset of surfaces. In this case, the 389 * Camera3StreamInterface object needs the surface ID information to acquire 390 * buffers for those surfaces. For the case of single surface for a HAL 391 * stream, surface_ids parameter has no effect. 392 * 393 * This method may only be called once finishConfiguration has been called. 394 * For bidirectional streams, this method applies to the output-side 395 * buffers. 396 * 397 */ 398 virtual status_t getBuffer(camera_stream_buffer *buffer, 399 nsecs_t waitBufferTimeout, 400 const std::vector<size_t>& surface_ids = std::vector<size_t>()) = 0; 401 402 struct OutstandingBuffer { 403 camera_stream_buffer* outBuffer; 404 405 /** 406 * Multiple surfaces could share the same HAL stream, but a request may 407 * be only for a subset of surfaces. In this case, the 408 * Camera3StreamInterface object needs the surface ID information to acquire 409 * buffers for those surfaces. For the case of single surface for a HAL 410 * stream, surface_ids parameter has no effect. 411 */ 412 std::vector<size_t> surface_ids; 413 }; 414 415 /** 416 * Return a buffer to the stream after use by the HAL. 417 * 418 * Multiple surfaces could share the same HAL stream, but a request may 419 * be only for a subset of surfaces. In this case, the 420 * Camera3StreamInterface object needs the surface ID information to attach 421 * buffers for those surfaces. For the case of single surface for a HAL 422 * stream, surface_ids parameter has no effect. 423 * 424 * This method may only be called for buffers provided by getBuffer(). 425 * For bidirectional streams, this method applies to the output-side buffers 426 */ 427 virtual status_t returnBuffer(const camera_stream_buffer &buffer, 428 nsecs_t timestamp, nsecs_t readoutTimestamp, bool timestampIncreasing = true, 429 const std::vector<size_t>& surface_ids = std::vector<size_t>(), 430 uint64_t frameNumber = 0, int32_t transform = -1) = 0; 431 432 /** 433 * Fill in the camera_stream_buffer with the next valid buffer for this 434 * stream, to hand over to the HAL. 435 * 436 * This method may only be called once finishConfiguration has been called. 437 * For bidirectional streams, this method applies to the input-side 438 * buffers. 439 * 440 * Normally this call will block until the handed out buffer count is less than the stream 441 * max buffer count; if respectHalLimit is set to false, this is ignored. 442 */ 443 virtual status_t getInputBuffer(camera_stream_buffer *buffer, 444 Size *size, bool respectHalLimit = true) = 0; 445 446 /** 447 * Return a buffer to the stream after use by the HAL. 448 * 449 * This method may only be called for buffers provided by getBuffer(). 450 * For bidirectional streams, this method applies to the input-side buffers 451 */ 452 virtual status_t returnInputBuffer(const camera_stream_buffer &buffer) = 0; 453 454 #if !WB_CAMERA3_AND_PROCESSORS_WITH_DEPENDENCIES 455 /** 456 * Get the buffer producer of the input buffer queue. 457 * 458 * This method only applies to input streams. 459 */ 460 virtual status_t getInputBufferProducer(sp<IGraphicBufferProducer> *producer) = 0; 461 #endif 462 463 /** 464 * Whether any of the stream's buffers are currently in use by the HAL, 465 * including buffers that have been returned but not yet had their 466 * release fence signaled. 467 */ 468 virtual bool hasOutstandingBuffers() const = 0; 469 470 /** 471 * Get number of buffers currently handed out to HAL 472 */ 473 virtual size_t getOutstandingBuffersCount() const = 0; 474 475 enum { 476 TIMEOUT_NEVER = -1 477 }; 478 479 /** 480 * Set the state tracker to use for signaling idle transitions. 481 */ 482 virtual status_t setStatusTracker(sp<StatusTracker> statusTracker) = 0; 483 484 /** 485 * Disconnect stream from its non-HAL endpoint. After this, 486 * start/finishConfiguration must be called before the stream can be used 487 * again. This cannot be called if the stream has outstanding dequeued 488 * buffers. 489 */ 490 virtual status_t disconnect() = 0; 491 492 /** 493 * Return if the buffer queue of the stream is abandoned. 494 */ 495 virtual bool isAbandoned() const = 0; 496 497 /** 498 * Debug dump of the stream's state. 499 */ 500 virtual void dump(int fd, const Vector<String16> &args) = 0; 501 502 virtual void addBufferListener( 503 wp<Camera3StreamBufferListener> listener) = 0; 504 virtual void removeBufferListener( 505 const sp<Camera3StreamBufferListener>& listener) = 0; 506 507 /** 508 * Setting listner will remove previous listener (if exists) 509 * Only allow set listener during stream configuration because stream is guaranteed to be IDLE 510 * at this state, so setBufferFreedListener won't collide with onBufferFreed callbacks. 511 * Client is responsible to keep the listener object alive throughout the lifecycle of this 512 * Camera3Stream. 513 */ 514 virtual void setBufferFreedListener(wp<Camera3StreamBufferFreedListener> listener) = 0; 515 516 /** 517 * Notify buffer stream listeners about incoming request with particular frame number. 518 */ 519 virtual void fireBufferRequestForFrameNumber(uint64_t frameNumber, 520 const CameraMetadata& settings) = 0; 521 }; 522 523 } // namespace camera3 524 525 } // namespace android 526 527 #endif 528