iOS FlutterTextureRegistry should be a proxy, not the engine itself (flutter/engine#37666)

* feat: add texture registry relay.

* fix: add test.

* fix: format

* fix: add comment.

* fix: remove useless import.

* fix: remove

* fix: file name order.

* fix.

* add test for retain cycle.

* fix compile

* Add comments.

* remove relay.

* add FlutterTextureRegistryRelay in gn

* add golden files.

* add

* add comments.
This commit is contained in:
JiaJian
2023-01-10 02:51:13 +08:00
committed by GitHub
parent 0e37dda533
commit 5a17cf21d2
8 changed files with 163 additions and 6 deletions

View File

@@ -2519,6 +2519,9 @@ ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextI
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextureRegistryRelay.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextureRegistryRelay.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextureRegistryRelayTest.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterUIPressProxy.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterUIPressProxy.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterUmbrellaImport.m + ../../../flutter/LICENSE
@@ -5000,6 +5003,9 @@ FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInp
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextureRegistryRelay.h
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextureRegistryRelay.mm
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextureRegistryRelayTest.mm
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterUIPressProxy.h
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterUIPressProxy.mm
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterUmbrellaImport.m

View File

@@ -106,6 +106,8 @@ source_set("flutter_framework_source") {
"framework/Source/FlutterSemanticsScrollView.mm",
"framework/Source/FlutterSpellCheckPlugin.h",
"framework/Source/FlutterSpellCheckPlugin.mm",
"framework/Source/FlutterTextureRegistryRelay.h",
"framework/Source/FlutterTextureRegistryRelay.mm",
"framework/Source/FlutterUIPressProxy.h",
"framework/Source/FlutterUIPressProxy.mm",
"framework/Source/FlutterUndoManagerDelegate.h",
@@ -282,6 +284,7 @@ shared_library("ios_test_flutter") {
"framework/Source/FlutterRestorationPluginTest.mm",
"framework/Source/FlutterSpellCheckPluginTest.mm",
"framework/Source/FlutterTextInputPluginTest.mm",
"framework/Source/FlutterTextureRegistryRelayTest.mm",
"framework/Source/FlutterUndoManagerPluginTest.mm",
"framework/Source/FlutterViewControllerTest.mm",
"framework/Source/SemanticsObjectTest.mm",

View File

@@ -56,7 +56,7 @@ extern NSString* const FlutterDefaultInitialRoute;
* One of these methods must be invoked before calling `-setViewController:`.
*/
FLUTTER_DARWIN_EXPORT
@interface FlutterEngine : NSObject <FlutterTextureRegistry, FlutterPluginRegistry>
@interface FlutterEngine : NSObject <FlutterPluginRegistry>
/**
* Default initializer for a FlutterEngine.
@@ -424,6 +424,11 @@ FLUTTER_DARWIN_EXPORT
*/
@property(nonatomic, readonly) NSObject<FlutterBinaryMessenger>* binaryMessenger;
/**
* The `FlutterTextureRegistry` associated with this FlutterEngine (used to register textures).
*/
@property(nonatomic, readonly) NSObject<FlutterTextureRegistry>* textureRegistry;
/**
* The UI Isolate ID of the engine.
*

View File

@@ -26,6 +26,7 @@
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterSpellCheckPlugin.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputDelegate.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextureRegistryRelay.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerDelegate.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerPlugin.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h"
@@ -87,7 +88,8 @@ static constexpr int kNumProfilerSamplesPerSec = 5;
@interface FlutterEngine () <FlutterIndirectScribbleDelegate,
FlutterUndoManagerDelegate,
FlutterTextInputDelegate,
FlutterBinaryMessenger>
FlutterBinaryMessenger,
FlutterTextureRegistry>
// Maintains a dictionary of plugin names that have registered with the engine. Used by
// FlutterEngineRegistrar to implement a FlutterPluginRegistrar.
@property(nonatomic, readonly) NSMutableDictionary* pluginPublications;
@@ -138,6 +140,7 @@ static constexpr int kNumProfilerSamplesPerSec = 5;
BOOL _allowHeadlessExecution;
BOOL _restorationEnabled;
FlutterBinaryMessengerRelay* _binaryMessenger;
FlutterTextureRegistryRelay* _textureRegistry;
std::unique_ptr<flutter::ConnectionCollection> _connections;
}
@@ -197,6 +200,7 @@ static constexpr int kNumProfilerSamplesPerSec = 5;
[self recreatePlatformViewController];
_binaryMessenger = [[FlutterBinaryMessengerRelay alloc] initWithParent:self];
_textureRegistry = [[FlutterTextureRegistryRelay alloc] initWithParent:self];
_connections.reset(new flutter::ConnectionCollection());
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
@@ -261,7 +265,10 @@ static constexpr int kNumProfilerSamplesPerSec = 5;
[_pluginPublications release];
[_registrars release];
_binaryMessenger.parent = nil;
_textureRegistry.parent = nil;
[_binaryMessenger release];
[_textureRegistry release];
_textureRegistry = nil;
[_isolateId release];
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
@@ -1073,6 +1080,10 @@ static void SetEntryPoint(flutter::Settings* settings, NSString* entrypoint, NSS
return _binaryMessenger;
}
- (NSObject<FlutterTextureRegistry>*)textureRegistry {
return _textureRegistry;
}
// For test only. Ideally we should create a dependency injector for all dependencies and
// remove this.
- (void)setBinaryMessenger:(FlutterBinaryMessengerRelay*)binaryMessenger {
@@ -1333,7 +1344,7 @@ static void SetEntryPoint(flutter::Settings* settings, NSString* entrypoint, NSS
}
- (NSObject<FlutterTextureRegistry>*)textures {
return _flutterEngine;
return _flutterEngine.textureRegistry;
}
- (void)publish:(NSObject*)value {

View File

@@ -0,0 +1,24 @@
// 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.
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterTexture.h"
#if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
FLUTTER_DARWIN_EXPORT
#endif
/**
* Wrapper around a weakly held collection of registered textures.
*
* Avoids a retain cycle between plugins and the engine.
*/
@interface FlutterTextureRegistryRelay : NSObject <FlutterTextureRegistry>
/**
* A weak reference to a FlutterEngine that will be passed texture registration.
*/
@property(nonatomic, assign) NSObject<FlutterTextureRegistry>* parent;
- (instancetype)initWithParent:(NSObject<FlutterTextureRegistry>*)parent;
@end

View File

@@ -0,0 +1,42 @@
// 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.
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextureRegistryRelay.h"
#include "flutter/fml/logging.h"
@implementation FlutterTextureRegistryRelay : NSObject
#pragma mark - FlutterTextureRegistry
- (instancetype)initWithParent:(NSObject<FlutterTextureRegistry>*)parent {
if (self = [super init]) {
_parent = parent;
}
return self;
}
- (int64_t)registerTexture:(NSObject<FlutterTexture>*)texture {
if (!self.parent) {
FML_LOG(WARNING) << "Using on an empty registry.";
return 0;
}
return [self.parent registerTexture:texture];
}
- (void)textureFrameAvailable:(int64_t)textureId {
if (!self.parent) {
FML_LOG(WARNING) << "Using on an empty registry.";
}
return [self.parent textureFrameAvailable:textureId];
}
- (void)unregisterTexture:(int64_t)textureId {
if (!self.parent) {
FML_LOG(WARNING) << "Using on an empty registry.";
}
return [self.parent unregisterTexture:textureId];
}
@end

View File

@@ -0,0 +1,66 @@
// 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.
#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterEngine.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextureRegistryRelay.h"
#import <OCMock/OCMock.h>
#import <XCTest/XCTest.h>
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterTexture.h"
FLUTTER_ASSERT_ARC
@interface FlutterTextureRegistryRelayTest : XCTestCase
@end
@implementation FlutterTextureRegistryRelayTest
- (void)testCreate {
id textureRegistry = OCMProtocolMock(@protocol(FlutterTextureRegistry));
FlutterTextureRegistryRelay* relay =
[[FlutterTextureRegistryRelay alloc] initWithParent:textureRegistry];
XCTAssertNotNil(relay);
XCTAssertEqual(textureRegistry, relay.parent);
}
- (void)testRegisterTexture {
id textureRegistry = OCMProtocolMock(@protocol(FlutterTextureRegistry));
FlutterTextureRegistryRelay* relay =
[[FlutterTextureRegistryRelay alloc] initWithParent:textureRegistry];
id texture = OCMProtocolMock(@protocol(FlutterTexture));
[relay registerTexture:texture];
OCMVerify([textureRegistry registerTexture:texture]);
}
- (void)testTextureFrameAvailable {
id textureRegistry = OCMProtocolMock(@protocol(FlutterTextureRegistry));
FlutterTextureRegistryRelay* relay =
[[FlutterTextureRegistryRelay alloc] initWithParent:textureRegistry];
[relay textureFrameAvailable:0];
OCMVerify([textureRegistry textureFrameAvailable:0]);
}
- (void)testUnregisterTexture {
id textureRegistry = OCMProtocolMock(@protocol(FlutterTextureRegistry));
FlutterTextureRegistryRelay* relay =
[[FlutterTextureRegistryRelay alloc] initWithParent:textureRegistry];
[relay unregisterTexture:0];
OCMVerify([textureRegistry unregisterTexture:0]);
}
- (void)testRetainCycle {
__weak FlutterEngine* weakEngine;
NSObject<FlutterTextureRegistry>* strongRelay;
@autoreleasepool {
id project = OCMClassMock([FlutterDartProject class]);
FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"foobar" project:project];
strongRelay = [engine textureRegistry];
weakEngine = engine;
}
XCTAssertNil(weakEngine);
}
@end

View File

@@ -2022,15 +2022,15 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch)
#pragma mark - FlutterTextureRegistry
- (int64_t)registerTexture:(NSObject<FlutterTexture>*)texture {
return [_engine.get() registerTexture:texture];
return [_engine.get().textureRegistry registerTexture:texture];
}
- (void)unregisterTexture:(int64_t)textureId {
[_engine.get() unregisterTexture:textureId];
[_engine.get().textureRegistry unregisterTexture:textureId];
}
- (void)textureFrameAvailable:(int64_t)textureId {
[_engine.get() textureFrameAvailable:textureId];
[_engine.get().textureRegistry textureFrameAvailable:textureId];
}
- (NSString*)lookupKeyForAsset:(NSString*)asset {