Skip to content

Commit

Permalink
EngineLayer::dispose
Browse files Browse the repository at this point in the history
  • Loading branch information
dnfield committed May 18, 2021
1 parent 2029cba commit e1cad62
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 8 deletions.
5 changes: 5 additions & 0 deletions lib/ui/compositing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ abstract class _EngineLayerWrapper implements EngineLayer {

EngineLayer _nativeLayer;

@override
void dispose() {
_nativeLayer.dispose();
}

// Children of this layer.
//
// Null if this layer has no children. This field is populated only in debug
Expand Down
1 change: 1 addition & 0 deletions lib/ui/compositing/scene.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Scene::Scene(std::shared_ptr<flutter::Layer> rootLayer,
Scene::~Scene() {}

void Scene::dispose() {
layer_tree_.reset();
ClearDartWrapper();
}

Expand Down
7 changes: 4 additions & 3 deletions lib/ui/compositing/scene_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,10 @@ void SceneBuilder::setCheckerboardOffscreenLayers(bool checkerboard) {
void SceneBuilder::build(Dart_Handle scene_handle) {
FML_DCHECK(layer_stack_.size() >= 1);

Scene::create(scene_handle, layer_stack_[0], rasterizer_tracing_threshold_,
checkerboard_raster_cache_images_,
checkerboard_offscreen_layers_);
Scene::create(
scene_handle, std::move(layer_stack_[0]), rasterizer_tracing_threshold_,
checkerboard_raster_cache_images_, checkerboard_offscreen_layers_);
layer_stack_.clear();
ClearDartWrapper(); // may delete this object.
}

Expand Down
4 changes: 4 additions & 0 deletions lib/ui/painting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2233,6 +2233,10 @@ class EngineLayer extends NativeFieldWrapperClass2 {
/// or extended directly.
@pragma('vm:entry-point')
EngineLayer._();

/// Release the resources used by this object. The object is no longer usable
/// after this method is called.
void dispose() native 'EngineLayer_dispose';
}

/// A complex, one-dimensional subset of a plane.
Expand Down
19 changes: 14 additions & 5 deletions lib/ui/painting/engine_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "flutter/lib/ui/painting/engine_layer.h"

#include "flutter/lib/ui/ui_dart_state.h"
#include "third_party/tonic/converter/dart_converter.h"
#include "third_party/tonic/dart_args.h"
#include "third_party/tonic/dart_binding_macros.h"
Expand All @@ -13,22 +14,30 @@ using tonic::ToDart;

namespace flutter {

IMPLEMENT_WRAPPERTYPEINFO(ui, EngineLayer);

#define FOR_EACH_BINDING(V) V(EngineLayer, dispose)

DART_BIND_ALL(EngineLayer, FOR_EACH_BINDING)

EngineLayer::EngineLayer(std::shared_ptr<flutter::ContainerLayer> layer)
: layer_(layer) {}

EngineLayer::~EngineLayer() = default;

size_t EngineLayer::GetAllocationSize() const {
// TODO(dnfield): Remove this when scene disposal changes land in the
// framework. https://github.com/flutter/flutter/issues/81514

// Provide an approximation of the total memory impact of this object to the
// Dart GC. The ContainerLayer may hold references to a tree of other layers,
// which in turn may contain Skia objects.
return 3000;
};

IMPLEMENT_WRAPPERTYPEINFO(ui, EngineLayer);

#define FOR_EACH_BINDING(V) // nothing to bind

DART_BIND_ALL(EngineLayer, FOR_EACH_BINDING)
void EngineLayer::dispose() {
layer_.reset();
ClearDartWrapper();
}

} // namespace flutter
2 changes: 2 additions & 0 deletions lib/ui/painting/engine_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class EngineLayer : public RefCountedDartWrappable<EngineLayer> {

static void RegisterNatives(tonic::DartLibraryNatives* natives);

void dispose();

std::shared_ptr<flutter::ContainerLayer> Layer() const { return layer_; }

private:
Expand Down
2 changes: 2 additions & 0 deletions lib/ui/painting/image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Dart_Handle CanvasImage::toByteData(int format, Dart_Handle callback) {
}

void CanvasImage::dispose() {
// TODO(dnfield): Remove the hint freed delegate once Picture disposal is in
// the framework https://github.com/flutter/flutter/issues/81514
auto hint_freed_delegate = UIDartState::Current()->GetHintFreedDelegate();
if (hint_freed_delegate) {
hint_freed_delegate->HintFreed(GetAllocationSize());
Expand Down

0 comments on commit e1cad62

Please sign in to comment.