Skip to content

Commit 6d2efe7

Browse files
knopp9AZX
andauthored
Reland: Only render views that need to be rendered (#192128)
This relands #179874, reverted in #191252. The reason for revert was stalled web benchmarks (#191251). Stalling was caused by `_autoUpdateBenchmarkPhase` inside `macrobenchmarks/lib/src/web/recorder.dart` expecting certain number of `preroll_frame` and `apply_frame` events. These events are only published for frames that are composited (ui.Scene being rendered), but some of the web benchmarks request frame without making any render objects dirty, which after #191252 did not result on view being composited. The fix is to force view composition on every frame when the benchmarks are running. cc @yjbanov regarding the changes in `recorder.dart`. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Jon Ihlas <[email protected]> Co-authored-by: Jon Ihlas <[email protected]>
1 parent 53c2903 commit 6d2efe7

31 files changed

Lines changed: 835 additions & 38 deletions

File tree

‎dev/benchmarks/macrobenchmarks/lib/src/web/recorder.dart‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,13 @@ class _RecordingWidgetsBinding extends BindingBase
12341234
}
12351235
try {
12361236
_recorder?.frameWillDraw();
1237+
// Flutter will not render views that don't need compositing,
1238+
// but this recorder expect preroll/applyFrame for every frame,
1239+
// even if the views don't change otherwise it stalls.
1240+
// See https://github.com/flutter/flutter/issues/191251
1241+
for (final RenderView renderView in renderViews) {
1242+
renderView.markNeedsCompositeFrame();
1243+
}
12371244
super.handleDrawFrame();
12381245
_recorder?.frameDidDraw();
12391246
} catch (error, stackTrace) {

‎dev/tracing_tests/test/timeline_test.dart‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ void main() {
8080
'LAYOUT',
8181
'UPDATING COMPOSITING BITS',
8282
'PAINT',
83-
'COMPOSITING',
8483
'FINALIZE TREE',
8584
]);
8685
debugProfileBuildsEnabled = false;
@@ -94,7 +93,6 @@ void main() {
9493
'LAYOUT',
9594
'UPDATING COMPOSITING BITS',
9695
'PAINT',
97-
'COMPOSITING',
9896
'FINALIZE TREE',
9997
]);
10098
debugProfileLayoutsEnabled = false;
@@ -108,7 +106,6 @@ void main() {
108106
'LAYOUT',
109107
'UPDATING COMPOSITING BITS',
110108
'PAINT',
111-
'COMPOSITING',
112109
'FINALIZE TREE',
113110
]);
114111
debugProfilePaintsEnabled = false;

‎engine/src/flutter/lib/ui/fixtures/ui_test.dart‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,6 +1530,42 @@ void hooksTests() async {
15301530
expectEquals(display.refreshRate, 65);
15311531
});
15321532

