From bb78d9043eb071428682334d20cdd5168d0c91b4 Mon Sep 17 00:00:00 2001 From: Xiaodan Zhu Date: Mon, 20 Nov 2023 22:38:40 +0000 Subject: [PATCH] jellyroll shadow: uneven rounded corners part 1 This cl implements the uneven rounded corners for texture layer based shadow. The shadow based on nine patch layer will be implemented in following cls. Test in style viewer. Bug: b:307326019 Change-Id: I747abd73f0b322fe2fb781c14811599ab2e6e09b Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5041718 Commit-Queue: Xiaodan Zhu Reviewed-by: Sean Kau Cr-Commit-Position: refs/heads/main@{#1227073} --- ash/style/system_shadow.h | 10 ++++++++ .../system_shadow_on_nine_patch_layer.cc | 8 +++++++ ash/style/system_shadow_on_nine_patch_layer.h | 1 + ash/style/system_shadow_on_texture_layer.cc | 23 ++++++++++++++++--- ash/style/system_shadow_on_texture_layer.h | 3 ++- 5 files changed, 41 insertions(+), 4 deletions(-) diff --git a/ash/style/system_shadow.h b/ash/style/system_shadow.h index 83602e8b97c3ac..6ef1c44998772c 100644 --- a/ash/style/system_shadow.h +++ b/ash/style/system_shadow.h @@ -8,6 +8,7 @@ #include "ash/ash_export.h" #include "ui/color/color_provider_source_observer.h" #include "ui/gfx/geometry/rect.h" +#include "ui/gfx/geometry/rounded_corners_f.h" #include "ui/gfx/shadow_value.h" namespace aura { @@ -87,8 +88,17 @@ class ASH_EXPORT SystemShadow : public ui::ColorProviderSourceObserver { virtual void SetContentBounds(const gfx::Rect& bounds) = 0; + // TODO(http://b/307326019): Deprecate this method when all shadow + // implementations use `gfx::RoundedCornersF`. virtual void SetRoundedCornerRadius(int corner_radius) = 0; + // TODO(http://b/307326019): This is only used for + // `SystemShadowOnTextureLayer` for now. Should be applied to + // `SystemShadowOnNinePatchLayer` when `ui::Shadow` is able to use + // `gfx::RoundedCornersF`. + virtual void SetRoundedCorners( + const gfx::RoundedCornersF& rounded_corners) = 0; + virtual const gfx::Rect& GetContentBounds() = 0; // Return the layer of the shadow. This function can be used by any types of diff --git a/ash/style/system_shadow_on_nine_patch_layer.cc b/ash/style/system_shadow_on_nine_patch_layer.cc index af5a909b132529..c6ac4f5c25e511 100644 --- a/ash/style/system_shadow_on_nine_patch_layer.cc +++ b/ash/style/system_shadow_on_nine_patch_layer.cc @@ -30,6 +30,14 @@ void SystemShadowOnNinePatchLayer::SetRoundedCornerRadius(int corner_radius) { shadow()->SetRoundedCornerRadius(corner_radius); } +void SystemShadowOnNinePatchLayer::SetRoundedCorners( + const gfx::RoundedCornersF& rounded_corners) { + // TODO(http://b/307326019): use corresponding interface of `ui::Shadow` when + // available. + NOTREACHED() << "Setting uneven rounded corners to the shadow on nine patch " + "layer is not ready."; +} + const gfx::Rect& SystemShadowOnNinePatchLayer::GetContentBounds() { return shadow()->content_bounds(); } diff --git a/ash/style/system_shadow_on_nine_patch_layer.h b/ash/style/system_shadow_on_nine_patch_layer.h index 0ca7211390b6dc..f00e4216db8272 100644 --- a/ash/style/system_shadow_on_nine_patch_layer.h +++ b/ash/style/system_shadow_on_nine_patch_layer.h @@ -32,6 +32,7 @@ class SystemShadowOnNinePatchLayer : public SystemShadow { void SetType(SystemShadow::Type type) override; void SetContentBounds(const gfx::Rect& bounds) override; void SetRoundedCornerRadius(int corner_radius) override; + void SetRoundedCorners(const gfx::RoundedCornersF& rounded_corners) override; const gfx::Rect& GetContentBounds() override; ui::Layer* GetLayer() override; ui::Layer* GetNinePatchLayer() override; diff --git a/ash/style/system_shadow_on_texture_layer.cc b/ash/style/system_shadow_on_texture_layer.cc index 4d529a6a89744f..aaa13441c5dfa7 100644 --- a/ash/style/system_shadow_on_texture_layer.cc +++ b/ash/style/system_shadow_on_texture_layer.cc @@ -35,7 +35,16 @@ void SystemShadowOnTextureLayer::SetContentBounds(const gfx::Rect& bounds) { } void SystemShadowOnTextureLayer::SetRoundedCornerRadius(int corner_radius) { - corner_radius_ = corner_radius; + SetRoundedCorners(gfx::RoundedCornersF(corner_radius)); +} + +void SystemShadowOnTextureLayer::SetRoundedCorners( + const gfx::RoundedCornersF& rounded_corners) { + if (rounded_corners_ == rounded_corners) { + return; + } + + rounded_corners_ = rounded_corners; UpdateLayer(); } @@ -83,8 +92,16 @@ void SystemShadowOnTextureLayer::OnPaintLayer(const ui::PaintContext& context) { // Create a rounded rect of content area. const gfx::Rect r_rect_bounds = content_bounds_ - GetLayerBounds().OffsetFromOrigin(); - const SkRRect r_rect = SkRRect::MakeRectXY(gfx::RectToSkRect(r_rect_bounds), - corner_radius_, corner_radius_); + SkRRect r_rect; + float upper_left = rounded_corners_.upper_left(); + float upper_right = rounded_corners_.upper_right(); + float lower_right = rounded_corners_.lower_right(); + float lower_left = rounded_corners_.lower_left(); + SkVector radii[4] = {{upper_left, upper_left}, + {upper_right, upper_right}, + {lower_right, lower_right}, + {lower_left, lower_left}}; + r_rect.setRectRadii(gfx::RectToSkRect(r_rect_bounds), radii); // Clip out the center. ui::PaintRecorder recorder(context, content_bounds_.size()); diff --git a/ash/style/system_shadow_on_texture_layer.h b/ash/style/system_shadow_on_texture_layer.h index 76ff55a4b743ce..8661f3ad4659b6 100644 --- a/ash/style/system_shadow_on_texture_layer.h +++ b/ash/style/system_shadow_on_texture_layer.h @@ -39,6 +39,7 @@ class SystemShadowOnTextureLayer : public SystemShadow, void SetType(SystemShadow::Type type) override; void SetContentBounds(const gfx::Rect& bounds) override; void SetRoundedCornerRadius(int corner_radius) override; + void SetRoundedCorners(const gfx::RoundedCornersF& rounded_corners) override; const gfx::Rect& GetContentBounds() override; ui::Layer* GetLayer() override; ui::Layer* GetNinePatchLayer() override; @@ -69,7 +70,7 @@ class SystemShadowOnTextureLayer : public SystemShadow, gfx::ShadowValues shadow_values_; // The bounds of the content area. gfx::Rect content_bounds_; - int corner_radius_ = 0; + gfx::RoundedCornersF rounded_corners_; // The shadow colors map. ui::Shadow::ElevationToColorsMap colors_map_; };