This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Improve check to render (or not) a DRRect when inner falls outside of outer on RecordingCanvas #12229
Merged
Merged
Improve check to render (or not) a DRRect when inner falls outside of outer on RecordingCanvas #12229
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1b2555b
Add check to determine if a rrect is fully contained within another.
ditman cebffb8
Move rounded rect check to recording_canvas, and undo other changes
ditman 8677ae4
Add drawDRRect RecordingCanvas test
ditman d2ef8b0
Reduce object allocation compared to prior approach
ditman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:ui/ui.dart'; | ||
| import 'package:ui/src/engine.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| import '../mock_engine_canvas.dart'; | ||
|
|
||
| void main() { | ||
|
|
||
| RecordingCanvas underTest; | ||
| MockEngineCanvas mockCanvas; | ||
|
|
||
| setUp(() { | ||
| underTest = RecordingCanvas(Rect.largest); | ||
| mockCanvas = MockEngineCanvas(); | ||
| }); | ||
|
|
||
| group('drawDRRect', () { | ||
| final RRect rrect = RRect.fromLTRBR(10, 10, 50, 50, Radius.circular(3)); | ||
| final Paint somePaint = Paint()..color = const Color(0xFFFF0000); | ||
| test('Happy case', () { | ||
| underTest.drawDRRect(rrect, rrect.deflate(1), somePaint); | ||
| underTest.apply(mockCanvas); | ||
| // Expect drawDRRect to be called | ||
| expect(mockCanvas.methodCallLog.length, equals(1)); | ||
| MockCanvasCall mockCall = mockCanvas.methodCallLog[0]; | ||
| expect(mockCall.methodName, equals('drawDRRect')); | ||
| expect(mockCall.arguments, equals({ | ||
| 'outer': rrect, | ||
| 'inner': rrect.deflate(1), | ||
| 'paint': somePaint.webOnlyPaintData, | ||
| })); | ||
| }); | ||
|
|
||
| test('Inner RRect > Outer RRect', () { | ||
| underTest.drawDRRect(rrect, rrect.inflate(1), somePaint); | ||
| underTest.apply(mockCanvas); | ||
| // Expect nothing to be called | ||
| expect(mockCanvas.methodCallLog.length, equals(0)); | ||
| }); | ||
|
|
||
| test('Inner RRect not completely inside Outer RRect', () { | ||
| underTest.drawDRRect(rrect, rrect.deflate(1).shift(const Offset(0.0, 10)), somePaint); | ||
| underTest.apply(mockCanvas); | ||
| // Expect nothing to be called | ||
| expect(mockCanvas.methodCallLog.length, equals(0)); | ||
| }); | ||
|
|
||
| test('Inner RRect same as Outer RRect', () { | ||
| underTest.drawDRRect(rrect, rrect, somePaint); | ||
| underTest.apply(mockCanvas); | ||
| // Expect nothing to be called | ||
| expect(mockCanvas.methodCallLog.length, equals(0)); | ||
| }); | ||
| }); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the happy case when the drrect is properly specified, this check will allocate 8
ui.Offsetobjects. Is it possible to optimize that away?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/cc @rakudrama Am I being too paranoid here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can try with a different check, but I'm not sure it'll be geometrically precise, let me give that a quick shot, I can always come back to this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't mean rewrite the check. I mean do exactly the same math without allocation. Or is there something that prevents you from doing that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The API of contains is what it is, and once the radius information is used to construct the RRect, everything is stored as doubles, so I don't see any way of computing the offsets of each of the points of the corner without allocating an Offset. Also Offsets seem to be immutable, so even if I try to move it around, it'll return a new instance all the time, right?
Anyway, I wrote a slightly different revision that does the same with a little bit less allocation (but in a slightly less compact code).