From 4a01af4737302ad54b6a4a8e3ef98fdc84e2234e Mon Sep 17 00:00:00 2001 From: Tong Mu Date: Sun, 21 May 2023 16:53:57 -0700 Subject: [PATCH] [macOS] Clean up unused methods in FlutterRenderer (flutter/engine#42196) This PR removes the methods in `FlutterRenderer` that will never be used. These methods allows the engine to get a drawable and present a drawable, but they will never be called on the macOS embedder. This is because they go through the `FlutterMetalRendererConfig` struct to `GPUSurfaceMetalSkia`, which is bypassed if the engine has a non-null external view embedder, which the macOS always provides. Removing these methods not only cleans up the code (and the confusion), but also reduces the methods that might need to be migrated for the multi-view project. After this change, `FlutterRenderer` is left with very few functionalities: * It hosts `device` and `commandQueue`. * It creates `FlutterRendererConfig`. * It implements `FlutterTextureRegistry` and `FlutterTextureRegistrarDelegate`, linking `FlutterTexture` and `FlutterTextureRegistrar`. We might want to refactor this class, but that's for the future. ## Pre-launch Checklist - [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [ ] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [ ] I read and followed the [Flutter Style Guide] and the [C++, Objective-C, Java style guides]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I added new tests to check the change I am making or feature I am adding, or Hixie said the PR is test-exempt. See [testing the engine] for instructions on writing and running engine tests. - [ ] I updated/added relevant documentation (doc comments with `///`). - [ ] I signed the [CLA]. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [Contributor Guide]: https://github.com/flutter/flutter/wiki/Tree-hygiene#overview [Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene [Flutter Style Guide]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style [testing the engine]: https://github.com/flutter/flutter/wiki/Testing-the-engine [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/wiki/Chat --- .../ci/licenses_golden/licenses_flutter | 2 - .../shell/platform/darwin/macos/BUILD.gn | 1 - .../macos/framework/Source/FlutterRenderer.h | 10 -- .../macos/framework/Source/FlutterRenderer.mm | 58 ++-------- .../framework/Source/FlutterRendererTest.mm | 104 ------------------ .../shell/platform/embedder/embedder.h | 4 + 6 files changed, 15 insertions(+), 164 deletions(-) delete mode 100644 engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterRendererTest.mm diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index a90f22da06..4db3297ba4 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -2705,7 +2705,6 @@ ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPla ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewControllerTest.mm + ../../../flutter/LICENSE ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderer.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderer.mm + ../../../flutter/LICENSE -ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterRendererTest.mm + ../../../flutter/LICENSE ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterSurface.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterSurface.mm + ../../../flutter/LICENSE ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.h + ../../../flutter/LICENSE @@ -5344,7 +5343,6 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatf FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewControllerTest.mm FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderer.h FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderer.mm -FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterRendererTest.mm FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterSurface.h FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterSurface.mm FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.h diff --git a/engine/src/flutter/shell/platform/darwin/macos/BUILD.gn b/engine/src/flutter/shell/platform/darwin/macos/BUILD.gn index da8152cd86..edebb4ebfd 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/BUILD.gn +++ b/engine/src/flutter/shell/platform/darwin/macos/BUILD.gn @@ -179,7 +179,6 @@ executable("flutter_desktop_darwin_unittests") { "framework/Source/FlutterMutatorViewTest.mm", "framework/Source/FlutterPlatformNodeDelegateMacTest.mm", "framework/Source/FlutterPlatformViewControllerTest.mm", - "framework/Source/FlutterRendererTest.mm", "framework/Source/FlutterSurfaceManagerTest.mm", "framework/Source/FlutterTextInputPluginTest.mm", "framework/Source/FlutterTextInputSemanticsObjectTest.mm", diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderer.h b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderer.h index 1665008d20..46f0048e35 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderer.h +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderer.h @@ -35,16 +35,6 @@ */ - (FlutterRendererConfig)createRendererConfig; -/** - * Called by the engine when the given view's buffers should be swapped. - */ -- (BOOL)present:(FlutterViewId)viewId texture:(nonnull const FlutterMetalTexture*)texture; - -/** - * Creates a Metal texture for the given view with the given size. - */ -- (FlutterMetalTexture)createTextureForView:(FlutterViewId)viewId size:(CGSize)size; - /** * Populates the texture registry with the provided metalTexture. */ diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderer.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderer.mm index cf05690f52..63c4df7d5e 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderer.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderer.mm @@ -12,23 +12,15 @@ #pragma mark - Static callbacks that require the engine. -static FlutterMetalTexture OnGetNextDrawableForDefaultView(FlutterEngine* engine, - const FlutterFrameInfo* frameInfo) { - // TODO(dkwingsmt): This callback only supports single-view, therefore it only - // operates on the default view. To support multi-view, we need a new callback - // that also receives a view ID, or pass the ID via FlutterFrameInfo. - FlutterViewId viewId = kFlutterDefaultViewId; - CGSize size = CGSizeMake(frameInfo->size.width, frameInfo->size.height); - return [engine.renderer createTextureForView:viewId size:size]; +static FlutterMetalTexture OnGetNextDrawable(FlutterEngine* engine, + const FlutterFrameInfo* frameInfo) { + NSCAssert(NO, @"The renderer config should not be used to get the next drawable."); + return FlutterMetalTexture{}; } -static bool OnPresentDrawableOfDefaultView(FlutterEngine* engine, - const FlutterMetalTexture* texture) { - // TODO(dkwingsmt): This callback only supports single-view, therefore it only - // operates on the default view. To support multi-view, we need a new callback - // that also receives a view ID. - FlutterViewId viewId = kFlutterDefaultViewId; - return [engine.renderer present:viewId texture:texture]; +static bool OnPresentDrawable(FlutterEngine* engine, const FlutterMetalTexture* texture) { + NSCAssert(NO, @"The renderer config should not be used to present drawable."); + return false; } static bool OnAcquireExternalTexture(FlutterEngine* engine, @@ -43,15 +35,12 @@ static bool OnAcquireExternalTexture(FlutterEngine* engine, #pragma mark - FlutterRenderer implementation @implementation FlutterRenderer { - FlutterViewEngineProvider* _viewProvider; - FlutterDarwinContextMetalSkia* _darwinMetalContext; } - (instancetype)initWithFlutterEngine:(nonnull FlutterEngine*)flutterEngine { self = [super initWithDelegate:self engine:flutterEngine]; if (self) { - _viewProvider = [[FlutterViewEngineProvider alloc] initWithEngine:flutterEngine]; _device = MTLCreateSystemDefaultDevice(); if (!_device) { NSLog(@"Could not acquire Metal device."); @@ -77,9 +66,9 @@ static bool OnAcquireExternalTexture(FlutterEngine* engine, .metal.device = (__bridge FlutterMetalDeviceHandle)_device, .metal.present_command_queue = (__bridge FlutterMetalCommandQueueHandle)_commandQueue, .metal.get_next_drawable_callback = - reinterpret_cast(OnGetNextDrawableForDefaultView), + reinterpret_cast(OnGetNextDrawable), .metal.present_drawable_callback = - reinterpret_cast(OnPresentDrawableOfDefaultView), + reinterpret_cast(OnPresentDrawable), .metal.external_texture_frame_callback = reinterpret_cast(OnAcquireExternalTexture), }; @@ -88,39 +77,14 @@ static bool OnAcquireExternalTexture(FlutterEngine* engine, #pragma mark - Embedder callback implementations. -- (FlutterMetalTexture)createTextureForView:(FlutterViewId)viewId size:(CGSize)size { - FlutterView* view = [_viewProvider viewForId:viewId]; - NSAssert(view != nil, @"Can't create texture on a non-existent view 0x%llx.", viewId); - if (view == nil) { - // FlutterMetalTexture has texture `null`, therefore is discarded. - return FlutterMetalTexture{}; - } - return [view.surfaceManager surfaceForSize:size].asFlutterMetalTexture; -} - -- (BOOL)present:(FlutterViewId)viewId texture:(const FlutterMetalTexture*)texture { - FlutterView* view = [_viewProvider viewForId:viewId]; - if (view == nil) { - return NO; - } - FlutterSurface* surface = [FlutterSurface fromFlutterMetalTexture:texture]; - if (surface == nil) { - return NO; - } - FlutterSurfacePresentInfo* info = [[FlutterSurfacePresentInfo alloc] init]; - info.surface = surface; - [view.surfaceManager present:@[ info ] notify:nil]; - return YES; -} - -#pragma mark - FlutterTextureRegistrar methods. - - (BOOL)populateTextureWithIdentifier:(int64_t)textureID metalTexture:(FlutterMetalExternalTexture*)textureOut { FlutterExternalTexture* texture = [self getTextureWithID:textureID]; return [texture populateTexture:textureOut]; } +#pragma mark - FlutterTextureRegistrar methods. + - (FlutterExternalTexture*)onRegisterTexture:(id)texture { return [[FlutterExternalTexture alloc] initWithFlutterTexture:texture darwinMetalContext:_darwinMetalContext]; diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterRendererTest.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterRendererTest.mm deleted file mode 100644 index 46da658efd..0000000000 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterRendererTest.mm +++ /dev/null @@ -1,104 +0,0 @@ -// 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 - -#import "flutter/shell/platform/darwin/macos/framework/Headers/FlutterEngine.h" -#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h" -#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h" -#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderer.h" -#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterView.h" -#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewControllerTestUtils.h" -#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController_Internal.h" -#include "flutter/shell/platform/embedder/embedder.h" -#include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h" -#include "flutter/testing/testing.h" - -@interface RendererTestViewController : FlutterViewController -- (void)loadMockFlutterView:(FlutterView*)mockView; -@end - -@implementation RendererTestViewController { - FlutterView* _mockFlutterView; -} - -- (void)loadMockFlutterView:(FlutterView*)mockView { - _mockFlutterView = mockView; - [self loadView]; -} - -- (nonnull FlutterView*)createFlutterViewWithMTLDevice:(id)device - commandQueue:(id)commandQueue { - return _mockFlutterView; -} -@end - -namespace flutter::testing { - -namespace { -// Returns an engine configured for the test fixture resource configuration. -RendererTestViewController* CreateTestViewController() { - NSString* fixtures = @(testing::GetFixturesPath()); - FlutterDartProject* project = [[FlutterDartProject alloc] - initWithAssetsPath:fixtures - ICUDataPath:[fixtures stringByAppendingString:@"/icudtl.dat"]]; - RendererTestViewController* viewController = - [[RendererTestViewController alloc] initWithProject:project]; - return viewController; -} - -} // namespace - -TEST(FlutterRenderer, PresentDelegatesToFlutterView) { - RendererTestViewController* viewController = CreateTestViewController(); - FlutterEngine* engine = viewController.engine; - - id viewMock = OCMClassMock([FlutterView class]); - [viewController loadMockFlutterView:viewMock]; - FlutterRenderer* renderer = [[FlutterRenderer alloc] initWithFlutterEngine:engine]; - - id surfaceManagerMock = OCMClassMock([FlutterSurfaceManager class]); - OCMStub([viewMock surfaceManager]).andReturn(surfaceManagerMock); - - id surfaceMock = OCMClassMock([FlutterSurface class]); - - FlutterMetalTexture texture = { - .user_data = (__bridge void*)surfaceMock, - }; - - [[surfaceManagerMock expect] present:[OCMArg checkWithBlock:^(id obj) { - NSArray* array = (NSArray*)obj; - return array.count == 1 ? YES : NO; - }] - notify:nil]; - - [renderer present:kFlutterDefaultViewId texture:&texture]; - [surfaceManagerMock verify]; -} - -TEST(FlutterRenderer, TextureReturnedByFlutterView) { - RendererTestViewController* viewController = CreateTestViewController(); - FlutterEngine* engine = viewController.engine; - - id viewMock = OCMClassMock([FlutterView class]); - [viewController loadMockFlutterView:viewMock]; - FlutterRenderer* renderer = [[FlutterRenderer alloc] initWithFlutterEngine:engine]; - - id surfaceManagerMock = OCMClassMock([FlutterSurfaceManager class]); - OCMStub([viewMock surfaceManager]).andReturn(surfaceManagerMock); - - FlutterFrameInfo frameInfo; - frameInfo.struct_size = sizeof(FlutterFrameInfo); - FlutterUIntSize dimensions; - dimensions.width = 100; - dimensions.height = 200; - frameInfo.size = dimensions; - CGSize size = CGSizeMake(dimensions.width, dimensions.height); - - [[surfaceManagerMock expect] surfaceForSize:size]; - [renderer createTextureForView:kFlutterDefaultViewId size:size]; - [surfaceManagerMock verify]; -} - -} // namespace flutter::testing diff --git a/engine/src/flutter/shell/platform/embedder/embedder.h b/engine/src/flutter/shell/platform/embedder/embedder.h index bd92484433..f1197403b7 100644 --- a/engine/src/flutter/shell/platform/embedder/embedder.h +++ b/engine/src/flutter/shell/platform/embedder/embedder.h @@ -676,9 +676,13 @@ typedef struct { FlutterMetalCommandQueueHandle present_command_queue; /// The callback that gets invoked when the engine requests the embedder for a /// texture to render to. + /// + /// Not used if a FlutterCompositor is supplied in FlutterProjectArgs. FlutterMetalTextureCallback get_next_drawable_callback; /// The callback presented to the embedder to present a fully populated metal /// texture to the user. + /// + /// Not used if a FlutterCompositor is supplied in FlutterProjectArgs. FlutterMetalPresentCallback present_drawable_callback; /// When the embedder specifies that a texture has a frame available, the /// engine will call this method (on an internal engine managed thread) so