Skip to content

Commit e11a342

Browse files
9AZXknopp
andauthored
Only render views that need to be rendered (with external texture & platform view support) (flutter#179874)
Fixes flutter#164320 Based on @knopp work in flutter#164322 This PR adds a dirty-view compositing optimization that skips compositing for views that don't have any dirty render objects, so it should improves performance in multi-view scenarios by avoiding unnessacery work. When a texture has new content, the framework now gets notified via a new onTextureFrameAvailable callback from the engine. This marks the texture's render object as needing paint, so its view is included in compositing even when nothing else changed. On Android, texture callbacks now use `markTextureFrameAvailable` instead of `scheduleEngineFrame`. This is actually more efficient since it reuses the existing layer tree rather than regenerating it. Other platforms already used the correct path. It also forces all views to composite when returning from background, ensuring the display is properly refreshed after the app was paused. On MacOS: https://github.com/user-attachments/assets/9d8b21b5-0c61-40d9-aadc-8c5a4b76f20c ## Pre-launch Checklist - [X] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [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 documentation (doc comments with `///`). - [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. --------- Co-authored-by: Matej Knopp <[email protected]>
1 parent 3527084 commit e11a342

26 files changed

Lines changed: 722 additions & 30 deletions

File tree

‎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].
@@ -638,6 +646,52 @@ class PlatformDispatcher {
638646
@Native<Void Function(Bool)>(symbol: 'PlatformConfigurationNativeApi::SetNeedsReportTimings')
639647
external static void __nativeSetNeedsReportTimings(bool value);
640648

649+
/// A callback that is invoked when a texture has a new frame available.
650+
///
651+
/// The engine invokes this callback after a platform embedder notifies it
652+
/// that a native texture has new content. The mechanism by which embedders
653+
/// raise this notification is platform-specific and part of the embedder
654+
/// API. The callback receives the ID of the texture that has a new frame.
655+
///
656+
/// The framework uses this to mark texture render objects as needing paint
657+
/// when their backing texture has new content.
658+
///
659+
/// See also:
660+
///
661+
/// * [TextureBox], which uses this to stay updated with texture changes.
662+
TextureFrameAvailableCallback? get onTextureFrameAvailable => _onTextureFrameAvailable;
663+
TextureFrameAvailableCallback? _onTextureFrameAvailable;
664+
Zone _onTextureFrameAvailableZone = Zone.root;
665+
set onTextureFrameAvailable(TextureFrameAvailableCallback? callback) {
666+
_onTextureFrameAvailable = callback;
667+
_onTextureFrameAvailableZone = Zone.current;
668+
}
669+
670+
// Called from the engine, via hooks.dart
671+
void _notifyTextureFrameAvailable(int textureId) {
672+
_invoke1<int>(onTextureFrameAvailable, _onTextureFrameAvailableZone, textureId);
673+
}
674+
675+
/// A callback that is invoked when the engine requires the application to
676+
/// re-render all of its views.
677+
///
678+
/// On the next [onBeginFrame] or [onDrawFrame], the application should
679+
/// invoke [FlutterView.render] on all of its [views]. This callback
680+
/// can be ignored if the application always renders all views on each
681+
/// frame.
682+
MarkAllViewsNeedRenderCallback? get onMarkAllViewsNeedRender => _onMarkAllViewsNeedRender;
683+
MarkAllViewsNeedRenderCallback? _onMarkAllViewsNeedRender;
684+
Zone _onMarkAllViewsNeedRenderZone = Zone.root;
685+
set onMarkAllViewsNeedRender(MarkAllViewsNeedRenderCallback? callback) {
686+
_onMarkAllViewsNeedRender = callback;
687+
_onMarkAllViewsNeedRenderZone = Zone.current;
688+
}
689+
690+
// Called from the engine, via hooks.dart
691+
void _markAllViewsNeedRender() {
692+
_invoke(onMarkAllViewsNeedRender, _onMarkAllViewsNeedRenderZone);
693+
}
694+
641695
// Called from the engine, via hooks.dart
642696
void _reportTimings(List<int> timings) {
643697
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
@@ -16,6 +16,8 @@ typedef PlatformMessageResponseCallback = void Function(ByteData? data);
1616
typedef PlatformMessageCallback =
1717
void Function(String name, ByteData? data, PlatformMessageResponseCallback? callback);
1818
typedef ErrorCallback = bool Function(Object exception, StackTrace stackTrace);
19+
typedef TextureFrameAvailableCallback = void Function(int textureId);
20+
typedef MarkAllViewsNeedRenderCallback = void Function();
1921

2022
/// A token that represents a root isolate.
2123
class RootIsolateToken {
@@ -67,6 +69,12 @@ abstract class PlatformDispatcher {
6769
TimingsCallback? get onReportTimings;
6870
set onReportTimings(TimingsCallback? callback);
6971

72+
TextureFrameAvailableCallback? get onTextureFrameAvailable;
73+
set onTextureFrameAvailable(TextureFrameAvailableCallback? callback);
74+
75+
MarkAllViewsNeedRenderCallback? get onMarkAllViewsNeedRender;
76+
set onMarkAllViewsNeedRender(MarkAllViewsNeedRenderCallback? callback);
77+
7078
void sendPlatformMessage(String name, ByteData? data, PlatformMessageResponseCallback? callback);
7179

7280
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
@@ -402,6 +402,36 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
402402
invoke1<List<ui.FrameTiming>>(_onReportTimings, _onReportTimingsZone, timings);
403403
}
404404

405+
/// A callback invoked when a new frame is available for a texture.
406+
@override
407+
ui.TextureFrameAvailableCallback? get onTextureFrameAvailable => _onTextureFrameAvailable;
408+
ui.TextureFrameAvailableCallback? _onTextureFrameAvailable;
409+
Zone _onTextureFrameAvailableZone = Zone.root;
410+
@override
411+
set onTextureFrameAvailable(ui.TextureFrameAvailableCallback? callback) {
412+
_onTextureFrameAvailable = callback;
413+
_onTextureFrameAvailableZone = Zone.current;
414+
}
415+
416+
void invokeOnTextureFrameAvailable(int textureId) {
417+
invoke1<int>(_onTextureFrameAvailable, _onTextureFrameAvailableZone, textureId);
418+
}
419+
420+
/// A callback that is invoked when the application should re-render.
421+
@override
422+
ui.MarkAllViewsNeedRenderCallback? get onMarkAllViewsNeedRender => _onMarkAllViewsNeedRender;
423+
ui.MarkAllViewsNeedRenderCallback? _onMarkAllViewsNeedRender;
424+
Zone _onMarkAllViewsNeedRenderZone = Zone.root;
425+
@override
426+
set onMarkAllViewsNeedRender(ui.MarkAllViewsNeedRenderCallback? callback) {
427+
_onMarkAllViewsNeedRender = callback;
428+
_onMarkAllViewsNeedRenderZone = Zone.current;
429+
}
430+
431+
void markAllViewsNeedRender() {
432+
invoke(onMarkAllViewsNeedRender, _onMarkAllViewsNeedRenderZone);
433+
}
434+
405435
@override
406436
void sendPlatformMessage(
407437
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)) {

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,33 @@ class RuntimeController : public PlatformConfigurationClient,
375375
///
376376
bool ReportTimings(std::vector<int64_t> timings);
377377

378+
//----------------------------------------------------------------------------
379+
/// @brief Notifies the framework that a texture has a new frame
380+
/// available.
381+
///
382+
/// This is called by the shell when the platform marks a texture
383+
/// as having new content. The framework uses this to mark the
384+
/// corresponding texture render object as needing paint.
385+
///
386+
/// @param[in] texture_id The ID of the texture that has a new frame.
387+
///
388+
/// @return Returns if the notification to the running isolate was
389+
/// successful. Returns false if the root isolate is not running.
390+
///
391+
bool NotifyTextureFrameAvailable(int64_t texture_id);
392+
393+
//----------------------------------------------------------------------------
394+
/// @brief Notifies the framework that all views should be marked dirty.
395+
///
396+
/// This is called when the engine needs to force full re-render
397+
/// of all views on the next frame, for example during lifecycle
398+
/// events.
399+
///
400+
/// @return Returns if the notification to the running isolate was
401+
/// successful. Returns false if the root isolate is not running.
402+
///
403+
bool MarkAllViewsNeedRender();
404+
378405
//----------------------------------------------------------------------------
379406
/// @brief Notify the Dart VM that no frame workloads are expected on the
380407
/// UI task runner till the specified deadline. The VM uses this

0 commit comments

Comments
 (0)