From 07c646b28d0fb9e73815ebd5526f3a6c553a8e24 Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Thu, 3 Aug 2023 08:11:57 -0700 Subject: [PATCH] [Flutter GPU] Export symbols from engine, stub for testing on CI. (flutter/engine#44280) Part of https://github.com/flutter/flutter/issues/131346 Stubs a minimal test of the FFI utilities that `dart:ui` uses, but using public symbols exported from the engine library. If this goes well, I'll move the stuff from `dart:ui` into here and begin landing parts of the API with test coverage. --- .../ci/licenses_golden/licenses_flutter | 6 ++ .../src/flutter/common/exported_symbols.sym | 2 + .../flutter/common/exported_symbols_mac.sym | 2 + engine/src/flutter/lib/gpu/BUILD.gn | 51 ++++++++++++++++ engine/src/flutter/lib/gpu/gpu.cc | 60 +++++++++++++++++++ engine/src/flutter/lib/gpu/gpu.dart | 39 ++++++++++++ engine/src/flutter/lib/gpu/gpu.h | 57 ++++++++++++++++++ engine/src/flutter/runtime/BUILD.gn | 1 + engine/src/flutter/testing/dart/BUILD.gn | 1 + engine/src/flutter/testing/dart/gpu_test.dart | 24 ++++++++ .../testing/symbols/verify_exported.dart | 13 ++-- 11 files changed, 252 insertions(+), 4 deletions(-) create mode 100644 engine/src/flutter/lib/gpu/BUILD.gn create mode 100644 engine/src/flutter/lib/gpu/gpu.cc create mode 100644 engine/src/flutter/lib/gpu/gpu.dart create mode 100644 engine/src/flutter/lib/gpu/gpu.h create mode 100644 engine/src/flutter/testing/dart/gpu_test.dart diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 0d16668ffb..9207b90c4c 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -1716,6 +1716,9 @@ ORIGIN: ../../../flutter/impeller/typographer/text_run.cc + ../../../flutter/LIC ORIGIN: ../../../flutter/impeller/typographer/text_run.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/typographer/typeface.cc + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/typographer/typeface.h + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/lib/gpu/gpu.cc + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/lib/gpu/gpu.dart + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/lib/gpu/gpu.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/lib/io/dart_io.cc + ../../../flutter/LICENSE ORIGIN: ../../../flutter/lib/io/dart_io.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/lib/snapshot/snapshot.h + ../../../flutter/LICENSE @@ -4415,6 +4418,9 @@ FILE: ../../../flutter/impeller/typographer/text_run.cc FILE: ../../../flutter/impeller/typographer/text_run.h FILE: ../../../flutter/impeller/typographer/typeface.cc FILE: ../../../flutter/impeller/typographer/typeface.h +FILE: ../../../flutter/lib/gpu/gpu.cc +FILE: ../../../flutter/lib/gpu/gpu.dart +FILE: ../../../flutter/lib/gpu/gpu.h FILE: ../../../flutter/lib/io/dart_io.cc FILE: ../../../flutter/lib/io/dart_io.h FILE: ../../../flutter/lib/snapshot/libraries_experimental.json diff --git a/engine/src/flutter/common/exported_symbols.sym b/engine/src/flutter/common/exported_symbols.sym index d85c253e3c..c9ad42cd33 100644 --- a/engine/src/flutter/common/exported_symbols.sym +++ b/engine/src/flutter/common/exported_symbols.sym @@ -5,4 +5,6 @@ kDartVmSnapshotInstructions; kDartIsolateSnapshotData; kDartIsolateSnapshotInstructions; + InternalFlutterGpu*; + kInternalFlutterGpu*; }; diff --git a/engine/src/flutter/common/exported_symbols_mac.sym b/engine/src/flutter/common/exported_symbols_mac.sym index c65b740925..09687a7542 100644 --- a/engine/src/flutter/common/exported_symbols_mac.sym +++ b/engine/src/flutter/common/exported_symbols_mac.sym @@ -4,3 +4,5 @@ _kDartVmSnapshotData _kDartVmSnapshotInstructions _kDartIsolateSnapshotData _kDartIsolateSnapshotInstructions +_InternalFlutterGpu* +_kInternalFlutterGpu* diff --git a/engine/src/flutter/lib/gpu/BUILD.gn b/engine/src/flutter/lib/gpu/BUILD.gn new file mode 100644 index 0000000000..7b24bf4007 --- /dev/null +++ b/engine/src/flutter/lib/gpu/BUILD.gn @@ -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. + +import("//flutter/common/config.gni") +import("//flutter/impeller/tools/impeller.gni") +import("//flutter/testing/testing.gni") + +source_set("gpu") { + cflags = [ + # Dart gives us doubles. Skia and Impeller work in floats. + # If Dart gives us a double > FLT_MAX or < -FLT_MAX, implicit conversion + # will convert it to either inf/-inf or FLT_MAX/-FLT_MAX (undefined + # behavior). This can result in surprising and difficult to debug behavior + # for Flutter application developers, so it should be explicitly handled + # via SafeNarrow. + "-Wimplicit-float-conversion", + ] + + if (is_win) { + # This causes build failures in third_party dependencies on Windows. + cflags += [ "-Wno-implicit-int-float-conversion" ] + } + + public_configs = [ "//flutter:config" ] + + public_deps = [] + + if (!defined(defines)) { + defines = [] + } + + sources = [ + "gpu.cc", + "gpu.h", + ] + deps = [ + "//flutter/impeller", + "//flutter/impeller/display_list:skia_conversions", + "//flutter/lib/ui", + "//flutter/third_party/tonic", + ] + + if (is_win) { + # Required for M_PI and others. + defines += [ "_USE_MATH_DEFINES" ] + } +} + +if (enable_unittests) { +} diff --git a/engine/src/flutter/lib/gpu/gpu.cc b/engine/src/flutter/lib/gpu/gpu.cc new file mode 100644 index 0000000000..4d6bf37bbc --- /dev/null +++ b/engine/src/flutter/lib/gpu/gpu.cc @@ -0,0 +1,60 @@ +// 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/lib/gpu/gpu.h" +#include "flutter/fml/memory/ref_ptr.h" +#include "flutter/lib/ui/dart_wrapper.h" +#include "flutter/lib/ui/ui_dart_state.h" +#include "third_party/dart/runtime/include/dart_api.h" +#include "third_party/tonic/converter/dart_converter.h" +#include "third_party/tonic/dart_state.h" +#include "third_party/tonic/dart_wrappable.h" +#include "third_party/tonic/dart_wrapper_info.h" +#include "third_party/tonic/logging/dart_invoke.h" + +namespace flutter { + +// TODO(131346): Remove this once we migrate the Dart GPU API into this space. +IMPLEMENT_WRAPPERTYPEINFO(gpu, FlutterGpuTestClass); + +// TODO(131346): Remove this once we migrate the Dart GPU API into this space. +FlutterGpuTestClass::~FlutterGpuTestClass() = default; + +} // namespace flutter + +// TODO(131346): Remove this once we migrate the Dart GPU API into this space. +uint32_t InternalFlutterGpuTestProc() { + return 1; +} + +// TODO(131346): Remove this once we migrate the Dart GPU API into this space. +Dart_Handle InternalFlutterGpuTestProcWithCallback(Dart_Handle callback) { + flutter::UIDartState::ThrowIfUIOperationsProhibited(); + if (!Dart_IsClosure(callback)) { + return tonic::ToDart("Callback must be a function"); + } + + tonic::DartInvoke(callback, {tonic::ToDart(1234)}); + + return Dart_Null(); +} + +// TODO(131346): Remove this once we migrate the Dart GPU API into this space. +void InternalFlutterGpuTestClass_Create(Dart_Handle wrapper) { + auto res = fml::MakeRefCounted(); + res->AssociateWithDartWrapper(wrapper); + FML_LOG(INFO) << "FlutterGpuTestClass Wrapped."; +} + +// TODO(131346): Remove this once we migrate the Dart GPU API into this space. +void InternalFlutterGpuTestClass_Method(flutter::FlutterGpuTestClass* self, + int something) { + FML_LOG(INFO) << "Something: " << something; +} + +// TODO(131346): Remove this once we migrate the Dart GPU API into this space. +void FlutterGpuTestClass_Dispose(flutter::FlutterGpuTestClass* self, + int something) { + self->ClearDartWrapper(); +} diff --git a/engine/src/flutter/lib/gpu/gpu.dart b/engine/src/flutter/lib/gpu/gpu.dart new file mode 100644 index 0000000000..039d11585e --- /dev/null +++ b/engine/src/flutter/lib/gpu/gpu.dart @@ -0,0 +1,39 @@ +// 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. + +// ignore_for_file: public_member_api_docs + +import 'dart:ffi'; +import 'dart:nativewrappers'; + +/// This is a simple test fuction. +@Native(symbol: 'InternalFlutterGpuTestProc') +external int testProc(); + +/// This is a test callback that follows the same pattern as much of dart:ui -- +/// immediately returning an error string and supplying an asynchronous result +/// via callback later. +typedef Callback = void Function(T result); +@Native( + symbol: 'InternalFlutterGpuTestProcWithCallback') +external String? testProcWithCallback(Callback callback); + +/// This is a test of NativeFieldWrapperClass1, which is commonly used in +/// dart:ui to enable Dart to dictate the lifetime of a C counterpart. +base class FlutterGpuTestClass extends NativeFieldWrapperClass1 { + FlutterGpuTestClass() { + _constructor(); + } + + /// This "constructor" is used to instantiate and wrap the C counterpart. + /// This is a common pattern in dart:ui. + @Native(symbol: 'InternalFlutterGpuTestClass_Create') + external void _constructor(); + + /// This is a method that will supply a pointer to the C data counterpart when + /// calling the gunction + @Native, Int)>( + symbol: 'InternalFlutterGpuTestClass_Method') + external void coolMethod(int something); +} diff --git a/engine/src/flutter/lib/gpu/gpu.h b/engine/src/flutter/lib/gpu/gpu.h new file mode 100644 index 0000000000..0007845a34 --- /dev/null +++ b/engine/src/flutter/lib/gpu/gpu.h @@ -0,0 +1,57 @@ +// 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_LIB_GPU_GPU_H_ +#define FLUTTER_LIB_GPU_GPU_H_ + +#include + +#include "flutter/lib/ui/dart_wrapper.h" +#include "third_party/dart/runtime/include/dart_api.h" +#include "third_party/tonic/dart_state.h" +#include "third_party/tonic/dart_wrapper_info.h" + +#if FML_OS_WIN +#define FLUTTER_EXPORT __declspec(dllexport) +#else // FML_OS_WIN +#define FLUTTER_EXPORT __attribute__((visibility("default"))) +#endif // FML_OS_WIN + +namespace flutter { + +// TODO(131346): Remove this once we migrate the Dart GPU API into this space. +class FlutterGpuTestClass + : public RefCountedDartWrappable { + DEFINE_WRAPPERTYPEINFO(); + FML_FRIEND_MAKE_REF_COUNTED(FlutterGpuTestClass); + + public: + ~FlutterGpuTestClass() override; +}; + +} // namespace flutter + +extern "C" { + +// TODO(131346): Remove this once we migrate the Dart GPU API into this space. +FLUTTER_EXPORT +extern uint32_t InternalFlutterGpuTestProc(); + +// TODO(131346): Remove this once we migrate the Dart GPU API into this space. +FLUTTER_EXPORT +extern Dart_Handle InternalFlutterGpuTestProcWithCallback(Dart_Handle callback); + +// TODO(131346): Remove this once we migrate the Dart GPU API into this space. +FLUTTER_EXPORT +extern void InternalFlutterGpuTestClass_Create(Dart_Handle wrapper); + +// TODO(131346): Remove this once we migrate the Dart GPU API into this space. +FLUTTER_EXPORT +extern void InternalFlutterGpuTestClass_Method( + flutter::FlutterGpuTestClass* self, + int something); + +} // extern "C" + +#endif // FLUTTER_LIB_GPU_GPU_H_ diff --git a/engine/src/flutter/runtime/BUILD.gn b/engine/src/flutter/runtime/BUILD.gn index 70e9172d51..8d07eb8e1d 100644 --- a/engine/src/flutter/runtime/BUILD.gn +++ b/engine/src/flutter/runtime/BUILD.gn @@ -90,6 +90,7 @@ source_set("runtime") { } public_deps = [ + "//flutter/lib/gpu", "//flutter/lib/ui", "//third_party/rapidjson", ] diff --git a/engine/src/flutter/testing/dart/BUILD.gn b/engine/src/flutter/testing/dart/BUILD.gn index 1c372c5ed9..2e1bb1ad38 100644 --- a/engine/src/flutter/testing/dart/BUILD.gn +++ b/engine/src/flutter/testing/dart/BUILD.gn @@ -17,6 +17,7 @@ tests = [ "fragment_shader_test.dart", "geometry_test.dart", "gesture_settings_test.dart", + "gpu_test.dart", "gradient_test.dart", "http_allow_http_connections_test.dart", "http_disallow_http_connections_test.dart", diff --git a/engine/src/flutter/testing/dart/gpu_test.dart b/engine/src/flutter/testing/dart/gpu_test.dart new file mode 100644 index 0000000000..e6fab02478 --- /dev/null +++ b/engine/src/flutter/testing/dart/gpu_test.dart @@ -0,0 +1,24 @@ +// 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. + +// ignore_for_file: avoid_relative_lib_imports + +import 'package:litetest/litetest.dart'; +import '../../lib/gpu/gpu.dart' as gpu; + +void main() { + // TODO(131346): Remove this once we migrate the Dart GPU API into this space. + test('no crash', () async { + final int result = gpu.testProc(); + expect(result, 1); + + final String? message = gpu.testProcWithCallback((int result) { + expect(result, 1234); + }); + expect(message, null); + + final gpu.FlutterGpuTestClass a = gpu.FlutterGpuTestClass(); + a.coolMethod(9847); + }); +} diff --git a/engine/src/flutter/testing/symbols/verify_exported.dart b/engine/src/flutter/testing/symbols/verify_exported.dart index 2e97f3e755..22e9736aff 100644 --- a/engine/src/flutter/testing/symbols/verify_exported.dart +++ b/engine/src/flutter/testing/symbols/verify_exported.dart @@ -94,9 +94,12 @@ int _checkIos(String outPath, String nmPath, Iterable builds) { continue; } final Iterable unexpectedEntries = NmEntry.parse(nmResult.stdout as String).where((NmEntry entry) { - return !(((entry.type == '(__DATA,__common)' || entry.type == '(__DATA,__const)') && entry.name.startsWith('_Flutter')) - || (entry.type == '(__DATA,__objc_data)' - && (entry.name.startsWith(r'_OBJC_METACLASS_$_Flutter') || entry.name.startsWith(r'_OBJC_CLASS_$_Flutter')))); + final bool cSymbol = (entry.type == '(__DATA,__common)' || entry.type == '(__DATA,__const)') + && entry.name.startsWith('_Flutter'); + final bool cInternalSymbol = entry.type == '(__TEXT,__text)' && entry.name.startsWith('_InternalFlutter'); + final bool objcSymbol = entry.type == '(__DATA,__objc_data)' + && (entry.name.startsWith(r'_OBJC_METACLASS_$_Flutter') || entry.name.startsWith(r'_OBJC_CLASS_$_Flutter')); + return !(cSymbol || cInternalSymbol || objcSymbol); }); if (unexpectedEntries.isNotEmpty) { print('ERROR: $libFlutter exports unexpected symbols:'); @@ -275,7 +278,9 @@ int _checkLinux(String outPath, String nmPath, Iterable builds) { } if (!(entry.name.startsWith('Flutter') || entry.name.startsWith('__Flutter') - || entry.name.startsWith('kFlutter'))) { + || entry.name.startsWith('kFlutter') + || entry.name.startsWith('InternalFlutter') + || entry.name.startsWith('kInternalFlutter'))) { print('ERROR: $libFlutter exports an unexpected symbol name: ($entry)'); print(' Library has $entries.'); failures++;