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

Commit 29e256a

Browse files
authored
Add CanvasKit platform support for Skia.setResourceCacheMaxBytes. (#19254)
* Add CanvasKit platform support for Skia.setResourceCacheMaxBytes.
1 parent 55e0d29 commit 29e256a

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

lib/web_ui/lib/src/engine/compositor/rasterizer.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ class Rasterizer {
1313

1414
Rasterizer(this.surface);
1515

16+
void setSkiaResourceCacheMaxBytes(int bytes) =>
17+
surface.setSkiaResourceCacheMaxBytes(bytes);
18+
1619
/// Creates a new frame from this rasterizer's surface, draws the given
1720
/// [LayerTree] into it, and then submits the frame.
1821
void draw(LayerTree layerTree) {

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@ class Surface {
3838

3939
CkSurface? _surface;
4040
html.Element? htmlElement;
41+
js.JsObject? _grContext;
42+
int? _skiaCacheBytes;
43+
44+
/// Specify the GPU resource cache limits.
45+
void setSkiaResourceCacheMaxBytes(int bytes) {
46+
_skiaCacheBytes = bytes;
47+
_syncCacheBytes();
48+
}
49+
50+
void _syncCacheBytes() {
51+
if(_skiaCacheBytes != null) {
52+
_grContext?.callMethod('setResourceCacheLimitBytes', <dynamic>[
53+
_skiaCacheBytes]);
54+
}
55+
}
4156

4257
bool _addedToScene = false;
4358

@@ -110,16 +125,20 @@ class Surface {
110125
// anti-aliased by setting their `Paint` object's `antialias` property.
111126
js.JsObject.jsify({'antialias': 0}),
112127
]);
113-
final js.JsObject? grContext =
128+
_grContext =
114129
canvasKit.callMethod('MakeGrContext', <dynamic>[glContext]);
115130

116-
if (grContext == null) {
131+
if (_grContext == null) {
117132
throw CanvasKitError('Could not create a graphics context.');
118133
}
119134

135+
// Set the cache byte limit for this grContext, if not specified it will use
136+
// CanvasKit's default.
137+
_syncCacheBytes();
138+
120139
final js.JsObject? skSurface =
121140
canvasKit.callMethod('MakeOnScreenGLSurface', <dynamic>[
122-
grContext,
141+
_grContext,
123142
size.width,
124143
size.height,
125144
canvasKit['SkColorSpace']['SRGB'],
@@ -130,7 +149,7 @@ class Surface {
130149
}
131150

132151
htmlElement = htmlCanvas;
133-
return CkSurface(skSurface, grContext, glContext);
152+
return CkSurface(skSurface, _grContext!, glContext);
134153
}
135154

136155
bool _presentSurface() {

lib/web_ui/lib/src/engine/window.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,19 @@ class EngineWindow extends ui.Window {
494494
}
495495

496496
switch (name) {
497+
/// This should be in sync with shell/common/shell.cc
498+
case 'flutter/skia':
499+
const MethodCodec codec = JSONMethodCodec();
500+
final MethodCall decoded = codec.decodeMethodCall(data);
501+
switch (decoded.method) {
502+
case 'Skia.setResourceCacheMaxBytes':
503+
if (decoded.arguments is int) {
504+
rasterizer?.setSkiaResourceCacheMaxBytes(decoded.arguments);
505+
}
506+
break;
507+
}
508+
509+
return;
497510
case 'flutter/assets':
498511
assert(ui.webOnlyAssetManager != null); // ignore: unnecessary_null_comparison
499512
final String url = utf8.decode(data!.buffer.asUint8List());

0 commit comments

Comments
 (0)