[Impeller] Flutter GPU: Add GpuContext. (flutter/engine#44359)

Move the GpuContext to its new home. I added a `flutter_tester` test that just verifies an exception is gracefully thrown when Impeller isn't available.

In a later patch, I'll land a way to eagerly supply the Impeller context on the cpp side to enable testing through the playground (as mentioned in https://github.com/flutter/flutter/issues/127712).
This commit is contained in:
Brandon DeRosier
2023-08-07 12:41:57 -07:00
committed by GitHub
parent 0cc053b836
commit e8e4f8c118
18 changed files with 214 additions and 114 deletions

View File

@@ -174,6 +174,8 @@
../../../flutter/impeller/tools/malioc_diff.py
../../../flutter/impeller/tools/xxd.py
../../../flutter/impeller/typographer/typographer_unittests.cc
../../../flutter/lib/gpu/analysis_options.yaml
../../../flutter/lib/gpu/pubspec.yaml
../../../flutter/lib/snapshot/libraries.json
../../../flutter/lib/snapshot/libraries.yaml
../../../flutter/lib/snapshot/pubspec.yaml

View File

@@ -1725,9 +1725,15 @@ 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/gpu/context.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/gpu/context.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/gpu/export.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/gpu/export.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/gpu/lib/gpu.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/gpu/lib/src/context.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/gpu/lib/src/smoketest.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/gpu/smoketest.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/gpu/smoketest.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
@@ -1749,8 +1755,6 @@ ORIGIN: ../../../flutter/lib/ui/experiments/setup_hooks.dart + ../../../flutter/
ORIGIN: ../../../flutter/lib/ui/experiments/ui.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/ui/floating_point.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/ui/geometry.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/ui/gpu/context.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/ui/gpu/context.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/ui/hash_codes.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/ui/hooks.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/ui/io_manager.cc + ../../../flutter/LICENSE
@@ -4445,9 +4449,15 @@ 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/gpu/context.cc
FILE: ../../../flutter/lib/gpu/context.h
FILE: ../../../flutter/lib/gpu/export.cc
FILE: ../../../flutter/lib/gpu/export.h
FILE: ../../../flutter/lib/gpu/lib/gpu.dart
FILE: ../../../flutter/lib/gpu/lib/src/context.dart
FILE: ../../../flutter/lib/gpu/lib/src/smoketest.dart
FILE: ../../../flutter/lib/gpu/smoketest.cc
FILE: ../../../flutter/lib/gpu/smoketest.h
FILE: ../../../flutter/lib/io/dart_io.cc
FILE: ../../../flutter/lib/io/dart_io.h
FILE: ../../../flutter/lib/snapshot/libraries_experimental.json
@@ -4470,8 +4480,6 @@ FILE: ../../../flutter/lib/ui/experiments/setup_hooks.dart
FILE: ../../../flutter/lib/ui/experiments/ui.dart
FILE: ../../../flutter/lib/ui/floating_point.h
FILE: ../../../flutter/lib/ui/geometry.dart
FILE: ../../../flutter/lib/ui/gpu/context.cc
FILE: ../../../flutter/lib/ui/gpu/context.h
FILE: ../../../flutter/lib/ui/hash_codes.dart
FILE: ../../../flutter/lib/ui/hooks.dart
FILE: ../../../flutter/lib/ui/io_manager.cc

View File

@@ -30,10 +30,16 @@ source_set("gpu") {
defines = []
}
sources = [
"gpu.cc",
"gpu.h",
]
if (!is_fuchsia) {
sources = [
"context.cc",
"context.h",
"export.cc",
"export.h",
"smoketest.cc",
"smoketest.h",
]
}
deps = [
"//flutter/impeller",
"//flutter/impeller/display_list:skia_conversions",

View File

@@ -0,0 +1,7 @@
# 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.
linter:
rules:
- public_member_api_docs

View File

@@ -2,27 +2,35 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/lib/ui/gpu/context.h"
#include "flutter/lib/gpu/context.h"
#include <memory>
#include <sstream>
#include <future>
#include "flutter/fml/log_level.h"
#include "flutter/fml/logging.h"
#include "flutter/fml/make_copyable.h"
#include "flutter/fml/memory/ref_ptr.h"
#include "dart_api.h"
#include "flutter/lib/ui/ui_dart_state.h"
#include "third_party/tonic/dart_wrappable.h"
#include "fml/make_copyable.h"
#include "tonic/converter/dart_converter.h"
namespace flutter {
IMPLEMENT_WRAPPERTYPEINFO(ui, GpuContext);
IMPLEMENT_WRAPPERTYPEINFO(gpu, Context);
std::string GpuContext::InitializeDefault(Dart_Handle wrapper) {
auto dart_state = UIDartState::Current();
Context::Context(std::shared_ptr<impeller::Context> context)
: context_(std::move(context)) {}
Context::~Context() = default;
} // namespace flutter
//----------------------------------------------------------------------------
/// Exports
///
Dart_Handle InternalFlutterGpu_Context_InitializeDefault(Dart_Handle wrapper) {
auto dart_state = flutter::UIDartState::Current();
if (!dart_state->IsImpellerEnabled()) {
return "The GpuContext API requires the Impeller rendering backend to be "
"enabled.";
return tonic::ToDart(
"Flutter GPU requires the Impeller rendering backend to be enabled.");
}
// Grab the Impeller context from the IO manager.
@@ -38,17 +46,10 @@ std::string GpuContext::InitializeDefault(Dart_Handle wrapper) {
auto impeller_context = impeller_context_future.get();
if (!impeller_context) {
return "Unable to retrieve the Impeller context.";
return tonic::ToDart("Unable to retrieve the Impeller context.");
}
auto res = fml::MakeRefCounted<GpuContext>(impeller_context);
auto res = fml::MakeRefCounted<flutter::Context>(impeller_context);
res->AssociateWithDartWrapper(wrapper);
return "";
return Dart_Null();
}
GpuContext::GpuContext(std::shared_ptr<impeller::Context> context)
: context_(std::move(context)) {}
GpuContext::~GpuContext() = default;
} // namespace flutter

View File

@@ -0,0 +1,40 @@
// 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.
#pragma once
#include "dart_api.h"
#include "flutter/lib/gpu/export.h"
#include "flutter/lib/ui/dart_wrapper.h"
#include "impeller/renderer/context.h"
namespace flutter {
class Context : public RefCountedDartWrappable<Context> {
DEFINE_WRAPPERTYPEINFO();
FML_FRIEND_MAKE_REF_COUNTED(Context);
public:
explicit Context(std::shared_ptr<impeller::Context> context);
~Context() override;
private:
std::shared_ptr<impeller::Context> context_;
FML_DISALLOW_COPY_AND_ASSIGN(Context);
};
} // namespace flutter
//----------------------------------------------------------------------------
/// Exports
///
extern "C" {
FLUTTER_GPU_EXPORT
extern Dart_Handle InternalFlutterGpu_Context_InitializeDefault(
Dart_Handle wrapper);
} // extern "C"

View File

@@ -0,0 +1,11 @@
// 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/export.h"
namespace flutter {
//
} // namespace flutter

View File

@@ -0,0 +1,11 @@
// 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.
#pragma once
#if FML_OS_WIN
#define FLUTTER_GPU_EXPORT __declspec(dllexport)
#else // FML_OS_WIN
#define FLUTTER_GPU_EXPORT __attribute__((visibility("default")))
#endif // FML_OS_WIN

View File

@@ -0,0 +1,15 @@
// 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.
/// The Flutter GPU library.
///
/// To use, import `package:flutter_gpu/gpu.dart`.
///
/// See also:
///
/// * [Flutter GPU Wiki page](https://github.com/flutter/flutter/wiki/Flutter-GPU).
library flutter_gpu;
export 'src/context.dart';
export 'src/smoketest.dart';

View File

@@ -0,0 +1,28 @@
// 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 'dart:ffi';
import 'dart:nativewrappers';
/// A handle to a graphics context. Used to create and manage GPU resources.
///
/// To obtain the default graphics context, use [getContext].
class GpuContext extends NativeFieldWrapperClass1 {
/// Creates a new graphics context that corresponds to the default Impeller
/// context.
GpuContext._createDefault() {
final String? error = _initializeDefault();
if (error != null) {
throw Exception(error);
}
}
/// Associates the default Impeller context with this Context.
@Native<Handle Function(Handle)>(
symbol: 'InternalFlutterGpu_Context_InitializeDefault')
external String? _initializeDefault();
}
/// The default graphics context.
final GpuContext gpuContext = GpuContext._createDefault();

View File

@@ -2,8 +2,6 @@
// 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';
@@ -11,10 +9,12 @@ import 'dart:nativewrappers';
@Native<Int32 Function()>(symbol: 'InternalFlutterGpuTestProc')
external int testProc();
/// A single parameter callback.
typedef Callback<T> = void Function(T result);
/// 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<T> = void Function(T result);
@Native<Handle Function(Handle)>(
symbol: 'InternalFlutterGpuTestProcWithCallback')
external String? testProcWithCallback(Callback<int> callback);
@@ -22,6 +22,7 @@ external String? testProcWithCallback(Callback<int> 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 {
/// Default constructor for the test class
FlutterGpuTestClass() {
_constructor();
}

View File

@@ -0,0 +1,14 @@
# 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.
name: flutter_gpu
description: A framework for writing Flutter applications
homepage: https://flutter.dev
environment:
sdk: '>=3.0.0-0 <4.0.0'
dependencies:
sky_engine:
sdk: flutter

View File

@@ -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/lib/gpu/gpu.h"
#include "flutter/lib/gpu/smoketest.h"
#include "flutter/fml/memory/ref_ptr.h"
#include "flutter/lib/ui/dart_wrapper.h"
#include "flutter/lib/ui/ui_dart_state.h"
@@ -23,6 +23,10 @@ FlutterGpuTestClass::~FlutterGpuTestClass() = default;
} // namespace flutter
//----------------------------------------------------------------------------
/// Exports
///
// TODO(131346): Remove this once we migrate the Dart GPU API into this space.
uint32_t InternalFlutterGpuTestProc() {
return 1;
@@ -52,9 +56,3 @@ 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();
}

View File

@@ -2,22 +2,16 @@
// 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_
#pragma once
#include <cstdint>
#include "flutter/lib/gpu/export.h"
#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.
@@ -32,26 +26,28 @@ class FlutterGpuTestClass
} // namespace flutter
//----------------------------------------------------------------------------
/// Exports
///
extern "C" {
// TODO(131346): Remove this once we migrate the Dart GPU API into this space.
FLUTTER_EXPORT
FLUTTER_GPU_EXPORT
extern uint32_t InternalFlutterGpuTestProc();
// TODO(131346): Remove this once we migrate the Dart GPU API into this space.
FLUTTER_EXPORT
FLUTTER_GPU_EXPORT
extern Dart_Handle InternalFlutterGpuTestProcWithCallback(Dart_Handle callback);
// TODO(131346): Remove this once we migrate the Dart GPU API into this space.
FLUTTER_EXPORT
FLUTTER_GPU_EXPORT
extern void InternalFlutterGpuTestClass_Create(Dart_Handle wrapper);
// TODO(131346): Remove this once we migrate the Dart GPU API into this space.
FLUTTER_EXPORT
FLUTTER_GPU_EXPORT
extern void InternalFlutterGpuTestClass_Method(
flutter::FlutterGpuTestClass* self,
int something);
} // extern "C"
#endif // FLUTTER_LIB_GPU_GPU_H_

View File

@@ -216,12 +216,6 @@ source_set("ui") {
"painting/scene/scene_shader.h",
]
# Experimental "Impeller Dart" API.
sources += [
"gpu/context.cc",
"gpu/context.h",
]
deps += [ "//flutter/impeller/scene" ]
}
}

View File

@@ -12,7 +12,6 @@
#include "flutter/lib/ui/compositing/scene.h"
#include "flutter/lib/ui/compositing/scene_builder.h"
#include "flutter/lib/ui/dart_runtime_hooks.h"
#include "flutter/lib/ui/gpu/context.h"
#include "flutter/lib/ui/isolate_name_server/isolate_name_server_natives.h"
#include "flutter/lib/ui/painting/canvas.h"
#include "flutter/lib/ui/painting/codec.h"
@@ -316,10 +315,6 @@ typedef CanvasPath Path;
V(SceneShader, SetCameraTransform, 2) \
V(SceneShader, Dispose, 1)
#define FFI_FUNCTION_LIST_GPU(V) V(GpuContext::InitializeDefault, 1)
#define FFI_METHOD_LIST_GPU(V)
#endif // IMPELLER_ENABLE_3D
#define FFI_FUNCTION_INSERT(FUNCTION, ARGS) \
@@ -352,9 +347,6 @@ void InitDispatcherMap() {
#ifdef IMPELLER_ENABLE_3D
FFI_FUNCTION_LIST_3D(FFI_FUNCTION_INSERT)
FFI_METHOD_LIST_3D(FFI_METHOD_INSERT)
FFI_FUNCTION_LIST_GPU(FFI_FUNCTION_INSERT)
FFI_METHOD_LIST_GPU(FFI_METHOD_INSERT)
#endif // IMPELLER_ENABLE_3D
}

View File

@@ -1,37 +0,0 @@
// 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_UI_GPU_H_
#define FLUTTER_LIB_UI_GPU_H_
#include <memory>
#include "flutter/fml/macros.h"
#include "flutter/fml/memory/ref_counted.h"
#include "flutter/lib/ui/dart_wrapper.h"
#include "impeller/renderer/context.h"
#include "third_party/tonic/dart_library_natives.h"
#include "third_party/tonic/dart_wrappable.h"
namespace flutter {
class GpuContext : public RefCountedDartWrappable<GpuContext> {
DEFINE_WRAPPERTYPEINFO();
FML_FRIEND_MAKE_REF_COUNTED(GpuContext);
public:
explicit GpuContext(std::shared_ptr<impeller::Context> context);
~GpuContext() override;
static std::string InitializeDefault(Dart_Handle wrapper);
private:
std::shared_ptr<impeller::Context> context_;
FML_DISALLOW_COPY_AND_ASSIGN(GpuContext);
};
} // namespace flutter
#endif // FLUTTER_LIB_UI_GPU_H_

View File

@@ -5,11 +5,11 @@
// ignore_for_file: avoid_relative_lib_imports
import 'package:litetest/litetest.dart';
import '../../lib/gpu/gpu.dart' as gpu;
import '../../lib/gpu/lib/gpu.dart' as gpu;
void main() {
// TODO(131346): Remove this once we migrate the Dart GPU API into this space.
test('no crash', () async {
test('smoketest', () async {
final int result = gpu.testProc();
expect(result, 1);
@@ -21,4 +21,17 @@ void main() {
final gpu.FlutterGpuTestClass a = gpu.FlutterGpuTestClass();
a.coolMethod(9847);
});
test('gpu.context throws exception for incompatible embedders', () async {
try {
// ignore: unnecessary_statements
gpu.gpuContext; // Force the
fail('Exception not thrown');
} catch (e) {
expect(
e.toString(),
contains(
'Flutter GPU requires the Impeller rendering backend to be enabled.'));
}
});
}