Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/web_ui/dev/goldens_lock.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
repository: https://github.com/flutter/goldens.git
revision: a1e78be938fe79544eeacccce1e2a5ace3d4058e
revision: 2eb81061582dbe73939811d4471d6167b463f11c
2 changes: 1 addition & 1 deletion lib/web_ui/lib/src/engine/canvaskit/canvaskit_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ class SkImage {
Float32List? matrix, // 3x3 matrix
);
external Uint8List readPixels(int srcX, int srcY, SkImageInfo imageInfo);
external SkData encodeToData();
external Uint8List? encodeToBytes();
external bool isAliasOf(SkImage other);
external bool isDeleted();
}
Expand Down
27 changes: 17 additions & 10 deletions lib/web_ui/lib/src/engine/canvaskit/image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,18 @@ class CkImage implements ui.Image, StackTraceDebugger {
// IMPORTANT: the alphaType, colorType, and colorSpace passed to
// _encodeImage and to canvasKit.MakeImage must be the same. Otherwise
// Skia will misinterpret the pixels and corrupt the image.
final ByteData originalBytes = _encodeImage(
final ByteData? originalBytes = _encodeImage(
skImage: skImage,
format: ui.ImageByteFormat.rawRgba,
alphaType: canvasKit.AlphaType.Premul,
colorType: canvasKit.ColorType.RGBA_8888,
colorSpace: SkColorSpaceSRGB,
);
if (originalBytes == null) {
html.window.console.warn('Unable to encode image to bytes. We will not '
'be able to resurrect it once it has been garbage collected.');
return;
}
final int originalWidth = skImage.width();
final int originalHeight = skImage.height();
box = SkiaObjectBox<CkImage, SkImage>.resurrectable(this, skImage, () {
Expand Down Expand Up @@ -276,23 +281,28 @@ class CkImage implements ui.Image, StackTraceDebugger {
ui.ImageByteFormat format = ui.ImageByteFormat.rawRgba,
}) {
assert(_debugCheckIsNotDisposed());
return Future<ByteData>.value(_encodeImage(
ByteData? data = _encodeImage(
skImage: skImage,
format: format,
alphaType: canvasKit.AlphaType.Premul,
colorType: canvasKit.ColorType.RGBA_8888,
colorSpace: SkColorSpaceSRGB,
));
);
if (data == null) {
return Future<ByteData>.error('Failed to encode the image into bytes.');
} else {
return Future<ByteData>.value(data);
}
}

static ByteData _encodeImage({
static ByteData? _encodeImage({
required SkImage skImage,
required ui.ImageByteFormat format,
required SkAlphaType alphaType,
required SkColorType colorType,
required ColorSpace colorSpace,
}) {
Uint8List bytes;
Uint8List? bytes;

if (format == ui.ImageByteFormat.rawRgba) {
final SkImageInfo imageInfo = SkImageInfo(
Expand All @@ -304,13 +314,10 @@ class CkImage implements ui.Image, StackTraceDebugger {
);
bytes = skImage.readPixels(0, 0, imageInfo);
} else {
final SkData skData = skImage.encodeToData(); //defaults to PNG 100%
// make a copy that we can return
bytes = Uint8List.fromList(canvasKit.getDataBytes(skData));
skData.delete();
bytes = skImage.encodeToBytes(); //defaults to PNG 100%
}

return bytes.buffer.asByteData(0, bytes.length);
return bytes?.buffer.asByteData(0, bytes.length);
}

@override
Expand Down
14 changes: 9 additions & 5 deletions lib/web_ui/lib/src/engine/canvaskit/initialization.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ const bool canvasKitForceCpuOnly = bool.fromEnvironment(
/// NPM, update this URL to `https://unpkg.com/[email protected]/bin/`.
const String canvasKitBaseUrl = String.fromEnvironment(
'FLUTTER_WEB_CANVASKIT_URL',
defaultValue: 'https://unpkg.com/canvaskit-wasm@0.22.0/bin/',
defaultValue: 'https://unpkg.com/canvaskit-wasm@0.24.0/bin/',
);
final String canvasKitBuildUrl = canvasKitBaseUrl + (kProfileMode ? 'profiling/' : '');
final String canvasKitJavaScriptBindingsUrl = canvasKitBuildUrl + 'canvaskit.js';
final String canvasKitBuildUrl =
canvasKitBaseUrl + (kProfileMode ? 'profiling/' : '');
final String canvasKitJavaScriptBindingsUrl =
canvasKitBuildUrl + 'canvaskit.js';
String canvasKitWasmModuleUrl(String file) => canvasKitBuildUrl + file;

/// Initialize CanvasKit.
Expand All @@ -89,8 +91,10 @@ Future<void> initializeCanvasKit() {
late StreamSubscription<html.Event> loadSubscription;
loadSubscription = domRenderer.canvasKitScript!.onLoad.listen((_) {
loadSubscription.cancel();
final CanvasKitInitPromise canvasKitInitPromise = CanvasKitInit(CanvasKitInitOptions(
locateFile: js.allowInterop((String file, String unusedBase) => canvasKitWasmModuleUrl(file)),
final CanvasKitInitPromise canvasKitInitPromise =
CanvasKitInit(CanvasKitInitOptions(
locateFile: js.allowInterop(
(String file, String unusedBase) => canvasKitWasmModuleUrl(file)),
));
canvasKitInitPromise.then(js.allowInterop((CanvasKit ck) {
canvasKit = ck;
Expand Down
2 changes: 1 addition & 1 deletion lib/web_ui/test/canvaskit/canvaskit_api_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,7 @@ void _paragraphTests() {
expect(paragraph.getIdeographicBaseline(), within<double>(distance: 0.5, from: 28));
expect(paragraph.getLongestLine(), 50);
expect(paragraph.getMaxIntrinsicWidth(), 50);
expect(paragraph.getMinIntrinsicWidth(), 0);
expect(paragraph.getMinIntrinsicWidth(), 50);
expect(paragraph.getMaxWidth(), 55);
expect(paragraph.getRectsForRange(1, 3, canvasKit.RectHeightStyle.Tight, canvasKit.RectWidthStyle.Max), <double>[]);
expect(paragraph.getRectsForPlaceholders(), hasLength(1));
Expand Down