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