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