Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit e1226fe

Browse files
committed
Add Impeller context
1 parent be34e69 commit e1226fe

File tree

6 files changed

+73
-21
lines changed

6 files changed

+73
-21
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,6 +1689,8 @@ ORIGIN: ../../../flutter/lib/ui/experiments/setup_hooks.dart + ../../../flutter/
16891689
ORIGIN: ../../../flutter/lib/ui/experiments/ui.dart + ../../../flutter/LICENSE
16901690
ORIGIN: ../../../flutter/lib/ui/floating_point.h + ../../../flutter/LICENSE
16911691
ORIGIN: ../../../flutter/lib/ui/geometry.dart + ../../../flutter/LICENSE
1692+
ORIGIN: ../../../flutter/lib/ui/gpu/context.cc + ../../../flutter/LICENSE
1693+
ORIGIN: ../../../flutter/lib/ui/gpu/context.h + ../../../flutter/LICENSE
16921694
ORIGIN: ../../../flutter/lib/ui/hash_codes.dart + ../../../flutter/LICENSE
16931695
ORIGIN: ../../../flutter/lib/ui/hooks.dart + ../../../flutter/LICENSE
16941696
ORIGIN: ../../../flutter/lib/ui/io_manager.cc + ../../../flutter/LICENSE
@@ -4311,6 +4313,8 @@ FILE: ../../../flutter/lib/ui/experiments/setup_hooks.dart
43114313
FILE: ../../../flutter/lib/ui/experiments/ui.dart
43124314
FILE: ../../../flutter/lib/ui/floating_point.h
43134315
FILE: ../../../flutter/lib/ui/geometry.dart
4316+
FILE: ../../../flutter/lib/ui/gpu/context.cc
4317+
FILE: ../../../flutter/lib/ui/gpu/context.h
43144318
FILE: ../../../flutter/lib/ui/hash_codes.dart
43154319
FILE: ../../../flutter/lib/ui/hooks.dart
43164320
FILE: ../../../flutter/lib/ui/io_manager.cc

lib/ui/BUILD.gn

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ source_set("ui") {
216216
"painting/scene/scene_shader.h",
217217
]
218218

219+
# Experimental "Impeller Dart" API.
220+
sources += [
221+
"gpu/context.cc",
222+
"gpu/context.h",
223+
]
224+
219225
deps += [ "//flutter/impeller/scene" ]
220226
}
221227
}

lib/ui/dart_ui.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "flutter/lib/ui/compositing/scene.h"
1313
#include "flutter/lib/ui/compositing/scene_builder.h"
1414
#include "flutter/lib/ui/dart_runtime_hooks.h"
15+
#include "flutter/lib/ui/gpu/context.h"
1516
#include "flutter/lib/ui/isolate_name_server/isolate_name_server_natives.h"
1617
#include "flutter/lib/ui/painting/canvas.h"
1718
#include "flutter/lib/ui/painting/codec.h"
@@ -315,8 +316,7 @@ typedef CanvasPath Path;
315316
V(SceneShader, SetCameraTransform, 2) \
316317
V(SceneShader, Dispose, 1)
317318

318-
#define FFI_FUNCTION_LIST_GPU(V) \
319-
V(GpuContext::InitializeDefault, 1)
319+
#define FFI_FUNCTION_LIST_GPU(V) V(GpuContext::InitializeDefault, 1)
320320

321321
#define FFI_METHOD_LIST_GPU(V)
322322

