From de0d337fcd0ad46ec26656b4dd597ce83dbf86e6 Mon Sep 17 00:00:00 2001 From: dujiangnan Date: Tue, 4 Jun 2024 19:52:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=99=AE=E9=80=9A=E9=98=B4?= =?UTF-8?q?=E5=BD=B1=E4=B8=8E=E5=BD=A9=E8=89=B2=E9=98=B4=E5=BD=B1=E9=87=8D?= =?UTF-8?q?=E5=8F=A0=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: dujiangnan --- .../rs_property_drawable_background.h | 9 --- .../include/property/rs_properties.h | 5 +- .../rs_property_drawable_background.cpp | 43 +---------- .../src/property/rs_properties.cpp | 74 +++++++++++-------- .../src/render/rs_colorful_shadow_filter.cpp | 5 ++ 5 files changed, 54 insertions(+), 82 deletions(-) diff --git a/rosen/modules/render_service_base/include/drawable/rs_property_drawable_background.h b/rosen/modules/render_service_base/include/drawable/rs_property_drawable_background.h index 2b160d7685..bd4c7172ac 100644 --- a/rosen/modules/render_service_base/include/drawable/rs_property_drawable_background.h +++ b/rosen/modules/render_service_base/include/drawable/rs_property_drawable_background.h @@ -68,15 +68,6 @@ public: Drawing::RecordingCanvas::DrawFunc CreateDrawFunc() const override; }; -class RSColorfulShadowDrawable : public RSPropertyDrawable { -public: - RSColorfulShadowDrawable( - std::shared_ptr&& drawCmdList) : RSPropertyDrawable(std::move(drawCmdList)) - {} - RSColorfulShadowDrawable() = default; - bool OnUpdate(const RSRenderNode& node) override; -}; - class RSMaskDrawable : public RSPropertyDrawable { public: RSMaskDrawable(std::shared_ptr&& drawCmdList) : RSPropertyDrawable(std::move(drawCmdList)) {} diff --git a/rosen/modules/render_service_base/include/property/rs_properties.h b/rosen/modules/render_service_base/include/property/rs_properties.h index dcd6fbda4f..64c27b497c 100644 --- a/rosen/modules/render_service_base/include/property/rs_properties.h +++ b/rosen/modules/render_service_base/include/property/rs_properties.h @@ -333,7 +333,7 @@ public: bool IsBackgroundMaterialFilterValid() const; bool IsForegroundMaterialFilterVaild() const; - + // shadow properties void SetShadowColor(Color color); void SetShadowOffsetX(float offsetX); @@ -612,7 +612,7 @@ private: int foregroundColorMode_ = BLUR_COLOR_MODE::DEFAULT; float foregroundBlurRadiusX_ = 0.f; float foregroundBlurRadiusY_ = 0.f; - + std::weak_ptr backref_; std::optional pixelStretch_; @@ -644,6 +644,7 @@ private: void CheckGreyCoef(); void UpdateFilter(); + void UpdateForegroundFilter(); // partial update bool colorFilterNeedUpdate_ = false; diff --git a/rosen/modules/render_service_base/src/drawable/rs_property_drawable_background.cpp b/rosen/modules/render_service_base/src/drawable/rs_property_drawable_background.cpp index a5b996a791..e19a395689 100644 --- a/rosen/modules/render_service_base/src/drawable/rs_property_drawable_background.cpp +++ b/rosen/modules/render_service_base/src/drawable/rs_property_drawable_background.cpp @@ -54,8 +54,8 @@ RSDrawable::Ptr RSShadowDrawable::OnGenerate(const RSRenderNode& node) bool RSShadowDrawable::OnUpdate(const RSRenderNode& node) { const RSProperties& properties = node.GetRenderProperties(); - // skip shadow if not valid. ShadowMask is processed by foregound - if (!properties.IsShadowValid() || node.GetRenderProperties().GetShadowMask()) { + // skip shadow if not valid. ShadowMask is processed by foreground + if (!properties.IsShadowValid() || properties.GetShadowMask()) { return false; } @@ -120,8 +120,8 @@ Drawing::RecordingCanvas::DrawFunc RSShadowDrawable::CreateDrawFunc() const bool RSMaskShadowDrawable::OnUpdate(const RSRenderNode& node) { - // skip shadow if not valid - if (!node.GetRenderProperties().IsShadowValid()) { + // skip shadow if not valid. ShadowMask is processed by foreground + if (!node.GetRenderProperties().IsShadowValid() || node.GetRenderProperties().GetShadowMask()) { return false; } RSPropertyDrawCmdListUpdater updater(0, 0, this); @@ -189,41 +189,6 @@ Drawing::RecordingCanvas::DrawFunc RSMaskShadowDrawable::CreateDrawFunc() const }; } -bool RSColorfulShadowDrawable::OnUpdate(const RSRenderNode& node) -{ - // regenerate stagingDrawCmdList_ - RSPropertyDrawCmdListUpdater updater(0, 0, this); - Drawing::Canvas& canvas = *updater.GetRecordingCanvas(); - const RSProperties& properties = node.GetRenderProperties(); - // skip shadow if not valid or cache is enabled - if (!properties.IsShadowValid() || canvas.GetCacheType() == Drawing::CacheType::ENABLED) { - return false; - } - Drawing::AutoCanvasRestore acr(canvas, true); - Drawing::Path path = RSPropertyDrawableUtils::CreateShadowPath(properties.GetShadowPath(), - properties.GetClipBounds(), properties.GetRRect()); - if (!properties.GetShadowIsFilled()) { - canvas.ClipPath(path, Drawing::ClipOp::DIFFERENCE, true); - } - // blurRadius calculation is based on the formula in Canvas::DrawShadow, 0.25f and 128.0f are constants - const Drawing::scalar blurRadius = - properties.GetShadowElevation() > 0.f - ? 0.25f * properties.GetShadowElevation() * (1 + properties.GetShadowElevation() / 128.0f) - : properties.GetShadowRadius(); - // save layer, draw image with clipPath, blur and draw back - Drawing::Brush blurBrush; - Drawing::Filter filter; - filter.SetImageFilter( - Drawing::ImageFilter::CreateBlurImageFilter(blurRadius, blurRadius, Drawing::TileMode::DECAL, nullptr)); - blurBrush.SetFilter(filter); - canvas.SaveLayer({ nullptr, &blurBrush }); - canvas.Translate(properties.GetShadowOffsetX(), properties.GetShadowOffsetY()); - canvas.ClipPath(path, Drawing::ClipOp::INTERSECT, false); - // draw node content as shadow - // [PLANNING]: maybe we should also draw background color / image here, and we should cache the shadow image - return true; -} - RSDrawable::Ptr RSMaskDrawable::OnGenerate(const RSRenderNode& node) { if (auto ret = std::make_shared(); ret->OnUpdate(node)) { diff --git a/rosen/modules/render_service_base/src/property/rs_properties.cpp b/rosen/modules/render_service_base/src/property/rs_properties.cpp index 6336b2c3ce..63106fb12c 100644 --- a/rosen/modules/render_service_base/src/property/rs_properties.cpp +++ b/rosen/modules/render_service_base/src/property/rs_properties.cpp @@ -2177,7 +2177,7 @@ float RSProperties::GetBackgroundBlurBrightness() const { return backgroundBlurBrightness_; } - + bool RSProperties::IsBackgroundBlurBrightnessValid() const { return (!ROSEN_EQ(GetBackgroundBlurBrightness(), 1.0f)) && ROSEN_GE(GetBackgroundBlurBrightness(), 0.0f); @@ -2193,7 +2193,7 @@ void RSProperties::SetBackgroundBlurMaskColor(Color backgroundMaskColor) SetDirty(); contentDirty_ = true; } - + const Color& RSProperties::GetBackgroundBlurMaskColor() const { return backgroundMaskColor_; @@ -2211,12 +2211,12 @@ void RSProperties::SetBackgroundBlurColorMode(int backgroundColorMode) SetDirty(); contentDirty_ = true; } - + int RSProperties::GetBackgroundBlurColorMode() const { return backgroundColorMode_; } - + void RSProperties::SetBackgroundBlurRadiusX(float backgroundBlurRadiusX) { backgroundBlurRadiusX_ = backgroundBlurRadiusX; @@ -2232,7 +2232,7 @@ float RSProperties::GetBackgroundBlurRadiusX() const { return backgroundBlurRadiusX_; } - + bool RSProperties::IsBackgroundBlurRadiusXValid() const { return ROSEN_GNE(GetBackgroundBlurRadiusX(), 0.999f); @@ -2248,12 +2248,12 @@ void RSProperties::SetBackgroundBlurRadiusY(float backgroundBlurRadiusY) SetDirty(); contentDirty_ = true; } - + float RSProperties::GetBackgroundBlurRadiusY() const { return backgroundBlurRadiusY_; } - + bool RSProperties::IsBackgroundBlurRadiusYValid() const { return ROSEN_GNE(GetBackgroundBlurRadiusY(), 0.999f); @@ -2290,7 +2290,7 @@ void RSProperties::SetForegroundBlurSaturation(float foregroundBlurSaturation) SetDirty(); contentDirty_ = true; } - + float RSProperties::GetForegroundBlurSaturation() const { return foregroundBlurSaturation_; @@ -2311,12 +2311,12 @@ void RSProperties::SetForegroundBlurBrightness(float foregroundBlurBrightness) SetDirty(); contentDirty_ = true; } - + float RSProperties::GetForegroundBlurBrightness() const { return foregroundBlurBrightness_; } - + bool RSProperties::IsForegroundBlurBrightnessValid() const { return ROSEN_GE(GetForegroundBlurBrightness(), 1.0); @@ -2332,7 +2332,7 @@ void RSProperties::SetForegroundBlurMaskColor(Color foregroundMaskColor) SetDirty(); contentDirty_ = true; } - + const Color& RSProperties::GetForegroundBlurMaskColor() const { return foregroundMaskColor_; @@ -2350,7 +2350,7 @@ void RSProperties::SetForegroundBlurColorMode(int foregroundColorMode) SetDirty(); contentDirty_ = true; } - + int RSProperties::GetForegroundBlurColorMode() const { return foregroundColorMode_; @@ -2371,7 +2371,7 @@ float RSProperties::GetForegroundBlurRadiusX() const { return foregroundBlurRadiusX_; } - + bool RSProperties::IsForegroundBlurRadiusXValid() const { return ROSEN_GNE(GetForegroundBlurRadiusX(), 0.999f); @@ -2387,12 +2387,12 @@ void RSProperties::SetForegroundBlurRadiusY(float foregroundBlurRadiusY) SetDirty(); contentDirty_ = true; } - + float RSProperties::GetForegroundBlurRadiusY() const { return foregroundBlurRadiusY_; } - + bool RSProperties::IsForegroundBlurRadiusYValid() const { return ROSEN_GNE(GetForegroundBlurRadiusY(), 0.999f); @@ -2463,7 +2463,7 @@ void RSProperties::GenerateBackgroundBlurFilter() backgroundFilter_ = originalFilter; backgroundFilter_->SetFilterType(RSFilter::BLUR); } - + void RSProperties::GenerateBackgroundMaterialBlurFilter() { if (backgroundColorMode_ == BLUR_COLOR_MODE::FASTAVERAGE) { @@ -2474,7 +2474,7 @@ void RSProperties::GenerateBackgroundMaterialBlurFilter() backgroundBlurSaturation_, backgroundBlurBrightness_); std::shared_ptr blurColorFilter = Drawing::ImageFilter::CreateColorBlurImageFilter(*colorFilter, backgroundBlurRadius_, backgroundBlurRadius_); - + std::shared_ptr originalFilter = nullptr; if (greyCoef_.has_value()) { std::shared_ptr greyShaderFilter = @@ -2510,7 +2510,7 @@ void RSProperties::GenerateBackgroundMaterialBlurFilter() maskColorShaderFilter->InitColorMod(); backgroundFilter_->SetFilterType(RSFilter::MATERIAL); } - + void RSProperties::GenerateForegroundBlurFilter() { std::shared_ptr blurFilter = Drawing::ImageFilter::CreateBlurImageFilter( @@ -2548,7 +2548,7 @@ void RSProperties::GenerateForegroundBlurFilter() filter_ = originalFilter; filter_->SetFilterType(RSFilter::BLUR); } - + void RSProperties::GenerateForegroundMaterialBlurFilter() { if (foregroundColorMode_ == BLUR_COLOR_MODE::FASTAVERAGE) { @@ -2559,9 +2559,9 @@ void RSProperties::GenerateForegroundMaterialBlurFilter() foregroundBlurSaturation_, foregroundBlurBrightness_); std::shared_ptr blurColorFilter = Drawing::ImageFilter::CreateColorBlurImageFilter(*colorFilter, foregroundBlurRadius_, foregroundBlurRadius_); - + std::shared_ptr originalFilter = nullptr; - + if (greyCoef_.has_value()) { std::shared_ptr greyShaderFilter = std::make_shared(greyCoef_->x_, greyCoef_->y_); @@ -2628,11 +2628,11 @@ void RSProperties::GenerateLinearGradientBlurFilter() auto linearBlurFilter = std::make_shared(linearGradientBlurPara_, frameGeo_->GetWidth(), frameGeo_->GetHeight()); std::shared_ptr originalFilter = std::make_shared(linearBlurFilter); - + filter_ = originalFilter; filter_->SetFilterType(RSFilter::LINEAR_GRADIENT_BLUR); } - + void RSProperties::GenerateBackgroundFilter() { if (aiInvert_.has_value() || systemBarEffect_) { @@ -2648,7 +2648,7 @@ void RSProperties::GenerateBackgroundFilter() ROSEN_LOGD("RSProperties::GenerateBackgroundFilter failed"); } } - + void RSProperties::GenerateForegroundFilter() { IfLinearGradientBlurInvalid(); @@ -3774,6 +3774,18 @@ void RSProperties::UpdateFilter() if (filter_ != nullptr && !filter_->IsValid()) { filter_.reset(); } + + UpdateForegroundFilter(); + + needFilter_ = backgroundFilter_ != nullptr || filter_ != nullptr || useEffect_ || IsLightUpEffectValid() || + IsDynamicLightUpValid() || greyCoef_.has_value() || linearGradientBlurPara_ != nullptr || + IsDynamicDimValid() || GetShadowColorStrategy() != SHADOW_COLOR_STRATEGY::COLOR_STRATEGY_NONE || + foregroundFilter_ != nullptr || motionBlurPara_ != nullptr || IsFgBrightnessValid() || + IsBgBrightnessValid() || foregroundFilterCache_ != nullptr; +} + +void RSProperties::UpdateForegroundFilter() +{ if (IsForegroundEffectRadiusValid()) { auto foregroundEffectFilter = std::make_shared(foregroundEffectRadius_); if (IS_UNI_RENDER) { @@ -3785,12 +3797,15 @@ void RSProperties::UpdateFilter() auto spherizeEffectFilter = std::make_shared(spherizeDegree_); foregroundFilter_ = spherizeEffectFilter; } else if (GetShadowMask()) { + foregroundFilter_.reset(); float elevation = GetShadowElevation(); Drawing::scalar n1 = 0.25f * elevation * (1 + elevation / 128.0f); // 0.25f 128.0f Drawing::scalar blurRadius = elevation > 0.0f ? n1 : GetShadowRadius(); - auto colorfulShadowFilter = - std::make_shared(blurRadius, GetShadowOffsetX(), GetShadowOffsetY()); - foregroundFilter_ = colorfulShadowFilter; + if (ROSEN_GE(blurRadius, 1.0)) { + auto colorfulShadowFilter = + std::make_shared(blurRadius, GetShadowOffsetX(), GetShadowOffsetY()); + foregroundFilter_ = colorfulShadowFilter; + } } else { foregroundFilter_.reset(); foregroundFilterCache_.reset(); @@ -3799,11 +3814,6 @@ void RSProperties::UpdateFilter() auto motionBlurFilter = std::make_shared(motionBlurPara_); foregroundFilter_ = motionBlurFilter; } - needFilter_ = backgroundFilter_ != nullptr || filter_ != nullptr || useEffect_ || IsLightUpEffectValid() || - IsDynamicLightUpValid() || greyCoef_.has_value() || linearGradientBlurPara_ != nullptr || - IsDynamicDimValid() || GetShadowColorStrategy() != SHADOW_COLOR_STRATEGY::COLOR_STRATEGY_NONE || - foregroundFilter_ != nullptr || motionBlurPara_ != nullptr || IsFgBrightnessValid() || - IsBgBrightnessValid() || foregroundFilterCache_ != nullptr; } void RSProperties::CalculatePixelStretch() diff --git a/rosen/modules/render_service_base/src/render/rs_colorful_shadow_filter.cpp b/rosen/modules/render_service_base/src/render/rs_colorful_shadow_filter.cpp index 2e1115b4af..eb8f471cd4 100644 --- a/rosen/modules/render_service_base/src/render/rs_colorful_shadow_filter.cpp +++ b/rosen/modules/render_service_base/src/render/rs_colorful_shadow_filter.cpp @@ -42,6 +42,11 @@ bool RSColorfulShadowFilter::IsValid() const void RSColorfulShadowFilter::DrawImageRect(Drawing::Canvas &canvas, const std::shared_ptr &image, const Drawing::Rect &src, const Drawing::Rect &dst) const { + if (image == nullptr) { + ROSEN_LOGE("RSColorfulShadowFilter::DrawImageRect error"); + return; + } + // draw blur image canvas.Translate(offsetX_, offsetY_); RSForegroundEffectFilter::DrawImageRect(canvas, image, src, dst);