1 /*
2 * Copyright (c) 2024-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "ability/camera_ability.h"
16 #include "session/capture_session.h"
17 #include "camera_util.h"
18 #include "camera_log.h"
19
20 namespace OHOS {
21 namespace CameraStandard {
22
23 constexpr size_t VALID_RANGE_SIZE = 2;
24
g_isValidZoomRaioRange(std::vector<float> range)25 bool g_isValidZoomRaioRange(std::vector<float> range)
26 {
27 return range.size() == VALID_RANGE_SIZE && range[0] <= range[1];
28 }
29
HasFlash()30 bool CameraAbility::HasFlash()
31 {
32 bool onlyHasCloseMode = supportedFlashModes_.size() == 1 && supportedFlashModes_[0] == FLASH_MODE_CLOSE;
33 return !supportedFlashModes_.empty() && !onlyHasCloseMode;
34 }
35
IsFlashModeSupported(FlashMode flashMode)36 bool CameraAbility::IsFlashModeSupported(FlashMode flashMode)
37 {
38 return std::find(
39 supportedFlashModes_.begin(), supportedFlashModes_.end(), flashMode) != supportedFlashModes_.end();
40 }
41
GetSupportedFlashModes()42 std::vector<FlashMode> CameraAbility::GetSupportedFlashModes()
43 {
44 return supportedFlashModes_;
45 }
46
IsExposureModeSupported(ExposureMode exposureMode)47 bool CameraAbility::IsExposureModeSupported(ExposureMode exposureMode)
48 {
49 return std::find(
50 supportedExposureModes_.begin(), supportedExposureModes_.end(), exposureMode) != supportedExposureModes_.end();
51 }
52
GetSupportedExposureModes()53 std::vector<ExposureMode> CameraAbility::GetSupportedExposureModes()
54 {
55 return supportedExposureModes_;
56 }
57
GetExposureBiasRange()58 std::vector<float> CameraAbility::GetExposureBiasRange()
59 {
60 return exposureBiasRange_;
61 }
62
GetSupportedFocusModes()63 std::vector<FocusMode> CameraAbility::GetSupportedFocusModes()
64 {
65 return supportedFocusModes_;
66 }
67
IsFocusModeSupported(FocusMode focusMode)68 bool CameraAbility::IsFocusModeSupported(FocusMode focusMode)
69 {
70 return std::find(supportedFocusModes_.begin(), supportedFocusModes_.end(), focusMode) != supportedFocusModes_.end();
71 }
72
IsFocusRangeTypeSupported(FocusRangeType focusRangeType)73 bool CameraAbility::IsFocusRangeTypeSupported(FocusRangeType focusRangeType)
74 {
75 return std::find(supportedFocusRangeTypes_.begin(), supportedFocusRangeTypes_.end(), focusRangeType) !=
76 supportedFocusRangeTypes_.end();
77 }
78
IsFocusDrivenTypeSupported(FocusDrivenType focusDrivenType)79 bool CameraAbility::IsFocusDrivenTypeSupported(FocusDrivenType focusDrivenType)
80 {
81 return std::find(supportedFocusDrivenTypes_.begin(), supportedFocusDrivenTypes_.end(), focusDrivenType) !=
82 supportedFocusDrivenTypes_.end();
83 }
84
GetZoomRatioRange()85 std::vector<float> CameraAbility::GetZoomRatioRange()
86 {
87 return zoomRatioRange_.value_or(std::vector<float>{1.0f, 1.0f});
88 }
89
GetSupportedBeautyTypes()90 std::vector<BeautyType> CameraAbility::GetSupportedBeautyTypes()
91 {
92 return supportedBeautyTypes_;
93 }
94
GetSupportedBeautyRange(BeautyType beautyType)95 std::vector<int32_t> CameraAbility::GetSupportedBeautyRange(BeautyType beautyType)
96 {
97 auto it = supportedBeautyRangeMap_.find(beautyType);
98 if (it != supportedBeautyRangeMap_.end()) {
99 return it->second;
100 }
101 return {};
102 }
103
GetSupportedColorEffects()104 std::vector<ColorEffect> CameraAbility::GetSupportedColorEffects()
105 {
106 return supportedColorEffects_;
107 }
108
GetSupportedColorSpaces()109 std::vector<ColorSpace> CameraAbility::GetSupportedColorSpaces()
110 {
111 return supportedColorSpaces_;
112 }
113
IsMacroSupported()114 bool CameraAbility::IsMacroSupported()
115 {
116 return isMacroSupported_.value_or(false);
117 }
118
IsDepthFusionSupported()119 bool CameraAbility::IsDepthFusionSupported()
120 {
121 return isDepthFusionSupported_.value_or(false);
122 }
123
GetDepthFusionThreshold()124 std::vector<float> CameraAbility::GetDepthFusionThreshold()
125 {
126 return getDepthFusionThreshold_.value_or(std::vector<float>{1.0f, 1.0f});
127 }
128
GetSupportedPortraitEffects()129 std::vector<PortraitEffect> CameraAbility::GetSupportedPortraitEffects()
130 {
131 return supportedPortraitEffects_;
132 }
133
GetSupportedVirtualApertures()134 std::vector<float> CameraAbility::GetSupportedVirtualApertures()
135 {
136 return supportedVirtualApertures_;
137 }
138
GetSupportedPhysicalApertures()139 std::vector<std::vector<float>> CameraAbility::GetSupportedPhysicalApertures()
140 {
141 return supportedPhysicalApertures_;
142 }
143
GetSupportedStabilizationMode()144 std::vector<VideoStabilizationMode> CameraAbility::GetSupportedStabilizationMode()
145 {
146 return supportedVideoStabilizationMode_;
147 }
148
IsVideoStabilizationModeSupported(VideoStabilizationMode stabilizationMode)149 bool CameraAbility::IsVideoStabilizationModeSupported(VideoStabilizationMode stabilizationMode)
150 {
151 auto it = std::find(
152 supportedVideoStabilizationMode_.begin(), supportedVideoStabilizationMode_.end(), stabilizationMode);
153 return it != supportedVideoStabilizationMode_.end();
154 }
155
GetSupportedExposureRange()156 std::vector<uint32_t> CameraAbility::GetSupportedExposureRange()
157 {
158 return supportedExposureRange_;
159 }
160
IsFeatureSupported(SceneFeature sceneFeature)161 bool CameraAbility::IsFeatureSupported(SceneFeature sceneFeature)
162 {
163 return std::find(
164 supportedSceneFeature_.begin(), supportedSceneFeature_.end(), sceneFeature) != supportedSceneFeature_.end();
165 }
166
IsLcdFlashSupported()167 bool CameraAbility::IsLcdFlashSupported()
168 {
169 return isLcdFlashSupported_;
170 }
171
DumpCameraAbilityInfo()172 void CameraAbility::DumpCameraAbilityInfo()
173 {
174 auto logFunc = [](const std::string& label, const auto& container) {
175 std::string vecStr = Container2String(container.begin(), container.end());
176 MEDIA_DEBUG_LOG("%{public}s: %{public}s", label.c_str(), vecStr.c_str());
177 };
178
179 logFunc("Supported Flash Modes", supportedFlashModes_);
180 logFunc("Supported Exposure Modes", supportedExposureModes_);
181 logFunc("Exposure Bias Range", exposureBiasRange_);
182 logFunc("Supported Focus Modes", supportedFocusModes_);
183 logFunc("Supported Color Effects", supportedColorEffects_);
184 logFunc("Supported Color Spaces", supportedColorSpaces_);
185 logFunc("Supported Portrait Effects", supportedPortraitEffects_);
186 logFunc("Supported Video Stabilization Modes", supportedVideoStabilizationMode_);
187 logFunc("Supported Beauty Types", supportedBeautyTypes_);
188 logFunc("Supported Exposure Range", supportedExposureRange_);
189 logFunc("Supported Scene Feature", supportedSceneFeature_);
190
191 for (const auto& [type, range] : supportedBeautyRangeMap_) {
192 std::string vecStr = Container2String(range.begin(), range.end());
193 MEDIA_DEBUG_LOG("Beauty Types: %{public}d Supported Beauty Ranges: %{public}s", type, vecStr.c_str());
194 }
195
196 logFunc("Supported Virtual Apertures", supportedVirtualApertures_);
197
198 for (const auto& apertureList : supportedPhysicalApertures_) {
199 logFunc("Supported Physical Apertures", apertureList);
200 }
201
202 if (zoomRatioRange_.has_value()) {
203 logFunc("Zoom Ratio Range", zoomRatioRange_.value());
204 } else {
205 MEDIA_DEBUG_LOG("Zoom Ratio Range: Not supported");
206 }
207
208 if (isMacroSupported_.has_value()) {
209 MEDIA_DEBUG_LOG("Macro Supported: %{public}d", isMacroSupported_.value());
210 } else {
211 MEDIA_DEBUG_LOG("Macro Supported: Not supported");
212 }
213
214 if (isDepthFusionSupported_.has_value()) {
215 MEDIA_DEBUG_LOG("Depth Fusion Supported: %{public}d", isDepthFusionSupported_.value());
216 } else {
217 MEDIA_DEBUG_LOG("Depth Fusion Supported: Not supported");
218 }
219
220 if (getDepthFusionThreshold_.has_value()) {
221 logFunc("Depth Fusion Threshold", getDepthFusionThreshold_.value());
222 } else {
223 MEDIA_DEBUG_LOG("Depth Fusion Threshold: Not supported");
224 }
225 }
226
~CameraAbilityContainer()227 CameraAbilityContainer::~CameraAbilityContainer()
228 {
229 conflictAbilities_.clear();
230 }
231
OnAbilityChange()232 void CameraAbilityContainer::OnAbilityChange()
233 {
234 auto session = session_.promote();
235 if (session != nullptr) {
236 MEDIA_DEBUG_LOG("CameraAbilityContainer OnAbilityChange");
237 session->ExecuteAbilityChangeCallback();
238 }
239 }
240
CameraAbilityContainer(std::vector<sptr<CameraAbility>> abilities,std::vector<sptr<CameraAbility>> conflictAbilities,wptr<CaptureSession> session)241 CameraAbilityContainer::CameraAbilityContainer(std::vector<sptr<CameraAbility>> abilities,
242 std::vector<sptr<CameraAbility>> conflictAbilities, wptr<CaptureSession> session)
243 : session_(session), conflictAbilities_(conflictAbilities)
244 {
245 PrepareSpecMaximumValue(abilities);
246 }
247
PrepareSpecMaximumValue(std::vector<sptr<CameraAbility>> & abilities)248 void CameraAbilityContainer::PrepareSpecMaximumValue(std::vector<sptr<CameraAbility>>& abilities)
249 {
250 std::optional<std::vector<float>> tempRange;
251
252 for (const auto& ability : abilities) {
253 if (!ability) {
254 continue;
255 }
256
257 if (!IsMacroSupportedInSpec_) {
258 IsMacroSupportedInSpec_ = ability->IsMacroSupported();
259 }
260
261 auto range = ability->GetZoomRatioRange();
262 if (!g_isValidZoomRaioRange(range)) {
263 continue;
264 }
265 if (!tempRange.has_value()) {
266 tempRange = range;
267 } else {
268 float min = std::min(tempRange.value()[0], range[0]);
269 float max = std::max(tempRange.value()[1], range[1]);
270 tempRange = std::vector<float>{min, max};
271 }
272 }
273
274 zoomRatioRangeInSpec_ = tempRange.value_or(std::vector<float>{1.0, 1.0});
275 abilities.clear();
276 }
277
FilterByZoomRatio(float zoomRatio)278 void CameraAbilityContainer::FilterByZoomRatio(float zoomRatio)
279 {
280 zoomRatioSet_ = zoomRatio;
281 if (lastIsMacroSupported_.has_value()) {
282 bool oldValue = lastIsMacroSupported_.value();
283 bool newValue = IsMacroSupported();
284 MEDIA_INFO_LOG("CameraAbilityContainer macroValue %{public}d", static_cast<int32_t>(newValue));
285 if (oldValue != newValue) {
286 OnAbilityChange();
287 }
288 }
289 }
290
FilterByMacro(bool enableMacro)291 void CameraAbilityContainer::FilterByMacro(bool enableMacro)
292 {
293 enableMacroSet_ = enableMacro;
294 if (lastGetZoomRatioRange_.has_value()) {
295 std::vector<float> oldValue = lastGetZoomRatioRange_.value();
296 std::vector<float> newValue = GetZoomRatioRange();
297 MEDIA_INFO_LOG("CameraAbilityContainer zoomValue %{public}f %{public}f", newValue[0], newValue[1]);
298 if (oldValue[0] != newValue[0] || oldValue[1] != newValue[1]) {
299 OnAbilityChange();
300 }
301 }
302 }
303
GetZoomRatioRange()304 std::vector<float> CameraAbilityContainer::GetZoomRatioRange()
305 {
306 bool enableMacro = enableMacroSet_.value_or(false);
307 if (!g_isValidZoomRaioRange(zoomRatioRangeInSpec_)) {
308 MEDIA_ERR_LOG("zoomRatioRangeInSpec_ invalid data");
309 return std::vector<float>{1.0f, 1.0f};
310 }
311 for (const auto& ability : conflictAbilities_) {
312 if (ability == nullptr) {
313 continue;
314 }
315 std::vector<float> range = ability->GetZoomRatioRange();
316 if (enableMacro == ability->IsMacroSupported() && g_isValidZoomRaioRange(range)) {
317 float min = std::max(zoomRatioRangeInSpec_[0], range[0]);
318 float max = std::min(zoomRatioRangeInSpec_[1], range[1]);
319 lastGetZoomRatioRange_ = {min, max};
320 return {min, max};
321 }
322 }
323 lastGetZoomRatioRange_ = zoomRatioRangeInSpec_;
324 return zoomRatioRangeInSpec_;
325 }
326
IsMacroSupported()327 bool CameraAbilityContainer::IsMacroSupported()
328 {
329 float zoomRatio = zoomRatioSet_.value_or(1.0f);
330 for (const auto& ability : conflictAbilities_) {
331 if (ability == nullptr) {
332 continue;
333 }
334 std::vector<float> range = ability->GetZoomRatioRange();
335 if (g_isValidZoomRaioRange(range) && ability->IsMacroSupported()) {
336 bool isSupport = IsMacroSupportedInSpec_ && (range[0] <= zoomRatio && zoomRatio <= range[1]);
337 lastIsMacroSupported_ = isSupport;
338 return isSupport;
339 }
340 }
341 lastIsMacroSupported_ = IsMacroSupportedInSpec_;
342 return IsMacroSupportedInSpec_;
343 }
344 } // namespace CameraStandard
345 } // namespace OHOS