Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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_CAMERA_CAMERA2DEVICE_H |
| 18 | #define ANDROID_SERVERS_CAMERA_CAMERA2DEVICE_H |
| 19 | |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 20 | #include <utils/Condition.h> |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 21 | #include <utils/Errors.h> |
Eino-Ville Talvala | 3297daa | 2012-06-14 10:49:45 -0700 | [diff] [blame] | 22 | #include <utils/List.h> |
| 23 | #include <utils/Mutex.h> |
Eino-Ville Talvala | 3297daa | 2012-06-14 10:49:45 -0700 | [diff] [blame] | 24 | |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame^] | 25 | #include "common/CameraDeviceBase.h" |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 29 | /** |
| 30 | * CameraDevice for HAL devices with version CAMERA_DEVICE_API_VERSION_2_0 |
| 31 | */ |
| 32 | class Camera2Device: public CameraDeviceBase { |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 33 | public: |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 34 | Camera2Device(int id); |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 35 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 36 | virtual ~Camera2Device(); |
Eino-Ville Talvala | 3297daa | 2012-06-14 10:49:45 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 39 | * CameraDevice interface |
Eino-Ville Talvala | 3297daa | 2012-06-14 10:49:45 -0700 | [diff] [blame] | 40 | */ |
Igor Murashkin | 7138105 | 2013-03-04 14:53:08 -0800 | [diff] [blame] | 41 | virtual int getId() const; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 42 | virtual status_t initialize(camera_module_t *module); |
| 43 | virtual status_t disconnect(); |
| 44 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 45 | virtual const CameraMetadata& info() const; |
| 46 | virtual status_t capture(CameraMetadata &request); |
| 47 | virtual status_t setStreamingRequest(const CameraMetadata &request); |
| 48 | virtual status_t clearStreamingRequest(); |
| 49 | virtual status_t waitUntilRequestReceived(int32_t requestId, nsecs_t timeout); |
| 50 | virtual status_t createStream(sp<ANativeWindow> consumer, |
Eino-Ville Talvala | d4bcfde | 2012-06-07 17:12:38 -0700 | [diff] [blame] | 51 | uint32_t width, uint32_t height, int format, size_t size, |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 52 | int *id); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 53 | virtual status_t createReprocessStreamFromStream(int outputId, int *id); |
| 54 | virtual status_t getStreamInfo(int id, |
Eino-Ville Talvala | d4bcfde | 2012-06-07 17:12:38 -0700 | [diff] [blame] | 55 | uint32_t *width, uint32_t *height, uint32_t *format); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 56 | virtual status_t setStreamTransform(int id, int transform); |
| 57 | virtual status_t deleteStream(int id); |
| 58 | virtual status_t deleteReprocessStream(int id); |
| 59 | virtual status_t createDefaultRequest(int templateId, CameraMetadata *request); |
| 60 | virtual status_t waitUntilDrained(); |
| 61 | virtual status_t setNotifyCallback(NotificationListener *listener); |
Eino-Ville Talvala | 46910bd | 2013-07-18 19:15:17 -0700 | [diff] [blame] | 62 | virtual bool willNotify3A(); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 63 | virtual status_t waitForNextFrame(nsecs_t timeout); |
| 64 | virtual status_t getNextFrame(CameraMetadata *frame); |
| 65 | virtual status_t triggerAutofocus(uint32_t id); |
| 66 | virtual status_t triggerCancelAutofocus(uint32_t id); |
| 67 | virtual status_t triggerPrecaptureMetering(uint32_t id); |
| 68 | virtual status_t pushReprocessBuffer(int reprocessStreamId, |
Eino-Ville Talvala | 69230df | 2012-08-29 17:37:16 -0700 | [diff] [blame] | 69 | buffer_handle_t *buffer, wp<BufferReleasedListener> listener); |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 70 | private: |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 71 | const int mId; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 72 | camera2_device_t *mHal2Device; |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 73 | |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 74 | CameraMetadata mDeviceInfo; |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 75 | vendor_tag_query_ops_t *mVendorTagOps; |
| 76 | |
| 77 | /** |
| 78 | * Queue class for both sending requests to a camera2 device, and for |
| 79 | * receiving frames from a camera2 device. |
| 80 | */ |
| 81 | class MetadataQueue: public camera2_request_queue_src_ops_t, |
| 82 | public camera2_frame_queue_dst_ops_t { |
| 83 | public: |
| 84 | MetadataQueue(); |
| 85 | ~MetadataQueue(); |
| 86 | |
| 87 | // Interface to camera2 HAL device, either for requests (device is |
| 88 | // consumer) or for frames (device is producer) |
| 89 | const camera2_request_queue_src_ops_t* getToConsumerInterface(); |
| 90 | void setFromConsumerInterface(camera2_device_t *d); |
| 91 | |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 92 | // Connect queue consumer endpoint to a camera2 device |
| 93 | status_t setConsumerDevice(camera2_device_t *d); |
| 94 | // Connect queue producer endpoint to a camera2 device |
| 95 | status_t setProducerDevice(camera2_device_t *d); |
| 96 | |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 97 | const camera2_frame_queue_dst_ops_t* getToProducerInterface(); |
| 98 | |
| 99 | // Real interfaces. On enqueue, queue takes ownership of buffer pointer |
| 100 | // On dequeue, user takes ownership of buffer pointer. |
| 101 | status_t enqueue(camera_metadata_t *buf); |
Eino-Ville Talvala | fbd6066 | 2012-10-16 10:28:07 -0700 | [diff] [blame] | 102 | status_t dequeue(camera_metadata_t **buf, bool incrementCount = false); |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 103 | int getBufferCount(); |
| 104 | status_t waitForBuffer(nsecs_t timeout); |
Eino-Ville Talvala | 4c9eb71 | 2012-10-02 13:30:28 -0700 | [diff] [blame] | 105 | // Wait until a buffer with the given ID is dequeued. Will return |
| 106 | // immediately if the latest buffer dequeued has that ID. |
| 107 | status_t waitForDequeue(int32_t id, nsecs_t timeout); |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 108 | |
| 109 | // Set repeating buffer(s); if the queue is empty on a dequeue call, the |
| 110 | // queue copies the contents of the stream slot into the queue, and then |
Eino-Ville Talvala | 6ed1ed1 | 2012-06-07 10:46:38 -0700 | [diff] [blame] | 111 | // dequeues the first new entry. The metadata buffers passed in are |
| 112 | // copied. |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 113 | status_t setStreamSlot(camera_metadata_t *buf); |
| 114 | status_t setStreamSlot(const List<camera_metadata_t*> &bufs); |
| 115 | |
Eino-Ville Talvala | 3297daa | 2012-06-14 10:49:45 -0700 | [diff] [blame] | 116 | status_t dump(int fd, const Vector<String16>& args); |
| 117 | |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 118 | private: |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 119 | status_t signalConsumerLocked(); |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 120 | status_t freeBuffers(List<camera_metadata_t*>::iterator start, |
| 121 | List<camera_metadata_t*>::iterator end); |
| 122 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 123 | camera2_device_t *mHal2Device; |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 124 | |
| 125 | Mutex mMutex; |
| 126 | Condition notEmpty; |
| 127 | |
| 128 | int mFrameCount; |
Eino-Ville Talvala | 4c9eb71 | 2012-10-02 13:30:28 -0700 | [diff] [blame] | 129 | int32_t mLatestRequestId; |
| 130 | Condition mNewRequestId; |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 131 | |
| 132 | int mCount; |
| 133 | List<camera_metadata_t*> mEntries; |
| 134 | int mStreamSlotCount; |
| 135 | List<camera_metadata_t*> mStreamSlot; |
| 136 | |
| 137 | bool mSignalConsumer; |
| 138 | |
| 139 | static MetadataQueue* getInstance( |
| 140 | const camera2_frame_queue_dst_ops_t *q); |
| 141 | static MetadataQueue* getInstance( |
| 142 | const camera2_request_queue_src_ops_t *q); |
| 143 | |
| 144 | static int consumer_buffer_count( |
| 145 | const camera2_request_queue_src_ops_t *q); |
| 146 | |
| 147 | static int consumer_dequeue(const camera2_request_queue_src_ops_t *q, |
| 148 | camera_metadata_t **buffer); |
| 149 | |
| 150 | static int consumer_free(const camera2_request_queue_src_ops_t *q, |
| 151 | camera_metadata_t *old_buffer); |
| 152 | |
| 153 | static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q, |
| 154 | size_t entries, size_t bytes, |
| 155 | camera_metadata_t **buffer); |
| 156 | |
| 157 | static int producer_cancel(const camera2_frame_queue_dst_ops_t *q, |
| 158 | camera_metadata_t *old_buffer); |
| 159 | |
| 160 | static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q, |
| 161 | camera_metadata_t *filled_buffer); |
| 162 | |
| 163 | }; // class MetadataQueue |
| 164 | |
| 165 | MetadataQueue mRequestQueue; |
| 166 | MetadataQueue mFrameQueue; |
| 167 | |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 168 | /** |
| 169 | * Adapter from an ANativeWindow interface to camera2 device stream ops. |
| 170 | * Also takes care of allocating/deallocating stream in device interface |
| 171 | */ |
| 172 | class StreamAdapter: public camera2_stream_ops, public virtual RefBase { |
| 173 | public: |
| 174 | StreamAdapter(camera2_device_t *d); |
| 175 | |
| 176 | ~StreamAdapter(); |
| 177 | |
Eino-Ville Talvala | d4bcfde | 2012-06-07 17:12:38 -0700 | [diff] [blame] | 178 | /** |
| 179 | * Create a HAL device stream of the requested size and format. |
| 180 | * |
| 181 | * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device |
| 182 | * selects an appropriate format; it can be queried with getFormat. |
| 183 | * |
| 184 | * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must |
| 185 | * be equal to the size in bytes of the buffers to allocate for the |
| 186 | * stream. For other formats, the size parameter is ignored. |
| 187 | */ |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 188 | status_t connectToDevice(sp<ANativeWindow> consumer, |
Eino-Ville Talvala | d4bcfde | 2012-06-07 17:12:38 -0700 | [diff] [blame] | 189 | uint32_t width, uint32_t height, int format, size_t size); |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 190 | |
Eino-Ville Talvala | 9cca4c6 | 2012-06-15 15:41:44 -0700 | [diff] [blame] | 191 | status_t release(); |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 192 | |
Eino-Ville Talvala | c94cd19 | 2012-06-15 12:47:42 -0700 | [diff] [blame] | 193 | status_t setTransform(int transform); |
| 194 | |
Eino-Ville Talvala | d4bcfde | 2012-06-07 17:12:38 -0700 | [diff] [blame] | 195 | // Get stream parameters. |
| 196 | // Only valid after a successful connectToDevice call. |
| 197 | int getId() const { return mId; } |
| 198 | uint32_t getWidth() const { return mWidth; } |
| 199 | uint32_t getHeight() const { return mHeight; } |
| 200 | uint32_t getFormat() const { return mFormat; } |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 201 | |
Eino-Ville Talvala | 3297daa | 2012-06-14 10:49:45 -0700 | [diff] [blame] | 202 | // Dump stream information |
| 203 | status_t dump(int fd, const Vector<String16>& args); |
| 204 | |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 205 | private: |
| 206 | enum { |
| 207 | ERROR = -1, |
Eino-Ville Talvala | 9cca4c6 | 2012-06-15 15:41:44 -0700 | [diff] [blame] | 208 | RELEASED = 0, |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 209 | ALLOCATED, |
| 210 | CONNECTED, |
| 211 | ACTIVE |
| 212 | } mState; |
| 213 | |
| 214 | sp<ANativeWindow> mConsumerInterface; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 215 | camera2_device_t *mHal2Device; |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 216 | |
| 217 | uint32_t mId; |
| 218 | uint32_t mWidth; |
| 219 | uint32_t mHeight; |
| 220 | uint32_t mFormat; |
Eino-Ville Talvala | d4bcfde | 2012-06-07 17:12:38 -0700 | [diff] [blame] | 221 | size_t mSize; |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 222 | uint32_t mUsage; |
| 223 | uint32_t mMaxProducerBuffers; |
| 224 | uint32_t mMaxConsumerBuffers; |
Eino-Ville Talvala | 3297daa | 2012-06-14 10:49:45 -0700 | [diff] [blame] | 225 | uint32_t mTotalBuffers; |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 226 | int mFormatRequested; |
| 227 | |
Eino-Ville Talvala | 3297daa | 2012-06-14 10:49:45 -0700 | [diff] [blame] | 228 | /** Debugging information */ |
| 229 | uint32_t mActiveBuffers; |
| 230 | uint32_t mFrameCount; |
| 231 | int64_t mLastTimestamp; |
| 232 | |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 233 | const camera2_stream_ops *getStreamOps(); |
| 234 | |
| 235 | static ANativeWindow* toANW(const camera2_stream_ops_t *w); |
| 236 | |
| 237 | static int dequeue_buffer(const camera2_stream_ops_t *w, |
| 238 | buffer_handle_t** buffer); |
| 239 | |
| 240 | static int enqueue_buffer(const camera2_stream_ops_t* w, |
| 241 | int64_t timestamp, |
| 242 | buffer_handle_t* buffer); |
| 243 | |
| 244 | static int cancel_buffer(const camera2_stream_ops_t* w, |
| 245 | buffer_handle_t* buffer); |
| 246 | |
| 247 | static int set_crop(const camera2_stream_ops_t* w, |
| 248 | int left, int top, int right, int bottom); |
| 249 | }; // class StreamAdapter |
| 250 | |
| 251 | typedef List<sp<StreamAdapter> > StreamList; |
| 252 | StreamList mStreams; |
| 253 | |
Eino-Ville Talvala | 69230df | 2012-08-29 17:37:16 -0700 | [diff] [blame] | 254 | /** |
| 255 | * Adapter from an ANativeWindow interface to camera2 device stream ops. |
| 256 | * Also takes care of allocating/deallocating stream in device interface |
| 257 | */ |
| 258 | class ReprocessStreamAdapter: public camera2_stream_in_ops, public virtual RefBase { |
| 259 | public: |
| 260 | ReprocessStreamAdapter(camera2_device_t *d); |
| 261 | |
| 262 | ~ReprocessStreamAdapter(); |
| 263 | |
| 264 | /** |
| 265 | * Create a HAL device reprocess stream based on an existing output stream. |
| 266 | */ |
| 267 | status_t connectToDevice(const sp<StreamAdapter> &outputStream); |
| 268 | |
| 269 | status_t release(); |
| 270 | |
| 271 | /** |
| 272 | * Push buffer into stream for reprocessing. Takes ownership until it notifies |
| 273 | * that the buffer has been released |
| 274 | */ |
| 275 | status_t pushIntoStream(buffer_handle_t *handle, |
| 276 | const wp<BufferReleasedListener> &releaseListener); |
| 277 | |
| 278 | /** |
| 279 | * Get stream parameters. |
| 280 | * Only valid after a successful connectToDevice call. |
| 281 | */ |
| 282 | int getId() const { return mId; } |
| 283 | uint32_t getWidth() const { return mWidth; } |
| 284 | uint32_t getHeight() const { return mHeight; } |
| 285 | uint32_t getFormat() const { return mFormat; } |
| 286 | |
| 287 | // Dump stream information |
| 288 | status_t dump(int fd, const Vector<String16>& args); |
| 289 | |
| 290 | private: |
| 291 | enum { |
| 292 | ERROR = -1, |
| 293 | RELEASED = 0, |
| 294 | ACTIVE |
| 295 | } mState; |
| 296 | |
| 297 | sp<ANativeWindow> mConsumerInterface; |
| 298 | wp<StreamAdapter> mBaseStream; |
| 299 | |
| 300 | struct QueueEntry { |
| 301 | buffer_handle_t *handle; |
| 302 | wp<BufferReleasedListener> releaseListener; |
| 303 | }; |
| 304 | |
| 305 | List<QueueEntry> mQueue; |
| 306 | |
| 307 | List<QueueEntry> mInFlightQueue; |
| 308 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 309 | camera2_device_t *mHal2Device; |
Eino-Ville Talvala | 69230df | 2012-08-29 17:37:16 -0700 | [diff] [blame] | 310 | |
| 311 | uint32_t mId; |
| 312 | uint32_t mWidth; |
| 313 | uint32_t mHeight; |
| 314 | uint32_t mFormat; |
| 315 | |
| 316 | /** Debugging information */ |
| 317 | uint32_t mActiveBuffers; |
| 318 | uint32_t mFrameCount; |
| 319 | int64_t mLastTimestamp; |
| 320 | |
| 321 | const camera2_stream_in_ops *getStreamOps(); |
| 322 | |
| 323 | static int acquire_buffer(const camera2_stream_in_ops_t *w, |
| 324 | buffer_handle_t** buffer); |
| 325 | |
| 326 | static int release_buffer(const camera2_stream_in_ops_t* w, |
| 327 | buffer_handle_t* buffer); |
| 328 | |
| 329 | }; // class ReprocessStreamAdapter |
| 330 | |
| 331 | typedef List<sp<ReprocessStreamAdapter> > ReprocessStreamList; |
| 332 | ReprocessStreamList mReprocessStreams; |
| 333 | |
Eino-Ville Talvala | 160d4af | 2012-08-03 09:40:16 -0700 | [diff] [blame] | 334 | // Receives HAL notifications and routes them to the NotificationListener |
| 335 | static void notificationCallback(int32_t msg_type, |
| 336 | int32_t ext1, |
| 337 | int32_t ext2, |
| 338 | int32_t ext3, |
| 339 | void *user); |
| 340 | |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 341 | }; // class Camera2Device |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 342 | |
| 343 | }; // namespace android |
| 344 | |
| 345 | #endif |