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_AIDLCAMERA3DEVICE_H 18 #define ANDROID_SERVERS_AIDLCAMERA3DEVICE_H 19 20 #include "../Camera3Device.h" 21 #include "AidlCamera3OutputUtils.h" 22 #include <fmq/AidlMessageQueue.h> 23 24 #include <aidl/android/hardware/camera/device/BnCameraDeviceCallback.h> 25 #include <aidl/android/hardware/camera/device/ICameraDevice.h> 26 #include <aidl/android/hardware/camera/device/ICameraInjectionSession.h> 27 namespace android { 28 29 using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite; 30 using ::android::AidlMessageQueue; 31 32 /** 33 * CameraDevice for AIDL HAL devices. 34 */ 35 class AidlCamera3Device : 36 public Camera3Device { 37 public: 38 39 using AidlRequestMetadataQueue = AidlMessageQueue<int8_t, SynchronizedReadWrite>; 40 class AidlCameraDeviceCallbacks; 41 friend class AidlCameraDeviceCallbacks; 42 explicit AidlCamera3Device( 43 std::shared_ptr<CameraServiceProxyWrapper>& cameraServiceProxyWrapper, 44 const String8& id, bool overrideForPerfClass, bool overrideToPortrait, 45 bool legacyClient = false); 46 ~AidlCamera3Device()47 virtual ~AidlCamera3Device() { } 48 49 static aidl::android::hardware::graphics::common::PixelFormat mapToAidlPixelFormat( 50 int frameworkFormat); 51 static aidl::android::hardware::graphics::common::Dataspace mapToAidlDataspace( 52 android_dataspace dataSpace); 53 static aidl::android::hardware::graphics::common::BufferUsage mapToAidlConsumerUsage( 54 uint64_t usage); 55 static aidl::android::hardware::camera::device::StreamRotation 56 mapToAidlStreamRotation(camera_stream_rotation_t rotation); 57 58 static status_t mapToAidlStreamConfigurationMode( 59 camera_stream_configuration_mode_t operationMode, 60 aidl::android::hardware::camera::device::StreamConfigurationMode *mode); 61 62 static int mapToFrameworkFormat( 63 aidl::android::hardware::graphics::common::PixelFormat pixelFormat); 64 static android_dataspace mapToFrameworkDataspace( 65 aidl::android::hardware::graphics::common::Dataspace); 66 static uint64_t mapConsumerToFrameworkUsage( 67 aidl::android::hardware::graphics::common::BufferUsage usage); 68 static uint64_t mapProducerToFrameworkUsage( 69 aidl::android::hardware::graphics::common::BufferUsage usage); 70 71 virtual status_t switchToOffline(const std::vector<int32_t>& /*streamsToKeep*/, 72 /*out*/ sp<CameraOfflineSessionBase>* /*session*/) override; 73 74 status_t initialize(sp<CameraProviderManager> manager, const String8& monitorTags) override; 75 class AidlHalInterface : public Camera3Device::HalInterface { 76 public: 77 AidlHalInterface(std::shared_ptr< 78 aidl::android::hardware::camera::device::ICameraDeviceSession> &session, 79 std::shared_ptr<AidlRequestMetadataQueue> queue, 80 bool useHalBufManager, bool supportOfflineProcessing); 81 AidlHalInterface( 82 std::shared_ptr<aidl::android::hardware::camera::device::ICameraDeviceSession> 83 &deviceSession, 84 std::shared_ptr< 85 aidl::android::hardware::camera::device::ICameraInjectionSession> &injectionSession, 86 std::shared_ptr<AidlRequestMetadataQueue> queue, 87 bool useHalBufManager, bool supportOfflineProcessing); 88 getTransportType()89 virtual IPCTransport getTransportType() const override {return IPCTransport::AIDL; } 90 91 92 // Returns true if constructed with a valid device or session, and not yet cleared 93 virtual bool valid() override; 94 95 // Reset this HalInterface object (does not call close()) 96 virtual void clear() override; 97 98 // Caller takes ownership of requestTemplate 99 virtual status_t constructDefaultRequestSettings(camera_request_template templateId, 100 /*out*/ camera_metadata_t **requestTemplate) override; 101 102 virtual status_t configureStreams(const camera_metadata_t *sessionParams, 103 /*inout*/ camera_stream_configuration_t *config, 104 const std::vector<uint32_t>& bufferSizes, 105 int64_t logId) override; 106 107 // The injection camera configures the streams to hal. 108 virtual status_t configureInjectedStreams( 109 const camera_metadata_t* sessionParams, 110 /*inout*/ camera_stream_configuration_t* config, 111 const std::vector<uint32_t>& bufferSizes, 112 const CameraMetadata& cameraCharacteristics) override; 113 114 // When the call succeeds, the ownership of acquire fences in requests is transferred to 115 // HalInterface. More specifically, the current implementation will send the fence to 116 // HAL process and close the FD in cameraserver process. When the call fails, the ownership 117 // of the acquire fence still belongs to the caller. 118 virtual status_t processBatchCaptureRequests( 119 std::vector<camera_capture_request_t*>& requests, 120 /*out*/uint32_t* numRequestProcessed) override; 121 122 // Calls into the HAL interface 123 virtual status_t flush() override; 124 virtual status_t dump(int fd) override; 125 virtual status_t close() override; 126 virtual void signalPipelineDrain(const std::vector<int>& streamIds) override; 127 virtual bool isReconfigurationRequired(CameraMetadata& oldSessionParams, 128 CameraMetadata& newSessionParams) override; 129 130 virtual status_t repeatingRequestEnd(uint32_t , 131 const std::vector<int32_t> &) override; 132 133 status_t switchToOffline( 134 const std::vector<int32_t>& streamsToKeep, 135 /*out*/aidl::android::hardware::camera::device::CameraOfflineSessionInfo* 136 offlineSessionInfo, 137 /*out*/std::shared_ptr<aidl::android::hardware::camera::device::ICameraOfflineSession>* 138 offlineSession, 139 /*out*/camera3::BufferRecords* bufferRecords); 140 141 private: 142 143 // Always valid 144 std::shared_ptr<aidl::android::hardware::camera::device::ICameraDeviceSession> 145 mAidlSession = nullptr; 146 //Valid for injection sessions 147 std::shared_ptr<aidl::android::hardware::camera::device::ICameraInjectionSession> 148 mAidlInjectionSession = nullptr; 149 150 status_t wrapAsAidlRequest(camera_capture_request_t* request, 151 /*out*/aidl::android::hardware::camera::device::CaptureRequest* captureRequest, 152 /*out*/std::vector<native_handle_t*>* handlesCreated, 153 /*out*/std::vector<std::pair<int32_t, int32_t>>* inflightBuffers); 154 155 std::shared_ptr<AidlRequestMetadataQueue> mRequestMetadataQueue; 156 }; // class AidlHalInterface 157 158 /** 159 * Implementation of aidl::android::hardware::camera::device::ICameraDeviceCallback 160 */ 161 ::ndk::ScopedAStatus processCaptureResult( 162 const std::vector<aidl::android::hardware::camera::device::CaptureResult>& results); 163 ::ndk::ScopedAStatus notify( 164 const std::vector<aidl::android::hardware::camera::device::NotifyMsg>& msgs); 165 166 ::ndk::ScopedAStatus requestStreamBuffers( 167 const std::vector<aidl::android::hardware::camera::device::BufferRequest>& bufReqs, 168 std::vector<aidl::android::hardware::camera::device::StreamBufferRet>* outBuffers, 169 aidl::android::hardware::camera::device::BufferRequestStatus* status); 170 171 ::ndk::ScopedAStatus returnStreamBuffers( 172 const std::vector<aidl::android::hardware::camera::device::StreamBuffer>& buffers); 173 174 class AidlRequestThread : public Camera3Device::RequestThread { 175 public: 176 AidlRequestThread(wp<Camera3Device> parent, 177 sp<camera3::StatusTracker> statusTracker, 178 sp<HalInterface> interface, 179 const Vector<int32_t>& sessionParamKeys, 180 bool useHalBufManager, 181 bool supportCameraMute, 182 bool overrideToPortrait, 183 bool supportSettingsOverride); 184 185 status_t switchToOffline( 186 const std::vector<int32_t>& streamsToKeep, 187 /*out*/aidl::android::hardware::camera::device::CameraOfflineSessionInfo* 188 offlineSessionInfo, 189 /*out*/std::shared_ptr< 190 aidl::android::hardware::camera::device::ICameraOfflineSession>* 191 offlineSession, 192 /*out*/camera3::BufferRecords* bufferRecords); 193 }; // class AidlRequestThread 194 195 class AidlCamera3DeviceInjectionMethods : public Camera3DeviceInjectionMethods { 196 public: 197 // Initialize the injection camera and generate an hal interface. 198 status_t injectionInitialize( 199 const String8& injectedCamId, sp<CameraProviderManager> manager, 200 const std::shared_ptr< 201 aidl::android::hardware::camera::device::ICameraDeviceCallback>& 202 callback); AidlCamera3DeviceInjectionMethods(wp<Camera3Device> parent)203 AidlCamera3DeviceInjectionMethods(wp<Camera3Device> parent) : 204 Camera3DeviceInjectionMethods(parent) { }; ~AidlCamera3DeviceInjectionMethods()205 ~AidlCamera3DeviceInjectionMethods() {} 206 private: 207 // Backup of the original camera hal result FMQ. 208 std::unique_ptr<AidlResultMetadataQueue> mBackupResultMetadataQueue; 209 210 // FMQ writes the result for the injection camera. Must be guarded by 211 // mProcessCaptureResultLock. 212 std::unique_ptr<AidlResultMetadataQueue> mInjectionResultMetadataQueue; 213 214 // Use injection camera hal interface to replace and backup original 215 // camera hal interface. 216 virtual status_t replaceHalInterface(sp<HalInterface> newHalInterface, 217 bool keepBackup) override; 218 }; 219 220 // We need a separate class which inherits from AIDL ICameraDeviceCallbacks 221 // since we use the ndk backend for AIDL HAL interfaces. The ndk backend of 222 // ICameraDeviceCallbacks doesn't support sp<> (since it doesn't inherit 223 // from RefBase). 224 // As a result we can't write sp<Camera3Device> = new AidlCamera3Device(...). 225 // It supports std::shared_ptr instead. Other references to 226 // Camera3Device in cameraserver use sp<> widely, so to keep supporting 227 // that, we create a new class which will be managed through std::shared_ptr 228 // internally by AidlCamera3Device. 229 class AidlCameraDeviceCallbacks : 230 public aidl::android::hardware::camera::device::BnCameraDeviceCallback { 231 public: 232 AidlCameraDeviceCallbacks(wp<AidlCamera3Device> parent)233 AidlCameraDeviceCallbacks(wp<AidlCamera3Device> parent) : mParent(parent) { } ~AidlCameraDeviceCallbacks()234 ~AidlCameraDeviceCallbacks() { } 235 ::ndk::ScopedAStatus processCaptureResult( 236 const std::vector< 237 aidl::android::hardware::camera::device::CaptureResult>& results) override; 238 ::ndk::ScopedAStatus notify( 239 const std::vector< 240 aidl::android::hardware::camera::device::NotifyMsg>& msgs) override; 241 242 ::ndk::ScopedAStatus requestStreamBuffers( 243 const std::vector< 244 aidl::android::hardware::camera::device::BufferRequest>& bufReqs, 245 std::vector<aidl::android::hardware::camera::device::StreamBufferRet>* outBuffers, 246 aidl::android::hardware::camera::device::BufferRequestStatus* status) override; 247 248 ::ndk::ScopedAStatus returnStreamBuffers( 249 const std::vector< 250 aidl::android::hardware::camera::device::StreamBuffer>& buffers) override; 251 252 protected: 253 ::ndk::SpAIBinder createBinder() override; 254 255 private: 256 wp<AidlCamera3Device> mParent = nullptr; 257 }; 258 259 private: 260 virtual status_t injectionCameraInitialize(const String8 &injectCamId, 261 sp<CameraProviderManager> manager) override; 262 263 virtual sp<RequestThread> createNewRequestThread(wp<Camera3Device> parent, 264 sp<camera3::StatusTracker> statusTracker, 265 sp<HalInterface> interface, 266 const Vector<int32_t>& sessionParamKeys, 267 bool useHalBufManager, 268 bool supportCameraMute, 269 bool overrideToPortrait, 270 bool supportSettingsOverride) override; 271 272 virtual sp<Camera3DeviceInjectionMethods> 273 createCamera3DeviceInjectionMethods(wp<Camera3Device>) override; 274 275 // FMQ to write result on. Must be guarded by mProcessCaptureResultLock. 276 std::unique_ptr<AidlResultMetadataQueue> mResultMetadataQueue = nullptr; 277 278 std::shared_ptr<AidlCameraDeviceCallbacks> mCallbacks = nullptr; 279 280 281 }; // class AidlCamera3Device 282 283 }; // namespace android 284 285 #endif 286