Repository: flutter
Issue: Significant Android rendering performance issue using Path.addPath()
Labels: platform-android,engine,a: animation,c: performance,dependency: skia,perf: speed,P2,team-android,triaged-android

<!-- Thank you for using Flutter!

     If you are looking for support, please check out our documentation
     or consider asking a question on Stack Overflow:
      * https://flutter.dev/
      * https://api.flutter.dev/
      * https://stackoverflow.com/questions/tagged/flutter?sort=frequent

     If you have found a performance problem, then fill out the template below.
     Please read our guide to filing a bug first: https://flutter.dev/docs/resources/bug-reports
-->

## Details

<!--
1.  Please tell us exactly how to reproduce the problem you are running into.

2.  Please attach a small application (ideally just one main.dart file) that
     reproduces the problem. You could use https://gist.github.com/ for this.

3.  Switch flutter to master channel and run this app on a physical device
     using profile mode with Skia tracing enabled, as follows:
       flutter channel master
       flutter run --profile --trace-skia
     Then press ‘P’ to enable the performance overlay.

     The bleeding edge master channel is encouraged here because Flutter is
     constantly fixing bugs and improving its performance. Your problem in an
     older Flutter version may have already been solved in the master channel.

4.  Record a video of the performance issue using another phone so we
     can have an intuitive understanding of what happened. Don’t use
     "adb screenrecord", as that affects the performance of the profile run.

5.  Open Observatory and save a timeline trace of the performance issue
     so we know which functions might be causing it. See "How to Collect
     and Read Timeline Traces" on this blog post:
       https://medium.com/flutter/profiling-flutter-applications-using-the-timeline-a1a434964af3#a499
-->
There seems to be an issue when drawing to a Canvas with Paths which have sub-paths added by `Path.addPath()`.  The problem manifests in the Raster thread. I originally found the issue using the lottie-flutter package (https://github.com/xvrh/lottie-flutter) with a moderately-sized lottie file. However, I have created a demo app which does not depend on lottie-flutter - you can find it here: https://gist.github.com/dsyrstad/14274fe5bb2c2da4572b31316ca5c1bd.

The demo app initially starts out demonstrating the `addPath()` issue. You'll see 4 radio buttons and a small rectangle pulsing. The pulsing rectangle is actually 150 congruent filled rectangles drawn on top of each other. The first radio button "DrawingMethod.AddPath" is selected and shows the performance issue. On my Pixel 3 XL, the UI thread is averaging 1.8ms/frame but the Raster thread is consuming ~34ms/frame. It is definitely losing frames.

If you tap "DrawingMethod.AddPathWithMoveTo", it is drawing with the same method as above, except `Path.moveTo(0, 0)` is called before each `addPath()` call. For some reason, this innocuous call speeds up the Render frame timings significantly - down to ~9.6ms.

"DrawingMethod.AddPathWithNoAA" draws using the same method, except it uses a `Paint` with `isAntiAlias` set to `false`. This cuts the Render thread frame times down to ~5.8ms.

"DrawingMethod.DirectToCanvas" draws the 150 rectangles, but does not use `addPath()`. It also gets Render thread frame times of ~5.8ms.

All of these timings seem to depend on which way the wind is blowing and switching between the radio buttons will produce different results the longer you run the app. 

I'm running in profile mode with `--trace-skia` using the master channel as of 2020-09-03. I have the performance overlay turned on. 

You can find a video here: 
https://drive.google.com/file/d/14DakzThpd9Axexk0uE4tpnPTINZDDbwr/view?usp=sharing

The Observatory timeline trace is here: 
https://drive.google.com/file/d/1bP2CbLjcR_1TyqnQlLdTkBqC_SpMMC5i/view?usp=sharing

<!--
     Please tell us which target platform(s) the problem occurs (Android / iOS / Web / macOS / Linux / Windows)
     Which target OS version, for Web, browser, is the test system running?
     Does the problem occur on emulator/simulator as well as on physical devices?
-->

**Target Platform:** Android
**Target OS version/browser:** Android 10
**Devices:** Pixel 3 XL

## Logs

<!--
     Run `flutter analyze` and attach any output of that command below.
     If there are any analysis errors, try resolving them before filing this issue.
-->

<details>
  <summary>flutter analyze</summary>
  
```
Running "flutter pub get" in flutter_add_path_bug...                0.9s
Analyzing flutter_add_path_bug...                                       
No issues found! (ran in 5.6s)
```
</details>

<!-- Finally, paste the output of running `flutter doctor -v` here, with your device plugged in. -->

<details>
  <summary>flutter doctor -v</summary>
  
```
[✓] Flutter (Channel master, 1.22.0-10.0.pre.77, on Linux, locale en_US.UTF-8)
    • Flutter version 1.22.0-10.0.pre.77 at /home/dsyrstad/Flutter/flutter
    • Framework revision 34c977f610 (68 minutes ago), 2020-09-03 13:49:26 -0700
    • Engine revision 165abef0a2
    • Dart version 2.10.0 (build 2.10.0-77.0.dev)

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at /home/dsyrstad/Android/Sdk
    • Platform android-29, build-tools 29.0.2
    • ANDROID_SDK_ROOT = /home/dsyrstad/Android/Sdk
    • Java binary at: /home/dsyrstad/AndroidStudio/android-studio/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
    • All Android licenses accepted.

[✓] Android Studio (version 3.5)
    • Android Studio at /home/dsyrstad/AndroidStudio/android-studio
    • Flutter plugin version 42.1.1
    • Dart plugin version 191.8593
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)

[✓] IntelliJ IDEA Ultimate Edition (version 2019.3)
    • IntelliJ at /home/dsyrstad/IntelliJ/idea-latest
    • Flutter plugin version 45.1.2
    • Dart plugin version 193.6911.31

[✓] IntelliJ IDEA Ultimate Edition (version 2019.1)
    • IntelliJ at /home/dsyrstad/IntelliJ/idea-IU-181.4668.68
    • Flutter plugin version 39.0.3
    • Dart plugin version 191.8423

[✓] Connected device (1 available)
    • Pixel 3 XL (mobile) • 192.168.86.28:5555 • android-arm64 • Android 10 (API 29)

• No issues found!
```
</details>