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#include <utils/RefBase.h> 25#include <utils/String8.h> 26#include <utils/String16.h> 27#include <utils/Vector.h> 28 29#include "hardware/camera2.h" 30#include "camera2/CameraMetadata.h" 31 32namespace android { 33 34class Camera2Device : public virtual RefBase { 35 public: 36 typedef camera2::CameraMetadata CameraMetadata; 37 38 Camera2Device(int id); 39 40 ~Camera2Device(); 41 42 status_t initialize(camera_module_t *module); 43 status_t disconnect(); 44 45 status_t dump(int fd, const Vector<String16>& args); 46 47 /** 48 * The device's static characteristics metadata buffer 49 */ 50 const CameraMetadata& info() const; 51 52 /** 53 * Submit request for capture. The Camera2Device takes ownership of the 54 * passed-in buffer. 55 */ 56 status_t capture(CameraMetadata &request); 57 58 /** 59 * Submit request for streaming. The Camera2Device makes a copy of the 60 * passed-in buffer and the caller retains ownership. 61 */ 62 status_t setStreamingRequest(const CameraMetadata &request); 63 64 /** 65 * Clear the streaming request slot. 66 */ 67 status_t clearStreamingRequest(); 68 69 /** 70 * Wait until a request with the given ID has been dequeued by the 71 * HAL. Returns TIMED_OUT if the timeout duration is reached. Returns 72 * immediately if the latest request received by the HAL has this id. 73 */ 74 status_t waitUntilRequestReceived(int32_t requestId, nsecs_t timeout); 75 76 /** 77 * Create an output stream of the requested size and format. 78 * 79 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device selects 80 * an appropriate format; it can be queried with getStreamInfo. 81 * 82 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must be 83 * equal to the size in bytes of the buffers to allocate for the stream. For 84 * other formats, the size parameter is ignored. 85 */ 86 status_t createStream(sp<ANativeWindow> consumer, 87 uint32_t width, uint32_t height, int format, size_t size, 88 int *id); 89 90 /** 91 * Create an input reprocess stream that uses buffers from an existing 92 * output stream. 93 */ 94 status_t createReprocessStreamFromStream(int outputId, int *id); 95 96 /** 97 * Get information about a given stream. 98 */ 99 status_t getStreamInfo(int id, 100 uint32_t *width, uint32_t *height, uint32_t *format); 101 102 /** 103 * Set stream gralloc buffer transform 104 */ 105 status_t setStreamTransform(int id, int transform); 106 107 /** 108 * Delete stream. Must not be called if there are requests in flight which 109 * reference that stream. 110 */ 111 status_t deleteStream(int id); 112 113 /** 114 * Delete reprocess stream. Must not be called if there are requests in 115 * flight which reference that stream. 116 */ 117 status_t deleteReprocessStream(int id); 118 119 /** 120 * Create a metadata buffer with fields that the HAL device believes are 121 * best for the given use case 122 */ 123 status_t createDefaultRequest(int templateId, CameraMetadata *request); 124 125 /** 126 * Wait until all requests have been processed. Returns INVALID_OPERATION if 127 * the streaming slot is not empty, or TIMED_OUT if the requests haven't 128 * finished processing in 10 seconds. 129 */ 130 status_t waitUntilDrained(); 131 132 /** 133 * Abstract class for HAL notification listeners 134 */ 135 class NotificationListener { 136 public: 137 // Refer to the Camera2 HAL definition for notification definitions 138 virtual void notifyError(int errorCode, int arg1, int arg2) = 0; 139 virtual void notifyShutter(int frameNumber, nsecs_t timestamp) = 0; 140 virtual void notifyAutoFocus(uint8_t newState, int triggerId) = 0; 141 virtual void notifyAutoExposure(uint8_t newState, int triggerId) = 0; 142 virtual void notifyAutoWhitebalance(uint8_t newState, int triggerId) = 0; 143 protected: 144 virtual ~NotificationListener(); 145 }; 146 147 /** 148 * Connect HAL notifications to a listener. Overwrites previous 149 * listener. Set to NULL to stop receiving notifications. 150 */ 151 status_t setNotifyCallback(NotificationListener *listener); 152 153 /** 154 * Wait for a new frame to be produced, with timeout in nanoseconds. 155 * Returns TIMED_OUT when no frame produced within the specified duration 156 */ 157 status_t waitForNextFrame(nsecs_t timeout); 158 159 /** 160 * Get next metadata frame from the frame queue. Returns NULL if the queue 161 * is empty; caller takes ownership of the metadata buffer. 162 */ 163 status_t getNextFrame(CameraMetadata *frame); 164 165 /** 166 * Trigger auto-focus. The latest ID used in a trigger autofocus or cancel 167 * autofocus call will be returned by the HAL in all subsequent AF 168 * notifications. 169 */ 170 status_t triggerAutofocus(uint32_t id); 171 172 /** 173 * Cancel auto-focus. The latest ID used in a trigger autofocus/cancel 174 * autofocus call will be returned by the HAL in all subsequent AF 175 * notifications. 176 */ 177 status_t triggerCancelAutofocus(uint32_t id); 178 179 /** 180 * Trigger pre-capture metering. The latest ID used in a trigger pre-capture 181 * call will be returned by the HAL in all subsequent AE and AWB 182 * notifications. 183 */ 184 status_t triggerPrecaptureMetering(uint32_t id); 185 186 /** 187 * Abstract interface for clients that want to listen to reprocess buffer 188 * release events 189 */ 190 struct BufferReleasedListener: public virtual RefBase { 191 virtual void onBufferReleased(buffer_handle_t *handle) = 0; 192 }; 193 194 /** 195 * Push a buffer to be reprocessed into a reprocessing stream, and 196 * provide a listener to call once the buffer is returned by the HAL 197 */ 198 status_t pushReprocessBuffer(int reprocessStreamId, 199 buffer_handle_t *buffer, wp<BufferReleasedListener> listener); 200 201 private: 202 const int mId; 203 camera2_device_t *mDevice; 204 205 CameraMetadata mDeviceInfo; 206 vendor_tag_query_ops_t *mVendorTagOps; 207 208 /** 209 * Queue class for both sending requests to a camera2 device, and for 210 * receiving frames from a camera2 device. 211 */ 212 class MetadataQueue: public camera2_request_queue_src_ops_t, 213 public camera2_frame_queue_dst_ops_t { 214 public: 215 MetadataQueue(); 216 ~MetadataQueue(); 217 218 // Interface to camera2 HAL device, either for requests (device is 219 // consumer) or for frames (device is producer) 220 const camera2_request_queue_src_ops_t* getToConsumerInterface(); 221 void setFromConsumerInterface(camera2_device_t *d); 222 223 // Connect queue consumer endpoint to a camera2 device 224 status_t setConsumerDevice(camera2_device_t *d); 225 // Connect queue producer endpoint to a camera2 device 226 status_t setProducerDevice(camera2_device_t *d); 227 228 const camera2_frame_queue_dst_ops_t* getToProducerInterface(); 229 230 // Real interfaces. On enqueue, queue takes ownership of buffer pointer 231 // On dequeue, user takes ownership of buffer pointer. 232 status_t enqueue(camera_metadata_t *buf); 233 status_t dequeue(camera_metadata_t **buf, bool incrementCount = false); 234 int getBufferCount(); 235 status_t waitForBuffer(nsecs_t timeout); 236 // Wait until a buffer with the given ID is dequeued. Will return 237 // immediately if the latest buffer dequeued has that ID. 238 status_t waitForDequeue(int32_t id, nsecs_t timeout); 239 240 // Set repeating buffer(s); if the queue is empty on a dequeue call, the 241 // queue copies the contents of the stream slot into the queue, and then 242 // dequeues the first new entry. The metadata buffers passed in are 243 // copied. 244 status_t setStreamSlot(camera_metadata_t *buf); 245 status_t setStreamSlot(const List<camera_metadata_t*> &bufs); 246 247 status_t dump(int fd, const Vector<String16>& args); 248 249 private: 250 status_t signalConsumerLocked(); 251 status_t freeBuffers(List<camera_metadata_t*>::iterator start, 252 List<camera_metadata_t*>::iterator end); 253 254 camera2_device_t *mDevice; 255 256 Mutex mMutex; 257 Condition notEmpty; 258 259 int mFrameCount; 260 int32_t mLatestRequestId; 261 Condition mNewRequestId; 262 263 int mCount; 264 List<camera_metadata_t*> mEntries; 265 int mStreamSlotCount; 266 List<camera_metadata_t*> mStreamSlot; 267 268 bool mSignalConsumer; 269 270 static MetadataQueue* getInstance( 271 const camera2_frame_queue_dst_ops_t *q); 272 static MetadataQueue* getInstance( 273 const camera2_request_queue_src_ops_t *q); 274 275 static int consumer_buffer_count( 276 const camera2_request_queue_src_ops_t *q); 277 278 static int consumer_dequeue(const camera2_request_queue_src_ops_t *q, 279 camera_metadata_t **buffer); 280 281 static int consumer_free(const camera2_request_queue_src_ops_t *q, 282 camera_metadata_t *old_buffer); 283 284 static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q, 285 size_t entries, size_t bytes, 286 camera_metadata_t **buffer); 287 288 static int producer_cancel(const camera2_frame_queue_dst_ops_t *q, 289 camera_metadata_t *old_buffer); 290 291 static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q, 292 camera_metadata_t *filled_buffer); 293 294 }; // class MetadataQueue 295 296 MetadataQueue mRequestQueue; 297 MetadataQueue mFrameQueue; 298 299 /** 300 * Adapter from an ANativeWindow interface to camera2 device stream ops. 301 * Also takes care of allocating/deallocating stream in device interface 302 */ 303 class StreamAdapter: public camera2_stream_ops, public virtual RefBase { 304 public: 305 StreamAdapter(camera2_device_t *d); 306 307 ~StreamAdapter(); 308 309 /** 310 * Create a HAL device stream of the requested size and format. 311 * 312 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device 313 * selects an appropriate format; it can be queried with getFormat. 314 * 315 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must 316 * be equal to the size in bytes of the buffers to allocate for the 317 * stream. For other formats, the size parameter is ignored. 318 */ 319 status_t connectToDevice(sp<ANativeWindow> consumer, 320 uint32_t width, uint32_t height, int format, size_t size); 321 322 status_t release(); 323 324 status_t setTransform(int transform); 325 326 // Get stream parameters. 327 // Only valid after a successful connectToDevice call. 328 int getId() const { return mId; } 329 uint32_t getWidth() const { return mWidth; } 330 uint32_t getHeight() const { return mHeight; } 331 uint32_t getFormat() const { return mFormat; } 332 333 // Dump stream information 334 status_t dump(int fd, const Vector<String16>& args); 335 336 private: 337 enum { 338 ERROR = -1, 339 RELEASED = 0, 340 ALLOCATED, 341 CONNECTED, 342 ACTIVE 343 } mState; 344 345 sp<ANativeWindow> mConsumerInterface; 346 camera2_device_t *mDevice; 347 348 uint32_t mId; 349 uint32_t mWidth; 350 uint32_t mHeight; 351 uint32_t mFormat; 352 size_t mSize; 353 uint32_t mUsage; 354 uint32_t mMaxProducerBuffers; 355 uint32_t mMaxConsumerBuffers; 356 uint32_t mTotalBuffers; 357 int mFormatRequested; 358 359 /** Debugging information */ 360 uint32_t mActiveBuffers; 361 uint32_t mFrameCount; 362 int64_t mLastTimestamp; 363 364 const camera2_stream_ops *getStreamOps(); 365 366 static ANativeWindow* toANW(const camera2_stream_ops_t *w); 367 368 static int dequeue_buffer(const camera2_stream_ops_t *w, 369 buffer_handle_t** buffer); 370 371 static int enqueue_buffer(const camera2_stream_ops_t* w, 372 int64_t timestamp, 373 buffer_handle_t* buffer); 374 375 static int cancel_buffer(const camera2_stream_ops_t* w, 376 buffer_handle_t* buffer); 377 378 static int set_crop(const camera2_stream_ops_t* w, 379 int left, int top, int right, int bottom); 380 }; // class StreamAdapter 381 382 typedef List<sp<StreamAdapter> > StreamList; 383 StreamList mStreams; 384 385 /** 386 * Adapter from an ANativeWindow interface to camera2 device stream ops. 387 * Also takes care of allocating/deallocating stream in device interface 388 */ 389 class ReprocessStreamAdapter: public camera2_stream_in_ops, public virtual RefBase { 390 public: 391 ReprocessStreamAdapter(camera2_device_t *d); 392 393 ~ReprocessStreamAdapter(); 394 395 /** 396 * Create a HAL device reprocess stream based on an existing output stream. 397 */ 398 status_t connectToDevice(const sp<StreamAdapter> &outputStream); 399 400 status_t release(); 401 402 /** 403 * Push buffer into stream for reprocessing. Takes ownership until it notifies 404 * that the buffer has been released 405 */ 406 status_t pushIntoStream(buffer_handle_t *handle, 407 const wp<BufferReleasedListener> &releaseListener); 408 409 /** 410 * Get stream parameters. 411 * Only valid after a successful connectToDevice call. 412 */ 413 int getId() const { return mId; } 414 uint32_t getWidth() const { return mWidth; } 415 uint32_t getHeight() const { return mHeight; } 416 uint32_t getFormat() const { return mFormat; } 417 418 // Dump stream information 419 status_t dump(int fd, const Vector<String16>& args); 420 421 private: 422 enum { 423 ERROR = -1, 424 RELEASED = 0, 425 ACTIVE 426 } mState; 427 428 sp<ANativeWindow> mConsumerInterface; 429 wp<StreamAdapter> mBaseStream; 430 431 struct QueueEntry { 432 buffer_handle_t *handle; 433 wp<BufferReleasedListener> releaseListener; 434 }; 435 436 List<QueueEntry> mQueue; 437 438 List<QueueEntry> mInFlightQueue; 439 440 camera2_device_t *mDevice; 441 442 uint32_t mId; 443 uint32_t mWidth; 444 uint32_t mHeight; 445 uint32_t mFormat; 446 447 /** Debugging information */ 448 uint32_t mActiveBuffers; 449 uint32_t mFrameCount; 450 int64_t mLastTimestamp; 451 452 const camera2_stream_in_ops *getStreamOps(); 453 454 static int acquire_buffer(const camera2_stream_in_ops_t *w, 455 buffer_handle_t** buffer); 456 457 static int release_buffer(const camera2_stream_in_ops_t* w, 458 buffer_handle_t* buffer); 459 460 }; // class ReprocessStreamAdapter 461 462 typedef List<sp<ReprocessStreamAdapter> > ReprocessStreamList; 463 ReprocessStreamList mReprocessStreams; 464 465 // Receives HAL notifications and routes them to the NotificationListener 466 static void notificationCallback(int32_t msg_type, 467 int32_t ext1, 468 int32_t ext2, 469 int32_t ext3, 470 void *user); 471 472}; // class Camera2Device 473 474}; // namespace android 475 476#endif 477