Igor Murashkin | ae500e5 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 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 <utils/RefBase.h> |
| 21 | #include "Camera3StreamBufferListener.h" |
Yin-Chia Yeh | be83fa7 | 2017-03-30 13:35:36 -0700 | [diff] [blame] | 22 | #include "Camera3StreamBufferFreedListener.h" |
Igor Murashkin | ae500e5 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 23 | |
| 24 | struct camera3_stream_buffer; |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | namespace camera3 { |
| 29 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 30 | enum { |
| 31 | /** |
| 32 | * This stream set ID indicates that the set ID is invalid, and this stream doesn't intend to |
| 33 | * share buffers with any other stream. It is illegal to register this kind of stream to |
| 34 | * Camera3BufferManager. |
| 35 | */ |
| 36 | CAMERA3_STREAM_SET_ID_INVALID = -1, |
| 37 | |
| 38 | /** |
| 39 | * Invalid output stream ID. |
| 40 | */ |
| 41 | CAMERA3_STREAM_ID_INVALID = -1, |
| 42 | }; |
| 43 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 44 | class StatusTracker; |
| 45 | |
Igor Murashkin | ae500e5 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 46 | /** |
| 47 | * An interface for managing a single stream of input and/or output data from |
| 48 | * the camera device. |
| 49 | */ |
| 50 | class Camera3StreamInterface : public virtual RefBase { |
| 51 | public: |
Ruben Brunk | c78ac26 | 2015-08-13 17:58:46 -0700 | [diff] [blame] | 52 | |
| 53 | enum { |
| 54 | ALLOCATE_PIPELINE_MAX = 0, // Allocate max buffers used by a given surface |
| 55 | }; |
| 56 | |
Igor Murashkin | ae500e5 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 57 | /** |
| 58 | * Get the stream's ID |
| 59 | */ |
| 60 | virtual int getId() const = 0; |
| 61 | |
| 62 | /** |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 63 | * Get the output stream set id. |
| 64 | */ |
| 65 | virtual int getStreamSetId() const = 0; |
| 66 | |
| 67 | /** |
Igor Murashkin | ae500e5 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 68 | * Get the stream's dimensions and format |
| 69 | */ |
| 70 | virtual uint32_t getWidth() const = 0; |
| 71 | virtual uint32_t getHeight() const = 0; |
| 72 | virtual int getFormat() const = 0; |
Eino-Ville Talvala | d46a6b9 | 2015-05-14 17:26:24 -0700 | [diff] [blame] | 73 | virtual android_dataspace getDataSpace() const = 0; |
Igor Murashkin | ae500e5 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 74 | |
| 75 | /** |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 76 | * Get a HAL3 handle for the stream, without starting stream configuration. |
| 77 | */ |
| 78 | virtual camera3_stream* asHalStream() = 0; |
| 79 | |
| 80 | /** |
Igor Murashkin | ae500e5 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 81 | * Start the stream configuration process. Returns a handle to the stream's |
| 82 | * information to be passed into the HAL device's configure_streams call. |
| 83 | * |
| 84 | * Until finishConfiguration() is called, no other methods on the stream may |
| 85 | * be called. The usage and max_buffers fields of camera3_stream may be |
| 86 | * modified between start/finishConfiguration, but may not be changed after |
| 87 | * that. The priv field of camera3_stream may be modified at any time after |
| 88 | * startConfiguration. |
| 89 | * |
| 90 | * Returns NULL in case of error starting configuration. |
| 91 | */ |
| 92 | virtual camera3_stream* startConfiguration() = 0; |
| 93 | |
| 94 | /** |
| 95 | * Check if the stream is mid-configuration (start has been called, but not |
| 96 | * finish). Used for lazy completion of configuration. |
| 97 | */ |
| 98 | virtual bool isConfiguring() const = 0; |
| 99 | |
| 100 | /** |
| 101 | * Completes the stream configuration process. During this call, the stream |
| 102 | * may call the device's register_stream_buffers() method. The stream |
| 103 | * information structure returned by startConfiguration() may no longer be |
| 104 | * modified after this call, but can still be read until the destruction of |
| 105 | * the stream. |
| 106 | * |
| 107 | * Returns: |
| 108 | * OK on a successful configuration |
| 109 | * NO_INIT in case of a serious error from the HAL device |
| 110 | * NO_MEMORY in case of an error registering buffers |
| 111 | * INVALID_OPERATION in case connecting to the consumer failed |
| 112 | */ |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 113 | virtual status_t finishConfiguration() = 0; |
Igor Murashkin | ae500e5 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 114 | |
| 115 | /** |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 116 | * Cancels the stream configuration process. This returns the stream to the |
| 117 | * initial state, allowing it to be configured again later. |
| 118 | * This is done if the HAL rejects the proposed combined stream configuration |
| 119 | */ |
| 120 | virtual status_t cancelConfiguration() = 0; |
| 121 | |
| 122 | /** |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 123 | * Determine whether the stream has already become in-use (has received |
| 124 | * a valid filled buffer), which determines if a stream can still have |
| 125 | * prepareNextBuffer called on it. |
| 126 | */ |
| 127 | virtual bool isUnpreparable() = 0; |
| 128 | |
| 129 | /** |
| 130 | * Start stream preparation. May only be called in the CONFIGURED state, |
Ruben Brunk | c78ac26 | 2015-08-13 17:58:46 -0700 | [diff] [blame] | 131 | * when no valid buffers have yet been returned to this stream. Prepares |
| 132 | * up to maxCount buffers, or the maximum number of buffers needed by the |
| 133 | * pipeline if maxCount is ALLOCATE_PIPELINE_MAX. |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 134 | * |
| 135 | * If no prepartion is necessary, returns OK and does not transition to |
| 136 | * PREPARING state. Otherwise, returns NOT_ENOUGH_DATA and transitions |
| 137 | * to PREPARING. |
| 138 | * |
| 139 | * Returns: |
| 140 | * OK if no more buffers need to be preallocated |
| 141 | * NOT_ENOUGH_DATA if calls to prepareNextBuffer are needed to finish |
| 142 | * buffer pre-allocation, and transitions to the PREPARING state. |
| 143 | * NO_INIT in case of a serious error from the HAL device |
| 144 | * INVALID_OPERATION if called when not in CONFIGURED state, or a |
| 145 | * valid buffer has already been returned to this stream. |
| 146 | */ |
Ruben Brunk | c78ac26 | 2015-08-13 17:58:46 -0700 | [diff] [blame] | 147 | virtual status_t startPrepare(int maxCount) = 0; |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 148 | |
| 149 | /** |
| 150 | * Check if the stream is mid-preparing. |
| 151 | */ |
| 152 | virtual bool isPreparing() const = 0; |
| 153 | |
| 154 | /** |
| 155 | * Continue stream buffer preparation by allocating the next |
| 156 | * buffer for this stream. May only be called in the PREPARED state. |
| 157 | * |
| 158 | * Returns OK and transitions to the CONFIGURED state if all buffers |
| 159 | * are allocated after the call concludes. Otherwise returns NOT_ENOUGH_DATA. |
| 160 | * |
| 161 | * Returns: |
| 162 | * OK if no more buffers need to be preallocated, and transitions |
| 163 | * to the CONFIGURED state. |
| 164 | * NOT_ENOUGH_DATA if more calls to prepareNextBuffer are needed to finish |
| 165 | * buffer pre-allocation. |
| 166 | * NO_INIT in case of a serious error from the HAL device |
| 167 | * INVALID_OPERATION if called when not in CONFIGURED state, or a |
| 168 | * valid buffer has already been returned to this stream. |
| 169 | */ |
| 170 | virtual status_t prepareNextBuffer() = 0; |
| 171 | |
| 172 | /** |
| 173 | * Cancel stream preparation early. In case allocation needs to be |
| 174 | * stopped, this method transitions the stream back to the CONFIGURED state. |
| 175 | * Buffers that have been allocated with prepareNextBuffer remain that way, |
| 176 | * but a later use of prepareNextBuffer will require just as many |
| 177 | * calls as if the earlier prepare attempt had not existed. |
| 178 | * |
| 179 | * Returns: |
| 180 | * OK if cancellation succeeded, and transitions to the CONFIGURED state |
| 181 | * INVALID_OPERATION if not in the PREPARING state |
| 182 | * NO_INIT in case of a serious error from the HAL device |
| 183 | */ |
| 184 | virtual status_t cancelPrepare() = 0; |
| 185 | |
| 186 | /** |
Eino-Ville Talvala | b25e3c8 | 2015-07-15 16:04:27 -0700 | [diff] [blame] | 187 | * Tear down memory for this stream. This frees all unused gralloc buffers |
| 188 | * allocated for this stream, but leaves it ready for operation afterward. |
| 189 | * |
| 190 | * May only be called in the CONFIGURED state, and keeps the stream in |
| 191 | * the CONFIGURED state. |
| 192 | * |
| 193 | * Returns: |
| 194 | * OK if teardown succeeded. |
| 195 | * INVALID_OPERATION if not in the CONFIGURED state |
| 196 | * NO_INIT in case of a serious error from the HAL device |
| 197 | */ |
| 198 | virtual status_t tearDown() = 0; |
| 199 | |
| 200 | /** |
Igor Murashkin | ae500e5 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 201 | * Fill in the camera3_stream_buffer with the next valid buffer for this |
| 202 | * stream, to hand over to the HAL. |
| 203 | * |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 204 | * Multiple surfaces could share the same HAL stream, but a request may |
| 205 | * be only for a subset of surfaces. In this case, the |
| 206 | * Camera3StreamInterface object needs the surface ID information to acquire |
| 207 | * buffers for those surfaces. For the case of single surface for a HAL |
| 208 | * stream, surface_ids parameter has no effect. |
| 209 | * |
Igor Murashkin | ae500e5 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 210 | * This method may only be called once finishConfiguration has been called. |
| 211 | * For bidirectional streams, this method applies to the output-side |
| 212 | * buffers. |
| 213 | * |
| 214 | */ |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 215 | virtual status_t getBuffer(camera3_stream_buffer *buffer, |
| 216 | const std::vector<size_t>& surface_ids = std::vector<size_t>()) = 0; |
Igor Murashkin | ae500e5 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 217 | |
| 218 | /** |
| 219 | * Return a buffer to the stream after use by the HAL. |
| 220 | * |
| 221 | * This method may only be called for buffers provided by getBuffer(). |
| 222 | * For bidirectional streams, this method applies to the output-side buffers |
| 223 | */ |
| 224 | virtual status_t returnBuffer(const camera3_stream_buffer &buffer, |
| 225 | nsecs_t timestamp) = 0; |
| 226 | |
| 227 | /** |
| 228 | * Fill in the camera3_stream_buffer with the next valid buffer for this |
| 229 | * stream, to hand over to the HAL. |
| 230 | * |
| 231 | * This method may only be called once finishConfiguration has been called. |
| 232 | * For bidirectional streams, this method applies to the input-side |
| 233 | * buffers. |
| 234 | * |
Eino-Ville Talvala | ba43525 | 2017-06-21 16:07:25 -0700 | [diff] [blame^] | 235 | * Normally this call will block until the handed out buffer count is less than the stream |
| 236 | * max buffer count; if respectHalLimit is set to false, this is ignored. |
Igor Murashkin | ae500e5 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 237 | */ |
Eino-Ville Talvala | ba43525 | 2017-06-21 16:07:25 -0700 | [diff] [blame^] | 238 | virtual status_t getInputBuffer(camera3_stream_buffer *buffer, bool respectHalLimit = true) = 0; |
Igor Murashkin | ae500e5 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 239 | |
| 240 | /** |
| 241 | * Return a buffer to the stream after use by the HAL. |
| 242 | * |
| 243 | * This method may only be called for buffers provided by getBuffer(). |
| 244 | * For bidirectional streams, this method applies to the input-side buffers |
| 245 | */ |
| 246 | virtual status_t returnInputBuffer(const camera3_stream_buffer &buffer) = 0; |
| 247 | |
| 248 | /** |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 249 | * Get the buffer producer of the input buffer queue. |
| 250 | * |
| 251 | * This method only applies to input streams. |
| 252 | */ |
| 253 | virtual status_t getInputBufferProducer(sp<IGraphicBufferProducer> *producer) = 0; |
| 254 | |
| 255 | /** |
Igor Murashkin | ae500e5 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 256 | * Whether any of the stream's buffers are currently in use by the HAL, |
| 257 | * including buffers that have been returned but not yet had their |
| 258 | * release fence signaled. |
| 259 | */ |
| 260 | virtual bool hasOutstandingBuffers() const = 0; |
| 261 | |
| 262 | enum { |
| 263 | TIMEOUT_NEVER = -1 |
| 264 | }; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 265 | |
Igor Murashkin | ae500e5 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 266 | /** |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 267 | * Set the state tracker to use for signaling idle transitions. |
Igor Murashkin | ae500e5 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 268 | */ |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 269 | virtual status_t setStatusTracker(sp<StatusTracker> statusTracker) = 0; |
Igor Murashkin | ae500e5 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 270 | |
| 271 | /** |
| 272 | * Disconnect stream from its non-HAL endpoint. After this, |
| 273 | * start/finishConfiguration must be called before the stream can be used |
| 274 | * again. This cannot be called if the stream has outstanding dequeued |
| 275 | * buffers. |
| 276 | */ |
| 277 | virtual status_t disconnect() = 0; |
| 278 | |
| 279 | /** |
Chien-Yu Chen | e8c535e | 2016-04-14 12:18:26 -0700 | [diff] [blame] | 280 | * Return if the buffer queue of the stream is abandoned. |
| 281 | */ |
| 282 | virtual bool isAbandoned() const = 0; |
| 283 | |
| 284 | /** |
Igor Murashkin | ae500e5 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 285 | * Debug dump of the stream's state. |
| 286 | */ |
| 287 | virtual void dump(int fd, const Vector<String16> &args) const = 0; |
| 288 | |
| 289 | virtual void addBufferListener( |
| 290 | wp<Camera3StreamBufferListener> listener) = 0; |
| 291 | virtual void removeBufferListener( |
| 292 | const sp<Camera3StreamBufferListener>& listener) = 0; |
Yin-Chia Yeh | be83fa7 | 2017-03-30 13:35:36 -0700 | [diff] [blame] | 293 | |
| 294 | /** |
| 295 | * Setting listner will remove previous listener (if exists) |
| 296 | * Only allow set listener during stream configuration because stream is guaranteed to be IDLE |
| 297 | * at this state, so setBufferFreedListener won't collide with onBufferFreed callbacks. |
| 298 | * Client is responsible to keep the listener object alive throughout the lifecycle of this |
| 299 | * Camera3Stream. |
| 300 | */ |
| 301 | virtual void setBufferFreedListener(Camera3StreamBufferFreedListener* listener) = 0; |
Igor Murashkin | ae500e5 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 302 | }; |
| 303 | |
| 304 | } // namespace camera3 |
| 305 | |
| 306 | } // namespace android |
| 307 | |
| 308 | #endif |