@@ -348,9 +348,13 @@ void* ResolveFfiNativeFunction(const char* name, uintptr_t args) {
348348
void InitDispatcherMap() {
349349
FFI_FUNCTION_LIST(FFI_FUNCTION_INSERT)
350350
FFI_METHOD_LIST(FFI_METHOD_INSERT)
351+
351352
#ifdef IMPELLER_ENABLE_3D
352353
FFI_FUNCTION_LIST_3D(FFI_FUNCTION_INSERT)
353354
FFI_METHOD_LIST_3D(FFI_METHOD_INSERT)
355+
356+
FFI_FUNCTION_LIST_GPU(FFI_FUNCTION_INSERT)
357+
FFI_METHOD_LIST_GPU(FFI_METHOD_INSERT)
354358
#endif // IMPELLER_ENABLE_3D
355359
}
356360

lib/ui/experiments/gpu.dart

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66

77
part of dart.ui;
88

9+
class GpuContextException implements Exception {
10+
GpuContextException(this.message);
11+
String message;
12+
13+
@override
14+
String toString() {
15+
return 'GpuContextException: $message';
16+
}
17+
}
18+
919
enum BlendOperation { add, subtract, reverseSubtract }
1020

1121
enum BlendFactor {
@@ -141,7 +151,10 @@ class GpuContext extends NativeFieldWrapperClass1 {
141151
/// Creates a new graphics context that corresponds to the default Impeller
142152
/// context.
143153
GpuContext._createDefault() {
144-
_initializeDefault();
154+
final String error = _initializeDefault();
155+
if (error.isNotEmpty) {
156+
throw GpuContextException(error);
157+
}
145158
}
146159

147160
//registerShaderLibrary() async
@@ -157,9 +170,9 @@ class GpuContext extends NativeFieldWrapperClass1 {
157170
return RasterPipeline();
158171
}
159172

160-
/// Links this GpuContext to the default Impeller context.
161-
@Native<Void Function(Handle)>(symbol: 'GpuContext::InitializeDefault')
162-
external void _initializeDefault();
173+
/// Associates the default Impeller context with this GpuContext.
174+
@Native<Handle Function(Handle)>(symbol: 'GpuContext::InitializeDefault')
175+
external String _initializeDefault();
163176
}
164177

165178
GpuContext? _defaultGpuContext;

lib/ui/gpu/context.cc

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,47 @@
77
#include <memory>
88
#include <sstream>
99

10+
#include "flutter/fml/log_level.h"
11+
#include "flutter/fml/logging.h"
12+
#include "flutter/fml/make_copyable.h"
13+
#include "flutter/fml/memory/ref_ptr.h"
14+
#include "flutter/lib/ui/ui_dart_state.h"
15+
#include "third_party/tonic/dart_wrappable.h"
16+
1017
namespace flutter {
1118

1219
IMPLEMENT_WRAPPERTYPEINFO(ui, GpuContext);
1320

14-
void GpuContext::InitializeDefault(Dart_Handle wrapper) {
15-
auto res = fml::MakeRefCounted<SceneNode>();
21+
std::string GpuContext::InitializeDefault(Dart_Handle wrapper) {
22+
auto dart_state = UIDartState::Current();
23+
if (!dart_state->IsImpellerEnabled()) {
24+
return "The GpuContext API requires the Impeller rendering backend to be "
25+
"enabled.";
26+
}
27+
28+
// Grab the Impeller context from the IO manager.
29+
30+
std::promise<std::shared_ptr<impeller::Context>> context_promise;
31+
auto impeller_context_future = context_promise.get_future();
32+
dart_state->GetTaskRunners().GetIOTaskRunner()->PostTask(
33+
fml::MakeCopyable([promise = std::move(context_promise),
34+
io_manager = dart_state->GetIOManager()]() mutable {
35+
promise.set_value(io_manager ? io_manager->GetImpellerContext()
36+
: nullptr);
37+
}));
38+
39+
auto impeller_context = impeller_context_future.get();
40+
if (!impeller_context) {
41+
return "Unable to retrieve the Impeller context.";
42+
}
43+
auto res = fml::MakeRefCounted<GpuContext>(impeller_context);
1644
res->AssociateWithDartWrapper(wrapper);
45+
46+
return "";
1747
}
1848

19-
GpuContext::GpuContext() = default;
49+
GpuContext::GpuContext(std::shared_ptr<impeller::Context> context)
50+
: context_(std::move(context)) {}
2051

2152
GpuContext::~GpuContext() = default;
2253

lib/ui/gpu/context.h

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,23 @@
77

88
#include <memory>
99

10-
#include "impeller/context.h"
11-
10+
#include "flutter/fml/memory/ref_counted.h"
11+
#include "flutter/lib/ui/dart_wrapper.h"
12+
#include "impeller/renderer/context.h"
1213
#include "third_party/tonic/dart_library_natives.h"
14+
#include "third_party/tonic/dart_wrappable.h"
1315

1416
namespace flutter {
1517

16-
/// @brief A scene node, which may be a deserialized ipscene asset. This node
17-
/// can be safely added as a child to multiple scene nodes, whether
18-
/// they're in the same scene or a different scene. The deserialized
19-
/// node itself is treated as immutable on the IO thread.
20-
///
21-
/// Internally, nodes may have an animation player, which is controlled
22-
/// via the mutation log in the `DlSceneColorSource`, which is built by
23-
/// `SceneShader`.
2418
class GpuContext : public RefCountedDartWrappable<GpuContext> {
2519
DEFINE_WRAPPERTYPEINFO();
2620
FML_FRIEND_MAKE_REF_COUNTED(GpuContext);
2721

2822
public:
29-
GpuContext(std::shared_ptr<impeller::Context> context);
23+
explicit GpuContext(std::shared_ptr<impeller::Context> context);
3024
~GpuContext() override;
3125

32-
static void InitializeDefault(Dart_Handle wrapper);
26+
static std::string InitializeDefault(Dart_Handle wrapper);
3327

3428
private:
3529
std::shared_ptr<impeller::Context> context_;

0 commit comments

Comments
 (0)