Skip to content

Commit b49e2d6

Browse files
authored
Revert "[web] Fix canvas leak when dpi changes. Evict from BitmapCanvas cache under… (flutter#16721)"
This reverts commit f15b08d.
1 parent 88c9383 commit b49e2d6

File tree

2 files changed

+20
-54
lines changed

2 files changed

+20
-54
lines changed

lib/web_ui/lib/src/engine/surface/picture.dart

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,6 @@ const int _kCanvasCacheSize = 30;
1919
/// Canvases available for reuse, capped at [_kCanvasCacheSize].
2020
final List<BitmapCanvas> _recycledCanvases = <BitmapCanvas>[];
2121

22-
/// Reduces recycled canvas list by 50% to reduce bitmap canvas memory use.
23-
void _reduceCanvasMemoryUsage() {
24-
final int canvasCount = _recycledCanvases.length;
25-
for (int i = 0; i < canvasCount; i++) {
26-
_recycledCanvases[i].dispose();
27-
}
28-
_recycledCanvases.clear();
29-
}
30-
3122
/// A request to repaint a canvas.
3223
///
3324
/// Paint requests are prioritized such that the larger pictures go first. This
@@ -51,27 +42,22 @@ class _PaintRequest {
5142
List<_PaintRequest> _paintQueue = <_PaintRequest>[];
5243

5344
void _recycleCanvas(EngineCanvas canvas) {
54-
if (canvas is BitmapCanvas) {
55-
if (canvas.isReusable()) {
56-
_recycledCanvases.add(canvas);
57-
if (_recycledCanvases.length > _kCanvasCacheSize) {
58-
final BitmapCanvas removedCanvas = _recycledCanvases.removeAt(0);
59-
removedCanvas.dispose();
60-
if (_debugShowCanvasReuseStats) {
61-
DebugCanvasReuseOverlay.instance.disposedCount++;
62-
}
63-
}
45+
if (canvas is BitmapCanvas && canvas.isReusable()) {
46+
_recycledCanvases.add(canvas);
47+
if (_recycledCanvases.length > _kCanvasCacheSize) {
48+
final BitmapCanvas removedCanvas = _recycledCanvases.removeAt(0);
49+
removedCanvas.dispose();
6450
if (_debugShowCanvasReuseStats) {
65-
DebugCanvasReuseOverlay.instance.inRecycleCount =
66-
_recycledCanvases.length;
51+
DebugCanvasReuseOverlay.instance.disposedCount++;
6752
}
68-
} else {
69-
canvas.dispose();
53+
}
54+
if (_debugShowCanvasReuseStats) {
55+
DebugCanvasReuseOverlay.instance.inRecycleCount =
56+
_recycledCanvases.length;
7057
}
7158
}
7259
}
7360

74-
7561
/// Signature of a function that instantiates a [PersistedPicture].
7662
typedef PersistedPictureFactory = PersistedPicture Function(
7763
double dx,
@@ -286,6 +272,7 @@ class PersistedStandardPicture extends PersistedPicture {
286272
final ui.Size canvasSize = bounds.size;
287273
BitmapCanvas bestRecycledCanvas;
288274
double lastPixelCount = double.infinity;
275+
289276
for (int i = 0; i < _recycledCanvases.length; i++) {
290277
final BitmapCanvas candidate = _recycledCanvases[i];
291278
if (!candidate.isReusable()) {
@@ -299,21 +286,13 @@ class PersistedStandardPicture extends PersistedPicture {
299286
final bool fits = candidate.doesFitBounds(bounds);
300287
final bool isSmaller = candidatePixelCount < lastPixelCount;
301288
if (fits && isSmaller) {
302-
// [isTooSmall] is used to make sure that a small picture doesn't
303-
// reuse and hold onto memory of a large canvas.
304-
final double requestedPixelCount = bounds.width * bounds.height;
305-
final bool isTooSmall = isSmaller &&
306-
requestedPixelCount > 1 &&
307-
(candidatePixelCount / requestedPixelCount) > 4;
308-
if (!isTooSmall) {
309-
bestRecycledCanvas = candidate;
310-
lastPixelCount = candidatePixelCount;
311-
final bool fitsExactly = candidateSize.width == canvasSize.width &&
312-
candidateSize.height == canvasSize.height;
313-
if (fitsExactly) {
314-
// No need to keep looking any more.
315-
break;
316-
}
289+
bestRecycledCanvas = candidate;
290+
lastPixelCount = candidatePixelCount;
291+
final bool fitsExactly = candidateSize.width == canvasSize.width &&
292+
candidateSize.height == canvasSize.height;
293+
if (fitsExactly) {
294+
// No need to keep looking any more.
295+
break;
317296
}
318297
}
319298
}

lib/web_ui/lib/src/engine/surface/recording_canvas.dart

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ class RecordingCanvas {
183183
}
184184

185185
void drawColor(ui.Color color, ui.BlendMode blendMode) {
186+
_hasArbitraryPaint = true;
187+
_didDraw = true;
186188
_paintBounds.grow(_paintBounds.maxPaintBounds);
187189
_commands.add(PaintDrawColor(color, blendMode));
188190
}
@@ -313,21 +315,6 @@ class RecordingCanvas {
313315
}
314316

315317
void drawPath(ui.Path path, SurfacePaint paint) {
316-
if (paint.shader == null) {
317-
// For Rect/RoundedRect paths use drawRect/drawRRect code paths for
318-
// DomCanvas optimization.
319-
SurfacePath sPath = path;
320-
final ui.Rect rect = sPath.webOnlyPathAsRect;
321-
if (rect != null) {
322-
drawRect(rect, paint);
323-
return;
324-
}
325-
final ui.RRect rrect = sPath.webOnlyPathAsRoundedRect;
326-
if (rrect != null) {
327-
drawRRect(rrect, paint);
328-
return;
329-
}
330-
}
331318
_hasArbitraryPaint = true;
332319
_didDraw = true;
333320
ui.Rect pathBounds = path.getBounds();

0 commit comments

Comments
 (0)