|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:js_interop'; |
| 6 | +import 'package:meta/meta.dart'; |
| 7 | + |
| 8 | +import '../../engine.dart' show Instrumentation; |
| 9 | +import '../util.dart'; |
| 10 | +import 'canvaskit_api.dart'; |
| 11 | + |
| 12 | +/// Collects native objects that weren't explicitly disposed of using |
| 13 | +/// [UniqueRef.dispose] or [CountedRef.unref]. |
| 14 | +SkObjectFinalizationRegistry _finalizationRegistry = createSkObjectFinalizationRegistry( |
| 15 | + (UniqueRef<Object> uniq) { |
| 16 | + uniq.collect(); |
| 17 | + }.toJS |
| 18 | +); |
| 19 | + |
| 20 | +NativeMemoryFinalizationRegistry nativeMemoryFinalizationRegistry = NativeMemoryFinalizationRegistry(); |
| 21 | + |
| 22 | +/// An indirection to [SkObjectFinalizationRegistry] to enable tests provide a |
| 23 | +/// mock implementation of a finalization registry. |
| 24 | +class NativeMemoryFinalizationRegistry { |
| 25 | + void register(Object owner, UniqueRef<Object> ref) { |
| 26 | + _finalizationRegistry.register(owner, ref); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/// Manages the lifecycle of a C++ object referenced by a single Dart object. |
| 31 | +/// |
| 32 | +/// It is expected that when the C++ object is no longer needed [dispose] is |
| 33 | +/// called. |
| 34 | +/// |
| 35 | +/// To prevent memory leaks, the underlying C++ object is deleted by the GC if |
| 36 | +/// it wasn't previously disposed of explicitly. |
| 37 | +class UniqueRef<T extends Object> { |
| 38 | + UniqueRef(Object owner, T nativeObject, this._debugOwnerLabel) { |
| 39 | + _nativeObject = nativeObject; |
| 40 | + if (Instrumentation.enabled) { |
| 41 | + Instrumentation.instance.incrementCounter('$_debugOwnerLabel Created'); |
| 42 | + } |
| 43 | + nativeMemoryFinalizationRegistry.register(owner, this); |
| 44 | + } |
| 45 | + |
| 46 | + T? _nativeObject; |
| 47 | + final String _debugOwnerLabel; |
| 48 | + |
| 49 | + /// Returns the underlying native object reference, if it has not been |
| 50 | + /// disposed of yet. |
| 51 | + /// |
| 52 | + /// The returned reference must not be stored. I should only be borrowed |
| 53 | + /// temporarily. Storing this reference may result in dangling pointer errors. |
| 54 | + T get nativeObject { |
| 55 | + assert(!isDisposed, 'Native object was disposed.'); |
| 56 | + return _nativeObject!; |
| 57 | + } |
| 58 | + |
| 59 | + /// Returns whether the underlying native object has been disposed and |
| 60 | + /// therefore can no longer be used. |
| 61 | + bool get isDisposed => _nativeObject == null; |
| 62 | + |
| 63 | + /// Disposes the underlying native object. |
| 64 | + /// |
| 65 | + /// The underlying object may be deleted or its ref count may be bumped down. |
| 66 | + /// The exact action taken depends on the sharing model of that particular |
| 67 | + /// object. For example, an [SkImage] may not be immediately deleted if a |
| 68 | + /// [SkPicture] exists that still references it. On the other hand, [SkPaint] |
| 69 | + /// is deleted eagerly. |
| 70 | + void dispose() { |
| 71 | + assert(!isDisposed, 'A native object reference cannot be disposed more than once.'); |
| 72 | + if (Instrumentation.enabled) { |
| 73 | + Instrumentation.instance.incrementCounter('$_debugOwnerLabel Deleted'); |
| 74 | + } |
| 75 | + final SkDeletable object = nativeObject as SkDeletable; |
| 76 | + if (!object.isDeleted()) { |
| 77 | + object.delete(); |
| 78 | + } |
| 79 | + _nativeObject = null; |
| 80 | + } |
| 81 | + |
| 82 | + /// Called by the garbage [Collector] when the owner of this handle is |
| 83 | + /// collected. |
| 84 | + /// |
| 85 | + /// Garbage collection is used as a back-up for the cases when the handle |
| 86 | + /// isn't disposed of explicitly by calling [dispose]. It most likely |
| 87 | + /// indicates a memory leak or inefficiency in the framework or application |
| 88 | + /// code. |
| 89 | + @visibleForTesting |
| 90 | + void collect() { |
| 91 | + if (!isDisposed) { |
| 92 | + if (Instrumentation.enabled) { |
| 93 | + Instrumentation.instance.incrementCounter('$_debugOwnerLabel Leaked'); |
| 94 | + } |
| 95 | + dispose(); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +/// Interface that classes wrapping [UniqueRef] must implement. |
| 101 | +/// |
| 102 | +/// Used to collect stack traces in debug mode. |
| 103 | +abstract class StackTraceDebugger { |
| 104 | + /// The stack trace pointing to code location that created or upreffed a |
| 105 | + /// [CountedRef]. |
| 106 | + StackTrace get debugStackTrace; |
| 107 | +} |
| 108 | + |
| 109 | +/// Manages the lifecycle of a C++ object referenced by multiple Dart objects. |
| 110 | +/// |
| 111 | +/// Uses reference counting to manage the lifecycle of the C++ object. |
| 112 | +/// |
| 113 | +/// If the C++ object has a unique owner, use [UniqueRef] instead. |
| 114 | +/// |
| 115 | +/// The [ref] method can be used to increment the refcount to tell this box to |
| 116 | +/// keep the underlying C++ object alive. |
| 117 | +/// |
| 118 | +/// The [unref] method can be used to decrement the refcount indicating that a |
| 119 | +/// referring object no longer needs it. When the refcount drops to zero the |
| 120 | +/// underlying C++ object is deleted. |
| 121 | +/// |
| 122 | +/// In addition to ref counting, this object is also managed by GC. When this |
| 123 | +/// reference is garbage collected, the underlying C++ object is automatically |
| 124 | +/// deleted. This is mostly done to prevent memory leaks in production. Well |
| 125 | +/// behaving framework and app code are expected to rely on [ref] and [unref] |
| 126 | +/// for timely collection of resources. |
| 127 | +class CountedRef<R extends StackTraceDebugger, T extends Object> { |
| 128 | + /// Creates a counted reference. |
| 129 | + CountedRef(T nativeObject, R debugReferrer, String debugLabel) { |
| 130 | + _ref = UniqueRef<T>(this, nativeObject, debugLabel); |
| 131 | + if (assertionsEnabled) { |
| 132 | + debugReferrers.add(debugReferrer); |
| 133 | + } |
| 134 | + assert(refCount == debugReferrers.length); |
| 135 | + } |
| 136 | + |
| 137 | + /// The native object reference whose lifecycle is being managed by this ref |
| 138 | + /// count. |
| 139 | + /// |
| 140 | + /// Do not store this value outside this class. |
| 141 | + late final UniqueRef<T> _ref; |
| 142 | + |
| 143 | + /// Returns the underlying native object reference, if it has not been |
| 144 | + /// disposed of yet. |
| 145 | + /// |
| 146 | + /// The returned reference must not be stored. I should only be borrowed |
| 147 | + /// temporarily. Storing this reference may result in dangling pointer errors. |
| 148 | + T get nativeObject => _ref.nativeObject; |
| 149 | + |
| 150 | + /// The number of objects sharing references to this box. |
| 151 | + /// |
| 152 | + /// When this count reaches zero, the underlying [nativeObject] is scheduled |
| 153 | + /// for deletion. |
| 154 | + int get refCount => _refCount; |
| 155 | + int _refCount = 1; |
| 156 | + |
| 157 | + /// Whether the underlying [nativeObject] has been disposed and is no longer |
| 158 | + /// accessible. |
| 159 | + bool get isDisposed => _ref.isDisposed; |
| 160 | + |
| 161 | + /// When assertions are enabled, stores all objects that share this box. |
| 162 | + /// |
| 163 | + /// The length of this list is always identical to [refCount]. |
| 164 | + /// |
| 165 | + /// This list can be used for debugging ref counting issues. |
| 166 | + final Set<R> debugReferrers = <R>{}; |
| 167 | + |
| 168 | + /// If asserts are enabled, the [StackTrace]s representing when a reference |
| 169 | + /// was created. |
| 170 | + List<StackTrace> debugGetStackTraces() { |
| 171 | + if (assertionsEnabled) { |
| 172 | + return debugReferrers |
| 173 | + .map<StackTrace>((R referrer) => referrer.debugStackTrace) |
| 174 | + .toList(); |
| 175 | + } |
| 176 | + throw UnsupportedError(''); |
| 177 | + } |
| 178 | + |
| 179 | + /// Increases the reference count of this box because a new object began |
| 180 | + /// sharing ownership of the underlying [nativeObject]. |
| 181 | + void ref(R debugReferrer) { |
| 182 | + assert( |
| 183 | + !_ref.isDisposed, |
| 184 | + 'Cannot increment ref count on a deleted handle.', |
| 185 | + ); |
| 186 | + assert(_refCount > 0); |
| 187 | + assert( |
| 188 | + debugReferrers.add(debugReferrer), |
| 189 | + 'Attempted to increment ref count by the same referrer more than once.', |
| 190 | + ); |
| 191 | + _refCount += 1; |
| 192 | + assert(refCount == debugReferrers.length); |
| 193 | + } |
| 194 | + |
| 195 | + /// Decrements the reference count for the [nativeObject]. |
| 196 | + /// |
| 197 | + /// Does nothing if the object has already been deleted. |
| 198 | + /// |
| 199 | + /// If this causes the reference count to drop to zero, deletes the |
| 200 | + /// [nativeObject]. |
| 201 | + void unref(R debugReferrer) { |
| 202 | + assert( |
| 203 | + !_ref.isDisposed, |
| 204 | + 'Attempted to unref an already deleted native object.', |
| 205 | + ); |
| 206 | + assert( |
| 207 | + debugReferrers.remove(debugReferrer), |
| 208 | + 'Attempted to decrement ref count by the same referrer more than once.', |
| 209 | + ); |
| 210 | + _refCount -= 1; |
| 211 | + assert(refCount == debugReferrers.length); |
| 212 | + if (_refCount == 0) { |
| 213 | + _ref.dispose(); |
| 214 | + } |
| 215 | + } |
| 216 | +} |
0 commit comments