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