Skip to content

Commit 0d2e389

Browse files
authored
Respond to Skia platform messages (flutter#24880)
1 parent 874b8e8 commit 0d2e389

File tree

3 files changed

+103
-5
lines changed

3 files changed

+103
-5
lines changed

lib/web_ui/lib/src/engine/platform_dispatcher.dart

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,20 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
333333
final MethodCall decoded = codec.decodeMethodCall(data);
334334
switch (decoded.method) {
335335
case 'Skia.setResourceCacheMaxBytes':
336-
if (decoded.arguments is int) {
337-
rasterizer?.setSkiaResourceCacheMaxBytes(decoded.arguments);
336+
if (useCanvasKit) {
337+
// If we're in CanvasKit mode, we must also have a rasterizer.
338+
assert(rasterizer != null);
339+
assert(
340+
decoded.arguments is int,
341+
'Argument to Skia.setResourceCacheMaxBytes must be an int, but was ${decoded.arguments.runtimeType}',
342+
);
343+
final int cacheSizeInBytes = decoded.arguments as int;
344+
rasterizer!.setSkiaResourceCacheMaxBytes(cacheSizeInBytes);
338345
}
346+
347+
// Also respond in HTML mode. Otherwise, apps would have to detect
348+
// CanvasKit vs HTML before invoking this method.
349+
_replyToPlatformMessage(callback, codec.encodeSuccessEnvelope([true]));
339350
break;
340351
}
341352
return;
@@ -896,9 +907,9 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
896907
/// messages and responses have to be exchanged asynchronously. We simulate
897908
/// that by adding a zero-length delay to the reply.
898909
void _replyToPlatformMessage(
899-
ui.PlatformMessageResponseCallback? callback,
900-
ByteData? data,
901-
) {
910+
ui.PlatformMessageResponseCallback? callback,
911+
ByteData? data,
912+
) {
902913
Future<void>.delayed(Duration.zero).then((_) {
903914
if (callback != null) {
904915
callback(data);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
// @dart = 2.12
6+
import 'dart:async';
7+
import 'dart:typed_data';
8+
9+
import 'package:ui/src/engine.dart';
10+
import 'package:ui/ui.dart' as ui;
11+
12+
import 'package:test/bootstrap/browser.dart';
13+
import 'package:test/test.dart';
14+
15+
import 'common.dart';
16+
17+
void main() {
18+
internalBootstrapBrowserTest(() => testMain);
19+
}
20+
21+
void testMain() {
22+
group('PlatformDispatcher', () {
23+
setUpCanvasKitTest();
24+
25+
test('responds to flutter/skia Skia.setResourceCacheMaxBytes', () async {
26+
const MethodCodec codec = JSONMethodCodec();
27+
final Completer<ByteData?> completer = Completer<ByteData?>();
28+
ui.PlatformDispatcher.instance.sendPlatformMessage(
29+
'flutter/skia',
30+
codec.encodeMethodCall(MethodCall(
31+
'Skia.setResourceCacheMaxBytes',
32+
512 * 1000 * 1000,
33+
)),
34+
completer.complete,
35+
);
36+
37+
final ByteData? response = await completer.future;
38+
expect(response, isNotNull);
39+
expect(
40+
codec.decodeEnvelope(response!),
41+
[true],
42+
);
43+
});
44+
// TODO: https://github.com/flutter/flutter/issues/60040
45+
}, skip: isIosSafari);
46+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
// @dart = 2.12
6+
import 'dart:async';
7+
import 'dart:typed_data';
8+
9+
import 'package:ui/src/engine.dart';
10+
import 'package:ui/ui.dart' as ui;
11+
12+
import 'package:test/bootstrap/browser.dart';
13+
import 'package:test/test.dart';
14+
15+
void main() {
16+
internalBootstrapBrowserTest(() => testMain);
17+
}
18+
19+
void testMain() {
20+
group('PlatformDispatcher', () {
21+
test('responds to flutter/skia Skia.setResourceCacheMaxBytes', () async {
22+
const MethodCodec codec = JSONMethodCodec();
23+
final Completer<ByteData?> completer = Completer<ByteData?>();
24+
ui.PlatformDispatcher.instance.sendPlatformMessage(
25+
'flutter/skia',
26+
codec.encodeMethodCall(MethodCall(
27+
'Skia.setResourceCacheMaxBytes',
28+
512 * 1000 * 1000,
29+
)),
30+
completer.complete,
31+
);
32+
33+
final ByteData? response = await completer.future;
34+
expect(response, isNotNull);
35+
expect(
36+
codec.decodeEnvelope(response!),
37+
[true],
38+
);
39+
});
40+
});
41+
}

0 commit comments

Comments
 (0)