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