Roll CanvasKit to 0.16.1 (flutter/engine#18618)
* Roll CanvasKit to 0.16.1 Fixes https://github.com/flutter/flutter/issues/58014 Fixes https://github.com/flutter/flutter/issues/56938 * Use multiple shared color arrays * Use fresh color arrays for computeTonalColors * Use drawColorInt * Add TODO to use a flattened array when CanvasKit supports it
This commit is contained in:
@@ -14,7 +14,8 @@ class SkCanvas {
|
||||
int get saveCount => skCanvas.callMethod('getSaveCount');
|
||||
|
||||
void clear(ui.Color color) {
|
||||
skCanvas.callMethod('clear', <int>[color.value]);
|
||||
setSharedSkColor1(color);
|
||||
skCanvas.callMethod('clear', <js.JsObject>[sharedSkColor1]);
|
||||
}
|
||||
|
||||
void clipPath(ui.Path path, bool doAntiAlias) {
|
||||
@@ -73,7 +74,7 @@ class SkCanvas {
|
||||
ui.Image atlas,
|
||||
Float32List rstTransforms,
|
||||
Float32List rects,
|
||||
Int32List colors,
|
||||
js.JsArray<Float32List> colors,
|
||||
ui.BlendMode blendMode,
|
||||
) {
|
||||
final SkImage skAtlas = atlas;
|
||||
@@ -97,7 +98,7 @@ class SkCanvas {
|
||||
}
|
||||
|
||||
void drawColor(ui.Color color, ui.BlendMode blendMode) {
|
||||
skCanvas.callMethod('drawColor', <dynamic>[
|
||||
skCanvas.callMethod('drawColorInt', <dynamic>[
|
||||
color.value,
|
||||
makeSkBlendMode(blendMode),
|
||||
]);
|
||||
@@ -185,7 +186,9 @@ class SkCanvas {
|
||||
skCanvas.callMethod('drawPicture', <js.JsObject>[skPicture.skPicture]);
|
||||
}
|
||||
|
||||
void drawPoints(SkPaint paint, ui.PointMode pointMode, Float32List points) {
|
||||
// TODO(hterkelsen): https://github.com/flutter/flutter/issues/58824
|
||||
void drawPoints(SkPaint paint, ui.PointMode pointMode,
|
||||
js.JsArray<js.JsArray<double>> points) {
|
||||
skCanvas.callMethod('drawPoints', <dynamic>[
|
||||
makeSkPointMode(pointMode),
|
||||
points,
|
||||
@@ -273,7 +276,8 @@ class SkCanvas {
|
||||
}
|
||||
|
||||
void transform(Float32List matrix4) {
|
||||
skCanvas.callMethod('concat', <js.JsArray<double>>[makeSkMatrixFromFloat32(matrix4)]);
|
||||
skCanvas.callMethod(
|
||||
'concat', <js.JsArray<double>>[makeSkMatrixFromFloat32(matrix4)]);
|
||||
}
|
||||
|
||||
void translate(double dx, double dy) {
|
||||
|
||||
@@ -319,10 +319,11 @@ class CanvasKitCanvas implements ui.Canvas {
|
||||
if (points.length % 2 != 0) {
|
||||
throw ArgumentError('"points" must have an even number of values.');
|
||||
}
|
||||
_drawPoints(paint, pointMode, points);
|
||||
_drawPoints(paint, pointMode, encodeRawPointList(points));
|
||||
}
|
||||
|
||||
void _drawPoints(ui.Paint paint, ui.PointMode pointMode, Float32List points) {
|
||||
void _drawPoints(
|
||||
ui.Paint paint, ui.PointMode pointMode, List<List<double>> points) {
|
||||
_canvas.drawPoints(paint, pointMode, points);
|
||||
}
|
||||
|
||||
@@ -386,8 +387,8 @@ class CanvasKitCanvas implements ui.Canvas {
|
||||
rectBuffer[index3] = rect.bottom;
|
||||
}
|
||||
|
||||
final Int32List colorBuffer =
|
||||
colors.isEmpty ? null : _encodeColorList(colors);
|
||||
final js.JsArray<Float32List> colorBuffer =
|
||||
colors.isEmpty ? null : makeColorList(colors);
|
||||
|
||||
_drawAtlas(
|
||||
paint, atlas, rstTransformBuffer, rectBuffer, colorBuffer, blendMode);
|
||||
@@ -419,7 +420,8 @@ class CanvasKitCanvas implements ui.Canvas {
|
||||
throw ArgumentError(
|
||||
'If non-null, "colors" length must be one fourth the length of "rstTransforms" and "rects".');
|
||||
|
||||
_drawAtlas(paint, atlas, rstTransforms, rects, colors, blendMode);
|
||||
_drawAtlas(paint, atlas, rstTransforms, rects, _encodeRawColorList(colors),
|
||||
blendMode);
|
||||
}
|
||||
|
||||
// TODO(hterkelsen): Pass a cull_rect once CanvasKit supports that.
|
||||
@@ -428,7 +430,7 @@ class CanvasKitCanvas implements ui.Canvas {
|
||||
ui.Image atlas,
|
||||
Float32List rstTransforms,
|
||||
Float32List rects,
|
||||
Int32List colors,
|
||||
js.JsArray<Float32List> colors,
|
||||
ui.BlendMode blendMode,
|
||||
) {
|
||||
_canvas.drawAtlasRaw(paint, atlas, rstTransforms, rects, colors, blendMode);
|
||||
|
||||
@@ -10,9 +10,10 @@ class SkColorFilter {
|
||||
js.JsObject skColorFilter;
|
||||
|
||||
SkColorFilter.mode(EngineColorFilter filter) {
|
||||
setSharedSkColor1(filter._color);
|
||||
skColorFilter =
|
||||
canvasKit['SkColorFilter'].callMethod('MakeBlend', <dynamic>[
|
||||
filter._color.value,
|
||||
sharedSkColor1,
|
||||
makeSkBlendMode(filter._blendMode),
|
||||
]);
|
||||
}
|
||||
@@ -29,10 +30,12 @@ class SkColorFilter {
|
||||
}
|
||||
|
||||
SkColorFilter.linearToSrgbGamma(EngineColorFilter filter) {
|
||||
skColorFilter = canvasKit['SkColorFilter'].callMethod('MakeLinearToSRGBGamma');
|
||||
skColorFilter =
|
||||
canvasKit['SkColorFilter'].callMethod('MakeLinearToSRGBGamma');
|
||||
}
|
||||
|
||||
SkColorFilter.srgbToLinearGamma(EngineColorFilter filter) {
|
||||
skColorFilter = canvasKit['SkColorFilter'].callMethod('MakeSRGBToLinearGamma');
|
||||
skColorFilter =
|
||||
canvasKit['SkColorFilter'].callMethod('MakeSRGBToLinearGamma');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -172,12 +172,15 @@ class SkiaFontCollection {
|
||||
final js.JsObject tmpFontMgr =
|
||||
canvasKit['SkFontMgr'].callMethod('FromData', <Uint8List>[bytes]);
|
||||
String actualFamily = tmpFontMgr.callMethod('getFamilyName', <int>[0]);
|
||||
tmpFontMgr.callMethod('delete');
|
||||
return actualFamily;
|
||||
}
|
||||
|
||||
Future<ByteBuffer> _getArrayBuffer(dynamic fetchResult) {
|
||||
// TODO(yjbanov): fetchResult.arrayBuffer is a dynamic invocation. Clean it up.
|
||||
return fetchResult.arrayBuffer().then<ByteBuffer>((dynamic x) => x as ByteBuffer);
|
||||
return fetchResult
|
||||
.arrayBuffer()
|
||||
.then<ByteBuffer>((dynamic x) => x as ByteBuffer);
|
||||
}
|
||||
|
||||
js.JsObject skFontMgr;
|
||||
|
||||
@@ -14,7 +14,7 @@ const bool experimentalUseSkia =
|
||||
/// When CanvasKit pushes a new release to NPM, update this URL to reflect the
|
||||
/// most recent version. For example, if CanvasKit releases version 0.34.0 to
|
||||
/// NPM, update this URL to `https://unpkg.com/canvaskit-wasm@0.34.0/bin/`.
|
||||
const String canvasKitBaseUrl = 'https://unpkg.com/canvaskit-wasm@0.14.0/bin/';
|
||||
const String canvasKitBaseUrl = 'https://unpkg.com/canvaskit-wasm@0.16.1/bin/';
|
||||
|
||||
/// Initialize the Skia backend.
|
||||
///
|
||||
@@ -27,9 +27,8 @@ Future<void> initializeSkia() {
|
||||
final js.JsObject canvasKitInitArgs = js.JsObject.jsify(<String, dynamic>{
|
||||
'locateFile': (String file, String unusedBase) => canvasKitBaseUrl + file,
|
||||
});
|
||||
final js.JsObject canvasKitInit =
|
||||
final js.JsObject canvasKitInitPromise =
|
||||
js.JsObject(js.context['CanvasKitInit'], <dynamic>[canvasKitInitArgs]);
|
||||
final js.JsObject canvasKitInitPromise = canvasKitInit.callMethod('ready');
|
||||
canvasKitInitPromise.callMethod('then', <dynamic>[
|
||||
(js.JsObject ck) {
|
||||
canvasKit = ck;
|
||||
|
||||
@@ -13,7 +13,8 @@ class SkPaint extends SkiaObject implements ui.Paint {
|
||||
SkPaint();
|
||||
|
||||
static const ui.Color _defaultPaintColor = ui.Color(0xFF000000);
|
||||
static final js.JsObject _skPaintStyleStroke = canvasKit['PaintStyle']['Stroke'];
|
||||
static final js.JsObject _skPaintStyleStroke =
|
||||
canvasKit['PaintStyle']['Stroke'];
|
||||
static final js.JsObject _skPaintStyleFill = canvasKit['PaintStyle']['Fill'];
|
||||
|
||||
@override
|
||||
@@ -23,10 +24,12 @@ class SkPaint extends SkiaObject implements ui.Paint {
|
||||
_blendMode = value;
|
||||
_syncBlendMode(skiaObject);
|
||||
}
|
||||
|
||||
void _syncBlendMode(js.JsObject object) {
|
||||
final js.JsObject skBlendMode = makeSkBlendMode(_blendMode);
|
||||
object.callMethod('setBlendMode', <js.JsObject>[skBlendMode]);
|
||||
}
|
||||
|
||||
ui.BlendMode _blendMode = ui.BlendMode.srcOver;
|
||||
|
||||
@override
|
||||
@@ -37,6 +40,7 @@ class SkPaint extends SkiaObject implements ui.Paint {
|
||||
_style = value;
|
||||
_syncStyle(skiaObject);
|
||||
}
|
||||
|
||||
void _syncStyle(js.JsObject object) {
|
||||
js.JsObject skPaintStyle;
|
||||
switch (_style) {
|
||||
@@ -49,6 +53,7 @@ class SkPaint extends SkiaObject implements ui.Paint {
|
||||
}
|
||||
object.callMethod('setStyle', <js.JsObject>[skPaintStyle]);
|
||||
}
|
||||
|
||||
ui.PaintingStyle _style = ui.PaintingStyle.fill;
|
||||
|
||||
@override
|
||||
@@ -58,9 +63,11 @@ class SkPaint extends SkiaObject implements ui.Paint {
|
||||
_strokeWidth = value;
|
||||
_syncStrokeWidth(skiaObject);
|
||||
}
|
||||
|
||||
void _syncStrokeWidth(js.JsObject object) {
|
||||
object.callMethod('setStrokeWidth', <double>[strokeWidth]);
|
||||
}
|
||||
|
||||
double _strokeWidth = 0.0;
|
||||
|
||||
// TODO(yjbanov): implement
|
||||
@@ -70,6 +77,7 @@ class SkPaint extends SkiaObject implements ui.Paint {
|
||||
set strokeCap(ui.StrokeCap value) {
|
||||
_strokeCap = value;
|
||||
}
|
||||
|
||||
ui.StrokeCap _strokeCap = ui.StrokeCap.butt;
|
||||
|
||||
// TODO(yjbanov): implement
|
||||
@@ -79,6 +87,7 @@ class SkPaint extends SkiaObject implements ui.Paint {
|
||||
set strokeJoin(ui.StrokeJoin value) {
|
||||
_strokeJoin = value;
|
||||
}
|
||||
|
||||
ui.StrokeJoin _strokeJoin = ui.StrokeJoin.miter;
|
||||
|
||||
@override
|
||||
@@ -88,9 +97,11 @@ class SkPaint extends SkiaObject implements ui.Paint {
|
||||
_isAntiAlias = value;
|
||||
_syncAntiAlias(skiaObject);
|
||||
}
|
||||
|
||||
void _syncAntiAlias(js.JsObject object) {
|
||||
object.callMethod('setAntiAlias', <bool>[_isAntiAlias]);
|
||||
}
|
||||
|
||||
bool _isAntiAlias = true;
|
||||
|
||||
@override
|
||||
@@ -100,13 +111,17 @@ class SkPaint extends SkiaObject implements ui.Paint {
|
||||
_color = value;
|
||||
_syncColor(skiaObject);
|
||||
}
|
||||
|
||||
void _syncColor(js.JsObject object) {
|
||||
int colorValue = _defaultPaintColor.value;
|
||||
ui.Color colorValue = _defaultPaintColor;
|
||||
if (_color != null) {
|
||||
colorValue = _color.value;
|
||||
colorValue = _color;
|
||||
}
|
||||
object.callMethod('setColor', <int>[colorValue]);
|
||||
object.callMethod('setColorInt', <int>[
|
||||
colorValue.value,
|
||||
]);
|
||||
}
|
||||
|
||||
ui.Color _color = _defaultPaintColor;
|
||||
|
||||
// TODO(yjbanov): implement
|
||||
@@ -116,6 +131,7 @@ class SkPaint extends SkiaObject implements ui.Paint {
|
||||
set invertColors(bool value) {
|
||||
_invertColors = value;
|
||||
}
|
||||
|
||||
bool _invertColors = false;
|
||||
|
||||
@override
|
||||
@@ -125,6 +141,7 @@ class SkPaint extends SkiaObject implements ui.Paint {
|
||||
_shader = value as EngineShader;
|
||||
_syncShader(skiaObject);
|
||||
}
|
||||
|
||||
void _syncShader(js.JsObject object) {
|
||||
js.JsObject skShader;
|
||||
if (_shader != null) {
|
||||
@@ -132,6 +149,7 @@ class SkPaint extends SkiaObject implements ui.Paint {
|
||||
}
|
||||
object.callMethod('setShader', <js.JsObject>[skShader]);
|
||||
}
|
||||
|
||||
EngineShader _shader;
|
||||
|
||||
@override
|
||||
@@ -141,6 +159,7 @@ class SkPaint extends SkiaObject implements ui.Paint {
|
||||
_maskFilter = value;
|
||||
_syncMaskFilter(skiaObject);
|
||||
}
|
||||
|
||||
void _syncMaskFilter(js.JsObject object) {
|
||||
js.JsObject skMaskFilter;
|
||||
if (_maskFilter != null) {
|
||||
@@ -163,11 +182,12 @@ class SkPaint extends SkiaObject implements ui.Paint {
|
||||
break;
|
||||
}
|
||||
|
||||
skMaskFilter = canvasKit
|
||||
.callMethod('MakeBlurMaskFilter', <dynamic>[skBlurStyle, sigma, true]);
|
||||
skMaskFilter = canvasKit.callMethod(
|
||||
'MakeBlurMaskFilter', <dynamic>[skBlurStyle, sigma, true]);
|
||||
}
|
||||
object.callMethod('setMaskFilter', <js.JsObject>[skMaskFilter]);
|
||||
}
|
||||
|
||||
ui.MaskFilter _maskFilter;
|
||||
|
||||
// TODO(yjbanov): implement
|
||||
@@ -177,6 +197,7 @@ class SkPaint extends SkiaObject implements ui.Paint {
|
||||
set filterQuality(ui.FilterQuality value) {
|
||||
_filterQuality = value;
|
||||
}
|
||||
|
||||
ui.FilterQuality _filterQuality = ui.FilterQuality.none;
|
||||
|
||||
@override
|
||||
@@ -186,6 +207,7 @@ class SkPaint extends SkiaObject implements ui.Paint {
|
||||
_colorFilter = value;
|
||||
_syncColorFilter(skiaObject);
|
||||
}
|
||||
|
||||
void _syncColorFilter(js.JsObject object) {
|
||||
js.JsObject skColorFilterJs;
|
||||
if (_colorFilter != null) {
|
||||
@@ -194,6 +216,7 @@ class SkPaint extends SkiaObject implements ui.Paint {
|
||||
}
|
||||
object.callMethod('setColorFilter', <js.JsObject>[skColorFilterJs]);
|
||||
}
|
||||
|
||||
EngineColorFilter _colorFilter;
|
||||
|
||||
// TODO(yjbanov): implement
|
||||
@@ -203,6 +226,7 @@ class SkPaint extends SkiaObject implements ui.Paint {
|
||||
set strokeMiterLimit(double value) {
|
||||
_strokeMiterLimit = value;
|
||||
}
|
||||
|
||||
double _strokeMiterLimit = 0.0;
|
||||
|
||||
@override
|
||||
@@ -212,6 +236,7 @@ class SkPaint extends SkiaObject implements ui.Paint {
|
||||
_imageFilter = value;
|
||||
_syncImageFilter(skiaObject);
|
||||
}
|
||||
|
||||
void _syncImageFilter(js.JsObject object) {
|
||||
js.JsObject imageFilterJs;
|
||||
if (_imageFilter != null) {
|
||||
@@ -219,6 +244,7 @@ class SkPaint extends SkiaObject implements ui.Paint {
|
||||
}
|
||||
object.callMethod('setImageFilter', <js.JsObject>[imageFilterJs]);
|
||||
}
|
||||
|
||||
SkImageFilter _imageFilter;
|
||||
|
||||
@override
|
||||
|
||||
@@ -94,7 +94,8 @@ class SkPath implements ui.Path {
|
||||
@override
|
||||
void addPolygon(List<ui.Offset> points, bool close) {
|
||||
assert(points != null);
|
||||
final Float32List encodedPoints = encodePointList(points);
|
||||
// TODO(hterkelsen): https://github.com/flutter/flutter/issues/58824
|
||||
final List<List<double>> encodedPoints = encodePointList(points);
|
||||
_skPath.callMethod('addPoly', <dynamic>[encodedPoints, close]);
|
||||
}
|
||||
|
||||
|
||||
@@ -127,6 +127,7 @@ class Surface {
|
||||
grContext,
|
||||
size.width,
|
||||
size.height,
|
||||
canvasKit['SkColorSpace']['SRGB'],
|
||||
]);
|
||||
|
||||
htmlElement = htmlCanvas;
|
||||
|
||||
@@ -166,11 +166,13 @@ class SkTextStyle implements ui.TextStyle {
|
||||
final Map<String, dynamic> style = <String, dynamic>{};
|
||||
|
||||
if (background != null) {
|
||||
style['backgroundColor'] = background.skiaObject;
|
||||
setSharedSkColor2(background.color);
|
||||
style['backgroundColor'] = sharedSkColor2;
|
||||
}
|
||||
|
||||
if (color != null) {
|
||||
style['color'] = color.value;
|
||||
setSharedSkColor1(color);
|
||||
style['color'] = sharedSkColor1;
|
||||
}
|
||||
|
||||
if (decoration != null) {
|
||||
@@ -216,7 +218,8 @@ class SkTextStyle implements ui.TextStyle {
|
||||
}
|
||||
|
||||
if (foreground != null) {
|
||||
style['foreground'] = foreground.skiaObject;
|
||||
setSharedSkColor3(foreground.color);
|
||||
style['foregroundColor'] = sharedSkColor3;
|
||||
}
|
||||
|
||||
// TODO(hterkelsen): Add support for
|
||||
|
||||
@@ -96,6 +96,70 @@ class SkiaObjects {
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts a list of [ui.Color] into the 2d array expected by CanvasKit.
|
||||
js.JsArray<Float32List> makeColorList(List<ui.Color> colors) {
|
||||
var result = js.JsArray<Float32List>();
|
||||
result.length = colors.length;
|
||||
for (var i = 0; i < colors.length; i++) {
|
||||
var color = colors[i];
|
||||
var jsColor = Float32List(4);
|
||||
jsColor[0] = color.red / 255.0;
|
||||
jsColor[1] = color.green / 255.0;
|
||||
jsColor[2] = color.blue / 255.0;
|
||||
jsColor[3] = color.alpha / 255.0;
|
||||
result[i] = jsColor;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
js.JsObject _mallocColorArray() {
|
||||
return canvasKit
|
||||
.callMethod('Malloc', <dynamic>[js.context['Float32Array'], 4]);
|
||||
}
|
||||
|
||||
js.JsObject sharedSkColor1;
|
||||
js.JsObject sharedSkColor2;
|
||||
js.JsObject sharedSkColor3;
|
||||
|
||||
void _setSharedColor(js.JsObject sharedColor, ui.Color color) {
|
||||
Float32List array = sharedColor.callMethod('toTypedArray');
|
||||
array[0] = color.red / 255.0;
|
||||
array[1] = color.green / 255.0;
|
||||
array[2] = color.blue / 255.0;
|
||||
array[3] = color.alpha / 255.0;
|
||||
}
|
||||
|
||||
void setSharedSkColor1(ui.Color color) {
|
||||
if (sharedSkColor1 == null) {
|
||||
sharedSkColor1 = _mallocColorArray();
|
||||
}
|
||||
_setSharedColor(sharedSkColor1, color);
|
||||
}
|
||||
|
||||
void setSharedSkColor2(ui.Color color) {
|
||||
if (sharedSkColor2 == null) {
|
||||
sharedSkColor2 = _mallocColorArray();
|
||||
}
|
||||
_setSharedColor(sharedSkColor2, color);
|
||||
}
|
||||
|
||||
void setSharedSkColor3(ui.Color color) {
|
||||
if (sharedSkColor3 == null) {
|
||||
sharedSkColor3 = _mallocColorArray();
|
||||
}
|
||||
_setSharedColor(sharedSkColor3, color);
|
||||
}
|
||||
|
||||
/// Creates a new color array.
|
||||
Float32List makeFreshSkColor(ui.Color color) {
|
||||
final Float32List result = Float32List(4);
|
||||
result[0] = color.red / 255.0;
|
||||
result[1] = color.green / 255.0;
|
||||
result[2] = color.blue / 255.0;
|
||||
result[3] = color.alpha / 255.0;
|
||||
return result;
|
||||
}
|
||||
|
||||
js.JsObject makeSkRect(ui.Rect rect) {
|
||||
return js.JsObject(canvasKit['LTRBRect'],
|
||||
<double>[rect.left, rect.top, rect.right, rect.bottom]);
|
||||
@@ -148,18 +212,47 @@ js.JsArray<double> makeSkPoint(ui.Offset point) {
|
||||
return skPoint;
|
||||
}
|
||||
|
||||
/// Creates a point list using a typed buffer created by CanvasKit.Malloc.
|
||||
Float32List encodePointList(List<ui.Offset> points) {
|
||||
assert(points != null);
|
||||
// TODO(hterkelsen): https://github.com/flutter/flutter/issues/58824
|
||||
/// Creates a point list using a 2D JS array.
|
||||
js.JsArray<js.JsArray<double>> encodePointList(List<ui.Offset> points) {
|
||||
if (points == null) {
|
||||
return null;
|
||||
}
|
||||
final int pointCount = points.length;
|
||||
final Float32List result = canvasKit.callMethod('Malloc', <dynamic>[js.context['Float32Array'], pointCount * 2]);
|
||||
final js.JsArray<js.JsArray<double>> result =
|
||||
js.JsArray<js.JsArray<double>>();
|
||||
result.length = pointCount;
|
||||
for (int i = 0; i < pointCount; ++i) {
|
||||
final int xIndex = i * 2;
|
||||
final int yIndex = xIndex + 1;
|
||||
final ui.Offset point = points[i];
|
||||
assert(_offsetIsValid(point));
|
||||
result[xIndex] = point.dx;
|
||||
result[yIndex] = point.dy;
|
||||
final js.JsArray<double> skPoint = js.JsArray<double>();
|
||||
skPoint.length = 2;
|
||||
skPoint[0] = point.dx;
|
||||
skPoint[1] = point.dy;
|
||||
result[i] = skPoint;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// TODO(hterkelsen): https://github.com/flutter/flutter/issues/58824
|
||||
/// Creates a point list using a 2D JS array.
|
||||
List<List<double>> encodeRawPointList(Float32List points) {
|
||||
if (points == null) {
|
||||
return null;
|
||||
}
|
||||
assert(points.length % 2 == 0);
|
||||
var pointLength = points.length ~/ 2;
|
||||
final js.JsArray<js.JsArray<double>> result =
|
||||
js.JsArray<js.JsArray<double>>();
|
||||
result.length = pointLength;
|
||||
for (var i = 0; i < pointLength; i++) {
|
||||
var x = i * 2;
|
||||
var y = x + 1;
|
||||
final js.JsArray<double> skPoint = js.JsArray<double>();
|
||||
skPoint.length = 2;
|
||||
skPoint[0] = points[x];
|
||||
skPoint[1] = points[y];
|
||||
result[i] = skPoint;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -319,9 +412,9 @@ void drawSkShadow(
|
||||
ui.Color inAmbient = color.withAlpha((color.alpha * ambientAlpha).round());
|
||||
ui.Color inSpot = color.withAlpha((color.alpha * spotAlpha).round());
|
||||
|
||||
final js.JsObject inTonalColors = js.JsObject.jsify(<String, int>{
|
||||
'ambient': inAmbient.value,
|
||||
'spot': inSpot.value,
|
||||
final js.JsObject inTonalColors = js.JsObject.jsify(<String, Float32List>{
|
||||
'ambient': makeFreshSkColor(inAmbient),
|
||||
'spot': makeFreshSkColor(inSpot),
|
||||
});
|
||||
|
||||
final js.JsObject tonalColors =
|
||||
|
||||
@@ -5,22 +5,17 @@
|
||||
// @dart = 2.6
|
||||
part of engine;
|
||||
|
||||
Int32List _encodeColorList(List<ui.Color> colors) {
|
||||
final int colorCount = colors.length;
|
||||
final Int32List result = Int32List(colorCount);
|
||||
js.JsArray<Float32List> _encodeRawColorList(Int32List rawColors) {
|
||||
final int colorCount = rawColors.length;
|
||||
final List<ui.Color> colors = List<ui.Color>(colorCount);
|
||||
for (int i = 0; i < colorCount; ++i) {
|
||||
result[i] = colors[i].value;
|
||||
colors[i] = ui.Color(rawColors[i]);
|
||||
}
|
||||
return result;
|
||||
return makeColorList(colors);
|
||||
}
|
||||
|
||||
class SkVertices implements ui.Vertices {
|
||||
js.JsObject skVertices;
|
||||
final Int32List _colors;
|
||||
final Float32List _positions;
|
||||
final ui.VertexMode _mode;
|
||||
final Float32List _textureCoordinates;
|
||||
final Uint16List _indices;
|
||||
|
||||
SkVertices(
|
||||
ui.VertexMode mode,
|
||||
@@ -29,14 +24,7 @@ class SkVertices implements ui.Vertices {
|
||||
List<ui.Color> colors,
|
||||
List<int> indices,
|
||||
}) : assert(mode != null),
|
||||
assert(positions != null),
|
||||
_colors =
|
||||
Int32List.fromList(colors.map((ui.Color c) => c.value).toList()),
|
||||
_positions = encodePointList(positions),
|
||||
_mode = mode,
|
||||
_textureCoordinates = (textureCoordinates != null)
|
||||
? encodePointList(textureCoordinates) : null,
|
||||
_indices = indices != null ? Uint16List.fromList(indices) : null {
|
||||
assert(positions != null) {
|
||||
if (textureCoordinates != null &&
|
||||
textureCoordinates.length != positions.length)
|
||||
throw ArgumentError(
|
||||
@@ -48,11 +36,12 @@ class SkVertices implements ui.Vertices {
|
||||
throw ArgumentError(
|
||||
'"indices" values must be valid indices in the positions list.');
|
||||
|
||||
final Float32List encodedPositions = encodePointList(positions);
|
||||
final Int32List encodedColors =
|
||||
colors != null ? _encodeColorList(colors) : null;
|
||||
if (!_init(mode, encodedPositions, _textureCoordinates, encodedColors,
|
||||
_indices))
|
||||
final js.JsArray<js.JsArray<double>> encodedPositions = encodePointList(positions);
|
||||
final js.JsArray<js.JsArray<double>> encodedTextures =
|
||||
encodePointList(textureCoordinates);
|
||||
final js.JsArray<Float32List> encodedColors =
|
||||
colors != null ? makeColorList(colors) : null;
|
||||
if (!_init(mode, encodedPositions, encodedTextures, encodedColors, indices))
|
||||
throw ArgumentError('Invalid configuration for vertices.');
|
||||
}
|
||||
|
||||
@@ -63,12 +52,7 @@ class SkVertices implements ui.Vertices {
|
||||
Int32List colors,
|
||||
Uint16List indices,
|
||||
}) : assert(mode != null),
|
||||
assert(positions != null),
|
||||
_colors = colors,
|
||||
_positions = positions,
|
||||
_mode = mode,
|
||||
_textureCoordinates = textureCoordinates,
|
||||
_indices = indices {
|
||||
assert(positions != null) {
|
||||
if (textureCoordinates != null &&
|
||||
textureCoordinates.length != positions.length)
|
||||
throw ArgumentError(
|
||||
@@ -80,12 +64,23 @@ class SkVertices implements ui.Vertices {
|
||||
throw ArgumentError(
|
||||
'"indices" values must be valid indices in the positions list.');
|
||||
|
||||
if (!_init(mode, positions, textureCoordinates, colors, indices))
|
||||
if (!_init(
|
||||
mode,
|
||||
encodeRawPointList(positions),
|
||||
encodeRawPointList(textureCoordinates),
|
||||
_encodeRawColorList(colors),
|
||||
indices,
|
||||
)) {
|
||||
throw ArgumentError('Invalid configuration for vertices.');
|
||||
}
|
||||
}
|
||||
|
||||
bool _init(ui.VertexMode mode, Float32List positions,
|
||||
Float32List textureCoordinates, Int32List colors, Uint16List indices) {
|
||||
bool _init(
|
||||
ui.VertexMode mode,
|
||||
js.JsArray<js.JsArray<double>> positions,
|
||||
js.JsArray<js.JsArray<double>> textureCoordinates,
|
||||
js.JsArray<Float32List> colors,
|
||||
List<int> indices) {
|
||||
js.JsObject skVertexMode;
|
||||
switch (mode) {
|
||||
case ui.VertexMode.triangles:
|
||||
@@ -102,8 +97,8 @@ class SkVertices implements ui.Vertices {
|
||||
final js.JsObject vertices =
|
||||
canvasKit.callMethod('MakeSkVertices', <dynamic>[
|
||||
skVertexMode,
|
||||
_encodePoints(positions),
|
||||
_encodePoints(textureCoordinates),
|
||||
positions,
|
||||
textureCoordinates,
|
||||
colors,
|
||||
indices,
|
||||
]);
|
||||
@@ -116,30 +111,16 @@ class SkVertices implements ui.Vertices {
|
||||
}
|
||||
}
|
||||
|
||||
static js.JsArray<js.JsArray<double>> _encodePoints(List<double> points) {
|
||||
if (points == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
js.JsArray<js.JsArray<double>> encodedPoints =
|
||||
js.JsArray<js.JsArray<double>>();
|
||||
encodedPoints.length = points.length ~/ 2;
|
||||
for (int i = 0; i < points.length; i += 2) {
|
||||
encodedPoints[i ~/ 2] = makeSkPoint(ui.Offset(points[i], points[i + 1]));
|
||||
}
|
||||
return encodedPoints;
|
||||
}
|
||||
@override
|
||||
Int32List get colors => null;
|
||||
|
||||
@override
|
||||
Int32List get colors => _colors;
|
||||
Float32List get positions => null;
|
||||
|
||||
@override
|
||||
Float32List get positions => _positions;
|
||||
ui.VertexMode get mode => null;
|
||||
|
||||
@override
|
||||
ui.VertexMode get mode => _mode;
|
||||
Float32List get textureCoordinates => null;
|
||||
|
||||
Float32List get textureCoordinates => _textureCoordinates;
|
||||
|
||||
Uint16List get indices => _indices;
|
||||
Uint16List get indices => null;
|
||||
}
|
||||
|
||||
@@ -82,12 +82,12 @@ void _validateColorStops(List<ui.Color> colors, List<double> colorStops) {
|
||||
|
||||
class GradientLinear extends EngineGradient {
|
||||
GradientLinear(
|
||||
this.from,
|
||||
this.to,
|
||||
this.colors,
|
||||
this.colorStops,
|
||||
this.tileMode,
|
||||
Float64List matrix,
|
||||
this.from,
|
||||
this.to,
|
||||
this.colors,
|
||||
this.colorStops,
|
||||
this.tileMode,
|
||||
Float64List matrix,
|
||||
) : assert(_offsetIsValid(from)),
|
||||
assert(_offsetIsValid(to)),
|
||||
assert(colors != null),
|
||||
@@ -157,13 +157,9 @@ class GradientLinear extends EngineGradient {
|
||||
js.JsObject createSkiaShader() {
|
||||
assert(experimentalUseSkia);
|
||||
|
||||
final js.JsArray<num> jsColors = js.JsArray<num>();
|
||||
jsColors.length = colors.length;
|
||||
for (int i = 0; i < colors.length; i++) {
|
||||
jsColors[i] = colors[i].value;
|
||||
}
|
||||
var jsColors = makeColorList(colors);
|
||||
|
||||
return canvasKit.callMethod('MakeLinearGradientShader', <dynamic>[
|
||||
return canvasKit['SkShader'].callMethod('MakeLinearGradient', <dynamic>[
|
||||
makeSkPoint(from),
|
||||
makeSkPoint(to),
|
||||
jsColors,
|
||||
@@ -216,13 +212,9 @@ class GradientRadial extends EngineGradient {
|
||||
js.JsObject createSkiaShader() {
|
||||
assert(experimentalUseSkia);
|
||||
|
||||
final js.JsArray<num> jsColors = js.JsArray<num>();
|
||||
jsColors.length = colors.length;
|
||||
for (int i = 0; i < colors.length; i++) {
|
||||
jsColors[i] = colors[i].value;
|
||||
}
|
||||
var jsColors = makeColorList(colors);
|
||||
|
||||
return canvasKit.callMethod('MakeRadialGradientShader', <dynamic>[
|
||||
return canvasKit['SkShader'].callMethod('MakeRadialGradient', <dynamic>[
|
||||
makeSkPoint(center),
|
||||
radius,
|
||||
jsColors,
|
||||
@@ -257,13 +249,10 @@ class GradientConical extends EngineGradient {
|
||||
js.JsObject createSkiaShader() {
|
||||
assert(experimentalUseSkia);
|
||||
|
||||
final js.JsArray<num> jsColors = js.JsArray<num>();
|
||||
jsColors.length = colors.length;
|
||||
for (int i = 0; i < colors.length; i++) {
|
||||
jsColors[i] = colors[i].value;
|
||||
}
|
||||
var jsColors = makeColorList(colors);
|
||||
|
||||
return canvasKit.callMethod('MakeTwoPointConicalGradient', <dynamic>[
|
||||
return canvasKit['SkShader']
|
||||
.callMethod('MakeTwoPointConicalGradient', <dynamic>[
|
||||
makeSkPoint(focal),
|
||||
focalRadius,
|
||||
makeSkPoint(center),
|
||||
@@ -305,7 +294,7 @@ class EngineImageFilter implements ui.ImageFilter {
|
||||
}
|
||||
|
||||
js.JsObject _skTileMode(ui.TileMode tileMode) {
|
||||
switch(tileMode) {
|
||||
switch (tileMode) {
|
||||
case ui.TileMode.clamp:
|
||||
return canvasKit['TileMode']['Clamp'];
|
||||
case ui.TileMode.repeated:
|
||||
@@ -318,14 +307,15 @@ js.JsObject _skTileMode(ui.TileMode tileMode) {
|
||||
|
||||
/// Backend implementation of [ui.ImageShader].
|
||||
class EngineImageShader implements ui.ImageShader, EngineShader {
|
||||
EngineImageShader(ui.Image image, this.tileModeX, this.tileModeY,
|
||||
this.matrix4) : _skImage = image as SkImage;
|
||||
EngineImageShader(
|
||||
ui.Image image, this.tileModeX, this.tileModeY, this.matrix4)
|
||||
: _skImage = image as SkImage;
|
||||
|
||||
final ui.TileMode tileModeX;
|
||||
final ui.TileMode tileModeY;
|
||||
final Float64List matrix4;
|
||||
final SkImage _skImage;
|
||||
|
||||
js.JsObject createSkiaShader() => _skImage.skImage.callMethod('makeShader',
|
||||
<dynamic>[_skTileMode(tileModeX), _skTileMode(tileModeY)]);
|
||||
js.JsObject createSkiaShader() => _skImage.skImage.callMethod(
|
||||
'makeShader', <dynamic>[_skTileMode(tileModeX), _skTileMode(tileModeY)]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user