1 /*
2  * Copyright (c) 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 
16 #include "ge_visual_effect_impl.h"
17 
18 #include <unordered_map>
19 
20 #include "ge_log.h"
21 
22 namespace OHOS {
23 namespace Rosen {
24 namespace Drawing {
25 
26 std::map<const std::string, std::function<void(GEVisualEffectImpl*)>> GEVisualEffectImpl::g_initialMap = {
27     { GE_FILTER_KAWASE_BLUR,
__anon7cca34240102() 28         [](GEVisualEffectImpl* impl) {
29             impl->SetFilterType(GEVisualEffectImpl::FilterType::KAWASE_BLUR);
30             impl->MakeKawaseParams();
31         }
32     },
33     { GE_FILTER_MESA_BLUR,
__anon7cca34240202() 34         [](GEVisualEffectImpl* impl) {
35             impl->SetFilterType(GEVisualEffectImpl::FilterType::MESA_BLUR);
36             impl->MakeMESAParams();
37         }
38     },
39     { GE_FILTER_GREY,
__anon7cca34240302() 40         [](GEVisualEffectImpl* impl) {
41             impl->SetFilterType(GEVisualEffectImpl::FilterType::GREY);
42             impl->MakeGreyParams();
43         }
44     },
45     { GE_FILTER_AI_BAR,
__anon7cca34240402() 46         [](GEVisualEffectImpl* impl) {
47             impl->SetFilterType(GEVisualEffectImpl::FilterType::AIBAR);
48             impl->MakeAIBarParams();
49         }
50     },
51     { GE_FILTER_LINEAR_GRADIENT_BLUR,
__anon7cca34240502() 52         [](GEVisualEffectImpl* impl) {
53             impl->SetFilterType(GEVisualEffectImpl::FilterType::LINEAR_GRADIENT_BLUR);
54             impl->MakeLinearGradientBlurParams();
55         }
56     },
57     { GE_FILTER_MAGNIFIER,
__anon7cca34240602() 58         [](GEVisualEffectImpl* impl) {
59             impl->SetFilterType(GEVisualEffectImpl::FilterType::MAGNIFIER);
60             impl->MakeMagnifierParams();
61         }
62     },
63     { GE_FILTER_WATER_RIPPLE,
__anon7cca34240702() 64         [](GEVisualEffectImpl* impl) {
65             impl->SetFilterType(GEVisualEffectImpl::FilterType::WATER_RIPPLE);
66             impl->MakeWaterRippleParams();
67         }
68     }
69 };
70 
GEVisualEffectImpl(const std::string & name)71 GEVisualEffectImpl::GEVisualEffectImpl(const std::string& name)
72 {
73     auto iter = g_initialMap.find(name);
74     if (iter != g_initialMap.end()) {
75         iter->second(this);
76     }
77 }
78 
~GEVisualEffectImpl()79 GEVisualEffectImpl::~GEVisualEffectImpl() {}
80 
SetParam(const std::string & tag,int32_t param)81 void GEVisualEffectImpl::SetParam(const std::string& tag, int32_t param)
82 {
83     switch (filterType_) {
84         case FilterType::KAWASE_BLUR: {
85             if (kawaseParams_ == nullptr) {
86                 return;
87             }
88 
89             if (tag == GE_FILTER_KAWASE_BLUR_RADIUS) {
90                 kawaseParams_->radius = param;
91             }
92             break;
93         }
94         case FilterType::MESA_BLUR: {
95             if (mesaParams_ == nullptr) {
96                 return;
97             }
98 
99             if (tag == GE_FILTER_MESA_BLUR_RADIUS) {
100                 mesaParams_->radius = param;
101             }
102             if (tag == GE_FILTER_MESA_BLUR_STRETCH_TILE_MODE) {
103                 mesaParams_->tileMode = param;
104             }
105             break;
106         }
107         case FilterType::LINEAR_GRADIENT_BLUR: {
108             if (linearGradientBlurParams_ == nullptr) {
109                 return;
110             }
111 
112             if (tag == GE_FILTER_LINEAR_GRADIENT_BLUR_DIRECTION) {
113                 linearGradientBlurParams_->direction = param;
114             }
115             break;
116         }
117         case FilterType::MAGNIFIER: {
118             if (magnifierParams_ == nullptr) {
119                 return;
120             }
121 
122             if (tag == GE_FILTER_MAGNIFIER_ROTATE_DEGREE) {
123                 magnifierParams_->rotateDegree = param;
124             }
125             break;
126         }
127         default:
128             break;
129     }
130 }
131 
SetParam(const std::string & tag,bool param)132 void GEVisualEffectImpl::SetParam(const std::string& tag, bool param)
133 {
134     switch (filterType_) {
135         case FilterType::LINEAR_GRADIENT_BLUR: {
136             if (linearGradientBlurParams_ == nullptr) {
137                 return;
138             }
139 
140             if (tag == GE_FILTER_LINEAR_GRADIENT_BLUR_IS_OFF_SCREEN) {
141                 linearGradientBlurParams_->isOffscreenCanvas = param;
142             }
143             break;
144         }
145         default:
146             break;
147     }
148 }
149 
SetParam(const std::string & tag,int64_t param)150 void GEVisualEffectImpl::SetParam(const std::string& tag, int64_t param) {}
151 
SetParam(const std::string & tag,float param)152 void GEVisualEffectImpl::SetParam(const std::string& tag, float param)
153 {
154     switch (filterType_) {
155         case FilterType::MESA_BLUR: {
156             SetMESABlurParams(tag, param);
157             break;
158         }
159         case FilterType::AIBAR: {
160             SetAIBarParams(tag, param);
161             break;
162         }
163         case FilterType::GREY: {
164             SetGreyParams(tag, param);
165             break;
166         }
167 
168         case FilterType::LINEAR_GRADIENT_BLUR: {
169             SetLinearGradientBlurParams(tag, param);
170             break;
171         }
172         case FilterType::MAGNIFIER: {
173             SetMagnifierParamsFloat(tag, param);
174             break;
175         }
176         case FilterType::WATER_RIPPLE: {
177             SetWaterRippleParams(tag, param);
178             break;
179         }
180         default:
181             break;
182     }
183 }
184 
SetParam(const std::string & tag,double param)185 void GEVisualEffectImpl::SetParam(const std::string& tag, double param) {}
186 
SetParam(const std::string & tag,const char * const param)187 void GEVisualEffectImpl::SetParam(const std::string& tag, const char* const param) {}
188 
SetParam(const std::string & tag,const std::shared_ptr<Drawing::Image> param)189 void GEVisualEffectImpl::SetParam(const std::string& tag, const std::shared_ptr<Drawing::Image> param) {}
190 
SetParam(const std::string & tag,const std::shared_ptr<Drawing::ColorFilter> param)191 void GEVisualEffectImpl::SetParam(const std::string& tag, const std::shared_ptr<Drawing::ColorFilter> param) {}
192 
SetParam(const std::string & tag,const Drawing::Matrix param)193 void GEVisualEffectImpl::SetParam(const std::string& tag, const Drawing::Matrix param)
194 {
195     switch (filterType_) {
196         case FilterType::LINEAR_GRADIENT_BLUR: {
197             if (linearGradientBlurParams_ == nullptr) {
198                 return;
199             }
200 
201             if (tag == GE_FILTER_LINEAR_GRADIENT_BLUR_CANVAS_MAT) {
202                 linearGradientBlurParams_->mat = param;
203             }
204             break;
205         }
206         default:
207             break;
208     }
209 }
210 
SetParam(const std::string & tag,const std::vector<std::pair<float,float>> param)211 void GEVisualEffectImpl::SetParam(const std::string& tag, const std::vector<std::pair<float, float>> param)
212 {
213     switch (filterType_) {
214         case FilterType::LINEAR_GRADIENT_BLUR: {
215             if (linearGradientBlurParams_ == nullptr) {
216                 return;
217             }
218             if (tag == GE_FILTER_LINEAR_GRADIENT_BLUR_FRACTION_STOPS) {
219                 linearGradientBlurParams_->fractionStops = param;
220             }
221             break;
222         }
223         default:
224             break;
225     }
226 }
227 
SetParam(const std::string & tag,const uint32_t param)228 void GEVisualEffectImpl::SetParam(const std::string& tag, const uint32_t param)
229 {
230     switch (filterType_) {
231         case FilterType::MAGNIFIER: {
232             SetMagnifierParamsUint32(tag, param);
233             break;
234         }
235         case FilterType::WATER_RIPPLE: {
236             if (waterRippleParams_ == nullptr) {
237                 return;
238             }
239             if (tag == GE_FILTER_WATER_RIPPLE_RIPPLE_MODE) {
240                 waterRippleParams_->rippleMode = param;
241             } else if (tag == GE_FILTER_WATER_RIPPLE_WAVE_NUM) {
242                 waterRippleParams_->waveCount = param;
243             }
244             break;
245         }
246         default:
247             break;
248     }
249 }
250 
SetMESABlurParams(const std::string & tag,float param)251 void GEVisualEffectImpl::SetMESABlurParams(const std::string& tag, float param)
252 {
253     if (mesaParams_ == nullptr) {
254         return;
255     }
256 
257     static std::unordered_map<std::string, std::function<void(GEVisualEffectImpl*, float)>> actions = {
258         { GE_FILTER_MESA_BLUR_GREY_COEF_1,
259             [](GEVisualEffectImpl* obj, float p) { obj->mesaParams_->greyCoef1 = p; } },
260         { GE_FILTER_MESA_BLUR_GREY_COEF_2,
261             [](GEVisualEffectImpl* obj, float p) { obj->mesaParams_->greyCoef2 = p; } },
262         { GE_FILTER_MESA_BLUR_STRETCH_OFFSET_X,
263             [](GEVisualEffectImpl* obj, float p) { obj->mesaParams_->offsetX = p; } },
264         { GE_FILTER_MESA_BLUR_STRETCH_OFFSET_Y,
265             [](GEVisualEffectImpl* obj, float p) { obj->mesaParams_->offsetY = p; } },
266         { GE_FILTER_MESA_BLUR_STRETCH_OFFSET_Z,
267             [](GEVisualEffectImpl* obj, float p) { obj->mesaParams_->offsetZ = p; } },
268         { GE_FILTER_MESA_BLUR_STRETCH_OFFSET_W,
269             [](GEVisualEffectImpl* obj, float p) { obj->mesaParams_->offsetW = p; } },
270         { GE_FILTER_MESA_BLUR_STRETCH_WIDTH,
271             [](GEVisualEffectImpl* obj, float p) { obj->mesaParams_->width = p; } },
272         { GE_FILTER_MESA_BLUR_STRETCH_HEIGHT,
273             [](GEVisualEffectImpl* obj, float p) { obj->mesaParams_->height = p; } }
274     };
275 
276     auto it = actions.find(tag);
277     if (it != actions.end()) {
278         it->second(this, param);
279     }
280 }
281 
SetAIBarParams(const std::string & tag,float param)282 void GEVisualEffectImpl::SetAIBarParams(const std::string& tag, float param)
283 {
284     if (aiBarParams_ == nullptr) {
285         return;
286     }
287 
288     static std::unordered_map<std::string, std::function<void(GEVisualEffectImpl*, float)>> actions = {
289         { GE_FILTER_AI_BAR_LOW,
290             [](GEVisualEffectImpl* obj, float p) { obj->aiBarParams_->aiBarLow        = p; } },
291         { GE_FILTER_AI_BAR_HIGH,
292             [](GEVisualEffectImpl* obj, float p) { obj->aiBarParams_->aiBarHigh       = p; } },
293         { GE_FILTER_AI_BAR_THRESHOLD,
294             [](GEVisualEffectImpl* obj, float p) { obj->aiBarParams_->aiBarThreshold  = p; } },
295         { GE_FILTER_AI_BAR_OPACITY,
296             [](GEVisualEffectImpl* obj, float p) { obj->aiBarParams_->aiBarOpacity    = p; } },
297         { GE_FILTER_AI_BAR_SATURATION,
298             [](GEVisualEffectImpl* obj, float p) { obj->aiBarParams_->aiBarSaturation = p; } }
299     };
300 
301     auto it = actions.find(tag);
302     if (it != actions.end()) {
303         it->second(this, param);
304     }
305 }
306 
SetGreyParams(const std::string & tag,float param)307 void GEVisualEffectImpl::SetGreyParams(const std::string& tag, float param)
308 {
309     if (greyParams_ == nullptr) {
310         return;
311     }
312 
313     static std::unordered_map<std::string, std::function<void(GEVisualEffectImpl*, float)>> actions = {
314         { GE_FILTER_GREY_COEF_1, [](GEVisualEffectImpl* obj, float p) { obj->greyParams_->greyCoef1 = p; } },
315         { GE_FILTER_GREY_COEF_2, [](GEVisualEffectImpl* obj, float p) { obj->greyParams_->greyCoef2 = p; } }
316     };
317 
318     auto it = actions.find(tag);
319     if (it != actions.end()) {
320         it->second(this, param);
321     }
322 }
323 
SetLinearGradientBlurParams(const std::string & tag,float param)324 void GEVisualEffectImpl::SetLinearGradientBlurParams(const std::string& tag, float param)
325 {
326     if (linearGradientBlurParams_ == nullptr) {
327         return;
328     }
329 
330     static std::unordered_map<std::string, std::function<void(GEVisualEffectImpl*, float)>> actions = {
331         { GE_FILTER_LINEAR_GRADIENT_BLUR_RADIUS,
332             [](GEVisualEffectImpl* obj, float p) { obj->linearGradientBlurParams_->blurRadius = p; } },
333         { GE_FILTER_LINEAR_GRADIENT_BLUR_GEO_WIDTH,
334             [](GEVisualEffectImpl* obj, float p) { obj->linearGradientBlurParams_->geoWidth   = p; } },
335         { GE_FILTER_LINEAR_GRADIENT_BLUR_GEO_HEIGHT,
336             [](GEVisualEffectImpl* obj, float p) { obj->linearGradientBlurParams_->geoHeight  = p; } },
337         { GE_FILTER_LINEAR_GRADIENT_BLUR_TRAN_X,
338             [](GEVisualEffectImpl* obj, float p) { obj->linearGradientBlurParams_->tranX      = p; } },
339         { GE_FILTER_LINEAR_GRADIENT_BLUR_TRAN_Y,
340             [](GEVisualEffectImpl* obj, float p) { obj->linearGradientBlurParams_->tranY      = p; } }
341     };
342 
343     auto it = actions.find(tag);
344     if (it != actions.end()) {
345         it->second(this, param);
346     }
347 }
348 
SetMagnifierParamsFloat(const std::string & tag,float param)349 void GEVisualEffectImpl::SetMagnifierParamsFloat(const std::string& tag, float param)
350 {
351     if (magnifierParams_ == nullptr) {
352         return;
353     }
354 
355     static std::unordered_map<std::string, std::function<void(GEVisualEffectImpl*, float)>> actions = {
356         { GE_FILTER_MAGNIFIER_FACTOR,
357             [](GEVisualEffectImpl* obj, float p) { obj->magnifierParams_->factor = p; } },
358         { GE_FILTER_MAGNIFIER_WIDTH,
359             [](GEVisualEffectImpl* obj, float p) { obj->magnifierParams_->width = p; } },
360         { GE_FILTER_MAGNIFIER_HEIGHT,
361             [](GEVisualEffectImpl* obj, float p) { obj->magnifierParams_->height = p; } },
362         { GE_FILTER_MAGNIFIER_CORNER_RADIUS,
363             [](GEVisualEffectImpl* obj, float p) { obj->magnifierParams_->cornerRadius = p; } },
364         { GE_FILTER_MAGNIFIER_BORDER_WIDTH,
365             [](GEVisualEffectImpl* obj, float p) { obj->magnifierParams_->borderWidth = p; } },
366         { GE_FILTER_MAGNIFIER_SHADOW_OFFSET_X,
367             [](GEVisualEffectImpl* obj, float p) { obj->magnifierParams_->shadowOffsetX = p; } },
368         { GE_FILTER_MAGNIFIER_SHADOW_OFFSET_Y,
369             [](GEVisualEffectImpl* obj, float p) { obj->magnifierParams_->shadowOffsetY = p; } },
370         { GE_FILTER_MAGNIFIER_SHADOW_SIZE,
371             [](GEVisualEffectImpl* obj, float p) { obj->magnifierParams_->shadowSize = p; } },
372         { GE_FILTER_MAGNIFIER_SHADOW_STRENGTH,
373             [](GEVisualEffectImpl* obj, float p) { obj->magnifierParams_->shadowStrength = p; } }
374     };
375 
376     auto it = actions.find(tag);
377     if (it != actions.end()) {
378         it->second(this, param);
379     }
380 }
381 
SetMagnifierParamsUint32(const std::string & tag,uint32_t param)382 void GEVisualEffectImpl::SetMagnifierParamsUint32(const std::string& tag, uint32_t param)
383 {
384     if (magnifierParams_ == nullptr) {
385         return;
386     }
387 
388     static std::unordered_map<std::string, std::function<void(GEVisualEffectImpl*, uint32_t)>> actions = {
389         { GE_FILTER_MAGNIFIER_GRADIENT_MASK_COLOR_1,
390             [](GEVisualEffectImpl* obj, uint32_t p) { obj->magnifierParams_->gradientMaskColor1 = p; } },
391         { GE_FILTER_MAGNIFIER_GRADIENT_MASK_COLOR_2,
392             [](GEVisualEffectImpl* obj, uint32_t p) { obj->magnifierParams_->gradientMaskColor2 = p; } },
393         { GE_FILTER_MAGNIFIER_OUTER_CONTOUR_COLOR_1,
394             [](GEVisualEffectImpl* obj, uint32_t p) { obj->magnifierParams_->outerContourColor1 = p; } },
395         { GE_FILTER_MAGNIFIER_OUTER_CONTOUR_COLOR_2,
396             [](GEVisualEffectImpl* obj, uint32_t p) { obj->magnifierParams_->outerContourColor2 = p; } }
397     };
398 
399     auto it = actions.find(tag);
400     if (it != actions.end()) {
401         it->second(this, param);
402     }
403 }
404 
SetWaterRippleParams(const std::string & tag,float param)405 void GEVisualEffectImpl::SetWaterRippleParams(const std::string& tag, float param)
406 {
407     if (waterRippleParams_ == nullptr) {
408         return;
409     }
410 
411     static std::unordered_map<std::string, std::function<void(GEVisualEffectImpl*, float)>> actions = {
412 
413         { GE_FILTER_WATER_RIPPLE_PROGRESS,
414             [](GEVisualEffectImpl* obj, float p) { obj->waterRippleParams_->progress = p; } },
415         { GE_FILTER_WATER_RIPPLE_RIPPLE_CENTER_X,
416             [](GEVisualEffectImpl* obj, float p) { obj->waterRippleParams_->rippleCenterX = p; } },
417         { GE_FILTER_WATER_RIPPLE_RIPPLE_CENTER_Y,
418             [](GEVisualEffectImpl* obj, float p) { obj->waterRippleParams_->rippleCenterY = p; } },
419     };
420 
421     auto it = actions.find(tag);
422     if (it != actions.end()) {
423         it->second(this, param);
424     }
425 }
426 } // namespace Drawing
427 } // namespace Rosen
428 } // namespace OHOS
429