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