Skip to content

Commit 3392c91

Browse files
authored
Avoid unnecessary work in BoxDecoration and RenderPhysicalModel (flutter#187891)
Part of flutter#187894 Improve `box_decoration.dart` In Flutter’s codebase that pattern already exists in similar hit-test code (RenderClipOval.hitTest https://github.com/flutter/flutter/blob/8c17c5e0677a5c923a9f606d8963e2e0350e6ec2/packages/flutter/lib/src/rendering/proxy_box.dart#L1932). Old code: ```dart final double distance = (position - center).distance; return distance <= math.min(size.width, size.height) / 2.0; ``` Offset.distance computes: `sqrt(dx * dx + dy * dy)` --- New code: ```dart final double radius = math.min(size.width, size.height) / 2.0; return (position - center).distanceSquared <= radius * radius; ``` Offset.distanceSquared computes only: `dx * dx + dy * dy` Then it compares that to radius * radius. Mathematically: `sqrt(dx² + dy²) <= r` is equivalent to: `dx² + dy² <= r²` Then, I also found an issue: `elevated` shadow path doesn't need to be initialized when there is no elevation to use it for.
1 parent 9d98366 commit 3392c91

4 files changed

Lines changed: 33 additions & 3 deletions

File tree

‎packages/flutter/lib/src/painting/box_decoration.dart‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,9 @@ class BoxDecoration extends Decoration {
384384
case BoxShape.circle:
385385
// Circles are inscribed into our smallest dimension.
386386
final Offset center = size.center(Offset.zero);
387-
final double distance = (position - center).distance;
388-
return distance <= math.min(size.width, size.height) / 2.0;
387+
final double radius = math.min(size.width, size.height) / 2.0;
388+
// Comparing squared distances avoids computing sqrt(dx * dx + dy * dy).
389+
return (position - center).distanceSquared <= radius * radius;
389390
}
390391
}
391392

‎packages/flutter/lib/src/rendering/proxy_box.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2210,7 +2210,6 @@ class RenderPhysicalModel extends _RenderPhysicalModelBase<RRect> {
22102210

22112211
_updateClip();
22122212
final RRect offsetRRect = _clip!.shift(offset);
2213-
final offsetRRectAsPath = Path()..addRRect(offsetRRect);
22142213
var paintShadows = true;
22152214
assert(() {
22162215
if (debugDisableShadows) {
@@ -2230,6 +2229,7 @@ class RenderPhysicalModel extends _RenderPhysicalModelBase<RRect> {
22302229

22312230
final Canvas canvas = context.canvas;
22322231
if (elevation != 0.0 && paintShadows) {
2232+
final offsetRRectAsPath = Path()..addRRect(offsetRRect);
22332233
canvas.drawShadow(offsetRRectAsPath, shadowColor, elevation, color.alpha != 0xFF);
22342234
}
22352235
final usesSaveLayer = clipBehavior == Clip.antiAliasWithSaveLayer;

‎packages/flutter/test/painting/box_decoration_test.dart‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,16 @@ void main() {
174174
expect(clipPath, isLookLikeExpectedPath);
175175
});
176176

177+
test('BoxDecoration.hitTest with shape BoxShape.circle', () {
178+
const decoration = BoxDecoration(shape: BoxShape.circle);
179+
const size = Size(100.0, 20.0);
180+
181+
expect(decoration.hitTest(size, const Offset(50.0, 0.0)), isTrue);
182+
expect(decoration.hitTest(size, const Offset(40.0, 10.0)), isTrue);
183+
expect(decoration.hitTest(size, const Offset(40.0, 0.0)), isFalse);
184+
expect(decoration.hitTest(size, const Offset(10.0, 10.0)), isFalse);
185+
});
186+
177187
test('BoxDecorations with different blendModes are not equal', () {
178188
// Regression test for https://github.com/flutter/flutter/issues/100754.
179189
const one = BoxDecoration(color: Color(0x00000000), backgroundBlendMode: BlendMode.color);

‎packages/flutter/test/rendering/proxy_box_test.dart‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,25 @@ void main() {
7878
expect(root.needsCompositing, isFalse);
7979
});
8080

81+
test('RenderPhysicalModel paints shadow only for non-zero elevation', () {
82+
final root = RenderPhysicalModel(
83+
color: const Color(0xffff00ff),
84+
child: RenderSizedBox(const Size(10.0, 10.0)),
85+
);
86+
layout(root, phase: EnginePhase.paint);
87+
expect(
88+
(PaintingContext context, Offset offset) => root.paint(context, offset),
89+
paintsExactlyCountTimes(#drawShadow, 0),
90+
);
91+
92+
root.elevation = 1.0;
93+
pumpFrame(phase: EnginePhase.paint);
94+
expect(
95+
(PaintingContext context, Offset offset) => root.paint(context, offset),
96+
paintsExactlyCountTimes(#drawShadow, 1),
97+
);
98+
});
99+
81100
test('RenderSemanticsGestureHandler adds/removes correct semantic actions', () {
82101
final renderObj = RenderSemanticsGestureHandler(
83102
onTap: () {},

0 commit comments

Comments
 (0)