1533+
await test('onTextureFrameAvailable preserves callback zone', () {
1534+
late Zone innerZone;
1535+
late Zone runZone;
1536+
late int receivedTextureId;
1537+
1538+
runZoned(() {
1539+
innerZone = Zone.current;
1540+
PlatformDispatcher.instance.onTextureFrameAvailable = (int textureId) {
1541+
runZone = Zone.current;
1542+
receivedTextureId = textureId;
1543+
};
1544+
});
1545+
1546+
_callHook('_notifyTextureFrameAvailable', 1, 1234);
1547+
expectIdentical(runZone, innerZone);
1548+
expectEquals(receivedTextureId, 1234);
1549+
});
1550+
1551+
await test('onMarkAllViewsNeedRender preserves callback zone', () {
1552+
late Zone innerZone;
1553+
late Zone runZone;
1554+
bool called = false;
1555+
1556+
runZoned(() {
1557+
innerZone = Zone.current;
1558+
PlatformDispatcher.instance.onMarkAllViewsNeedRender = () {
1559+
runZone = Zone.current;
1560+
called = true;
1561+
};
1562+
});
1563+
1564+
_callHook('_markAllViewsNeedRender', 0);
1565+
expectIdentical(runZone, innerZone);
1566+
expectEquals(called, true);
1567+
});
1568+
15331569
await test('_futureize handles callbacker sync error', () async {
15341570
String? callbacker(void Function(Object? arg) cb) {
15351571
return 'failure';

‎engine/src/flutter/lib/ui/hooks.dart‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,16 @@ void _drawFrame() {
413413
PlatformDispatcher.instance._drawFrame();
414414
}
415415

416+
@pragma('vm:entry-point')
417+
void _notifyTextureFrameAvailable(int textureId) {
418+
PlatformDispatcher.instance._notifyTextureFrameAvailable(textureId);
419+
}
420+
421+
@pragma('vm:entry-point')
422+
void _markAllViewsNeedRender() {
423+
PlatformDispatcher.instance._markAllViewsNeedRender();
424+
}
425+
416426
@pragma('vm:entry-point')
417427
bool _onError(Object error, StackTrace? stackTrace) {
418428
return PlatformDispatcher.instance._dispatchError(error, stackTrace ?? StackTrace.empty);

‎engine/src/flutter/lib/ui/platform_dispatcher.dart‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ typedef HitTestCallback = HitTestResponse Function(HitTestRequest request);
5252
/// [PlatformDispatcher.onPlatformMessage].
5353
typedef PlatformMessageResponseCallback = void Function(ByteData? data);
5454

55+
/// Signature for [PlatformDispatcher.onTextureFrameAvailable].
56+
///
57+
/// The callback receives the ID of the texture that has a new frame available.
58+
typedef TextureFrameAvailableCallback = void Function(int textureId);
59+
60+
/// Signature for [PlatformDispatcher.onMarkAllViewsNeedRender].
61+
typedef MarkAllViewsNeedRenderCallback = void Function();
62+
5563
/// Deprecated. Migrate to [ChannelBuffers.setListener] instead.
5664
///
5765
/// Signature for [PlatformDispatcher.onPlatformMessage].
@@ -641,6 +649,52 @@ class PlatformDispatcher {
641649
@Native<Void Function(Bool)>(symbol: 'PlatformConfigurationNativeApi::SetNeedsReportTimings')
642650
external static void __nativeSetNeedsReportTimings(bool value);
643651

652+
/// A callback that is invoked when a texture has a new frame available.
653+
///
654+
/// The engine invokes this callback after a platform embedder notifies it
655+
/// that a native texture has new content. The mechanism by which embedders
656+
/// raise this notification is platform-specific and part of the embedder
657+
/// API. The callback receives the ID of the texture that has a new frame.
658+
///
659+
/// The framework uses this to mark texture render objects as needing paint
660+
/// when their backing texture has new content.
661+
///
662+
/// See also:
663+
///
664+
/// * [TextureBox], which uses this to stay updated with texture changes.
665+
TextureFrameAvailableCallback? get onTextureFrameAvailable => _onTextureFrameAvailable;
666+
TextureFrameAvailableCallback? _onTextureFrameAvailable;
667+
Zone _onTextureFrameAvailableZone = Zone.root;
668+
set onTextureFrameAvailable(TextureFrameAvailableCallback? callback) {
669+
_onTextureFrameAvailable = callback;
670+
_onTextureFrameAvailableZone = Zone.current;
671+
}
672+
673+
// Called from the engine, via hooks.dart
674+
void _notifyTextureFrameAvailable(int textureId) {
675+
_invoke1<int>(onTextureFrameAvailable, _onTextureFrameAvailableZone, textureId);
676+
}
677+
678+
/// A callback that is invoked when the engine requires the application to
679+
/// re-render all of its views.
680+
///
681+
/// On the next [onBeginFrame] or [onDrawFrame], the application should
682+
/// invoke [FlutterView.render] on all of its [views]. This callback
683+
/// can be ignored if the application always renders all views on each
684+
/// frame.
685+
MarkAllViewsNeedRenderCallback? get onMarkAllViewsNeedRender => _onMarkAllViewsNeedRender;
686+
MarkAllViewsNeedRenderCallback? _onMarkAllViewsNeedRender;
687+
Zone _onMarkAllViewsNeedRenderZone = Zone.root;
688+
set onMarkAllViewsNeedRender(MarkAllViewsNeedRenderCallback? callback) {
689+
_onMarkAllViewsNeedRender = callback;
690+
_onMarkAllViewsNeedRenderZone = Zone.current;
691+
}
692+
693+
// Called from the engine, via hooks.dart
694+
void _markAllViewsNeedRender() {
695+
_invoke(onMarkAllViewsNeedRender, _onMarkAllViewsNeedRenderZone);
696+
}
697+
644698
// Called from the engine, via hooks.dart
645699
void _reportTimings(List<int> timings) {
646700
assert(timings.length % FrameTiming._dataLength == 0);

‎engine/src/flutter/lib/ui/window/platform_configuration.cc‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ void PlatformConfiguration::DidCreateIsolate() {
8888
Dart_GetField(library, tonic::ToDart("_drawFrame")));
8989
report_timings_.Set(tonic::DartState::Current(),
9090
Dart_GetField(library, tonic::ToDart("_reportTimings")));
91+
notify_texture_frame_available_.Set(
92+
tonic::DartState::Current(),
93+
Dart_GetField(library, tonic::ToDart("_notifyTextureFrameAvailable")));
94+
mark_all_views_need_render_.Set(
95+
tonic::DartState::Current(),
96+
Dart_GetField(library, tonic::ToDart("_markAllViewsNeedRender")));
9197
}
9298

9399
bool PlatformConfiguration::AddView(int64_t view_id,
@@ -518,6 +524,32 @@ void PlatformConfiguration::ReportTimings(std::vector<int64_t> timings) {
518524
}));
519525
}
520526

527+
void PlatformConfiguration::NotifyTextureFrameAvailable(int64_t texture_id) {
528+
std::shared_ptr<tonic::DartState> dart_state =
529+
notify_texture_frame_available_.dart_state().lock();
530+
if (!dart_state) {
531+
return;
532+
}
533+
tonic::DartState::Scope scope(dart_state);
534+
535+
tonic::CheckAndHandleError(tonic::DartInvoke(
536+
notify_texture_frame_available_.Get(), {
537+
tonic::ToDart(texture_id),
538+
}));
539+
}
540+
541+
void PlatformConfiguration::MarkAllViewsNeedRender() {
542+
std::shared_ptr<tonic::DartState> dart_state =
543+
mark_all_views_need_render_.dart_state().lock();
544+
if (!dart_state) {
545+
return;
546+
}
547+
tonic::DartState::Scope scope(dart_state);
548+
549+
tonic::CheckAndHandleError(
550+
tonic::DartInvoke(mark_all_views_need_render_.Get(), {}));
551+
}
552+
521553
const ViewportMetrics* PlatformConfiguration::GetMetrics(int view_id) {
522554
auto found = metrics_.find(view_id);
523555
if (found != metrics_.end()) {

‎engine/src/flutter/lib/ui/window/platform_configuration.h‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,34 @@ class PlatformConfiguration final {
549549
///
550550
void ReportTimings(std::vector<int64_t> timings);
551551

552+
//----------------------------------------------------------------------------
553+
/// @brief Notifies the framework that a texture has a new frame
554+
/// available.
555+
///
556+
/// This is called when the platform marks a texture as having new
557+
/// content via `MarkTextureFrameAvailable`. The framework uses
558+
/// this to mark the corresponding texture render object as
559+
/// needing paint, ensuring the view containing the texture is
560+
/// recomposited even if no other render objects are dirty.
561+
///
562+
/// This method calls the `::_notifyTextureFrameAvailable` method
563+
/// in `hooks.dart`.
564+
///
565+
/// @param[in] texture_id The ID of the texture that has a new frame.
566+
///
567+
/// @note Must be called on the UI task runner.
568+
///
569+
void NotifyTextureFrameAvailable(int64_t texture_id);
570+
571+
//----------------------------------------------------------------------------
572+
/// @brief Notifies the framework that all views should be marked dirty.
573+
///
574+
/// This is called when the engine needs to force full re-render
575+
/// of all views on the next frame, for example during lifecycle
576+
/// events.
577+
///
578+
void MarkAllViewsNeedRender();
579+
552580
//----------------------------------------------------------------------------
553581
/// @brief Retrieves the viewport metrics with the given ID managed by
554582
/// the `PlatformConfiguration`.
@@ -605,6 +633,8 @@ class PlatformConfiguration final {
605633
tonic::DartPersistentValue begin_frame_;
606634
tonic::DartPersistentValue draw_frame_;
607635
tonic::DartPersistentValue report_timings_;
636+
tonic::DartPersistentValue notify_texture_frame_available_;
637+
tonic::DartPersistentValue mark_all_views_need_render_;
608638

609639
uint64_t last_frame_number_ = 0;
610640
int64_t last_microseconds_ = 0;

‎engine/src/flutter/lib/web_ui/lib/platform_dispatcher.dart‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ typedef PlatformMessageCallback = void Function(
1919
PlatformMessageResponseCallback? callback,
2020
);
2121
typedef ErrorCallback = bool Function(Object exception, StackTrace stackTrace);
22+
typedef TextureFrameAvailableCallback = void Function(int textureId);
23+
typedef MarkAllViewsNeedRenderCallback = void Function();
2224

2325
/// A token that represents a root isolate.
2426
class RootIsolateToken {
@@ -70,6 +72,12 @@ abstract class PlatformDispatcher {
7072
TimingsCallback? get onReportTimings;
7173
set onReportTimings(TimingsCallback? callback);
7274

75+
TextureFrameAvailableCallback? get onTextureFrameAvailable;
76+
set onTextureFrameAvailable(TextureFrameAvailableCallback? callback);
77+
78+
MarkAllViewsNeedRenderCallback? get onMarkAllViewsNeedRender;
79+
set onMarkAllViewsNeedRender(MarkAllViewsNeedRenderCallback? callback);
80+
7381
void sendPlatformMessage(String name, ByteData? data, PlatformMessageResponseCallback? callback);
7482

7583
void sendPortPlatformMessage(String name, ByteData? data, int identifier, Object port);

‎engine/src/flutter/lib/web_ui/lib/src/engine/platform_dispatcher.dart‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,36 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
396396
invoke1<List<ui.FrameTiming>>(_onReportTimings, _onReportTimingsZone, timings);
397397
}
398398

399+
/// A callback invoked when a new frame is available for a texture.
400+
@override
401+
ui.TextureFrameAvailableCallback? get onTextureFrameAvailable => _onTextureFrameAvailable;
402+
ui.TextureFrameAvailableCallback? _onTextureFrameAvailable;
403+
Zone _onTextureFrameAvailableZone = Zone.root;
404+
@override
405+
set onTextureFrameAvailable(ui.TextureFrameAvailableCallback? callback) {
406+
_onTextureFrameAvailable = callback;
407+
_onTextureFrameAvailableZone = Zone.current;
408+
}
409+
410+
void invokeOnTextureFrameAvailable(int textureId) {
411+
invoke1<int>(_onTextureFrameAvailable, _onTextureFrameAvailableZone, textureId);
412+
}
413+
414+
/// A callback that is invoked when the application should re-render.
415+
@override
416+
ui.MarkAllViewsNeedRenderCallback? get onMarkAllViewsNeedRender => _onMarkAllViewsNeedRender;
417+
ui.MarkAllViewsNeedRenderCallback? _onMarkAllViewsNeedRender;
418+
Zone _onMarkAllViewsNeedRenderZone = Zone.root;
419+
@override
420+
set onMarkAllViewsNeedRender(ui.MarkAllViewsNeedRenderCallback? callback) {
421+
_onMarkAllViewsNeedRender = callback;
422+
_onMarkAllViewsNeedRenderZone = Zone.current;
423+
}
424+
425+
void markAllViewsNeedRender() {
426+
invoke(_onMarkAllViewsNeedRender, _onMarkAllViewsNeedRenderZone);
427+
}
428+
399429
@override
400430
void sendPlatformMessage(
401431
String name,

‎engine/src/flutter/runtime/runtime_controller.cc‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,24 @@ bool RuntimeController::ReportTimings(std::vector<int64_t> timings) {
316316
return false;
317317
}
318318

319+
bool RuntimeController::NotifyTextureFrameAvailable(int64_t texture_id) {
320+
if (auto* platform_configuration = GetPlatformConfigurationIfAvailable()) {
321+
platform_configuration->NotifyTextureFrameAvailable(texture_id);
322+
return true;
323+
}
324+
325+
return false;
326+
}
327+
328+
bool RuntimeController::MarkAllViewsNeedRender() {
329+
if (auto* platform_configuration = GetPlatformConfigurationIfAvailable()) {
330+
platform_configuration->MarkAllViewsNeedRender();
331+
return true;
332+
}
333+
334+
return false;
335+
}
336+
319337
bool RuntimeController::NotifyIdle(fml::TimeDelta deadline) {
320338
if (deadline - fml::TimeDelta::FromMicroseconds(Dart_TimelineGetMicros()) <
321339
fml::TimeDelta::FromMilliseconds(1)) {

0 commit comments

Comments
 (0)