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