xref: /aosp_15_r20/frameworks/av/services/camera/libcameraservice/utils/SessionConfigurationUtilsHidl.cpp (revision ec779b8e0859a360c3d303172224686826e6e0e1)
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 #include <cutils/properties.h>
18 
19 #include "SessionConfigurationUtils.h"
20 #include "SessionConfigurationUtilsHidl.h"
21 
22 #include "../CameraService.h"
23 #include "device3/aidl/AidlCamera3Device.h"
24 #include "device3/hidl/HidlCamera3Device.h"
25 #include "device3/Camera3OutputStream.h"
26 #include "utils/Utils.h"
27 
28 using android::camera3::OutputStreamInfo;
29 using android::hardware::camera2::ICameraDeviceUser;
30 using android::hardware::camera::metadata::V3_6::CameraMetadataEnumAndroidSensorPixelMode;
31 
32 namespace android {
33 namespace camera3 {
34 
35 namespace SessionConfigurationUtils {
36 
37 status_t
convertAidlToHidl37StreamCombination(const aidl::android::hardware::camera::device::StreamConfiguration & aidl,hardware::camera::device::V3_7::StreamConfiguration & hidl)38 convertAidlToHidl37StreamCombination(
39         const aidl::android::hardware::camera::device::StreamConfiguration &aidl,
40         hardware::camera::device::V3_7::StreamConfiguration &hidl) {
41     hidl.operationMode =
42         static_cast<hardware::camera::device::V3_2::StreamConfigurationMode>(aidl.operationMode);
43     if (aidl.streamConfigCounter < 0) {
44         return BAD_VALUE;
45     }
46     hidl.streamConfigCounter = static_cast<uint32_t>(aidl.streamConfigCounter);
47     hidl.multiResolutionInputImage = aidl.multiResolutionInputImage;
48     hidl.sessionParams = aidl.sessionParams.metadata;
49     hidl.streams.resize(aidl.streams.size());
50     size_t i = 0;
51     for (const auto &stream : aidl.streams) {
52         if (eToI(stream.dynamicRangeProfile) !=
53                 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
54             ALOGE("%s Dynamic range profile %" PRId64 " not supported by HIDL", __FUNCTION__,
55                     eToI(stream.dynamicRangeProfile));
56             return BAD_VALUE;
57         }
58 
59         if (eToI(stream.useCase) != ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT) {
60             ALOGE("%s Stream use case %" PRId64 "not supported by HIDL", __FUNCTION__,
61                     eToI(stream.useCase));
62             return BAD_VALUE;
63         }
64 
65         // hidl v3_7
66         hidl.streams[i].groupId = stream.groupId;
67         hidl.streams[i].sensorPixelModesUsed.resize(stream.sensorPixelModesUsed.size());
68         size_t j = 0;
69         for (const auto &mode : stream.sensorPixelModesUsed) {
70             hidl.streams[i].sensorPixelModesUsed[j] =
71                     static_cast<CameraMetadataEnumAndroidSensorPixelMode>(mode);
72             j++;
73         }
74 
75         //hidl v3_4
76         hidl.streams[i].v3_4.physicalCameraId = stream.physicalCameraId;
77 
78         if (stream.bufferSize < 0) {
79             return BAD_VALUE;
80         }
81         hidl.streams[i].v3_4.bufferSize = static_cast<uint32_t>(stream.bufferSize);
82 
83         // hild v3_2
84         hidl.streams[i].v3_4.v3_2.id = stream.id;
85         hidl.streams[i].v3_4.v3_2.format =
86                 static_cast<hardware::graphics::common::V1_0::PixelFormat>(stream.format);
87 
88         if (stream.width < 0 || stream.height < 0) {
89             return BAD_VALUE;
90         }
91         hidl.streams[i].v3_4.v3_2.width = static_cast<uint32_t>(stream.width);
92         hidl.streams[i].v3_4.v3_2.height = static_cast<uint32_t>(stream.height);
93         hidl.streams[i].v3_4.v3_2.usage =
94                 static_cast<hardware::camera::device::V3_2::BufferUsageFlags>(stream.usage);
95         hidl.streams[i].v3_4.v3_2.streamType =
96                 static_cast<hardware::camera::device::V3_2::StreamType>(stream.streamType);
97         hidl.streams[i].v3_4.v3_2.dataSpace =
98                 static_cast<hardware::camera::device::V3_2::DataspaceFlags>(stream.dataSpace);
99         hidl.streams[i].v3_4.v3_2.rotation =
100                 static_cast<hardware::camera::device::V3_2::StreamRotation>(stream.rotation);
101         i++;
102     }
103     return OK;
104 }
105 
106 binder::Status
convertToHALStreamCombination(const SessionConfiguration & sessionConfiguration,const std::string & logicalCameraId,const CameraMetadata & deviceInfo,metadataGetter getMetadata,const std::vector<std::string> & physicalCameraIds,hardware::camera::device::V3_7::StreamConfiguration & streamConfiguration,bool overrideForPerfClass,metadata_vendor_id_t vendorTagId,bool * earlyExit)107 convertToHALStreamCombination(
108         const SessionConfiguration& sessionConfiguration,
109         const std::string &logicalCameraId, const CameraMetadata &deviceInfo,
110         metadataGetter getMetadata, const std::vector<std::string> &physicalCameraIds,
111         hardware::camera::device::V3_7::StreamConfiguration &streamConfiguration,
112         bool overrideForPerfClass, metadata_vendor_id_t vendorTagId, bool *earlyExit) {
113     aidl::android::hardware::camera::device::StreamConfiguration aidlStreamConfiguration;
114     auto ret = convertToHALStreamCombination(sessionConfiguration, logicalCameraId, deviceInfo,
115             false /*isCompositeJpegRDisabled*/, getMetadata, physicalCameraIds,
116             aidlStreamConfiguration, overrideForPerfClass, vendorTagId,
117             /*checkSessionParams*/false, /*additionalKeys*/{}, earlyExit);
118     if (!ret.isOk()) {
119         return ret;
120     }
121     if (earlyExit != nullptr && *earlyExit) {
122         return binder::Status::ok();
123     }
124 
125     if (convertAidlToHidl37StreamCombination(aidlStreamConfiguration, streamConfiguration) != OK) {
126         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
127                 "Invalid AIDL->HIDL3.7 conversion");
128     }
129 
130     return binder::Status::ok();
131 }
132 
convertHALStreamCombinationFromV37ToV34(hardware::camera::device::V3_4::StreamConfiguration & streamConfigV34,const hardware::camera::device::V3_7::StreamConfiguration & streamConfigV37)133 bool convertHALStreamCombinationFromV37ToV34(
134         hardware::camera::device::V3_4::StreamConfiguration &streamConfigV34,
135         const hardware::camera::device::V3_7::StreamConfiguration &streamConfigV37) {
136     if (streamConfigV37.multiResolutionInputImage) {
137         // ICameraDevice older than 3.7 doesn't support multi-resolution input image.
138         return false;
139     }
140 
141     streamConfigV34.streams.resize(streamConfigV37.streams.size());
142     for (size_t i = 0; i < streamConfigV37.streams.size(); i++) {
143         if (streamConfigV37.streams[i].groupId != -1) {
144             // ICameraDevice older than 3.7 doesn't support multi-resolution output
145             // image
146             return false;
147         }
148         streamConfigV34.streams[i] = streamConfigV37.streams[i].v3_4;
149     }
150     streamConfigV34.operationMode = streamConfigV37.operationMode;
151     streamConfigV34.sessionParams = streamConfigV37.sessionParams;
152 
153     return true;
154 }
155 
156 } // namespace SessionConfigurationUtils
157 } // namespace camera3
158 } // namespace android
159