This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
iOS: Refactor ShellTestPlatformViewMetal #56370
Merged
auto-submit
merged 1 commit into
flutter:main
from
cbracken:arc-shell-test-platform-view-metal
Nov 5, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,64 +8,50 @@ | |
|
|
||
| #include <utility> | ||
|
|
||
| #include "flutter/fml/platform/darwin/scoped_nsobject.h" | ||
| #include "flutter/shell/gpu/gpu_surface_metal_impeller.h" | ||
| #include "flutter/shell/gpu/gpu_surface_metal_skia.h" | ||
| #include "flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetalImpeller.h" | ||
| #include "flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetalSkia.h" | ||
|
|
||
| FLUTTER_ASSERT_ARC | ||
|
|
||
| namespace flutter { | ||
| namespace testing { | ||
|
|
||
| static fml::scoped_nsprotocol<id<MTLTexture>> CreateOffscreenTexture(id<MTLDevice> device) { | ||
| // This is out of the header so that shell_test_platform_view_metal.h can be included in | ||
| // non-Objective-C TUs. | ||
| struct DarwinContextMetal { | ||
| FlutterDarwinContextMetalSkia* skia_context; | ||
| FlutterDarwinContextMetalImpeller* impeller_context; | ||
| id<MTLTexture> offscreen_texture; | ||
| }; | ||
|
|
||
| static std::unique_ptr<DarwinContextMetal> CreateDarwinContext( | ||
| bool impeller, | ||
| const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch) { | ||
| FlutterDarwinContextMetalSkia* skia_context = nil; | ||
| FlutterDarwinContextMetalImpeller* impeller_context = nil; | ||
| id<MTLDevice> device = nil; | ||
| if (impeller) { | ||
| impeller_context = [[FlutterDarwinContextMetalImpeller alloc] init:is_gpu_disabled_sync_switch]; | ||
| FML_CHECK(impeller_context.context); | ||
| device = impeller_context.context->GetMTLDevice(); | ||
| } else { | ||
| skia_context = [[FlutterDarwinContextMetalSkia alloc] initWithDefaultMTLDevice]; | ||
| FML_CHECK(skia_context.mainContext); | ||
| device = skia_context.device; | ||
| } | ||
| auto descriptor = | ||
| [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm | ||
| width:800 | ||
| height:600 | ||
| mipmapped:NO]; | ||
| descriptor.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead; | ||
| return fml::scoped_nsprotocol<id<MTLTexture>>{[device newTextureWithDescriptor:descriptor]}; | ||
| id<MTLTexture> offscreen_texture = [device newTextureWithDescriptor:descriptor]; | ||
| return std::make_unique<DarwinContextMetal>( | ||
| DarwinContextMetal{skia_context, impeller_context, offscreen_texture}); | ||
| } | ||
|
|
||
| // This is out of the header so that shell_test_platform_view_metal.h can be included in | ||
| // non-Objective-C TUs. | ||
| class DarwinContextMetal { | ||
| public: | ||
| explicit DarwinContextMetal( | ||
| bool impeller, | ||
| const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch) | ||
| : context_(impeller ? nil : [[FlutterDarwinContextMetalSkia alloc] initWithDefaultMTLDevice]), | ||
| impeller_context_( | ||
| impeller ? [[FlutterDarwinContextMetalImpeller alloc] init:is_gpu_disabled_sync_switch] | ||
| : nil), | ||
| offscreen_texture_(CreateOffscreenTexture( | ||
| impeller ? [impeller_context_ context]->GetMTLDevice() : [context_ device])) {} | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is now done in the static None of the rest of the getter boilerplate is necessary. |
||
|
|
||
| ~DarwinContextMetal() = default; | ||
|
|
||
| fml::scoped_nsobject<FlutterDarwinContextMetalImpeller> impeller_context() const { | ||
| return impeller_context_; | ||
| } | ||
|
|
||
| fml::scoped_nsobject<FlutterDarwinContextMetalSkia> context() const { return context_; } | ||
|
|
||
| fml::scoped_nsprotocol<id<MTLTexture>> offscreen_texture() const { return offscreen_texture_; } | ||
|
|
||
| GPUMTLTextureInfo offscreen_texture_info() const { | ||
| GPUMTLTextureInfo info = {}; | ||
| info.texture_id = 0; | ||
| info.texture = reinterpret_cast<GPUMTLTextureHandle>(offscreen_texture_.get()); | ||
| return info; | ||
| } | ||
|
|
||
| private: | ||
| const fml::scoped_nsobject<FlutterDarwinContextMetalSkia> context_; | ||
| const fml::scoped_nsobject<FlutterDarwinContextMetalImpeller> impeller_context_; | ||
| const fml::scoped_nsprotocol<id<MTLTexture>> offscreen_texture_; | ||
|
|
||
| FML_DISALLOW_COPY_AND_ASSIGN(DarwinContextMetal); | ||
| }; | ||
|
|
||
| ShellTestPlatformViewMetal::ShellTestPlatformViewMetal( | ||
| PlatformView::Delegate& delegate, | ||
| const TaskRunners& task_runners, | ||
|
|
@@ -75,17 +61,11 @@ GPUMTLTextureInfo offscreen_texture_info() const { | |
| const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch) | ||
| : ShellTestPlatformView(delegate, task_runners), | ||
| GPUSurfaceMetalDelegate(MTLRenderTargetType::kMTLTexture), | ||
| metal_context_(std::make_unique<DarwinContextMetal>(GetSettings().enable_impeller, | ||
| is_gpu_disabled_sync_switch)), | ||
| metal_context_( | ||
| CreateDarwinContext(GetSettings().enable_impeller, is_gpu_disabled_sync_switch)), | ||
| create_vsync_waiter_(std::move(create_vsync_waiter)), | ||
| vsync_clock_(std::move(vsync_clock)), | ||
| shell_test_external_view_embedder_(std::move(shell_test_external_view_embedder)) { | ||
| if (GetSettings().enable_impeller) { | ||
| FML_CHECK([metal_context_->impeller_context() context] != nil); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } else { | ||
| FML_CHECK([metal_context_->context() mainContext] != nil); | ||
| } | ||
| } | ||
| shell_test_external_view_embedder_(std::move(shell_test_external_view_embedder)) {} | ||
|
|
||
| ShellTestPlatformViewMetal::~ShellTestPlatformViewMetal() = default; | ||
|
|
||
|
|
@@ -113,16 +93,16 @@ GPUMTLTextureInfo offscreen_texture_info() const { | |
| // |PlatformView| | ||
| std::unique_ptr<Surface> ShellTestPlatformViewMetal::CreateRenderingSurface() { | ||
| if (GetSettings().enable_impeller) { | ||
| auto context = [metal_context_->impeller_context() context]; | ||
| auto context = metal_context_->impeller_context.context; | ||
| return std::make_unique<GPUSurfaceMetalImpeller>( | ||
| this, std::make_shared<impeller::AiksContext>(context, nullptr)); | ||
| } | ||
| return std::make_unique<GPUSurfaceMetalSkia>(this, [metal_context_->context() mainContext]); | ||
| return std::make_unique<GPUSurfaceMetalSkia>(this, metal_context_->skia_context.mainContext); | ||
| } | ||
|
|
||
| // |PlatformView| | ||
| std::shared_ptr<impeller::Context> ShellTestPlatformViewMetal::GetImpellerContext() const { | ||
| return [metal_context_->impeller_context() context]; | ||
| return metal_context_->impeller_context.context; | ||
| } | ||
|
|
||
| // |GPUSurfaceMetalDelegate| | ||
|
|
@@ -141,7 +121,10 @@ GPUMTLTextureInfo offscreen_texture_info() const { | |
|
|
||
| // |GPUSurfaceMetalDelegate| | ||
| GPUMTLTextureInfo ShellTestPlatformViewMetal::GetMTLTexture(const SkISize& frame_info) const { | ||
| return metal_context_->offscreen_texture_info(); | ||
| return { | ||
| .texture_id = 0, | ||
| .texture = (__bridge GPUMTLTextureHandle)metal_context_->offscreen_texture, | ||
| }; | ||
| } | ||
|
|
||
| // |GPUSurfaceMetalDelegate| | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this have been a union or std::variant? Yes. But we're going to delete the skia backend eventually anyway, so may as well avoid the extra complexity and make life easy on the person who deletes it.