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 "ge_filter_composer.h"
17 #include "ge_render.h"
18 #include "ge_log.h"
19
20 namespace OHOS {
21 namespace Rosen {
22
GEFilterComposer(const std::shared_ptr<GEShaderFilter> & shaderFilter)23 GEFilterComposer::GEFilterComposer(const std::shared_ptr<GEShaderFilter>& shaderFilter)
24 : composedFilter_(shaderFilter), composedType_(shaderFilter->Type())
25 {
26 if (composedFilter_ != nullptr) {
27 filterParams_[composedType_] = composedFilter_->Params();
28 } else {
29 LOGE("GEFilterComposer: ctor with nullptr");
30 }
31 }
32
Compose(const std::shared_ptr<GEShaderFilter> other)33 bool GEFilterComposer::Compose(const std::shared_ptr<GEShaderFilter> other)
34 {
35 if (other == nullptr || other == composedFilter_) {
36 LOGE("GEFilterComposer: compose with nullptr or self");
37 return false;
38 }
39
40 auto otherFilterType = other->Type();
41 std::string composedType = composedType_ + otherFilterType;
42 if (composedEffects_.count(composedType)) {
43 filterParams_[otherFilterType] = other->Params();
44 if (auto composedFilter = GenerateComposedFilter(composedType, filterParams_)) {
45 composedFilter_ = composedFilter;
46 composedType_ = composedType;
47 return true;
48 }
49 }
50 return false;
51 }
52
GetComposedFilter()53 std::shared_ptr<GEShaderFilter> GEFilterComposer::GetComposedFilter()
54 {
55 return composedFilter_;
56 }
57
GenerateComposedFilter(const std::string composedType,const std::map<std::string,GEShaderFilter::FilterParams> filterParams)58 std::shared_ptr<GEShaderFilter> GEFilterComposer::GenerateComposedFilter(
59 const std::string composedType, const std::map<std::string, GEShaderFilter::FilterParams> filterParams)
60 {
61 // Only support GreyBlur now, support more complex filter in the future
62 if (composedType == "GreyBlur") {
63 auto mesaFilter = std::make_shared<Drawing::GEVisualEffect>(Drawing::GE_FILTER_MESA_BLUR,
64 Drawing::DrawingPaintType::BRUSH);
65 auto blurIt = filterParams.find("Blur");
66 if (blurIt == filterParams.end()) {
67 return nullptr;
68 }
69 const auto* blurParams = std::get_if<Drawing::GEKawaseBlurShaderFilterParams>(&blurIt->second.value());
70 if (!blurParams) {
71 return nullptr;
72 }
73 auto greyIt = filterParams.find("Grey");
74 if (greyIt == filterParams.end()) {
75 return nullptr;
76 }
77
78 const auto* greyParams = std::get_if<Drawing::GEGreyShaderFilterParams>(&greyIt->second.value());
79 if (!greyParams) {
80 return nullptr;
81 }
82 mesaFilter->SetParam(Drawing::GE_FILTER_MESA_BLUR_RADIUS, blurParams->radius);
83 mesaFilter->SetParam(Drawing::GE_FILTER_MESA_BLUR_GREY_COEF_1, greyParams->greyCoef1);
84 mesaFilter->SetParam(Drawing::GE_FILTER_MESA_BLUR_GREY_COEF_2, greyParams->greyCoef2);
85 mesaFilter->SetParam(Drawing::GE_FILTER_MESA_BLUR_STRETCH_OFFSET_X, 0.f);
86 mesaFilter->SetParam(Drawing::GE_FILTER_MESA_BLUR_STRETCH_OFFSET_Y, 0.f);
87 mesaFilter->SetParam(Drawing::GE_FILTER_MESA_BLUR_STRETCH_OFFSET_Z, 0.f);
88 mesaFilter->SetParam(Drawing::GE_FILTER_MESA_BLUR_STRETCH_OFFSET_W, 0.f);
89 mesaFilter->SetParam(Drawing::GE_FILTER_MESA_BLUR_STRETCH_TILE_MODE, 0);
90 mesaFilter->SetParam(Drawing::GE_FILTER_MESA_BLUR_STRETCH_WIDTH, 0.f);
91 mesaFilter->SetParam(Drawing::GE_FILTER_MESA_BLUR_STRETCH_HEIGHT, 0.f);
92
93 GraphicsEffectEngine::GERender render;
94 return render.GenerateShaderFilter(mesaFilter);
95 }
96 return nullptr;
97 }
98
ApplyComposedEffect(Drawing::Canvas & canvas,const std::shared_ptr<Drawing::Image> image,const Drawing::Rect & src,const Drawing::Rect & dst)99 std::shared_ptr<Drawing::Image> GEFilterComposer::ApplyComposedEffect(Drawing::Canvas& canvas,
100 const std::shared_ptr<Drawing::Image> image, const Drawing::Rect& src, const Drawing::Rect& dst)
101 {
102 auto resImage = composedFilter_->OnProcessImage(canvas, image, src, dst);
103 return resImage;
104 }
105
106 } // namespace Rosen
107 } // namespace OHOS
108