• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
73 // LCOV_EXCL_START
IsFocusRangeTypeSupported(FocusRangeType focusRangeType)74 bool CameraAbility::IsFocusRangeTypeSupported(FocusRangeType focusRangeType)
75 {
76     return std::find(supportedFocusRangeTypes_.begin(), supportedFocusRangeTypes_.end(), focusRangeType) !=
77         supportedFocusRangeTypes_.end();
78 }
79 
IsFocusDrivenTypeSupported(FocusDrivenType focusDrivenType)80 bool CameraAbility::IsFocusDrivenTypeSupported(FocusDrivenType focusDrivenType)
81 {
82     return std::find(supportedFocusDrivenTypes_.begin(), supportedFocusDrivenTypes_.end(), focusDrivenType) !=
83         supportedFocusDrivenTypes_.end();
84 }
85 // LCOV_EXCL_STOP
86 
GetZoomRatioRange()87 std::vector<float> CameraAbility::GetZoomRatioRange()
88 {
89     return zoomRatioRange_.value_or(std::vector<float>{1.0f, 1.0f});
90 }
91 
GetSupportedBeautyTypes()92 std::vector<BeautyType> CameraAbility::GetSupportedBeautyTypes()
93 {
94     return supportedBeautyTypes_;
95 }
96 
GetSupportedBeautyRange(BeautyType beautyType)97 std::vector<int32_t> CameraAbility::GetSupportedBeautyRange(BeautyType beautyType)
98 {
99     auto it = supportedBeautyRangeMap_.find(beautyType);
100     CHECK_ERROR_RETURN_RET(it != supportedBeautyRangeMap_.end(), it->second);
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 
119 // LCOV_EXCL_START
IsDepthFusionSupported()120 bool CameraAbility::IsDepthFusionSupported()
121 {
122     return isDepthFusionSupported_.value_or(false);
123 }
124 
GetDepthFusionThreshold()125 std::vector<float> CameraAbility::GetDepthFusionThreshold()
126 {
127     return getDepthFusionThreshold_.value_or(std::vector<float>{1.0f, 1.0f});
128 }
129 // LCOV_EXCL_STOP
130 
GetSupportedPortraitEffects()131 std::vector<PortraitEffect> CameraAbility::GetSupportedPortraitEffects()
132 {
133     return supportedPortraitEffects_;
134 }
135 
GetSupportedVirtualApertures()136 std::vector<float> CameraAbility::GetSupportedVirtualApertures()
137 {
138     return supportedVirtualApertures_;
139 }
140 
GetSupportedPhysicalApertures()141 std::vector<std::vector<float>> CameraAbility::GetSupportedPhysicalApertures()
142 {
143     return supportedPhysicalApertures_;
144 }
145 
GetSupportedStabilizationMode()146 std::vector<VideoStabilizationMode> CameraAbility::GetSupportedStabilizationMode()
147 {
148     return supportedVideoStabilizationMode_;
149 }
150 
IsVideoStabilizationModeSupported(VideoStabilizationMode stabilizationMode)151 bool CameraAbility::IsVideoStabilizationModeSupported(VideoStabilizationMode stabilizationMode)
152 {
153     auto it = std::find(
154         supportedVideoStabilizationMode_.begin(), supportedVideoStabilizationMode_.end(), stabilizationMode);
155     return it != supportedVideoStabilizationMode_.end();
156 }
157 
GetSupportedExposureRange()158 std::vector<uint32_t> CameraAbility::GetSupportedExposureRange()
159 {
160     return supportedExposureRange_;
161 }
162 
IsFeatureSupported(SceneFeature sceneFeature)163 bool CameraAbility::IsFeatureSupported(SceneFeature sceneFeature)
164 {
165     return std::find(
166         supportedSceneFeature_.begin(), supportedSceneFeature_.end(), sceneFeature) != supportedSceneFeature_.end();
167 }
168 
IsLcdFlashSupported()169 bool CameraAbility::IsLcdFlashSupported()
170 {
171     return isLcdFlashSupported_;
172 }
173 
DumpCameraAbilityInfo()174 void CameraAbility::DumpCameraAbilityInfo()
175 {
176     auto logFunc = [](const std::string& label, const auto& container) {
177         std::string vecStr = Container2String(container.begin(), container.end());
178         MEDIA_DEBUG_LOG("%{public}s: %{public}s", label.c_str(), vecStr.c_str());
179     };
180 
181     logFunc("Supported Flash Modes", supportedFlashModes_);
182     logFunc("Supported Exposure Modes", supportedExposureModes_);
183     logFunc("Exposure Bias Range", exposureBiasRange_);
184     logFunc("Supported Focus Modes", supportedFocusModes_);
185     logFunc("Supported Color Effects", supportedColorEffects_);
186     logFunc("Supported Color Spaces", supportedColorSpaces_);
187     logFunc("Supported Portrait Effects", supportedPortraitEffects_);
188     logFunc("Supported Video Stabilization Modes", supportedVideoStabilizationMode_);
189     logFunc("Supported Beauty Types", supportedBeautyTypes_);
190     logFunc("Supported Exposure Range", supportedExposureRange_);
191     logFunc("Supported Scene Feature", supportedSceneFeature_);
192 
193     for (const auto& [type, range] : supportedBeautyRangeMap_) {
194         std::string vecStr = Container2String(range.begin(), range.end());
195         MEDIA_DEBUG_LOG("Beauty Types: %{public}d Supported Beauty Ranges: %{public}s", type, vecStr.c_str());
196     }
197 
198     logFunc("Supported Virtual Apertures", supportedVirtualApertures_);
199 
200     for (const auto& apertureList : supportedPhysicalApertures_) {
201         logFunc("Supported Physical Apertures", apertureList);
202     }
203 
204     if (zoomRatioRange_.has_value()) {
205         logFunc("Zoom Ratio Range", zoomRatioRange_.value());
206     } else {
207         MEDIA_DEBUG_LOG("Zoom Ratio Range: Not supported");
208     }
209 
210     if (isMacroSupported_.has_value()) {
211         MEDIA_DEBUG_LOG("Macro Supported: %{public}d", isMacroSupported_.value());
212     } else {
213         MEDIA_DEBUG_LOG("Macro Supported: Not supported");
214     }
215 
216     if (isDepthFusionSupported_.has_value()) {
217         MEDIA_DEBUG_LOG("Depth Fusion Supported: %{public}d", isDepthFusionSupported_.value());
218     } else {
219         MEDIA_DEBUG_LOG("Depth Fusion Supported: Not supported");
220     }
221 
222     if (getDepthFusionThreshold_.has_value()) {
223         logFunc("Depth Fusion Threshold", getDepthFusionThreshold_.value());
224     } else {
225         MEDIA_DEBUG_LOG("Depth Fusion Threshold: Not supported");
226     }
227 }
228 
~CameraAbilityContainer()229 CameraAbilityContainer::~CameraAbilityContainer()
230 {
231     conflictAbilities_.clear();
232 }
233 
234 // LCOV_EXCL_START
OnAbilityChange()235 void CameraAbilityContainer::OnAbilityChange()
236 {
237     auto session = session_.promote();
238     if (session != nullptr) {
239         MEDIA_DEBUG_LOG("CameraAbilityContainer OnAbilityChange");
240         session->ExecuteAbilityChangeCallback();
241     }
242 }
243 // LCOV_EXCL_STOP
244 
CameraAbilityContainer(std::vector<sptr<CameraAbility>> abilities,std::vector<sptr<CameraAbility>> conflictAbilities,wptr<CaptureSession> session)245 CameraAbilityContainer::CameraAbilityContainer(std::vector<sptr<CameraAbility>> abilities,
246     std::vector<sptr<CameraAbility>> conflictAbilities, wptr<CaptureSession> session)
247     : session_(session), conflictAbilities_(conflictAbilities)
248 {
249     PrepareSpecMaximumValue(abilities);
250 }
251 
PrepareSpecMaximumValue(std::vector<sptr<CameraAbility>> & abilities)252 void CameraAbilityContainer::PrepareSpecMaximumValue(std::vector<sptr<CameraAbility>>& abilities)
253 {
254     std::optional<std::vector<float>> tempRange;
255 
256     for (const auto& ability : abilities) {
257         if (!ability) {
258             continue;
259         }
260 
261         if (!IsMacroSupportedInSpec_) {
262             IsMacroSupportedInSpec_ = ability->IsMacroSupported();
263         }
264 
265         auto range = ability->GetZoomRatioRange();
266         if (!g_isValidZoomRaioRange(range)) {
267             continue;
268         }
269         if (!tempRange.has_value()) {
270             tempRange = range;
271         } else {
272             // LCOV_EXCL_START
273             float min = std::min(tempRange.value()[0], range[0]);
274             float max = std::max(tempRange.value()[1], range[1]);
275             tempRange = std::vector<float>{min, max};
276             // LCOV_EXCL_STOP
277         }
278     }
279 
280     zoomRatioRangeInSpec_ = tempRange.value_or(std::vector<float>{1.0, 1.0});
281     abilities.clear();
282 }
283 
FilterByZoomRatio(float zoomRatio)284 void CameraAbilityContainer::FilterByZoomRatio(float zoomRatio)
285 {
286     zoomRatioSet_ = zoomRatio;
287     if (lastIsMacroSupported_.has_value()) {
288         // LCOV_EXCL_START
289         bool oldValue = lastIsMacroSupported_.value();
290         bool newValue = IsMacroSupported();
291         MEDIA_INFO_LOG("CameraAbilityContainer macroValue %{public}d", static_cast<int32_t>(newValue));
292         CHECK_EXECUTE(oldValue != newValue, OnAbilityChange());
293         // LCOV_EXCL_STOP
294     }
295 }
296 
297 // LCOV_EXCL_START
FilterByMacro(bool enableMacro)298 void CameraAbilityContainer::FilterByMacro(bool enableMacro)
299 {
300     enableMacroSet_ = enableMacro;
301     if (lastGetZoomRatioRange_.has_value()) {
302         std::vector<float> oldValue = lastGetZoomRatioRange_.value();
303         std::vector<float> newValue = GetZoomRatioRange();
304         MEDIA_INFO_LOG("CameraAbilityContainer zoomValue %{public}f %{public}f", newValue[0], newValue[1]);
305         CHECK_EXECUTE(oldValue[0] != newValue[0] || oldValue[1] != newValue[1], OnAbilityChange());
306     }
307 }
308 // LCOV_EXCL_STOP
309 
GetZoomRatioRange()310 std::vector<float> CameraAbilityContainer::GetZoomRatioRange()
311 {
312     bool enableMacro = enableMacroSet_.value_or(false);
313     if (!g_isValidZoomRaioRange(zoomRatioRangeInSpec_)) {
314         MEDIA_ERR_LOG("zoomRatioRangeInSpec_ invalid data");
315         return std::vector<float>{1.0f, 1.0f};
316     }
317     for (const auto& ability : conflictAbilities_) {
318         if (ability == nullptr) {
319             continue;
320         }
321         std::vector<float> range = ability->GetZoomRatioRange();
322         if (enableMacro == ability->IsMacroSupported() && g_isValidZoomRaioRange(range)) {
323             float min = std::max(zoomRatioRangeInSpec_[0], range[0]);
324             float max = std::min(zoomRatioRangeInSpec_[1], range[1]);
325             lastGetZoomRatioRange_ = {min, max};
326             return {min, max};
327         }
328     }
329     lastGetZoomRatioRange_ = zoomRatioRangeInSpec_;
330     return zoomRatioRangeInSpec_;
331 }
332 
IsMacroSupported()333 bool CameraAbilityContainer::IsMacroSupported()
334 {
335     float zoomRatio = zoomRatioSet_.value_or(1.0f);
336     for (const auto& ability : conflictAbilities_) {
337         if (ability == nullptr) {
338             continue;
339         }
340         std::vector<float> range = ability->GetZoomRatioRange();
341         if (g_isValidZoomRaioRange(range) && ability->IsMacroSupported()) {
342             bool isSupport = IsMacroSupportedInSpec_ && (range[0] <= zoomRatio && zoomRatio <= range[1]);
343             lastIsMacroSupported_ = isSupport;
344             return isSupport;
345         }
346     }
347     lastIsMacroSupported_ = IsMacroSupportedInSpec_;
348     return IsMacroSupportedInSpec_;
349 }
350 } // namespace CameraStandard
351 } // namespace OHOS