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 legacyClient = false) :
Camera3Device(cameraServiceProxyWrapper,attributionAndPermissionUtils,id,overrideForPerfClass,rotationOverride,legacyClient)39         Camera3Device(cameraServiceProxyWrapper, attributionAndPermissionUtils, id,
40                 overrideForPerfClass, rotationOverride, 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     /**
67      * Implementation of android::hardware::camera::device::V3_5::ICameraDeviceCallback
68      */
69 
70     hardware::Return<void> processCaptureResult_3_4(
71             const hardware::hidl_vec<
72                     hardware::camera::device::V3_4::CaptureResult>& results) override;
73     hardware::Return<void> processCaptureResult(
74             const hardware::hidl_vec<
75                     hardware::camera::device::V3_2::CaptureResult>& results) override;
76     hardware::Return<void> notify(
77             const hardware::hidl_vec<
78                     hardware::camera::device::V3_2::NotifyMsg>& msgs) override;
79 
80     hardware::Return<void> requestStreamBuffers(
81             const hardware::hidl_vec<
82                     hardware::camera::device::V3_5::BufferRequest>& bufReqs,
83             requestStreamBuffers_cb _hidl_cb) override;
84 
85     hardware::Return<void> returnStreamBuffers(
86             const hardware::hidl_vec<
87                     hardware::camera::device::V3_2::StreamBuffer>& buffers) override;
88 
89     status_t switchToOffline(const std::vector<int32_t>& streamsToKeep,
90             /*out*/ sp<CameraOfflineSessionBase>* session) override;
91 
92     using RequestMetadataQueue = hardware::MessageQueue<uint8_t, hardware::kSynchronizedReadWrite>;
93 
94     class HidlHalInterface : public Camera3Device::HalInterface {
95      public:
96         HidlHalInterface(sp<hardware::camera::device::V3_2::ICameraDeviceSession> &session,
97                      std::shared_ptr<RequestMetadataQueue> queue,
98                      bool useHalBufManager, bool supportOfflineProcessing);
99 
getTransportType()100         virtual IPCTransport getTransportType() const override { return IPCTransport::HIDL; }
101         // Returns true if constructed with a valid device or session, and not yet cleared
102         virtual bool valid() override;
103 
104         // Reset this HalInterface object (does not call close())
105         virtual void clear() override;
106 
107         // Calls into the HAL interface
108 
109         // Caller takes ownership of requestTemplate
110         virtual status_t constructDefaultRequestSettings(camera_request_template templateId,
111                 /*out*/ camera_metadata_t **requestTemplate) override;
112 
113         virtual status_t configureStreams(const camera_metadata_t *sessionParams,
114                 /*inout*/ camera_stream_configuration_t *config,
115                 const std::vector<uint32_t>& bufferSizes,
116                 int64_t logId) override;
117 
118         // The injection camera configures the streams to hal.
119         virtual status_t configureInjectedStreams(
120                 const camera_metadata_t* sessionParams,
121                 /*inout*/ camera_stream_configuration_t* config,
122                 const std::vector<uint32_t>& bufferSizes,
123                 const CameraMetadata& cameraCharacteristics) override;
124 
125         // When the call succeeds, the ownership of acquire fences in requests is transferred to
126         // HalInterface. More specifically, the current implementation will send the fence to
127         // HAL process and close the FD in cameraserver process. When the call fails, the ownership
128         // of the acquire fence still belongs to the caller.
129         virtual status_t processBatchCaptureRequests(
130                 std::vector<camera_capture_request_t*>& requests,
131                 /*out*/uint32_t* numRequestProcessed) override;
132         virtual status_t flush() override;
133         virtual status_t dump(int fd) override;
134         virtual status_t close() override;
135 
136         virtual void signalPipelineDrain(const std::vector<int>& streamIds) override;
137         virtual bool isReconfigurationRequired(CameraMetadata& oldSessionParams,
138                 CameraMetadata& newSessionParams) override;
139 
140         virtual status_t repeatingRequestEnd(uint32_t frameNumber,
141                 const std::vector<int32_t> &streamIds) override;
142 
143         status_t switchToOffline(
144         const std::vector<int32_t>& streamsToKeep,
145         /*out*/hardware::camera::device::V3_6::CameraOfflineSessionInfo* offlineSessionInfo,
146         /*out*/sp<hardware::camera::device::V3_6::ICameraOfflineSession>* offlineSession,
147         /*out*/camera3::BufferRecords* bufferRecords);
148 
149      private:
150 
151         // Always valid
152         sp<hardware::camera::device::V3_2::ICameraDeviceSession> mHidlSession;
153         // Valid if ICameraDeviceSession is @3.3 or newer
154         sp<hardware::camera::device::V3_3::ICameraDeviceSession> mHidlSession_3_3;
155         // Valid if ICameraDeviceSession is @3.4 or newer
156         sp<hardware::camera::device::V3_4::ICameraDeviceSession> mHidlSession_3_4;
157         // Valid if ICameraDeviceSession is @3.5 or newer
158         sp<hardware::camera::device::V3_5::ICameraDeviceSession> mHidlSession_3_5;
159         // Valid if ICameraDeviceSession is @3.6 or newer
160         sp<hardware::camera::device::V3_6::ICameraDeviceSession> mHidlSession_3_6;
161         // Valid if ICameraDeviceSession is @3.7 or newer
162         sp<hardware::camera::device::V3_7::ICameraDeviceSession> mHidlSession_3_7;
163 
164         std::shared_ptr<RequestMetadataQueue> mRequestMetadataQueue;
165 
166         // The output HIDL request still depends on input camera_capture_request_t
167         // Do not free input camera_capture_request_t before output HIDL request
168         status_t wrapAsHidlRequest(camera_capture_request_t* in,
169                 /*out*/hardware::camera::device::V3_2::CaptureRequest* out,
170                 /*out*/std::vector<native_handle_t*>* handlesCreated,
171                 /*out*/std::vector<std::pair<int32_t, int32_t>>* inflightBuffers);
172     }; // class HidlHalInterface
173 
174     class HidlRequestThread : public Camera3Device::RequestThread {
175       public:
176         HidlRequestThread(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                 int rotationOverride,
183                 bool supportSettingsOverride);
184 
185         status_t switchToOffline(
186                 const std::vector<int32_t>& streamsToKeep,
187                 /*out*/hardware::camera::device::V3_6::CameraOfflineSessionInfo* offlineSessionInfo,
188                 /*out*/sp<hardware::camera::device::V3_6::ICameraOfflineSession>* offlineSession,
189                 /*out*/camera3::BufferRecords* bufferRecords);
190     }; // class HidlRequestThread
191 
192     class HidlCamera3DeviceInjectionMethods : public Camera3DeviceInjectionMethods {
193      public:
194         // Initialize the injection camera and generate an hal interface.
195         status_t injectionInitialize(
196                 const std::string& injectedCamId, sp<CameraProviderManager> manager,
197                 const sp<
198                     android::hardware::camera::device::V3_2 ::ICameraDeviceCallback>&
199                     callback);
HidlCamera3DeviceInjectionMethods(wp<Camera3Device> parent)200         HidlCamera3DeviceInjectionMethods(wp<Camera3Device> parent) :
201                 Camera3DeviceInjectionMethods(parent) { };
~HidlCamera3DeviceInjectionMethods()202         ~HidlCamera3DeviceInjectionMethods() {}
203      private:
204         // Backup of the original camera hal result FMQ.
205         std::unique_ptr<ResultMetadataQueue> mBackupResultMetadataQueue;
206 
207         // FMQ writes the result for the injection camera. Must be guarded by
208         // mProcessCaptureResultLock.
209         std::unique_ptr<ResultMetadataQueue> mInjectionResultMetadataQueue;
210 
211         // Use injection camera hal interface to replace and backup original
212         // camera hal interface.
213         virtual status_t replaceHalInterface(sp<HalInterface> newHalInterface,
214                 bool keepBackup) override;
215     };
216 
217   private:
218     template<typename NotifyMsgType>
219     hardware::Return<void> notifyHelper(
220             const hardware::hidl_vec<NotifyMsgType>& msgs);
221 
222     virtual void applyMaxBatchSizeLocked(
223             RequestList* requestList,
224             const sp<camera3::Camera3OutputStreamInterface>& stream) override;
225 
226     virtual status_t injectionCameraInitialize(const std::string &injectCamId,
227             sp<CameraProviderManager> manager) override;
228 
229     virtual sp<RequestThread> createNewRequestThread(wp<Camera3Device> parent,
230                 sp<camera3::StatusTracker> statusTracker,
231                 sp<HalInterface> interface,
232                 const Vector<int32_t>& sessionParamKeys,
233                 bool useHalBufManager,
234                 bool supportCameraMute,
235                 int rotationOverride,
236                 bool supportSettingsOverride) override;
237 
238     virtual sp<Camera3DeviceInjectionMethods>
239             createCamera3DeviceInjectionMethods(wp<Camera3Device>) override;
240 
241     // FMQ to write result on. Must be guarded by mProcessCaptureResultLock.
242     std::unique_ptr<ResultMetadataQueue> mResultMetadataQueue;
243 
244 }; // class HidlCamera3Device
245 
246 }; // namespace android
247 
248 #endif
249