1 /* 2 * Copyright (C) 2022 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_HIDLCAMERA3DEVICE_H 18 #define ANDROID_SERVERS_HIDLCAMERA3DEVICE_H 19 20 #include "../Camera3Device.h" 21 #include "HidlCamera3OutputUtils.h" 22 23 namespace android { 24 25 26 /** 27 * CameraDevice for HIDL HAL devices with version CAMERA_DEVICE_API_VERSION_3_0 or higher. 28 */ 29 class HidlCamera3Device : 30 virtual public hardware::camera::device::V3_5::ICameraDeviceCallback, 31 public Camera3Device { 32 public: 33 34 explicit HidlCamera3Device( 35 std::shared_ptr<CameraServiceProxyWrapper>& cameraServiceProxyWrapper, 36 std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils, 37 const std::string& id, bool overrideForPerfClass, int rotationOverride, 38 bool isVendorClient, bool legacyClient = false) : Camera3Device(cameraServiceProxyWrapper,attributionAndPermissionUtils,id,overrideForPerfClass,rotationOverride,isVendorClient,legacyClient)39 Camera3Device(cameraServiceProxyWrapper, attributionAndPermissionUtils, id, 40 overrideForPerfClass, rotationOverride, isVendorClient, legacyClient) { } 41 ~HidlCamera3Device()42 virtual ~HidlCamera3Device() {} 43 44 /** 45 * Helper functions to map between framework and HIDL values 46 */ 47 static hardware::graphics::common::V1_0::PixelFormat mapToPixelFormat(int frameworkFormat); 48 static hardware::camera::device::V3_2::DataspaceFlags mapToHidlDataspace( 49 android_dataspace dataSpace); 50 static hardware::camera::device::V3_2::BufferUsageFlags mapToConsumerUsage(uint64_t usage); 51 static hardware::camera::device::V3_2::StreamRotation mapToStreamRotation( 52 camera_stream_rotation_t rotation); 53 // Returns a negative error code if the passed-in operation mode is not valid. 54 static status_t mapToStreamConfigurationMode(camera_stream_configuration_mode_t operationMode, 55 /*out*/ hardware::camera::device::V3_2::StreamConfigurationMode *mode); 56 static int mapToFrameworkFormat(hardware::graphics::common::V1_0::PixelFormat pixelFormat); 57 static android_dataspace mapToFrameworkDataspace( 58 hardware::camera::device::V3_2::DataspaceFlags); 59 static uint64_t mapConsumerToFrameworkUsage( 60 hardware::camera::device::V3_2::BufferUsageFlags usage); 61 static uint64_t mapProducerToFrameworkUsage( 62 hardware::camera::device::V3_2::BufferUsageFlags usage); 63 64 status_t initialize(sp<CameraProviderManager> manager, const std::string& monitorTags) override; 65 66 virtual int32_t getCaptureResultFMQSize() override; 67 68 /** 69 * Implementation of android::hardware::camera::device::V3_5::ICameraDeviceCallback 70 */ 71 72 hardware::Return<void> processCaptureResult_3_4( 73 const hardware::hidl_vec< 74 hardware::camera::device::V3_4::CaptureResult>& results) override; 75 hardware::Return<void> processCaptureResult( 76 const hardware::hidl_vec< 77 hardware::camera::device::V3_2::CaptureResult>& results) override; 78 hardware::Return<void> notify( 79 const hardware::hidl_vec< 80 hardware::camera::device::V3_2::NotifyMsg>& msgs) override; 81 82 hardware::Return<void> requestStreamBuffers( 83 const hardware::hidl_vec< 84 hardware::camera::device::V3_5::BufferRequest>& bufReqs, 85 requestStreamBuffers_cb _hidl_cb) override; 86 87 hardware::Return<void> returnStreamBuffers( 88 const hardware::hidl_vec< 89 hardware::camera::device::V3_2::StreamBuffer>& buffers) override; 90 91 status_t switchToOffline(const std::vector<int32_t>& streamsToKeep, 92 /*out*/ sp<CameraOfflineSessionBase>* session) override; 93 94 using RequestMetadataQueue = hardware::MessageQueue<uint8_t, hardware::kSynchronizedReadWrite>; 95 96 class HidlHalInterface : public Camera3Device::HalInterface { 97 public: 98 HidlHalInterface(sp<hardware::camera::device::V3_2::ICameraDeviceSession> &session, 99 std::shared_ptr<RequestMetadataQueue> queue, 100 bool useHalBufManager, bool supportOfflineProcessing); 101 getTransportType()102 virtual IPCTransport getTransportType() const override { return IPCTransport::HIDL; } 103 // Returns true if constructed with a valid device or session, and not yet cleared 104 virtual bool valid() override; 105 106 // Reset this HalInterface object (does not call close()) 107 virtual void clear() override; 108 109 // Calls into the HAL interface 110 111 // Caller takes ownership of requestTemplate 112 virtual status_t constructDefaultRequestSettings(camera_request_template templateId, 113 /*out*/ camera_metadata_t **requestTemplate) override; 114 115 virtual status_t configureStreams(const camera_metadata_t *sessionParams, 116 /*inout*/ camera_stream_configuration_t *config, 117 const std::vector<uint32_t>& bufferSizes, 118 int64_t logId) override; 119 120 // The injection camera configures the streams to hal. 121 virtual status_t configureInjectedStreams( 122 const camera_metadata_t* sessionParams, 123 /*inout*/ camera_stream_configuration_t* config, 124 const std::vector<uint32_t>& bufferSizes, 125 const CameraMetadata& cameraCharacteristics) override; 126 127 // When the call succeeds, the ownership of acquire fences in requests is transferred to 128 // HalInterface. More specifically, the current implementation will send the fence to 129 // HAL process and close the FD in cameraserver process. When the call fails, the ownership 130 // of the acquire fence still belongs to the caller. 131 virtual status_t processBatchCaptureRequests( 132 std::vector<camera_capture_request_t*>& requests, 133 /*out*/uint32_t* numRequestProcessed) override; 134 virtual status_t flush() override; 135 virtual status_t dump(int fd) override; 136 virtual status_t close() override; 137 138 virtual void signalPipelineDrain(const std::vector<int>& streamIds) override; 139 virtual bool isReconfigurationRequired(CameraMetadata& oldSessionParams, 140 CameraMetadata& newSessionParams) override; 141 142 virtual status_t repeatingRequestEnd(uint32_t frameNumber, 143 const std::vector<int32_t> &streamIds) override; 144 145 status_t switchToOffline( 146 const std::vector<int32_t>& streamsToKeep, 147 /*out*/hardware::camera::device::V3_6::CameraOfflineSessionInfo* offlineSessionInfo, 148 /*out*/sp<hardware::camera::device::V3_6::ICameraOfflineSession>* offlineSession, 149 /*out*/camera3::BufferRecords* bufferRecords); 150 151 private: 152 153 // Always valid 154 sp<hardware::camera::device::V3_2::ICameraDeviceSession> mHidlSession; 155 // Valid if ICameraDeviceSession is @3.3 or newer 156 sp<hardware::camera::device::V3_3::ICameraDeviceSession> mHidlSession_3_3; 157 // Valid if ICameraDeviceSession is @3.4 or newer 158 sp<hardware::camera::device::V3_4::ICameraDeviceSession> mHidlSession_3_4; 159 // Valid if ICameraDeviceSession is @3.5 or newer 160 sp<hardware::camera::device::V3_5::ICameraDeviceSession> mHidlSession_3_5; 161 // Valid if ICameraDeviceSession is @3.6 or newer 162 sp<hardware::camera::device::V3_6::ICameraDeviceSession> mHidlSession_3_6; 163 // Valid if ICameraDeviceSession is @3.7 or newer 164 sp<hardware::camera::device::V3_7::ICameraDeviceSession> mHidlSession_3_7; 165 166 std::shared_ptr<RequestMetadataQueue> mRequestMetadataQueue; 167 168 // The output HIDL request still depends on input camera_capture_request_t 169 // Do not free input camera_capture_request_t before output HIDL request 170 status_t wrapAsHidlRequest(camera_capture_request_t* in, 171 /*out*/hardware::camera::device::V3_2::CaptureRequest* out, 172 /*out*/std::vector<native_handle_t*>* handlesCreated, 173 /*out*/std::vector<std::pair<int32_t, int32_t>>* inflightBuffers); 174 }; // class HidlHalInterface 175 176 class HidlRequestThread : public Camera3Device::RequestThread { 177 public: 178 HidlRequestThread(wp<Camera3Device> parent, 179 sp<camera3::StatusTracker> statusTracker, 180 sp<HalInterface> interface, 181 const Vector<int32_t>& sessionParamKeys, 182 bool useHalBufManager, 183 bool supportCameraMute, 184 int rotationOverride, 185 bool supportSettingsOverride); 186 187 status_t switchToOffline( 188 const std::vector<int32_t>& streamsToKeep, 189 /*out*/hardware::camera::device::V3_6::CameraOfflineSessionInfo* offlineSessionInfo, 190 /*out*/sp<hardware::camera::device::V3_6::ICameraOfflineSession>* offlineSession, 191 /*out*/camera3::BufferRecords* bufferRecords); 192 }; // class HidlRequestThread 193 194 class HidlCamera3DeviceInjectionMethods : public Camera3DeviceInjectionMethods { 195 public: 196 // Initialize the injection camera and generate an hal interface. 197 status_t injectionInitialize( 198 const std::string& injectedCamId, sp<CameraProviderManager> manager, 199 const sp< 200 android::hardware::camera::device::V3_2 ::ICameraDeviceCallback>& 201 callback); HidlCamera3DeviceInjectionMethods(wp<Camera3Device> parent)202 HidlCamera3DeviceInjectionMethods(wp<Camera3Device> parent) : 203 Camera3DeviceInjectionMethods(parent) { }; ~HidlCamera3DeviceInjectionMethods()204 ~HidlCamera3DeviceInjectionMethods() {} 205 private: 206 // Backup of the original camera hal result FMQ. 207 std::unique_ptr<ResultMetadataQueue> mBackupResultMetadataQueue; 208 209 // FMQ writes the result for the injection camera. Must be guarded by 210 // mProcessCaptureResultLock. 211 std::unique_ptr<ResultMetadataQueue> mInjectionResultMetadataQueue; 212 213 // Use injection camera hal interface to replace and backup original 214 // camera hal interface. 215 virtual status_t replaceHalInterface(sp<HalInterface> newHalInterface, 216 bool keepBackup) override; 217 }; 218 219 private: 220 template<typename NotifyMsgType> 221 hardware::Return<void> notifyHelper( 222 const hardware::hidl_vec<NotifyMsgType>& msgs); 223 224 virtual void applyMaxBatchSizeLocked( 225 RequestList* requestList, 226 const sp<camera3::Camera3OutputStreamInterface>& stream) override; 227 228 virtual status_t injectionCameraInitialize(const std::string &injectCamId, 229 sp<CameraProviderManager> manager) override; 230 231 virtual sp<RequestThread> createNewRequestThread(wp<Camera3Device> parent, 232 sp<camera3::StatusTracker> statusTracker, 233 sp<HalInterface> interface, 234 const Vector<int32_t>& sessionParamKeys, 235 bool useHalBufManager, 236 bool supportCameraMute, 237 int rotationOverride, 238 bool supportSettingsOverride) override; 239 240 virtual sp<Camera3DeviceInjectionMethods> 241 createCamera3DeviceInjectionMethods(wp<Camera3Device>) override; 242 243 // FMQ to write result on. Must be guarded by mProcessCaptureResultLock. 244 std::unique_ptr<ResultMetadataQueue> mResultMetadataQueue; 245 246 }; // class HidlCamera3Device 247 248 }; // namespace android 249 250 #endif 251