|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:test/bootstrap/browser.dart'; |
| 6 | +import 'package:test/test.dart'; |
| 7 | +import 'package:ui/src/engine.dart' show renderer; |
| 8 | +import 'package:ui/ui.dart' as ui; |
| 9 | + |
| 10 | +import '../common/rendering.dart'; |
| 11 | +import '../common/test_initialization.dart'; |
| 12 | + |
| 13 | +void main() { |
| 14 | + internalBootstrapBrowserTest(() => testMain); |
| 15 | +} |
| 16 | + |
| 17 | +const int _maxFrames = 200; |
| 18 | +const Duration _maxTestTime = Duration(seconds: 10); |
| 19 | +const int _paragraphsPerFrame = 24; |
| 20 | +const int _spansPerParagraph = 3; |
| 21 | + |
| 22 | +const List<String> _fontFamilies = <String>['Roboto', 'RobotoVariable', 'Ahem']; |
| 23 | +const List<ui.FontWeight> _fontWeights = <ui.FontWeight>[ |
| 24 | + ui.FontWeight.w100, |
| 25 | + ui.FontWeight.w300, |
| 26 | + ui.FontWeight.w400, |
| 27 | + ui.FontWeight.w500, |
| 28 | + ui.FontWeight.w700, |
| 29 | + ui.FontWeight.w900, |
| 30 | +]; |
| 31 | + |
| 32 | +Future<void> testMain() async { |
| 33 | + setUpUnitTests(withImplicitView: true, setUpTestViewDimensions: false); |
| 34 | + |
| 35 | + // Advanced by an irrational stride so fractional font sizes never repeat, |
| 36 | + // forcing a fresh SkStrike for every span. |
| 37 | + double fontSizeSeed = 0; |
| 38 | + |
| 39 | + List<ui.Paragraph> buildAndLayoutParagraphs(int frame) { |
| 40 | + final paragraphs = <ui.Paragraph>[]; |
| 41 | + for (var i = 0; i < _paragraphsPerFrame; i++) { |
| 42 | + final builder = ui.ParagraphBuilder(ui.ParagraphStyle()); |
| 43 | + for (var span = 0; span < _spansPerParagraph; span++) { |
| 44 | + fontSizeSeed += 0.6180339887; |
| 45 | + builder.pushStyle( |
| 46 | + ui.TextStyle( |
| 47 | + color: const ui.Color(0xFF000000), |
| 48 | + fontFamily: _fontFamilies[(i + span) % _fontFamilies.length], |
| 49 | + fontSize: 8.0 + (fontSizeSeed % 32.0), |
| 50 | + fontWeight: _fontWeights[(frame + i + span) % _fontWeights.length], |
| 51 | + fontStyle: (frame + i + span).isEven ? ui.FontStyle.normal : ui.FontStyle.italic, |
| 52 | + ), |
| 53 | + ); |
| 54 | + builder.addText('Quick zephyrs blow 0123456789 frame $frame paragraph $i span $span. '); |
| 55 | + builder.pop(); |
| 56 | + } |
| 57 | + final ui.Paragraph paragraph = builder.build(); |
| 58 | + paragraph.layout(const ui.ParagraphConstraints(width: 400)); |
| 59 | + paragraphs.add(paragraph); |
| 60 | + } |
| 61 | + return paragraphs; |
| 62 | + } |
| 63 | + |
| 64 | + ui.Picture recordPicture(List<ui.Paragraph> paragraphs) { |
| 65 | + final recorder = ui.PictureRecorder(); |
| 66 | + final canvas = ui.Canvas(recorder, const ui.Rect.fromLTWH(0, 0, 500, 500)); |
| 67 | + canvas.drawColor(const ui.Color(0xFFFFFFFF), ui.BlendMode.src); |
| 68 | + var offset = 0.0; |
| 69 | + for (final paragraph in paragraphs) { |
| 70 | + canvas.drawParagraph(paragraph, ui.Offset(0, offset)); |
| 71 | + offset += 20.0; |
| 72 | + } |
| 73 | + return recorder.endRecording(); |
| 74 | + } |
| 75 | + |
| 76 | + test('concurrent text layout and rasterization does not corrupt the heap', () async { |
| 77 | + List<ui.Paragraph> paragraphs = buildAndLayoutParagraphs(0); |
| 78 | + final stopwatch = Stopwatch()..start(); |
| 79 | + var frame = 0; |
| 80 | + while (frame < _maxFrames && stopwatch.elapsed < _maxTestTime) { |
| 81 | + frame++; |
| 82 | + |
| 83 | + final ui.Picture picture = recordPicture(paragraphs); |
| 84 | + final sceneBuilder = ui.SceneBuilder(); |
| 85 | + sceneBuilder.addPicture(ui.Offset.zero, picture); |
| 86 | + |
| 87 | + // Kick off rasterization unawaited, then yield once so the render |
| 88 | + // message reaches the raster thread. |
| 89 | + final Future<void> renderFuture = renderScene(sceneBuilder.build()); |
| 90 | + await Future<void>.delayed(Duration.zero); |
| 91 | + |
| 92 | + // Lay out the next frame's paragraphs while the raster thread is |
| 93 | + // rasterizing this one — the concurrent strike cache access under test. |
| 94 | + final List<ui.Paragraph> nextParagraphs = buildAndLayoutParagraphs(frame); |
| 95 | + |
| 96 | + await renderFuture; |
| 97 | + |
| 98 | + // Disposing exercises the concurrent teardown path of |
| 99 | + // https://github.com/flutter/flutter/issues/184858. |
| 100 | + for (final paragraph in paragraphs) { |
| 101 | + paragraph.dispose(); |
| 102 | + } |
| 103 | + picture.dispose(); |
| 104 | + paragraphs = nextParagraphs; |
| 105 | + |
| 106 | + // Shrink and re-grow the GPU resource cache so purging runs against |
| 107 | + // in-flight rasterization. |
| 108 | + if (frame % 16 == 0) { |
| 109 | + renderer.resourceCacheMaxBytes = frame % 32 == 0 ? 1024 * 1024 : 64 * 1024 * 1024; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + for (final paragraph in paragraphs) { |
| 114 | + paragraph.dispose(); |
| 115 | + } |
| 116 | + |
| 117 | + // Completing without a RuntimeError or hang is the pass condition. |
| 118 | + expect(frame, greaterThan(0)); |
| 119 | + }, timeout: const Timeout(Duration(minutes: 2))); |
| 120 | +} |
0 commit comments