Frame timing and memory pressure: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: clock (line 42), timing (line 54), pressure (line 67).
1/// How long since the last frame, what a window of frames cost, and giving
2/// pooled render targets back when the platform warns about memory.
3///
4/// Quoted by `diagnostics.md` and shown whole in the Source tab.
5library;
7import 'package:flutter/widgets.dart';
8import 'package:flutter3d/flutter3d.dart';
9import 'package:flutter3d_app/flutter3d_app.dart';
10import 'package:flutter3d_showcase/src/demo/demo.dart';
12/// The `diagnostics` page: a clock marker timed by a frame clock and a timing
13/// log. Its check is that the first tick measures no time and a log with a
14/// window of two reports after its second frame.
15final class DiagnosticsDemo extends ShowcaseDemo {
16 late final String _report;
17 late final double _firstTick;
18 late final bool _hasTimingLine;
21 Scene build(DemoContext context) {
22 final (String report, double firstTick, bool hasTimingLine) = _run(context);
23 _report = report;
24 _firstTick = firstTick;
25 _hasTimingLine = hasTimingLine;
26 final material = RenderMaterial(
27 name: 'clock',
28 baseColor: LinearColor.fromSrgb(0.7, 0.7, 0.4, 1.0),
29 );
30 final node = MeshNode(
31 DeviceMesh.upload(context.device, SphereShape(segments: 16).build()),
32 material,
33 );
34 return Scene()
35 ..add(node)
36 ..add(
37 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
38 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
39 );
40 }
42 static (String, double) _clockLine() {
43 final clock = FrameClock();
44 final first = clock.nextFrameSeconds();
45 clock.secondsSince(const Duration(milliseconds: 16));
46 clock.secondsSince(const Duration(milliseconds: 32));
47 return (
48 'first tick: ${first}s, elapsed after two frames: '
49 '${clock.elapsedSeconds.toStringAsFixed(3)}s',
50 first,
51 );
52 }
54 static String? _timingLine() {
55 final log = FrameTimingLog(label: 'demo', window: 2);
56 log.note(
57 build: const Duration(milliseconds: 4),
58 raster: const Duration(milliseconds: 6),
59 );
60 return log.note(
61 build: const Duration(milliseconds: 8),
62 raster: const Duration(milliseconds: 10),
63 );
64 }
66 static (String, double, bool) _run(DemoContext context) {
67 // What a memory warning does: give the renderer's pooled render targets
68 // back. `MemoryPressureRelease` calls exactly this when the platform
69 // warns; calling it directly here proves the release itself.
70 context.renderer.releaseTransientTargets();
72 final (String clockLine, double firstTick) = _clockLine();
73 final String? timingLine = _timingLine();
74 return (
75 '$clockLine\n${timingLine ?? 'no line yet'}',
76 firstTick,
77 timingLine != null,
78 );
79 }
82 Widget? customBody(BuildContext buildContext, DemoContext context) =>
83 Container(
84 color: const Color(0xFF14161A),
85 padding: const EdgeInsets.all(24),
86 alignment: Alignment.topLeft,
87 child: DefaultTextStyle(
88 style: const TextStyle(color: Color(0xFFE8E8EC), fontSize: 16),
89 child: Text(_report),
90 ),
91 );
94 void verify(Scene scene, FrameResult frame) {
95 if (frame.drawCalls < 1) {
96 throw StateError('the clock marker was not drawn');
97 }
98 // Compared as a number, not read back out of `_report`: the first tick
99 // is a whole-number double, and a web backend prints one of those
100 // without its trailing `.0` — a compiled `0` failing a substring match
101 // against `'tick: 0.0s'` would be this check catching its own string,
102 // not the clock.
103 if (_firstTick != 0.0) {
104 throw StateError(
105 'the very first tick of a frame clock should measure '
106 'as no time at all',
107 );
108 }
109 if (!_hasTimingLine) {
110 throw StateError(
111 'a timing log with a window of two should report '
112 'after its second frame',
113 );
114 }
115 }
116}