Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [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_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 | |
| 25 | #include "hardware/camera3.h" |
| 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | namespace camera3 { |
| 30 | |
| 31 | /** |
| 32 | * A class for managing a single stream of input or output data from the camera |
| 33 | * device. |
| 34 | * |
| 35 | * The stream has an internal state machine to track whether it's |
| 36 | * connected/configured/etc. |
| 37 | * |
| 38 | * States: |
| 39 | * |
| 40 | * STATE_ERROR: A serious error has occurred, stream is unusable. Outstanding |
| 41 | * buffers may still be returned. |
| 42 | * |
| 43 | * STATE_CONSTRUCTED: The stream is ready for configuration, but buffers cannot |
| 44 | * be gotten yet. Not connected to any endpoint, no buffers are registered |
| 45 | * with the HAL. |
| 46 | * |
| 47 | * STATE_IN_CONFIG: Configuration has started, but not yet concluded. During this |
| 48 | * time, the usage, max_buffers, and priv fields of camera3_stream returned by |
| 49 | * startConfiguration() may be modified. |
| 50 | * |
| 51 | * STATE_IN_RE_CONFIG: Configuration has started, and the stream has been |
| 52 | * configured before. Need to track separately from IN_CONFIG to avoid |
| 53 | * re-registering buffers with HAL. |
| 54 | * |
| 55 | * STATE_CONFIGURED: Stream is configured, and has registered buffers with the |
| 56 | * HAL. The stream's getBuffer/returnBuffer work. The priv pointer may still be |
| 57 | * modified. |
| 58 | * |
| 59 | * Transition table: |
| 60 | * |
| 61 | * <none> => STATE_CONSTRUCTED: |
| 62 | * When constructed with valid arguments |
| 63 | * <none> => STATE_ERROR: |
| 64 | * When constructed with invalid arguments |
| 65 | * STATE_CONSTRUCTED => STATE_IN_CONFIG: |
| 66 | * When startConfiguration() is called |
| 67 | * STATE_IN_CONFIG => STATE_CONFIGURED: |
| 68 | * When finishConfiguration() is called |
| 69 | * STATE_IN_CONFIG => STATE_ERROR: |
| 70 | * When finishConfiguration() fails to allocate or register buffers. |
| 71 | * STATE_CONFIGURED => STATE_IN_RE_CONFIG: * |
| 72 | * When startConfiguration() is called again, after making sure stream is |
| 73 | * idle with waitUntilIdle(). |
| 74 | * STATE_IN_RE_CONFIG => STATE_CONFIGURED: |
| 75 | * When finishConfiguration() is called. |
| 76 | * STATE_IN_RE_CONFIG => STATE_ERROR: |
| 77 | * When finishConfiguration() fails to allocate or register buffers. |
| 78 | * STATE_CONFIGURED => STATE_CONSTRUCTED: |
| 79 | * When disconnect() is called after making sure stream is idle with |
| 80 | * waitUntilIdle(). |
| 81 | */ |
| 82 | class Camera3Stream : |
| 83 | protected camera3_stream, |
| 84 | public LightRefBase<Camera3Stream> { |
| 85 | public: |
| 86 | |
| 87 | virtual ~Camera3Stream(); |
| 88 | |
| 89 | static Camera3Stream* cast(camera3_stream *stream); |
| 90 | static const Camera3Stream* cast(const camera3_stream *stream); |
| 91 | |
| 92 | /** |
| 93 | * Get the stream's ID |
| 94 | */ |
| 95 | int getId() const; |
| 96 | |
| 97 | /** |
| 98 | * Get the stream's dimensions and format |
| 99 | */ |
| 100 | uint32_t getWidth() const; |
| 101 | uint32_t getHeight() const; |
| 102 | int getFormat() const; |
| 103 | |
| 104 | /** |
| 105 | * Start the stream configuration process. Returns a handle to the stream's |
| 106 | * information to be passed into the HAL device's configure_streams call. |
| 107 | * |
| 108 | * Until finishConfiguration() is called, no other methods on the stream may be |
| 109 | * called. The usage and max_buffers fields of camera3_stream may be modified |
| 110 | * between start/finishConfiguration, but may not be changed after that. |
| 111 | * The priv field of camera3_stream may be modified at any time after |
| 112 | * startConfiguration. |
| 113 | * |
| 114 | * Returns NULL in case of error starting configuration. |
| 115 | */ |
| 116 | camera3_stream* startConfiguration(); |
| 117 | |
| 118 | /** |
| 119 | * Check if the stream is mid-configuration (start has been called, but not |
| 120 | * finish). Used for lazy completion of configuration. |
| 121 | */ |
| 122 | bool isConfiguring() const; |
| 123 | |
| 124 | /** |
| 125 | * Completes the stream configuration process. During this call, the stream |
| 126 | * may call the device's register_stream_buffers() method. The stream |
| 127 | * information structure returned by startConfiguration() may no longer be |
| 128 | * modified after this call, but can still be read until the destruction of |
| 129 | * the stream. |
| 130 | * |
| 131 | * Returns: |
| 132 | * OK on a successful configuration |
| 133 | * NO_INIT in case of a serious error from the HAL device |
| 134 | * NO_MEMORY in case of an error registering buffers |
| 135 | * INVALID_OPERATION in case connecting to the consumer failed |
| 136 | */ |
| 137 | status_t finishConfiguration(camera3_device *hal3Device); |
| 138 | |
| 139 | /** |
| 140 | * Fill in the camera3_stream_buffer with the next valid buffer for this |
| 141 | * stream, to hand over to the HAL. |
| 142 | * |
| 143 | * This method may only be called once finishConfiguration has been called. |
| 144 | * For bidirectional streams, this method applies to the output-side |
| 145 | * buffers. |
| 146 | * |
| 147 | */ |
| 148 | status_t getBuffer(camera3_stream_buffer *buffer); |
| 149 | |
| 150 | /** |
| 151 | * Return a buffer to the stream after use by the HAL. |
| 152 | * |
| 153 | * This method may only be called for buffers provided by getBuffer(). |
| 154 | * For bidirectional streams, this method applies to the output-side buffers |
| 155 | */ |
| 156 | status_t returnBuffer(const camera3_stream_buffer &buffer, |
| 157 | nsecs_t timestamp); |
| 158 | |
| 159 | /** |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame^] | 160 | * Fill in the camera3_stream_buffer with the next valid buffer for this |
| 161 | * stream, to hand over to the HAL. |
| 162 | * |
| 163 | * This method may only be called once finishConfiguration has been called. |
| 164 | * For bidirectional streams, this method applies to the input-side |
| 165 | * buffers. |
| 166 | * |
| 167 | */ |
| 168 | status_t getInputBuffer(camera3_stream_buffer *buffer); |
| 169 | |
| 170 | /** |
| 171 | * Return a buffer to the stream after use by the HAL. |
| 172 | * |
| 173 | * This method may only be called for buffers provided by getBuffer(). |
| 174 | * For bidirectional streams, this method applies to the input-side buffers |
| 175 | */ |
| 176 | status_t returnInputBuffer(const camera3_stream_buffer &buffer); |
| 177 | |
| 178 | /** |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 179 | * Whether any of the stream's buffers are currently in use by the HAL, |
| 180 | * including buffers that have been returned but not yet had their |
| 181 | * release fence signaled. |
| 182 | */ |
| 183 | bool hasOutstandingBuffers() const; |
| 184 | |
| 185 | enum { |
| 186 | TIMEOUT_NEVER = -1 |
| 187 | }; |
| 188 | /** |
| 189 | * Wait until the HAL is done with all of this stream's buffers, including |
| 190 | * signalling all release fences. Returns TIMED_OUT if the timeout is exceeded, |
| 191 | * OK on success. Pass in TIMEOUT_NEVER for timeout to indicate an indefinite wait. |
| 192 | */ |
| 193 | virtual status_t waitUntilIdle(nsecs_t timeout) = 0; |
| 194 | |
| 195 | /** |
| 196 | * Disconnect stream from its non-HAL endpoint. After this, |
| 197 | * start/finishConfiguration must be called before the stream can be used |
| 198 | * again. This cannot be called if the stream has outstanding dequeued |
| 199 | * buffers. |
| 200 | */ |
| 201 | status_t disconnect(); |
| 202 | |
| 203 | /** |
| 204 | * Debug dump of the stream's state. |
| 205 | */ |
| 206 | virtual void dump(int fd, const Vector<String16> &args) const = 0; |
| 207 | |
| 208 | protected: |
| 209 | const int mId; |
| 210 | const String8 mName; |
| 211 | // Zero for formats with fixed buffer size for given dimensions. |
| 212 | const size_t mMaxSize; |
| 213 | |
| 214 | enum { |
| 215 | STATE_ERROR, |
| 216 | STATE_CONSTRUCTED, |
| 217 | STATE_IN_CONFIG, |
| 218 | STATE_IN_RECONFIG, |
| 219 | STATE_CONFIGURED |
| 220 | } mState; |
| 221 | |
| 222 | mutable Mutex mLock; |
| 223 | |
| 224 | Camera3Stream(int id, camera3_stream_type type, |
| 225 | uint32_t width, uint32_t height, size_t maxSize, int format); |
| 226 | |
| 227 | /** |
| 228 | * Interface to be implemented by derived classes |
| 229 | */ |
| 230 | |
| 231 | // getBuffer / returnBuffer implementations |
| 232 | |
| 233 | // Since camera3_stream_buffer includes a raw pointer to the stream, |
| 234 | // cast to camera3_stream*, implementations must increment the |
| 235 | // refcount of the stream manually in getBufferLocked, and decrement it in |
| 236 | // returnBufferLocked. |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame^] | 237 | virtual status_t getBufferLocked(camera3_stream_buffer *buffer); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 238 | virtual status_t returnBufferLocked(const camera3_stream_buffer &buffer, |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame^] | 239 | nsecs_t timestamp); |
| 240 | virtual status_t getInputBufferLocked(camera3_stream_buffer *buffer); |
| 241 | virtual status_t returnInputBufferLocked( |
| 242 | const camera3_stream_buffer &buffer); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 243 | virtual bool hasOutstandingBuffersLocked() const = 0; |
| 244 | virtual status_t disconnectLocked() = 0; |
| 245 | |
| 246 | // Configure the buffer queue interface to the other end of the stream, |
| 247 | // after the HAL has provided usage and max_buffers values. After this call, |
| 248 | // the stream must be ready to produce all buffers for registration with |
| 249 | // HAL. |
| 250 | virtual status_t configureQueueLocked() = 0; |
| 251 | |
| 252 | // Get the total number of buffers in the queue |
| 253 | virtual size_t getBufferCountLocked() = 0; |
| 254 | |
| 255 | private: |
| 256 | static const unsigned int kRegisterFenceTimeoutMs = 5000; |
| 257 | |
| 258 | uint32_t oldUsage; |
| 259 | uint32_t oldMaxBuffers; |
| 260 | |
| 261 | // Gets all buffers from endpoint and registers them with the HAL. |
| 262 | status_t registerBuffersLocked(camera3_device *hal3Device); |
| 263 | |
| 264 | }; // class Camera3Stream |
| 265 | |
| 266 | }; // namespace camera3 |
| 267 | |
| 268 | }; // namespace android |
| 269 | |
| 270 | #endif |