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