From 1ba45ac86eccb85fa303fbf52a74ecb9878d56c5 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Fri, 23 Aug 2024 10:06:16 -0700 Subject: [PATCH] [Engine] Add no op surface (flutter/engine#54694) Still needs to be tested in metalless environment. This should probably print out some kind of error message that tells folks that this is WAI, but I'm not sure if that would be too disruptive for g3. Fixes https://github.com/flutter/flutter/issues/153883 --- .../flutter/ci/licenses_golden/excluded_files | 2 + .../ci/licenses_golden/licenses_flutter | 8 ++ engine/src/flutter/shell/gpu/BUILD.gn | 7 +- .../src/flutter/shell/gpu/gpu_surface_noop.h | 68 +++++++++++++ .../src/flutter/shell/gpu/gpu_surface_noop.mm | 97 +++++++++++++++++++ .../shell/gpu/gpu_surface_noop_unittests.mm | 25 +++++ .../shell/platform/darwin/ios/BUILD.gn | 3 + .../shell/platform/darwin/ios/ios_surface.mm | 6 ++ .../platform/darwin/ios/ios_surface_noop.h | 46 +++++++++ .../platform/darwin/ios/ios_surface_noop.mm | 38 ++++++++ .../darwin/ios/ios_surface_noop_unittests.mm | 31 ++++++ 11 files changed, 330 insertions(+), 1 deletion(-) create mode 100644 engine/src/flutter/shell/gpu/gpu_surface_noop.h create mode 100644 engine/src/flutter/shell/gpu/gpu_surface_noop.mm create mode 100644 engine/src/flutter/shell/gpu/gpu_surface_noop_unittests.mm create mode 100644 engine/src/flutter/shell/platform/darwin/ios/ios_surface_noop.h create mode 100644 engine/src/flutter/shell/platform/darwin/ios/ios_surface_noop.mm create mode 100644 engine/src/flutter/shell/platform/darwin/ios/ios_surface_noop_unittests.mm diff --git a/engine/src/flutter/ci/licenses_golden/excluded_files b/engine/src/flutter/ci/licenses_golden/excluded_files index a8ee59cf82..bdc1265d8d 100644 --- a/engine/src/flutter/ci/licenses_golden/excluded_files +++ b/engine/src/flutter/ci/licenses_golden/excluded_files @@ -293,6 +293,7 @@ ../../../flutter/shell/common/variable_refresh_rate_display_unittests.cc ../../../flutter/shell/common/vsync_waiter_unittests.cc ../../../flutter/shell/gpu/gpu_surface_metal_impeller_unittests.mm +../../../flutter/shell/gpu/gpu_surface_noop_unittests.mm ../../../flutter/shell/platform/android/.gitignore ../../../flutter/shell/platform/android/README.md ../../../flutter/shell/platform/android/android_context_gl_impeller_unittests.cc @@ -337,6 +338,7 @@ ../../../flutter/shell/platform/darwin/common/availability_version_check_unittests.cc ../../../flutter/shell/platform/darwin/common/framework/Source/flutter_codecs_unittest.mm ../../../flutter/shell/platform/darwin/common/framework/Source/flutter_standard_codec_unittest.mm +../../../flutter/shell/platform/darwin/ios/ios_surface_noop_unittests.mm ../../../flutter/shell/platform/darwin/macos/README.md ../../../flutter/shell/platform/darwin/macos/framework/Source/fixtures ../../../flutter/shell/platform/embedder/fixtures diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 470accd69b..1f2f795bef 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -43983,6 +43983,8 @@ ORIGIN: ../../../flutter/shell/gpu/gpu_surface_metal_impeller.h + ../../../flutt ORIGIN: ../../../flutter/shell/gpu/gpu_surface_metal_impeller.mm + ../../../flutter/LICENSE ORIGIN: ../../../flutter/shell/gpu/gpu_surface_metal_skia.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/shell/gpu/gpu_surface_metal_skia.mm + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/shell/gpu/gpu_surface_noop.h + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/shell/gpu/gpu_surface_noop.mm + ../../../flutter/LICENSE ORIGIN: ../../../flutter/shell/gpu/gpu_surface_software.cc + ../../../flutter/LICENSE ORIGIN: ../../../flutter/shell/gpu/gpu_surface_software.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/shell/gpu/gpu_surface_software_delegate.cc + ../../../flutter/LICENSE @@ -44448,6 +44450,8 @@ ORIGIN: ../../../flutter/shell/platform/darwin/ios/ios_surface_metal_impeller.h ORIGIN: ../../../flutter/shell/platform/darwin/ios/ios_surface_metal_impeller.mm + ../../../flutter/LICENSE ORIGIN: ../../../flutter/shell/platform/darwin/ios/ios_surface_metal_skia.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/shell/platform/darwin/ios/ios_surface_metal_skia.mm + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/shell/platform/darwin/ios/ios_surface_noop.h + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/shell/platform/darwin/ios/ios_surface_noop.mm + ../../../flutter/LICENSE ORIGIN: ../../../flutter/shell/platform/darwin/ios/ios_surface_software.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/shell/platform/darwin/ios/ios_surface_software.mm + ../../../flutter/LICENSE ORIGIN: ../../../flutter/shell/platform/darwin/ios/platform_message_handler_ios.h + ../../../flutter/LICENSE @@ -46874,6 +46878,8 @@ 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_noop.h +FILE: ../../../flutter/shell/gpu/gpu_surface_noop.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 @@ -47353,6 +47359,8 @@ 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_noop.h +FILE: ../../../flutter/shell/platform/darwin/ios/ios_surface_noop.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/platform_message_handler_ios.h diff --git a/engine/src/flutter/shell/gpu/BUILD.gn b/engine/src/flutter/shell/gpu/BUILD.gn index 0e7e686705..ebf3bc45e1 100644 --- a/engine/src/flutter/shell/gpu/BUILD.gn +++ b/engine/src/flutter/shell/gpu/BUILD.gn @@ -75,6 +75,8 @@ source_set("gpu_surface_metal") { "gpu_surface_metal_delegate.h", "gpu_surface_metal_skia.h", "gpu_surface_metal_skia.mm", + "gpu_surface_noop.h", + "gpu_surface_noop.mm", ] public_deps = gpu_common_deps @@ -93,7 +95,10 @@ if (is_mac) { impeller_component("gpu_surface_metal_unittests") { testonly = true target_type = "executable" - sources = [ "gpu_surface_metal_impeller_unittests.mm" ] + sources = [ + "gpu_surface_metal_impeller_unittests.mm", + "gpu_surface_noop_unittests.mm", + ] frameworks = [ "AppKit.framework", diff --git a/engine/src/flutter/shell/gpu/gpu_surface_noop.h b/engine/src/flutter/shell/gpu/gpu_surface_noop.h new file mode 100644 index 0000000000..e1a5498ee4 --- /dev/null +++ b/engine/src/flutter/shell/gpu/gpu_surface_noop.h @@ -0,0 +1,68 @@ +// 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_NOOP_H_ +#define FLUTTER_SHELL_GPU_GPU_SURFACE_NOOP_H_ + +#include + +#include "flutter/flow/surface.h" + +namespace flutter { + +/// @brief A rendering surface that accepts rendering intent but does not render +/// anything. +/// +/// This is useful for running on platforms that need an engine instance and +/// don't have the required drivers. +class GPUSurfaceNoop : public Surface { + public: + explicit GPUSurfaceNoop(); + + // |Surface| + ~GPUSurfaceNoop(); + + // |Surface| + bool IsValid() override; + + // |Surface| + Surface::SurfaceData GetSurfaceData() const override; + + private: + // |Surface| + std::unique_ptr AcquireFrame( + const SkISize& frame_size) override; + + std::unique_ptr AcquireFrameFromCAMetalLayer( + const SkISize& frame_size); + + std::unique_ptr AcquireFrameFromMTLTexture( + const SkISize& frame_size); + + // |Surface| + SkMatrix GetRootTransformation() const override; + + // |Surface| + GrDirectContext* GetContext() override; + + // |Surface| + std::unique_ptr MakeRenderContextCurrent() override; + + // |Surface| + bool AllowsDrawingWhenGpuDisabled() const override; + + // |Surface| + bool EnableRasterCache() const override; + + // |Surface| + std::shared_ptr GetAiksContext() const override; + + GPUSurfaceNoop(const GPUSurfaceNoop&) = delete; + + GPUSurfaceNoop& operator=(const GPUSurfaceNoop&) = delete; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_GPU_GPU_SURFACE_NOOP_H_ diff --git a/engine/src/flutter/shell/gpu/gpu_surface_noop.mm b/engine/src/flutter/shell/gpu/gpu_surface_noop.mm new file mode 100644 index 0000000000..31e15d6a43 --- /dev/null +++ b/engine/src/flutter/shell/gpu/gpu_surface_noop.mm @@ -0,0 +1,97 @@ +// 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_noop.h" + +#import +#import + +#include "flow/surface.h" +#include "flow/surface_frame.h" +#include "flutter/common/settings.h" +#include "flutter/fml/mapping.h" +#include "flutter/fml/trace_event.h" + +static_assert(!__has_feature(objc_arc), "ARC must be disabled."); + +namespace flutter { + +GPUSurfaceNoop::GPUSurfaceNoop() = default; + +GPUSurfaceNoop::~GPUSurfaceNoop() = default; + +// |Surface| +bool GPUSurfaceNoop::IsValid() { + return true; +} + +Surface::SurfaceData GPUSurfaceNoop::GetSurfaceData() const { + return Surface::SurfaceData{}; +} + +// |Surface| +std::unique_ptr GPUSurfaceNoop::AcquireFrame(const SkISize& frame_size) { + auto callback = [](const SurfaceFrame&, DlCanvas*) { return true; }; + auto submit_callback = [](const SurfaceFrame&) { return true; }; + SurfaceFrame::FramebufferInfo framebuffer_info; + + return std::make_unique( + /*surface=*/nullptr, + /*framebuffer_info=*/framebuffer_info, + /*encode_callback=*/callback, + /*submit_callback=*/submit_callback, + /*frame_size=*/frame_size, + /*context_result=*/nullptr, + /*display_list_fallback=*/true); +} + +std::unique_ptr GPUSurfaceNoop::AcquireFrameFromMTLTexture( + const SkISize& frame_size) { + auto callback = [](const SurfaceFrame&, DlCanvas*) { return true; }; + auto submit_callback = [](const SurfaceFrame&) { return true; }; + SurfaceFrame::FramebufferInfo framebuffer_info; + + return std::make_unique( + /*surface=*/nullptr, + /*framebuffer_info=*/framebuffer_info, + /*encode_callback=*/callback, + /*submit_callback=*/submit_callback, + /*frame_size=*/frame_size, + /*context_result=*/nullptr, + /*display_list_fallback=*/true); +} + +// |Surface| +SkMatrix GPUSurfaceNoop::GetRootTransformation() const { + // This backend does not currently support root surface transformations. Just + // return identity. + return {}; +} + +// |Surface| +GrDirectContext* GPUSurfaceNoop::GetContext() { + return nullptr; +} + +// |Surface| +std::unique_ptr GPUSurfaceNoop::MakeRenderContextCurrent() { + // This backend has no such concept. + return std::make_unique(true); +} + +bool GPUSurfaceNoop::AllowsDrawingWhenGpuDisabled() const { + return true; +} + +// |Surface| +bool GPUSurfaceNoop::EnableRasterCache() const { + return false; +} + +// |Surface| +std::shared_ptr GPUSurfaceNoop::GetAiksContext() const { + return nullptr; +} + +} // namespace flutter diff --git a/engine/src/flutter/shell/gpu/gpu_surface_noop_unittests.mm b/engine/src/flutter/shell/gpu/gpu_surface_noop_unittests.mm new file mode 100644 index 0000000000..ec462a400c --- /dev/null +++ b/engine/src/flutter/shell/gpu/gpu_surface_noop_unittests.mm @@ -0,0 +1,25 @@ +// 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 +#include + +#include "flutter/shell/gpu/gpu_surface_noop.h" +#include "gtest/gtest.h" +#include "impeller/entity/mtl/entity_shaders.h" +#include "impeller/entity/mtl/framebuffer_blend_shaders.h" +#include "impeller/entity/mtl/modern_shaders.h" +#include "impeller/renderer/backend/metal/context_mtl.h" + +namespace flutter { +namespace testing { + +TEST(GPUSurfaceNoop, InvalidImpellerContextCreatesCausesSurfaceToBeInvalid) { + auto surface = std::make_shared(); + + EXPECT_TRUE(surface->IsValid()); +} + +} // namespace testing +} // namespace flutter diff --git a/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn b/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn index f63d7e1f7f..10fcaab633 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn +++ b/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn @@ -137,6 +137,8 @@ source_set("flutter_framework_source_arc") { "ios_surface_metal_impeller.mm", "ios_surface_metal_skia.h", "ios_surface_metal_skia.mm", + "ios_surface_noop.h", + "ios_surface_noop.mm", "ios_surface_software.h", "ios_surface_software.mm", "platform_message_handler_ios.h", @@ -284,6 +286,7 @@ shared_library("ios_test_flutter") { "framework/Source/accessibility_bridge_test.mm", "framework/Source/availability_version_check_test.mm", "framework/Source/connection_collection_test.mm", + "ios_surface_noop_unittests.mm", "platform_message_handler_ios_test.mm", ] deps = [ 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 33a71c090a..ae27f5fb94 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/ios_surface.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/ios_surface.mm @@ -4,8 +4,11 @@ #import "flutter/shell/platform/darwin/ios/ios_surface.h" +#include + #import "flutter/shell/platform/darwin/ios/ios_surface_metal_impeller.h" #import "flutter/shell/platform/darwin/ios/ios_surface_metal_skia.h" +#import "flutter/shell/platform/darwin/ios/ios_surface_noop.h" #import "flutter/shell/platform/darwin/ios/ios_surface_software.h" #include "flutter/shell/platform/darwin/ios/rendering_api_selection.h" @@ -40,6 +43,9 @@ std::unique_ptr IOSSurface::Create(std::shared_ptr conte } } } + if (context->GetBackend() == IOSRenderingBackend::kImpeller) { + return std::make_unique(std::move(context)); + } return std::make_unique(layer, // layer std::move(context) // context diff --git a/engine/src/flutter/shell/platform/darwin/ios/ios_surface_noop.h b/engine/src/flutter/shell/platform/darwin/ios/ios_surface_noop.h new file mode 100644 index 0000000000..d3902a901e --- /dev/null +++ b/engine/src/flutter/shell/platform/darwin/ios/ios_surface_noop.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_SURFACE_NOOP_H_ +#define FLUTTER_SHELL_PLATFORM_DARWIN_IOS_IOS_SURFACE_NOOP_H_ + +#include "flutter/fml/platform/darwin/scoped_nsobject.h" +#import "flutter/shell/platform/darwin/ios/ios_context.h" +#import "flutter/shell/platform/darwin/ios/ios_surface.h" + +#include "third_party/skia/include/core/SkSurface.h" + +@class CALayer; + +namespace flutter { + +/// @brief A rendering surface that accepts rendering intent but does not render +/// anything. +/// +/// This is useful for running on platforms that need an engine instance and +/// don't have the required drivers. +class IOSSurfaceNoop final : public IOSSurface { + public: + explicit IOSSurfaceNoop(std::shared_ptr context); + + ~IOSSurfaceNoop() override; + + // |IOSSurface| + bool IsValid() const override; + + // |IOSSurface| + void UpdateStorageSizeIfNecessary() override; + + // |IOSSurface| + std::unique_ptr CreateGPUSurface(GrDirectContext* gr_context = nullptr) override; + + private: + IOSSurfaceNoop(const IOSSurfaceNoop&) = delete; + + IOSSurfaceNoop& operator=(const IOSSurfaceNoop&) = delete; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_DARWIN_IOS_IOS_SURFACE_NOOP_H_ diff --git a/engine/src/flutter/shell/platform/darwin/ios/ios_surface_noop.mm b/engine/src/flutter/shell/platform/darwin/ios/ios_surface_noop.mm new file mode 100644 index 0000000000..813e2e1a21 --- /dev/null +++ b/engine/src/flutter/shell/platform/darwin/ios/ios_surface_noop.mm @@ -0,0 +1,38 @@ +// 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_noop.h" +#include "shell/gpu/gpu_surface_noop.h" + +#include + +#include + +#include "flutter/fml/logging.h" +#include "flutter/fml/platform/darwin/cf_utils.h" +#include "flutter/fml/trace_event.h" + +#include "third_party/skia/include/core/SkSurface.h" +#include "third_party/skia/include/utils/mac/SkCGUtils.h" + +FLUTTER_ASSERT_ARC + +namespace flutter { + +IOSSurfaceNoop::IOSSurfaceNoop(std::shared_ptr context) + : IOSSurface(std::move(context)) {} + +IOSSurfaceNoop::~IOSSurfaceNoop() = default; + +bool IOSSurfaceNoop::IsValid() const { + return true; +} + +void IOSSurfaceNoop::UpdateStorageSizeIfNecessary() {} + +std::unique_ptr IOSSurfaceNoop::CreateGPUSurface(GrDirectContext* gr_context) { + return std::make_unique(); +} + +} // namespace flutter diff --git a/engine/src/flutter/shell/platform/darwin/ios/ios_surface_noop_unittests.mm b/engine/src/flutter/shell/platform/darwin/ios/ios_surface_noop_unittests.mm new file mode 100644 index 0000000000..567026f144 --- /dev/null +++ b/engine/src/flutter/shell/platform/darwin/ios/ios_surface_noop_unittests.mm @@ -0,0 +1,31 @@ +// 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 +#import + +#import "flutter/shell/platform/darwin/ios/ios_surface_noop.h" + +#import "flutter/common/task_runners.h" +#import "flutter/fml/message_loop.h" +#import "flutter/fml/thread.h" +#import "flutter/lib/ui/window/platform_message.h" +#import "flutter/lib/ui/window/platform_message_response.h" +#import "flutter/shell/common/thread_host.h" +#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h" + +FLUTTER_ASSERT_ARC + +@interface IOSSurfaceNoopTest : XCTestCase +@end + +@implementation IOSSurfaceNoopTest +- (void)testCreateSurface { + flutter::IOSSurfaceNoop noop(nullptr); + + XCTAssertTrue(noop.IsValid()); + XCTAssertTrue(!!noop.CreateGPUSurface()); +} + +@end