From e5e759fcb74f9f893e5dbe1e4a66495437ba53ec Mon Sep 17 00:00:00 2001 From: p30034710 Date: Wed, 25 Jun 2025 16:50:31 +0800 Subject: [PATCH] ge adapt for cacheData Signed-off-by: panjiao --- include/ge_render.h | 2 +- include/ge_shader.h | 16 +++- include/ge_visual_effect_container.h | 4 + include/ge_visual_effect_impl.h | 13 +++ include/ge_wavy_ripple_light_shader.h | 64 +++++++++++++ src/ge_render.cpp | 75 ++++++++------- src/ge_visual_effect_container.cpp | 34 +++++++ src/ge_wavy_ripple_light_shader.cpp | 128 ++++++++++++++++++++++++++ 8 files changed, 302 insertions(+), 34 deletions(-) create mode 100644 include/ge_wavy_ripple_light_shader.h create mode 100644 src/ge_wavy_ripple_light_shader.cpp diff --git a/include/ge_render.h b/include/ge_render.h index ffb5970..7c7cf19 100644 --- a/include/ge_render.h +++ b/include/ge_render.h @@ -52,7 +52,7 @@ public: const Drawing::Rect& src, const Drawing::Rect& dst, const Drawing::SamplingOptions& sampling); private: - std::vector> GenerateShaderEffect(Drawing::GEVisualEffectContainer& veContainer); + std::shared_ptr GenerateShaderEffect(std::shared_ptr ve); std::vector> GenerateShaderFilter(Drawing::GEVisualEffectContainer& veContainer); diff --git a/include/ge_shader.h b/include/ge_shader.h index 696f31d..f5328a2 100644 --- a/include/ge_shader.h +++ b/include/ge_shader.h @@ -15,7 +15,7 @@ #ifndef GRAPHICS_EFFECT_GE_SHADER_H #define GRAPHICS_EFFECT_GE_SHADER_H - +#include #include "draw/canvas.h" #include "utils/rect.h" #include "effect/shader_effect.h" @@ -35,9 +35,23 @@ public: uint32_t Hash() const { return hash_; } + // to calculate your cache data + virtual void Preprocess(Drawing::Canvas& canvas, const Drawing::Rect& rect) {} + + void SetCache(const std::shared_ptr& cacheData) + { + cacheAnyPtr_ = cacheData; + } + + std::shared_ptr GetCache() const + { + return cacheAnyPtr_; + } + protected: uint32_t hash_ = 0; std::shared_ptr drShader_ = nullptr; + std::shared_ptr cacheAnyPtr_ = nullptr; }; } // namespace Rosen } // namespace OHOS diff --git a/include/ge_visual_effect_container.h b/include/ge_visual_effect_container.h index 7c771b4..82561ac 100644 --- a/include/ge_visual_effect_container.h +++ b/include/ge_visual_effect_container.h @@ -33,7 +33,11 @@ public: return filterVec_; } + void UpdateCacheDataFrom(const std::shared_ptr& ge); + private: + std::shared_ptr GetGEVisualEffect(const std::string& name); + std::vector> filterVec_; }; diff --git a/include/ge_visual_effect_impl.h b/include/ge_visual_effect_impl.h index 356dd69..04516b1 100644 --- a/include/ge_visual_effect_impl.h +++ b/include/ge_visual_effect_impl.h @@ -270,6 +270,17 @@ public: { return auroNoiseParams_; } + + void SetCache(const std::shared_ptr& cacheData) + { + cacheAnyPtr_ = cacheData; + } + + std::shared_ptr GetCache() const + { + return cacheAnyPtr_; + } + private: static std::map> g_initialMap; @@ -318,6 +329,8 @@ private: std::shared_ptr contentDiagonalParams_ = nullptr; std::shared_ptr wavyRippleLightParams_ = nullptr; std::shared_ptr auroNoiseParams_ = nullptr; + + std::shared_ptr cacheAnyPtr_ = nullptr; }; } // namespace Drawing diff --git a/include/ge_wavy_ripple_light_shader.h b/include/ge_wavy_ripple_light_shader.h new file mode 100644 index 0000000..8777a19 --- /dev/null +++ b/include/ge_wavy_ripple_light_shader.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef GRAPHICS_EFFECT_WAVY_RIPPLE_LIGHT_SHADER_H +#define GRAPHICS_EFFECT_WAVY_RIPPLE_LIGHT_SHADER_H + +#include + +#include "ge_shader.h" +#include "common/rs_vector2.h" +#include "effect/runtime_shader_builder.h" +#include "utils/matrix.h" +#include "ge_shader_filter_params.h" + +namespace OHOS { +namespace Rosen { + +class GE_EXPORT GEWavyRippleLightShader : public GEShader { +public: + GEWavyRippleLightShader(); + GEWavyRippleLightShader(Drawing::GEWavyRippleLightShaderParams& param); + ~GEWavyRippleLightShader() override = default; + + void MakeDrawngShader(const Drawing::Rect& rect, float progress) override; + + const std::string GetDescription() const { return "GEWavyRippleLightShader";} + + void SetWavyRippleLightParams(const Drawing::GEWavyRippleLightShaderParams& param) + { + wavyRippleLightParams_ = param; + } + + std::shared_ptr MakeWavyRippleLightShader(const Drawing::Rect& rect); + + static std::shared_ptr CreateWavyRippleLightShader(Drawing::GEWavyRippleLightShaderParams& + param); + + std::shared_ptr GetWavyRippleLightBuilder(); + +private: + GEWavyRippleLightShader(const GEWavyRippleLightShader&) = delete; + GEWavyRippleLightShader(const GEWavyRippleLightShader&&) = delete; + GEWavyRippleLightShader& operator=(const GEWavyRippleLightShader&) = delete; + GEWavyRippleLightShader& operator=(const GEWavyRippleLightShader&&) = delete; + Drawing::GEWavyRippleLightShaderParams wavyRippleLightParams_; + std::shared_ptr builder_; +}; + +} // namespace Rosen +} // namespace OHOS + +#endif // GRAPHICS_EFFECT_WAVY_RIPPLE_LIGHT_SHADER_H \ No newline at end of file diff --git a/src/ge_render.cpp b/src/ge_render.cpp index 1842b18..65533ec 100644 --- a/src/ge_render.cpp +++ b/src/ge_render.cpp @@ -34,7 +34,34 @@ namespace OHOS { namespace GraphicsEffectEngine { +using namespace Rosen::Drawing; +using ShaderCreator = std::function(std::shared_ptr)>; +static std::unordered_map shaderCreatorLUT = { + {GEVisualEffectImpl::FilterType::CONTOUR_DIAGONAL_FLOW_LIGHT, [] (std::shared_ptr ve) + { + std::shared_ptr out = nullptr; + return out; + } + }, + {GEVisualEffectImpl::FilterType::WAVY_RIPPLE_LIGHT, [] (std::shared_ptr ve) + { + std::shared_ptr out = nullptr; + if (ve == nullptr) { + return out; + } + const auto& params = ve->GetWavyRippleLightParams(); + (void)params; + return out; + } + }, + {GEVisualEffectImpl::FilterType::AURORA_NOISE, [] (std::shared_ptr ve) + { + std::shared_ptr out = nullptr; + return out; + } + } +}; GERender::GERender() {} GERender::~GERender() {} @@ -221,52 +248,36 @@ std::vector> GERender::GenerateShaderFilter( void GERender::DrawShaderEffect(Drawing::Canvas& canvas, Drawing::GEVisualEffectContainer& veContainer, const Drawing::Rect& bounds) { - std::vector> geShaderEffects = GenerateShaderEffect(veContainer); - for (auto geShaderEffect : geShaderEffects) { + LOGD("GERender::DrawShaderEffect %{public}zu", veContainer.GetFilters().size()); + for (auto vef : veContainer.GetFilters()) { + if (vef == nullptr) { + LOGD("GERender::DrawShaderEffect vef is null"); + continue; + } + auto ve = vef->GetImpl(); + std::shared_ptr geShaderEffect = GenerateShaderEffect(ve); if (geShaderEffect == nullptr) { LOGD("GERender::DrawShaderEffect shader is null"); continue; } - geShaderEffect->MakeDrawingShader(bounds, -1.f); // new flow not use progress + geShaderEffect->SetCache(ve->GetCache()); + geShaderEffect->Preprocess(canvas, bounds); // to calculate your cache data + geShaderEffect->MakeDrawingShader(bounds, -1.f); // not use progress auto shader = geShaderEffect->GetDrawingShader(); Drawing::Brush brush; brush.SetShaderEffect(shader); canvas.AttachBrush(brush); canvas.DrawRect(bounds); canvas.DetachBrush(); + + ve->SetCache(geShaderEffect->GetCache()); } } -std::vector> GERender::GenerateShaderEffect(Drawing::GEVisualEffectContainer& veContainer) +std::shared_ptr GERender::GenerateShaderEffect(std::shared_ptr ve) { - LOGD("GERender::shaderEffects %{public}zu", veContainer.GetFilters().size()); - std::vector> shaderEffects; - for (auto vef : veContainer.GetFilters()) { - auto ve = vef->GetImpl(); - std::shared_ptr shaderEffect; - LOGD("GERender::shaderEffects %{public}d", static_cast(ve->GetFilterType())); - switch (ve->GetFilterType()) { - case Drawing::GEVisualEffectImpl::FilterType::CONTOUR_DIAGONAL_FLOW_LIGHT: { - const auto& params = ve->GetWavyRippleLightParams(); - (void)params; - break; - } - case Drawing::GEVisualEffectImpl::FilterType::WAVY_RIPPLE_LIGHT: { - const auto& params = ve->GetWavyRippleLightParams(); - (void)params; - break; - } - case Drawing::GEVisualEffectImpl::FilterType::AURORA_NOISE: { - const auto& params = ve->GetAuroraNoiseParams(); - (void)params; - break; - } - default: - break; - } - shaderEffects.push_back(shaderEffect); - } - return shaderEffects; + auto it = shaderCreatorLUT.find(ve->GetFilterType()); + return it != shaderCreatorLUT.end() ? it->second(ve) : nullptr; } } // namespace GraphicsEffectEngine } // namespace OHOS diff --git a/src/ge_visual_effect_container.cpp b/src/ge_visual_effect_container.cpp index cf717c3..56b6a01 100644 --- a/src/ge_visual_effect_container.cpp +++ b/src/ge_visual_effect_container.cpp @@ -16,6 +16,7 @@ #include "ge_visual_effect_container.h" #include "ge_log.h" +#include "ge_visual_effect_impl.h" namespace OHOS { namespace Rosen { @@ -33,6 +34,39 @@ void GEVisualEffectContainer::AddToChainedFilter(std::shared_ptr& ge) +{ + if (ge == nullptr) { + return; + } + for (auto vef : ge->GetFilters()) { + if (vef == nullptr) { + LOGD("GEVisualEffectContainer::UpdateCacheDataFrom vef is null"); + continue; + } + auto vefTarget = GetGEVisualEffect(vef->GetName()); + if (vef->GetImpl() == nullptr || vefTarget == nullptr || vefTarget->GetImpl() == nullptr) { + LOGD("GEVisualEffectContainer::UpdateCacheDataFrom ve is null"); + continue; + } + vefTarget->GetImpl()->SetCache(vef->GetImpl()->GetCache()); + } +} + +std::shared_ptr GEVisualEffectContainer::GetGEVisualEffect(const std::string& name) +{ + for (auto vef : filterVec_) { + if (vef == nullptr) { + LOGD("GEVisualEffectContainer::GetGEVisualEffect vef is null"); + continue; + } + if (vef->GetName() == name) { + return vef; + } + } + return nullptr; +} + } // namespace Drawing } // namespace Rosen } // namespace OHOS diff --git a/src/ge_wavy_ripple_light_shader.cpp b/src/ge_wavy_ripple_light_shader.cpp new file mode 100644 index 0000000..119df32 --- /dev/null +++ b/src/ge_wavy_ripple_light_shader.cpp @@ -0,0 +1,128 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ge_wavy_ripple_light_shader.h" +#include "ge_log.h" + +namespace OHOS { +namespace Rosen { +GEWavyRippleLightShader::GEWavyRippleLightShader() {} + +GEWavyRippleLightShader::GEWavyRippleLightShader(Drawing::GEWavyRippleLightShaderParams& param) +{ + wavyRippleLightParams_ = param; +} +std::shared_ptr GEWavyRippleLightShader::CreateWavyRippleLightShader( + Drawing::GEWavyRippleLightShaderParams& param) +{ + std::shared_ptr wavyRippleLightShader = std::make_shared(param); + return wavyRippleLightShader; +} + +void GEWavyRippleLightShader::MakeDrawngShader(const Drawing::Rect& rect, float progress) +{ + drShader_ = MakeWavyRippleLightShader(rect); +} + +void GEWavyRippleLightShader::SetWavyRippleLightParams(const Drawing::GEWavyRippleLightShaderParams& param) +{ + wavyRippleLightParams_ = param; +} + +std::shared_ptr GEWavyRippleLightShader::GetWavyRippleLightBuilder() +{ + thread_local std::shared_ptr wavyRippleLightShaderEffect_ = nullptr; + if (wavyRippleLightShaderEffect_ == nullptr) { + static constexpr char prog[] = R"( + uniform vec2 iResolution; + uniform vec2 center; + uniform float radius; + + const float noiseScale = 0.3; + const float waveScale = 0.5; + + const float freqX = 4.0; + const float freqY = 6.0; + const float freqDiag = 8.0; + + const float thickness = 0.2; + + float ShapeSDF(vec2 p, float radius) + { + float dist = length(p); + + float noise = 0.0; + noise += sin(p.x * freqX) * 0.25; + noise += sin(p.y * freqY) * 0.35; + noise += sin((p.x + p.y) * freqDiag) * 0.3; + + float distortedDist = dist + noise * noiseScale; + float attenuation = waveScale / (1.0 + distortedDist * 5.0); + float wave = sin(distortedDist * 30.0) * attenuation; + return distortedDist - radius + wave * 0.05; + } + + vec4 main(vec2 fragCoord) + { + vec2 uv = fragCoord.xy / iResolution.xy; + float aspect = iResolution.x / iResolution.y; + vec3 color = vec3(0.0); + + vec2 p = uv * 2.0 - 1.0; + c.x *= aspect; + + vec2 delta = p -c; + + float dis0 = length(c - vec2(-aspect, -1.0)); + float dis1 = length(c - vec2(-aspect, -1.0)); + float dis2 = length(c - vec2(-aspect, -1.0)); + float dis3 = length(c - vec2(-aspect, -1.0)); + float maxRadius = max(max(dist0, dist1), max(dist2, dist3)); + + float currentRadius = maxRadius * radius; + float d = abs(ShapeSDF(delta, currentRadius)); + float mask = smoothstep(thickness, 0.0, d); + + vec3 baseColor = vec3(0.8, 0.5, 1.0); + color = baseColor * mask; + return vec4(color, mask); + } + )"; + wavyRippleLightShaderEffect_ = Drawing::RuntimeEffect::CreateForShader(prog) + } + if (wavyRippleLightShaderEffect_ == nullptr) { + GE_LOGE("GEWavyRippleLightShader::GetWavyRippleLightBuilder wavyRippleLightShaderEffect_ is nullptr"); + return nullptr; + } + return std::make_shared(wavyRippleLightShaderEffect_); +} + +std::shared_ptr GEWavyRippleLightShader::MakeWavyRippleLightShader(const Drawing::Rect& rect) +{ + auto width = rect.GetWidth(); + auto height = rect.GetHeight(); + builder_ = GetWavyRippleLightBuilder(); + builder_->SetUniform("iResolution", width, height); + builder_->SetUniform("center", wavyRippleLightParams_.center_.first, wavyRippleLightParams_.center_.second); + builder_->SetUniform("radius", wavyRippleLightParams_.radius_); + auto wavyRippleLightShader = builder_->MakeShader(nullptr, false); + if (wavyRippleLightShader == nullptr) { + GE_LOGE("GEWavyRippleLightShader::MakeWavyRippleLightShader wavyRippleLightShader is nullptr"); + return nullptr; + } + return wavyRippleLightShader; +} +} // namespace Rosen +} // namespace OHOS \ No newline at end of file -- Gitee