From 2c1e5efae37bbb939aae8f84943077d10fe52de3 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Fri, 2 Dec 2022 16:01:33 -0800 Subject: [PATCH] [Impeller] add --enable-impeller-3d flag to support scene experimentation (#37990) * ++ * ++ * ++ * ++ * ++ * ++ * ++ * ++ --- ci/licenses_golden/licenses_flutter | 3 ++ impeller/tools/impeller.gni | 3 ++ lib/snapshot/BUILD.gn | 10 ++++-- lib/snapshot/libraries_experimental.json | 17 ++++++++++ lib/ui/BUILD.gn | 3 ++ lib/ui/compositing/scene_builder.cc | 16 +++++++++ lib/ui/compositing/scene_builder.h | 9 +++++ lib/ui/dart_ui.cc | 7 ++++ lib/ui/experiments/compositing_3d.dart | 18 ++++++++++ lib/ui/experiments/ui.dart | 43 ++++++++++++++++++++++++ tools/gn | 10 ++++++ 11 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 lib/snapshot/libraries_experimental.json create mode 100644 lib/ui/experiments/compositing_3d.dart create mode 100644 lib/ui/experiments/ui.dart diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index bb63196688c61..05afde6907752 100644 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -1714,6 +1714,7 @@ FILE: ../../../flutter/impeller/typographer/typographer_unittests.cc FILE: ../../../flutter/lib/io/dart_io.cc FILE: ../../../flutter/lib/io/dart_io.h FILE: ../../../flutter/lib/snapshot/libraries.json +FILE: ../../../flutter/lib/snapshot/libraries_experimental.json FILE: ../../../flutter/lib/snapshot/snapshot.h FILE: ../../../flutter/lib/ui/annotations.dart FILE: ../../../flutter/lib/ui/channel_buffers.dart @@ -1728,6 +1729,8 @@ FILE: ../../../flutter/lib/ui/dart_runtime_hooks.h FILE: ../../../flutter/lib/ui/dart_ui.cc FILE: ../../../flutter/lib/ui/dart_ui.h FILE: ../../../flutter/lib/ui/dart_wrapper.h +FILE: ../../../flutter/lib/ui/experiments/compositing_3d.dart +FILE: ../../../flutter/lib/ui/experiments/ui.dart FILE: ../../../flutter/lib/ui/geometry.dart FILE: ../../../flutter/lib/ui/hash_codes.dart FILE: ../../../flutter/lib/ui/hooks.dart diff --git a/impeller/tools/impeller.gni b/impeller/tools/impeller.gni index 11e775cd54f28..ae39455ad68e4 100644 --- a/impeller/tools/impeller.gni +++ b/impeller/tools/impeller.gni @@ -35,6 +35,9 @@ declare_args() { # Call glGetError after each OpenGL call and log failures. impeller_error_check_all_gl_calls = is_debug + + # Eperimentally enable 3d code paths. + impeller_enable_3d = false } declare_args() { diff --git a/lib/snapshot/BUILD.gn b/lib/snapshot/BUILD.gn index d8176bdf5151d..98bee65ef8d1d 100644 --- a/lib/snapshot/BUILD.gn +++ b/lib/snapshot/BUILD.gn @@ -5,6 +5,7 @@ import("//build/compiled_action.gni") import("//build/fuchsia/sdk.gni") import("//flutter/common/config.gni") +import("//flutter/impeller/tools/impeller.gni") import("//flutter/lib/ui/dart_ui.gni") import("//third_party/dart/utils/compile_platform.gni") @@ -315,8 +316,13 @@ source_set("snapshot") { compile_platform("strong_platform") { single_root_scheme = "org-dartlang-sdk" single_root_base = rebase_path("../../../") - libraries_specification_uri = - "org-dartlang-sdk:///flutter/lib/snapshot/libraries.json" + if (impeller_enable_3d) { + libraries_specification_uri = + "org-dartlang-sdk:///flutter/lib/snapshot/libraries_experimental.json" + } else { + libraries_specification_uri = + "org-dartlang-sdk:///flutter/lib/snapshot/libraries.json" + } outputs = [ "$root_out_dir/flutter_patched_sdk/platform_strong.dill", diff --git a/lib/snapshot/libraries_experimental.json b/lib/snapshot/libraries_experimental.json new file mode 100644 index 0000000000000..08577acb38ace --- /dev/null +++ b/lib/snapshot/libraries_experimental.json @@ -0,0 +1,17 @@ +{ + "comment:0": "NOTE: THIS FILE IS GENERATED. DO NOT EDIT.", + "comment:1": "Instead modify 'flutter/lib/snapshot/libraries.yaml' and follow the instructions therein.", + "flutter": { + "include": [ + { + "path": "../../../third_party/dart/sdk/lib/libraries.json", + "target": "vm_common" + } + ], + "libraries": { + "ui": { + "uri": "../../lib/ui/experiments/ui.dart" + } + } + } + } \ No newline at end of file diff --git a/lib/ui/BUILD.gn b/lib/ui/BUILD.gn index fff9b1cf7bc4a..9048897a15ff1 100644 --- a/lib/ui/BUILD.gn +++ b/lib/ui/BUILD.gn @@ -189,6 +189,9 @@ source_set("ui") { # Required for M_PI and others. defines += [ "_USE_MATH_DEFINES" ] } + if (impeller_enable_3d) { + defines += [ "IMPELLER_ENABLE_3D" ] + } } if (enable_unittests) { diff --git a/lib/ui/compositing/scene_builder.cc b/lib/ui/compositing/scene_builder.cc index 6f1c718fa1b5f..deba7695f4fb7 100644 --- a/lib/ui/compositing/scene_builder.cc +++ b/lib/ui/compositing/scene_builder.cc @@ -262,6 +262,22 @@ void SceneBuilder::addTexture(double dx, AddLayer(std::move(layer)); } +#ifdef IMPELLER_ENABLE_3D +// static +void SceneBuilder::addModelLayer(Dart_Handle wrapper, + double dx, + double dy, + double width, + double height, + int64_t viewId) { + auto* scene_builder = tonic::DartConverter::FromDart(wrapper); + SkMatrix sk_matrix = SkMatrix::Translate(dx, dy); + auto layer = std::make_shared(sk_matrix); + scene_builder->AddLayer(std::move(layer)); +} + +#endif // IMPELLER_ENABLE_3D + void SceneBuilder::addPlatformView(double dx, double dy, double width, diff --git a/lib/ui/compositing/scene_builder.h b/lib/ui/compositing/scene_builder.h index f2de29c4802d2..04d350236dabb 100644 --- a/lib/ui/compositing/scene_builder.h +++ b/lib/ui/compositing/scene_builder.h @@ -34,6 +34,15 @@ class SceneBuilder : public RefCountedDartWrappable { res->AssociateWithDartWrapper(wrapper); } +#ifdef IMPELLER_ENABLE_3D + static void addModelLayer(Dart_Handle wrapper, + double dx, + double dy, + double width, + double height, + int64_t viewId); +#endif // IMPELLER_ENABLE_3D + ~SceneBuilder() override; void pushTransformHandle(Dart_Handle layer_handle, diff --git a/lib/ui/dart_ui.cc b/lib/ui/dart_ui.cc index 0410926e68334..4d1a6bca3ac0d 100644 --- a/lib/ui/dart_ui.cc +++ b/lib/ui/dart_ui.cc @@ -294,6 +294,10 @@ typedef CanvasPath Path; V(SemanticsUpdate, dispose, 1) \ V(Vertices, dispose, 1) +#ifdef IMPELLER_ENABLE_3D +#define FFI_METHOD_LIST_3D(V) V(SceneBuilder::addModelLayer, 7) +#endif // IMPELLER_ENABLE_3D + #define FFI_FUNCTION_INSERT(FUNCTION, ARGS) \ g_function_dispatchers.insert(std::make_pair( \ std::string_view(#FUNCTION), \ @@ -320,6 +324,9 @@ void* ResolveFfiNativeFunction(const char* name, uintptr_t args) { void InitDispatcherMap() { FFI_FUNCTION_LIST(FFI_FUNCTION_INSERT) FFI_METHOD_LIST(FFI_METHOD_INSERT) +#ifdef IMPELLER_ENABLE_3D + FFI_METHOD_LIST_3D(FFI_FUNCTION_INSERT) +#endif // IMPELLER_ENABLE_3D } } // anonymous namespace diff --git a/lib/ui/experiments/compositing_3d.dart b/lib/ui/experiments/compositing_3d.dart new file mode 100644 index 0000000000000..3fce6a31e7844 --- /dev/null +++ b/lib/ui/experiments/compositing_3d.dart @@ -0,0 +1,18 @@ +// 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. +part of dart.ui; + +void addModelLayer( + SceneBuilder builder, + int viewId, { + Offset offset = Offset.zero, + double width = 0.0, + double height = 0.0, +}) { + assert(offset != null, 'Offset argument was null'); + _addModelLayer(builder, offset.dx, offset.dy, width, height, viewId); +} + +@FfiNative('SceneBuilder::addModelLayer') +external void _addModelLayer(Object object, double dx, double dy, double width, double height, int viewId); diff --git a/lib/ui/experiments/ui.dart b/lib/ui/experiments/ui.dart new file mode 100644 index 0000000000000..e6ee89e1097d2 --- /dev/null +++ b/lib/ui/experiments/ui.dart @@ -0,0 +1,43 @@ +// 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. + +/// Built-in types and core primitives for a Flutter application. +/// +/// To use, import `dart:ui`. +/// +/// This library exposes the lowest-level services that Flutter frameworks use +/// to bootstrap applications, such as classes for driving the input, graphics +/// text, layout, and rendering subsystems. +library dart.ui; + +import 'dart:async'; +import 'dart:collection' as collection; +import 'dart:convert'; +import 'dart:developer' as developer; +import 'dart:ffi'; +import 'dart:io'; +import 'dart:isolate' show SendPort; +import 'dart:math' as math; +import 'dart:nativewrappers'; +import 'dart:typed_data'; + +part '../annotations.dart'; +part '../channel_buffers.dart'; +part '../compositing.dart'; +part 'compositing_3d.dart'; +part '../geometry.dart'; +part '../hash_codes.dart'; +part '../hooks.dart'; +part '../isolate_name_server.dart'; +part '../key.dart'; +part '../lerp.dart'; +part '../math.dart'; +part '../natives.dart'; +part '../painting.dart'; +part '../platform_dispatcher.dart'; +part '../plugins.dart'; +part '../pointer.dart'; +part '../semantics.dart'; +part '../text.dart'; +part '../window.dart'; diff --git a/tools/gn b/tools/gn index 27f65988f53b3..863565e7bc131 100755 --- a/tools/gn +++ b/tools/gn @@ -551,6 +551,9 @@ def to_gn_args(args): elif os.getenv('FLUTTER_IMPELLER_ENABLE_PLAYGROUND', '0') == '1': gn_args['impeller_enable_playground'] = True + if args.enable_impeller_3d: + gn_args['impeller_enable_3d'] = True + if args.prebuilt_impellerc is not None: gn_args['impeller_use_prebuilt_impellerc'] = args.prebuilt_impellerc @@ -978,6 +981,13 @@ def parse_args(args): 'https://github.com/flutter/flutter/issues/107357' ) + parser.add_argument( + '--enable-impeller-3d', + default=False, + action='store_true', + help='Enables experimental 3d support.' + ) + # Sanitizers. parser.add_argument('--asan', default=False, action='store_true') parser.add_argument('--lsan', default=False, action='store_true')