From ef308788e22617e3ab9992432f254181f861aba9 Mon Sep 17 00:00:00 2001 From: Chris Yang Date: Mon, 18 Mar 2019 15:38:40 -0700 Subject: [PATCH] [platform_view] iOSP platformView composition optimize. (flutter/engine#8202) Optimize the performance of the platform view composition. When recomposition is required, we previously remove all the platform view related views (intercepting view, platform view) and re-add them for each frame. This is mentioned in flutter/flutter#23793. This PR enhanced the performance by re-arrange the existing UIViews and only add the newly created Views. As a result, it would also fix flutter/flutter#29427 --- .../framework/Source/FlutterPlatformViews.mm | 41 +++++++++++++------ .../Source/FlutterPlatformViews_Internal.h | 1 + 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm index fddc75fa54..82659b4fe2 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm @@ -225,22 +225,24 @@ bool FlutterPlatformViewsController::SubmitFrame(bool gl_rendering, composition_order_.clear(); return did_submit; } + DetachUnusedLayers(); + active_composition_order_.clear(); UIView* flutter_view = flutter_view_.get(); - // This can be more efficient, instead of removing all views and then re-attaching them, - // we should only remove the views that has been completly removed from the layer tree, and - // reorder the views using UIView's bringSubviewToFront. - // TODO(amirh): make this more efficient. - // https://github.com/flutter/flutter/issues/23793 - for (UIView* sub_view in [flutter_view subviews]) { - [sub_view removeFromSuperview]; - } - - active_composition_order_.clear(); for (size_t i = 0; i < composition_order_.size(); i++) { int view_id = composition_order_[i]; - [flutter_view addSubview:touch_interceptors_[view_id].get()]; - [flutter_view addSubview:overlays_[view_id]->overlay_view.get()]; + UIView* intercepter = touch_interceptors_[view_id].get(); + UIView* overlay = overlays_[view_id]->overlay_view; + FML_CHECK(intercepter.superview == overlay.superview); + + if (intercepter.superview == flutter_view) { + [flutter_view bringSubviewToFront:intercepter]; + [flutter_view bringSubviewToFront:overlay]; + } else { + [flutter_view addSubview:intercepter]; + [flutter_view addSubview:overlay]; + } + active_composition_order_.push_back(view_id); } @@ -248,6 +250,21 @@ bool FlutterPlatformViewsController::SubmitFrame(bool gl_rendering, return did_submit; } +void FlutterPlatformViewsController::DetachUnusedLayers() { + std::unordered_set composition_order_set; + + for (int64_t view_id : composition_order_) { + composition_order_set.insert(view_id); + } + + for (int64_t view_id : active_composition_order_) { + if (composition_order_set.find(view_id) == composition_order_set.end()) { + [touch_interceptors_[view_id].get() removeFromSuperview]; + [overlays_[view_id]->overlay_view.get() removeFromSuperview]; + } + } +} + void FlutterPlatformViewsController::EnsureOverlayInitialized(int64_t overlay_id) { if (overlays_.count(overlay_id) != 0) { return; diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h index a89a2ff9bc..118822c3b7 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h @@ -98,6 +98,7 @@ class FlutterPlatformViewsController { void OnAcceptGesture(FlutterMethodCall* call, FlutterResult& result); void OnRejectGesture(FlutterMethodCall* call, FlutterResult& result); + void DetachUnusedLayers(); void EnsureOverlayInitialized(int64_t overlay_id); void EnsureGLOverlayInitialized(int64_t overlay_id, std::shared_ptr gl_context,