Skip to content

Commit

Permalink
[Impeller] Flutter GPU: Add GpuContext. (flutter#44359)
Browse files Browse the repository at this point in the history
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 flutter/flutter#127712).
  • Loading branch information
bdero authored Aug 7, 2023
1 parent 4505525 commit 6ec3566
Show file tree
Hide file tree
Showing 18 changed files with 214 additions and 114 deletions.
2 changes: 2 additions & 0 deletions ci/licenses_golden/excluded_files
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 18 additions & 10 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
14 changes: 10 additions & 4 deletions lib/gpu/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions lib/gpu/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -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
47 changes: 24 additions & 23 deletions lib/ui/gpu/context.cc → lib/gpu/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
40 changes: 40 additions & 0 deletions lib/gpu/context.h
Original file line number Diff line number Diff line change
@@ -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"
11 changes: 11 additions & 0 deletions lib/gpu/export.cc
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions lib/gpu/export.h
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions lib/gpu/lib/gpu.dart
Original file line number Diff line number Diff line change
@@ -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';
28 changes: 28 additions & 0 deletions lib/gpu/lib/src/context.dart
Original file line number Diff line number Diff line change
@@ -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();
7 changes: 4 additions & 3 deletions lib/gpu/gpu.dart → lib/gpu/lib/src/smoketest.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@
// 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<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);

/// 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();
}
Expand Down
14 changes: 14 additions & 0 deletions lib/gpu/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
12 changes: 5 additions & 7 deletions lib/gpu/gpu.cc → lib/gpu/smoketest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
Loading

0 comments on commit 6ec3566

Please sign in to comment.