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