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