From 9b150f134b9b6ba103ec305f489645b5fee905d2 Mon Sep 17 00:00:00 2001 From: gaaclarke <30870216+gaaclarke@users.noreply.github.com> Date: Fri, 26 Jul 2019 10:05:40 -0700 Subject: [PATCH] Sped up shader warmup by only drawing on a 100x100 surface (#36482) Sped up shader warmup by only drawing on a 100x100 surface and doing the draw calls on top of each other. --- examples/layers/raw/shader_warm_up.dart | 4 ++- .../lib/src/painting/shader_warm_up.dart | 31 +++++++++++++------ 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/examples/layers/raw/shader_warm_up.dart b/examples/layers/raw/shader_warm_up.dart index f2b4aed01b..569c6c2119 100644 --- a/examples/layers/raw/shader_warm_up.dart +++ b/examples/layers/raw/shader_warm_up.dart @@ -16,7 +16,9 @@ Future beginFrame(Duration timeStamp) async { final ui.Canvas canvas = ui.Canvas(recorder, paintBounds); final ui.Paint backgroundPaint = ui.Paint()..color = Colors.white; canvas.drawRect(paintBounds, backgroundPaint); - await const DefaultShaderWarmUp().warmUpOnCanvas(canvas); + await const DefaultShaderWarmUp( + drawCallSpacing: 80.0, canvasSize: ui.Size(1024, 1024)) + .warmUpOnCanvas(canvas); final ui.Picture picture = recorder.endRecording(); // COMPOSITE diff --git a/packages/flutter/lib/src/painting/shader_warm_up.dart b/packages/flutter/lib/src/painting/shader_warm_up.dart index d450a7ba84..9c7f308964 100644 --- a/packages/flutter/lib/src/painting/shader_warm_up.dart +++ b/packages/flutter/lib/src/painting/shader_warm_up.dart @@ -56,12 +56,12 @@ abstract class ShaderWarmUp { /// The size of the warm up image. /// - /// The exact size shouldn't matter much as long as it's not too far away from - /// the target device's screen. 1024x1024 is a good choice as it is within an - /// order of magnitude of most devices. + /// The exact size shouldn't matter much as long as all draws are onscreen. + /// 100x100 is an arbitrary small size that's easy to fit significant draw + /// calls onto. /// /// A custom shader warm up can override this based on targeted devices. - ui.Size get size => const ui.Size(1024.0, 1024.0); + ui.Size get size => const ui.Size(100.0, 100.0); /// Trigger draw operations on a given canvas to warm up GPU shader /// compilation cache. @@ -99,7 +99,20 @@ abstract class ShaderWarmUp { /// issues seen so far. class DefaultShaderWarmUp extends ShaderWarmUp { /// Allow [DefaultShaderWarmUp] to be used as the default value of parameters. - const DefaultShaderWarmUp(); + const DefaultShaderWarmUp( + {this.drawCallSpacing = 0.0, + this.canvasSize = const ui.Size(100.0, 100.0)}); + + /// Constant that can be used to space out draw calls for visualizing the draws + /// for debugging purposes (example: 80.0). Be sure to also change your canvas + /// size. + final double drawCallSpacing; + + /// Value that returned by this.size to control canvas size where draws happen. + final ui.Size canvasSize; + + @override + ui.Size get size => canvasSize; /// Trigger common draw operations on a canvas to warm up GPU shader /// compilation cache. @@ -157,22 +170,22 @@ class DefaultShaderWarmUp extends ShaderWarmUp { canvas.save(); for (ui.Paint paint in paints) { canvas.drawPath(paths[i], paint); - canvas.translate(80.0, 0.0); + canvas.translate(drawCallSpacing, 0.0); } canvas.restore(); - canvas.translate(0.0, 80.0); + canvas.translate(0.0, drawCallSpacing); } // Warm up shadow shaders. const ui.Color black = ui.Color(0xFF000000); canvas.save(); canvas.drawShadow(rrectPath, black, 10.0, true); - canvas.translate(80.0, 0.0); + canvas.translate(drawCallSpacing, 0.0); canvas.drawShadow(rrectPath, black, 10.0, false); canvas.restore(); // Warm up text shaders. - canvas.translate(0.0, 80.0); + canvas.translate(0.0, drawCallSpacing); final ui.ParagraphBuilder paragraphBuilder = ui.ParagraphBuilder( ui.ParagraphStyle(textDirection: ui.TextDirection.ltr), )..pushStyle(ui.TextStyle(color: black))..addText('_');