CameraHardwareInterface.h revision b5df547bce900fa5764d735bee304e79c001d60d
1/* 2 * Copyright (C) 2008 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_HARDWARE_CAMERA_HARDWARE_INTERFACE_H 18#define ANDROID_HARDWARE_CAMERA_HARDWARE_INTERFACE_H 19 20#include <unordered_map> 21#include <binder/IMemory.h> 22#include <binder/MemoryBase.h> 23#include <binder/MemoryHeapBase.h> 24#include <utils/RefBase.h> 25#include <ui/GraphicBuffer.h> 26#include <camera/Camera.h> 27#include <camera/CameraParameters.h> 28#include <system/window.h> 29#include <hardware/camera.h> 30 31#include <common/CameraModule.h> 32#include <common/CameraProviderManager.h> 33 34namespace android { 35 36typedef void (*notify_callback)(int32_t msgType, 37 int32_t ext1, 38 int32_t ext2, 39 void* user); 40 41typedef void (*data_callback)(int32_t msgType, 42 const sp<IMemory> &dataPtr, 43 camera_frame_metadata_t *metadata, 44 void* user); 45 46typedef void (*data_callback_timestamp)(nsecs_t timestamp, 47 int32_t msgType, 48 const sp<IMemory> &dataPtr, 49 void *user); 50 51struct HandleTimestampMessage { 52 nsecs_t timestamp; 53 const sp<IMemory> dataPtr; 54}; 55 56typedef void (*data_callback_timestamp_batch)( 57 int32_t msgType, 58 const std::vector<HandleTimestampMessage>&, void* user); 59 60/** 61 * CameraHardwareInterface.h defines the interface to the 62 * camera hardware abstraction layer, used for setting and getting 63 * parameters, live previewing, and taking pictures. It is used for 64 * HAL devices with version CAMERA_DEVICE_API_VERSION_1_0 only. 65 * 66 * It is a referenced counted interface with RefBase as its base class. 67 * CameraService calls openCameraHardware() to retrieve a strong pointer to the 68 * instance of this interface and may be called multiple times. The 69 * following steps describe a typical sequence: 70 * 71 * -# After CameraService calls openCameraHardware(), getParameters() and 72 * setParameters() are used to initialize the camera instance. 73 * -# startPreview() is called. 74 * 75 * Prior to taking a picture, CameraService often calls autofocus(). When auto 76 * focusing has completed, the camera instance sends a CAMERA_MSG_FOCUS notification, 77 * which informs the application whether focusing was successful. The camera instance 78 * only sends this message once and it is up to the application to call autoFocus() 79 * again if refocusing is desired. 80 * 81 * CameraService calls takePicture() to request the camera instance take a 82 * picture. At this point, if a shutter, postview, raw, and/or compressed 83 * callback is desired, the corresponding message must be enabled. Any memory 84 * provided in a data callback must be copied if it's needed after returning. 85 */ 86 87class CameraHardwareInterface : 88 public virtual RefBase, 89 public virtual hardware::camera::device::V1_0::ICameraDeviceCallback, 90 public virtual hardware::camera::device::V1_0::ICameraDevicePreviewCallback { 91 92public: 93 explicit CameraHardwareInterface(const char *name): 94 mDevice(nullptr), 95 mHidlDevice(nullptr), 96 mName(name), 97 mPreviewScalingMode(NOT_SET), 98 mPreviewTransform(NOT_SET), 99 mPreviewWidth(NOT_SET), 100 mPreviewHeight(NOT_SET), 101 mPreviewFormat(NOT_SET), 102 mPreviewUsage(0), 103 mPreviewSwapInterval(NOT_SET), 104 mPreviewCrop{NOT_SET,NOT_SET,NOT_SET,NOT_SET} 105 { 106 } 107 108 ~CameraHardwareInterface(); 109 110 status_t initialize(CameraModule *module); 111 status_t initialize(sp<CameraProviderManager> manager); 112 113 /** Set the ANativeWindow to which preview frames are sent */ 114 status_t setPreviewWindow(const sp<ANativeWindow>& buf); 115 116 status_t setPreviewScalingMode(int scalingMode); 117 118 status_t setPreviewTransform(int transform); 119 120 /** Set the notification and data callbacks */ 121 void setCallbacks(notify_callback notify_cb, 122 data_callback data_cb, 123 data_callback_timestamp data_cb_timestamp, 124 data_callback_timestamp_batch data_cb_timestamp_batch, 125 void* user); 126 127 /** 128 * The following three functions all take a msgtype, 129 * which is a bitmask of the messages defined in 130 * include/ui/Camera.h 131 */ 132 133 /** 134 * Enable a message, or set of messages. 135 */ 136 void enableMsgType(int32_t msgType); 137 138 /** 139 * Disable a message, or a set of messages. 140 * 141 * Once received a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), camera hal 142 * should not rely on its client to call releaseRecordingFrame() to release 143 * video recording frames sent out by the cameral hal before and after the 144 * disableMsgType(CAMERA_MSG_VIDEO_FRAME) call. Camera hal clients must not 145 * modify/access any video recording frame after calling 146 * disableMsgType(CAMERA_MSG_VIDEO_FRAME). 147 */ 148 void disableMsgType(int32_t msgType); 149 150 /** 151 * Query whether a message, or a set of messages, is enabled. 152 * Note that this is operates as an AND, if any of the messages 153 * queried are off, this will return false. 154 */ 155 int msgTypeEnabled(int32_t msgType); 156 157 /** 158 * Start preview mode. 159 */ 160 status_t startPreview(); 161 162 /** 163 * Stop a previously started preview. 164 */ 165 void stopPreview(); 166 167 /** 168 * Returns true if preview is enabled. 169 */ 170 int previewEnabled(); 171 172 /** 173 * Request the camera hal to store meta data or real YUV data in 174 * the video buffers send out via CAMERA_MSG_VIDEO_FRRAME for a 175 * recording session. If it is not called, the default camera 176 * hal behavior is to store real YUV data in the video buffers. 177 * 178 * This method should be called before startRecording() in order 179 * to be effective. 180 * 181 * If meta data is stored in the video buffers, it is up to the 182 * receiver of the video buffers to interpret the contents and 183 * to find the actual frame data with the help of the meta data 184 * in the buffer. How this is done is outside of the scope of 185 * this method. 186 * 187 * Some camera hal may not support storing meta data in the video 188 * buffers, but all camera hal should support storing real YUV data 189 * in the video buffers. If the camera hal does not support storing 190 * the meta data in the video buffers when it is requested to do 191 * do, INVALID_OPERATION must be returned. It is very useful for 192 * the camera hal to pass meta data rather than the actual frame 193 * data directly to the video encoder, since the amount of the 194 * uncompressed frame data can be very large if video size is large. 195 * 196 * @param enable if true to instruct the camera hal to store 197 * meta data in the video buffers; false to instruct 198 * the camera hal to store real YUV data in the video 199 * buffers. 200 * 201 * @return OK on success. 202 */ 203 204 status_t storeMetaDataInBuffers(int enable); 205 206 /** 207 * Start record mode. When a record image is available a CAMERA_MSG_VIDEO_FRAME 208 * message is sent with the corresponding frame. Every record frame must be released 209 * by a cameral hal client via releaseRecordingFrame() before the client calls 210 * disableMsgType(CAMERA_MSG_VIDEO_FRAME). After the client calls 211 * disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's responsibility 212 * to manage the life-cycle of the video recording frames, and the client must 213 * not modify/access any video recording frames. 214 */ 215 status_t startRecording(); 216 217 /** 218 * Stop a previously started recording. 219 */ 220 void stopRecording(); 221 222 /** 223 * Returns true if recording is enabled. 224 */ 225 int recordingEnabled(); 226 227 /** 228 * Release a record frame previously returned by CAMERA_MSG_VIDEO_FRAME. 229 * 230 * It is camera hal client's responsibility to release video recording 231 * frames sent out by the camera hal before the camera hal receives 232 * a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME). After it receives 233 * the call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's 234 * responsibility of managing the life-cycle of the video recording 235 * frames. 236 */ 237 void releaseRecordingFrame(const sp<IMemory>& mem); 238 239 /** 240 * Release a batch of recording frames previously returned by 241 * CAMERA_MSG_VIDEO_FRAME. This method only supports frames that are 242 * stored as VideoNativeHandleMetadata. 243 * 244 * It is camera hal client's responsibility to release video recording 245 * frames sent out by the camera hal before the camera hal receives 246 * a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME). After it receives 247 * the call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's 248 * responsibility of managing the life-cycle of the video recording 249 * frames. 250 */ 251 void releaseRecordingFrameBatch(const std::vector<sp<IMemory>>& frames); 252 253 /** 254 * Start auto focus, the notification callback routine is called 255 * with CAMERA_MSG_FOCUS once when focusing is complete. autoFocus() 256 * will be called again if another auto focus is needed. 257 */ 258 status_t autoFocus(); 259 260 /** 261 * Cancels auto-focus function. If the auto-focus is still in progress, 262 * this function will cancel it. Whether the auto-focus is in progress 263 * or not, this function will return the focus position to the default. 264 * If the camera does not support auto-focus, this is a no-op. 265 */ 266 status_t cancelAutoFocus(); 267 268 /** 269 * Take a picture. 270 */ 271 status_t takePicture(); 272 273 /** 274 * Cancel a picture that was started with takePicture. Calling this 275 * method when no picture is being taken is a no-op. 276 */ 277 status_t cancelPicture(); 278 279 /** 280 * Set the camera parameters. This returns BAD_VALUE if any parameter is 281 * invalid or not supported. */ 282 status_t setParameters(const CameraParameters ¶ms); 283 284 /** Return the camera parameters. */ 285 CameraParameters getParameters() const; 286 287 /** 288 * Send command to camera driver. 289 */ 290 status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2); 291 292 /** 293 * Release the hardware resources owned by this object. Note that this is 294 * *not* done in the destructor. 295 */ 296 void release(); 297 298 /** 299 * Dump state of the camera hardware 300 */ 301 status_t dump(int fd, const Vector<String16>& /*args*/) const; 302 303private: 304 camera_device_t *mDevice; 305 sp<hardware::camera::device::V1_0::ICameraDevice> mHidlDevice; 306 String8 mName; 307 308 static void sNotifyCb(int32_t msg_type, int32_t ext1, 309 int32_t ext2, void *user); 310 311 static void sDataCb(int32_t msg_type, 312 const camera_memory_t *data, unsigned int index, 313 camera_frame_metadata_t *metadata, 314 void *user); 315 316 static void sDataCbTimestamp(nsecs_t timestamp, int32_t msg_type, 317 const camera_memory_t *data, unsigned index, 318 void *user); 319 320 // TODO: b/35625849 321 // Meta data buffer layout for passing a native_handle to codec 322 // matching frameworks/native/include/media/hardware/MetadataBufferType.h and 323 // frameworks/native/include/media/hardware/HardwareAPI.h 324 struct VideoNativeHandleMetadata { 325 static const uint32_t kMetadataBufferTypeNativeHandleSource = 3; 326 uint32_t eType; // must be kMetadataBufferTypeNativeHandleSource 327 native_handle_t* pHandle; 328 }; 329 330 // This is a utility class that combines a MemoryHeapBase and a MemoryBase 331 // in one. Since we tend to use them in a one-to-one relationship, this is 332 // handy. 333 class CameraHeapMemory : public RefBase { 334 public: 335 CameraHeapMemory(int fd, size_t buf_size, uint_t num_buffers = 1) : 336 mBufSize(buf_size), 337 mNumBufs(num_buffers) 338 { 339 mHeap = new MemoryHeapBase(fd, buf_size * num_buffers); 340 commonInitialization(); 341 } 342 343 explicit CameraHeapMemory(size_t buf_size, uint_t num_buffers = 1) : 344 mBufSize(buf_size), 345 mNumBufs(num_buffers) 346 { 347 mHeap = new MemoryHeapBase(buf_size * num_buffers); 348 commonInitialization(); 349 } 350 351 void commonInitialization() 352 { 353 handle.data = mHeap->base(); 354 handle.size = mBufSize * mNumBufs; 355 handle.handle = this; 356 357 mBuffers = new sp<MemoryBase>[mNumBufs]; 358 for (uint_t i = 0; i < mNumBufs; i++) 359 mBuffers[i] = new MemoryBase(mHeap, 360 i * mBufSize, 361 mBufSize); 362 363 handle.release = sPutMemory; 364 } 365 366 virtual ~CameraHeapMemory() 367 { 368 delete [] mBuffers; 369 } 370 371 size_t mBufSize; 372 uint_t mNumBufs; 373 sp<MemoryHeapBase> mHeap; 374 sp<MemoryBase> *mBuffers; 375 376 camera_memory_t handle; 377 }; 378 379 static camera_memory_t* sGetMemory(int fd, size_t buf_size, uint_t num_bufs, 380 void *user __attribute__((unused))); 381 382 static void sPutMemory(camera_memory_t *data); 383 384 static ANativeWindow *sToAnw(void *user); 385 386 static int sDequeueBuffer(struct preview_stream_ops* w, 387 buffer_handle_t** buffer, int *stride); 388 389 static int sLockBuffer(struct preview_stream_ops* w, 390 buffer_handle_t* /*buffer*/); 391 392 static int sEnqueueBuffer(struct preview_stream_ops* w, 393 buffer_handle_t* buffer); 394 395 static int sCancelBuffer(struct preview_stream_ops* w, 396 buffer_handle_t* buffer); 397 398 static int sSetBufferCount(struct preview_stream_ops* w, int count); 399 400 static int sSetBuffersGeometry(struct preview_stream_ops* w, 401 int width, int height, int format); 402 403 static int sSetCrop(struct preview_stream_ops *w, 404 int left, int top, int right, int bottom); 405 406 static int sSetTimestamp(struct preview_stream_ops *w, 407 int64_t timestamp); 408 409 static int sSetUsage(struct preview_stream_ops* w, int usage); 410 411 static int sSetSwapInterval(struct preview_stream_ops *w, int interval); 412 413 static int sGetMinUndequeuedBufferCount( 414 const struct preview_stream_ops *w, 415 int *count); 416 417 void initHalPreviewWindow(); 418 419 std::pair<bool, uint64_t> getBufferId(ANativeWindowBuffer* anb); 420 void cleanupCirculatingBuffers(); 421 422 /** 423 * Implementation of android::hardware::camera::device::V1_0::ICameraDeviceCallback 424 */ 425 hardware::Return<void> notifyCallback( 426 hardware::camera::device::V1_0::NotifyCallbackMsg msgType, 427 int32_t ext1, int32_t ext2) override; 428 hardware::Return<uint32_t> registerMemory( 429 const hardware::hidl_handle& descriptor, 430 uint32_t bufferSize, uint32_t bufferCount) override; 431 hardware::Return<void> unregisterMemory(uint32_t memId) override; 432 hardware::Return<void> dataCallback( 433 hardware::camera::device::V1_0::DataCallbackMsg msgType, 434 uint32_t data, uint32_t bufferIndex, 435 const hardware::camera::device::V1_0::CameraFrameMetadata& metadata) override; 436 hardware::Return<void> dataCallbackTimestamp( 437 hardware::camera::device::V1_0::DataCallbackMsg msgType, 438 uint32_t data, uint32_t bufferIndex, int64_t timestamp) override; 439 hardware::Return<void> handleCallbackTimestamp( 440 hardware::camera::device::V1_0::DataCallbackMsg msgType, 441 const hardware::hidl_handle& frameData, uint32_t data, 442 uint32_t bufferIndex, int64_t timestamp) override; 443 hardware::Return<void> handleCallbackTimestampBatch( 444 hardware::camera::device::V1_0::DataCallbackMsg msgType, 445 const hardware::hidl_vec< 446 hardware::camera::device::V1_0::HandleTimestampMessage>&) override; 447 448 /** 449 * Implementation of android::hardware::camera::device::V1_0::ICameraDevicePreviewCallback 450 */ 451 hardware::Return<void> dequeueBuffer(dequeueBuffer_cb _hidl_cb) override; 452 hardware::Return<hardware::camera::common::V1_0::Status> 453 enqueueBuffer(uint64_t bufferId) override; 454 hardware::Return<hardware::camera::common::V1_0::Status> 455 cancelBuffer(uint64_t bufferId) override; 456 hardware::Return<hardware::camera::common::V1_0::Status> 457 setBufferCount(uint32_t count) override; 458 hardware::Return<hardware::camera::common::V1_0::Status> 459 setBuffersGeometry(uint32_t w, uint32_t h, 460 hardware::graphics::common::V1_0::PixelFormat format) override; 461 hardware::Return<hardware::camera::common::V1_0::Status> 462 setCrop(int32_t left, int32_t top, int32_t right, int32_t bottom) override; 463 hardware::Return<hardware::camera::common::V1_0::Status> 464 setUsage(hardware::graphics::allocator::V2_0::ProducerUsage usage) override; 465 hardware::Return<hardware::camera::common::V1_0::Status> 466 setSwapInterval(int32_t interval) override; 467 hardware::Return<void> getMinUndequeuedBufferCount( 468 getMinUndequeuedBufferCount_cb _hidl_cb) override; 469 hardware::Return<hardware::camera::common::V1_0::Status> 470 setTimestamp(int64_t timestamp) override; 471 472 sp<ANativeWindow> mPreviewWindow; 473 474 struct camera_preview_window { 475 struct preview_stream_ops nw; 476 void *user; 477 }; 478 479 struct camera_preview_window mHalPreviewWindow; 480 481 notify_callback mNotifyCb; 482 data_callback mDataCb; 483 data_callback_timestamp mDataCbTimestamp; 484 data_callback_timestamp_batch mDataCbTimestampBatch; 485 void *mCbUser; 486 487 // Cached values for preview stream parameters 488 static const int NOT_SET = -1; 489 int mPreviewScalingMode; 490 int mPreviewTransform; 491 int mPreviewWidth; 492 int mPreviewHeight; 493 int mPreviewFormat; 494 int mPreviewUsage; 495 int mPreviewSwapInterval; 496 android_native_rect_t mPreviewCrop; 497 498 struct BufferHasher { 499 size_t operator()(const buffer_handle_t& buf) const { 500 if (buf == nullptr) 501 return 0; 502 503 size_t result = 1; 504 result = 31 * result + buf->numFds; 505 result = 31 * result + buf->numInts; 506 int length = buf->numFds + buf->numInts; 507 for (int i = 0; i < length; i++) { 508 result = 31 * result + buf->data[i]; 509 } 510 return result; 511 } 512 }; 513 514 struct BufferComparator { 515 bool operator()(const buffer_handle_t& buf1, const buffer_handle_t& buf2) const { 516 if (buf1->numFds == buf2->numFds && buf1->numInts == buf2->numInts) { 517 int length = buf1->numFds + buf1->numInts; 518 for (int i = 0; i < length; i++) { 519 if (buf1->data[i] != buf2->data[i]) { 520 return false; 521 } 522 } 523 return true; 524 } 525 return false; 526 } 527 }; 528 529 std::mutex mBufferIdMapLock; // protecting mBufferIdMap and mNextBufferId 530 typedef std::unordered_map<const buffer_handle_t, uint64_t, 531 BufferHasher, BufferComparator> BufferIdMap; 532 // stream ID -> per stream buffer ID map 533 BufferIdMap mBufferIdMap; 534 std::unordered_map<uint64_t, ANativeWindowBuffer*> mReversedBufMap; 535 uint64_t mNextBufferId = 1; 536 static const uint64_t BUFFER_ID_NO_BUFFER = 0; 537 538 std::unordered_map<int, camera_memory_t*> mHidlMemPoolMap; 539}; 540 541}; // namespace android 542 543#endif 544