1 /* 2 * Copyright (C) 2017 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_DEVICE_V1_0_CAMERADEVICE_H 18 #define ANDROID_HARDWARE_CAMERA_DEVICE_V1_0_CAMERADEVICE_H 19 20 #include <unordered_map> 21 #include "utils/Mutex.h" 22 #include "utils/SortedVector.h" 23 #include "CameraModule.h" 24 #include "HandleImporter.h" 25 26 #include <android/hardware/camera/device/1.0/ICameraDevice.h> 27 #include <android/hidl/allocator/1.0/IAllocator.h> 28 #include <android/hidl/memory/1.0/IMemory.h> 29 #include <hidl/MQDescriptor.h> 30 #include <hidl/Status.h> 31 32 namespace android { 33 namespace hardware { 34 namespace camera { 35 namespace device { 36 namespace V1_0 { 37 namespace implementation { 38 39 using ::android::hardware::camera::common::V1_0::CameraResourceCost; 40 using ::android::hardware::camera::common::V1_0::Status; 41 using ::android::hardware::camera::common::V1_0::TorchMode; 42 using ::android::hardware::camera::common::V1_0::helper::CameraModule; 43 using ::android::hardware::camera::common::V1_0::helper::HandleImporter; 44 using ::android::hardware::camera::device::V1_0::CameraInfo; 45 using ::android::hardware::camera::device::V1_0::CommandType; 46 using ::android::hardware::camera::device::V1_0::ICameraDevice; 47 using ::android::hardware::camera::device::V1_0::ICameraDeviceCallback; 48 using ::android::hardware::camera::device::V1_0::ICameraDevicePreviewCallback; 49 using ::android::hardware::camera::device::V1_0::MemoryId; 50 using ::android::hidl::allocator::V1_0::IAllocator; 51 using ::android::hidl::base::V1_0::IBase; 52 using ::android::hidl::memory::V1_0::IMemory; 53 using ::android::hardware::hidl_array; 54 using ::android::hardware::hidl_memory; 55 using ::android::hardware::hidl_string; 56 using ::android::hardware::hidl_vec; 57 using ::android::hardware::Return; 58 using ::android::hardware::Void; 59 using ::android::sp; 60 61 struct CameraDevice : public ICameraDevice { 62 63 // Called by provider HAL. Provider HAL must ensure the uniqueness of 64 // CameraDevice object per cameraId, or there could be multiple CameraDevice 65 // trying to access the same physical camera. 66 // Also, provider will have to keep track of all CameraDevice objects in 67 // order to notify CameraDevice when the underlying camera is detached 68 CameraDevice(sp<CameraModule> module, 69 const std::string& cameraId, 70 const SortedVector<std::pair<std::string, std::string>>& cameraDeviceNames); 71 ~CameraDevice(); 72 73 // Caller must use this method to check if CameraDevice ctor failed isInitFailedCameraDevice74 bool isInitFailed() { return mInitFail; } 75 // Used by provider HAL to signal external camera disconnected 76 void setConnectionStatus(bool connected); 77 78 // Methods from ::android::hardware::camera::device::V1_0::ICameraDevice follow. 79 Return<void> getResourceCost(getResourceCost_cb _hidl_cb) override; 80 Return<void> getCameraInfo(getCameraInfo_cb _hidl_cb) override; 81 Return<Status> setTorchMode(TorchMode mode) override; 82 Return<Status> dumpState(const hidl_handle& fd) override; 83 Return<Status> open(const sp<ICameraDeviceCallback>& callback) override; 84 Return<Status> setPreviewWindow(const sp<ICameraDevicePreviewCallback>& window) override; 85 Return<void> enableMsgType(uint32_t msgType) override; 86 Return<void> disableMsgType(uint32_t msgType) override; 87 Return<bool> msgTypeEnabled(uint32_t msgType) override; 88 Return<Status> startPreview() override; 89 Return<void> stopPreview() override; 90 Return<bool> previewEnabled() override; 91 Return<Status> storeMetaDataInBuffers(bool enable) override; 92 Return<Status> startRecording() override; 93 Return<void> stopRecording() override; 94 Return<bool> recordingEnabled() override; 95 Return<void> releaseRecordingFrame(uint32_t memId, uint32_t bufferIndex) override; 96 Return<void> releaseRecordingFrameHandle( 97 uint32_t memId, uint32_t bufferIndex, const hidl_handle& frame) override; 98 Return<void> releaseRecordingFrameHandleBatch( 99 const hidl_vec<VideoFrameMessage>&) override; 100 Return<Status> autoFocus() override; 101 Return<Status> cancelAutoFocus() override; 102 Return<Status> takePicture() override; 103 Return<Status> cancelPicture() override; 104 Return<Status> setParameters(const hidl_string& params) override; 105 Return<void> getParameters(getParameters_cb _hidl_cb) override; 106 Return<Status> sendCommand(CommandType cmd, int32_t arg1, int32_t arg2) override; 107 Return<void> close() override; 108 109 private: 110 struct CameraMemory : public camera_memory_t { 111 MemoryId mId; 112 CameraDevice* mDevice; 113 }; 114 115 class CameraHeapMemory : public RefBase { 116 public: 117 CameraHeapMemory(int fd, size_t buf_size, uint_t num_buffers = 1); 118 explicit CameraHeapMemory( 119 sp<IAllocator> ashmemAllocator, size_t buf_size, uint_t num_buffers = 1); 120 void commonInitialization(); 121 virtual ~CameraHeapMemory(); 122 123 size_t mBufSize; 124 uint_t mNumBufs; 125 126 // Shared memory related members 127 hidl_memory mHidlHeap; 128 native_handle_t* mHidlHandle; // contains one shared memory FD 129 void* mHidlHeapMemData; 130 sp<IMemory> mHidlHeapMemory; // munmap happens in ~IMemory() 131 132 CameraMemory handle; 133 }; 134 sp<IAllocator> mAshmemAllocator; 135 136 const sp<CameraModule> mModule; 137 const std::string mCameraId; 138 // const after ctor 139 int mCameraIdInt; 140 int mDeviceVersion; 141 142 camera_device_t* mDevice = nullptr; 143 144 void initHalPreviewWindow(); 145 struct CameraPreviewWindow : public preview_stream_ops { 146 // Called when we expect buffer will be re-allocated 147 void cleanUpCirculatingBuffers(); 148 149 Mutex mLock; 150 sp<ICameraDevicePreviewCallback> mPreviewCallback = nullptr; 151 std::unordered_map<uint64_t, buffer_handle_t> mCirculatingBuffers; 152 std::unordered_map<buffer_handle_t*, uint64_t> mBufferIdMap; 153 } mHalPreviewWindow; 154 155 // gating access to mDevice, mInitFail, mDisconnected 156 mutable Mutex mLock; 157 158 bool mInitFail = false; 159 // Set by provider (when external camera is connected/disconnected) 160 bool mDisconnected; 161 162 static HandleImporter sHandleImporter; 163 164 const SortedVector<std::pair<std::string, std::string>>& mCameraDeviceNames; 165 166 sp<ICameraDeviceCallback> mDeviceCallback = nullptr; 167 168 mutable Mutex mMemoryMapLock; // gating access to mMemoryMap 169 // must not hold mLock after this lock is acquired 170 std::unordered_map<MemoryId, CameraHeapMemory*> mMemoryMap; 171 172 bool mMetadataMode = false; 173 174 mutable Mutex mBatchLock; 175 // Start of protection scope for mBatchLock 176 uint32_t mBatchSize = 0; // 0 for non-batch mode, set to other value to start batching 177 int32_t mBatchMsgType; // Maybe only allow DataCallbackMsg::VIDEO_FRAME? 178 std::vector<HandleTimestampMessage> mInflightBatch; 179 // End of protection scope for mBatchLock 180 181 void handleCallbackTimestamp( 182 nsecs_t timestamp, int32_t msg_type, 183 MemoryId memId , unsigned index, native_handle_t* handle); 184 void releaseRecordingFrameLocked(uint32_t memId, uint32_t bufferIndex, const native_handle_t*); 185 186 // shared memory methods 187 static camera_memory_t* sGetMemory(int fd, size_t buf_size, uint_t num_bufs, void *user); 188 static void sPutMemory(camera_memory_t *data); 189 190 // Device callback forwarding methods 191 static void sNotifyCb(int32_t msg_type, int32_t ext1, int32_t ext2, void *user); 192 static void sDataCb(int32_t msg_type, const camera_memory_t *data, unsigned int index, 193 camera_frame_metadata_t *metadata, void *user); 194 static void sDataCbTimestamp(nsecs_t timestamp, int32_t msg_type, 195 const camera_memory_t *data, unsigned index, void *user); 196 197 // Preview window callback forwarding methods 198 static int sDequeueBuffer(struct preview_stream_ops* w, 199 buffer_handle_t** buffer, int *stride); 200 201 static int sLockBuffer(struct preview_stream_ops* w, buffer_handle_t* buffer); 202 203 static int sEnqueueBuffer(struct preview_stream_ops* w, buffer_handle_t* buffer); 204 205 static int sCancelBuffer(struct preview_stream_ops* w, buffer_handle_t* buffer); 206 207 static int sSetBufferCount(struct preview_stream_ops* w, int count); 208 209 static int sSetBuffersGeometry(struct preview_stream_ops* w, 210 int width, int height, int format); 211 212 static int sSetCrop(struct preview_stream_ops *w, int left, int top, int right, int bottom); 213 214 static int sSetTimestamp(struct preview_stream_ops *w, int64_t timestamp); 215 216 static int sSetUsage(struct preview_stream_ops* w, int usage); 217 218 static int sSetSwapInterval(struct preview_stream_ops *w, int interval); 219 220 static int sGetMinUndequeuedBufferCount(const struct preview_stream_ops *w, int *count); 221 222 // convert conventional HAL status to HIDL Status 223 static Status getHidlStatus(const int&); 224 static status_t getStatusT(const Status& s); 225 226 Status initStatus() const; 227 void closeLocked(); 228 }; 229 230 } // namespace implementation 231 } // namespace V1_0 232 } // namespace device 233 } // namespace camera 234 } // namespace hardware 235 } // namespace android 236 237 #endif // ANDROID_HARDWARE_CAMERA_DEVICE_V1_0_CAMERADEVICE_H 238