From 34f3581a3cb212a380d3ac638b1f47d8906875c7 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Tue, 15 Mar 2022 14:12:53 -0700 Subject: [PATCH] Switch the renderer to impeller based on the presence of a command line flag. (flutter/engine#31959) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Specifying the `--enable-impeller` flag will switch the renderer to using Impeller instead of Skia. On platforms where Impeller is not supported, this flag is ignored. The notion of the `flutter::SurfaceFrame` has been augmented. Now, in the absence of a Skia surface to render to, the surface frame will render into a display list instead. Impeller variants of the context and surface variants have been added to `shell/gpu` and `shell/platform`. The variants prepare surface frames that don’t/can’t specify a Skia surface thus forcing the surface frame to render to a display list instead. Then, in the submit callback, they forward the display list ops to the Impeller display list dispatcher. This scheme has been chosen as it requires the fewest updates to engine internals which all depend on Skia data structures. Instead of updating all call-sites to be Skia neutral, the display list interface itself is being made graphics package agnostic. --- DEPS | 4 +- .../ci/licenses_golden/licenses_flutter | 20 ++- engine/src/flutter/common/settings.h | 4 + .../display_list_canvas_recorder.cc | 63 ++++++++- .../display_list_canvas_recorder.h | 24 +--- engine/src/flutter/flow/surface_frame.cc | 29 ++-- engine/src/flutter/flow/surface_frame.h | 18 +-- .../src/flutter/shell/common/platform_view.cc | 4 + .../src/flutter/shell/common/platform_view.h | 16 +++ engine/src/flutter/shell/common/shell.cc | 5 + engine/src/flutter/shell/common/shell.h | 3 + .../common/shell_test_platform_view_metal.mm | 4 +- .../flutter/shell/common/shell_unittests.cc | 5 + engine/src/flutter/shell/common/switches.cc | 3 + engine/src/flutter/shell/common/switches.h | 5 +- engine/src/flutter/shell/gpu/BUILD.gn | 19 +-- .../shell/gpu/gpu_surface_metal_impeller.h | 51 +++++++ .../shell/gpu/gpu_surface_metal_impeller.mm | 124 ++++++++++++++++++ ...rface_metal.h => gpu_surface_metal_skia.h} | 14 +- ...ace_metal.mm => gpu_surface_metal_skia.mm} | 28 ++-- .../shell/platform/android/surface/BUILD.gn | 2 +- .../shell/platform/darwin/ios/BUILD.gn | 12 +- .../Source/FlutterEnginePlatformViewTest.mm | 4 + .../Source/FlutterPlatformViewsTest.mm | 4 + .../Source/accessibility_bridge_test.mm | 4 + .../shell/platform/darwin/ios/ios_context.h | 13 +- .../shell/platform/darwin/ios/ios_context.mm | 18 ++- .../darwin/ios/ios_context_metal_impeller.h | 46 +++++++ .../darwin/ios/ios_context_metal_impeller.mm | 47 +++++++ ...ntext_metal.h => ios_context_metal_skia.h} | 17 ++- ...ext_metal.mm => ios_context_metal_skia.mm} | 22 ++-- .../shell/platform/darwin/ios/ios_surface.mm | 23 +++- .../darwin/ios/ios_surface_metal_impeller.h | 59 +++++++++ .../darwin/ios/ios_surface_metal_impeller.mm | 71 ++++++++++ ...rface_metal.h => ios_surface_metal_skia.h} | 17 +-- ...ace_metal.mm => ios_surface_metal_skia.mm} | 36 ++--- .../platform/darwin/ios/platform_view_ios.mm | 5 +- .../darwin/ios/rendering_api_selection.h | 5 + .../embedder/embedder_surface_metal.h | 2 +- .../embedder/embedder_surface_metal.mm | 2 +- .../fuchsia/flutter/platform_view_unittest.cc | 5 + .../tests/flatland_platform_view_unittest.cc | 5 + 42 files changed, 717 insertions(+), 145 deletions(-) create mode 100644 engine/src/flutter/shell/gpu/gpu_surface_metal_impeller.h create mode 100644 engine/src/flutter/shell/gpu/gpu_surface_metal_impeller.mm rename engine/src/flutter/shell/gpu/{gpu_surface_metal.h => gpu_surface_metal_skia.h} (84%) rename engine/src/flutter/shell/gpu/{gpu_surface_metal.mm => gpu_surface_metal_skia.mm} (88%) create mode 100644 engine/src/flutter/shell/platform/darwin/ios/ios_context_metal_impeller.h create mode 100644 engine/src/flutter/shell/platform/darwin/ios/ios_context_metal_impeller.mm rename engine/src/flutter/shell/platform/darwin/ios/{ios_context_metal.h => ios_context_metal_skia.h} (75%) rename engine/src/flutter/shell/platform/darwin/ios/{ios_context_metal.mm => ios_context_metal_skia.mm} (75%) create mode 100644 engine/src/flutter/shell/platform/darwin/ios/ios_surface_metal_impeller.h create mode 100644 engine/src/flutter/shell/platform/darwin/ios/ios_surface_metal_impeller.mm rename engine/src/flutter/shell/platform/darwin/ios/{ios_surface_metal.h => ios_surface_metal_skia.h} (69%) rename engine/src/flutter/shell/platform/darwin/ios/{ios_surface_metal.mm => ios_surface_metal_skia.mm} (64%) diff --git a/DEPS b/DEPS index baef7516d0..3874333310 100644 --- a/DEPS +++ b/DEPS @@ -107,10 +107,10 @@ allowed_hosts = [ ] deps = { - 'src': 'https://github.com/flutter/buildroot.git' + '@' + 'e4cee118fc80c7cd3b8f8959aa02747a67c38a63', + 'src': 'https://github.com/flutter/buildroot.git' + '@' + '8cbf38af7d48cc298ae86e614533b4b2d0dc6758', 'src/flutter/impeller': - Var('github_git') + '/flutter/impeller' + '@' + '21f922f9d8409d61b32b135298127f4e30d5bbb1', + Var('github_git') + '/flutter/impeller' + '@' + 'c54ddfab21691cf5693c15a4f84d9156fb7d8cfb', # Fuchsia compatibility # diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index a07b05d5bd..6993606b51 100755 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -434,6 +434,8 @@ FILE: ../../../flutter/impeller/entity/contents/content_context.cc FILE: ../../../flutter/impeller/entity/contents/content_context.h FILE: ../../../flutter/impeller/entity/contents/contents.cc FILE: ../../../flutter/impeller/entity/contents/contents.h +FILE: ../../../flutter/impeller/entity/contents/filter_contents.cc +FILE: ../../../flutter/impeller/entity/contents/filter_contents.h FILE: ../../../flutter/impeller/entity/contents/linear_gradient_contents.cc FILE: ../../../flutter/impeller/entity/contents/linear_gradient_contents.h FILE: ../../../flutter/impeller/entity/contents/solid_color_contents.cc @@ -1098,10 +1100,12 @@ FILE: ../../../flutter/shell/gpu/gpu_surface_gl.cc FILE: ../../../flutter/shell/gpu/gpu_surface_gl.h FILE: ../../../flutter/shell/gpu/gpu_surface_gl_delegate.cc FILE: ../../../flutter/shell/gpu/gpu_surface_gl_delegate.h -FILE: ../../../flutter/shell/gpu/gpu_surface_metal.h -FILE: ../../../flutter/shell/gpu/gpu_surface_metal.mm FILE: ../../../flutter/shell/gpu/gpu_surface_metal_delegate.cc FILE: ../../../flutter/shell/gpu/gpu_surface_metal_delegate.h +FILE: ../../../flutter/shell/gpu/gpu_surface_metal_impeller.h +FILE: ../../../flutter/shell/gpu/gpu_surface_metal_impeller.mm +FILE: ../../../flutter/shell/gpu/gpu_surface_metal_skia.h +FILE: ../../../flutter/shell/gpu/gpu_surface_metal_skia.mm FILE: ../../../flutter/shell/gpu/gpu_surface_software.cc FILE: ../../../flutter/shell/gpu/gpu_surface_software.h FILE: ../../../flutter/shell/gpu/gpu_surface_software_delegate.cc @@ -1506,8 +1510,10 @@ FILE: ../../../flutter/shell/platform/darwin/ios/ios_context.h FILE: ../../../flutter/shell/platform/darwin/ios/ios_context.mm FILE: ../../../flutter/shell/platform/darwin/ios/ios_context_gl.h FILE: ../../../flutter/shell/platform/darwin/ios/ios_context_gl.mm -FILE: ../../../flutter/shell/platform/darwin/ios/ios_context_metal.h -FILE: ../../../flutter/shell/platform/darwin/ios/ios_context_metal.mm +FILE: ../../../flutter/shell/platform/darwin/ios/ios_context_metal_impeller.h +FILE: ../../../flutter/shell/platform/darwin/ios/ios_context_metal_impeller.mm +FILE: ../../../flutter/shell/platform/darwin/ios/ios_context_metal_skia.h +FILE: ../../../flutter/shell/platform/darwin/ios/ios_context_metal_skia.mm FILE: ../../../flutter/shell/platform/darwin/ios/ios_context_software.h FILE: ../../../flutter/shell/platform/darwin/ios/ios_context_software.mm FILE: ../../../flutter/shell/platform/darwin/ios/ios_external_texture_gl.h @@ -1522,8 +1528,10 @@ FILE: ../../../flutter/shell/platform/darwin/ios/ios_surface.h FILE: ../../../flutter/shell/platform/darwin/ios/ios_surface.mm FILE: ../../../flutter/shell/platform/darwin/ios/ios_surface_gl.h FILE: ../../../flutter/shell/platform/darwin/ios/ios_surface_gl.mm -FILE: ../../../flutter/shell/platform/darwin/ios/ios_surface_metal.h -FILE: ../../../flutter/shell/platform/darwin/ios/ios_surface_metal.mm +FILE: ../../../flutter/shell/platform/darwin/ios/ios_surface_metal_impeller.h +FILE: ../../../flutter/shell/platform/darwin/ios/ios_surface_metal_impeller.mm +FILE: ../../../flutter/shell/platform/darwin/ios/ios_surface_metal_skia.h +FILE: ../../../flutter/shell/platform/darwin/ios/ios_surface_metal_skia.mm FILE: ../../../flutter/shell/platform/darwin/ios/ios_surface_software.h FILE: ../../../flutter/shell/platform/darwin/ios/ios_surface_software.mm FILE: ../../../flutter/shell/platform/darwin/ios/ios_switchable_gl_context.h diff --git a/engine/src/flutter/common/settings.h b/engine/src/flutter/common/settings.h index 31b16fa931..1f3b2c12d1 100644 --- a/engine/src/flutter/common/settings.h +++ b/engine/src/flutter/common/settings.h @@ -192,6 +192,10 @@ struct Settings { // Selects the SkParagraph implementation of the text layout engine. bool enable_skparagraph = false; + // Enable the Impeller renderer on supported platforms. Ignored if Impeller is + // not supported on the platform. + bool enable_impeller = false; + // Selects the DisplayList for storage of rendering operations. bool enable_display_list = true; diff --git a/engine/src/flutter/display_list/display_list_canvas_recorder.cc b/engine/src/flutter/display_list/display_list_canvas_recorder.cc index 82185a8d6f..f9a1454f92 100644 --- a/engine/src/flutter/display_list/display_list_canvas_recorder.cc +++ b/engine/src/flutter/display_list/display_list_canvas_recorder.cc @@ -211,13 +211,6 @@ void DisplayListCanvasRecorder::onDrawTextBlob(const SkTextBlob* blob, builder_->setAttributesFromPaint(paint, kDrawTextBlobFlags); builder_->drawTextBlob(sk_ref_sp(blob), x, y); } -void DisplayListCanvasRecorder::onDrawShadowRec(const SkPath& path, - const SkDrawShadowRec& rec) { - // Skia does not expose the SkDrawShadowRec structure in a public - // header file so we cannot record this operation. - // See: https://bugs.chromium.org/p/skia/issues/detail?id=12125 - FML_DCHECK(false); -} void DisplayListCanvasRecorder::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix, @@ -228,4 +221,60 @@ void DisplayListCanvasRecorder::onDrawPicture(const SkPicture* picture, builder_->drawPicture(sk_ref_sp(picture), matrix, paint != nullptr); } +void DisplayListCanvasRecorder::onDrawShadowRec(const SkPath& path, + const SkDrawShadowRec& rec) { + // Skia does not expose the SkDrawShadowRec structure in a public + // header file so we cannot record this operation. + // See: https://bugs.chromium.org/p/skia/issues/detail?id=12125 + FML_DLOG(ERROR) << "Unimplemented DisplayListCanvasRecorder::" + << __FUNCTION__; +} + +void DisplayListCanvasRecorder::didSetM44(const SkM44&) { + FML_DLOG(ERROR) << "Unimplemented DisplayListCanvasRecorder::" + << __FUNCTION__; +} + +void DisplayListCanvasRecorder::onDrawBehind(const SkPaint&) { + FML_DLOG(ERROR) << "Unimplemented DisplayListCanvasRecorder::" + << __FUNCTION__; +} + +void DisplayListCanvasRecorder::onDrawRegion(const SkRegion& region, + const SkPaint& paint) { + FML_DLOG(ERROR) << "Unimplemented DisplayListCanvasRecorder::" + << __FUNCTION__; +} + +void DisplayListCanvasRecorder::onDrawPatch(const SkPoint cubics[12], + const SkColor colors[4], + const SkPoint texCoords[4], + SkBlendMode mode, + const SkPaint& paint) { + FML_DLOG(ERROR) << "Unimplemented DisplayListCanvasRecorder::" + << __FUNCTION__; +} + +void DisplayListCanvasRecorder::onDrawEdgeAAQuad(const SkRect& rect, + const SkPoint clip[4], + SkCanvas::QuadAAFlags aaFlags, + const SkColor4f& color, + SkBlendMode mode) { + FML_DLOG(ERROR) << "Unimplemented DisplayListCanvasRecorder::" + << __FUNCTION__; +} + +void DisplayListCanvasRecorder::onDrawAnnotation(const SkRect& rect, + const char key[], + SkData* value) { + FML_DLOG(ERROR) << "Unimplemented DisplayListCanvasRecorder::" + << __FUNCTION__; +} + +void DisplayListCanvasRecorder::onDrawDrawable(SkDrawable* drawable, + const SkMatrix* matrix) { + FML_DLOG(ERROR) << "Unimplemented DisplayListCanvasRecorder::" + << __FUNCTION__; +} + } // namespace flutter diff --git a/engine/src/flutter/display_list/display_list_canvas_recorder.h b/engine/src/flutter/display_list/display_list_canvas_recorder.h index 2cd185d1e0..b5d079bdb3 100644 --- a/engine/src/flutter/display_list/display_list_canvas_recorder.h +++ b/engine/src/flutter/display_list/display_list_canvas_recorder.h @@ -33,7 +33,7 @@ class DisplayListCanvasRecorder sk_sp Build(); void didConcat44(const SkM44&) override; - void didSetM44(const SkM44&) override { FML_DCHECK(false); } + void didSetM44(const SkM44&) override; void didTranslate(SkScalar, SkScalar) override; void didScale(SkScalar, SkScalar) override; @@ -52,7 +52,7 @@ class DisplayListCanvasRecorder void didRestore() override; void onDrawPaint(const SkPaint& paint) override; - void onDrawBehind(const SkPaint&) override { FML_DCHECK(false); } + void onDrawBehind(const SkPaint&) override; void onDrawRect(const SkRect& rect, const SkPaint& paint) override; void onDrawRRect(const SkRRect& rrect, const SkPaint& paint) override; void onDrawDRRect(const SkRRect& outer, @@ -65,9 +65,7 @@ class DisplayListCanvasRecorder bool useCenter, const SkPaint& paint) override; void onDrawPath(const SkPath& path, const SkPaint& paint) override; - void onDrawRegion(const SkRegion& region, const SkPaint& paint) override { - FML_DCHECK(false); - } + void onDrawRegion(const SkRegion& region, const SkPaint& paint) override; void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, @@ -78,9 +76,7 @@ class DisplayListCanvasRecorder const SkColor colors[4], const SkPoint texCoords[4], SkBlendMode mode, - const SkPaint& paint) override { - FML_DCHECK(false); - } + const SkPaint& paint) override; void onDrawPoints(SkCanvas::PointMode mode, size_t count, const SkPoint pts[], @@ -119,20 +115,14 @@ class DisplayListCanvasRecorder const SkPoint clip[4], SkCanvas::QuadAAFlags aaFlags, const SkColor4f& color, - SkBlendMode mode) override { - FML_DCHECK(0); - } + SkBlendMode mode) override; void onDrawAnnotation(const SkRect& rect, const char key[], - SkData* value) override { - FML_DCHECK(false); - } + SkData* value) override; void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&) override; - void onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) override { - FML_DCHECK(false); - } + void onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) override; void onDrawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint) override; diff --git a/engine/src/flutter/flow/surface_frame.cc b/engine/src/flutter/flow/surface_frame.cc index c7512b4048..46f9db80b3 100644 --- a/engine/src/flutter/flow/surface_frame.cc +++ b/engine/src/flutter/flow/surface_frame.cc @@ -3,19 +3,15 @@ // found in the LICENSE file. #include "flutter/flow/surface_frame.h" + +#include + #include "flutter/fml/logging.h" +#include "flutter/fml/trace_event.h" +#include "third_party/skia/include/utils/SkNWayCanvas.h" namespace flutter { -SurfaceFrame::SurfaceFrame(sk_sp surface, - FramebufferInfo framebuffer_info, - const SubmitCallback& submit_callback) - : surface_(surface), - framebuffer_info_(std::move(framebuffer_info)), - submit_callback_(submit_callback) { - FML_DCHECK(submit_callback_); -} - SurfaceFrame::SurfaceFrame(sk_sp surface, FramebufferInfo framebuffer_info, const SubmitCallback& submit_callback, @@ -26,6 +22,14 @@ SurfaceFrame::SurfaceFrame(sk_sp surface, submit_callback_(submit_callback), context_result_(std::move(context_result)) { FML_DCHECK(submit_callback_); + if (surface_) { + canvas_ = surface_->getCanvas(); + } else { + dl_recorder_ = sk_make_sp( + SkRect::MakeWH(std::numeric_limits::max(), + std::numeric_limits::max())); + canvas_ = dl_recorder_.get(); + } } SurfaceFrame::~SurfaceFrame() { @@ -50,7 +54,7 @@ bool SurfaceFrame::IsSubmitted() const { } SkCanvas* SurfaceFrame::SkiaCanvas() { - return surface_ != nullptr ? surface_->getCanvas() : nullptr; + return canvas_; } sk_sp SurfaceFrame::SkiaSurface() const { @@ -69,4 +73,9 @@ bool SurfaceFrame::PerformSubmit() { return false; } +sk_sp SurfaceFrame::BuildDisplayList() { + TRACE_EVENT0("impeller", "SurfaceFrame::BuildDisplayList"); + return dl_recorder_ ? dl_recorder_->Build() : nullptr; +} + } // namespace flutter diff --git a/engine/src/flutter/flow/surface_frame.h b/engine/src/flutter/flow/surface_frame.h index 2ecee0b176..278bbe2654 100644 --- a/engine/src/flutter/flow/surface_frame.h +++ b/engine/src/flutter/flow/surface_frame.h @@ -9,6 +9,7 @@ #include #include "flutter/common/graphics/gl_context_switch.h" +#include "flutter/display_list/display_list_canvas_recorder.h" #include "flutter/fml/macros.h" #include "third_party/skia/include/core/SkCanvas.h" #include "third_party/skia/include/core/SkSurface.h" @@ -20,7 +21,7 @@ namespace flutter { class SurfaceFrame { public: using SubmitCallback = - std::function; + std::function; // Information about the underlying framebuffer struct FramebufferInfo { @@ -46,14 +47,10 @@ class SurfaceFrame { std::optional existing_damage; }; - SurfaceFrame(sk_sp surface, - FramebufferInfo framebuffer_info, - const SubmitCallback& submit_callback); - SurfaceFrame(sk_sp surface, FramebufferInfo framebuffer_info, const SubmitCallback& submit_callback, - std::unique_ptr context_result); + std::unique_ptr context_result = nullptr); ~SurfaceFrame(); @@ -76,10 +73,10 @@ class SurfaceFrame { bool IsSubmitted() const; - SkCanvas* SkiaCanvas(); - sk_sp SkiaSurface() const; + SkCanvas* SkiaCanvas(); + const FramebufferInfo& framebuffer_info() const { return framebuffer_info_; } void set_submit_info(const SubmitInfo& submit_info) { @@ -87,9 +84,14 @@ class SurfaceFrame { } const SubmitInfo& submit_info() const { return submit_info_; } + sk_sp BuildDisplayList(); + private: bool submitted_ = false; + + sk_sp dl_recorder_; sk_sp surface_; + SkCanvas* canvas_ = nullptr; FramebufferInfo framebuffer_info_; SubmitInfo submit_info_; SubmitCallback submit_callback_; diff --git a/engine/src/flutter/shell/common/platform_view.cc b/engine/src/flutter/shell/common/platform_view.cc index 71dbd26829..cd5d36bc8b 100644 --- a/engine/src/flutter/shell/common/platform_view.cc +++ b/engine/src/flutter/shell/common/platform_view.cc @@ -187,4 +187,8 @@ PlatformView::GetPlatformMessageHandler() const { return nullptr; } +const Settings& PlatformView::GetSettings() const { + return delegate_.OnPlatformViewGetSettings(); +} + } // namespace flutter diff --git a/engine/src/flutter/shell/common/platform_view.h b/engine/src/flutter/shell/common/platform_view.h index 3e8ab2201b..d6a8b38e70 100644 --- a/engine/src/flutter/shell/common/platform_view.h +++ b/engine/src/flutter/shell/common/platform_view.h @@ -308,6 +308,15 @@ class PlatformView { virtual void UpdateAssetResolverByType( std::unique_ptr updated_asset_resolver, AssetResolver::AssetResolverType type) = 0; + + //-------------------------------------------------------------------------- + /// @brief Called by the platform view on the platform thread to get + /// the settings object associated with the platform view + /// instance. + /// + /// @return The settings. + /// + virtual const Settings& OnPlatformViewGetSettings() const = 0; }; //---------------------------------------------------------------------------- @@ -808,6 +817,13 @@ class PlatformView { virtual std::shared_ptr GetPlatformMessageHandler() const; + //---------------------------------------------------------------------------- + /// @brief Get the settings for this platform view instance. + /// + /// @return The settings. + /// + const Settings& GetSettings() const; + protected: // This is the only method called on the raster task runner. virtual std::unique_ptr CreateRenderingSurface(); diff --git a/engine/src/flutter/shell/common/shell.cc b/engine/src/flutter/shell/common/shell.cc index 7ae9ff7a98..90662852d9 100644 --- a/engine/src/flutter/shell/common/shell.cc +++ b/engine/src/flutter/shell/common/shell.cc @@ -1079,6 +1079,11 @@ void Shell::OnPlatformViewSetNextFrameCallback(const fml::closure& closure) { }); } +// |PlatformView::Delegate| +const Settings& Shell::OnPlatformViewGetSettings() const { + return settings_; +} + // |Animator::Delegate| void Shell::OnAnimatorBeginFrame(fml::TimePoint frame_target_time, uint64_t frame_number) { diff --git a/engine/src/flutter/shell/common/shell.h b/engine/src/flutter/shell/common/shell.h index 099743e6be..4aefcb2c0f 100644 --- a/engine/src/flutter/shell/common/shell.h +++ b/engine/src/flutter/shell/common/shell.h @@ -557,6 +557,9 @@ class Shell final : public PlatformView::Delegate, // |PlatformView::Delegate| void OnPlatformViewSetNextFrameCallback(const fml::closure& closure) override; + // |PlatformView::Delegate| + const Settings& OnPlatformViewGetSettings() const override; + // |PlatformView::Delegate| void LoadDartDeferredLibrary( intptr_t loading_unit_id, diff --git a/engine/src/flutter/shell/common/shell_test_platform_view_metal.mm b/engine/src/flutter/shell/common/shell_test_platform_view_metal.mm index 585d1e0e04..18869d69b8 100644 --- a/engine/src/flutter/shell/common/shell_test_platform_view_metal.mm +++ b/engine/src/flutter/shell/common/shell_test_platform_view_metal.mm @@ -7,7 +7,7 @@ #import #include "flutter/fml/platform/darwin/scoped_nsobject.h" -#include "flutter/shell/gpu/gpu_surface_metal.h" +#include "flutter/shell/gpu/gpu_surface_metal_skia.h" #include "flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetal.h" namespace flutter { @@ -91,7 +91,7 @@ PointerDataDispatcherMaker ShellTestPlatformViewMetal::GetDispatcherMaker() { // |PlatformView| std::unique_ptr ShellTestPlatformViewMetal::CreateRenderingSurface() { - return std::make_unique(this, [metal_context_->context() mainContext]); + return std::make_unique(this, [metal_context_->context() mainContext]); } // |GPUSurfaceMetalDelegate| diff --git a/engine/src/flutter/shell/common/shell_unittests.cc b/engine/src/flutter/shell/common/shell_unittests.cc index 7221c47592..2265661eda 100644 --- a/engine/src/flutter/shell/common/shell_unittests.cc +++ b/engine/src/flutter/shell/common/shell_unittests.cc @@ -86,6 +86,11 @@ class MockPlatformViewDelegate : public PlatformView::Delegate { MOCK_METHOD1(OnPlatformViewMarkTextureFrameAvailable, void(int64_t texture_id)); + MOCK_METHOD(const Settings&, + OnPlatformViewGetSettings, + (), + (const, override)); + MOCK_METHOD3(LoadDartDeferredLibrary, void(intptr_t loading_unit_id, std::unique_ptr snapshot_data, diff --git a/engine/src/flutter/shell/common/switches.cc b/engine/src/flutter/shell/common/switches.cc index 8efcf55a47..853c3e88ea 100644 --- a/engine/src/flutter/shell/common/switches.cc +++ b/engine/src/flutter/shell/common/switches.cc @@ -421,6 +421,9 @@ Settings SettingsFromCommandLine(const fml::CommandLine& command_line) { FlagForSwitch(Switch::EnableSkParagraph), ""); settings.enable_skparagraph = enable_skparagraph != "false"; + settings.enable_impeller = + command_line.HasOption(FlagForSwitch(Switch::EnableImpeller)); + settings.prefetched_default_font_manager = command_line.HasOption( FlagForSwitch(Switch::PrefetchedDefaultFontManager)); diff --git a/engine/src/flutter/shell/common/switches.h b/engine/src/flutter/shell/common/switches.h index eab0ac3ead..42043d39d0 100644 --- a/engine/src/flutter/shell/common/switches.h +++ b/engine/src/flutter/shell/common/switches.h @@ -223,7 +223,10 @@ DEF_SWITCH(OldGenHeapSize, DEF_SWITCH(EnableSkParagraph, "enable-skparagraph", "Selects the SkParagraph implementation of the text layout engine.") - +DEF_SWITCH(EnableImpeller, + "enable-impeller", + "Enable the Impeller renderer on supported platforms. Ignored if " + "Impeller is not supported on the platform.") DEF_SWITCH(LeakVM, "leak-vm", "When the last shell shuts down, the shared VM is leaked by default " diff --git a/engine/src/flutter/shell/gpu/BUILD.gn b/engine/src/flutter/shell/gpu/BUILD.gn index ec1d377d96..019c472e25 100644 --- a/engine/src/flutter/shell/gpu/BUILD.gn +++ b/engine/src/flutter/shell/gpu/BUILD.gn @@ -22,7 +22,7 @@ source_set("gpu_surface_software") { "gpu_surface_software_delegate.h", ] - deps = gpu_common_deps + public_deps = gpu_common_deps } source_set("gpu_surface_gl") { @@ -33,7 +33,7 @@ source_set("gpu_surface_gl") { "gpu_surface_gl_delegate.h", ] - deps = gpu_common_deps + public_deps = gpu_common_deps } source_set("gpu_surface_vulkan") { @@ -43,18 +43,21 @@ source_set("gpu_surface_vulkan") { "gpu_surface_vulkan_delegate.cc", "gpu_surface_vulkan_delegate.h", ] - deps = [ "//flutter/shell/platform/embedder:embedder_headers" ] - deps += gpu_common_deps - public_deps = [ "//flutter/vulkan" ] + public_deps = gpu_common_deps + [ + "//flutter/shell/platform/embedder:embedder_headers", + "//flutter/vulkan", + ] } source_set("gpu_surface_metal") { sources = [ - "gpu_surface_metal.h", - "gpu_surface_metal.mm", "gpu_surface_metal_delegate.cc", "gpu_surface_metal_delegate.h", + "gpu_surface_metal_impeller.h", + "gpu_surface_metal_impeller.mm", + "gpu_surface_metal_skia.h", + "gpu_surface_metal_skia.mm", ] - deps = gpu_common_deps + public_deps = gpu_common_deps + [ "//flutter/impeller" ] } diff --git a/engine/src/flutter/shell/gpu/gpu_surface_metal_impeller.h b/engine/src/flutter/shell/gpu/gpu_surface_metal_impeller.h new file mode 100644 index 0000000000..e8f60be58b --- /dev/null +++ b/engine/src/flutter/shell/gpu/gpu_surface_metal_impeller.h @@ -0,0 +1,51 @@ +// 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_GPU_GPU_SURFACE_METAL_IMPELLER_H_ +#define FLUTTER_SHELL_GPU_GPU_SURFACE_METAL_IMPELLER_H_ + +#include "flutter/flow/surface.h" +#include "flutter/fml/macros.h" +#include "flutter/impeller/aiks/aiks_context.h" +#include "flutter/impeller/renderer/renderer.h" +#include "flutter/shell/gpu/gpu_surface_metal_delegate.h" + +namespace flutter { + +class SK_API_AVAILABLE_CA_METAL_LAYER GPUSurfaceMetalImpeller : public Surface { + public: + GPUSurfaceMetalImpeller(GPUSurfaceMetalDelegate* delegate); + + // |Surface| + ~GPUSurfaceMetalImpeller(); + + // |Surface| + bool IsValid() override; + + private: + const GPUSurfaceMetalDelegate* delegate_; + std::shared_ptr impeller_renderer_; + std::shared_ptr aiks_context_; + + // |Surface| + std::unique_ptr AcquireFrame(const SkISize& size) override; + + // |Surface| + SkMatrix GetRootTransformation() const override; + + // |Surface| + GrDirectContext* GetContext() override; + + // |Surface| + std::unique_ptr MakeRenderContextCurrent() override; + + // |Surface| + bool AllowsDrawingWhenGpuDisabled() const override; + + FML_DISALLOW_COPY_AND_ASSIGN(GPUSurfaceMetalImpeller); +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_GPU_GPU_SURFACE_METAL_IMPELLER_H_ diff --git a/engine/src/flutter/shell/gpu/gpu_surface_metal_impeller.mm b/engine/src/flutter/shell/gpu/gpu_surface_metal_impeller.mm new file mode 100644 index 0000000000..12b3d28301 --- /dev/null +++ b/engine/src/flutter/shell/gpu/gpu_surface_metal_impeller.mm @@ -0,0 +1,124 @@ +// 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/gpu/gpu_surface_metal_impeller.h" + +#import +#import + +#include "flutter/fml/make_copyable.h" +#include "flutter/fml/mapping.h" +#include "flutter/impeller/display_list/display_list_dispatcher.h" +#include "flutter/impeller/entity/entity_shaders.h" +#include "flutter/impeller/renderer/backend/metal/context_mtl.h" +#include "flutter/impeller/renderer/backend/metal/surface_mtl.h" + +static_assert(!__has_feature(objc_arc), "ARC must be disabled."); + +namespace flutter { + +static std::shared_ptr CreateImpellerRenderer() { + std::vector> shader_mappings = { + std::make_shared(impeller_entity_shaders_data, + impeller_entity_shaders_length), + }; + auto context = impeller::ContextMTL::Create(shader_mappings, "Impeller Library"); + if (!context) { + FML_LOG(ERROR) << "Could not create Metal Impeller Context."; + return nullptr; + } + + auto renderer = std::make_shared(std::move(context)); + if (!renderer->IsValid()) { + FML_LOG(ERROR) << "Could not create valid Impeller Renderer."; + return nullptr; + } + + return renderer; +} + +GPUSurfaceMetalImpeller::GPUSurfaceMetalImpeller(GPUSurfaceMetalDelegate* delegate) + : delegate_(delegate), + impeller_renderer_(CreateImpellerRenderer()), + aiks_context_(std::make_shared( + impeller_renderer_ ? impeller_renderer_->GetContext() : nullptr)) {} + +GPUSurfaceMetalImpeller::~GPUSurfaceMetalImpeller() = default; + +// |Surface| +bool GPUSurfaceMetalImpeller::IsValid() { + return !!aiks_context_; +} + +// |Surface| +std::unique_ptr GPUSurfaceMetalImpeller::AcquireFrame(const SkISize& frame_info) { + if (!IsValid()) { + FML_LOG(ERROR) << "Metal surface was invalid."; + return nullptr; + } + + auto layer = delegate_->GetCAMetalLayer(frame_info); + if (!layer) { + FML_LOG(ERROR) << "Invalid CAMetalLayer given by the embedder."; + return nullptr; + } + + auto* mtl_layer = (CAMetalLayer*)layer; + + auto surface = impeller::SurfaceMTL::WrapCurrentMetalLayerDrawable( + impeller_renderer_->GetContext(), mtl_layer); + + SurfaceFrame::SubmitCallback submit_callback = + fml::MakeCopyable([renderer = impeller_renderer_, // + aiks_context = aiks_context_, // + surface = std::move(surface) // + ](SurfaceFrame& surface_frame, SkCanvas* canvas) mutable -> bool { + if (!aiks_context) { + return false; + } + + auto display_list = surface_frame.BuildDisplayList(); + if (!display_list) { + FML_LOG(ERROR) << "Could not build display list for surface frame."; + return false; + } + + impeller::DisplayListDispatcher impeller_dispatcher; + display_list->Dispatch(impeller_dispatcher); + auto picture = impeller_dispatcher.EndRecordingAsPicture(); + + return renderer->Render(std::move(surface), + fml::MakeCopyable([aiks_context, picture = std::move(picture)]( + impeller::RenderPass& pass) -> bool { + return aiks_context->Render(picture, pass); + })); + }); + + return std::make_unique(nullptr, SurfaceFrame::FramebufferInfo{}, submit_callback, + nullptr); +} + +// |Surface| +SkMatrix GPUSurfaceMetalImpeller::GetRootTransformation() const { + // This backend does not currently support root surface transformations. Just + // return identity. + return {}; +} + +// |Surface| +GrDirectContext* GPUSurfaceMetalImpeller::GetContext() { + return nullptr; +} + +// |Surface| +std::unique_ptr GPUSurfaceMetalImpeller::MakeRenderContextCurrent() { + // This backend has no such concept. + return std::make_unique(true); +} + +bool GPUSurfaceMetalImpeller::AllowsDrawingWhenGpuDisabled() const { + return delegate_->AllowsDrawingWhenGpuDisabled(); +} + +} // namespace flutter diff --git a/engine/src/flutter/shell/gpu/gpu_surface_metal.h b/engine/src/flutter/shell/gpu/gpu_surface_metal_skia.h similarity index 84% rename from engine/src/flutter/shell/gpu/gpu_surface_metal.h rename to engine/src/flutter/shell/gpu/gpu_surface_metal_skia.h index cc180ec5f4..378051ba32 100644 --- a/engine/src/flutter/shell/gpu/gpu_surface_metal.h +++ b/engine/src/flutter/shell/gpu/gpu_surface_metal_skia.h @@ -13,14 +13,14 @@ namespace flutter { -class SK_API_AVAILABLE_CA_METAL_LAYER GPUSurfaceMetal : public Surface { +class SK_API_AVAILABLE_CA_METAL_LAYER GPUSurfaceMetalSkia : public Surface { public: - GPUSurfaceMetal(GPUSurfaceMetalDelegate* delegate, - sk_sp context, - bool render_to_surface = true); + GPUSurfaceMetalSkia(GPUSurfaceMetalDelegate* delegate, + sk_sp context, + bool render_to_surface = true); // |Surface| - ~GPUSurfaceMetal(); + ~GPUSurfaceMetalSkia(); // |Surface| bool IsValid() override; @@ -34,7 +34,7 @@ class SK_API_AVAILABLE_CA_METAL_LAYER GPUSurfaceMetal : public Surface { // external view embedder may want to render to the root surface. This is a // hack to make avoid allocating resources for the root surface when an // external view embedder is present. - bool render_to_surface_; + bool render_to_surface_ = true; // Accumulated damage for each framebuffer; Key is address of underlying // MTLTexture for each drawable @@ -63,7 +63,7 @@ class SK_API_AVAILABLE_CA_METAL_LAYER GPUSurfaceMetal : public Surface { void PrecompileKnownSkSLsIfNecessary(); - FML_DISALLOW_COPY_AND_ASSIGN(GPUSurfaceMetal); + FML_DISALLOW_COPY_AND_ASSIGN(GPUSurfaceMetalSkia); }; } // namespace flutter diff --git a/engine/src/flutter/shell/gpu/gpu_surface_metal.mm b/engine/src/flutter/shell/gpu/gpu_surface_metal_skia.mm similarity index 88% rename from engine/src/flutter/shell/gpu/gpu_surface_metal.mm rename to engine/src/flutter/shell/gpu/gpu_surface_metal_skia.mm index 31fcb3617b..99271510ac 100644 --- a/engine/src/flutter/shell/gpu/gpu_surface_metal.mm +++ b/engine/src/flutter/shell/gpu/gpu_surface_metal_skia.mm @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "flutter/shell/gpu/gpu_surface_metal.h" +#include "flutter/shell/gpu/gpu_surface_metal_skia.h" #import #import @@ -37,22 +37,22 @@ sk_sp CreateSurfaceFromMetalTexture(GrDirectContext* context, } } // namespace -GPUSurfaceMetal::GPUSurfaceMetal(GPUSurfaceMetalDelegate* delegate, - sk_sp context, - bool render_to_surface) +GPUSurfaceMetalSkia::GPUSurfaceMetalSkia(GPUSurfaceMetalDelegate* delegate, + sk_sp context, + bool render_to_surface) : delegate_(delegate), render_target_type_(delegate->GetRenderTargetType()), context_(std::move(context)), render_to_surface_(render_to_surface) {} -GPUSurfaceMetal::~GPUSurfaceMetal() = default; +GPUSurfaceMetalSkia::~GPUSurfaceMetalSkia() = default; // |Surface| -bool GPUSurfaceMetal::IsValid() { +bool GPUSurfaceMetalSkia::IsValid() { return context_ != nullptr; } -void GPUSurfaceMetal::PrecompileKnownSkSLsIfNecessary() { +void GPUSurfaceMetalSkia::PrecompileKnownSkSLsIfNecessary() { auto* current_context = GetContext(); if (current_context == precompiled_sksl_context_) { // Known SkSLs have already been prepared in this context. @@ -63,7 +63,7 @@ void GPUSurfaceMetal::PrecompileKnownSkSLsIfNecessary() { } // |Surface| -std::unique_ptr GPUSurfaceMetal::AcquireFrame(const SkISize& frame_size) { +std::unique_ptr GPUSurfaceMetalSkia::AcquireFrame(const SkISize& frame_size) { if (!IsValid()) { FML_LOG(ERROR) << "Metal surface was invalid."; return nullptr; @@ -94,7 +94,7 @@ std::unique_ptr GPUSurfaceMetal::AcquireFrame(const SkISize& frame return nullptr; } -std::unique_ptr GPUSurfaceMetal::AcquireFrameFromCAMetalLayer( +std::unique_ptr GPUSurfaceMetalSkia::AcquireFrameFromCAMetalLayer( const SkISize& frame_info) { auto layer = delegate_->GetCAMetalLayer(frame_info); if (!layer) { @@ -167,7 +167,7 @@ std::unique_ptr GPUSurfaceMetal::AcquireFrameFromCAMetalLayer( return std::make_unique(std::move(surface), framebuffer_info, submit_callback); } -std::unique_ptr GPUSurfaceMetal::AcquireFrameFromMTLTexture( +std::unique_ptr GPUSurfaceMetalSkia::AcquireFrameFromMTLTexture( const SkISize& frame_info) { GPUMTLTextureInfo texture = delegate_->GetMTLTexture(frame_info); id mtl_texture = (id)(texture.texture); @@ -210,19 +210,19 @@ std::unique_ptr GPUSurfaceMetal::AcquireFrameFromMTLTexture( } // |Surface| -SkMatrix GPUSurfaceMetal::GetRootTransformation() const { +SkMatrix GPUSurfaceMetalSkia::GetRootTransformation() const { // This backend does not currently support root surface transformations. Just // return identity. return {}; } // |Surface| -GrDirectContext* GPUSurfaceMetal::GetContext() { +GrDirectContext* GPUSurfaceMetalSkia::GetContext() { return context_.get(); } // |Surface| -std::unique_ptr GPUSurfaceMetal::MakeRenderContextCurrent() { +std::unique_ptr GPUSurfaceMetalSkia::MakeRenderContextCurrent() { // A context may either be necessary to render to the surface or to snapshot an offscreen // surface. Either way, SkSL precompilation must be attempted. PrecompileKnownSkSLsIfNecessary(); @@ -231,7 +231,7 @@ std::unique_ptr GPUSurfaceMetal::MakeRenderContextCurrent() { return std::make_unique(true); } -bool GPUSurfaceMetal::AllowsDrawingWhenGpuDisabled() const { +bool GPUSurfaceMetalSkia::AllowsDrawingWhenGpuDisabled() const { return delegate_->AllowsDrawingWhenGpuDisabled(); } diff --git a/engine/src/flutter/shell/platform/android/surface/BUILD.gn b/engine/src/flutter/shell/platform/android/surface/BUILD.gn index c3e2b90260..eb1f832980 100644 --- a/engine/src/flutter/shell/platform/android/surface/BUILD.gn +++ b/engine/src/flutter/shell/platform/android/surface/BUILD.gn @@ -51,10 +51,10 @@ source_set("surface_mock") { deps = [ ":surface", + "//flutter/flow", "//flutter/shell/gpu:gpu_surface_gl", "//third_party/googletest:gmock", "//third_party/googletest:gtest", - "//third_party/skia", ] if (shell_enable_vulkan) { diff --git a/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn b/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn index 7a09f6b691..a9dfa50c13 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn +++ b/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn @@ -138,12 +138,16 @@ source_set("flutter_framework_source") { if (shell_enable_metal) { sources += [ - "ios_context_metal.h", - "ios_context_metal.mm", + "ios_context_metal_impeller.h", + "ios_context_metal_impeller.mm", + "ios_context_metal_skia.h", + "ios_context_metal_skia.mm", "ios_external_texture_metal.h", "ios_external_texture_metal.mm", - "ios_surface_metal.h", - "ios_surface_metal.mm", + "ios_surface_metal_impeller.h", + "ios_surface_metal_impeller.mm", + "ios_surface_metal_skia.h", + "ios_surface_metal_skia.mm", ] deps += [ "//flutter/shell/platform/darwin/graphics" ] diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEnginePlatformViewTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEnginePlatformViewTest.mm index 9ed0f2ecb5..270aab3fba 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEnginePlatformViewTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEnginePlatformViewTest.mm @@ -24,6 +24,7 @@ class FakeDelegate : public PlatformView::Delegate { void OnPlatformViewScheduleFrame() override {} void OnPlatformViewSetNextFrameCallback(const fml::closure& closure) override {} void OnPlatformViewSetViewportMetrics(const ViewportMetrics& metrics) override {} + const flutter::Settings& OnPlatformViewGetSettings() const override { return settings_; } void OnPlatformViewDispatchPlatformMessage(std::unique_ptr message) override {} void OnPlatformViewDispatchPointerDataPacket(std::unique_ptr packet) override { } @@ -45,6 +46,9 @@ class FakeDelegate : public PlatformView::Delegate { bool transient) override {} void UpdateAssetResolverByType(std::unique_ptr updated_asset_resolver, AssetResolver::AssetResolverType type) override {} + + private: + flutter::Settings settings_; }; } // namespace diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsTest.mm index 690fc35073..fba267419a 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsTest.mm @@ -93,6 +93,7 @@ class FlutterPlatformViewsTestMockPlatformViewDelegate : public PlatformView::De void OnPlatformViewScheduleFrame() override {} void OnPlatformViewSetNextFrameCallback(const fml::closure& closure) override {} void OnPlatformViewSetViewportMetrics(const ViewportMetrics& metrics) override {} + const flutter::Settings& OnPlatformViewGetSettings() const override { return settings_; } void OnPlatformViewDispatchPlatformMessage(std::unique_ptr message) override {} void OnPlatformViewDispatchPointerDataPacket(std::unique_ptr packet) override { } @@ -114,6 +115,9 @@ class FlutterPlatformViewsTestMockPlatformViewDelegate : public PlatformView::De bool transient) override {} void UpdateAssetResolverByType(std::unique_ptr updated_asset_resolver, flutter::AssetResolver::AssetResolverType type) override {} + + private: + flutter::Settings settings_; }; } // namespace diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm index 51ad673334..096af929bb 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm @@ -77,6 +77,7 @@ class MockDelegate : public PlatformView::Delegate { void OnPlatformViewScheduleFrame() override {} void OnPlatformViewSetNextFrameCallback(const fml::closure& closure) override {} void OnPlatformViewSetViewportMetrics(const ViewportMetrics& metrics) override {} + const flutter::Settings& OnPlatformViewGetSettings() const override { return settings_; } void OnPlatformViewDispatchPlatformMessage(std::unique_ptr message) override {} void OnPlatformViewDispatchPointerDataPacket(std::unique_ptr packet) override { } @@ -98,6 +99,9 @@ class MockDelegate : public PlatformView::Delegate { bool transient) override {} void UpdateAssetResolverByType(std::unique_ptr updated_asset_resolver, flutter::AssetResolver::AssetResolverType type) override {} + + private: + flutter::Settings settings_; }; class MockIosDelegate : public AccessibilityBridge::IosDelegate { diff --git a/engine/src/flutter/shell/platform/darwin/ios/ios_context.h b/engine/src/flutter/shell/platform/darwin/ios/ios_context.h index 7e0e5e2c23..611f454101 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/ios_context.h +++ b/engine/src/flutter/shell/platform/darwin/ios/ios_context.h @@ -39,12 +39,12 @@ class IOSContext { /// In case the engine does not support the specified client /// rendering API, this a `nullptr` may be returned. /// - /// @param[in] rendering_api A client rendering API supported by the - /// engine/platform. + /// @param[in] api A client rendering API supported by the + /// engine/platform. /// /// @return A valid context on success. `nullptr` on failure. /// - static std::unique_ptr Create(IOSRenderingAPI rendering_api); + static std::unique_ptr Create(IOSRenderingAPI api, IOSRenderingBackend backend); //---------------------------------------------------------------------------- /// @brief Collects the context object. This must happen on the thread on @@ -52,6 +52,13 @@ class IOSContext { /// virtual ~IOSContext(); + //---------------------------------------------------------------------------- + /// @brief Get the rendering backend used by this context. + /// + /// @return The rendering backend. + /// + virtual IOSRenderingBackend GetBackend() const; + //---------------------------------------------------------------------------- /// @brief Create a resource context for use on the IO task runner. This /// resource context is used by Skia to upload texture to diff --git a/engine/src/flutter/shell/platform/darwin/ios/ios_context.mm b/engine/src/flutter/shell/platform/darwin/ios/ios_context.mm index 3752235cbd..8a0d530238 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/ios_context.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/ios_context.mm @@ -9,7 +9,8 @@ #import "flutter/shell/platform/darwin/ios/ios_context_software.h" #if SHELL_ENABLE_METAL -#import "flutter/shell/platform/darwin/ios/ios_context_metal.h" +#import "flutter/shell/platform/darwin/ios/ios_context_metal_impeller.h" +#import "flutter/shell/platform/darwin/ios/ios_context_metal_skia.h" #endif // SHELL_ENABLE_METAL namespace flutter { @@ -18,15 +19,20 @@ IOSContext::IOSContext() = default; IOSContext::~IOSContext() = default; -std::unique_ptr IOSContext::Create(IOSRenderingAPI rendering_api) { - switch (rendering_api) { +std::unique_ptr IOSContext::Create(IOSRenderingAPI api, IOSRenderingBackend backend) { + switch (api) { case IOSRenderingAPI::kOpenGLES: return std::make_unique(); case IOSRenderingAPI::kSoftware: return std::make_unique(); #if SHELL_ENABLE_METAL case IOSRenderingAPI::kMetal: - return std::make_unique(); + switch (backend) { + case IOSRenderingBackend::kSkia: + return std::make_unique(); + case IOSRenderingBackend::kImpeller: + return std::make_unique(); + } #endif // SHELL_ENABLE_METAL default: break; @@ -35,4 +41,8 @@ std::unique_ptr IOSContext::Create(IOSRenderingAPI rendering_api) { return nullptr; } +IOSRenderingBackend IOSContext::GetBackend() const { + return IOSRenderingBackend::kSkia; +} + } // namespace flutter diff --git a/engine/src/flutter/shell/platform/darwin/ios/ios_context_metal_impeller.h b/engine/src/flutter/shell/platform/darwin/ios/ios_context_metal_impeller.h new file mode 100644 index 0000000000..6dbf7681ae --- /dev/null +++ b/engine/src/flutter/shell/platform/darwin/ios/ios_context_metal_impeller.h @@ -0,0 +1,46 @@ +// 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_IOS_CONTEXT_METAL_IMPELER_H_ +#define FLUTTER_SHELL_PLATFORM_DARWIN_IOS_IOS_CONTEXT_METAL_IMPELER_H_ + +#include "flutter/fml/macros.h" +#include "flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetal.h" +#include "flutter/shell/platform/darwin/ios/ios_context.h" + +namespace flutter { + +class IOSContextMetalImpeller final : public IOSContext { + public: + IOSContextMetalImpeller(); + + ~IOSContextMetalImpeller(); + + fml::scoped_nsobject GetDarwinContext() const; + + IOSRenderingBackend GetBackend() const override; + + // |IOSContext| + sk_sp GetMainContext() const override; + + sk_sp GetResourceContext() const; + + private: + // |IOSContext| + sk_sp CreateResourceContext() override; + + // |IOSContext| + std::unique_ptr MakeCurrent() override; + + // |IOSContext| + std::unique_ptr CreateExternalTexture( + int64_t texture_id, + fml::scoped_nsobject> texture) override; + + FML_DISALLOW_COPY_AND_ASSIGN(IOSContextMetalImpeller); +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_DARWIN_IOS_IOS_CONTEXT_METAL_IMPELER_H_ diff --git a/engine/src/flutter/shell/platform/darwin/ios/ios_context_metal_impeller.mm b/engine/src/flutter/shell/platform/darwin/ios/ios_context_metal_impeller.mm new file mode 100644 index 0000000000..5f1eec8d6e --- /dev/null +++ b/engine/src/flutter/shell/platform/darwin/ios/ios_context_metal_impeller.mm @@ -0,0 +1,47 @@ +// 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/ios_context_metal_impeller.h" + +namespace flutter { + +IOSContextMetalImpeller::IOSContextMetalImpeller() = default; + +IOSContextMetalImpeller::~IOSContextMetalImpeller() = default; + +fml::scoped_nsobject IOSContextMetalImpeller::GetDarwinContext() const { + return fml::scoped_nsobject{}; +} + +IOSRenderingBackend IOSContextMetalImpeller::GetBackend() const { + return IOSRenderingBackend::kImpeller; +} + +sk_sp IOSContextMetalImpeller::GetMainContext() const { + return nullptr; +} + +sk_sp IOSContextMetalImpeller::GetResourceContext() const { + return nullptr; +} + +// |IOSContext| +sk_sp IOSContextMetalImpeller::CreateResourceContext() { + return nullptr; +} + +// |IOSContext| +std::unique_ptr IOSContextMetalImpeller::MakeCurrent() { + // This only makes sense for contexts that need to be bound to a specific thread. + return std::make_unique(true); +} + +// |IOSContext| +std::unique_ptr IOSContextMetalImpeller::CreateExternalTexture( + int64_t texture_id, + fml::scoped_nsobject> texture) { + return nullptr; +} + +} // namespace flutter diff --git a/engine/src/flutter/shell/platform/darwin/ios/ios_context_metal.h b/engine/src/flutter/shell/platform/darwin/ios/ios_context_metal_skia.h similarity index 75% rename from engine/src/flutter/shell/platform/darwin/ios/ios_context_metal.h rename to engine/src/flutter/shell/platform/darwin/ios/ios_context_metal_skia.h index ebda072bea..9d0eac239a 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/ios_context_metal.h +++ b/engine/src/flutter/shell/platform/darwin/ios/ios_context_metal_skia.h @@ -2,8 +2,8 @@ // 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_IOS_CONTEXT_METAL_H_ -#define FLUTTER_SHELL_PLATFORM_DARWIN_IOS_IOS_CONTEXT_METAL_H_ +#ifndef FLUTTER_SHELL_PLATFORM_DARWIN_IOS_IOS_CONTEXT_METAL_SKIA_H_ +#define FLUTTER_SHELL_PLATFORM_DARWIN_IOS_IOS_CONTEXT_METAL_SKIA_H_ #include @@ -16,14 +16,17 @@ namespace flutter { -class IOSContextMetal final : public IOSContext { +class IOSContextMetalSkia final : public IOSContext { public: - IOSContextMetal(); + IOSContextMetalSkia(); - ~IOSContextMetal(); + ~IOSContextMetalSkia(); fml::scoped_nsobject GetDarwinContext() const; + // |IOSContext| + IOSRenderingBackend GetBackend() const override; + // |IOSContext| sk_sp GetMainContext() const override; @@ -45,9 +48,9 @@ class IOSContextMetal final : public IOSContext { int64_t texture_id, fml::scoped_nsobject> texture) override; - FML_DISALLOW_COPY_AND_ASSIGN(IOSContextMetal); + FML_DISALLOW_COPY_AND_ASSIGN(IOSContextMetalSkia); }; } // namespace flutter -#endif // FLUTTER_SHELL_PLATFORM_DARWIN_IOS_IOS_CONTEXT_METAL_H_ +#endif // FLUTTER_SHELL_PLATFORM_DARWIN_IOS_IOS_CONTEXT_METAL_SKIA_H_ diff --git a/engine/src/flutter/shell/platform/darwin/ios/ios_context_metal.mm b/engine/src/flutter/shell/platform/darwin/ios/ios_context_metal_skia.mm similarity index 75% rename from engine/src/flutter/shell/platform/darwin/ios/ios_context_metal.mm rename to engine/src/flutter/shell/platform/darwin/ios/ios_context_metal_skia.mm index d0205774cb..0e1d53beba 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/ios_context_metal.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/ios_context_metal_skia.mm @@ -2,7 +2,7 @@ // 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/ios_context_metal.h" +#import "flutter/shell/platform/darwin/ios/ios_context_metal_skia.h" #include "flutter/common/graphics/persistent_cache.h" #include "flutter/fml/logging.h" @@ -12,7 +12,7 @@ namespace flutter { -IOSContextMetal::IOSContextMetal() { +IOSContextMetalSkia::IOSContextMetalSkia() { darwin_context_metal_ = fml::scoped_nsobject{ [[FlutterDarwinContextMetal alloc] initWithDefaultMTLDevice]}; @@ -36,33 +36,37 @@ IOSContextMetal::IOSContextMetal() { texture_cache_.Reset(texture_cache_raw); } -IOSContextMetal::~IOSContextMetal() = default; +IOSContextMetalSkia::~IOSContextMetalSkia() = default; -fml::scoped_nsobject IOSContextMetal::GetDarwinContext() const { +fml::scoped_nsobject IOSContextMetalSkia::GetDarwinContext() const { return darwin_context_metal_; } -sk_sp IOSContextMetal::GetMainContext() const { +IOSRenderingBackend IOSContextMetalSkia::GetBackend() const { + return IOSRenderingBackend::kSkia; +} + +sk_sp IOSContextMetalSkia::GetMainContext() const { return darwin_context_metal_.get().mainContext; } -sk_sp IOSContextMetal::GetResourceContext() const { +sk_sp IOSContextMetalSkia::GetResourceContext() const { return darwin_context_metal_.get().resourceContext; } // |IOSContext| -sk_sp IOSContextMetal::CreateResourceContext() { +sk_sp IOSContextMetalSkia::CreateResourceContext() { return darwin_context_metal_.get().resourceContext; } // |IOSContext| -std::unique_ptr IOSContextMetal::MakeCurrent() { +std::unique_ptr IOSContextMetalSkia::MakeCurrent() { // This only makes sense for context that need to be bound to a specific thread. return std::make_unique(true); } // |IOSContext| -std::unique_ptr IOSContextMetal::CreateExternalTexture( +std::unique_ptr IOSContextMetalSkia::CreateExternalTexture( int64_t texture_id, fml::scoped_nsobject> texture) { return std::make_unique( diff --git a/engine/src/flutter/shell/platform/darwin/ios/ios_surface.mm b/engine/src/flutter/shell/platform/darwin/ios/ios_surface.mm index e0cc09760b..32522ae646 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/ios_surface.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/ios_surface.mm @@ -10,7 +10,8 @@ #include "flutter/shell/platform/darwin/ios/rendering_api_selection.h" #if SHELL_ENABLE_METAL -#import "flutter/shell/platform/darwin/ios/ios_surface_metal.h" +#import "flutter/shell/platform/darwin/ios/ios_surface_metal_impeller.h" +#import "flutter/shell/platform/darwin/ios/ios_surface_metal_skia.h" #endif // SHELL_ENABLE_METAL namespace flutter { @@ -31,11 +32,21 @@ std::unique_ptr IOSSurface::Create(std::shared_ptr conte #if SHELL_ENABLE_METAL if (@available(iOS METAL_IOS_VERSION_BASELINE, *)) { if ([layer.get() isKindOfClass:[CAMetalLayer class]]) { - return std::make_unique( - fml::scoped_nsobject( - reinterpret_cast([layer.get() retain])), // Metal layer - std::move(context) // context - ); + switch (context->GetBackend()) { + case IOSRenderingBackend::kSkia: + return std::make_unique( + fml::scoped_nsobject( + reinterpret_cast([layer.get() retain])), // Metal layer + std::move(context) // context + ); + break; + case IOSRenderingBackend::kImpeller: + return std::make_unique( + fml::scoped_nsobject( + reinterpret_cast([layer.get() retain])), // Metal layer + std::move(context) // context + ); + } } } #endif // SHELL_ENABLE_METAL diff --git a/engine/src/flutter/shell/platform/darwin/ios/ios_surface_metal_impeller.h b/engine/src/flutter/shell/platform/darwin/ios/ios_surface_metal_impeller.h new file mode 100644 index 0000000000..1a2eabc65c --- /dev/null +++ b/engine/src/flutter/shell/platform/darwin/ios/ios_surface_metal_impeller.h @@ -0,0 +1,59 @@ +// 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_IOS_SURFACE_METAL_IMPELLER_H_ +#define FLUTTER_SHELL_PLATFORM_DARWIN_IOS_IOS_SURFACE_METAL_IMPELLER_H_ + +#include "flutter/fml/macros.h" +#include "flutter/shell/gpu/gpu_surface_metal_delegate.h" +#import "flutter/shell/platform/darwin/ios/ios_surface.h" + +@class CAMetalLayer; + +namespace flutter { + +class SK_API_AVAILABLE_CA_METAL_LAYER IOSSurfaceMetalImpeller final + : public IOSSurface, + public GPUSurfaceMetalDelegate { + public: + IOSSurfaceMetalImpeller(fml::scoped_nsobject layer, + std::shared_ptr context); + + // |IOSSurface| + ~IOSSurfaceMetalImpeller(); + + private: + fml::scoped_nsobject layer_; + bool is_valid_ = false; + + // |IOSSurface| + bool IsValid() const override; + + // |IOSSurface| + void UpdateStorageSizeIfNecessary() override; + + // |IOSSurface| + std::unique_ptr CreateGPUSurface(GrDirectContext* gr_context) override; + + // |GPUSurfaceMetalDelegate| + GPUCAMetalLayerHandle GetCAMetalLayer(const SkISize& frame_info) const override; + + // |GPUSurfaceMetalDelegate| + bool PresentDrawable(GrMTLHandle drawable) const override; + + // |GPUSurfaceMetalDelegate| + GPUMTLTextureInfo GetMTLTexture(const SkISize& frame_info) const override; + + // |GPUSurfaceMetalDelegate| + bool PresentTexture(GPUMTLTextureInfo texture) const override; + + // |GPUSurfaceMetalDelegate| + bool AllowsDrawingWhenGpuDisabled() const override; + + FML_DISALLOW_COPY_AND_ASSIGN(IOSSurfaceMetalImpeller); +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_DARWIN_IOS_IOS_SURFACE_METAL_IMPELLER_H_ diff --git a/engine/src/flutter/shell/platform/darwin/ios/ios_surface_metal_impeller.mm b/engine/src/flutter/shell/platform/darwin/ios/ios_surface_metal_impeller.mm new file mode 100644 index 0000000000..16b39909ef --- /dev/null +++ b/engine/src/flutter/shell/platform/darwin/ios/ios_surface_metal_impeller.mm @@ -0,0 +1,71 @@ +// 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/ios_surface_metal_impeller.h" + +#include "flutter/shell/gpu/gpu_surface_metal_impeller.h" + +namespace flutter { + +IOSSurfaceMetalImpeller::IOSSurfaceMetalImpeller(fml::scoped_nsobject layer, + std::shared_ptr context) + : IOSSurface(std::move(context)), + GPUSurfaceMetalDelegate(MTLRenderTargetType::kCAMetalLayer), + layer_(std::move(layer)) {} + +// |IOSSurface| +IOSSurfaceMetalImpeller::~IOSSurfaceMetalImpeller() = default; + +// |IOSSurface| +bool IOSSurfaceMetalImpeller::IsValid() const { + return is_valid_; +} + +// |IOSSurface| +void IOSSurfaceMetalImpeller::UpdateStorageSizeIfNecessary() { + // Nothing to do. +} + +// |IOSSurface| +std::unique_ptr IOSSurfaceMetalImpeller::CreateGPUSurface(GrDirectContext*) { + return std::make_unique(this /* delegate */); +} + +// |GPUSurfaceMetalDelegate| +GPUCAMetalLayerHandle IOSSurfaceMetalImpeller::GetCAMetalLayer(const SkISize& frame_info) const { + CAMetalLayer* layer = layer_.get(); + const auto drawable_size = CGSizeMake(frame_info.width(), frame_info.height()); + if (!CGSizeEqualToSize(drawable_size, layer.drawableSize)) { + layer.drawableSize = drawable_size; + } + return layer; +} + +// |GPUSurfaceMetalDelegate| +bool IOSSurfaceMetalImpeller::PresentDrawable(GrMTLHandle drawable) const { + FML_DCHECK(false); + return false; +} + +// |GPUSurfaceMetalDelegate| +GPUMTLTextureInfo IOSSurfaceMetalImpeller::GetMTLTexture(const SkISize& frame_info) const { + FML_CHECK(false); + return GPUMTLTextureInfo{ + .texture_id = -1, // + .texture = nullptr // + }; +} + +// |GPUSurfaceMetalDelegate| +bool IOSSurfaceMetalImpeller::PresentTexture(GPUMTLTextureInfo texture) const { + FML_CHECK(false); + return false; +} + +// |GPUSurfaceMetalDelegate| +bool IOSSurfaceMetalImpeller::AllowsDrawingWhenGpuDisabled() const { + return false; +} + +} // namespace flutter diff --git a/engine/src/flutter/shell/platform/darwin/ios/ios_surface_metal.h b/engine/src/flutter/shell/platform/darwin/ios/ios_surface_metal_skia.h similarity index 69% rename from engine/src/flutter/shell/platform/darwin/ios/ios_surface_metal.h rename to engine/src/flutter/shell/platform/darwin/ios/ios_surface_metal_skia.h index 2424ceafca..86b6490d01 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/ios_surface_metal.h +++ b/engine/src/flutter/shell/platform/darwin/ios/ios_surface_metal_skia.h @@ -2,8 +2,8 @@ // 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_IOS_SURFACE_METAL_H_ -#define FLUTTER_SHELL_PLATFORM_DARWIN_IOS_IOS_SURFACE_METAL_H_ +#ifndef FLUTTER_SHELL_PLATFORM_DARWIN_IOS_IOS_SURFACE_METAL_SKIA_H_ +#define FLUTTER_SHELL_PLATFORM_DARWIN_IOS_IOS_SURFACE_METAL_SKIA_H_ #include "flutter/fml/macros.h" #include "flutter/shell/gpu/gpu_surface_metal_delegate.h" @@ -14,13 +14,14 @@ namespace flutter { -class SK_API_AVAILABLE_CA_METAL_LAYER IOSSurfaceMetal final : public IOSSurface, - public GPUSurfaceMetalDelegate { +class SK_API_AVAILABLE_CA_METAL_LAYER IOSSurfaceMetalSkia final : public IOSSurface, + public GPUSurfaceMetalDelegate { public: - IOSSurfaceMetal(fml::scoped_nsobject layer, std::shared_ptr context); + IOSSurfaceMetalSkia(fml::scoped_nsobject layer, + std::shared_ptr context); // |IOSSurface| - ~IOSSurfaceMetal(); + ~IOSSurfaceMetalSkia(); private: fml::scoped_nsobject layer_; @@ -52,9 +53,9 @@ class SK_API_AVAILABLE_CA_METAL_LAYER IOSSurfaceMetal final : public IOSSurface, // |GPUSurfaceMetalDelegate| bool AllowsDrawingWhenGpuDisabled() const override; - FML_DISALLOW_COPY_AND_ASSIGN(IOSSurfaceMetal); + FML_DISALLOW_COPY_AND_ASSIGN(IOSSurfaceMetalSkia); }; } // namespace flutter -#endif // FLUTTER_SHELL_PLATFORM_DARWIN_IOS_IOS_SURFACE_METAL_H_ +#endif // FLUTTER_SHELL_PLATFORM_DARWIN_IOS_IOS_SURFACE_METAL_SKIA_H_ diff --git a/engine/src/flutter/shell/platform/darwin/ios/ios_surface_metal.mm b/engine/src/flutter/shell/platform/darwin/ios/ios_surface_metal_skia.mm similarity index 64% rename from engine/src/flutter/shell/platform/darwin/ios/ios_surface_metal.mm rename to engine/src/flutter/shell/platform/darwin/ios/ios_surface_metal_skia.mm index 2df3fd0840..b0e76f5e10 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/ios_surface_metal.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/ios_surface_metal_skia.mm @@ -2,20 +2,20 @@ // 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/ios_surface_metal.h" +#import "flutter/shell/platform/darwin/ios/ios_surface_metal_skia.h" -#include "flutter/shell/gpu/gpu_surface_metal.h" #include "flutter/shell/gpu/gpu_surface_metal_delegate.h" -#include "flutter/shell/platform/darwin/ios/ios_context_metal.h" +#include "flutter/shell/gpu/gpu_surface_metal_skia.h" +#include "flutter/shell/platform/darwin/ios/ios_context_metal_skia.h" namespace flutter { -static IOSContextMetal* CastToMetalContext(const std::shared_ptr& context) { - return reinterpret_cast(context.get()); +static IOSContextMetalSkia* CastToMetalContext(const std::shared_ptr& context) { + return reinterpret_cast(context.get()); } -IOSSurfaceMetal::IOSSurfaceMetal(fml::scoped_nsobject layer, - std::shared_ptr context) +IOSSurfaceMetalSkia::IOSSurfaceMetalSkia(fml::scoped_nsobject layer, + std::shared_ptr context) : IOSSurface(std::move(context)), GPUSurfaceMetalDelegate(MTLRenderTargetType::kCAMetalLayer), layer_(std::move(layer)) { @@ -27,28 +27,28 @@ IOSSurfaceMetal::IOSSurfaceMetal(fml::scoped_nsobject layer, } // |IOSSurface| -IOSSurfaceMetal::~IOSSurfaceMetal() = default; +IOSSurfaceMetalSkia::~IOSSurfaceMetalSkia() = default; // |IOSSurface| -bool IOSSurfaceMetal::IsValid() const { +bool IOSSurfaceMetalSkia::IsValid() const { return is_valid_; } // |IOSSurface| -void IOSSurfaceMetal::UpdateStorageSizeIfNecessary() { +void IOSSurfaceMetalSkia::UpdateStorageSizeIfNecessary() { // Nothing to do. } // |IOSSurface| -std::unique_ptr IOSSurfaceMetal::CreateGPUSurface(GrDirectContext* context) { +std::unique_ptr IOSSurfaceMetalSkia::CreateGPUSurface(GrDirectContext* context) { FML_DCHECK(context); - return std::make_unique(this, // layer - sk_ref_sp(context) // context + return std::make_unique(this, // layer + sk_ref_sp(context) // context ); } // |GPUSurfaceMetalDelegate| -GPUCAMetalLayerHandle IOSSurfaceMetal::GetCAMetalLayer(const SkISize& frame_info) const { +GPUCAMetalLayerHandle IOSSurfaceMetalSkia::GetCAMetalLayer(const SkISize& frame_info) const { CAMetalLayer* layer = layer_.get(); layer.device = device_; @@ -71,7 +71,7 @@ GPUCAMetalLayerHandle IOSSurfaceMetal::GetCAMetalLayer(const SkISize& frame_info } // |GPUSurfaceMetalDelegate| -bool IOSSurfaceMetal::PresentDrawable(GrMTLHandle drawable) const { +bool IOSSurfaceMetalSkia::PresentDrawable(GrMTLHandle drawable) const { if (drawable == nullptr) { FML_DLOG(ERROR) << "Could not acquire next Metal drawable from the SkSurface."; return false; @@ -87,19 +87,19 @@ bool IOSSurfaceMetal::PresentDrawable(GrMTLHandle drawable) const { } // |GPUSurfaceMetalDelegate| -GPUMTLTextureInfo IOSSurfaceMetal::GetMTLTexture(const SkISize& frame_info) const { +GPUMTLTextureInfo IOSSurfaceMetalSkia::GetMTLTexture(const SkISize& frame_info) const { FML_CHECK(false) << "render to texture not supported on ios"; return {.texture_id = -1, .texture = nullptr}; } // |GPUSurfaceMetalDelegate| -bool IOSSurfaceMetal::PresentTexture(GPUMTLTextureInfo texture) const { +bool IOSSurfaceMetalSkia::PresentTexture(GPUMTLTextureInfo texture) const { FML_CHECK(false) << "render to texture not supported on ios"; return false; } // |GPUSurfaceMetalDelegate| -bool IOSSurfaceMetal::AllowsDrawingWhenGpuDisabled() const { +bool IOSSurfaceMetalSkia::AllowsDrawingWhenGpuDisabled() const { return false; } diff --git a/engine/src/flutter/shell/platform/darwin/ios/platform_view_ios.mm b/engine/src/flutter/shell/platform/darwin/ios/platform_view_ios.mm index 59026d08f6..02cb0fd9b6 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/platform_view_ios.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/platform_view_ios.mm @@ -56,7 +56,10 @@ PlatformViewIOS::PlatformViewIOS( const std::shared_ptr& platform_views_controller, flutter::TaskRunners task_runners) : PlatformViewIOS(delegate, - IOSContext::Create(rendering_api), + IOSContext::Create(rendering_api, + delegate.OnPlatformViewGetSettings().enable_impeller + ? IOSRenderingBackend::kImpeller + : IOSRenderingBackend::kSkia), platform_views_controller, task_runners) {} diff --git a/engine/src/flutter/shell/platform/darwin/ios/rendering_api_selection.h b/engine/src/flutter/shell/platform/darwin/ios/rendering_api_selection.h index 07fe13ccb1..6bef32e853 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/rendering_api_selection.h +++ b/engine/src/flutter/shell/platform/darwin/ios/rendering_api_selection.h @@ -17,6 +17,11 @@ enum class IOSRenderingAPI { kMetal, }; +enum class IOSRenderingBackend { + kSkia, + kImpeller, +}; + // Pass force_software to force software rendering. This is only respected on // simulators. IOSRenderingAPI GetRenderingAPIForProcess(bool force_software); diff --git a/engine/src/flutter/shell/platform/embedder/embedder_surface_metal.h b/engine/src/flutter/shell/platform/embedder/embedder_surface_metal.h index ba0d28130f..bb4c8579e2 100644 --- a/engine/src/flutter/shell/platform/embedder/embedder_surface_metal.h +++ b/engine/src/flutter/shell/platform/embedder/embedder_surface_metal.h @@ -6,8 +6,8 @@ #define FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_SURFACE_METAL_H_ #include "flutter/fml/macros.h" -#include "flutter/shell/gpu/gpu_surface_metal.h" #include "flutter/shell/gpu/gpu_surface_metal_delegate.h" +#include "flutter/shell/gpu/gpu_surface_metal_skia.h" #include "flutter/shell/platform/embedder/embedder_external_view_embedder.h" #include "flutter/shell/platform/embedder/embedder_surface.h" diff --git a/engine/src/flutter/shell/platform/embedder/embedder_surface_metal.mm b/engine/src/flutter/shell/platform/embedder/embedder_surface_metal.mm index 805b2f25b2..8499cab048 100644 --- a/engine/src/flutter/shell/platform/embedder/embedder_surface_metal.mm +++ b/engine/src/flutter/shell/platform/embedder/embedder_surface_metal.mm @@ -44,7 +44,7 @@ std::unique_ptr EmbedderSurfaceMetal::CreateGPUSurface() API_AVAILABLE( } const bool render_to_surface = !external_view_embedder_; - auto surface = std::make_unique(this, main_context_, render_to_surface); + auto surface = std::make_unique(this, main_context_, render_to_surface); if (!surface->IsValid()) { return nullptr; diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view_unittest.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view_unittest.cc index 9e6e92586e..2f5e8606eb 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view_unittest.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view_unittest.cc @@ -94,6 +94,10 @@ class MockPlatformViewDelegate : public flutter::PlatformView::Delegate { metrics_ = metrics; } // |flutter::PlatformView::Delegate| + const flutter::Settings& OnPlatformViewGetSettings() const { + return settings_; + } + // |flutter::PlatformView::Delegate| void OnPlatformViewDispatchPlatformMessage( std::unique_ptr message) { message_ = std::move(message); @@ -164,6 +168,7 @@ class MockPlatformViewDelegate : public flutter::PlatformView::Delegate { std::vector> pointer_packets_; int32_t semantics_features_ = 0; bool semantics_enabled_ = false; + flutter::Settings settings_; }; class MockResponse : public flutter::PlatformMessageResponse { diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/tests/flatland_platform_view_unittest.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/tests/flatland_platform_view_unittest.cc index 78d2ffb9c7..473f013a04 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/tests/flatland_platform_view_unittest.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/tests/flatland_platform_view_unittest.cc @@ -93,6 +93,10 @@ class MockPlatformViewDelegate : public flutter::PlatformView::Delegate { metrics_ = metrics; } // |flutter::PlatformView::Delegate| + const flutter::Settings& OnPlatformViewGetSettings() const { + return settings_; + } + // |flutter::PlatformView::Delegate| void OnPlatformViewDispatchPlatformMessage( std::unique_ptr message) { message_ = std::move(message); @@ -167,6 +171,7 @@ class MockPlatformViewDelegate : public flutter::PlatformView::Delegate { std::vector> pointer_packets_; int32_t semantics_features_ = 0; bool semantics_enabled_ = false; + flutter::Settings settings_; }; class MockResponse : public flutter::PlatformMessageResponse {