1 /*
2 * Copyright (c) 2025 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
16 #include "effect/rs_render_filter_base.h"
17
18 #include <unordered_map>
19
20 #include "ge_visual_effect.h"
21 #include "ge_visual_effect_container.h"
22
23 #include "effect/rs_render_mask_base.h"
24 #include "platform/common/rs_log.h"
25
26 namespace OHOS {
27 namespace Rosen {
28 using MaskCreator = std::function<std::shared_ptr<RSNGRenderMaskBase>()>;
29
30 static std::unordered_map<RSNGEffectType, MaskCreator> creatorLUT = {
__anon1c0a36440102null31 {RSNGEffectType::RIPPLE_MASK, [] {
32 return std::make_shared<RSNGRenderRippleMask>();
33 }
34 },
__anon1c0a36440202null35 {RSNGEffectType::DOUBLE_RIPPLE_MASK, [] {
36 return std::make_shared<RSNGRenderDoubleRippleMask>();
37 }
38 },
__anon1c0a36440302null39 {RSNGEffectType::PIXEL_MAP_MASK, [] {
40 return std::make_shared<RSNGRenderPixelMapMask>();
41 }
42 },
__anon1c0a36440402null43 {RSNGEffectType::RADIAL_GRADIENT_MASK, [] {
44 return std::make_shared<RSNGRenderRadialGradientMask>();
45 }
46 },
__anon1c0a36440502null47 {RSNGEffectType::WAVE_GRADIENT_MASK, [] {
48 return std::make_shared<RSNGRenderWaveGradientMask>();
49 }
50 },
51 };
52
Create(RSNGEffectType type)53 std::shared_ptr<RSNGRenderMaskBase> RSNGRenderMaskBase::Create(RSNGEffectType type)
54 {
55 auto it = creatorLUT.find(type);
56 return it != creatorLUT.end() ? it->second() : nullptr;
57 }
58
Unmarshalling(Parcel & parcel,std::shared_ptr<RSNGRenderMaskBase> & val)59 [[nodiscard]] bool RSNGRenderMaskBase::Unmarshalling(Parcel& parcel, std::shared_ptr<RSNGRenderMaskBase>& val)
60 {
61 std::shared_ptr<RSNGRenderMaskBase> head = nullptr;
62 auto current = head;
63 for (size_t effectCount = 0; effectCount <= EFFECT_COUNT_LIMIT; ++effectCount) {
64 RSNGEffectTypeUnderlying type = 0;
65 if (!RSMarshallingHelper::Unmarshalling(parcel, type)) {
66 ROSEN_LOGE("RSNGRenderMaskBase: Unmarshalling type failed");
67 return false;
68 }
69
70 if (type == END_OF_CHAIN) {
71 val = head;
72 return true;
73 }
74
75 auto mask = Create(static_cast<RSNGEffectType>(type));
76 if (mask && !mask->OnUnmarshalling(parcel)) {
77 ROSEN_LOGE("RSNGRenderMaskBase: Unmarshalling mask failed with type %{public}d", type);
78 return false;
79 }
80 if (!current) {
81 head = mask; // init head
82 } else {
83 current->nextEffect_ = mask;
84 }
85 current = mask;
86 }
87
88 ROSEN_LOGE("RSNGRenderMaskBase: UnMarshalling mask count arrive limit(%{public}zu)",
89 EFFECT_COUNT_LIMIT);
90 return false;
91 }
92 } // namespace Rosen
93 } // namespace OHOS
94