[iOS] clean ups to platform view controller (flutter/engine#54335)

Splits FlutterPlatformViews.mm into the primarily C++ class FlutterPlatformViewsController/Pool/Overlay and all of the touch interceptor / gesture stuff.

Renames FlutterPlatformViewsController to PlatformViewsController, because, you know.
Renames FlutterPlatformViewLayer and FlutterPlatformViewLayerPool to OverlayLayer and OverlayLayerPool
This commit is contained in:
Jonah Williams
2024-08-06 14:33:55 -07:00
committed by GitHub
parent fed98ff528
commit 54bfbb4509
24 changed files with 1039 additions and 937 deletions

View File

@@ -43652,7 +43652,6 @@ ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterOverl
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPluginTest.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsTest.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.mm + ../../../flutter/LICENSE
@@ -43710,8 +43709,12 @@ ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/availability
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/connection_collection.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/connection_collection.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/connection_collection_test.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/overlay_layer_pool.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/overlay_layer_pool.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/platform_message_response_darwin.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/platform_message_response_darwin.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/platform_views_controller.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/platform_views_controller.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/profiler_metrics_ios.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/profiler_metrics_ios.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/vsync_waiter_ios.h + ../../../flutter/LICENSE
@@ -46553,7 +46556,6 @@ FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterOverlay
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.h
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.mm
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPluginTest.mm
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsTest.mm
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.mm
@@ -46611,8 +46613,12 @@ FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/availability_v
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/connection_collection.h
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/connection_collection.mm
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/connection_collection_test.mm
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/overlay_layer_pool.h
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/overlay_layer_pool.mm
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/platform_message_response_darwin.h
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/platform_message_response_darwin.mm
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/platform_views_controller.h
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/platform_views_controller.mm
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/profiler_metrics_ios.h
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/profiler_metrics_ios.mm
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/vsync_waiter_ios.h

View File

@@ -77,7 +77,6 @@ source_set("flutter_framework_source_arc") {
"framework/Source/FlutterMetalLayer.mm",
"framework/Source/FlutterOverlayView.h",
"framework/Source/FlutterOverlayView.mm",
"framework/Source/FlutterPlatformViews.mm",
"framework/Source/FlutterPlatformViews_Internal.h",
"framework/Source/FlutterPlatformViews_Internal.mm",
"framework/Source/FlutterPluginAppLifeCycleDelegate.mm",
@@ -110,8 +109,12 @@ source_set("flutter_framework_source_arc") {
"framework/Source/UIViewController+FlutterScreenAndSceneIfLoaded.mm",
"framework/Source/connection_collection.h",
"framework/Source/connection_collection.mm",
"framework/Source/overlay_layer_pool.h",
"framework/Source/overlay_layer_pool.mm",
"framework/Source/platform_message_response_darwin.h",
"framework/Source/platform_message_response_darwin.mm",
"framework/Source/platform_views_controller.h",
"framework/Source/platform_views_controller.mm",
"framework/Source/profiler_metrics_ios.h",
"framework/Source/profiler_metrics_ios.mm",
"framework/Source/vsync_waiter_ios.h",

View File

@@ -123,7 +123,7 @@ static constexpr int kNumProfilerSamplesPerSec = 5;
fml::WeakNSObject<FlutterViewController> _viewController;
fml::scoped_nsobject<FlutterDartVMServicePublisher> _publisher;
std::shared_ptr<flutter::FlutterPlatformViewsController> _platformViewsController;
std::shared_ptr<flutter::PlatformViewsController> _platformViewsController;
flutter::IOSRenderingAPI _renderingApi;
std::shared_ptr<flutter::ProfilerMetricsIOS> _profiler_metrics;
std::shared_ptr<flutter::SamplingProfiler> _profiler;
@@ -271,7 +271,7 @@ static constexpr int kNumProfilerSamplesPerSec = 5;
- (void)recreatePlatformViewController {
_renderingApi = flutter::GetRenderingAPIForProcess(FlutterView.forceSoftwareRendering);
_platformViewsController.reset(new flutter::FlutterPlatformViewsController());
_platformViewsController.reset(new flutter::PlatformViewsController());
}
- (flutter::IOSRenderingAPI)platformViewsRenderingAPI {
@@ -495,7 +495,7 @@ static constexpr int kNumProfilerSamplesPerSec = 5;
- (FlutterPlatformPlugin*)platformPlugin {
return _platformPlugin.get();
}
- (std::shared_ptr<flutter::FlutterPlatformViewsController>&)platformViewsController {
- (std::shared_ptr<flutter::PlatformViewsController>&)platformViewsController {
return _platformViewsController;
}
- (FlutterTextInputPlugin*)textInputPlugin {

View File

@@ -48,7 +48,7 @@ extern NSString* const kFlutterEngineWillDealloc;
base64Encode:(bool)base64Encode;
- (FlutterPlatformPlugin*)platformPlugin;
- (std::shared_ptr<flutter::FlutterPlatformViewsController>&)platformViewsController;
- (std::shared_ptr<flutter::PlatformViewsController>&)platformViewsController;
- (FlutterTextInputPlugin*)textInputPlugin;
- (FlutterRestorationPlugin*)restorationPlugin;
- (void)launchEngine:(nullable NSString*)entrypoint

View File

@@ -6,6 +6,7 @@
#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>
#include "fml/synchronization/count_down_latch.h"
#include "shell/platform/darwin/ios/framework/Source/platform_views_controller.h"
#import "flutter/fml/thread.h"
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
@@ -121,6 +122,11 @@ class FlutterPlatformViewsTestMockPlatformViewDelegate : public PlatformView::De
flutter::Settings settings_;
};
BOOL BlurRadiusEqualToBlurRadius(CGFloat radius1, CGFloat radius2) {
const CGFloat epsilon = 0.01;
return std::abs(radius1 - radius2) < epsilon;
}
} // namespace
} // namespace flutter
@@ -144,7 +150,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -200,7 +206,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -293,7 +299,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -370,7 +376,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -447,7 +453,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -525,7 +531,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -646,7 +652,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -792,7 +798,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -1082,7 +1088,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -1401,7 +1407,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -1462,7 +1468,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -1563,7 +1569,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -1639,7 +1645,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -1711,7 +1717,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -1782,7 +1788,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -1858,7 +1864,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -1961,7 +1967,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -2065,7 +2071,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -2132,7 +2138,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -2256,7 +2262,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -2370,7 +2376,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -2435,7 +2441,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -2506,7 +2512,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -2559,7 +2565,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -2622,7 +2628,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -2724,7 +2730,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -2911,7 +2917,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -3002,7 +3008,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -3082,7 +3088,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -3175,7 +3181,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -3286,7 +3292,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
/*raster=*/GetDefaultTaskRunner(),
/*ui=*/GetDefaultTaskRunner(),
/*io=*/GetDefaultTaskRunner());
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -3364,7 +3370,7 @@ fml::RefPtr<fml::TaskRunner> GetDefaultTaskRunner() {
auto ios_context = [engine iosPlatformView]->GetIosContext();
auto gr_context = ios_context->GetMainContext();
auto pool = flutter::FlutterPlatformViewLayerPool{};
auto pool = flutter::OverlayLayerPool{};
// Add layers to the pool.
pool.CreateLayer(gr_context.get(), ios_context, MTLPixelFormatBGRA8Unorm);

View File

@@ -19,6 +19,7 @@
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h"
#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterPlugin.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterViewResponder.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/platform_views_controller.h"
#import "flutter/shell/platform/darwin/ios/ios_context.h"
@class FlutterTouchInterceptingView;
@@ -129,363 +130,6 @@
- (NSMutableArray*)backdropFilterSubviews;
@end
namespace flutter {
// Converts a SkMatrix to CATransform3D.
// Certain fields are ignored in CATransform3D since SkMatrix is 3x3 and CATransform3D is 4x4.
CATransform3D GetCATransform3DFromSkMatrix(const SkMatrix& matrix);
// Reset the anchor of `layer` to match the transform operation from flow.
// The position of the `layer` should be unchanged after resetting the anchor.
void ResetAnchor(CALayer* layer);
CGRect GetCGRectFromSkRect(const SkRect& clipSkRect);
BOOL BlurRadiusEqualToBlurRadius(CGFloat radius1, CGFloat radius2);
class IOSSurface;
struct FlutterPlatformViewLayer {
FlutterPlatformViewLayer(const fml::scoped_nsobject<UIView>& overlay_view,
const fml::scoped_nsobject<UIView>& overlay_view_wrapper,
std::unique_ptr<IOSSurface> ios_surface,
std::unique_ptr<Surface> surface);
~FlutterPlatformViewLayer();
fml::scoped_nsobject<UIView> overlay_view;
fml::scoped_nsobject<UIView> overlay_view_wrapper;
std::unique_ptr<IOSSurface> ios_surface;
std::unique_ptr<Surface> surface;
// Whether a frame for this layer was submitted.
bool did_submit_last_frame;
// The GrContext that is currently used by the overlay surfaces.
// We track this to know when the GrContext for the Flutter app has changed
// so we can update the overlay with the new context.
GrDirectContext* gr_context;
void UpdateViewState(UIView* flutter_view, SkRect rect, int64_t view_id, int64_t overlay_id);
};
/// @brief Storage for Overlay layers across frames.
///
/// Note: this class does not synchronize access to its layers or any layer removal. As it
/// is currently used, layers must be created on the platform thread but other methods of
/// it are called on the raster thread. This is safe as overlay layers are only ever added
/// while the raster thread is latched.
class FlutterPlatformViewLayerPool {
public:
FlutterPlatformViewLayerPool() = default;
~FlutterPlatformViewLayerPool() = default;
/// @brief Gets a layer from the pool if available.
///
/// The layer is marked as used until [RecycleLayers] is called.
std::shared_ptr<FlutterPlatformViewLayer> GetNextLayer();
/// @brief Create a new overlay layer.
///
/// This method can only be called on the Platform thread.
void CreateLayer(GrDirectContext* gr_context,
const std::shared_ptr<IOSContext>& ios_context,
MTLPixelFormat pixel_format);
/// @brief Removes unused layers from the pool. Returns the unused layers.
std::vector<std::shared_ptr<FlutterPlatformViewLayer>> RemoveUnusedLayers();
/// @brief Marks the layers in the pool as available for reuse.
void RecycleLayers();
/// @brief The count of layers currently in the pool.
size_t size() const;
private:
// The index of the entry in the layers_ vector that determines the beginning of the unused
// layers. For example, consider the following vector:
// _____
// | 0 |
/// |---|
/// | 1 | <-- available_layer_index_
/// |---|
/// | 2 |
/// |---|
///
/// This indicates that entries starting from 1 can be reused meanwhile the entry at position 0
/// cannot be reused.
size_t available_layer_index_ = 0;
std::vector<std::shared_ptr<FlutterPlatformViewLayer>> layers_;
FML_DISALLOW_COPY_AND_ASSIGN(FlutterPlatformViewLayerPool);
};
class FlutterPlatformViewsController {
public:
FlutterPlatformViewsController();
~FlutterPlatformViewsController();
fml::WeakPtr<flutter::FlutterPlatformViewsController> GetWeakPtr();
void SetTaskRunner(const fml::RefPtr<fml::TaskRunner>& platform_task_runner);
void SetFlutterView(UIView* flutter_view) __attribute__((cf_audited_transfer));
void SetFlutterViewController(UIViewController<FlutterViewResponder>* flutter_view_controller)
__attribute__((cf_audited_transfer));
UIViewController<FlutterViewResponder>* getFlutterViewController()
__attribute__((cf_audited_transfer));
void RegisterViewFactory(
NSObject<FlutterPlatformViewFactory>* factory,
NSString* factoryId,
FlutterPlatformViewGestureRecognizersBlockingPolicy gestureRecognizerBlockingPolicy)
__attribute__((cf_audited_transfer));
// Called at the beginning of each frame.
void BeginFrame(SkISize frame_size);
// Indicates that we don't compisite any platform views or overlays during this frame.
// Also reverts the composition_order_ to its original state at the beginning of the frame.
void CancelFrame();
// Runs on the raster thread.
void PrerollCompositeEmbeddedView(int64_t view_id,
std::unique_ptr<flutter::EmbeddedViewParams> params);
size_t EmbeddedViewCount() const;
size_t LayerPoolSize() const;
// Returns the `FlutterPlatformView`'s `view` object associated with the view_id.
//
// If the `FlutterPlatformViewsController` does not contain any `FlutterPlatformView` object or
// a `FlutterPlatformView` object associated with the view_id cannot be found, the method
// returns nil.
UIView* GetPlatformViewByID(int64_t view_id);
// Returns the `FlutterTouchInterceptingView` with the view_id.
//
// If the `FlutterPlatformViewsController` does not contain any `FlutterPlatformView` object or
// a `FlutterPlatformView` object associated with the view_id cannot be found, the method
// returns nil.
FlutterTouchInterceptingView* GetFlutterTouchInterceptingViewByID(int64_t view_id);
// Runs on the raster thread.
PostPrerollResult PostPrerollAction(
const fml::RefPtr<fml::RasterThreadMerger>& raster_thread_merger);
// Runs on the raster thread.
void EndFrame(bool should_resubmit_frame,
const fml::RefPtr<fml::RasterThreadMerger>& raster_thread_merger);
// Return the Canvas for the overlay slice for the given platform view.
//
// Runs on the raster thread.
DlCanvas* CompositeEmbeddedView(int64_t view_id);
// Discards all platform views instances and auxiliary resources.
//
// Runs on the raster thread.
void Reset();
// Encode rendering for the Flutter overlay views and queue up perform platform view mutations.
//
// Runs on the raster thread.
bool SubmitFrame(GrDirectContext* gr_context,
const std::shared_ptr<IOSContext>& ios_context,
std::unique_ptr<SurfaceFrame> frame);
void OnMethodCall(FlutterMethodCall* call, FlutterResult result)
__attribute__((cf_audited_transfer));
// Returns the platform view id if the platform view (or any of its descendant view) is the first
// responder. Returns -1 if no such platform view is found.
long FindFirstResponderPlatformViewId();
// Pushes backdrop filter mutation to the mutator stack of each visited platform view.
void PushFilterToVisitedPlatformViews(const std::shared_ptr<const DlImageFilter>& filter,
const SkRect& filter_rect);
// Pushes the view id of a visted platform view to the list of visied platform views.
void PushVisitedPlatformView(int64_t view_id) { visited_platform_views_.push_back(view_id); }
// Visible for testing.
void CompositeWithParams(int64_t view_id, const EmbeddedViewParams& params);
const EmbeddedViewParams& GetCompositionParams(int64_t view_id) const {
return current_composition_params_.find(view_id)->second;
}
private:
struct LayerData {
SkRect rect;
int64_t view_id;
int64_t overlay_id;
std::shared_ptr<FlutterPlatformViewLayer> layer;
};
using LayersMap = std::unordered_map<int64_t, LayerData>;
// Update the buffers and mutate the platform views in CATransaction.
//
// Runs on the platform thread.
void PerformSubmit(const LayersMap& platform_view_layers,
std::unordered_map<int64_t, EmbeddedViewParams>& current_composition_params,
const std::unordered_set<int64_t>& views_to_recomposite,
const std::vector<int64_t>& composition_order,
const std::vector<std::shared_ptr<FlutterPlatformViewLayer>>& unused_layers,
const std::vector<std::unique_ptr<SurfaceFrame>>& surface_frames);
/// @brief Populate any missing overlay layers.
///
/// This requires posting a task to the platform thread and blocking on its completion.
void CreateMissingOverlays(GrDirectContext* gr_context,
const std::shared_ptr<IOSContext>& ios_context,
size_t required_overlay_layers);
void OnCreate(FlutterMethodCall* call, FlutterResult result) __attribute__((cf_audited_transfer));
void OnDispose(FlutterMethodCall* call, FlutterResult result)
__attribute__((cf_audited_transfer));
void OnAcceptGesture(FlutterMethodCall* call, FlutterResult result)
__attribute__((cf_audited_transfer));
void OnRejectGesture(FlutterMethodCall* call, FlutterResult result)
__attribute__((cf_audited_transfer));
/// @brief Return all views to be disposed on the platform thread.
std::vector<UIView*> GetViewsToDispose();
void ClipViewSetMaskView(UIView* clipView) __attribute__((cf_audited_transfer));
// Applies the mutators in the mutators_stack to the UIView chain that was constructed by
// `ReconstructClipViewsChain`
//
// Clips are applied to the `embedded_view`'s super view(|ChildClippingView|) using a
// |FlutterClippingMaskView|. Transforms are applied to `embedded_view`
//
// The `bounding_rect` is the final bounding rect of the PlatformView
// (EmbeddedViewParams::finalBoundingRect). If a clip mutator's rect contains the final bounding
// rect of the PlatformView, the clip mutator is not applied for performance optimization.
void ApplyMutators(const MutatorsStack& mutators_stack,
UIView* embedded_view,
const SkRect& bounding_rect) __attribute__((cf_audited_transfer));
std::shared_ptr<FlutterPlatformViewLayer> GetExistingLayer();
// Runs on the platform thread.
void CreateLayer(GrDirectContext* gr_context,
const std::shared_ptr<IOSContext>& ios_context,
MTLPixelFormat pixel_format);
// Removes overlay views and platform views that aren't needed in the current frame.
// Must run on the platform thread.
void RemoveUnusedLayers(
const std::vector<std::shared_ptr<FlutterPlatformViewLayer>>& unused_layers,
const std::vector<int64_t>& composition_order);
// Appends the overlay views and platform view and sets their z index based on the composition
// order.
void BringLayersIntoView(const LayersMap& layer_map,
const std::vector<int64_t>& composition_order);
// Resets the state of the frame.
void ResetFrameState();
// The pool of reusable view layers. The pool allows to recycle layer in each frame.
std::unique_ptr<FlutterPlatformViewLayerPool> layer_pool_;
// The platform view's |EmbedderViewSlice| keyed off the view id, which contains any subsequent
// operation until the next platform view or the end of the last leaf node in the layer tree.
//
// The Slices are deleted by the FlutterPlatformViewsController.reset().
std::unordered_map<int64_t, std::unique_ptr<EmbedderViewSlice>> slices_;
fml::scoped_nsobject<FlutterMethodChannel> channel_;
fml::scoped_nsobject<UIView> flutter_view_;
fml::scoped_nsobject<UIViewController<FlutterViewResponder>> flutter_view_controller_;
fml::scoped_nsobject<FlutterClippingMaskViewPool> mask_view_pool_;
std::unordered_map<std::string, fml::scoped_nsobject<NSObject<FlutterPlatformViewFactory>>>
factories_;
// The FlutterPlatformViewGestureRecognizersBlockingPolicy for each type of platform view.
std::unordered_map<std::string, FlutterPlatformViewGestureRecognizersBlockingPolicy>
gesture_recognizers_blocking_policies_;
/// The size of the current onscreen surface in physical pixels.
SkISize frame_size_;
/// The task runner for posting tasks to the platform thread.
fml::RefPtr<fml::TaskRunner> platform_task_runner_;
/// Each of the following structs stores part of the platform view hierarchy according to its
/// ID.
///
/// This data must only be accessed on the platform thread.
struct PlatformViewData {
fml::scoped_nsobject<NSObject<FlutterPlatformView>> view;
fml::scoped_nsobject<FlutterTouchInterceptingView> touch_interceptor;
fml::scoped_nsobject<UIView> root_view;
};
/// This data must only be accessed on the platform thread.
std::unordered_map<int64_t, PlatformViewData> platform_views_;
/// The composition parameters for each platform view.
///
/// This state is only modified on the raster thread.
std::unordered_map<int64_t, EmbeddedViewParams> current_composition_params_;
/// Method channel `OnDispose` calls adds the views to be disposed to this set to be disposed on
/// the next frame.
///
/// This state is modified on both the platform and raster thread.
std::unordered_set<int64_t> views_to_dispose_;
/// view IDs in composition order.
///
/// This state is only modified on the raster thread.
std::vector<int64_t> composition_order_;
/// platform view IDs visited during layer tree composition.
///
/// This state is only modified on the raster thread.
std::vector<int64_t> visited_platform_views_;
/// Only composite platform views in this set.
///
/// This state is only modified on the raster thread.
std::unordered_set<int64_t> views_to_recomposite_;
#if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
/// A set to keep track of embedded views that do not have (0, 0) origin.
/// An insertion triggers a warning message about non-zero origin logged on the debug console.
/// See https://github.com/flutter/flutter/issues/109700 for details.
std::unordered_set<int64_t> non_zero_origin_views_;
#endif
/// @brief The composition order from the previous thread.
///
/// Only accessed from the platform thread.
std::vector<int64_t> previous_composition_order_;
/// Whether the previous frame had any platform views in active composition order.
///
/// This state is tracked so that the first frame after removing the last platform view
/// runs through the platform view rendering code path, giving us a chance to remove the
/// platform view from the UIView hierarchy.
///
/// Only accessed from the raster thread.
bool had_platform_views_ = false;
// WeakPtrFactory must be the last member.
std::unique_ptr<fml::WeakPtrFactory<FlutterPlatformViewsController>> weak_factory_;
FML_DISALLOW_COPY_AND_ASSIGN(FlutterPlatformViewsController);
};
} // namespace flutter
// A UIView that is used as the parent for embedded UIViews.
//
// This view has 2 roles:
@@ -494,7 +138,7 @@ class FlutterPlatformViewsController {
@interface FlutterTouchInterceptingView : UIView
- (instancetype)initWithEmbeddedView:(UIView*)embeddedView
platformViewsController:
(fml::WeakPtr<flutter::FlutterPlatformViewsController>)platformViewsController
(fml::WeakPtr<flutter::PlatformViewsController>)platformViewsController
gestureRecognizersBlockingPolicy:
(FlutterPlatformViewGestureRecognizersBlockingPolicy)blockingPolicy;

View File

@@ -11,38 +11,11 @@
FLUTTER_ASSERT_ARC
static constexpr int kMaxPointsInVerb = 4;
static constexpr NSUInteger kFlutterClippingMaskViewPoolCapacity = 5;
namespace flutter {
FlutterPlatformViewLayer::FlutterPlatformViewLayer(
const fml::scoped_nsobject<UIView>& overlay_view,
const fml::scoped_nsobject<UIView>& overlay_view_wrapper,
std::unique_ptr<IOSSurface> ios_surface,
std::unique_ptr<Surface> surface)
: overlay_view(overlay_view),
overlay_view_wrapper(overlay_view_wrapper),
ios_surface(std::move(ios_surface)),
surface(std::move(surface)){};
FlutterPlatformViewLayer::~FlutterPlatformViewLayer() = default;
FlutterPlatformViewsController::FlutterPlatformViewsController()
: layer_pool_(std::make_unique<FlutterPlatformViewLayerPool>()),
weak_factory_(std::make_unique<fml::WeakPtrFactory<FlutterPlatformViewsController>>(this)) {
mask_view_pool_.reset(
[[FlutterClippingMaskViewPool alloc] initWithCapacity:kFlutterClippingMaskViewPoolCapacity]);
};
FlutterPlatformViewsController::~FlutterPlatformViewsController() = default;
void FlutterPlatformViewsController::SetTaskRunner(
const fml::RefPtr<fml::TaskRunner>& platform_task_runner) {
platform_task_runner_ = platform_task_runner;
}
fml::WeakPtr<flutter::FlutterPlatformViewsController> FlutterPlatformViewsController::GetWeakPtr() {
return weak_factory_->GetWeakPtr();
namespace {
CGRect GetCGRectFromSkRect(const SkRect& clipSkRect) {
return CGRectMake(clipSkRect.fLeft, clipSkRect.fTop, clipSkRect.fRight - clipSkRect.fLeft,
clipSkRect.fBottom - clipSkRect.fTop);
}
CATransform3D GetCATransform3DFromSkMatrix(const SkMatrix& matrix) {
@@ -59,24 +32,7 @@ CATransform3D GetCATransform3DFromSkMatrix(const SkMatrix& matrix) {
transform.m24 = matrix.getPerspY();
return transform;
}
void ResetAnchor(CALayer* layer) {
// Flow uses (0, 0) to apply transform matrix so we need to match that in Quartz.
layer.anchorPoint = CGPointZero;
layer.position = CGPointZero;
}
CGRect GetCGRectFromSkRect(const SkRect& clipSkRect) {
return CGRectMake(clipSkRect.fLeft, clipSkRect.fTop, clipSkRect.fRight - clipSkRect.fLeft,
clipSkRect.fBottom - clipSkRect.fTop);
}
BOOL BlurRadiusEqualToBlurRadius(CGFloat radius1, CGFloat radius2) {
const CGFloat epsilon = 0.01;
return radius1 - radius2 < epsilon;
}
} // namespace flutter
} // namespace
@interface PlatformViewFilter ()
@@ -291,11 +247,11 @@ static BOOL _preparedOnce = NO;
}
- (void)clipRect:(const SkRect&)clipSkRect matrix:(const SkMatrix&)matrix {
CGRect clipRect = flutter::GetCGRectFromSkRect(clipSkRect);
CGRect clipRect = GetCGRectFromSkRect(clipSkRect);
CGPathRef path = CGPathCreateWithRect(clipRect, nil);
// The `matrix` is based on the physical pixels, convert it to UIKit points.
CATransform3D matrixInPoints =
CATransform3DConcat(flutter::GetCATransform3DFromSkMatrix(matrix), _reverseScreenScale);
CATransform3DConcat(GetCATransform3DFromSkMatrix(matrix), _reverseScreenScale);
[self addTransformedPath:path matrix:matrixInPoints];
}
@@ -311,7 +267,7 @@ static BOOL _preparedOnce = NO;
}
case SkRRect::kOval_Type:
case SkRRect::kSimple_Type: {
CGRect clipRect = flutter::GetCGRectFromSkRect(clipSkRRect.rect());
CGRect clipRect = GetCGRectFromSkRect(clipSkRRect.rect());
pathRef = CGPathCreateWithRoundedRect(clipRect, clipSkRRect.getSimpleRadii().x(),
clipSkRRect.getSimpleRadii().y(), nil);
break;
@@ -361,7 +317,7 @@ static BOOL _preparedOnce = NO;
}
// The `matrix` is based on the physical pixels, convert it to UIKit points.
CATransform3D matrixInPoints =
CATransform3DConcat(flutter::GetCATransform3DFromSkMatrix(matrix), _reverseScreenScale);
CATransform3DConcat(GetCATransform3DFromSkMatrix(matrix), _reverseScreenScale);
// TODO(cyanglaz): iOS does not seem to support hard edge on CAShapeLayer. It clearly stated that
// the CAShaperLayer will be drawn antialiased. Need to figure out a way to do the hard edge
// clipping on iOS.
@@ -429,7 +385,7 @@ static BOOL _preparedOnce = NO;
}
// The `matrix` is based on the physical pixels, convert it to UIKit points.
CATransform3D matrixInPoints =
CATransform3DConcat(flutter::GetCATransform3DFromSkMatrix(matrix), _reverseScreenScale);
CATransform3DConcat(GetCATransform3DFromSkMatrix(matrix), _reverseScreenScale);
[self addTransformedPath:pathRef matrix:matrixInPoints];
}
@@ -491,3 +447,272 @@ static BOOL _preparedOnce = NO;
}
@end
@implementation UIView (FirstResponder)
- (BOOL)flt_hasFirstResponderInViewHierarchySubtree {
if (self.isFirstResponder) {
return YES;
}
for (UIView* subview in self.subviews) {
if (subview.flt_hasFirstResponderInViewHierarchySubtree) {
return YES;
}
}
return NO;
}
@end
// This recognizer delays touch events from being dispatched to the responder chain until it failed
// recognizing a gesture.
//
// We only fail this recognizer when asked to do so by the Flutter framework (which does so by
// invoking an acceptGesture method on the platform_views channel). And this is how we allow the
// Flutter framework to delay or prevent the embedded view from getting a touch sequence.
@interface FlutterDelayingGestureRecognizer : UIGestureRecognizer <UIGestureRecognizerDelegate>
// Indicates that if the `FlutterDelayingGestureRecognizer`'s state should be set to
// `UIGestureRecognizerStateEnded` during next `touchesEnded` call.
@property(nonatomic) BOOL shouldEndInNextTouchesEnded;
// Indicates that the `FlutterDelayingGestureRecognizer`'s `touchesEnded` has been invoked without
// setting the state to `UIGestureRecognizerStateEnded`.
@property(nonatomic) BOOL touchedEndedWithoutBlocking;
@property(nonatomic, readonly) UIGestureRecognizer* forwardingRecognizer;
- (instancetype)initWithTarget:(id)target
action:(SEL)action
forwardingRecognizer:(UIGestureRecognizer*)forwardingRecognizer;
@end
// While the FlutterDelayingGestureRecognizer is preventing touches from hitting the responder chain
// the touch events are not arriving to the FlutterView (and thus not arriving to the Flutter
// framework). We use this gesture recognizer to dispatch the events directly to the FlutterView
// while during this phase.
//
// If the Flutter framework decides to dispatch events to the embedded view, we fail the
// FlutterDelayingGestureRecognizer which sends the events up the responder chain. But since the
// events are handled by the embedded view they are not delivered to the Flutter framework in this
// phase as well. So during this phase as well the ForwardingGestureRecognizer dispatched the events
// directly to the FlutterView.
@interface ForwardingGestureRecognizer : UIGestureRecognizer <UIGestureRecognizerDelegate>
- (instancetype)initWithTarget:(id)target
platformViewsController:
(fml::WeakPtr<flutter::PlatformViewsController>)platformViewsController;
@end
@interface FlutterTouchInterceptingView ()
@property(nonatomic, weak, readonly) UIView* embeddedView;
@property(nonatomic, readonly) FlutterDelayingGestureRecognizer* delayingRecognizer;
@property(nonatomic, readonly) FlutterPlatformViewGestureRecognizersBlockingPolicy blockingPolicy;
@end
@implementation FlutterTouchInterceptingView
- (instancetype)initWithEmbeddedView:(UIView*)embeddedView
platformViewsController:
(fml::WeakPtr<flutter::PlatformViewsController>)platformViewsController
gestureRecognizersBlockingPolicy:
(FlutterPlatformViewGestureRecognizersBlockingPolicy)blockingPolicy {
self = [super initWithFrame:embeddedView.frame];
if (self) {
self.multipleTouchEnabled = YES;
_embeddedView = embeddedView;
embeddedView.autoresizingMask =
(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[self addSubview:embeddedView];
ForwardingGestureRecognizer* forwardingRecognizer =
[[ForwardingGestureRecognizer alloc] initWithTarget:self
platformViewsController:platformViewsController];
_delayingRecognizer =
[[FlutterDelayingGestureRecognizer alloc] initWithTarget:self
action:nil
forwardingRecognizer:forwardingRecognizer];
_blockingPolicy = blockingPolicy;
[self addGestureRecognizer:_delayingRecognizer];
[self addGestureRecognizer:forwardingRecognizer];
}
return self;
}
- (void)releaseGesture {
self.delayingRecognizer.state = UIGestureRecognizerStateFailed;
}
- (void)blockGesture {
switch (_blockingPolicy) {
case FlutterPlatformViewGestureRecognizersBlockingPolicyEager:
// We block all other gesture recognizers immediately in this policy.
self.delayingRecognizer.state = UIGestureRecognizerStateEnded;
break;
case FlutterPlatformViewGestureRecognizersBlockingPolicyWaitUntilTouchesEnded:
if (self.delayingRecognizer.touchedEndedWithoutBlocking) {
// If touchesEnded of the `DelayingGesureRecognizer` has been already invoked,
// we want to set the state of the `DelayingGesureRecognizer` to
// `UIGestureRecognizerStateEnded` as soon as possible.
self.delayingRecognizer.state = UIGestureRecognizerStateEnded;
} else {
// If touchesEnded of the `DelayingGesureRecognizer` has not been invoked,
// We will set a flag to notify the `DelayingGesureRecognizer` to set the state to
// `UIGestureRecognizerStateEnded` when touchesEnded is called.
self.delayingRecognizer.shouldEndInNextTouchesEnded = YES;
}
break;
default:
break;
}
}
// We want the intercepting view to consume the touches and not pass the touches up to the parent
// view. Make the touch event method not call super will not pass the touches up to the parent view.
// Hence we overide the touch event methods and do nothing.
- (void)touchesBegan:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
}
- (void)touchesMoved:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
}
- (void)touchesCancelled:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
}
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
}
- (id)accessibilityContainer {
return self.flutterAccessibilityContainer;
}
@end
@implementation FlutterDelayingGestureRecognizer
- (instancetype)initWithTarget:(id)target
action:(SEL)action
forwardingRecognizer:(UIGestureRecognizer*)forwardingRecognizer {
self = [super initWithTarget:target action:action];
if (self) {
self.delaysTouchesBegan = YES;
self.delaysTouchesEnded = YES;
self.delegate = self;
_shouldEndInNextTouchesEnded = NO;
_touchedEndedWithoutBlocking = NO;
_forwardingRecognizer = forwardingRecognizer;
}
return self;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer
shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer {
// The forwarding gesture recognizer should always get all touch events, so it should not be
// required to fail by any other gesture recognizer.
return otherGestureRecognizer != _forwardingRecognizer && otherGestureRecognizer != self;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer
shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer {
return otherGestureRecognizer == self;
}
- (void)touchesBegan:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
self.touchedEndedWithoutBlocking = NO;
[super touchesBegan:touches withEvent:event];
}
- (void)touchesEnded:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
if (self.shouldEndInNextTouchesEnded) {
self.state = UIGestureRecognizerStateEnded;
self.shouldEndInNextTouchesEnded = NO;
} else {
self.touchedEndedWithoutBlocking = YES;
}
[super touchesEnded:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event {
self.state = UIGestureRecognizerStateFailed;
}
@end
@implementation ForwardingGestureRecognizer {
// Weak reference to PlatformViewsController. The PlatformViewsController has
// a reference to the FlutterViewController, where we can dispatch pointer events to.
//
// The lifecycle of PlatformViewsController is bind to FlutterEngine, which should always
// outlives the FlutterViewController. And ForwardingGestureRecognizer is owned by a subview of
// FlutterView, so the ForwardingGestureRecognizer never out lives FlutterViewController.
// Therefore, `_platformViewsController` should never be nullptr.
fml::WeakPtr<flutter::PlatformViewsController> _platformViewsController;
// Counting the pointers that has started in one touch sequence.
NSInteger _currentTouchPointersCount;
// We can't dispatch events to the framework without this back pointer.
// This gesture recognizer retains the `FlutterViewController` until the
// end of a gesture sequence, that is all the touches in touchesBegan are concluded
// with |touchesCancelled| or |touchesEnded|.
fml::scoped_nsobject<UIViewController<FlutterViewResponder>> _flutterViewController;
}
- (instancetype)initWithTarget:(id)target
platformViewsController:
(fml::WeakPtr<flutter::PlatformViewsController>)platformViewsController {
self = [super initWithTarget:target action:nil];
if (self) {
self.delegate = self;
FML_DCHECK(platformViewsController.get() != nullptr);
_platformViewsController = std::move(platformViewsController);
_currentTouchPointersCount = 0;
}
return self;
}
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
FML_DCHECK(_currentTouchPointersCount >= 0);
if (_currentTouchPointersCount == 0) {
// At the start of each gesture sequence, we reset the `_flutterViewController`,
// so that all the touch events in the same sequence are forwarded to the same
// `_flutterViewController`.
_flutterViewController.reset(_platformViewsController->GetFlutterViewController());
}
[_flutterViewController.get() touchesBegan:touches withEvent:event];
_currentTouchPointersCount += touches.count;
}
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
[_flutterViewController.get() touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
[_flutterViewController.get() touchesEnded:touches withEvent:event];
_currentTouchPointersCount -= touches.count;
// Touches in one touch sequence are sent to the touchesEnded method separately if different
// fingers stop touching the screen at different time. So one touchesEnded method triggering does
// not necessarially mean the touch sequence has ended. We Only set the state to
// UIGestureRecognizerStateFailed when all the touches in the current touch sequence is ended.
if (_currentTouchPointersCount == 0) {
self.state = UIGestureRecognizerStateFailed;
_flutterViewController.reset(nil);
}
}
- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event {
// In the event of platform view is removed, iOS generates a "stationary" change type instead of
// "cancelled" change type.
// Flutter needs all the cancelled touches to be "cancelled" change types in order to correctly
// handle gesture sequence.
// We always override the change type to "cancelled".
[_flutterViewController.get() forceTouchesCancelled:touches];
_currentTouchPointersCount -= touches.count;
if (_currentTouchPointersCount == 0) {
self.state = UIGestureRecognizerStateFailed;
_flutterViewController.reset(nil);
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:
(UIGestureRecognizer*)otherGestureRecognizer {
return YES;
}
@end

View File

@@ -18,7 +18,7 @@
- (flutter::Rasterizer::Screenshot)takeScreenshot:(flutter::Rasterizer::ScreenshotType)type
asBase64Encoded:(BOOL)base64Encode;
- (std::shared_ptr<flutter::FlutterPlatformViewsController>&)platformViewsController;
- (std::shared_ptr<flutter::PlatformViewsController>&)platformViewsController;
/**
* A callback that is called when iOS queries accessibility information of the Flutter view.

View File

@@ -2305,7 +2305,7 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch)
#pragma mark - Platform views
- (std::shared_ptr<flutter::FlutterPlatformViewsController>&)platformViewsController {
- (std::shared_ptr<flutter::PlatformViewsController>&)platformViewsController {
return [_engine.get() platformViewsController];
}

View File

@@ -16,7 +16,7 @@
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterViewResponder.h"
namespace flutter {
class FlutterPlatformViewsController;
class PlatformViewsController;
}
FLUTTER_DARWIN_EXPORT
@@ -58,7 +58,7 @@ typedef void (^FlutterKeyboardAnimationCallback)(fml::TimePoint);
@property(nonatomic, assign, readwrite) BOOL prefersStatusBarHidden;
- (fml::WeakNSObject<FlutterViewController>)getWeakNSObject;
- (std::shared_ptr<flutter::FlutterPlatformViewsController>&)platformViewsController;
- (std::shared_ptr<flutter::PlatformViewsController>&)platformViewsController;
- (FlutterRestorationPlugin*)restorationPlugin;
// Accepts keypress events, and then calls |nextAction| if the event was not

View File

@@ -14,12 +14,12 @@ FLUTTER_ASSERT_ARC
@end
@implementation FakeDelegate {
std::shared_ptr<flutter::FlutterPlatformViewsController> _platformViewsController;
std::shared_ptr<flutter::PlatformViewsController> _platformViewsController;
}
- (instancetype)init {
_callbackCalled = NO;
_platformViewsController = std::shared_ptr<flutter::FlutterPlatformViewsController>(nullptr);
_platformViewsController = std::shared_ptr<flutter::PlatformViewsController>(nullptr);
return self;
}
@@ -28,7 +28,7 @@ FLUTTER_ASSERT_ARC
return {};
}
- (std::shared_ptr<flutter::FlutterPlatformViewsController>&)platformViewsController {
- (std::shared_ptr<flutter::PlatformViewsController>&)platformViewsController {
return _platformViewsController;
}

View File

@@ -43,7 +43,7 @@ class MockAccessibilityBridge : public AccessibilityBridgeIos {
}
void AccessibilityObjectDidBecomeFocused(int32_t id) override {}
void AccessibilityObjectDidLoseFocus(int32_t id) override {}
std::shared_ptr<FlutterPlatformViewsController> GetPlatformViewsController() const override {
std::shared_ptr<PlatformViewsController> GetPlatformViewsController() const override {
return nil;
}
std::vector<SemanticsActionObservation> observations;
@@ -74,7 +74,7 @@ class MockAccessibilityBridgeNoWindow : public AccessibilityBridgeIos {
}
void AccessibilityObjectDidBecomeFocused(int32_t id) override {}
void AccessibilityObjectDidLoseFocus(int32_t id) override {}
std::shared_ptr<FlutterPlatformViewsController> GetPlatformViewsController() const override {
std::shared_ptr<PlatformViewsController> GetPlatformViewsController() const override {
return nil;
}
std::vector<SemanticsActionObservation> observations;

View File

@@ -51,7 +51,7 @@ class AccessibilityBridge final : public AccessibilityBridgeIos {
AccessibilityBridge(FlutterViewController* view_controller,
PlatformViewIOS* platform_view,
std::shared_ptr<FlutterPlatformViewsController> platform_views_controller,
std::shared_ptr<PlatformViewsController> platform_views_controller,
std::unique_ptr<IosDelegate> ios_delegate = nullptr);
~AccessibilityBridge();
@@ -73,7 +73,7 @@ class AccessibilityBridge final : public AccessibilityBridgeIos {
fml::WeakPtr<AccessibilityBridge> GetWeakPtr();
std::shared_ptr<FlutterPlatformViewsController> GetPlatformViewsController() const override {
std::shared_ptr<PlatformViewsController> GetPlatformViewsController() const override {
return platform_views_controller_;
};
@@ -92,7 +92,7 @@ class AccessibilityBridge final : public AccessibilityBridgeIos {
FlutterViewController* view_controller_;
PlatformViewIOS* platform_view_;
const std::shared_ptr<FlutterPlatformViewsController> platform_views_controller_;
const std::shared_ptr<PlatformViewsController> platform_views_controller_;
// If the this id is kSemanticObjectIdInvalid, it means either nothing has
// been focused or the focus is currently outside of the flutter application
// (i.e. the status bar or keyboard)

View File

@@ -42,7 +42,7 @@ class DefaultIosDelegate : public AccessibilityBridge::IosDelegate {
AccessibilityBridge::AccessibilityBridge(
FlutterViewController* view_controller,
PlatformViewIOS* platform_view,
std::shared_ptr<FlutterPlatformViewsController> platform_views_controller,
std::shared_ptr<PlatformViewsController> platform_views_controller,
std::unique_ptr<IosDelegate> ios_delegate)
: view_controller_(view_controller),
platform_view_(platform_view),

View File

@@ -14,7 +14,7 @@
@class UIView;
namespace flutter {
class FlutterPlatformViewsController;
class PlatformViewsController;
/// Interface that represents an accessibility bridge for iOS.
class AccessibilityBridgeIos {
@@ -39,7 +39,7 @@ class AccessibilityBridgeIos {
* The input id is the uid of the newly focused SemanticObject.
*/
virtual void AccessibilityObjectDidLoseFocus(int32_t id) = 0;
virtual std::shared_ptr<FlutterPlatformViewsController> GetPlatformViewsController() const = 0;
virtual std::shared_ptr<PlatformViewsController> GetPlatformViewsController() const = 0;
};
} // namespace flutter

View File

@@ -279,8 +279,7 @@ fml::RefPtr<fml::TaskRunner> CreateNewThread(const std::string& name) {
/*ui=*/thread_task_runner,
/*io=*/thread_task_runner);
auto flutterPlatformViewsController =
std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(thread_task_runner);
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -341,8 +340,7 @@ fml::RefPtr<fml::TaskRunner> CreateNewThread(const std::string& name) {
/*ui=*/thread_task_runner,
/*io=*/thread_task_runner);
auto flutterPlatformViewsController =
std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(thread_task_runner);
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -389,7 +387,7 @@ fml::RefPtr<fml::TaskRunner> CreateNewThread(const std::string& name) {
/*ui=*/thread_task_runner,
/*io=*/thread_task_runner);
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(thread_task_runner);
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -486,7 +484,7 @@ fml::RefPtr<fml::TaskRunner> CreateNewThread(const std::string& name) {
/*ui=*/thread_task_runner,
/*io=*/thread_task_runner);
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(thread_task_runner);
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -561,7 +559,7 @@ fml::RefPtr<fml::TaskRunner> CreateNewThread(const std::string& name) {
/*ui=*/thread_task_runner,
/*io=*/thread_task_runner);
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(thread_task_runner);
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
@@ -2181,8 +2179,7 @@ fml::RefPtr<fml::TaskRunner> CreateNewThread(const std::string& name) {
/*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
auto flutterPlatformViewsController =
std::make_shared<flutter::FlutterPlatformViewsController>();
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
flutterPlatformViewsController->SetTaskRunner(thread_task_runner);
OCMStub([mockFlutterViewController platformViewsController])

View File

@@ -0,0 +1,102 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_OVERLAY_LAYER_POOL_H_
#define FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_OVERLAY_LAYER_POOL_H_
#include <Metal/Metal.h>
#include <memory>
#import <UIKit/UIKit.h>
#include "flow/surface.h"
#include "fml/platform/darwin/scoped_nsobject.h"
#import "flutter/shell/platform/darwin/ios/ios_context.h"
namespace flutter {
class IOSSurface;
/// @brief State holder for a Flutter overlay layer.
struct OverlayLayer {
OverlayLayer(const fml::scoped_nsobject<UIView>& overlay_view,
const fml::scoped_nsobject<UIView>& overlay_view_wrapper,
std::unique_ptr<IOSSurface> ios_surface,
std::unique_ptr<Surface> surface);
~OverlayLayer() = default;
fml::scoped_nsobject<UIView> overlay_view;
fml::scoped_nsobject<UIView> overlay_view_wrapper;
std::unique_ptr<IOSSurface> ios_surface;
std::unique_ptr<Surface> surface;
// Whether a frame for this layer was submitted.
bool did_submit_last_frame;
// The GrContext that is currently used by the overlay surfaces.
// We track this to know when the GrContext for the Flutter app has changed
// so we can update the overlay with the new context.
GrDirectContext* gr_context;
void UpdateViewState(UIView* flutter_view, SkRect rect, int64_t view_id, int64_t overlay_id);
};
/// @brief Storage for Overlay layers across frames.
///
/// Note: this class does not synchronize access to its layers or any layer removal. As it
/// is currently used, layers must be created on the platform thread but other methods of
/// it are called on the raster thread. This is safe as overlay layers are only ever added
/// while the raster thread is latched.
class OverlayLayerPool {
public:
OverlayLayerPool() = default;
~OverlayLayerPool() = default;
/// @brief Gets a layer from the pool if available.
///
/// The layer is marked as used until [RecycleLayers] is called.
std::shared_ptr<OverlayLayer> GetNextLayer();
/// @brief Create a new overlay layer.
///
/// This method can only be called on the Platform thread.
void CreateLayer(GrDirectContext* gr_context,
const std::shared_ptr<IOSContext>& ios_context,
MTLPixelFormat pixel_format);
/// @brief Removes unused layers from the pool. Returns the unused layers.
std::vector<std::shared_ptr<OverlayLayer>> RemoveUnusedLayers();
/// @brief Marks the layers in the pool as available for reuse.
void RecycleLayers();
/// @brief The count of layers currently in the pool.
size_t size() const;
private:
OverlayLayerPool(const OverlayLayerPool&) = delete;
OverlayLayerPool& operator=(const OverlayLayerPool&) = delete;
// The index of the entry in the layers_ vector that determines the beginning of the unused
// layers. For example, consider the following vector:
// _____
// | 0 |
/// |---|
/// | 1 | <-- available_layer_index_
/// |---|
/// | 2 |
/// |---|
///
/// This indicates that entries starting from 1 can be reused meanwhile the entry at position 0
/// cannot be reused.
size_t available_layer_index_ = 0;
std::vector<std::shared_ptr<OverlayLayer>> layers_;
};
} // namespace flutter
#endif // FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_OVERLAY_LAYER_POOL_H_

View File

@@ -0,0 +1,136 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/shell/platform/darwin/ios/framework/Source/overlay_layer_pool.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterOverlayView.h"
#import "flutter/shell/platform/darwin/ios/ios_surface.h"
namespace flutter {
OverlayLayer::OverlayLayer(const fml::scoped_nsobject<UIView>& overlay_view,
const fml::scoped_nsobject<UIView>& overlay_view_wrapper,
std::unique_ptr<IOSSurface> ios_surface,
std::unique_ptr<Surface> surface)
: overlay_view(overlay_view),
overlay_view_wrapper(overlay_view_wrapper),
ios_surface(std::move(ios_surface)),
surface(std::move(surface)){};
void OverlayLayer::UpdateViewState(UIView* flutter_view,
SkRect rect,
int64_t view_id,
int64_t overlay_id) {
UIView* overlay_view_wrapper = this->overlay_view_wrapper.get();
auto screenScale = [UIScreen mainScreen].scale;
// Set the size of the overlay view wrapper.
// This wrapper view masks the overlay view.
overlay_view_wrapper.frame = CGRectMake(rect.x() / screenScale, rect.y() / screenScale,
rect.width() / screenScale, rect.height() / screenScale);
// Set a unique view identifier, so the overlay_view_wrapper can be identified in XCUITests.
overlay_view_wrapper.accessibilityIdentifier =
[NSString stringWithFormat:@"platform_view[%lld].overlay[%lld]", view_id, overlay_id];
UIView* overlay_view = this->overlay_view.get();
// Set the size of the overlay view.
// This size is equal to the device screen size.
overlay_view.frame = [flutter_view convertRect:flutter_view.bounds toView:overlay_view_wrapper];
// Set a unique view identifier, so the overlay_view can be identified in XCUITests.
overlay_view.accessibilityIdentifier =
[NSString stringWithFormat:@"platform_view[%lld].overlay_view[%lld]", view_id, overlay_id];
}
// OverlayLayerPool
////////////////////////////////////////////////////////
std::shared_ptr<OverlayLayer> OverlayLayerPool::GetNextLayer() {
std::shared_ptr<OverlayLayer> result;
if (available_layer_index_ < layers_.size()) {
result = layers_[available_layer_index_];
available_layer_index_++;
}
return result;
}
void OverlayLayerPool::CreateLayer(GrDirectContext* gr_context,
const std::shared_ptr<IOSContext>& ios_context,
MTLPixelFormat pixel_format) {
FML_DCHECK([[NSThread currentThread] isMainThread]);
std::shared_ptr<OverlayLayer> layer;
fml::scoped_nsobject<UIView> overlay_view;
fml::scoped_nsobject<UIView> overlay_view_wrapper;
bool impeller_enabled = !!ios_context->GetImpellerContext();
if (!gr_context && !impeller_enabled) {
overlay_view.reset([[FlutterOverlayView alloc] init]);
overlay_view_wrapper.reset([[FlutterOverlayView alloc] init]);
auto ca_layer = fml::scoped_nsobject<CALayer>{[overlay_view.get() layer]};
std::unique_ptr<IOSSurface> ios_surface = IOSSurface::Create(ios_context, ca_layer);
std::unique_ptr<Surface> surface = ios_surface->CreateGPUSurface();
layer = std::make_shared<OverlayLayer>(std::move(overlay_view), std::move(overlay_view_wrapper),
std::move(ios_surface), std::move(surface));
} else {
CGFloat screenScale = [UIScreen mainScreen].scale;
overlay_view.reset([[FlutterOverlayView alloc] initWithContentsScale:screenScale
pixelFormat:pixel_format]);
overlay_view_wrapper.reset([[FlutterOverlayView alloc] initWithContentsScale:screenScale
pixelFormat:pixel_format]);
auto ca_layer = fml::scoped_nsobject<CALayer>{[overlay_view.get() layer]};
std::unique_ptr<IOSSurface> ios_surface = IOSSurface::Create(ios_context, ca_layer);
std::unique_ptr<Surface> surface = ios_surface->CreateGPUSurface(gr_context);
layer = std::make_shared<OverlayLayer>(std::move(overlay_view), std::move(overlay_view_wrapper),
std::move(ios_surface), std::move(surface));
layer->gr_context = gr_context;
}
// The overlay view wrapper masks the overlay view.
// This is required to keep the backing surface size unchanged between frames.
//
// Otherwise, changing the size of the overlay would require a new surface,
// which can be very expensive.
//
// This is the case of an animation in which the overlay size is changing in every frame.
//
// +------------------------+
// | overlay_view |
// | +--------------+ | +--------------+
// | | wrapper | | == mask => | overlay_view |
// | +--------------+ | +--------------+
// +------------------------+
layer->overlay_view_wrapper.get().clipsToBounds = YES;
[layer->overlay_view_wrapper.get() addSubview:layer->overlay_view];
layers_.push_back(layer);
}
void OverlayLayerPool::RecycleLayers() {
available_layer_index_ = 0;
}
std::vector<std::shared_ptr<OverlayLayer>> OverlayLayerPool::RemoveUnusedLayers() {
std::vector<std::shared_ptr<OverlayLayer>> results;
for (size_t i = available_layer_index_; i < layers_.size(); i++) {
results.push_back(layers_[i]);
}
// Leave at least one overlay layer, to work around cases where scrolling
// platform views under an app bar continually adds and removes an
// overlay layer. This logic could be removed if https://github.com/flutter/flutter/issues/150646
// is fixed.
static constexpr size_t kLeakLayerCount = 1;
size_t erase_offset = std::max(available_layer_index_, kLeakLayerCount);
if (erase_offset < layers_.size()) {
layers_.erase(layers_.begin() + erase_offset, layers_.end());
}
return results;
}
size_t OverlayLayerPool::size() const {
return layers_.size();
}
} // namespace flutter

View File

@@ -0,0 +1,324 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_PLATFORM_VIEWS_CONTROLLER_H_
#define FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_PLATFORM_VIEWS_CONTROLLER_H_
#include <Metal/Metal.h>
#include "flutter/flow/surface.h"
#include "flutter/fml/memory/weak_ptr.h"
#include "flutter/fml/platform/darwin/scoped_nsobject.h"
#include "flutter/fml/task_runner.h"
#include "flutter/fml/trace_event.h"
#include "impeller/base/thread_safety.h"
#include "third_party/skia/include/core/SkRect.h"
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h"
#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterPlatformViews.h"
#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterPlugin.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterViewResponder.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/overlay_layer_pool.h"
#import "flutter/shell/platform/darwin/ios/ios_context.h"
@class FlutterTouchInterceptingView;
@class FlutterClippingMaskViewPool;
namespace flutter {
/// @brief Composites Flutter UI and overlay layers alongside embedded UIViews.
class PlatformViewsController {
public:
PlatformViewsController();
~PlatformViewsController() = default;
/// @brief Retrieve a weak pointer to this controller.
fml::WeakPtr<flutter::PlatformViewsController> GetWeakPtr();
/// @brief Set the platform task runner used to post rendering tasks.
void SetTaskRunner(const fml::RefPtr<fml::TaskRunner>& platform_task_runner);
/// @brief Set the flutter view.
void SetFlutterView(UIView* flutter_view) __attribute__((cf_audited_transfer));
/// @brief Set the flutter view controller.
void SetFlutterViewController(UIViewController<FlutterViewResponder>* flutter_view_controller)
__attribute__((cf_audited_transfer));
/// @brief Retrieve the view controller.
UIViewController<FlutterViewResponder>* GetFlutterViewController()
__attribute__((cf_audited_transfer));
/// @brief set the factory used to construct embedded UI Views.
void RegisterViewFactory(
NSObject<FlutterPlatformViewFactory>* factory,
NSString* factoryId,
FlutterPlatformViewGestureRecognizersBlockingPolicy gestureRecognizerBlockingPolicy)
__attribute__((cf_audited_transfer));
/// @brief Mark the beginning of a frame and record the size of the onscreen.
void BeginFrame(SkISize frame_size);
/// @brief Cancel the current frame, indicating that no platform views are composited.
///
/// Additionally, reverts the composition order to its original state at the beginning of the
/// frame.
void CancelFrame();
/// @brief Record a platform view in the layer tree to be rendered, along with the positioning and
/// mutator parameters.
///
/// Called from the raster thread.
void PrerollCompositeEmbeddedView(int64_t view_id,
std::unique_ptr<flutter::EmbeddedViewParams> params);
/// @brief Returns the`FlutterTouchInterceptingView` with the provided view_id.
///
/// Returns nil if there is no platform view with the provided id. Called
/// from the platform thread.
FlutterTouchInterceptingView* GetFlutterTouchInterceptingViewByID(int64_t view_id);
/// @brief Determine if thread merging is required after prerolling platform views.
///
/// Called from the raster thread.
PostPrerollResult PostPrerollAction(
const fml::RefPtr<fml::RasterThreadMerger>& raster_thread_merger);
/// @brief Mark the end of a compositor frame.
///
/// May determine changes are required to the thread merging state.
/// Called from the raster thread.
void EndFrame(bool should_resubmit_frame,
const fml::RefPtr<fml::RasterThreadMerger>& raster_thread_merger);
/// @brief Returns the Canvas for the overlay slice for the given platform view.
///
/// Called from the raster thread.
DlCanvas* CompositeEmbeddedView(int64_t view_id);
/// @brief Discards all platform views instances and auxiliary resources.
///
/// Called from the raster thread.
void Reset();
/// @brief Encode rendering for the Flutter overlay views and queue up perform platform view
/// mutations.
///
/// Called from the raster thread.
bool SubmitFrame(GrDirectContext* gr_context,
const std::shared_ptr<IOSContext>& ios_context,
std::unique_ptr<SurfaceFrame> frame);
/// @brief Handler for platform view message channels.
void OnMethodCall(FlutterMethodCall* call, FlutterResult result)
__attribute__((cf_audited_transfer));
/// @brief Returns the platform view id if the platform view (or any of its descendant view) is
/// the first responder.
///
/// Returns -1 if no such platform view is found.
long FindFirstResponderPlatformViewId();
/// @brief Pushes backdrop filter mutation to the mutator stack of each visited platform view.
void PushFilterToVisitedPlatformViews(const std::shared_ptr<const DlImageFilter>& filter,
const SkRect& filter_rect);
/// @brief Pushes the view id of a visted platform view to the list of visied platform views.
void PushVisitedPlatformView(int64_t view_id) { visited_platform_views_.push_back(view_id); }
// visible for testing.
size_t EmbeddedViewCount() const;
// visible for testing.
size_t LayerPoolSize() const;
// visible for testing.
// Returns the `FlutterPlatformView`'s `view` object associated with the view_id.
//
// If the `PlatformViewsController` does not contain any `FlutterPlatformView` object or
// a `FlutterPlatformView` object associated with the view_id cannot be found, the method
// returns nil.
UIView* GetPlatformViewByID(int64_t view_id);
// Visible for testing.
void CompositeWithParams(int64_t view_id, const EmbeddedViewParams& params);
// Visible for testing.
const EmbeddedViewParams& GetCompositionParams(int64_t view_id) const {
return current_composition_params_.find(view_id)->second;
}
private:
PlatformViewsController(const PlatformViewsController&) = delete;
PlatformViewsController& operator=(const PlatformViewsController&) = delete;
struct LayerData {
SkRect rect;
int64_t view_id;
int64_t overlay_id;
std::shared_ptr<OverlayLayer> layer;
};
using LayersMap = std::unordered_map<int64_t, LayerData>;
// Update the buffers and mutate the platform views in CATransaction.
//
// Runs on the platform thread.
void PerformSubmit(const LayersMap& platform_view_layers,
std::unordered_map<int64_t, EmbeddedViewParams>& current_composition_params,
const std::unordered_set<int64_t>& views_to_recomposite,
const std::vector<int64_t>& composition_order,
const std::vector<std::shared_ptr<OverlayLayer>>& unused_layers,
const std::vector<std::unique_ptr<SurfaceFrame>>& surface_frames);
/// @brief Populate any missing overlay layers.
///
/// This requires posting a task to the platform thread and blocking on its completion.
void CreateMissingOverlays(GrDirectContext* gr_context,
const std::shared_ptr<IOSContext>& ios_context,
size_t required_overlay_layers);
void OnCreate(FlutterMethodCall* call, FlutterResult result) __attribute__((cf_audited_transfer));
void OnDispose(FlutterMethodCall* call, FlutterResult result)
__attribute__((cf_audited_transfer));
void OnAcceptGesture(FlutterMethodCall* call, FlutterResult result)
__attribute__((cf_audited_transfer));
void OnRejectGesture(FlutterMethodCall* call, FlutterResult result)
__attribute__((cf_audited_transfer));
/// @brief Return all views to be disposed on the platform thread.
std::vector<UIView*> GetViewsToDispose();
void ClipViewSetMaskView(UIView* clipView) __attribute__((cf_audited_transfer));
// Applies the mutators in the mutators_stack to the UIView chain that was constructed by
// `ReconstructClipViewsChain`
//
// Clips are applied to the `embedded_view`'s super view(|ChildClippingView|) using a
// |FlutterClippingMaskView|. Transforms are applied to `embedded_view`
//
// The `bounding_rect` is the final bounding rect of the PlatformView
// (EmbeddedViewParams::finalBoundingRect). If a clip mutator's rect contains the final bounding
// rect of the PlatformView, the clip mutator is not applied for performance optimization.
void ApplyMutators(const MutatorsStack& mutators_stack,
UIView* embedded_view,
const SkRect& bounding_rect) __attribute__((cf_audited_transfer));
std::shared_ptr<OverlayLayer> GetExistingLayer();
// Runs on the platform thread.
void CreateLayer(GrDirectContext* gr_context,
const std::shared_ptr<IOSContext>& ios_context,
MTLPixelFormat pixel_format);
// Removes overlay views and platform views that aren't needed in the current frame.
// Must run on the platform thread.
void RemoveUnusedLayers(const std::vector<std::shared_ptr<OverlayLayer>>& unused_layers,
const std::vector<int64_t>& composition_order);
// Appends the overlay views and platform view and sets their z index based on the composition
// order.
void BringLayersIntoView(const LayersMap& layer_map,
const std::vector<int64_t>& composition_order);
// Resets the state of the frame.
void ResetFrameState();
// The pool of reusable view layers. The pool allows to recycle layer in each frame.
std::unique_ptr<OverlayLayerPool> layer_pool_;
// The platform view's |EmbedderViewSlice| keyed off the view id, which contains any subsequent
// operation until the next platform view or the end of the last leaf node in the layer tree.
//
// The Slices are deleted by the PlatformViewsController.reset().
std::unordered_map<int64_t, std::unique_ptr<EmbedderViewSlice>> slices_;
fml::scoped_nsobject<FlutterMethodChannel> channel_;
fml::scoped_nsobject<UIView> flutter_view_;
fml::scoped_nsobject<UIViewController<FlutterViewResponder>> flutter_view_controller_;
fml::scoped_nsobject<FlutterClippingMaskViewPool> mask_view_pool_;
std::unordered_map<std::string, fml::scoped_nsobject<NSObject<FlutterPlatformViewFactory>>>
factories_;
// The FlutterPlatformViewGestureRecognizersBlockingPolicy for each type of platform view.
std::unordered_map<std::string, FlutterPlatformViewGestureRecognizersBlockingPolicy>
gesture_recognizers_blocking_policies_;
/// The size of the current onscreen surface in physical pixels.
SkISize frame_size_;
/// The task runner for posting tasks to the platform thread.
fml::RefPtr<fml::TaskRunner> platform_task_runner_;
/// Each of the following structs stores part of the platform view hierarchy according to its
/// ID.
///
/// This data must only be accessed on the platform thread.
struct PlatformViewData {
fml::scoped_nsobject<NSObject<FlutterPlatformView>> view;
fml::scoped_nsobject<FlutterTouchInterceptingView> touch_interceptor;
fml::scoped_nsobject<UIView> root_view;
};
/// This data must only be accessed on the platform thread.
std::unordered_map<int64_t, PlatformViewData> platform_views_;
/// The composition parameters for each platform view.
///
/// This state is only modified on the raster thread.
std::unordered_map<int64_t, EmbeddedViewParams> current_composition_params_;
/// Method channel `OnDispose` calls adds the views to be disposed to this set to be disposed on
/// the next frame.
///
/// This state is modified on both the platform and raster thread.
std::unordered_set<int64_t> views_to_dispose_;
/// view IDs in composition order.
///
/// This state is only modified on the raster thread.
std::vector<int64_t> composition_order_;
/// platform view IDs visited during layer tree composition.
///
/// This state is only modified on the raster thread.
std::vector<int64_t> visited_platform_views_;
/// Only composite platform views in this set.
///
/// This state is only modified on the raster thread.
std::unordered_set<int64_t> views_to_recomposite_;
#if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
/// A set to keep track of embedded views that do not have (0, 0) origin.
/// An insertion triggers a warning message about non-zero origin logged on the debug console.
/// See https://github.com/flutter/flutter/issues/109700 for details.
std::unordered_set<int64_t> non_zero_origin_views_;
#endif
/// @brief The composition order from the previous thread.
///
/// Only accessed from the platform thread.
std::vector<int64_t> previous_composition_order_;
/// Whether the previous frame had any platform views in active composition order.
///
/// This state is tracked so that the first frame after removing the last platform view
/// runs through the platform view rendering code path, giving us a chance to remove the
/// platform view from the UIView hierarchy.
///
/// Only accessed from the raster thread.
bool had_platform_views_ = false;
// WeakPtrFactory must be the last member.
std::unique_ptr<fml::WeakPtrFactory<PlatformViewsController>> weak_factory_;
};
} // namespace flutter
#endif // FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_PLATFORM_VIEWS_CONTROLLER_H_

View File

@@ -2,22 +2,18 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h"
#include <Metal/Metal.h>
#include <unordered_map>
#import "shell/platform/darwin/ios/framework/Source/platform_views_controller.h"
#include "flow/surface_frame.h"
#include "flutter/flow/view_slicer.h"
#include "flutter/fml/make_copyable.h"
#include "flutter/fml/platform/darwin/scoped_nsobject.h"
#include "fml/synchronization/count_down_latch.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterOverlayView.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterView.h"
#import "flutter/shell/platform/darwin/ios/ios_surface.h"
#include "fml/logging.h"
#include "fml/synchronization/count_down_latch.h"
FLUTTER_ASSERT_ARC
namespace {
#ifdef FML_OS_IOS_SIMULATOR
// The number of frames the rasterizer task runner will continue
@@ -27,19 +23,39 @@ FLUTTER_ASSERT_ARC
static const int kDefaultMergedLeaseDuration = 10;
#endif // FML_OS_IOS_SIMULATOR
@implementation UIView (FirstResponder)
- (BOOL)flt_hasFirstResponderInViewHierarchySubtree {
if (self.isFirstResponder) {
return YES;
}
for (UIView* subview in self.subviews) {
if (subview.flt_hasFirstResponderInViewHierarchySubtree) {
return YES;
}
}
return NO;
static constexpr NSUInteger kFlutterClippingMaskViewPoolCapacity = 5;
// Converts a SkMatrix to CATransform3D.
//
// Certain fields are ignored in CATransform3D since SkMatrix is 3x3 and CATransform3D is 4x4.
CATransform3D GetCATransform3DFromSkMatrix(const SkMatrix& matrix) {
// Skia only supports 2D transform so we don't map z.
CATransform3D transform = CATransform3DIdentity;
transform.m11 = matrix.getScaleX();
transform.m21 = matrix.getSkewX();
transform.m41 = matrix.getTranslateX();
transform.m14 = matrix.getPerspX();
transform.m12 = matrix.getSkewY();
transform.m22 = matrix.getScaleY();
transform.m42 = matrix.getTranslateY();
transform.m24 = matrix.getPerspY();
return transform;
}
// Reset the anchor of `layer` to match the transform operation from flow.
//
// The position of the `layer` should be unchanged after resetting the anchor.
void ResetAnchor(CALayer* layer) {
// Flow uses (0, 0) to apply transform matrix so we need to match that in Quartz.
layer.anchorPoint = CGPointZero;
layer.position = CGPointZero;
}
CGRect GetCGRectFromSkRect(const SkRect& clipSkRect) {
return CGRectMake(clipSkRect.fLeft, clipSkRect.fTop, clipSkRect.fRight - clipSkRect.fLeft,
clipSkRect.fBottom - clipSkRect.fTop);
}
@end
// Determines if the `clip_rect` from a clipRect mutator contains the
// `platformview_boundingrect`.
@@ -49,9 +65,9 @@ static const int kDefaultMergedLeaseDuration = 10;
//
// `platformview_boundingrect` is the final bounding rect of the PlatformView in the coordinate
// space where the PlatformView is displayed.
static bool ClipRectContainsPlatformViewBoundingRect(const SkRect& clip_rect,
const SkRect& platformview_boundingrect,
const SkMatrix& transform_matrix) {
bool ClipRectContainsPlatformViewBoundingRect(const SkRect& clip_rect,
const SkRect& platformview_boundingrect,
const SkMatrix& transform_matrix) {
SkRect transformed_rect = transform_matrix.mapRect(clip_rect);
return transformed_rect.contains(platformview_boundingrect);
}
@@ -64,9 +80,9 @@ static bool ClipRectContainsPlatformViewBoundingRect(const SkRect& clip_rect,
//
// `platformview_boundingrect` is the final bounding rect of the PlatformView in the coordinate
// space where the PlatformView is displayed.
static bool ClipRRectContainsPlatformViewBoundingRect(const SkRRect& clip_rrect,
const SkRect& platformview_boundingrect,
const SkMatrix& transform_matrix) {
bool ClipRRectContainsPlatformViewBoundingRect(const SkRRect& clip_rrect,
const SkRect& platformview_boundingrect,
const SkMatrix& transform_matrix) {
SkVector upper_left = clip_rrect.radii(SkRRect::Corner::kUpperLeft_Corner);
SkVector upper_right = clip_rrect.radii(SkRRect::Corner::kUpperRight_Corner);
SkVector lower_right = clip_rrect.radii(SkRRect::Corner::kLowerRight_Corner);
@@ -89,116 +105,43 @@ static bool ClipRRectContainsPlatformViewBoundingRect(const SkRRect& clip_rrect,
return transformed_rrect.contains(platformview_boundingrect);
}
} // namespace
namespace flutter {
// Becomes NO if Apple's API changes and blurred backdrop filters cannot be applied.
BOOL canApplyBlurBackdrop = YES;
std::shared_ptr<FlutterPlatformViewLayer> FlutterPlatformViewLayerPool::GetNextLayer() {
std::shared_ptr<FlutterPlatformViewLayer> result;
if (available_layer_index_ < layers_.size()) {
result = layers_[available_layer_index_];
available_layer_index_++;
}
PlatformViewsController::PlatformViewsController()
: layer_pool_(std::make_unique<OverlayLayerPool>()),
weak_factory_(std::make_unique<fml::WeakPtrFactory<PlatformViewsController>>(this)) {
mask_view_pool_.reset(
[[FlutterClippingMaskViewPool alloc] initWithCapacity:kFlutterClippingMaskViewPoolCapacity]);
};
return result;
void PlatformViewsController::SetTaskRunner(
const fml::RefPtr<fml::TaskRunner>& platform_task_runner) {
platform_task_runner_ = platform_task_runner;
}
void FlutterPlatformViewLayerPool::CreateLayer(GrDirectContext* gr_context,
const std::shared_ptr<IOSContext>& ios_context,
MTLPixelFormat pixel_format) {
FML_DCHECK([[NSThread currentThread] isMainThread]);
std::shared_ptr<FlutterPlatformViewLayer> layer;
fml::scoped_nsobject<UIView> overlay_view;
fml::scoped_nsobject<UIView> overlay_view_wrapper;
bool impeller_enabled = !!ios_context->GetImpellerContext();
if (!gr_context && !impeller_enabled) {
overlay_view.reset([[FlutterOverlayView alloc] init]);
overlay_view_wrapper.reset([[FlutterOverlayView alloc] init]);
auto ca_layer = fml::scoped_nsobject<CALayer>{[overlay_view.get() layer]};
std::unique_ptr<IOSSurface> ios_surface = IOSSurface::Create(ios_context, ca_layer);
std::unique_ptr<Surface> surface = ios_surface->CreateGPUSurface();
layer = std::make_shared<FlutterPlatformViewLayer>(std::move(overlay_view),
std::move(overlay_view_wrapper),
std::move(ios_surface), std::move(surface));
} else {
CGFloat screenScale = [UIScreen mainScreen].scale;
overlay_view.reset([[FlutterOverlayView alloc] initWithContentsScale:screenScale
pixelFormat:pixel_format]);
overlay_view_wrapper.reset([[FlutterOverlayView alloc] initWithContentsScale:screenScale
pixelFormat:pixel_format]);
auto ca_layer = fml::scoped_nsobject<CALayer>{[overlay_view.get() layer]};
std::unique_ptr<IOSSurface> ios_surface = IOSSurface::Create(ios_context, ca_layer);
std::unique_ptr<Surface> surface = ios_surface->CreateGPUSurface(gr_context);
layer = std::make_shared<FlutterPlatformViewLayer>(std::move(overlay_view),
std::move(overlay_view_wrapper),
std::move(ios_surface), std::move(surface));
layer->gr_context = gr_context;
}
// The overlay view wrapper masks the overlay view.
// This is required to keep the backing surface size unchanged between frames.
//
// Otherwise, changing the size of the overlay would require a new surface,
// which can be very expensive.
//
// This is the case of an animation in which the overlay size is changing in every frame.
//
// +------------------------+
// | overlay_view |
// | +--------------+ | +--------------+
// | | wrapper | | == mask => | overlay_view |
// | +--------------+ | +--------------+
// +------------------------+
layer->overlay_view_wrapper.get().clipsToBounds = YES;
[layer->overlay_view_wrapper.get() addSubview:layer->overlay_view];
layers_.push_back(layer);
fml::WeakPtr<flutter::PlatformViewsController> PlatformViewsController::GetWeakPtr() {
return weak_factory_->GetWeakPtr();
}
void FlutterPlatformViewLayerPool::RecycleLayers() {
available_layer_index_ = 0;
}
std::vector<std::shared_ptr<FlutterPlatformViewLayer>>
FlutterPlatformViewLayerPool::RemoveUnusedLayers() {
std::vector<std::shared_ptr<FlutterPlatformViewLayer>> results;
for (size_t i = available_layer_index_; i < layers_.size(); i++) {
results.push_back(layers_[i]);
}
// Leave at least one overlay layer, to work around cases where scrolling
// platform views under an app bar continually adds and removes an
// overlay layer. This logic could be removed if https://github.com/flutter/flutter/issues/150646
// is fixed.
static constexpr size_t kLeakLayerCount = 1;
size_t erase_offset = std::max(available_layer_index_, kLeakLayerCount);
if (erase_offset < layers_.size()) {
layers_.erase(layers_.begin() + erase_offset, layers_.end());
}
return results;
}
size_t FlutterPlatformViewLayerPool::size() const {
return layers_.size();
}
void FlutterPlatformViewsController::SetFlutterView(UIView* flutter_view) {
void PlatformViewsController::SetFlutterView(UIView* flutter_view) {
flutter_view_.reset(flutter_view);
}
void FlutterPlatformViewsController::SetFlutterViewController(
void PlatformViewsController::SetFlutterViewController(
UIViewController<FlutterViewResponder>* flutter_view_controller) {
flutter_view_controller_.reset(flutter_view_controller);
}
UIViewController<FlutterViewResponder>* FlutterPlatformViewsController::getFlutterViewController() {
UIViewController<FlutterViewResponder>* PlatformViewsController::GetFlutterViewController() {
return flutter_view_controller_.get();
}
void FlutterPlatformViewsController::OnMethodCall(FlutterMethodCall* call, FlutterResult result) {
void PlatformViewsController::OnMethodCall(FlutterMethodCall* call, FlutterResult result) {
if ([[call method] isEqualToString:@"create"]) {
OnCreate(call, result);
} else if ([[call method] isEqualToString:@"dispose"]) {
@@ -212,7 +155,7 @@ void FlutterPlatformViewsController::OnMethodCall(FlutterMethodCall* call, Flutt
}
}
void FlutterPlatformViewsController::OnCreate(FlutterMethodCall* call, FlutterResult result) {
void PlatformViewsController::OnCreate(FlutterMethodCall* call, FlutterResult result) {
NSDictionary<NSString*, id>* args = [call arguments];
int64_t viewId = [args[@"id"] longLongValue];
@@ -279,7 +222,7 @@ void FlutterPlatformViewsController::OnCreate(FlutterMethodCall* call, FlutterRe
result(nil);
}
void FlutterPlatformViewsController::OnDispose(FlutterMethodCall* call, FlutterResult result) {
void PlatformViewsController::OnDispose(FlutterMethodCall* call, FlutterResult result) {
NSNumber* arg = [call arguments];
int64_t viewId = [arg longLongValue];
@@ -294,8 +237,7 @@ void FlutterPlatformViewsController::OnDispose(FlutterMethodCall* call, FlutterR
result(nil);
}
void FlutterPlatformViewsController::OnAcceptGesture(FlutterMethodCall* call,
FlutterResult result) {
void PlatformViewsController::OnAcceptGesture(FlutterMethodCall* call, FlutterResult result) {
NSDictionary<NSString*, id>* args = [call arguments];
int64_t viewId = [args[@"id"] longLongValue];
@@ -312,8 +254,7 @@ void FlutterPlatformViewsController::OnAcceptGesture(FlutterMethodCall* call,
result(nil);
}
void FlutterPlatformViewsController::OnRejectGesture(FlutterMethodCall* call,
FlutterResult result) {
void PlatformViewsController::OnRejectGesture(FlutterMethodCall* call, FlutterResult result) {
NSDictionary<NSString*, id>* args = [call arguments];
int64_t viewId = [args[@"id"] longLongValue];
@@ -330,7 +271,7 @@ void FlutterPlatformViewsController::OnRejectGesture(FlutterMethodCall* call,
result(nil);
}
void FlutterPlatformViewsController::RegisterViewFactory(
void PlatformViewsController::RegisterViewFactory(
NSObject<FlutterPlatformViewFactory>* factory,
NSString* factoryId,
FlutterPlatformViewGestureRecognizersBlockingPolicy gestureRecognizerBlockingPolicy) {
@@ -340,16 +281,16 @@ void FlutterPlatformViewsController::RegisterViewFactory(
gesture_recognizers_blocking_policies_[idString] = gestureRecognizerBlockingPolicy;
}
void FlutterPlatformViewsController::BeginFrame(SkISize frame_size) {
void PlatformViewsController::BeginFrame(SkISize frame_size) {
ResetFrameState();
frame_size_ = frame_size;
}
void FlutterPlatformViewsController::CancelFrame() {
void PlatformViewsController::CancelFrame() {
ResetFrameState();
}
PostPrerollResult FlutterPlatformViewsController::PostPrerollAction(
PostPrerollResult PlatformViewsController::PostPrerollAction(
const fml::RefPtr<fml::RasterThreadMerger>& raster_thread_merger) {
// TODO(jonahwilliams): remove this once Software backend is removed for iOS Sim.
#ifdef FML_OS_IOS_SIMULATOR
@@ -379,7 +320,7 @@ PostPrerollResult FlutterPlatformViewsController::PostPrerollAction(
#endif // FML_OS_IOS_SIMULATOR
}
void FlutterPlatformViewsController::EndFrame(
void PlatformViewsController::EndFrame(
bool should_resubmit_frame,
const fml::RefPtr<fml::RasterThreadMerger>& raster_thread_merger) {
#if FML_OS_IOS_SIMULATOR
@@ -389,7 +330,7 @@ void FlutterPlatformViewsController::EndFrame(
#endif // FML_OS_IOS_SIMULATOR
}
void FlutterPlatformViewsController::PushFilterToVisitedPlatformViews(
void PlatformViewsController::PushFilterToVisitedPlatformViews(
const std::shared_ptr<const DlImageFilter>& filter,
const SkRect& filter_rect) {
for (int64_t id : visited_platform_views_) {
@@ -399,7 +340,7 @@ void FlutterPlatformViewsController::PushFilterToVisitedPlatformViews(
}
}
void FlutterPlatformViewsController::PrerollCompositeEmbeddedView(
void PlatformViewsController::PrerollCompositeEmbeddedView(
int64_t view_id,
std::unique_ptr<EmbeddedViewParams> params) {
SkRect view_bounds = SkRect::Make(frame_size_);
@@ -418,19 +359,19 @@ void FlutterPlatformViewsController::PrerollCompositeEmbeddedView(
views_to_recomposite_.insert(view_id);
}
size_t FlutterPlatformViewsController::EmbeddedViewCount() const {
size_t PlatformViewsController::EmbeddedViewCount() const {
return composition_order_.size();
}
size_t FlutterPlatformViewsController::LayerPoolSize() const {
size_t PlatformViewsController::LayerPoolSize() const {
return layer_pool_->size();
}
UIView* FlutterPlatformViewsController::GetPlatformViewByID(int64_t view_id) {
UIView* PlatformViewsController::GetPlatformViewByID(int64_t view_id) {
return [GetFlutterTouchInterceptingViewByID(view_id) embeddedView];
}
FlutterTouchInterceptingView* FlutterPlatformViewsController::GetFlutterTouchInterceptingViewByID(
FlutterTouchInterceptingView* PlatformViewsController::GetFlutterTouchInterceptingViewByID(
int64_t view_id) {
if (platform_views_.empty()) {
return nil;
@@ -438,7 +379,7 @@ FlutterTouchInterceptingView* FlutterPlatformViewsController::GetFlutterTouchInt
return platform_views_[view_id].touch_interceptor.get();
}
long FlutterPlatformViewsController::FindFirstResponderPlatformViewId() {
long PlatformViewsController::FindFirstResponderPlatformViewId() {
for (auto const& [id, platform_view_data] : platform_views_) {
UIView* root_view = (UIView*)platform_view_data.root_view.get();
if (root_view.flt_hasFirstResponderInViewHierarchySubtree) {
@@ -448,7 +389,7 @@ long FlutterPlatformViewsController::FindFirstResponderPlatformViewId() {
return -1;
}
void FlutterPlatformViewsController::ClipViewSetMaskView(UIView* clipView) {
void PlatformViewsController::ClipViewSetMaskView(UIView* clipView) {
FML_DCHECK([[NSThread currentThread] isMainThread]);
if (clipView.maskView) {
return;
@@ -462,9 +403,9 @@ void FlutterPlatformViewsController::ClipViewSetMaskView(UIView* clipView) {
// This method is only called when the `embedded_view` needs to be re-composited at the current
// frame. See: `CompositeWithParams` for details.
void FlutterPlatformViewsController::ApplyMutators(const MutatorsStack& mutators_stack,
UIView* embedded_view,
const SkRect& bounding_rect) {
void PlatformViewsController::ApplyMutators(const MutatorsStack& mutators_stack,
UIView* embedded_view,
const SkRect& bounding_rect) {
if (flutter_view_ == nullptr) {
return;
}
@@ -525,8 +466,7 @@ void FlutterPlatformViewsController::ApplyMutators(const MutatorsStack& mutators
if (!canApplyBlurBackdrop || !(*iter)->GetFilterMutation().GetFilter().asBlur()) {
break;
}
CGRect filterRect =
flutter::GetCGRectFromSkRect((*iter)->GetFilterMutation().GetFilterRect());
CGRect filterRect = GetCGRectFromSkRect((*iter)->GetFilterMutation().GetFilterRect());
// `filterRect` is in global coordinates. We need to convert to local space.
filterRect = CGRectApplyAffineTransform(
filterRect, CGAffineTransformMakeScale(1 / screenScale, 1 / screenScale));
@@ -579,7 +519,7 @@ void FlutterPlatformViewsController::ApplyMutators(const MutatorsStack& mutators
// the mask view, whose origin is always (0,0) to the flutter_view.
transformMatrix.postTranslate(-clipView.frame.origin.x, -clipView.frame.origin.y);
embedded_view.layer.transform = flutter::GetCATransform3DFromSkMatrix(transformMatrix);
embedded_view.layer.transform = GetCATransform3DFromSkMatrix(transformMatrix);
}
// Composite the PlatformView with `view_id`.
@@ -590,8 +530,8 @@ void FlutterPlatformViewsController::ApplyMutators(const MutatorsStack& mutators
// Note that `views_to_recomposite_` does not represent all the views in the view hierarchy,
// if a PlatformView does not change its composition parameter from last frame, it is not
// included in the `views_to_recomposite_`.
void FlutterPlatformViewsController::CompositeWithParams(int64_t view_id,
const EmbeddedViewParams& params) {
void PlatformViewsController::CompositeWithParams(int64_t view_id,
const EmbeddedViewParams& params) {
CGRect frame = CGRectMake(0, 0, params.sizePoints().width(), params.sizePoints().height());
FlutterTouchInterceptingView* touchInterceptor = platform_views_[view_id].touch_interceptor.get();
#if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
@@ -629,11 +569,11 @@ void FlutterPlatformViewsController::CompositeWithParams(int64_t view_id,
ApplyMutators(mutatorStack, touchInterceptor, rect);
}
DlCanvas* FlutterPlatformViewsController::CompositeEmbeddedView(int64_t view_id) {
DlCanvas* PlatformViewsController::CompositeEmbeddedView(int64_t view_id) {
return slices_[view_id]->canvas();
}
void FlutterPlatformViewsController::Reset() {
void PlatformViewsController::Reset() {
// Reset will only be called from the raster thread or a merged raster/platform thread.
// platform_views_ must only be modified on the platform thread, and any operations that
// read or modify platform views should occur there.
@@ -653,17 +593,17 @@ void FlutterPlatformViewsController::Reset() {
visited_platform_views_.clear();
}
bool FlutterPlatformViewsController::SubmitFrame(GrDirectContext* gr_context,
const std::shared_ptr<IOSContext>& ios_context,
std::unique_ptr<SurfaceFrame> background_frame) {
TRACE_EVENT0("flutter", "FlutterPlatformViewsController::SubmitFrame");
bool PlatformViewsController::SubmitFrame(GrDirectContext* gr_context,
const std::shared_ptr<IOSContext>& ios_context,
std::unique_ptr<SurfaceFrame> background_frame) {
TRACE_EVENT0("flutter", "PlatformViewsController::SubmitFrame");
// No platform views to render; we're done.
if (flutter_view_ == nullptr || (composition_order_.empty() && !had_platform_views_)) {
had_platform_views_ = false;
return background_frame->Submit();
}
had_platform_views_ = true;
had_platform_views_ = !composition_order_.empty();
bool did_encode = true;
LayersMap platform_view_layers;
@@ -698,7 +638,7 @@ bool FlutterPlatformViewsController::SubmitFrame(GrDirectContext* gr_context,
if (overlay == overlay_layers.end()) {
continue;
}
std::shared_ptr<FlutterPlatformViewLayer> layer = GetExistingLayer();
std::shared_ptr<OverlayLayer> layer = GetExistingLayer();
if (!layer) {
continue;
}
@@ -737,8 +677,7 @@ bool FlutterPlatformViewsController::SubmitFrame(GrDirectContext* gr_context,
surface_frames.push_back(std::move(background_frame));
// Mark all layers as available, so they can be used in the next frame.
std::vector<std::shared_ptr<FlutterPlatformViewLayer>> unused_layers =
layer_pool_->RemoveUnusedLayers();
std::vector<std::shared_ptr<OverlayLayer>> unused_layers = layer_pool_->RemoveUnusedLayers();
layer_pool_->RecycleLayers();
auto task = [&, //
@@ -763,11 +702,10 @@ bool FlutterPlatformViewsController::SubmitFrame(GrDirectContext* gr_context,
return did_encode;
}
void FlutterPlatformViewsController::CreateMissingOverlays(
GrDirectContext* gr_context,
const std::shared_ptr<IOSContext>& ios_context,
size_t required_overlay_layers) {
TRACE_EVENT0("flutter", "FlutterPlatformViewsController::CreateMissingLayers");
void PlatformViewsController::CreateMissingOverlays(GrDirectContext* gr_context,
const std::shared_ptr<IOSContext>& ios_context,
size_t required_overlay_layers) {
TRACE_EVENT0("flutter", "PlatformViewsController::CreateMissingLayers");
if (required_overlay_layers <= layer_pool_->size()) {
return;
@@ -792,14 +730,14 @@ void FlutterPlatformViewsController::CreateMissingOverlays(
}
/// Update the buffers and mutate the platform views in CATransaction on the platform thread.
void FlutterPlatformViewsController::PerformSubmit(
void PlatformViewsController::PerformSubmit(
const LayersMap& platform_view_layers,
std::unordered_map<int64_t, EmbeddedViewParams>& current_composition_params,
const std::unordered_set<int64_t>& views_to_recomposite,
const std::vector<int64_t>& composition_order,
const std::vector<std::shared_ptr<FlutterPlatformViewLayer>>& unused_layers,
const std::vector<std::shared_ptr<OverlayLayer>>& unused_layers,
const std::vector<std::unique_ptr<SurfaceFrame>>& surface_frames) {
TRACE_EVENT0("flutter", "FlutterPlatformViewsController::PerformSubmit");
TRACE_EVENT0("flutter", "PlatformViewsController::PerformSubmit");
FML_DCHECK([[NSThread currentThread] isMainThread]);
[CATransaction begin];
@@ -838,9 +776,8 @@ void FlutterPlatformViewsController::PerformSubmit(
[CATransaction commit];
}
void FlutterPlatformViewsController::BringLayersIntoView(
const LayersMap& layer_map,
const std::vector<int64_t>& composition_order) {
void PlatformViewsController::BringLayersIntoView(const LayersMap& layer_map,
const std::vector<int64_t>& composition_order) {
FML_DCHECK(flutter_view_);
UIView* flutter_view = flutter_view_.get();
@@ -876,43 +813,20 @@ void FlutterPlatformViewsController::BringLayersIntoView(
}
}
std::shared_ptr<FlutterPlatformViewLayer> FlutterPlatformViewsController::GetExistingLayer() {
std::shared_ptr<OverlayLayer> PlatformViewsController::GetExistingLayer() {
return layer_pool_->GetNextLayer();
}
void FlutterPlatformViewsController::CreateLayer(GrDirectContext* gr_context,
const std::shared_ptr<IOSContext>& ios_context,
MTLPixelFormat pixel_format) {
void PlatformViewsController::CreateLayer(GrDirectContext* gr_context,
const std::shared_ptr<IOSContext>& ios_context,
MTLPixelFormat pixel_format) {
layer_pool_->CreateLayer(gr_context, ios_context, pixel_format);
}
void FlutterPlatformViewLayer::UpdateViewState(UIView* flutter_view,
SkRect rect,
int64_t view_id,
int64_t overlay_id) {
UIView* overlay_view_wrapper = this->overlay_view_wrapper.get();
auto screenScale = [UIScreen mainScreen].scale;
// Set the size of the overlay view wrapper.
// This wrapper view masks the overlay view.
overlay_view_wrapper.frame = CGRectMake(rect.x() / screenScale, rect.y() / screenScale,
rect.width() / screenScale, rect.height() / screenScale);
// Set a unique view identifier, so the overlay_view_wrapper can be identified in XCUITests.
overlay_view_wrapper.accessibilityIdentifier =
[NSString stringWithFormat:@"platform_view[%lld].overlay[%lld]", view_id, overlay_id];
UIView* overlay_view = this->overlay_view.get();
// Set the size of the overlay view.
// This size is equal to the device screen size.
overlay_view.frame = [flutter_view convertRect:flutter_view.bounds toView:overlay_view_wrapper];
// Set a unique view identifier, so the overlay_view can be identified in XCUITests.
overlay_view.accessibilityIdentifier =
[NSString stringWithFormat:@"platform_view[%lld].overlay_view[%lld]", view_id, overlay_id];
}
void FlutterPlatformViewsController::RemoveUnusedLayers(
const std::vector<std::shared_ptr<FlutterPlatformViewLayer>>& unused_layers,
void PlatformViewsController::RemoveUnusedLayers(
const std::vector<std::shared_ptr<OverlayLayer>>& unused_layers,
const std::vector<int64_t>& composition_order) {
for (const std::shared_ptr<FlutterPlatformViewLayer>& layer : unused_layers) {
for (const std::shared_ptr<OverlayLayer>& layer : unused_layers) {
[layer->overlay_view_wrapper removeFromSuperview];
}
@@ -929,7 +843,7 @@ void FlutterPlatformViewsController::RemoveUnusedLayers(
}
}
std::vector<UIView*> FlutterPlatformViewsController::GetViewsToDispose() {
std::vector<UIView*> PlatformViewsController::GetViewsToDispose() {
std::vector<UIView*> views;
if (views_to_dispose_.empty()) {
return views;
@@ -953,264 +867,10 @@ std::vector<UIView*> FlutterPlatformViewsController::GetViewsToDispose() {
return views;
}
void FlutterPlatformViewsController::ResetFrameState() {
void PlatformViewsController::ResetFrameState() {
slices_.clear();
composition_order_.clear();
visited_platform_views_.clear();
}
} // namespace flutter
// This recognizers delays touch events from being dispatched to the responder chain until it failed
// recognizing a gesture.
//
// We only fail this recognizer when asked to do so by the Flutter framework (which does so by
// invoking an acceptGesture method on the platform_views channel). And this is how we allow the
// Flutter framework to delay or prevent the embedded view from getting a touch sequence.
@interface DelayingGestureRecognizer : UIGestureRecognizer <UIGestureRecognizerDelegate>
// Indicates that if the `DelayingGestureRecognizer`'s state should be set to
// `UIGestureRecognizerStateEnded` during next `touchesEnded` call.
@property(nonatomic) BOOL shouldEndInNextTouchesEnded;
// Indicates that the `DelayingGestureRecognizer`'s `touchesEnded` has been invoked without
// setting the state to `UIGestureRecognizerStateEnded`.
@property(nonatomic) BOOL touchedEndedWithoutBlocking;
@property(nonatomic, readonly) UIGestureRecognizer* forwardingRecognizer;
- (instancetype)initWithTarget:(id)target
action:(SEL)action
forwardingRecognizer:(UIGestureRecognizer*)forwardingRecognizer;
@end
// While the DelayingGestureRecognizer is preventing touches from hitting the responder chain
// the touch events are not arriving to the FlutterView (and thus not arriving to the Flutter
// framework). We use this gesture recognizer to dispatch the events directly to the FlutterView
// while during this phase.
//
// If the Flutter framework decides to dispatch events to the embedded view, we fail the
// DelayingGestureRecognizer which sends the events up the responder chain. But since the events
// are handled by the embedded view they are not delivered to the Flutter framework in this phase
// as well. So during this phase as well the ForwardingGestureRecognizer dispatched the events
// directly to the FlutterView.
@interface ForwardingGestureRecognizer : UIGestureRecognizer <UIGestureRecognizerDelegate>
- (instancetype)initWithTarget:(id)target
platformViewsController:
(fml::WeakPtr<flutter::FlutterPlatformViewsController>)platformViewsController;
@end
@interface FlutterTouchInterceptingView ()
@property(nonatomic, weak, readonly) UIView* embeddedView;
@property(nonatomic, readonly) DelayingGestureRecognizer* delayingRecognizer;
@property(nonatomic, readonly) FlutterPlatformViewGestureRecognizersBlockingPolicy blockingPolicy;
@end
@implementation FlutterTouchInterceptingView
- (instancetype)initWithEmbeddedView:(UIView*)embeddedView
platformViewsController:
(fml::WeakPtr<flutter::FlutterPlatformViewsController>)platformViewsController
gestureRecognizersBlockingPolicy:
(FlutterPlatformViewGestureRecognizersBlockingPolicy)blockingPolicy {
self = [super initWithFrame:embeddedView.frame];
if (self) {
self.multipleTouchEnabled = YES;
_embeddedView = embeddedView;
embeddedView.autoresizingMask =
(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[self addSubview:embeddedView];
ForwardingGestureRecognizer* forwardingRecognizer =
[[ForwardingGestureRecognizer alloc] initWithTarget:self
platformViewsController:platformViewsController];
_delayingRecognizer = [[DelayingGestureRecognizer alloc] initWithTarget:self
action:nil
forwardingRecognizer:forwardingRecognizer];
_blockingPolicy = blockingPolicy;
[self addGestureRecognizer:_delayingRecognizer];
[self addGestureRecognizer:forwardingRecognizer];
}
return self;
}
- (void)releaseGesture {
self.delayingRecognizer.state = UIGestureRecognizerStateFailed;
}
- (void)blockGesture {
switch (_blockingPolicy) {
case FlutterPlatformViewGestureRecognizersBlockingPolicyEager:
// We block all other gesture recognizers immediately in this policy.
self.delayingRecognizer.state = UIGestureRecognizerStateEnded;
break;
case FlutterPlatformViewGestureRecognizersBlockingPolicyWaitUntilTouchesEnded:
if (self.delayingRecognizer.touchedEndedWithoutBlocking) {
// If touchesEnded of the `DelayingGesureRecognizer` has been already invoked,
// we want to set the state of the `DelayingGesureRecognizer` to
// `UIGestureRecognizerStateEnded` as soon as possible.
self.delayingRecognizer.state = UIGestureRecognizerStateEnded;
} else {
// If touchesEnded of the `DelayingGesureRecognizer` has not been invoked,
// We will set a flag to notify the `DelayingGesureRecognizer` to set the state to
// `UIGestureRecognizerStateEnded` when touchesEnded is called.
self.delayingRecognizer.shouldEndInNextTouchesEnded = YES;
}
break;
default:
break;
}
}
// We want the intercepting view to consume the touches and not pass the touches up to the parent
// view. Make the touch event method not call super will not pass the touches up to the parent view.
// Hence we overide the touch event methods and do nothing.
- (void)touchesBegan:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
}
- (void)touchesMoved:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
}
- (void)touchesCancelled:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
}
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
}
- (id)accessibilityContainer {
return self.flutterAccessibilityContainer;
}
@end
@implementation DelayingGestureRecognizer
- (instancetype)initWithTarget:(id)target
action:(SEL)action
forwardingRecognizer:(UIGestureRecognizer*)forwardingRecognizer {
self = [super initWithTarget:target action:action];
if (self) {
self.delaysTouchesBegan = YES;
self.delaysTouchesEnded = YES;
self.delegate = self;
_shouldEndInNextTouchesEnded = NO;
_touchedEndedWithoutBlocking = NO;
_forwardingRecognizer = forwardingRecognizer;
}
return self;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer
shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer {
// The forwarding gesture recognizer should always get all touch events, so it should not be
// required to fail by any other gesture recognizer.
return otherGestureRecognizer != _forwardingRecognizer && otherGestureRecognizer != self;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer
shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer {
return otherGestureRecognizer == self;
}
- (void)touchesBegan:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
self.touchedEndedWithoutBlocking = NO;
[super touchesBegan:touches withEvent:event];
}
- (void)touchesEnded:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
if (self.shouldEndInNextTouchesEnded) {
self.state = UIGestureRecognizerStateEnded;
self.shouldEndInNextTouchesEnded = NO;
} else {
self.touchedEndedWithoutBlocking = YES;
}
[super touchesEnded:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event {
self.state = UIGestureRecognizerStateFailed;
}
@end
@implementation ForwardingGestureRecognizer {
// Weak reference to FlutterPlatformViewsController. The FlutterPlatformViewsController has
// a reference to the FlutterViewController, where we can dispatch pointer events to.
//
// The lifecycle of FlutterPlatformViewsController is bind to FlutterEngine, which should always
// outlives the FlutterViewController. And ForwardingGestureRecognizer is owned by a subview of
// FlutterView, so the ForwardingGestureRecognizer never out lives FlutterViewController.
// Therefore, `_platformViewsController` should never be nullptr.
fml::WeakPtr<flutter::FlutterPlatformViewsController> _platformViewsController;
// Counting the pointers that has started in one touch sequence.
NSInteger _currentTouchPointersCount;
// We can't dispatch events to the framework without this back pointer.
// This gesture recognizer retains the `FlutterViewController` until the
// end of a gesture sequence, that is all the touches in touchesBegan are concluded
// with |touchesCancelled| or |touchesEnded|.
fml::scoped_nsobject<UIViewController<FlutterViewResponder>> _flutterViewController;
}
- (instancetype)initWithTarget:(id)target
platformViewsController:
(fml::WeakPtr<flutter::FlutterPlatformViewsController>)platformViewsController {
self = [super initWithTarget:target action:nil];
if (self) {
self.delegate = self;
FML_DCHECK(platformViewsController.get() != nullptr);
_platformViewsController = std::move(platformViewsController);
_currentTouchPointersCount = 0;
}
return self;
}
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
FML_DCHECK(_currentTouchPointersCount >= 0);
if (_currentTouchPointersCount == 0) {
// At the start of each gesture sequence, we reset the `_flutterViewController`,
// so that all the touch events in the same sequence are forwarded to the same
// `_flutterViewController`.
_flutterViewController.reset(_platformViewsController->getFlutterViewController());
}
[_flutterViewController.get() touchesBegan:touches withEvent:event];
_currentTouchPointersCount += touches.count;
}
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
[_flutterViewController.get() touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
[_flutterViewController.get() touchesEnded:touches withEvent:event];
_currentTouchPointersCount -= touches.count;
// Touches in one touch sequence are sent to the touchesEnded method separately if different
// fingers stop touching the screen at different time. So one touchesEnded method triggering does
// not necessarially mean the touch sequence has ended. We Only set the state to
// UIGestureRecognizerStateFailed when all the touches in the current touch sequence is ended.
if (_currentTouchPointersCount == 0) {
self.state = UIGestureRecognizerStateFailed;
_flutterViewController.reset(nil);
}
}
- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event {
// In the event of platform view is removed, iOS generates a "stationary" change type instead of
// "cancelled" change type.
// Flutter needs all the cancelled touches to be "cancelled" change types in order to correctly
// handle gesture sequence.
// We always override the change type to "cancelled".
[_flutterViewController.get() forceTouchesCancelled:touches];
_currentTouchPointersCount -= touches.count;
if (_currentTouchPointersCount == 0) {
self.state = UIGestureRecognizerStateFailed;
_flutterViewController.reset(nil);
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:
(UIGestureRecognizer*)otherGestureRecognizer {
return YES;
}
@end

View File

@@ -12,16 +12,15 @@ namespace flutter {
class IOSExternalViewEmbedder : public ExternalViewEmbedder {
public:
IOSExternalViewEmbedder(const std::shared_ptr<FlutterPlatformViewsController>&
platform_views_controller,
const std::shared_ptr<IOSContext>& context);
IOSExternalViewEmbedder(
const std::shared_ptr<PlatformViewsController>& platform_views_controller,
const std::shared_ptr<IOSContext>& context);
// |ExternalViewEmbedder|
virtual ~IOSExternalViewEmbedder() override;
private:
const std::shared_ptr<FlutterPlatformViewsController>&
platform_views_controller_;
const std::shared_ptr<PlatformViewsController>& platform_views_controller_;
std::shared_ptr<IOSContext> ios_context_;
// |ExternalViewEmbedder|

View File

@@ -12,7 +12,7 @@ FLUTTER_ASSERT_ARC
namespace flutter {
IOSExternalViewEmbedder::IOSExternalViewEmbedder(
const std::shared_ptr<FlutterPlatformViewsController>& platform_views_controller,
const std::shared_ptr<PlatformViewsController>& platform_views_controller,
const std::shared_ptr<IOSContext>& context)
: platform_views_controller_(platform_views_controller), ios_context_(context) {
FML_CHECK(ios_context_);

View File

@@ -42,13 +42,13 @@ class PlatformViewIOS final : public PlatformView {
public:
PlatformViewIOS(PlatformView::Delegate& delegate,
const std::shared_ptr<IOSContext>& context,
const std::shared_ptr<FlutterPlatformViewsController>& platform_views_controller,
const std::shared_ptr<PlatformViewsController>& platform_views_controller,
const flutter::TaskRunners& task_runners);
explicit PlatformViewIOS(
PlatformView::Delegate& delegate,
IOSRenderingAPI rendering_api,
const std::shared_ptr<FlutterPlatformViewsController>& platform_views_controller,
const std::shared_ptr<PlatformViewsController>& platform_views_controller,
const flutter::TaskRunners& task_runners,
const std::shared_ptr<fml::ConcurrentTaskRunner>& worker_task_runner,
const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch);
@@ -139,7 +139,7 @@ class PlatformViewIOS final : public PlatformView {
std::mutex ios_surface_mutex_;
std::unique_ptr<IOSSurface> ios_surface_;
std::shared_ptr<IOSContext> ios_context_;
const std::shared_ptr<FlutterPlatformViewsController>& platform_views_controller_;
const std::shared_ptr<PlatformViewsController>& platform_views_controller_;
AccessibilityBridgeManager accessibility_bridge_;
fml::scoped_nsprotocol<FlutterTextInputPlugin*> text_input_plugin_;
ScopedObserver dealloc_view_controller_observer_;

View File

@@ -42,7 +42,7 @@ void PlatformViewIOS::AccessibilityBridgeManager::Clear() {
PlatformViewIOS::PlatformViewIOS(
PlatformView::Delegate& delegate,
const std::shared_ptr<IOSContext>& context,
const std::shared_ptr<FlutterPlatformViewsController>& platform_views_controller,
const std::shared_ptr<PlatformViewsController>& platform_views_controller,
const flutter::TaskRunners& task_runners)
: PlatformView(delegate, task_runners),
ios_context_(context),
@@ -54,7 +54,7 @@ PlatformViewIOS::PlatformViewIOS(
PlatformViewIOS::PlatformViewIOS(
PlatformView::Delegate& delegate,
IOSRenderingAPI rendering_api,
const std::shared_ptr<FlutterPlatformViewsController>& platform_views_controller,
const std::shared_ptr<PlatformViewsController>& platform_views_controller,
const flutter::TaskRunners& task_runners,
const std::shared_ptr<fml::ConcurrentTaskRunner>& worker_task_runner,
const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch)