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> |
| 24 | #include <utils/RefBase.h> |
| 25 | #include <utils/String8.h> |
| 26 | #include <utils/String16.h> |
| 27 | #include <utils/Vector.h> |
| 28 | |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 29 | #include "hardware/camera2.h" |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | class Camera2Device : public virtual RefBase { |
| 34 | public: |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 35 | Camera2Device(int id); |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 36 | |
| 37 | ~Camera2Device(); |
| 38 | |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 39 | status_t initialize(camera_module_t *module); |
| 40 | |
Eino-Ville Talvala | 3297daa | 2012-06-14 10:49:45 -0700 | [diff] [blame^] | 41 | status_t dump(int fd, const Vector<String16>& args); |
| 42 | |
| 43 | /** |
| 44 | * Get a pointer to the device's static characteristics metadata buffer |
| 45 | */ |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 46 | camera_metadata_t* info(); |
| 47 | |
Eino-Ville Talvala | d4bcfde | 2012-06-07 17:12:38 -0700 | [diff] [blame] | 48 | /** |
| 49 | * Submit request for capture. The Camera2Device takes ownership of the |
| 50 | * passed-in buffer. |
| 51 | */ |
| 52 | status_t capture(camera_metadata_t *request); |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 53 | |
Eino-Ville Talvala | d4bcfde | 2012-06-07 17:12:38 -0700 | [diff] [blame] | 54 | /** |
| 55 | * Submit request for streaming. The Camera2Device makes a copy of the |
| 56 | * passed-in buffer and the caller retains ownership. |
| 57 | */ |
| 58 | status_t setStreamingRequest(camera_metadata_t *request); |
| 59 | |
| 60 | /** |
| 61 | * Create an output stream of the requested size and format. |
| 62 | * |
| 63 | * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device selects |
| 64 | * an appropriate format; it can be queried with getStreamInfo. |
| 65 | * |
| 66 | * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must be |
| 67 | * equal to the size in bytes of the buffers to allocate for the stream. For |
| 68 | * other formats, the size parameter is ignored. |
| 69 | */ |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 70 | status_t createStream(sp<ANativeWindow> consumer, |
Eino-Ville Talvala | d4bcfde | 2012-06-07 17:12:38 -0700 | [diff] [blame] | 71 | uint32_t width, uint32_t height, int format, size_t size, |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 72 | int *id); |
| 73 | |
Eino-Ville Talvala | d4bcfde | 2012-06-07 17:12:38 -0700 | [diff] [blame] | 74 | /** |
| 75 | * Get information about a given stream. |
| 76 | */ |
| 77 | status_t getStreamInfo(int id, |
| 78 | uint32_t *width, uint32_t *height, uint32_t *format); |
| 79 | |
| 80 | /** |
| 81 | * Delete stream. Must not be called if there are requests in flight which |
| 82 | * reference that stream. |
| 83 | */ |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 84 | status_t deleteStream(int id); |
| 85 | |
Eino-Ville Talvala | d4bcfde | 2012-06-07 17:12:38 -0700 | [diff] [blame] | 86 | /** |
| 87 | * Create a metadata buffer with fields that the HAL device believes are |
| 88 | * best for the given use case |
| 89 | */ |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 90 | status_t createDefaultRequest(int templateId, |
| 91 | camera_metadata_t **request); |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 92 | |
Eino-Ville Talvala | d4bcfde | 2012-06-07 17:12:38 -0700 | [diff] [blame] | 93 | /** |
| 94 | * Wait until all requests have been processed. Returns INVALID_OPERATION if |
| 95 | * the streaming slot is not empty, or TIMED_OUT if the requests haven't |
| 96 | * finished processing in 10 seconds. |
| 97 | */ |
| 98 | status_t waitUntilDrained(); |
| 99 | |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 100 | private: |
| 101 | |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 102 | const int mId; |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 103 | camera2_device_t *mDevice; |
| 104 | |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 105 | camera_metadata_t *mDeviceInfo; |
| 106 | vendor_tag_query_ops_t *mVendorTagOps; |
| 107 | |
| 108 | /** |
| 109 | * Queue class for both sending requests to a camera2 device, and for |
| 110 | * receiving frames from a camera2 device. |
| 111 | */ |
| 112 | class MetadataQueue: public camera2_request_queue_src_ops_t, |
| 113 | public camera2_frame_queue_dst_ops_t { |
| 114 | public: |
| 115 | MetadataQueue(); |
| 116 | ~MetadataQueue(); |
| 117 | |
| 118 | // Interface to camera2 HAL device, either for requests (device is |
| 119 | // consumer) or for frames (device is producer) |
| 120 | const camera2_request_queue_src_ops_t* getToConsumerInterface(); |
| 121 | void setFromConsumerInterface(camera2_device_t *d); |
| 122 | |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 123 | // Connect queue consumer endpoint to a camera2 device |
| 124 | status_t setConsumerDevice(camera2_device_t *d); |
| 125 | // Connect queue producer endpoint to a camera2 device |
| 126 | status_t setProducerDevice(camera2_device_t *d); |
| 127 | |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 128 | const camera2_frame_queue_dst_ops_t* getToProducerInterface(); |
| 129 | |
| 130 | // Real interfaces. On enqueue, queue takes ownership of buffer pointer |
| 131 | // On dequeue, user takes ownership of buffer pointer. |
| 132 | status_t enqueue(camera_metadata_t *buf); |
| 133 | status_t dequeue(camera_metadata_t **buf, bool incrementCount = true); |
| 134 | int getBufferCount(); |
| 135 | status_t waitForBuffer(nsecs_t timeout); |
| 136 | |
| 137 | // Set repeating buffer(s); if the queue is empty on a dequeue call, the |
| 138 | // 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] | 139 | // dequeues the first new entry. The metadata buffers passed in are |
| 140 | // copied. |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 141 | status_t setStreamSlot(camera_metadata_t *buf); |
| 142 | status_t setStreamSlot(const List<camera_metadata_t*> &bufs); |
| 143 | |
Eino-Ville Talvala | 3297daa | 2012-06-14 10:49:45 -0700 | [diff] [blame^] | 144 | status_t dump(int fd, const Vector<String16>& args); |
| 145 | |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 146 | private: |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 147 | status_t signalConsumerLocked(); |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 148 | status_t freeBuffers(List<camera_metadata_t*>::iterator start, |
| 149 | List<camera_metadata_t*>::iterator end); |
| 150 | |
| 151 | camera2_device_t *mDevice; |
| 152 | |
| 153 | Mutex mMutex; |
| 154 | Condition notEmpty; |
| 155 | |
| 156 | int mFrameCount; |
| 157 | |
| 158 | int mCount; |
| 159 | List<camera_metadata_t*> mEntries; |
| 160 | int mStreamSlotCount; |
| 161 | List<camera_metadata_t*> mStreamSlot; |
| 162 | |
| 163 | bool mSignalConsumer; |
| 164 | |
| 165 | static MetadataQueue* getInstance( |
| 166 | const camera2_frame_queue_dst_ops_t *q); |
| 167 | static MetadataQueue* getInstance( |
| 168 | const camera2_request_queue_src_ops_t *q); |
| 169 | |
| 170 | static int consumer_buffer_count( |
| 171 | const camera2_request_queue_src_ops_t *q); |
| 172 | |
| 173 | static int consumer_dequeue(const camera2_request_queue_src_ops_t *q, |
| 174 | camera_metadata_t **buffer); |
| 175 | |
| 176 | static int consumer_free(const camera2_request_queue_src_ops_t *q, |
| 177 | camera_metadata_t *old_buffer); |
| 178 | |
| 179 | static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q, |
| 180 | size_t entries, size_t bytes, |
| 181 | camera_metadata_t **buffer); |
| 182 | |
| 183 | static int producer_cancel(const camera2_frame_queue_dst_ops_t *q, |
| 184 | camera_metadata_t *old_buffer); |
| 185 | |
| 186 | static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q, |
| 187 | camera_metadata_t *filled_buffer); |
| 188 | |
| 189 | }; // class MetadataQueue |
| 190 | |
| 191 | MetadataQueue mRequestQueue; |
| 192 | MetadataQueue mFrameQueue; |
| 193 | |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 194 | /** |
| 195 | * Adapter from an ANativeWindow interface to camera2 device stream ops. |
| 196 | * Also takes care of allocating/deallocating stream in device interface |
| 197 | */ |
| 198 | class StreamAdapter: public camera2_stream_ops, public virtual RefBase { |
| 199 | public: |
| 200 | StreamAdapter(camera2_device_t *d); |
| 201 | |
| 202 | ~StreamAdapter(); |
| 203 | |
Eino-Ville Talvala | d4bcfde | 2012-06-07 17:12:38 -0700 | [diff] [blame] | 204 | /** |
| 205 | * Create a HAL device stream of the requested size and format. |
| 206 | * |
| 207 | * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device |
| 208 | * selects an appropriate format; it can be queried with getFormat. |
| 209 | * |
| 210 | * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must |
| 211 | * be equal to the size in bytes of the buffers to allocate for the |
| 212 | * stream. For other formats, the size parameter is ignored. |
| 213 | */ |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 214 | status_t connectToDevice(sp<ANativeWindow> consumer, |
Eino-Ville Talvala | d4bcfde | 2012-06-07 17:12:38 -0700 | [diff] [blame] | 215 | uint32_t width, uint32_t height, int format, size_t size); |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 216 | |
| 217 | status_t disconnect(); |
| 218 | |
Eino-Ville Talvala | d4bcfde | 2012-06-07 17:12:38 -0700 | [diff] [blame] | 219 | // Get stream parameters. |
| 220 | // Only valid after a successful connectToDevice call. |
| 221 | int getId() const { return mId; } |
| 222 | uint32_t getWidth() const { return mWidth; } |
| 223 | uint32_t getHeight() const { return mHeight; } |
| 224 | uint32_t getFormat() const { return mFormat; } |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 225 | |
Eino-Ville Talvala | 3297daa | 2012-06-14 10:49:45 -0700 | [diff] [blame^] | 226 | // Dump stream information |
| 227 | status_t dump(int fd, const Vector<String16>& args); |
| 228 | |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 229 | private: |
| 230 | enum { |
| 231 | ERROR = -1, |
| 232 | DISCONNECTED = 0, |
| 233 | ALLOCATED, |
| 234 | CONNECTED, |
| 235 | ACTIVE |
| 236 | } mState; |
| 237 | |
| 238 | sp<ANativeWindow> mConsumerInterface; |
| 239 | camera2_device_t *mDevice; |
| 240 | |
| 241 | uint32_t mId; |
| 242 | uint32_t mWidth; |
| 243 | uint32_t mHeight; |
| 244 | uint32_t mFormat; |
Eino-Ville Talvala | d4bcfde | 2012-06-07 17:12:38 -0700 | [diff] [blame] | 245 | size_t mSize; |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 246 | uint32_t mUsage; |
| 247 | uint32_t mMaxProducerBuffers; |
| 248 | uint32_t mMaxConsumerBuffers; |
Eino-Ville Talvala | 3297daa | 2012-06-14 10:49:45 -0700 | [diff] [blame^] | 249 | uint32_t mTotalBuffers; |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 250 | int mFormatRequested; |
| 251 | |
Eino-Ville Talvala | 3297daa | 2012-06-14 10:49:45 -0700 | [diff] [blame^] | 252 | /** Debugging information */ |
| 253 | uint32_t mActiveBuffers; |
| 254 | uint32_t mFrameCount; |
| 255 | int64_t mLastTimestamp; |
| 256 | |
Eino-Ville Talvala | 6db981c | 2012-05-21 18:54:30 -0700 | [diff] [blame] | 257 | const camera2_stream_ops *getStreamOps(); |
| 258 | |
| 259 | static ANativeWindow* toANW(const camera2_stream_ops_t *w); |
| 260 | |
| 261 | static int dequeue_buffer(const camera2_stream_ops_t *w, |
| 262 | buffer_handle_t** buffer); |
| 263 | |
| 264 | static int enqueue_buffer(const camera2_stream_ops_t* w, |
| 265 | int64_t timestamp, |
| 266 | buffer_handle_t* buffer); |
| 267 | |
| 268 | static int cancel_buffer(const camera2_stream_ops_t* w, |
| 269 | buffer_handle_t* buffer); |
| 270 | |
| 271 | static int set_crop(const camera2_stream_ops_t* w, |
| 272 | int left, int top, int right, int bottom); |
| 273 | }; // class StreamAdapter |
| 274 | |
| 275 | typedef List<sp<StreamAdapter> > StreamList; |
| 276 | StreamList mStreams; |
| 277 | |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 278 | }; // class Camera2Device |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 279 | |
| 280 | }; // namespace android |
| 281 | |
| 282 | #endif |