Jayant Chowdhary | c67af1b | 2022-04-07 18:05:04 +0000 | [diff] [blame^] | 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 | using android::hardware::camera::metadata::V3_8::CameraMetadataEnumAndroidRequestAvailableDynamicRangeProfilesMap; |
| 31 | using android::hardware::camera::metadata::V3_8::CameraMetadataEnumAndroidScalerAvailableStreamUseCases; |
| 32 | |
| 33 | namespace android { |
| 34 | namespace camera3 { |
| 35 | |
| 36 | namespace SessionConfigurationUtils { |
| 37 | |
| 38 | status_t |
| 39 | convertAidlToHidl38StreamCombination( |
| 40 | const aidl::android::hardware::camera::device::StreamConfiguration &aidl, |
| 41 | hardware::camera::device::V3_8::StreamConfiguration &hidl) { |
| 42 | hidl.operationMode = |
| 43 | static_cast<hardware::camera::device::V3_2::StreamConfigurationMode>(aidl.operationMode); |
| 44 | if (aidl.streamConfigCounter < 0) { |
| 45 | return BAD_VALUE; |
| 46 | } |
| 47 | hidl.streamConfigCounter = static_cast<uint32_t>(aidl.streamConfigCounter); |
| 48 | hidl.multiResolutionInputImage = aidl.multiResolutionInputImage; |
| 49 | hidl.sessionParams = aidl.sessionParams.metadata; |
| 50 | hidl.streams.resize(aidl.streams.size()); |
| 51 | size_t i = 0; |
| 52 | for (const auto &stream : aidl.streams) { |
| 53 | //hidlv3_8 |
| 54 | hidl.streams[i].dynamicRangeProfile = |
| 55 | static_cast< |
| 56 | CameraMetadataEnumAndroidRequestAvailableDynamicRangeProfilesMap> |
| 57 | (stream.dynamicRangeProfile); |
| 58 | hidl.streams[i].useCase = |
| 59 | static_cast< |
| 60 | CameraMetadataEnumAndroidScalerAvailableStreamUseCases> |
| 61 | (stream.useCase); |
| 62 | |
| 63 | // hidl v3_7 |
| 64 | hidl.streams[i].v3_7.groupId = stream.groupId; |
| 65 | hidl.streams[i].v3_7.sensorPixelModesUsed.resize(stream.sensorPixelModesUsed.size()); |
| 66 | size_t j = 0; |
| 67 | for (const auto &mode : stream.sensorPixelModesUsed) { |
| 68 | hidl.streams[i].v3_7.sensorPixelModesUsed[j] = |
| 69 | static_cast<CameraMetadataEnumAndroidSensorPixelMode>(mode); |
| 70 | j++; |
| 71 | } |
| 72 | |
| 73 | //hidl v3_4 |
| 74 | hidl.streams[i].v3_7.v3_4.physicalCameraId = stream.physicalCameraId; |
| 75 | |
| 76 | if (stream.bufferSize < 0) { |
| 77 | return BAD_VALUE; |
| 78 | } |
| 79 | hidl.streams[i].v3_7.v3_4.bufferSize = static_cast<uint32_t>(stream.bufferSize); |
| 80 | |
| 81 | // hild v3_2 |
| 82 | hidl.streams[i].v3_7.v3_4.v3_2.id = stream.id; |
| 83 | hidl.streams[i].v3_7.v3_4.v3_2.format = |
| 84 | static_cast<hardware::graphics::common::V1_0::PixelFormat>(stream.format); |
| 85 | |
| 86 | if (stream.width < 0 || stream.height < 0) { |
| 87 | return BAD_VALUE; |
| 88 | } |
| 89 | hidl.streams[i].v3_7.v3_4.v3_2.width = static_cast<uint32_t>(stream.width); |
| 90 | hidl.streams[i].v3_7.v3_4.v3_2.height = static_cast<uint32_t>(stream.height); |
| 91 | hidl.streams[i].v3_7.v3_4.v3_2.usage = |
| 92 | static_cast<hardware::camera::device::V3_2::BufferUsageFlags>(stream.usage); |
| 93 | hidl.streams[i].v3_7.v3_4.v3_2.streamType = |
| 94 | static_cast<hardware::camera::device::V3_2::StreamType>(stream.streamType); |
| 95 | hidl.streams[i].v3_7.v3_4.v3_2.dataSpace = |
| 96 | static_cast<hardware::camera::device::V3_2::DataspaceFlags>(stream.dataSpace); |
| 97 | hidl.streams[i].v3_7.v3_4.v3_2.rotation = |
| 98 | static_cast<hardware::camera::device::V3_2::StreamRotation>(stream.rotation); |
| 99 | i++; |
| 100 | } |
| 101 | return OK; |
| 102 | } |
| 103 | |
| 104 | void mapStreamInfo(const OutputStreamInfo &streamInfo, |
| 105 | camera3::camera_stream_rotation_t rotation, String8 physicalId, |
| 106 | int32_t groupId, hardware::camera::device::V3_8::Stream *stream /*out*/) { |
| 107 | if (stream == nullptr) { |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | stream->v3_7.v3_4.v3_2.streamType = hardware::camera::device::V3_2::StreamType::OUTPUT; |
| 112 | stream->v3_7.v3_4.v3_2.width = streamInfo.width; |
| 113 | stream->v3_7.v3_4.v3_2.height = streamInfo.height; |
| 114 | stream->v3_7.v3_4.v3_2.format = HidlCamera3Device::mapToPixelFormat(streamInfo.format); |
| 115 | auto u = streamInfo.consumerUsage; |
| 116 | camera3::Camera3OutputStream::applyZSLUsageQuirk(streamInfo.format, &u); |
| 117 | stream->v3_7.v3_4.v3_2.usage = HidlCamera3Device::mapToConsumerUsage(u); |
| 118 | stream->v3_7.v3_4.v3_2.dataSpace = HidlCamera3Device::mapToHidlDataspace(streamInfo.dataSpace); |
| 119 | stream->v3_7.v3_4.v3_2.rotation = HidlCamera3Device::mapToStreamRotation(rotation); |
| 120 | stream->v3_7.v3_4.v3_2.id = -1; // Invalid stream id |
| 121 | stream->v3_7.v3_4.physicalCameraId = std::string(physicalId.string()); |
| 122 | stream->v3_7.v3_4.bufferSize = 0; |
| 123 | stream->v3_7.groupId = groupId; |
| 124 | stream->v3_7.sensorPixelModesUsed.resize(streamInfo.sensorPixelModesUsed.size()); |
| 125 | |
| 126 | size_t idx = 0; |
| 127 | for (auto mode : streamInfo.sensorPixelModesUsed) { |
| 128 | stream->v3_7.sensorPixelModesUsed[idx++] = |
| 129 | static_cast<CameraMetadataEnumAndroidSensorPixelMode>(mode); |
| 130 | } |
| 131 | stream->dynamicRangeProfile = |
| 132 | static_cast<CameraMetadataEnumAndroidRequestAvailableDynamicRangeProfilesMap> ( |
| 133 | streamInfo.dynamicRangeProfile); |
| 134 | stream->useCase = static_cast<CameraMetadataEnumAndroidScalerAvailableStreamUseCases>( |
| 135 | streamInfo.streamUseCase); |
| 136 | } |
| 137 | |
| 138 | binder::Status |
| 139 | convertToHALStreamCombination( |
| 140 | const SessionConfiguration& sessionConfiguration, |
| 141 | const String8 &logicalCameraId, const CameraMetadata &deviceInfo, |
| 142 | metadataGetter getMetadata, const std::vector<std::string> &physicalCameraIds, |
| 143 | hardware::camera::device::V3_8::StreamConfiguration &streamConfiguration, |
| 144 | bool overrideForPerfClass, bool *earlyExit) { |
| 145 | aidl::android::hardware::camera::device::StreamConfiguration aidlStreamConfiguration; |
| 146 | auto ret = convertToHALStreamCombination(sessionConfiguration, logicalCameraId, deviceInfo, |
| 147 | getMetadata, physicalCameraIds, aidlStreamConfiguration, overrideForPerfClass, |
| 148 | earlyExit); |
| 149 | if (!ret.isOk()) { |
| 150 | return ret; |
| 151 | } |
| 152 | if (earlyExit != nullptr && *earlyExit) { |
| 153 | return binder::Status::ok(); |
| 154 | } |
| 155 | |
| 156 | if (convertAidlToHidl38StreamCombination(aidlStreamConfiguration, streamConfiguration) != OK) { |
| 157 | return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, |
| 158 | "Invalid AIDL->HIDL3.8 conversion"); |
| 159 | } |
| 160 | |
| 161 | return binder::Status::ok(); |
| 162 | } |
| 163 | |
| 164 | bool convertHALStreamCombinationFromV38ToV37( |
| 165 | hardware::camera::device::V3_7::StreamConfiguration &streamConfigV37, |
| 166 | const hardware::camera::device::V3_8::StreamConfiguration &streamConfigV38) { |
| 167 | streamConfigV37.streams.resize(streamConfigV38.streams.size()); |
| 168 | for (size_t i = 0; i < streamConfigV38.streams.size(); i++) { |
| 169 | if (static_cast<int64_t>(streamConfigV38.streams[i].dynamicRangeProfile) != |
| 170 | ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) { |
| 171 | // ICameraDevice older than 3.8 doesn't support 10-bit dynamic range profiles |
| 172 | // image |
| 173 | return false; |
| 174 | } |
| 175 | if (static_cast<int64_t>(streamConfigV38.streams[i].useCase) != |
| 176 | ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT) { |
| 177 | // ICameraDevice older than 3.8 doesn't support stream use case |
| 178 | return false; |
| 179 | } |
| 180 | streamConfigV37.streams[i] = streamConfigV38.streams[i].v3_7; |
| 181 | } |
| 182 | streamConfigV37.operationMode = streamConfigV38.operationMode; |
| 183 | streamConfigV37.sessionParams = streamConfigV38.sessionParams; |
| 184 | |
| 185 | return true; |
| 186 | } |
| 187 | |
| 188 | bool convertHALStreamCombinationFromV37ToV34( |
| 189 | hardware::camera::device::V3_4::StreamConfiguration &streamConfigV34, |
| 190 | const hardware::camera::device::V3_7::StreamConfiguration &streamConfigV37) { |
| 191 | if (streamConfigV37.multiResolutionInputImage) { |
| 192 | // ICameraDevice older than 3.7 doesn't support multi-resolution input image. |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | streamConfigV34.streams.resize(streamConfigV37.streams.size()); |
| 197 | for (size_t i = 0; i < streamConfigV37.streams.size(); i++) { |
| 198 | if (streamConfigV37.streams[i].groupId != -1) { |
| 199 | // ICameraDevice older than 3.7 doesn't support multi-resolution output |
| 200 | // image |
| 201 | return false; |
| 202 | } |
| 203 | streamConfigV34.streams[i] = streamConfigV37.streams[i].v3_4; |
| 204 | } |
| 205 | streamConfigV34.operationMode = streamConfigV37.operationMode; |
| 206 | streamConfigV34.sessionParams = streamConfigV37.sessionParams; |
| 207 | |
| 208 | return true; |
| 209 | } |
| 210 | |
| 211 | } // namespace SessionConfigurationUtils |
| 212 | } // namespace camera3 |
| 213 | } // namespace android |