• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ge_render.h"
16 
17 #include "ge_aibar_shader_filter.h"
18 #include "ge_grey_shader_filter.h"
19 #include "ge_kawase_blur_shader_filter.h"
20 #include "ge_mesa_blur_shader_filter.h"
21 #include "ge_linear_gradient_blur_shader_filter.h"
22 #include "ge_log.h"
23 #include "ge_magnifier_shader_filter.h"
24 #include "ge_visual_effect_impl.h"
25 #include "ge_water_ripple_filter.h"
26 #include "ge_external_dynamic_loader.h"
27 
28 namespace OHOS {
29 namespace GraphicsEffectEngine {
30 
GERender()31 GERender::GERender() {}
32 
~GERender()33 GERender::~GERender() {}
34 
DrawImageEffect(Drawing::Canvas & canvas,Drawing::GEVisualEffectContainer & veContainer,const std::shared_ptr<Drawing::Image> & image,const Drawing::Rect & src,const Drawing::Rect & dst,const Drawing::SamplingOptions & sampling)35 void GERender::DrawImageEffect(Drawing::Canvas& canvas, Drawing::GEVisualEffectContainer& veContainer,
36     const std::shared_ptr<Drawing::Image>& image, const Drawing::Rect& src, const Drawing::Rect& dst,
37     const Drawing::SamplingOptions& sampling)
38 {
39     if (!image) {
40         LOGE("GERender::DrawImageRect image is null");
41         return;
42     }
43 
44     auto resImage = ApplyImageEffect(canvas, veContainer, image, src, dst, sampling);
45     Drawing::Brush brush;
46     canvas.AttachBrush(brush);
47     canvas.DrawImageRect(*resImage, src, dst, Drawing::SamplingOptions());
48     canvas.DetachBrush();
49 }
50 
ApplyImageEffect(Drawing::Canvas & canvas,Drawing::GEVisualEffectContainer & veContainer,const std::shared_ptr<Drawing::Image> & image,const Drawing::Rect & src,const Drawing::Rect & dst,const Drawing::SamplingOptions & sampling)51 std::shared_ptr<Drawing::Image> GERender::ApplyImageEffect(Drawing::Canvas& canvas,
52     Drawing::GEVisualEffectContainer& veContainer, const std::shared_ptr<Drawing::Image>& image,
53     const Drawing::Rect& src, const Drawing::Rect& dst, const Drawing::SamplingOptions& sampling)
54 {
55     if (!image) {
56         LOGE("GERender::ApplyImageEffect image is null");
57         return nullptr;
58     }
59     std::vector<std::shared_ptr<GEShaderFilter>> geShaderFilters = GenerateShaderFilter(veContainer);
60     auto resImage = image;
61     for (auto geShaderFilter : geShaderFilters) {
62         if (geShaderFilter != nullptr) {
63             resImage = geShaderFilter->ProcessImage(canvas, resImage, src, dst);
64         } else {
65             LOGD("GERender::ApplyImageEffect filter is null");
66         }
67     }
68 
69     return resImage;
70 }
71 
GenerateExtShaderFilter(const std::shared_ptr<Drawing::GEVisualEffectImpl> & ve)72 std::shared_ptr<GEShaderFilter> GERender::GenerateExtShaderFilter(
73     const std::shared_ptr<Drawing::GEVisualEffectImpl>& ve)
74 {
75     auto type = ve->GetFilterType();
76     switch (type) {
77         case Drawing::GEVisualEffectImpl::FilterType::MESA_BLUR: {
78             const auto& mesaParams = ve->GetMESAParams();
79             auto object = GEExternalDynamicLoader::GetInstance().CreateGEXObjectByType(
80                 static_cast<uint32_t>(type), sizeof(Drawing::GEMESABlurShaderFilterParams),
81                 static_cast<void*>(mesaParams.get()));
82             if (!object) {
83                 return std::make_shared<GEMESABlurShaderFilter>(*mesaParams);
84             }
85             std::shared_ptr<GEMESABlurShaderFilter> dmShader(static_cast<GEMESABlurShaderFilter*>(object));
86             return dmShader;
87         }
88         case Drawing::GEVisualEffectImpl::FilterType::LINEAR_GRADIENT_BLUR: {
89             const auto& linearGradientBlurParams = ve->GetLinearGradientBlurParams();
90             auto object = GEExternalDynamicLoader::GetInstance().CreateGEXObjectByType(
91                 static_cast<uint32_t>(type), sizeof(Drawing::GELinearGradientBlurShaderFilterParams),
92                 static_cast<void*>(linearGradientBlurParams.get()));
93             if (!object) {
94                 return std::make_shared<GELinearGradientBlurShaderFilter>(*linearGradientBlurParams);
95             }
96             std::shared_ptr<GELinearGradientBlurShaderFilter>
97                 dmShader(static_cast<GELinearGradientBlurShaderFilter*>(object));
98             return dmShader;
99         }
100         default:
101             break;
102     }
103     return nullptr;
104 }
105 
GenerateShaderFilter(Drawing::GEVisualEffectContainer & veContainer)106 std::vector<std::shared_ptr<GEShaderFilter>> GERender::GenerateShaderFilter(
107     Drawing::GEVisualEffectContainer& veContainer)
108 {
109     LOGD("GERender::shaderFilters %{public}d", (int)veContainer.GetFilters().size());
110     std::vector<std::shared_ptr<GEShaderFilter>> shaderFilters;
111     for (auto vef : veContainer.GetFilters()) {
112         auto ve = vef->GetImpl();
113         std::shared_ptr<GEShaderFilter> shaderFilter;
114         LOGD("GERender::shaderFilters %{public}d", (int)ve->GetFilterType());
115         switch (ve->GetFilterType()) {
116             case Drawing::GEVisualEffectImpl::FilterType::KAWASE_BLUR: {
117                 const auto& kawaseParams = ve->GetKawaseParams();
118                 shaderFilter = std::make_shared<GEKawaseBlurShaderFilter>(*kawaseParams);
119                 break;
120             }
121             case Drawing::GEVisualEffectImpl::FilterType::MESA_BLUR: {
122                 shaderFilter = GenerateExtShaderFilter(ve);
123                 break;
124             }
125             case Drawing::GEVisualEffectImpl::FilterType::AIBAR: {
126                 const auto& aiBarParams = ve->GetAIBarParams();
127                 shaderFilter = std::make_shared<GEAIBarShaderFilter>(*aiBarParams);
128                 break;
129             }
130             case Drawing::GEVisualEffectImpl::FilterType::GREY: {
131                 const auto& greyParams = ve->GetGreyParams();
132                 shaderFilter = std::make_shared<GEGreyShaderFilter>(*greyParams);
133                 break;
134             }
135             case Drawing::GEVisualEffectImpl::FilterType::LINEAR_GRADIENT_BLUR: {
136                 shaderFilter = GenerateExtShaderFilter(ve);
137                 break;
138             }
139             case Drawing::GEVisualEffectImpl::FilterType::MAGNIFIER: {
140                 const auto& magnifierParams = ve->GetMagnifierParams();
141                 shaderFilter = std::make_shared<GEMagnifierShaderFilter>(*magnifierParams);
142                 break;
143             }
144             case Drawing::GEVisualEffectImpl::FilterType::WATER_RIPPLE: {
145                 const auto& waterRippleParams = ve->GetWaterRippleParams();
146                 shaderFilter = std::make_shared<GEWaterRippleFilter>(*waterRippleParams);
147                 break;
148             }
149             default:
150                 break;
151         }
152         shaderFilters.push_back(shaderFilter);
153     }
154     return shaderFilters;
155 }
156 
157 } // namespace GraphicsEffectEngine
158 } // namespace OHOS
159