• 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(const String8& id, bool overrideForPerfClass, bool overrideToPortrait,
Camera3Device(id,overrideForPerfClass,overrideToPortrait,legacyClient)35           bool legacyClient = false) : Camera3Device(id, overrideForPerfClass, overrideToPortrait,
36           legacyClient) { }
37 
~HidlCamera3Device()38     virtual ~HidlCamera3Device() {}
39 
40    /**
41      * Helper functions to map between framework and HIDL values
42      */
43     static hardware::graphics::common::V1_0::PixelFormat mapToPixelFormat(int frameworkFormat);
44     static hardware::camera::device::V3_2::DataspaceFlags mapToHidlDataspace(
45             android_dataspace dataSpace);
46     static hardware::camera::device::V3_2::BufferUsageFlags mapToConsumerUsage(uint64_t usage);
47     static hardware::camera::device::V3_2::StreamRotation mapToStreamRotation(
48             camera_stream_rotation_t rotation);
49     // Returns a negative error code if the passed-in operation mode is not valid.
50     static status_t mapToStreamConfigurationMode(camera_stream_configuration_mode_t operationMode,
51             /*out*/ hardware::camera::device::V3_2::StreamConfigurationMode *mode);
52     static int mapToFrameworkFormat(hardware::graphics::common::V1_0::PixelFormat pixelFormat);
53     static android_dataspace mapToFrameworkDataspace(
54             hardware::camera::device::V3_2::DataspaceFlags);
55     static uint64_t mapConsumerToFrameworkUsage(
56             hardware::camera::device::V3_2::BufferUsageFlags usage);
57     static uint64_t mapProducerToFrameworkUsage(
58             hardware::camera::device::V3_2::BufferUsageFlags usage);
59 
60     status_t initialize(sp<CameraProviderManager> manager, const String8& monitorTags) override;
61 
62     /**
63      * Implementation of android::hardware::camera::device::V3_5::ICameraDeviceCallback
64      */
65 
66     hardware::Return<void> processCaptureResult_3_4(
67             const hardware::hidl_vec<
68                     hardware::camera::device::V3_4::CaptureResult>& results) override;
69     hardware::Return<void> processCaptureResult(
70             const hardware::hidl_vec<
71                     hardware::camera::device::V3_2::CaptureResult>& results) override;
72     hardware::Return<void> notify(
73             const hardware::hidl_vec<
74                     hardware::camera::device::V3_2::NotifyMsg>& msgs) override;
75 
76     hardware::Return<void> requestStreamBuffers(
77             const hardware::hidl_vec<
78                     hardware::camera::device::V3_5::BufferRequest>& bufReqs,
79             requestStreamBuffers_cb _hidl_cb) override;
80 
81     hardware::Return<void> returnStreamBuffers(
82             const hardware::hidl_vec<
83                     hardware::camera::device::V3_2::StreamBuffer>& buffers) override;
84 
85     // Handle one notify message
86     void notify(const hardware::camera::device::V3_2::NotifyMsg& msg);
87 
88     status_t switchToOffline(const std::vector<int32_t>& streamsToKeep,
89             /*out*/ sp<CameraOfflineSessionBase>* session) override;
90 
91     using RequestMetadataQueue = hardware::MessageQueue<uint8_t, hardware::kSynchronizedReadWrite>;
92 
93     class HidlHalInterface : public Camera3Device::HalInterface {
94      public:
95         HidlHalInterface(sp<hardware::camera::device::V3_2::ICameraDeviceSession> &session,
96                      std::shared_ptr<RequestMetadataQueue> queue,
97                      bool useHalBufManager, bool supportOfflineProcessing);
98 
getTransportType()99         virtual IPCTransport getTransportType() const override { return IPCTransport::HIDL; }
100         // Returns true if constructed with a valid device or session, and not yet cleared
101         virtual bool valid() override;
102 
103         // Reset this HalInterface object (does not call close())
104         virtual void clear() override;
105 
106         // Calls into the HAL interface
107 
108         // Caller takes ownership of requestTemplate
109         virtual status_t constructDefaultRequestSettings(camera_request_template templateId,
110                 /*out*/ camera_metadata_t **requestTemplate) override;
111 
112         virtual status_t configureStreams(const camera_metadata_t *sessionParams,
113                 /*inout*/ camera_stream_configuration_t *config,
114                 const std::vector<uint32_t>& bufferSizes) 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 
182         status_t switchToOffline(
183                 const std::vector<int32_t>& streamsToKeep,
184                 /*out*/hardware::camera::device::V3_6::CameraOfflineSessionInfo* offlineSessionInfo,
185                 /*out*/sp<hardware::camera::device::V3_6::ICameraOfflineSession>* offlineSession,
186                 /*out*/camera3::BufferRecords* bufferRecords);
187     }; // class HidlRequestThread
188 
189     class HidlCamera3DeviceInjectionMethods : public Camera3DeviceInjectionMethods {
190      public:
191         // Initialize the injection camera and generate an hal interface.
192         status_t injectionInitialize(
193                 const String8& injectedCamId, sp<CameraProviderManager> manager,
194                 const sp<
195                     android::hardware::camera::device::V3_2 ::ICameraDeviceCallback>&
196                     callback);
HidlCamera3DeviceInjectionMethods(wp<Camera3Device> parent)197         HidlCamera3DeviceInjectionMethods(wp<Camera3Device> parent) :
198                 Camera3DeviceInjectionMethods(parent) { };
~HidlCamera3DeviceInjectionMethods()199         ~HidlCamera3DeviceInjectionMethods() {}
200      private:
201         // Backup of the original camera hal result FMQ.
202         std::unique_ptr<ResultMetadataQueue> mBackupResultMetadataQueue;
203 
204         // FMQ writes the result for the injection camera. Must be guarded by
205         // mProcessCaptureResultLock.
206         std::unique_ptr<ResultMetadataQueue> mInjectionResultMetadataQueue;
207 
208         // Use injection camera hal interface to replace and backup original
209         // camera hal interface.
210         virtual status_t replaceHalInterface(sp<HalInterface> newHalInterface,
211                 bool keepBackup) override;
212     };
213 
214   private:
215     template<typename NotifyMsgType>
216     hardware::Return<void> notifyHelper(
217             const hardware::hidl_vec<NotifyMsgType>& msgs);
218 
219     virtual status_t injectionCameraInitialize(const String8 &injectCamId,
220             sp<CameraProviderManager> manager) override;
221 
222     virtual sp<RequestThread> createNewRequestThread(wp<Camera3Device> parent,
223                 sp<camera3::StatusTracker> statusTracker,
224                 sp<HalInterface> interface,
225                 const Vector<int32_t>& sessionParamKeys,
226                 bool useHalBufManager,
227                 bool supportCameraMute,
228                 bool overrideToPortrait) override;
229 
230     virtual sp<Camera3DeviceInjectionMethods>
231             createCamera3DeviceInjectionMethods(wp<Camera3Device>) override;
232 
233     // FMQ to write result on. Must be guarded by mProcessCaptureResultLock.
234     std::unique_ptr<ResultMetadataQueue> mResultMetadataQueue;
235 
236 }; // class HidlCamera3Device
237 
238 }; // namespace android
239 
240 #endif
241