diff --git a/dev/benchmarks/macrobenchmarks/lib/common.dart b/dev/benchmarks/macrobenchmarks/lib/common.dart index ad04770adb..ee6407e010 100644 --- a/dev/benchmarks/macrobenchmarks/lib/common.dart +++ b/dev/benchmarks/macrobenchmarks/lib/common.dart @@ -37,6 +37,8 @@ const String kListTextLayoutRouteName = '/list_text_layout'; const String kAnimatedBlurBackdropFilter = '/animated_blur_backdrop_filter'; const String kSlidersRouteName = '/sliders'; const String kDrawPointsPageRougeName = '/draw_points'; +const String kDrawVerticesPageRouteName = '/draw_vertices'; +const String kDrawAtlasPageRouteName = '/draw_atlas'; const String kOpacityPeepholeOneRectRouteName = '$kOpacityPeepholeRouteName/one_big_rect'; const String kOpacityPeepholeColumnOfOpacityRouteName = '$kOpacityPeepholeRouteName/column_of_opacity'; diff --git a/dev/benchmarks/macrobenchmarks/lib/main.dart b/dev/benchmarks/macrobenchmarks/lib/main.dart index 64e37e1224..a74b50732c 100644 --- a/dev/benchmarks/macrobenchmarks/lib/main.dart +++ b/dev/benchmarks/macrobenchmarks/lib/main.dart @@ -18,7 +18,9 @@ import 'src/color_filter_cache.dart'; import 'src/color_filter_with_unstable_child.dart'; import 'src/cubic_bezier.dart'; import 'src/cull_opacity.dart'; +import 'src/draw_atlas.dart'; import 'src/draw_points.dart'; +import 'src/draw_vertices.dart'; import 'src/filtered_child_animation.dart'; import 'src/fullscreen_textfield.dart'; import 'src/gradient_perf.dart'; @@ -91,6 +93,8 @@ class MacrobenchmarksApp extends StatelessWidget { kAnimatedBlurBackdropFilter: (BuildContext context) => const AnimatedBlurBackdropFilter(), kSlidersRouteName: (BuildContext context) => const SlidersPage(), kDrawPointsPageRougeName: (BuildContext context) => const DrawPointsPage(), + kDrawVerticesPageRouteName: (BuildContext context) => const DrawVerticesPage(), + kDrawAtlasPageRouteName: (BuildContext context) => const DrawAtlasPage(), }, ); } @@ -345,7 +349,21 @@ class HomePage extends StatelessWidget { onPressed: () { Navigator.pushNamed(context, kDrawPointsPageRougeName); }, - ) + ), + ElevatedButton( + key: const Key(kDrawVerticesPageRouteName), + child: const Text('Draw Vertices'), + onPressed: () { + Navigator.pushNamed(context, kDrawVerticesPageRouteName); + }, + ), + ElevatedButton( + key: const Key(kDrawAtlasPageRouteName), + child: const Text('Draw Atlas'), + onPressed: () { + Navigator.pushNamed(context, kDrawAtlasPageRouteName); + }, + ), ], ), ); diff --git a/dev/benchmarks/macrobenchmarks/lib/src/draw_atlas.dart b/dev/benchmarks/macrobenchmarks/lib/src/draw_atlas.dart new file mode 100644 index 0000000000..742301fb36 --- /dev/null +++ b/dev/benchmarks/macrobenchmarks/lib/src/draw_atlas.dart @@ -0,0 +1,116 @@ +// Copyright 2014 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 'dart:ui' as ui; + +import 'package:flutter/material.dart'; + +Future loadImage(String asset) async { + final ui.ImmutableBuffer buffer = await ui.ImmutableBuffer.fromAsset(asset); + final ui.Codec codec = await PaintingBinding.instance.instantiateImageCodecWithSize(buffer); + final ui.FrameInfo frameInfo = await codec.getNextFrame(); + return frameInfo.image; +} + +class DrawAtlasPage extends StatefulWidget { + const DrawAtlasPage({super.key}); + + @override + State createState() => _DrawAtlasPageState(); +} + +class _DrawAtlasPageState extends State with SingleTickerProviderStateMixin { + late final AnimationController controller; + double tick = 0.0; + ui.Image? image; + + @override + void initState() { + super.initState(); + loadImage('packages/flutter_gallery_assets/food/butternut_squash_soup.png').then((ui.Image pending) { + setState(() { + image = pending; + }); + }); + controller = AnimationController(vsync: this, duration: const Duration(hours: 1)); + controller.addListener(() { + setState(() { + tick += 1; + }); + }); + controller.forward(from: 0); + } + + @override + void dispose() { + controller.dispose(); + super.dispose(); + } + + + @override + Widget build(BuildContext context) { + if (image == null) { + return const Placeholder(); + } + return CustomPaint( + size: const Size(500, 500), + painter: VerticesPainter(tick, image!), + child: Container(), + ); + } +} + +class VerticesPainter extends CustomPainter { + VerticesPainter(this.tick, this.image); + + final double tick; + final ui.Image image; + + @override + void paint(Canvas canvas, Size size) { + canvas.translate(0, tick); + canvas.drawAtlas( + image, + [RSTransform.fromComponents(rotation: 0, scale: 1, anchorX: 0, anchorY: 0, translateX: 0, translateY: 0)], + [Rect.fromLTWH(0, 0, image.width.toDouble(), image.height.toDouble())], + [Colors.red], + BlendMode.plus, + null, + Paint() + ); + canvas.drawAtlas( + image, + [RSTransform.fromComponents(rotation: 0, scale: 1, anchorX: 0, anchorY: 0, translateX: 250, translateY: 0)], + [Rect.fromLTWH(0, 0, image.width.toDouble(), image.height.toDouble())], + [Colors.green], + BlendMode.plus, + null, + Paint() + ); + canvas.drawAtlas( + image, + [RSTransform.fromComponents(rotation: 0, scale: 1, anchorX: 0, anchorY: 0, translateX: 0, translateY: 250)], + [Rect.fromLTWH(0, 0, image.width.toDouble(), image.height.toDouble())], + [Colors.blue], + BlendMode.plus, + null, + Paint() + ); + canvas.drawAtlas( + image, + [RSTransform.fromComponents(rotation: 0, scale: 1, anchorX: 0, anchorY: 0, translateX: 250, translateY: 250)], + [Rect.fromLTWH(0, 0, image.width.toDouble(), image.height.toDouble())], + [Colors.yellow], + BlendMode.plus, + null, + Paint() + ); + } + + @override + bool shouldRepaint(covariant CustomPainter oldDelegate) { + return true; + } +} diff --git a/dev/benchmarks/macrobenchmarks/lib/src/draw_vertices.dart b/dev/benchmarks/macrobenchmarks/lib/src/draw_vertices.dart new file mode 100644 index 0000000000..669919d4bc --- /dev/null +++ b/dev/benchmarks/macrobenchmarks/lib/src/draw_vertices.dart @@ -0,0 +1,114 @@ +// Copyright 2014 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 'dart:ui' as ui; + +import 'package:flutter/material.dart'; + +Future loadImage(String asset) async { + final ui.ImmutableBuffer buffer = await ui.ImmutableBuffer.fromAsset(asset); + final ui.Codec codec = await PaintingBinding.instance.instantiateImageCodecWithSize(buffer); + final ui.FrameInfo frameInfo = await codec.getNextFrame(); + return frameInfo.image; +} + +class DrawVerticesPage extends StatefulWidget { + const DrawVerticesPage({super.key}); + + @override + State createState() => _DrawVerticesPageState(); +} + +class _DrawVerticesPageState extends State with SingleTickerProviderStateMixin { + late final AnimationController controller; + double tick = 0.0; + ui.Image? image; + + @override + void initState() { + super.initState(); + loadImage('packages/flutter_gallery_assets/food/butternut_squash_soup.png').then((ui.Image pending) { + setState(() { + image = pending; + }); + }); + controller = AnimationController(vsync: this, duration: const Duration(hours: 1)); + controller.addListener(() { + setState(() { + tick += 1; + }); + }); + controller.forward(from: 0); + } + + @override + void dispose() { + controller.dispose(); + super.dispose(); + } + + + @override + Widget build(BuildContext context) { + if (image == null) { + return const Placeholder(); + } + return CustomPaint( + size: const Size(500, 500), + painter: VerticesPainter(tick, image!), + child: Container(), + ); + } +} + +class VerticesPainter extends CustomPainter { + VerticesPainter(this.tick, this.image); + + final double tick; + final ui.Image image; + + @override + void paint(Canvas canvas, Size size) { + canvas.translate(0, tick); + final ui.Vertices vertices = ui.Vertices( + VertexMode.triangles, + const [ + Offset.zero, + Offset(0, 250), + Offset(250, 0), + Offset(0, 250), + Offset(250, 0), + Offset(250, 250) + ], + textureCoordinates: [ + Offset.zero, + Offset(0, image.height.toDouble()), + Offset(image.width.toDouble(), 0), + Offset(0, image.height.toDouble()), + Offset(image.width.toDouble(), 0), + Offset(image.width.toDouble(), image.height.toDouble()) + ], + colors: [ + Colors.red, + Colors.blue, + Colors.green, + Colors.red, + Colors.blue, + Colors.green, + ] + ); + canvas.drawVertices(vertices, BlendMode.plus, Paint()..shader = ImageShader(image, TileMode.clamp, TileMode.clamp, Matrix4.identity().storage)); + canvas.translate(250, 0); + canvas.drawVertices(vertices, BlendMode.plus, Paint()..shader = ImageShader(image, TileMode.clamp, TileMode.clamp, Matrix4.identity().storage)); + canvas.translate(0, 250); + canvas.drawVertices(vertices, BlendMode.plus, Paint()..shader = ImageShader(image, TileMode.clamp, TileMode.clamp, Matrix4.identity().storage)); + canvas.translate(-250, 0); + canvas.drawVertices(vertices, BlendMode.plus, Paint()..shader = ImageShader(image, TileMode.clamp, TileMode.clamp, Matrix4.identity().storage)); + } + + @override + bool shouldRepaint(covariant CustomPainter oldDelegate) { + return true; + } +} diff --git a/dev/benchmarks/macrobenchmarks/test_driver/draw_atlas_perf_test.dart b/dev/benchmarks/macrobenchmarks/test_driver/draw_atlas_perf_test.dart new file mode 100644 index 0000000000..a25028c151 --- /dev/null +++ b/dev/benchmarks/macrobenchmarks/test_driver/draw_atlas_perf_test.dart @@ -0,0 +1,16 @@ +// Copyright 2014 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:macrobenchmarks/common.dart'; + +import 'util.dart'; + +void main() { + macroPerfTest( + 'draw_atlas_perf', + kDrawAtlasPageRouteName, + pageDelay: const Duration(seconds: 1), + duration: const Duration(seconds: 10), + ); +} diff --git a/dev/benchmarks/macrobenchmarks/test_driver/draw_vertices_perf_test.dart b/dev/benchmarks/macrobenchmarks/test_driver/draw_vertices_perf_test.dart new file mode 100644 index 0000000000..e0c5786561 --- /dev/null +++ b/dev/benchmarks/macrobenchmarks/test_driver/draw_vertices_perf_test.dart @@ -0,0 +1,16 @@ +// Copyright 2014 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:macrobenchmarks/common.dart'; + +import 'util.dart'; + +void main() { + macroPerfTest( + 'draw_vertices_perf', + kDrawVerticesPageRouteName, + pageDelay: const Duration(seconds: 1), + duration: const Duration(seconds: 10), + ); +}