Refactor CanvasKit backend in preparation for platform views (flutter/engine#13935)
* SkCanvas -> SkLayerCanvas SkRecordingCanvas -> SkCanvas * Move files * WIP on canvas refactor * Refactor CanvasKit backend * fix tests * update licenses * Respond to PR comments
This commit is contained in:
@@ -365,6 +365,7 @@ FILE: ../../../flutter/lib/web_ui/lib/src/engine/browser_detection.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/browser_location.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/color_filter.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/compositor/canvas.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/compositor/canvas_kit_canvas.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/compositor/color_filter.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/compositor/engine_delegate.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/compositor/fonts.dart
|
||||
@@ -381,7 +382,6 @@ FILE: ../../../flutter/lib/web_ui/lib/src/engine/compositor/picture_recorder.dar
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/compositor/platform_message.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/compositor/raster_cache.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/compositor/rasterizer.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/compositor/recording_canvas.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/compositor/runtime_delegate.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/compositor/surface.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/compositor/text.dart
|
||||
@@ -398,6 +398,7 @@ FILE: ../../../flutter/lib/web_ui/lib/src/engine/html_image_codec.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/keyboard.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/onscreen_logging.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/path_to_svg.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/picture.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/platform_views.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/plugins.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/pointer_binding.dart
|
||||
|
||||
@@ -25,6 +25,7 @@ part 'engine/browser_detection.dart';
|
||||
part 'engine/browser_location.dart';
|
||||
part 'engine/color_filter.dart';
|
||||
part 'engine/compositor/canvas.dart';
|
||||
part 'engine/compositor/canvas_kit_canvas.dart';
|
||||
part 'engine/compositor/color_filter.dart';
|
||||
part 'engine/compositor/engine_delegate.dart';
|
||||
part 'engine/compositor/fonts.dart';
|
||||
@@ -41,7 +42,6 @@ part 'engine/compositor/picture_recorder.dart';
|
||||
part 'engine/compositor/platform_message.dart';
|
||||
part 'engine/compositor/raster_cache.dart';
|
||||
part 'engine/compositor/rasterizer.dart';
|
||||
part 'engine/compositor/recording_canvas.dart';
|
||||
part 'engine/compositor/runtime_delegate.dart';
|
||||
part 'engine/compositor/surface.dart';
|
||||
part 'engine/compositor/text.dart';
|
||||
@@ -58,6 +58,7 @@ part 'engine/html_image_codec.dart';
|
||||
part 'engine/keyboard.dart';
|
||||
part 'engine/onscreen_logging.dart';
|
||||
part 'engine/path_to_svg.dart';
|
||||
part 'engine/picture.dart';
|
||||
part 'engine/platform_views.dart';
|
||||
part 'engine/plugins.dart';
|
||||
part 'engine/pointer_binding.dart';
|
||||
|
||||
@@ -743,7 +743,8 @@ class BitmapCanvas extends EngineCanvas with SaveStackTracking {
|
||||
|
||||
/// Paints the [picture] into this canvas.
|
||||
void drawPicture(ui.Picture picture) {
|
||||
picture.recordingCanvas.apply(this);
|
||||
final EnginePicture enginePicture = picture;
|
||||
enginePicture.recordingCanvas.apply(this);
|
||||
}
|
||||
|
||||
/// Draws vertices on a gl context.
|
||||
|
||||
@@ -4,36 +4,247 @@
|
||||
|
||||
part of engine;
|
||||
|
||||
/// An actual [SkCanvas] which can receive raw drawing commands.
|
||||
///
|
||||
/// In order for the drawing commands to be flushed to the associated HTML
|
||||
/// canvas, you must call `flush()` on the canvas's `SkSurface`.
|
||||
///
|
||||
/// Although this class is backed by an `SkCanvas` and can in theory perform
|
||||
/// arbitrary drawing operations, this class is only used in the final
|
||||
/// compositing by the layers, and arbitrary drawings are done in a
|
||||
/// [ui.Picture] which uses a Skia recording canvas. This class receives
|
||||
/// drawing calls from the various `Layer` classes, e.g. [ClipRectLayer] and
|
||||
/// so only exposes a subset of the drawing operations that can be performed
|
||||
/// on a canvas.
|
||||
/// A Dart wrapper around Skia's SKCanvas.
|
||||
class SkCanvas {
|
||||
final js.JsObject skCanvas;
|
||||
final html.CanvasElement htmlCanvas;
|
||||
final js.JsObject skSurface;
|
||||
final ui.Size size;
|
||||
|
||||
SkCanvas(this.skCanvas, this.htmlCanvas, this.skSurface, this.size);
|
||||
SkCanvas(this.skCanvas);
|
||||
|
||||
int get saveCount => skCanvas.callMethod('getSaveCount');
|
||||
|
||||
void clipPath(ui.Path path, bool doAntiAlias) {
|
||||
final SkPath skPath = path;
|
||||
final js.JsObject intersectClipOp = canvasKit['ClipOp']['Intersect'];
|
||||
skCanvas.callMethod('clipPath', <dynamic>[
|
||||
skPath._skPath,
|
||||
intersectClipOp,
|
||||
doAntiAlias,
|
||||
]);
|
||||
}
|
||||
|
||||
void clipRRect(ui.RRect rrect, bool doAntiAlias) {
|
||||
final js.JsObject intersectClipOp = canvasKit['ClipOp']['Intersect'];
|
||||
skCanvas.callMethod('clipRRect', <dynamic>[
|
||||
makeSkRRect(rrect),
|
||||
intersectClipOp,
|
||||
doAntiAlias,
|
||||
]);
|
||||
}
|
||||
|
||||
void clipRect(ui.Rect rect, ui.ClipOp clipOp, bool doAntiAlias) {
|
||||
js.JsObject skClipOp;
|
||||
switch (clipOp) {
|
||||
case ui.ClipOp.difference:
|
||||
skClipOp = canvasKit['ClipOp']['Difference'];
|
||||
break;
|
||||
case ui.ClipOp.intersect:
|
||||
skClipOp = canvasKit['ClipOp']['Intersect'];
|
||||
break;
|
||||
}
|
||||
|
||||
skCanvas.callMethod(
|
||||
'clipRect', <dynamic>[makeSkRect(rect), skClipOp, doAntiAlias]);
|
||||
}
|
||||
|
||||
void drawArc(
|
||||
ui.Rect oval,
|
||||
double startAngle,
|
||||
double sweepAngle,
|
||||
bool useCenter,
|
||||
ui.Paint paint,
|
||||
) {
|
||||
skCanvas.callMethod('drawArc', <dynamic>[
|
||||
makeSkRect(oval),
|
||||
startAngle,
|
||||
sweepAngle,
|
||||
useCenter,
|
||||
makeSkPaint(paint),
|
||||
]);
|
||||
}
|
||||
|
||||
void drawAtlasRaw(
|
||||
ui.Paint paint,
|
||||
ui.Image atlas,
|
||||
Float32List rstTransforms,
|
||||
Float32List rects,
|
||||
Int32List colors,
|
||||
ui.BlendMode blendMode,
|
||||
) {
|
||||
final SkImage skAtlas = atlas;
|
||||
skCanvas.callMethod('drawAtlas', <dynamic>[
|
||||
skAtlas.skImage,
|
||||
rects,
|
||||
rstTransforms,
|
||||
makeSkPaint(paint),
|
||||
makeSkBlendMode(blendMode),
|
||||
colors,
|
||||
]);
|
||||
}
|
||||
|
||||
void drawCircle(ui.Offset c, double radius, ui.Paint paint) {
|
||||
skCanvas.callMethod('drawCircle', <dynamic>[
|
||||
c.dx,
|
||||
c.dy,
|
||||
radius,
|
||||
makeSkPaint(paint),
|
||||
]);
|
||||
}
|
||||
|
||||
void drawColor(ui.Color color, ui.BlendMode blendMode) {
|
||||
skCanvas.callMethod('drawColor', <dynamic>[
|
||||
color.value,
|
||||
makeSkBlendMode(blendMode),
|
||||
]);
|
||||
}
|
||||
|
||||
void drawDRRect(ui.RRect outer, ui.RRect inner, ui.Paint paint) {
|
||||
skCanvas.callMethod('drawDRRect', <js.JsObject>[
|
||||
makeSkRRect(outer),
|
||||
makeSkRRect(inner),
|
||||
makeSkPaint(paint),
|
||||
]);
|
||||
}
|
||||
|
||||
void drawImage(ui.Image image, ui.Offset offset, ui.Paint paint) {
|
||||
final SkImage skImage = image;
|
||||
skCanvas.callMethod('drawImage', <dynamic>[
|
||||
skImage.skImage,
|
||||
offset.dx,
|
||||
offset.dy,
|
||||
makeSkPaint(paint),
|
||||
]);
|
||||
}
|
||||
|
||||
void drawImageRect(ui.Image image, ui.Rect src, ui.Rect dst, ui.Paint paint) {
|
||||
final SkImage skImage = image;
|
||||
skCanvas.callMethod('drawImageRect', <dynamic>[
|
||||
skImage.skImage,
|
||||
makeSkRect(src),
|
||||
makeSkRect(dst),
|
||||
makeSkPaint(paint),
|
||||
false,
|
||||
]);
|
||||
}
|
||||
|
||||
void drawImageNine(
|
||||
ui.Image image, ui.Rect center, ui.Rect dst, ui.Paint paint) {
|
||||
final SkImage skImage = image;
|
||||
skCanvas.callMethod('drawImageNine', <dynamic>[
|
||||
skImage.skImage,
|
||||
makeSkRect(center),
|
||||
makeSkRect(dst),
|
||||
makeSkPaint(paint),
|
||||
]);
|
||||
}
|
||||
|
||||
void drawLine(ui.Offset p1, ui.Offset p2, ui.Paint paint) {
|
||||
skCanvas.callMethod('drawLine', <dynamic>[
|
||||
p1.dx,
|
||||
p1.dy,
|
||||
p2.dx,
|
||||
p2.dy,
|
||||
makeSkPaint(paint),
|
||||
]);
|
||||
}
|
||||
|
||||
void drawOval(ui.Rect rect, ui.Paint paint) {
|
||||
skCanvas.callMethod('drawOval', <js.JsObject>[
|
||||
makeSkRect(rect),
|
||||
makeSkPaint(paint),
|
||||
]);
|
||||
}
|
||||
|
||||
void drawPaint(ui.Paint paint) {
|
||||
skCanvas.callMethod('drawPaint', <js.JsObject>[makeSkPaint(paint)]);
|
||||
}
|
||||
|
||||
void drawParagraph(ui.Paragraph paragraph, ui.Offset offset) {
|
||||
final SkParagraph skParagraph = paragraph;
|
||||
skCanvas.callMethod('drawParagraph', <dynamic>[
|
||||
skParagraph.skParagraph,
|
||||
offset.dx,
|
||||
offset.dy,
|
||||
]);
|
||||
}
|
||||
|
||||
void drawPath(ui.Path path, ui.Paint paint) {
|
||||
final js.JsObject skPaint = makeSkPaint(paint);
|
||||
final SkPath enginePath = path;
|
||||
final js.JsObject skPath = enginePath._skPath;
|
||||
skCanvas.callMethod('drawPath', <js.JsObject>[skPath, skPaint]);
|
||||
}
|
||||
|
||||
void drawPicture(ui.Picture picture) {
|
||||
final SkPicture skPicture = picture;
|
||||
skCanvas.callMethod('drawPicture', <js.JsObject>[skPicture.skPicture]);
|
||||
}
|
||||
|
||||
void drawPoints(ui.Paint paint, ui.PointMode pointMode, Float32List points) {
|
||||
skCanvas.callMethod('drawPoints', <dynamic>[
|
||||
makeSkPointMode(pointMode),
|
||||
points,
|
||||
makeSkPaint(paint),
|
||||
]);
|
||||
}
|
||||
|
||||
void drawRRect(ui.RRect rrect, ui.Paint paint) {
|
||||
skCanvas.callMethod('drawRRect', <js.JsObject>[
|
||||
makeSkRRect(rrect),
|
||||
makeSkPaint(paint),
|
||||
]);
|
||||
}
|
||||
|
||||
void drawRect(ui.Rect rect, ui.Paint paint) {
|
||||
final js.JsObject skRect = makeSkRect(rect);
|
||||
final js.JsObject skPaint = makeSkPaint(paint);
|
||||
skCanvas.callMethod('drawRect', <js.JsObject>[skRect, skPaint]);
|
||||
}
|
||||
|
||||
void drawShadow(ui.Path path, ui.Color color, double elevation,
|
||||
bool transparentOccluder) {
|
||||
drawSkShadow(skCanvas, path, color, elevation, transparentOccluder,
|
||||
ui.window.devicePixelRatio);
|
||||
}
|
||||
|
||||
void drawVertices(
|
||||
ui.Vertices vertices, ui.BlendMode blendMode, ui.Paint paint) {
|
||||
SkVertices skVertices = vertices;
|
||||
skCanvas.callMethod('drawVertices', <js.JsObject>[
|
||||
skVertices.skVertices,
|
||||
makeSkBlendMode(blendMode),
|
||||
makeSkPaint(paint)
|
||||
]);
|
||||
}
|
||||
|
||||
void restore() {
|
||||
skCanvas.callMethod('restore');
|
||||
}
|
||||
|
||||
void restoreToCount(int count) {
|
||||
skCanvas.callMethod('restoreToCount', <int>[count]);
|
||||
}
|
||||
|
||||
void rotate(double radians) {
|
||||
skCanvas
|
||||
.callMethod('rotate', <double>[radians * 180.0 / math.pi, 0.0, 0.0]);
|
||||
}
|
||||
|
||||
int save() {
|
||||
return skCanvas.callMethod('save');
|
||||
}
|
||||
|
||||
int saveLayer(ui.Rect bounds, ui.Paint paint) {
|
||||
return skCanvas.callMethod(
|
||||
'saveLayer', <js.JsObject>[makeSkRect(bounds), makeSkPaint(paint)]);
|
||||
void saveLayer(ui.Rect bounds, ui.Paint paint) {
|
||||
skCanvas.callMethod('saveLayer', <js.JsObject>[
|
||||
makeSkRect(bounds),
|
||||
makeSkPaint(paint),
|
||||
]);
|
||||
}
|
||||
|
||||
int saveLayerWithFilter(ui.Rect bounds, ui.ImageFilter filter) {
|
||||
void saveLayerWithoutBounds(ui.Paint paint) {
|
||||
skCanvas.callMethod('saveLayer', <js.JsObject>[null, makeSkPaint(paint)]);
|
||||
}
|
||||
|
||||
void saveLayerWithFilter(ui.Rect bounds, ui.ImageFilter filter) {
|
||||
final SkImageFilter skImageFilter = filter;
|
||||
return skCanvas.callMethod(
|
||||
'saveLayer',
|
||||
@@ -46,69 +257,23 @@ class SkCanvas {
|
||||
);
|
||||
}
|
||||
|
||||
void restore() {
|
||||
skCanvas.callMethod('restore');
|
||||
void scale(double sx, double sy) {
|
||||
skCanvas.callMethod('scale', <double>[sx, sy]);
|
||||
}
|
||||
|
||||
void restoreToCount(int count) {
|
||||
skCanvas.callMethod('restoreToCount', <int>[count]);
|
||||
void skew(double sx, double sy) {
|
||||
skCanvas.callMethod('skew', <double>[sx, sy]);
|
||||
}
|
||||
|
||||
void clear() {
|
||||
skCanvas.callMethod('clear', <int>[0xffffffff]);
|
||||
void transform(Float64List matrix4) {
|
||||
skCanvas.callMethod('concat', <js.JsArray<double>>[makeSkMatrix(matrix4)]);
|
||||
}
|
||||
|
||||
void translate(double dx, double dy) {
|
||||
skCanvas.callMethod('translate', <double>[dx, dy]);
|
||||
}
|
||||
|
||||
void transform(Float64List matrix) {
|
||||
skCanvas.callMethod('concat', <js.JsArray<double>>[makeSkMatrix(matrix)]);
|
||||
}
|
||||
|
||||
void clipPath(ui.Path path, {bool doAntiAlias = true}) {
|
||||
final SkPath skPath = path;
|
||||
final js.JsObject intersectClipOp = canvasKit['ClipOp']['Intersect'];
|
||||
skCanvas.callMethod('clipPath', <dynamic>[
|
||||
skPath._skPath,
|
||||
intersectClipOp,
|
||||
doAntiAlias,
|
||||
]);
|
||||
}
|
||||
|
||||
void clipRect(ui.Rect rect, {bool doAntiAlias = true}) {
|
||||
final js.JsObject intersectClipOp = canvasKit['ClipOp']['Intersect'];
|
||||
skCanvas.callMethod('clipRect', <dynamic>[
|
||||
makeSkRect(rect),
|
||||
intersectClipOp,
|
||||
doAntiAlias,
|
||||
]);
|
||||
}
|
||||
|
||||
void clipRRect(ui.RRect rrect) {
|
||||
final SkPath skPath = SkPath();
|
||||
skPath.addRRect(rrect);
|
||||
clipPath(skPath);
|
||||
}
|
||||
|
||||
void drawPicture(ui.Picture picture) {
|
||||
final SkPicture skPicture = picture;
|
||||
skCanvas.callMethod('drawPicture', <js.JsObject>[skPicture.skPicture]);
|
||||
}
|
||||
|
||||
void drawPath(ui.Path path, ui.Paint paint) {
|
||||
final SkPath skPath = path;
|
||||
skCanvas.callMethod(
|
||||
'drawPath', <js.JsObject>[skPath._skPath, makeSkPaint(paint)]);
|
||||
}
|
||||
|
||||
void drawPaint(ui.Paint paint) {
|
||||
skCanvas.callMethod('drawPaint', <js.JsObject>[makeSkPaint(paint)]);
|
||||
}
|
||||
|
||||
void drawShadow(ui.Path path, ui.Color color, double elevation,
|
||||
bool transparentOccluder) {
|
||||
drawSkShadow(skCanvas, path, color, elevation, transparentOccluder,
|
||||
ui.window.devicePixelRatio);
|
||||
void flush() {
|
||||
skCanvas.callMethod('flush');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,449 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
part of engine;
|
||||
|
||||
/// An implementation of [ui.Canvas] that is backed by a CanvasKit canvas.
|
||||
class CanvasKitCanvas implements ui.Canvas {
|
||||
final SkCanvas _canvas;
|
||||
|
||||
factory CanvasKitCanvas(ui.PictureRecorder recorder, [ui.Rect cullRect]) {
|
||||
assert(recorder != null);
|
||||
if (recorder.isRecording) {
|
||||
throw ArgumentError(
|
||||
'"recorder" must not already be associated with another Canvas.');
|
||||
}
|
||||
cullRect ??= ui.Rect.largest;
|
||||
final SkPictureRecorder skRecorder = recorder;
|
||||
return CanvasKitCanvas._(skRecorder.beginRecording(cullRect));
|
||||
}
|
||||
|
||||
CanvasKitCanvas._(this._canvas);
|
||||
|
||||
@override
|
||||
void save() {
|
||||
_canvas.save();
|
||||
}
|
||||
|
||||
@override
|
||||
void saveLayer(ui.Rect bounds, ui.Paint paint) {
|
||||
assert(paint != null);
|
||||
if (bounds == null) {
|
||||
_saveLayerWithoutBounds(paint);
|
||||
} else {
|
||||
assert(rectIsValid(bounds));
|
||||
_saveLayer(bounds, paint);
|
||||
}
|
||||
}
|
||||
|
||||
void _saveLayerWithoutBounds(ui.Paint paint) {
|
||||
_canvas.saveLayerWithoutBounds(paint);
|
||||
}
|
||||
|
||||
void _saveLayer(ui.Rect bounds, ui.Paint paint) {
|
||||
_canvas.saveLayer(bounds, paint);
|
||||
}
|
||||
|
||||
@override
|
||||
void restore() {
|
||||
_canvas.restore();
|
||||
}
|
||||
|
||||
@override
|
||||
int getSaveCount() {
|
||||
return _canvas.saveCount;
|
||||
}
|
||||
|
||||
@override
|
||||
void translate(double dx, double dy) {
|
||||
_canvas.translate(dx, dy);
|
||||
}
|
||||
|
||||
@override
|
||||
void scale(double sx, [double sy]) => _scale(sx, sy ?? sx);
|
||||
|
||||
void _scale(double sx, double sy) {
|
||||
_canvas.scale(sx, sy);
|
||||
}
|
||||
|
||||
@override
|
||||
void rotate(double radians) {
|
||||
_canvas.rotate(radians);
|
||||
}
|
||||
|
||||
@override
|
||||
void skew(double sx, double sy) {
|
||||
_canvas.skew(sx, sy);
|
||||
}
|
||||
|
||||
@override
|
||||
void transform(Float64List matrix4) {
|
||||
assert(matrix4 != null);
|
||||
if (matrix4.length != 16) {
|
||||
throw ArgumentError('"matrix4" must have 16 entries.');
|
||||
}
|
||||
_transform(matrix4);
|
||||
}
|
||||
|
||||
void _transform(Float64List matrix4) {
|
||||
_canvas.transform(matrix4);
|
||||
}
|
||||
|
||||
@override
|
||||
void clipRect(ui.Rect rect,
|
||||
{ui.ClipOp clipOp = ui.ClipOp.intersect, bool doAntiAlias = true}) {
|
||||
assert(rectIsValid(rect));
|
||||
assert(clipOp != null);
|
||||
assert(doAntiAlias != null);
|
||||
_clipRect(rect, clipOp, doAntiAlias);
|
||||
}
|
||||
|
||||
void _clipRect(ui.Rect rect, ui.ClipOp clipOp, bool doAntiAlias) {
|
||||
_canvas.clipRect(rect, clipOp, doAntiAlias);
|
||||
}
|
||||
|
||||
@override
|
||||
void clipRRect(ui.RRect rrect, {bool doAntiAlias = true}) {
|
||||
assert(rrectIsValid(rrect));
|
||||
assert(doAntiAlias != null);
|
||||
_clipRRect(rrect, doAntiAlias);
|
||||
}
|
||||
|
||||
void _clipRRect(ui.RRect rrect, bool doAntiAlias) {
|
||||
_canvas.clipRRect(rrect, doAntiAlias);
|
||||
}
|
||||
|
||||
@override
|
||||
void clipPath(ui.Path path, {bool doAntiAlias = true}) {
|
||||
assert(path != null); // path is checked on the engine side
|
||||
assert(doAntiAlias != null);
|
||||
_clipPath(path, doAntiAlias);
|
||||
}
|
||||
|
||||
void _clipPath(ui.Path path, bool doAntiAlias) {
|
||||
_canvas.clipPath(path, doAntiAlias);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawColor(ui.Color color, ui.BlendMode blendMode) {
|
||||
assert(color != null);
|
||||
assert(blendMode != null);
|
||||
_drawColor(color, blendMode);
|
||||
}
|
||||
|
||||
void _drawColor(ui.Color color, ui.BlendMode blendMode) {
|
||||
_canvas.drawColor(color, blendMode);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawLine(ui.Offset p1, ui.Offset p2, ui.Paint paint) {
|
||||
assert(_offsetIsValid(p1));
|
||||
assert(_offsetIsValid(p2));
|
||||
assert(paint != null);
|
||||
_drawLine(p1, p2, paint);
|
||||
}
|
||||
|
||||
void _drawLine(ui.Offset p1, ui.Offset p2, paint) {
|
||||
_canvas.drawLine(p1, p2, paint);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawPaint(ui.Paint paint) {
|
||||
assert(paint != null);
|
||||
_drawPaint(paint);
|
||||
}
|
||||
|
||||
void _drawPaint(ui.Paint paint) {
|
||||
_canvas.drawPaint(paint);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawRect(ui.Rect rect, ui.Paint paint) {
|
||||
assert(rectIsValid(rect));
|
||||
assert(paint != null);
|
||||
_drawRect(rect, paint);
|
||||
}
|
||||
|
||||
void _drawRect(ui.Rect rect, ui.Paint paint) {
|
||||
_canvas.drawRect(rect, paint);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawRRect(ui.RRect rrect, ui.Paint paint) {
|
||||
assert(rrectIsValid(rrect));
|
||||
assert(paint != null);
|
||||
_drawRRect(rrect, paint);
|
||||
}
|
||||
|
||||
void _drawRRect(ui.RRect rrect, ui.Paint paint) {
|
||||
_canvas.drawRRect(rrect, paint);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawDRRect(ui.RRect outer, ui.RRect inner, ui.Paint paint) {
|
||||
assert(rrectIsValid(outer));
|
||||
assert(rrectIsValid(inner));
|
||||
assert(paint != null);
|
||||
_drawDRRect(outer, inner, paint);
|
||||
}
|
||||
|
||||
void _drawDRRect(ui.RRect outer, ui.RRect inner, ui.Paint paint) {
|
||||
_canvas.drawDRRect(outer, inner, paint);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawOval(ui.Rect rect, ui.Paint paint) {
|
||||
assert(rectIsValid(rect));
|
||||
assert(paint != null);
|
||||
_drawOval(rect, paint);
|
||||
}
|
||||
|
||||
void _drawOval(ui.Rect rect, ui.Paint paint) {
|
||||
_canvas.drawOval(rect, paint);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawCircle(ui.Offset c, double radius, ui.Paint paint) {
|
||||
assert(_offsetIsValid(c));
|
||||
assert(paint != null);
|
||||
_drawCircle(c, radius, paint);
|
||||
}
|
||||
|
||||
void _drawCircle(ui.Offset c, double radius, ui.Paint paint) {
|
||||
_canvas.drawCircle(c, radius, paint);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawArc(ui.Rect rect, double startAngle, double sweepAngle,
|
||||
bool useCenter, ui.Paint paint) {
|
||||
assert(rectIsValid(rect));
|
||||
assert(paint != null);
|
||||
_drawArc(rect, startAngle, sweepAngle, useCenter, paint);
|
||||
}
|
||||
|
||||
void _drawArc(ui.Rect rect, double startAngle, double sweepAngle,
|
||||
bool useCenter, ui.Paint paint) {
|
||||
_canvas.drawArc(rect, startAngle, sweepAngle, useCenter, paint);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawPath(ui.Path path, ui.Paint paint) {
|
||||
assert(path != null); // path is checked on the engine side
|
||||
assert(paint != null);
|
||||
_drawPath(path, paint);
|
||||
}
|
||||
|
||||
void _drawPath(ui.Path path, ui.Paint paint) {
|
||||
_canvas.drawPath(path, paint);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawImage(ui.Image image, ui.Offset p, ui.Paint paint) {
|
||||
assert(image != null); // image is checked on the engine side
|
||||
assert(_offsetIsValid(p));
|
||||
assert(paint != null);
|
||||
_drawImage(image, p, paint);
|
||||
}
|
||||
|
||||
void _drawImage(ui.Image image, ui.Offset p, ui.Paint paint) {
|
||||
_canvas.drawImage(image, p, paint);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawImageRect(ui.Image image, ui.Rect src, ui.Rect dst, ui.Paint paint) {
|
||||
assert(image != null); // image is checked on the engine side
|
||||
assert(rectIsValid(src));
|
||||
assert(rectIsValid(dst));
|
||||
assert(paint != null);
|
||||
_drawImageRect(image, src, dst, paint);
|
||||
}
|
||||
|
||||
void _drawImageRect(
|
||||
ui.Image image, ui.Rect src, ui.Rect dst, ui.Paint paint) {
|
||||
_canvas.drawImageRect(image, src, dst, paint);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawImageNine(
|
||||
ui.Image image, ui.Rect center, ui.Rect dst, ui.Paint paint) {
|
||||
assert(image != null); // image is checked on the engine side
|
||||
assert(rectIsValid(center));
|
||||
assert(rectIsValid(dst));
|
||||
assert(paint != null);
|
||||
_drawImageNine(image, center, dst, paint);
|
||||
}
|
||||
|
||||
void _drawImageNine(
|
||||
ui.Image image, ui.Rect center, ui.Rect dst, ui.Paint paint) {
|
||||
_canvas.drawImageNine(image, center, dst, paint);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawPicture(ui.Picture picture) {
|
||||
assert(picture != null); // picture is checked on the engine side
|
||||
_drawPicture(picture);
|
||||
}
|
||||
|
||||
void _drawPicture(ui.Picture picture) {
|
||||
_canvas.drawPicture(picture);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawParagraph(ui.Paragraph paragraph, ui.Offset offset) {
|
||||
assert(paragraph != null);
|
||||
assert(_offsetIsValid(offset));
|
||||
_drawParagraph(paragraph, offset);
|
||||
}
|
||||
|
||||
void _drawParagraph(ui.Paragraph paragraph, ui.Offset offset) {
|
||||
_canvas.drawParagraph(paragraph, offset);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawPoints(
|
||||
ui.PointMode pointMode, List<ui.Offset> points, ui.Paint paint) {
|
||||
assert(pointMode != null);
|
||||
assert(points != null);
|
||||
assert(paint != null);
|
||||
_drawPoints(paint, pointMode, encodePointList(points));
|
||||
}
|
||||
|
||||
@override
|
||||
void drawRawPoints(
|
||||
ui.PointMode pointMode, Float32List points, ui.Paint paint) {
|
||||
assert(pointMode != null);
|
||||
assert(points != null);
|
||||
assert(paint != null);
|
||||
if (points.length % 2 != 0) {
|
||||
throw ArgumentError('"points" must have an even number of values.');
|
||||
}
|
||||
_drawPoints(paint, pointMode, points);
|
||||
}
|
||||
|
||||
void _drawPoints(ui.Paint paint, ui.PointMode pointMode, Float32List points) {
|
||||
_canvas.drawPoints(paint, pointMode, points);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawVertices(
|
||||
ui.Vertices vertices, ui.BlendMode blendMode, ui.Paint paint) {
|
||||
assert(vertices != null); // vertices is checked on the engine side
|
||||
assert(paint != null);
|
||||
assert(blendMode != null);
|
||||
_drawVertices(vertices, blendMode, paint);
|
||||
}
|
||||
|
||||
void _drawVertices(
|
||||
ui.Vertices vertices, ui.BlendMode blendMode, ui.Paint paint) {
|
||||
_canvas.drawVertices(vertices, blendMode, paint);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawAtlas(
|
||||
ui.Image atlas,
|
||||
List<ui.RSTransform> transforms,
|
||||
List<ui.Rect> rects,
|
||||
List<ui.Color> colors,
|
||||
ui.BlendMode blendMode,
|
||||
ui.Rect cullRect,
|
||||
ui.Paint paint) {
|
||||
assert(atlas != null); // atlas is checked on the engine side
|
||||
assert(transforms != null);
|
||||
assert(rects != null);
|
||||
assert(colors != null);
|
||||
assert(blendMode != null);
|
||||
assert(paint != null);
|
||||
|
||||
final int rectCount = rects.length;
|
||||
if (transforms.length != rectCount) {
|
||||
throw ArgumentError('"transforms" and "rects" lengths must match.');
|
||||
}
|
||||
if (colors.isNotEmpty && colors.length != rectCount) {
|
||||
throw ArgumentError(
|
||||
'If non-null, "colors" length must match that of "transforms" and "rects".');
|
||||
}
|
||||
|
||||
final Float32List rstTransformBuffer = Float32List(rectCount * 4);
|
||||
final Float32List rectBuffer = Float32List(rectCount * 4);
|
||||
|
||||
for (int i = 0; i < rectCount; ++i) {
|
||||
final int index0 = i * 4;
|
||||
final int index1 = index0 + 1;
|
||||
final int index2 = index0 + 2;
|
||||
final int index3 = index0 + 3;
|
||||
final ui.RSTransform rstTransform = transforms[i];
|
||||
final ui.Rect rect = rects[i];
|
||||
assert(rectIsValid(rect));
|
||||
rstTransformBuffer[index0] = rstTransform.scos;
|
||||
rstTransformBuffer[index1] = rstTransform.ssin;
|
||||
rstTransformBuffer[index2] = rstTransform.tx;
|
||||
rstTransformBuffer[index3] = rstTransform.ty;
|
||||
rectBuffer[index0] = rect.left;
|
||||
rectBuffer[index1] = rect.top;
|
||||
rectBuffer[index2] = rect.right;
|
||||
rectBuffer[index3] = rect.bottom;
|
||||
}
|
||||
|
||||
final Int32List colorBuffer =
|
||||
colors.isEmpty ? null : _encodeColorList(colors);
|
||||
|
||||
_drawAtlas(
|
||||
paint, atlas, rstTransformBuffer, rectBuffer, colorBuffer, blendMode);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawRawAtlas(
|
||||
ui.Image atlas,
|
||||
Float32List rstTransforms,
|
||||
Float32List rects,
|
||||
Int32List colors,
|
||||
ui.BlendMode blendMode,
|
||||
ui.Rect cullRect,
|
||||
ui.Paint paint) {
|
||||
assert(atlas != null); // atlas is checked on the engine side
|
||||
assert(rstTransforms != null);
|
||||
assert(rects != null);
|
||||
assert(colors != null);
|
||||
assert(blendMode != null);
|
||||
assert(paint != null);
|
||||
|
||||
final int rectCount = rects.length;
|
||||
if (rstTransforms.length != rectCount)
|
||||
throw ArgumentError('"rstTransforms" and "rects" lengths must match.');
|
||||
if (rectCount % 4 != 0)
|
||||
throw ArgumentError(
|
||||
'"rstTransforms" and "rects" lengths must be a multiple of four.');
|
||||
if (colors != null && colors.length * 4 != rectCount)
|
||||
throw ArgumentError(
|
||||
'If non-null, "colors" length must be one fourth the length of "rstTransforms" and "rects".');
|
||||
|
||||
_drawAtlas(paint, atlas, rstTransforms, rects, colors, blendMode);
|
||||
}
|
||||
|
||||
// TODO(hterkelsen): Pass a cull_rect once CanvasKit supports that.
|
||||
void _drawAtlas(
|
||||
ui.Paint paint,
|
||||
ui.Image atlas,
|
||||
Float32List rstTransforms,
|
||||
Float32List rects,
|
||||
Int32List colors,
|
||||
ui.BlendMode blendMode,
|
||||
) {
|
||||
_canvas.drawAtlasRaw(paint, atlas, rstTransforms, rects, colors, blendMode);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawShadow(ui.Path path, ui.Color color, double elevation,
|
||||
bool transparentOccluder) {
|
||||
assert(path != null); // path is checked on the engine side
|
||||
assert(color != null);
|
||||
assert(transparentOccluder != null);
|
||||
_drawShadow(path, color, elevation, transparentOccluder);
|
||||
}
|
||||
|
||||
void _drawShadow(ui.Path path, ui.Color color, double elevation,
|
||||
bool transparentOccluder) {
|
||||
_canvas.drawShadow(path, color, elevation, transparentOccluder);
|
||||
}
|
||||
}
|
||||
@@ -110,8 +110,10 @@ class BackdropFilterLayer extends ContainerLayer {
|
||||
class ClipPathLayer extends ContainerLayer {
|
||||
/// The path used to clip child layers.
|
||||
final ui.Path _clipPath;
|
||||
final ui.Clip _clipBehavior;
|
||||
|
||||
ClipPathLayer(this._clipPath);
|
||||
ClipPathLayer(this._clipPath, this._clipBehavior)
|
||||
: assert(_clipBehavior != ui.Clip.none);
|
||||
|
||||
@override
|
||||
void preroll(PrerollContext prerollContext, Matrix4 matrix) {
|
||||
@@ -127,8 +129,15 @@ class ClipPathLayer extends ContainerLayer {
|
||||
assert(needsPainting);
|
||||
|
||||
paintContext.canvas.save();
|
||||
paintContext.canvas.clipPath(_clipPath);
|
||||
paintContext.canvas.clipPath(_clipPath, _clipBehavior != ui.Clip.hardEdge);
|
||||
|
||||
if (_clipBehavior == ui.Clip.antiAliasWithSaveLayer) {
|
||||
paintContext.canvas.saveLayer(paintBounds, null);
|
||||
}
|
||||
paintChildren(paintContext);
|
||||
if (_clipBehavior == ui.Clip.antiAliasWithSaveLayer) {
|
||||
paintContext.canvas.restore();
|
||||
}
|
||||
paintContext.canvas.restore();
|
||||
}
|
||||
}
|
||||
@@ -137,8 +146,10 @@ class ClipPathLayer extends ContainerLayer {
|
||||
class ClipRectLayer extends ContainerLayer {
|
||||
/// The rectangle used to clip child layers.
|
||||
final ui.Rect _clipRect;
|
||||
final ui.Clip _clipBehavior;
|
||||
|
||||
ClipRectLayer(this._clipRect);
|
||||
ClipRectLayer(this._clipRect, this._clipBehavior)
|
||||
: assert(_clipBehavior != ui.Clip.none);
|
||||
|
||||
@override
|
||||
void preroll(PrerollContext prerollContext, Matrix4 matrix) {
|
||||
@@ -153,8 +164,18 @@ class ClipRectLayer extends ContainerLayer {
|
||||
assert(needsPainting);
|
||||
|
||||
paintContext.canvas.save();
|
||||
paintContext.canvas.clipRect(_clipRect);
|
||||
paintContext.canvas.clipRect(
|
||||
_clipRect,
|
||||
ui.ClipOp.intersect,
|
||||
_clipBehavior != ui.Clip.hardEdge,
|
||||
);
|
||||
if (_clipBehavior == ui.Clip.antiAliasWithSaveLayer) {
|
||||
paintContext.canvas.saveLayer(_clipRect, null);
|
||||
}
|
||||
paintChildren(paintContext);
|
||||
if (_clipBehavior == ui.Clip.antiAliasWithSaveLayer) {
|
||||
paintContext.canvas.restore();
|
||||
}
|
||||
paintContext.canvas.restore();
|
||||
}
|
||||
}
|
||||
@@ -163,8 +184,10 @@ class ClipRectLayer extends ContainerLayer {
|
||||
class ClipRRectLayer extends ContainerLayer {
|
||||
/// The rounded rectangle used to clip child layers.
|
||||
final ui.RRect _clipRRect;
|
||||
final ui.Clip _clipBehavior;
|
||||
|
||||
ClipRRectLayer(this._clipRRect);
|
||||
ClipRRectLayer(this._clipRRect, this._clipBehavior)
|
||||
: assert(_clipBehavior != ui.Clip.none);
|
||||
|
||||
@override
|
||||
void preroll(PrerollContext prerollContext, Matrix4 matrix) {
|
||||
@@ -179,8 +202,15 @@ class ClipRRectLayer extends ContainerLayer {
|
||||
assert(needsPainting);
|
||||
|
||||
paintContext.canvas.save();
|
||||
paintContext.canvas.clipRRect(_clipRRect);
|
||||
paintContext.canvas
|
||||
.clipRRect(_clipRRect, _clipBehavior != ui.Clip.hardEdge);
|
||||
if (_clipBehavior == ui.Clip.antiAliasWithSaveLayer) {
|
||||
paintContext.canvas.saveLayer(paintBounds, null);
|
||||
}
|
||||
paintChildren(paintContext);
|
||||
if (_clipBehavior == ui.Clip.antiAliasWithSaveLayer) {
|
||||
paintContext.canvas.restore();
|
||||
}
|
||||
paintContext.canvas.restore();
|
||||
}
|
||||
}
|
||||
@@ -287,7 +317,7 @@ class TransformLayer extends ContainerLayer
|
||||
/// A layer containing a [Picture].
|
||||
class PictureLayer extends Layer {
|
||||
/// The picture to paint into the canvas.
|
||||
final ui.Picture picture;
|
||||
final SkPicture picture;
|
||||
|
||||
/// The offset at which to paint the picture.
|
||||
final ui.Offset offset;
|
||||
@@ -414,13 +444,13 @@ class PhysicalShapeLayer extends ContainerLayer
|
||||
final int saveCount = paintContext.canvas.save();
|
||||
switch (_clipBehavior) {
|
||||
case ui.Clip.hardEdge:
|
||||
paintContext.canvas.clipPath(_path, doAntiAlias: false);
|
||||
paintContext.canvas.clipPath(_path, false);
|
||||
break;
|
||||
case ui.Clip.antiAlias:
|
||||
paintContext.canvas.clipPath(_path, doAntiAlias: true);
|
||||
paintContext.canvas.clipPath(_path, true);
|
||||
break;
|
||||
case ui.Clip.antiAliasWithSaveLayer:
|
||||
paintContext.canvas.clipPath(_path, doAntiAlias: true);
|
||||
paintContext.canvas.clipPath(_path, true);
|
||||
paintContext.canvas.saveLayer(paintBounds, null);
|
||||
break;
|
||||
case ui.Clip.none:
|
||||
|
||||
@@ -99,21 +99,21 @@ class LayerSceneBuilder implements ui.SceneBuilder {
|
||||
@override
|
||||
ui.ClipPathEngineLayer pushClipPath(ui.Path path,
|
||||
{ui.Clip clipBehavior = ui.Clip.antiAlias, ui.EngineLayer oldLayer}) {
|
||||
pushLayer(ClipPathLayer(path));
|
||||
pushLayer(ClipPathLayer(path, clipBehavior));
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
ui.ClipRRectEngineLayer pushClipRRect(ui.RRect rrect,
|
||||
{ui.Clip clipBehavior, ui.EngineLayer oldLayer}) {
|
||||
pushLayer(ClipRRectLayer(rrect));
|
||||
pushLayer(ClipRRectLayer(rrect, clipBehavior));
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
ui.ClipRectEngineLayer pushClipRect(ui.Rect rect,
|
||||
{ui.Clip clipBehavior = ui.Clip.antiAlias, ui.EngineLayer oldLayer}) {
|
||||
pushLayer(ClipRectLayer(rect));
|
||||
pushLayer(ClipRectLayer(rect, clipBehavior));
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -41,11 +41,11 @@ class SkPath implements ui.Path {
|
||||
|
||||
@override
|
||||
void addArc(ui.Rect oval, double startAngle, double sweepAngle) {
|
||||
const double _toDegrees = 180.0 / math.pi;
|
||||
const double toDegrees = 180.0 / math.pi;
|
||||
_skPath.callMethod('addArc', <dynamic>[
|
||||
makeSkRect(oval),
|
||||
startAngle * _toDegrees,
|
||||
sweepAngle * _toDegrees,
|
||||
startAngle * toDegrees,
|
||||
sweepAngle * toDegrees,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -83,20 +83,9 @@ class SkPath implements ui.Path {
|
||||
|
||||
@override
|
||||
void addPolygon(List<ui.Offset> points, bool close) {
|
||||
// TODO(het): Use `addPoly` once CanvasKit makes it available.
|
||||
assert(points != null);
|
||||
if (points.isEmpty) {
|
||||
return;
|
||||
}
|
||||
|
||||
moveTo(points.first.dx, points.first.dy);
|
||||
for (int i = 1; i < points.length; i++) {
|
||||
final ui.Offset point = points[i];
|
||||
lineTo(point.dx, point.dy);
|
||||
}
|
||||
if (close) {
|
||||
this.close();
|
||||
}
|
||||
final Float32List encodedPoints = encodePointList(points);
|
||||
_skPath.callMethod('addPoly', <dynamic>[encodedPoints, close]);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -124,11 +113,11 @@ class SkPath implements ui.Path {
|
||||
@override
|
||||
void arcTo(
|
||||
ui.Rect rect, double startAngle, double sweepAngle, bool forceMoveTo) {
|
||||
const double radsToDegrees = 180.0 / math.pi;
|
||||
const double toDegrees = 180.0 / math.pi;
|
||||
_skPath.callMethod('arcTo', <dynamic>[
|
||||
makeSkRect(rect),
|
||||
startAngle * radsToDegrees,
|
||||
sweepAngle * radsToDegrees,
|
||||
startAngle * toDegrees,
|
||||
sweepAngle * toDegrees,
|
||||
forceMoveTo,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -102,8 +102,9 @@ class _SkPathMeasure {
|
||||
{bool startWithMoveTo = true}) {
|
||||
assert(contourIndex == currentContourIndex,
|
||||
'PathMetrics are invalid if it is not the current contour.');
|
||||
// TODO(het): Add this once `getSegment` is added to CanvasKit.
|
||||
throw UnimplementedError('extractPath');
|
||||
final js.JsObject skPath = pathMeasure
|
||||
.callMethod('getSegment', <dynamic>[start, end, startWithMoveTo]);
|
||||
return SkPath._fromSkPath(skPath);
|
||||
}
|
||||
|
||||
bool isClosed(int contourIndex) {
|
||||
|
||||
@@ -6,24 +6,18 @@ part of engine;
|
||||
|
||||
class SkPicture implements ui.Picture {
|
||||
final js.JsObject skPicture;
|
||||
final ui.Rect cullRect;
|
||||
|
||||
SkPicture(this.skPicture, this.cullRect);
|
||||
|
||||
@override
|
||||
int get approximateBytesUsed => 0;
|
||||
|
||||
@override
|
||||
final ui.Rect cullRect;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
// TODO: implement dispose
|
||||
}
|
||||
|
||||
@override
|
||||
// TODO: implement recordingCanvas
|
||||
RecordingCanvas get recordingCanvas => null;
|
||||
|
||||
@override
|
||||
Future<ui.Image> toImage(int width, int height) {
|
||||
// TODO: implement toImage
|
||||
|
||||
@@ -5,20 +5,17 @@
|
||||
part of engine;
|
||||
|
||||
class SkPictureRecorder implements ui.PictureRecorder {
|
||||
@override
|
||||
ui.Rect cullRect;
|
||||
|
||||
ui.Rect _cullRect;
|
||||
js.JsObject _recorder;
|
||||
|
||||
@override
|
||||
RecordingCanvas beginRecording(ui.Rect bounds) {
|
||||
cullRect = bounds;
|
||||
SkCanvas beginRecording(ui.Rect bounds) {
|
||||
_cullRect = bounds;
|
||||
_recorder = js.JsObject(canvasKit['SkPictureRecorder']);
|
||||
final js.JsObject skRect = js.JsObject(canvasKit['LTRBRect'],
|
||||
<double>[bounds.left, bounds.top, bounds.right, bounds.bottom]);
|
||||
final js.JsObject skCanvas =
|
||||
_recorder.callMethod('beginRecording', <js.JsObject>[skRect]);
|
||||
return SkRecordingCanvas(skCanvas);
|
||||
return SkCanvas(skCanvas);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -26,9 +23,10 @@ class SkPictureRecorder implements ui.PictureRecorder {
|
||||
final js.JsObject skPicture =
|
||||
_recorder.callMethod('finishRecordingAsPicture');
|
||||
_recorder.callMethod('delete');
|
||||
return SkPicture(skPicture, cullRect);
|
||||
_recorder = null;
|
||||
return SkPicture(skPicture, _cullRect);
|
||||
}
|
||||
|
||||
@override
|
||||
bool get isRecording => false;
|
||||
bool get isRecording => _recorder != null;
|
||||
}
|
||||
|
||||
@@ -14,11 +14,24 @@ class Rasterizer {
|
||||
/// Creates a new frame from this rasterizer's surface, draws the given
|
||||
/// [LayerTree] into it, and then submits the frame.
|
||||
void draw(LayerTree layerTree) {
|
||||
final SurfaceFrame frame = surface.acquireFrame(ui.window.physicalSize);
|
||||
final SkCanvas canvas = frame.canvas;
|
||||
final Frame compositorFrame = context.acquireFrame(canvas);
|
||||
if (layerTree == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
canvas.clear();
|
||||
final ui.Size physicalSize = ui.window.physicalSize;
|
||||
final ui.Size frameSize = ui.Size(
|
||||
physicalSize.width.truncate().toDouble(),
|
||||
physicalSize.height.truncate().toDouble(),
|
||||
);
|
||||
|
||||
if (frameSize.isEmpty) {
|
||||
return;
|
||||
}
|
||||
layerTree.frameSize = frameSize;
|
||||
|
||||
final SurfaceFrame frame = surface.acquireFrame(layerTree.frameSize);
|
||||
final SkCanvas canvas = frame.skiaCanvas;
|
||||
final Frame compositorFrame = context.acquireFrame(canvas);
|
||||
|
||||
compositorFrame.raster(layerTree, ignoreRasterCache: true);
|
||||
frame.submit();
|
||||
|
||||
@@ -1,282 +0,0 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
part of engine;
|
||||
|
||||
class SkRecordingCanvas implements RecordingCanvas {
|
||||
final js.JsObject skCanvas;
|
||||
SkRecordingCanvas(this.skCanvas);
|
||||
|
||||
@override
|
||||
bool _didDraw = true;
|
||||
|
||||
@override
|
||||
bool _hasArbitraryPaint = true;
|
||||
|
||||
@override
|
||||
int get saveCount => skCanvas.callMethod('getSaveCount');
|
||||
|
||||
// This is required to implement RecordingCanvas.
|
||||
@override
|
||||
int _saveCount = -1;
|
||||
|
||||
@override
|
||||
// TODO: implement _commands
|
||||
List<PaintCommand> get _commands => null;
|
||||
|
||||
@override
|
||||
// TODO: implement _paintBounds
|
||||
_PaintBounds get _paintBounds => null;
|
||||
|
||||
@override
|
||||
void apply(EngineCanvas engineCanvas) {
|
||||
throw UnimplementedError("The Skia backend doesn't support apply()");
|
||||
}
|
||||
|
||||
@override
|
||||
void clipPath(ui.Path path, {bool doAntiAlias = true}) {
|
||||
final SkPath skPath = path;
|
||||
final js.JsObject intersectClipOp = canvasKit['ClipOp']['Intersect'];
|
||||
skCanvas.callMethod('clipPath', <dynamic>[
|
||||
skPath._skPath,
|
||||
intersectClipOp,
|
||||
doAntiAlias,
|
||||
]);
|
||||
}
|
||||
|
||||
@override
|
||||
void clipRRect(
|
||||
ui.RRect rrect, {
|
||||
bool doAntiAlias = true,
|
||||
}) {
|
||||
// TODO(het): Use `clipRRect` when CanvasKit makes it available.
|
||||
// CanvasKit doesn't expose `Canvas.clipRRect`, so we create a path, add the
|
||||
// RRect to it, and call clipPath with it.
|
||||
final SkPath rrectPath = SkPath();
|
||||
rrectPath.addRRect(rrect);
|
||||
clipPath(rrectPath, doAntiAlias: doAntiAlias);
|
||||
}
|
||||
|
||||
@override
|
||||
void clipRect(
|
||||
ui.Rect rect, {
|
||||
ui.ClipOp clipOp = ui.ClipOp.intersect,
|
||||
bool doAntiAlias = true,
|
||||
}) {
|
||||
js.JsObject skClipOp;
|
||||
switch (clipOp) {
|
||||
case ui.ClipOp.difference:
|
||||
skClipOp = canvasKit['ClipOp']['Difference'];
|
||||
break;
|
||||
case ui.ClipOp.intersect:
|
||||
skClipOp = canvasKit['ClipOp']['Intersect'];
|
||||
break;
|
||||
}
|
||||
|
||||
skCanvas.callMethod(
|
||||
'clipRect', <dynamic>[makeSkRect(rect), skClipOp, doAntiAlias]);
|
||||
}
|
||||
|
||||
@override
|
||||
ui.Rect computePaintBounds() {
|
||||
throw UnimplementedError(
|
||||
"The Skia backend doesn't use computePaintBounds()");
|
||||
}
|
||||
|
||||
@override
|
||||
void debugDumpCommands() {
|
||||
throw UnimplementedError(
|
||||
"The Skia backend doesn't use debugDumpCommands()");
|
||||
}
|
||||
|
||||
@override
|
||||
void debugEnforceArbitraryPaint() {
|
||||
throw UnimplementedError(
|
||||
"The Skia backend doesn't use debugEnforceArbitraryPaint()");
|
||||
}
|
||||
|
||||
@override
|
||||
String debugPrintCommands() {
|
||||
throw UnimplementedError(
|
||||
"The Skia backend doesn't use debugPrintCommands()");
|
||||
}
|
||||
|
||||
@override
|
||||
bool get didDraw => true;
|
||||
|
||||
@override
|
||||
void drawCircle(ui.Offset c, double radius, ui.Paint paint) {
|
||||
skCanvas.callMethod('drawCircle', <dynamic>[
|
||||
c.dx,
|
||||
c.dy,
|
||||
radius,
|
||||
makeSkPaint(paint),
|
||||
]);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawColor(ui.Color color, ui.BlendMode blendMode) {
|
||||
// TODO(het): Implement this once SkCanvas.drawColor becomes available.
|
||||
throw 'drawColor';
|
||||
}
|
||||
|
||||
@override
|
||||
void drawDRRect(ui.RRect outer, ui.RRect inner, ui.Paint paint) {
|
||||
skCanvas.callMethod('drawDRRect', <js.JsObject>[
|
||||
makeSkRRect(outer),
|
||||
makeSkRRect(inner),
|
||||
makeSkPaint(paint),
|
||||
]);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawImage(ui.Image image, ui.Offset offset, ui.Paint paint) {
|
||||
final SkImage skImage = image;
|
||||
skCanvas.callMethod('drawImage', <dynamic>[
|
||||
skImage.skImage,
|
||||
offset.dx,
|
||||
offset.dy,
|
||||
makeSkPaint(paint),
|
||||
]);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawImageRect(ui.Image image, ui.Rect src, ui.Rect dst, ui.Paint paint) {
|
||||
final SkImage skImage = image;
|
||||
skCanvas.callMethod('drawImageRect', <dynamic>[
|
||||
skImage.skImage,
|
||||
makeSkRect(src),
|
||||
makeSkRect(dst),
|
||||
makeSkPaint(paint),
|
||||
false,
|
||||
]);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawLine(ui.Offset p1, ui.Offset p2, ui.Paint paint) {
|
||||
skCanvas.callMethod('drawLine', <dynamic>[
|
||||
p1.dx,
|
||||
p1.dy,
|
||||
p2.dx,
|
||||
p2.dy,
|
||||
makeSkPaint(paint),
|
||||
]);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawOval(ui.Rect rect, ui.Paint paint) {
|
||||
skCanvas.callMethod('drawOval', <js.JsObject>[
|
||||
makeSkRect(rect),
|
||||
makeSkPaint(paint),
|
||||
]);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawPaint(ui.Paint paint) {
|
||||
skCanvas.callMethod('drawPaint', <js.JsObject>[makeSkPaint(paint)]);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawParagraph(ui.Paragraph paragraph, ui.Offset offset) {
|
||||
final SkParagraph skParagraph = paragraph;
|
||||
skCanvas.callMethod('drawParagraph', <dynamic>[
|
||||
skParagraph.skParagraph,
|
||||
offset.dx,
|
||||
offset.dy,
|
||||
]);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawPath(ui.Path path, ui.Paint paint) {
|
||||
final js.JsObject skPaint = makeSkPaint(paint);
|
||||
final SkPath enginePath = path;
|
||||
final js.JsObject skPath = enginePath._skPath;
|
||||
skCanvas.callMethod('drawPath', <js.JsObject>[skPath, skPaint]);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawRRect(ui.RRect rrect, ui.Paint paint) {
|
||||
skCanvas.callMethod('drawRRect', <js.JsObject>[
|
||||
makeSkRRect(rrect),
|
||||
makeSkPaint(paint),
|
||||
]);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawRect(ui.Rect rect, ui.Paint paint) {
|
||||
final js.JsObject skRect = makeSkRect(rect);
|
||||
final js.JsObject skPaint = makeSkPaint(paint);
|
||||
skCanvas.callMethod('drawRect', <js.JsObject>[skRect, skPaint]);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawShadow(ui.Path path, ui.Color color, double elevation,
|
||||
bool transparentOccluder) {
|
||||
drawSkShadow(skCanvas, path, color, elevation, transparentOccluder,
|
||||
ui.window.devicePixelRatio);
|
||||
}
|
||||
|
||||
@override
|
||||
void drawVertices(
|
||||
ui.Vertices vertices, ui.BlendMode blendMode, ui.Paint paint) {
|
||||
SkVertices skVertices = vertices;
|
||||
skCanvas.callMethod('drawVertices', <js.JsObject>[
|
||||
skVertices.skVertices,
|
||||
makeSkBlendMode(blendMode),
|
||||
makeSkPaint(paint)
|
||||
]);
|
||||
}
|
||||
|
||||
@override
|
||||
bool get hasArbitraryPaint => true;
|
||||
|
||||
@override
|
||||
void restore() {
|
||||
skCanvas.callMethod('restore');
|
||||
}
|
||||
|
||||
@override
|
||||
void rotate(double radians) {
|
||||
skCanvas
|
||||
.callMethod('rotate', <double>[radians * 180.0 / math.pi, 0.0, 0.0]);
|
||||
}
|
||||
|
||||
@override
|
||||
void save() {
|
||||
skCanvas.callMethod('save');
|
||||
}
|
||||
|
||||
@override
|
||||
void saveLayer(ui.Rect bounds, ui.Paint paint) {
|
||||
skCanvas.callMethod('saveLayer', <js.JsObject>[
|
||||
makeSkRect(bounds),
|
||||
makeSkPaint(paint),
|
||||
]);
|
||||
}
|
||||
|
||||
@override
|
||||
void saveLayerWithoutBounds(ui.Paint paint) {
|
||||
skCanvas.callMethod('saveLayer', <js.JsObject>[null, makeSkPaint(paint)]);
|
||||
}
|
||||
|
||||
@override
|
||||
void scale(double sx, double sy) {
|
||||
skCanvas.callMethod('scale', <double>[sx, sy]);
|
||||
}
|
||||
|
||||
@override
|
||||
void skew(double sx, double sy) {
|
||||
skCanvas.callMethod('skew', <double>[sx, sy]);
|
||||
}
|
||||
|
||||
@override
|
||||
void transform(Float64List matrix4) {
|
||||
skCanvas.callMethod('concat', <js.JsArray<double>>[makeSkMatrix(matrix4)]);
|
||||
}
|
||||
|
||||
@override
|
||||
void translate(double dx, double dy) {
|
||||
skCanvas.callMethod('translate', <double>[dx, dy]);
|
||||
}
|
||||
}
|
||||
@@ -4,51 +4,87 @@
|
||||
|
||||
part of engine;
|
||||
|
||||
typedef SubmitCallback = bool Function(SurfaceFrame, SkCanvas);
|
||||
|
||||
/// A frame which contains a canvas to be drawn into.
|
||||
class SurfaceFrame {
|
||||
final void Function(SkCanvas) submitFn;
|
||||
final SkCanvas canvas;
|
||||
SurfaceFrame(this.submitFn, this.canvas);
|
||||
final SkSurface skiaSurface;
|
||||
final SubmitCallback submitCallback;
|
||||
bool _submitted;
|
||||
|
||||
SurfaceFrame(this.skiaSurface, this.submitCallback)
|
||||
: _submitted = false,
|
||||
assert(submitCallback != null);
|
||||
|
||||
/// Submit this frame to be drawn.
|
||||
void submit() {
|
||||
submitFn(canvas);
|
||||
bool submit() {
|
||||
if (_submitted) {
|
||||
return false;
|
||||
}
|
||||
return submitCallback(this, skiaCanvas);
|
||||
}
|
||||
|
||||
SkCanvas get skiaCanvas => skiaSurface?.getCanvas();
|
||||
}
|
||||
|
||||
/// A surface which can be drawn into by the compositor.
|
||||
///
|
||||
/// The underlying representation is a [BitmapCanvas], which can be reused by
|
||||
/// successive frames if they are the same size. Otherwise, a new canvas is
|
||||
/// The underlying representation is a [SkSurface], which can be reused by
|
||||
/// successive frames if they are the same size. Otherwise, a new [SkSurface] is
|
||||
/// created.
|
||||
class Surface {
|
||||
final _CanvasCache canvasCache = _CanvasCache();
|
||||
|
||||
/// This function is called with the canvas once drawing on it has been
|
||||
/// completed for a frame.
|
||||
final void Function(SkCanvas) submitFunction;
|
||||
|
||||
Surface(this.submitFunction);
|
||||
SkSurface _surface;
|
||||
|
||||
/// Acquire a frame of the given [size] containing a drawable canvas.
|
||||
///
|
||||
/// The given [size] is in physical pixels.
|
||||
SurfaceFrame acquireFrame(ui.Size size) {
|
||||
final SkCanvas canvas = canvasCache.acquireCanvas(size);
|
||||
return SurfaceFrame(submitFunction, canvas);
|
||||
final SkSurface surface = _acquireRenderSurface(size);
|
||||
|
||||
if (surface == null) return null;
|
||||
|
||||
SubmitCallback submitCallback = (SurfaceFrame surfaceFrame, SkCanvas canvas) {
|
||||
_presentSurface(canvas);
|
||||
};
|
||||
|
||||
return SurfaceFrame(surface, submitCallback);
|
||||
}
|
||||
|
||||
Matrix4 get rootTransformation => null;
|
||||
}
|
||||
|
||||
class _CanvasCache {
|
||||
SkCanvas _canvas;
|
||||
|
||||
SkCanvas acquireCanvas(ui.Size size) {
|
||||
assert(size != null);
|
||||
if (size == _canvas?.size) {
|
||||
return _canvas;
|
||||
SkSurface _acquireRenderSurface(ui.Size size) {
|
||||
if (!_createOrUpdateSurfaces(size)) {
|
||||
return null;
|
||||
}
|
||||
return _surface;
|
||||
}
|
||||
|
||||
bool _createOrUpdateSurfaces(ui.Size size) {
|
||||
if (_surface != null &&
|
||||
size ==
|
||||
ui.Size(
|
||||
_surface.width().toDouble(),
|
||||
_surface.height().toDouble(),
|
||||
)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
_surface?.dispose();
|
||||
_surface = null;
|
||||
|
||||
if (size.isEmpty) {
|
||||
html.window.console.error('Cannot create surfaces of empty size.');
|
||||
return false;
|
||||
}
|
||||
_surface = _wrapHtmlCanvas(size);
|
||||
|
||||
if (_surface == null) {
|
||||
html.window.console.error('Could not create a surface.');
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
SkSurface _wrapHtmlCanvas(ui.Size size) {
|
||||
final ui.Size logicalSize = size / ui.window.devicePixelRatio;
|
||||
final html.CanvasElement htmlCanvas =
|
||||
html.CanvasElement(width: size.width.ceil(), height: size.height.ceil())
|
||||
@@ -57,11 +93,46 @@ class _CanvasCache {
|
||||
..position = 'absolute'
|
||||
..width = '${logicalSize.width.ceil()}px'
|
||||
..height = '${logicalSize.height.ceil()}px';
|
||||
domRenderer.renderScene(htmlCanvas);
|
||||
final js.JsObject surface =
|
||||
canvasKit.callMethod('MakeCanvasSurface', <String>['flt-sk-canvas']);
|
||||
final js.JsObject skCanvas = surface.callMethod('getCanvas');
|
||||
_canvas = SkCanvas(skCanvas, htmlCanvas, surface, size);
|
||||
return _canvas;
|
||||
final js.JsObject skSurface =
|
||||
canvasKit.callMethod('MakeWebGLCanvasSurface', <dynamic>[
|
||||
htmlCanvas,
|
||||
size.width,
|
||||
size.height,
|
||||
]);
|
||||
|
||||
if (skSurface == null) {
|
||||
return null;
|
||||
} else {
|
||||
domRenderer.renderScene(htmlCanvas);
|
||||
return SkSurface(skSurface);
|
||||
}
|
||||
}
|
||||
|
||||
bool _presentSurface(SkCanvas canvas) {
|
||||
if (canvas == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
_surface.getCanvas().flush();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// A Dart wrapper around Skia's SkSurface.
|
||||
class SkSurface {
|
||||
final js.JsObject _surface;
|
||||
|
||||
SkSurface(this._surface);
|
||||
|
||||
SkCanvas getCanvas() {
|
||||
final js.JsObject skCanvas = _surface.callMethod('getCanvas');
|
||||
return SkCanvas(skCanvas);
|
||||
}
|
||||
|
||||
int width() => _surface.callMethod('width');
|
||||
int height() => _surface.callMethod('height');
|
||||
|
||||
void dispose() {
|
||||
_surface.callMethod('dispose');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,9 @@ const Map<String, String> _fontFamilyOverrides = <String, String>{
|
||||
'AbrilFatface': 'Abril Fatface',
|
||||
'packages/cupertino_icons/CupertinoIcons': 'CupertinoIcons',
|
||||
'.SF Pro Text': 'Google Sans',
|
||||
'.SF Pro Display': 'Google Sans',
|
||||
'.SF UI Text': 'Google Sans',
|
||||
'.SF UI Display': 'Google Sans',
|
||||
};
|
||||
|
||||
class SkParagraphStyle implements ui.ParagraphStyle {
|
||||
|
||||
@@ -56,6 +56,35 @@ 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);
|
||||
final int pointCount = points.length;
|
||||
final Float32List result = canvasKit.callMethod('Malloc', <dynamic>[js.context['Float32Array'], pointCount * 2]);
|
||||
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;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
js.JsObject makeSkPointMode(ui.PointMode pointMode) {
|
||||
switch (pointMode) {
|
||||
case ui.PointMode.points:
|
||||
return canvasKit['PointMode']['Points'];
|
||||
case ui.PointMode.lines:
|
||||
return canvasKit['PointMode']['Lines'];
|
||||
case ui.PointMode.polygon:
|
||||
return canvasKit['PointMode']['Polygon'];
|
||||
default:
|
||||
throw StateError('Unrecognized point mode $pointMode');
|
||||
}
|
||||
}
|
||||
|
||||
js.JsObject makeSkBlendMode(ui.BlendMode blendMode) {
|
||||
switch (blendMode) {
|
||||
case ui.BlendMode.clear:
|
||||
@@ -122,6 +151,8 @@ js.JsObject makeSkBlendMode(ui.BlendMode blendMode) {
|
||||
}
|
||||
|
||||
js.JsObject makeSkPaint(ui.Paint paint) {
|
||||
if (paint == null) return null;
|
||||
|
||||
final dynamic skPaint = js.JsObject(canvasKit['SkPaint']);
|
||||
|
||||
if (paint.shader != null) {
|
||||
|
||||
@@ -11,21 +11,6 @@ Int32List _encodeColorList(List<ui.Color> colors) {
|
||||
return result;
|
||||
}
|
||||
|
||||
Float32List _encodePointList(List<ui.Offset> points) {
|
||||
assert(points != null);
|
||||
final int pointCount = points.length;
|
||||
final Float32List result = Float32List(pointCount * 2);
|
||||
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;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
class SkVertices implements ui.Vertices {
|
||||
js.JsObject skVertices;
|
||||
final Int32List _colors;
|
||||
@@ -40,8 +25,9 @@ class SkVertices implements ui.Vertices {
|
||||
List<int> indices,
|
||||
}) : assert(mode != null),
|
||||
assert(positions != null),
|
||||
_colors = Int32List.fromList(colors.map((ui.Color c) => c.value).toList()),
|
||||
_positions = _offsetListToInt32List(positions),
|
||||
_colors =
|
||||
Int32List.fromList(colors.map((ui.Color c) => c.value).toList()),
|
||||
_positions = encodePointList(positions),
|
||||
_mode = mode {
|
||||
if (textureCoordinates != null &&
|
||||
textureCoordinates.length != positions.length)
|
||||
@@ -54,9 +40,9 @@ class SkVertices implements ui.Vertices {
|
||||
throw ArgumentError(
|
||||
'"indices" values must be valid indices in the positions list.');
|
||||
|
||||
final Float32List encodedPositions = _encodePointList(positions);
|
||||
final Float32List encodedPositions = encodePointList(positions);
|
||||
final Float32List encodedTextureCoordinates = (textureCoordinates != null)
|
||||
? _encodePointList(textureCoordinates)
|
||||
? encodePointList(textureCoordinates)
|
||||
: null;
|
||||
final Int32List encodedColors =
|
||||
colors != null ? _encodeColorList(colors) : null;
|
||||
@@ -140,19 +126,6 @@ class SkVertices implements ui.Vertices {
|
||||
return encodedPoints;
|
||||
}
|
||||
|
||||
static Float32List _offsetListToInt32List(List<ui.Offset> offsetList) {
|
||||
if (offsetList == null) {
|
||||
return null;
|
||||
}
|
||||
final int length = offsetList.length;
|
||||
final floatList = Float32List(length * 2);
|
||||
for (int i = 0, destIndex = 0; i < length; i++, destIndex += 2) {
|
||||
floatList[destIndex] = offsetList[i].dx;
|
||||
floatList[destIndex + 1] = offsetList[i].dx;
|
||||
}
|
||||
return floatList;
|
||||
}
|
||||
|
||||
@override
|
||||
Int32List get colors => _colors;
|
||||
|
||||
|
||||
70
engine/src/flutter/lib/web_ui/lib/src/engine/picture.dart
Normal file
70
engine/src/flutter/lib/web_ui/lib/src/engine/picture.dart
Normal file
@@ -0,0 +1,70 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
part of engine;
|
||||
|
||||
/// An implementation of [ui.PictureRecorder] backed by a [RecordingCanvas].
|
||||
class EnginePictureRecorder implements ui.PictureRecorder {
|
||||
EnginePictureRecorder();
|
||||
|
||||
RecordingCanvas _canvas;
|
||||
ui.Rect cullRect;
|
||||
bool _isRecording = false;
|
||||
|
||||
RecordingCanvas beginRecording(ui.Rect bounds) {
|
||||
assert(!_isRecording);
|
||||
cullRect = bounds;
|
||||
_isRecording = true;
|
||||
_canvas = RecordingCanvas(cullRect);
|
||||
return _canvas;
|
||||
}
|
||||
|
||||
@override
|
||||
bool get isRecording => _isRecording;
|
||||
|
||||
@override
|
||||
ui.Picture endRecording() {
|
||||
// Returning null is what the flutter engine does:
|
||||
// lib/ui/painting/picture_recorder.cc
|
||||
if (!_isRecording) {
|
||||
return null;
|
||||
}
|
||||
_isRecording = false;
|
||||
return EnginePicture(_canvas, cullRect);
|
||||
}
|
||||
}
|
||||
|
||||
/// An implementation of [ui.Picture] which is backed by a [RecordingCanvas].
|
||||
class EnginePicture implements ui.Picture {
|
||||
/// This class is created by the engine, and should not be instantiated
|
||||
/// or extended directly.
|
||||
///
|
||||
/// To create a [Picture], use a [PictureRecorder].
|
||||
EnginePicture(this.recordingCanvas, this.cullRect);
|
||||
|
||||
@override
|
||||
Future<ui.Image> toImage(int width, int height) async {
|
||||
final BitmapCanvas canvas = BitmapCanvas(ui.Rect.fromLTRB(0, 0, width.toDouble(), height.toDouble()));
|
||||
recordingCanvas.apply(canvas);
|
||||
final String imageDataUrl = canvas.canvas.toDataUrl();
|
||||
final html.ImageElement imageElement = html.ImageElement()
|
||||
..src = imageDataUrl
|
||||
..width = width
|
||||
..height = height;
|
||||
return HtmlImage(
|
||||
imageElement,
|
||||
width,
|
||||
height,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {}
|
||||
|
||||
@override
|
||||
int get approximateBytesUsed => 0;
|
||||
|
||||
final RecordingCanvas recordingCanvas;
|
||||
final ui.Rect cullRect;
|
||||
}
|
||||
@@ -340,7 +340,7 @@ abstract class PersistedPicture extends PersistedLeafSurface {
|
||||
|
||||
final double dx;
|
||||
final double dy;
|
||||
final ui.Picture picture;
|
||||
final EnginePicture picture;
|
||||
final ui.Rect localPaintBounds;
|
||||
final int hints;
|
||||
|
||||
|
||||
@@ -55,7 +55,8 @@ class EngineWindow extends ui.Window {
|
||||
double windowInnerHeight;
|
||||
if (html.window.visualViewport != null) {
|
||||
windowInnerWidth = html.window.visualViewport.width * devicePixelRatio;
|
||||
windowInnerHeight = html.window.visualViewport.height * devicePixelRatio;
|
||||
windowInnerHeight =
|
||||
html.window.visualViewport.height * devicePixelRatio;
|
||||
} else {
|
||||
windowInnerWidth = html.window.innerWidth * devicePixelRatio;
|
||||
windowInnerHeight = html.window.innerHeight * devicePixelRatio;
|
||||
@@ -281,12 +282,8 @@ class EngineWindow extends ui.Window {
|
||||
}
|
||||
}
|
||||
|
||||
final Rasterizer _rasterizer = experimentalUseSkia
|
||||
? Rasterizer(Surface((SkCanvas canvas) {
|
||||
domRenderer.renderScene(canvas.htmlCanvas);
|
||||
canvas.skSurface.callMethod('flush');
|
||||
}))
|
||||
: null;
|
||||
final Rasterizer _rasterizer =
|
||||
experimentalUseSkia ? Rasterizer(Surface()) : null;
|
||||
}
|
||||
|
||||
/// The window singleton.
|
||||
|
||||
@@ -165,7 +165,7 @@ class Vertices {
|
||||
///
|
||||
/// To begin recording, construct a [Canvas] to record the commands.
|
||||
/// To end recording, use the [PictureRecorder.endRecording] method.
|
||||
class PictureRecorder {
|
||||
abstract class PictureRecorder {
|
||||
/// Creates a new idle PictureRecorder. To associate it with a
|
||||
/// [Canvas] and begin recording, pass this [PictureRecorder] to the
|
||||
/// [Canvas] constructor.
|
||||
@@ -173,24 +173,10 @@ class PictureRecorder {
|
||||
if (engine.experimentalUseSkia) {
|
||||
return engine.SkPictureRecorder();
|
||||
} else {
|
||||
return PictureRecorder._();
|
||||
return engine.EnginePictureRecorder();
|
||||
}
|
||||
}
|
||||
|
||||
PictureRecorder._();
|
||||
|
||||
engine.RecordingCanvas _canvas;
|
||||
Rect cullRect;
|
||||
bool _isRecording = false;
|
||||
|
||||
engine.RecordingCanvas beginRecording(Rect bounds) {
|
||||
assert(!_isRecording);
|
||||
cullRect = bounds;
|
||||
_isRecording = true;
|
||||
_canvas = engine.RecordingCanvas(cullRect);
|
||||
return _canvas;
|
||||
}
|
||||
|
||||
/// Whether this object is currently recording commands.
|
||||
///
|
||||
/// Specifically, this returns true if a [Canvas] object has been
|
||||
@@ -198,7 +184,7 @@ class PictureRecorder {
|
||||
/// call to [endRecording], and false if either this
|
||||
/// [PictureRecorder] has not yet been associated with a [Canvas],
|
||||
/// or the [endRecording] method has already been called.
|
||||
bool get isRecording => _isRecording;
|
||||
bool get isRecording;
|
||||
|
||||
/// Finishes recording graphical operations.
|
||||
///
|
||||
@@ -207,15 +193,7 @@ class PictureRecorder {
|
||||
/// and the canvas objects are invalid and cannot be used further.
|
||||
///
|
||||
/// Returns null if the PictureRecorder is not associated with a canvas.
|
||||
Picture endRecording() {
|
||||
// Returning null is what the flutter engine does:
|
||||
// lib/ui/painting/picture_recorder.cc
|
||||
if (!_isRecording) {
|
||||
return null;
|
||||
}
|
||||
_isRecording = false;
|
||||
return Picture._(_canvas, cullRect);
|
||||
}
|
||||
Picture endRecording();
|
||||
}
|
||||
|
||||
/// An interface for recording graphical operations.
|
||||
@@ -238,6 +216,14 @@ class PictureRecorder {
|
||||
class Canvas {
|
||||
engine.RecordingCanvas _canvas;
|
||||
|
||||
factory Canvas(PictureRecorder recorder, [Rect cullRect]) {
|
||||
if (engine.experimentalUseSkia) {
|
||||
return engine.CanvasKitCanvas(recorder, cullRect);
|
||||
} else {
|
||||
return Canvas._(recorder, cullRect);
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a canvas for recording graphical operations into the
|
||||
/// given picture recorder.
|
||||
///
|
||||
@@ -250,7 +236,8 @@ class Canvas {
|
||||
///
|
||||
/// To end the recording, call [PictureRecorder.endRecording] on the
|
||||
/// given recorder.
|
||||
Canvas(PictureRecorder recorder, [Rect cullRect]) : assert(recorder != null) {
|
||||
Canvas._(engine.EnginePictureRecorder recorder, [Rect cullRect])
|
||||
: assert(recorder != null) {
|
||||
if (recorder.isRecording) {
|
||||
throw ArgumentError(
|
||||
'"recorder" must not already be associated with another Canvas.');
|
||||
@@ -1054,13 +1041,7 @@ class Canvas {
|
||||
/// A [Picture] can be placed in a [Scene] using a [SceneBuilder], via
|
||||
/// the [SceneBuilder.addPicture] method. A [Picture] can also be
|
||||
/// drawn into a [Canvas], using the [Canvas.drawPicture] method.
|
||||
class Picture {
|
||||
/// This class is created by the engine, and should not be instantiated
|
||||
/// or extended directly.
|
||||
///
|
||||
/// To create a [Picture], use a [PictureRecorder].
|
||||
Picture._(this.recordingCanvas, this.cullRect);
|
||||
|
||||
abstract class Picture {
|
||||
/// Creates an image from this picture.
|
||||
///
|
||||
/// The returned image will be `width` pixels wide and `height` pixels high.
|
||||
@@ -1069,33 +1050,17 @@ class Picture {
|
||||
///
|
||||
/// Although the image is returned synchronously, the picture is actually
|
||||
/// rasterized the first time the image is drawn and then cached.
|
||||
Future<Image> toImage(int width, int height) async {
|
||||
final engine.BitmapCanvas canvas = engine.BitmapCanvas(Rect.fromLTRB(0, 0, width.toDouble(), height.toDouble()));
|
||||
recordingCanvas.apply(canvas);
|
||||
final String imageDataUrl = canvas.canvas.toDataUrl();
|
||||
final html.ImageElement imageElement = html.ImageElement()
|
||||
..src = imageDataUrl
|
||||
..width = width
|
||||
..height = height;
|
||||
return engine.HtmlImage(
|
||||
imageElement,
|
||||
width,
|
||||
height,
|
||||
);
|
||||
}
|
||||
Future<Image> toImage(int width, int height);
|
||||
|
||||
/// Release the resources used by this object. The object is no longer usable
|
||||
/// after this method is called.
|
||||
void dispose() {}
|
||||
void dispose();
|
||||
|
||||
/// Returns the approximate number of bytes allocated for this object.
|
||||
///
|
||||
/// The actual size of this picture may be larger, particularly if it contains
|
||||
/// references to image or other large objects.
|
||||
int get approximateBytesUsed => 0;
|
||||
|
||||
final engine.RecordingCanvas recordingCanvas;
|
||||
final Rect cullRect;
|
||||
int get approximateBytesUsed;
|
||||
}
|
||||
|
||||
/// Determines the winding rule that decides how the interior of a [Path] is
|
||||
|
||||
@@ -255,7 +255,7 @@ void testLayerLifeCycle(
|
||||
|
||||
class MockPersistedPicture extends PersistedPicture {
|
||||
factory MockPersistedPicture() {
|
||||
final PictureRecorder recorder = PictureRecorder();
|
||||
final EnginePictureRecorder recorder = PictureRecorder();
|
||||
// Use the largest cull rect so that layer clips are effective. The tests
|
||||
// rely on this.
|
||||
recorder.beginRecording(Rect.largest)..drawPaint(Paint());
|
||||
|
||||
@@ -494,7 +494,7 @@ void _testCullRectComputation() {
|
||||
}
|
||||
|
||||
void _drawTestPicture(SceneBuilder builder) {
|
||||
final PictureRecorder recorder = PictureRecorder();
|
||||
final EnginePictureRecorder recorder = PictureRecorder();
|
||||
final RecordingCanvas canvas =
|
||||
recorder.beginRecording(const Rect.fromLTRB(0, 0, 100, 100));
|
||||
canvas.drawCircle(
|
||||
@@ -529,7 +529,7 @@ typedef PaintCallback = void Function(RecordingCanvas canvas);
|
||||
|
||||
void drawWithBitmapCanvas(SceneBuilder builder, PaintCallback callback,
|
||||
{Rect bounds = Rect.largest}) {
|
||||
final PictureRecorder recorder = PictureRecorder();
|
||||
final EnginePictureRecorder recorder = PictureRecorder();
|
||||
final RecordingCanvas canvas = recorder.beginRecording(bounds);
|
||||
|
||||
canvas.debugEnforceArbitraryPaint();
|
||||
|
||||
@@ -13,7 +13,7 @@ import 'package:web_engine_tester/golden_tester.dart';
|
||||
void main() {
|
||||
group('Picture', () {
|
||||
test('toImage produces an image', () async {
|
||||
final ui.PictureRecorder recorder = ui.PictureRecorder();
|
||||
final EnginePictureRecorder recorder = ui.PictureRecorder();
|
||||
final RecordingCanvas canvas = recorder.beginRecording(ui.Rect.fromLTRB(0, 0, 200, 100));
|
||||
canvas.drawCircle(
|
||||
const ui.Offset(100, 50),
|
||||
|
||||
Reference in New Issue
Block a user