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 20 #include <utils/Condition.h> 21 #include <utils/Errors.h> 22 #include <utils/List.h> 23 #include <utils/Mutex.h> 24 25 #include "CameraDeviceBase.h" 26 27 namespace android { 28 29 /** 30 * CameraDevice for HAL devices with version CAMERA_DEVICE_API_VERSION_2_0 31 */ 32 class Camera2Device: public CameraDeviceBase { 33 public: 34 Camera2Device(int id); 35 36 virtual ~Camera2Device(); 37 38 /** 39 * CameraDevice interface 40 */ 41 virtual int getId() const; 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, 51 uint32_t width, uint32_t height, int format, size_t size, 52 int *id); 53 virtual status_t createReprocessStreamFromStream(int outputId, int *id); 54 virtual status_t getStreamInfo(int id, 55 uint32_t *width, uint32_t *height, uint32_t *format); 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 bool willNotify3A(); 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, 69 buffer_handle_t *buffer, wp<BufferReleasedListener> listener); 70 private: 71 const int mId; 72 camera2_device_t *mHal2Device; 73 74 CameraMetadata mDeviceInfo; 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 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 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); 102 status_t dequeue(camera_metadata_t **buf, bool incrementCount = false); 103 int getBufferCount(); 104 status_t waitForBuffer(nsecs_t timeout); 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); 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 111 // dequeues the first new entry. The metadata buffers passed in are 112 // copied. 113 status_t setStreamSlot(camera_metadata_t *buf); 114 status_t setStreamSlot(const List<camera_metadata_t*> &bufs); 115 116 status_t dump(int fd, const Vector<String16>& args); 117 118 private: 119 status_t signalConsumerLocked(); 120 status_t freeBuffers(List<camera_metadata_t*>::iterator start, 121 List<camera_metadata_t*>::iterator end); 122 123 camera2_device_t *mHal2Device; 124 125 Mutex mMutex; 126 Condition notEmpty; 127 128 int mFrameCount; 129 int32_t mLatestRequestId; 130 Condition mNewRequestId; 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 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 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 */ 188 status_t connectToDevice(sp<ANativeWindow> consumer, 189 uint32_t width, uint32_t height, int format, size_t size); 190 191 status_t release(); 192 193 status_t setTransform(int transform); 194 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; } 201 202 // Dump stream information 203 status_t dump(int fd, const Vector<String16>& args); 204 205 private: 206 enum { 207 ERROR = -1, 208 RELEASED = 0, 209 ALLOCATED, 210 CONNECTED, 211 ACTIVE 212 } mState; 213 214 sp<ANativeWindow> mConsumerInterface; 215 camera2_device_t *mHal2Device; 216 217 uint32_t mId; 218 uint32_t mWidth; 219 uint32_t mHeight; 220 uint32_t mFormat; 221 size_t mSize; 222 uint32_t mUsage; 223 uint32_t mMaxProducerBuffers; 224 uint32_t mMaxConsumerBuffers; 225 uint32_t mTotalBuffers; 226 int mFormatRequested; 227 228 /** Debugging information */ 229 uint32_t mActiveBuffers; 230 uint32_t mFrameCount; 231 int64_t mLastTimestamp; 232 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 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 309 camera2_device_t *mHal2Device; 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 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 341 }; // class Camera2Device 342 343 }; // namespace android 344 345 #endif 346