This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Update CanvasKit to 0.7.0 and flesh out painting #13240
Merged
harryterkelsen
merged 4 commits into
flutter:master
from
harryterkelsen:canvaskit-0.7.0
Oct 21, 2019
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
// 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; | ||
|
||
/// A description of a color filter to apply when drawing a shape or compositing | ||
/// a layer with a particular [Paint]. A color filter is a function that takes | ||
/// two colors, and outputs one color. When applied during compositing, it is | ||
/// independently applied to each pixel of the layer being drawn before the | ||
/// entire layer is merged with the destination. | ||
/// | ||
/// Instances of this class are used with [Paint.colorFilter] on [Paint] | ||
/// objects. | ||
class EngineColorFilter implements ui.ColorFilter { | ||
/// Creates a color filter that applies the blend mode given as the second | ||
/// argument. The source color is the one given as the first argument, and the | ||
/// destination color is the one from the layer being composited. | ||
/// | ||
/// The output of this filter is then composited into the background according | ||
/// to the [Paint.blendMode], using the output of this filter as the source | ||
/// and the background as the destination. | ||
const EngineColorFilter.mode(ui.Color color, ui.BlendMode blendMode) | ||
: _color = color, | ||
_blendMode = blendMode, | ||
_matrix = null, | ||
_type = _TypeMode; | ||
|
||
/// Construct a color filter that transforms a color by a 5x5 matrix, where | ||
/// the fifth row is implicitly added in an identity configuration. | ||
/// | ||
/// Every pixel's color value, repsented as an `[R, G, B, A]`, is matrix | ||
/// multiplied to create a new color: | ||
/// | ||
/// ```text | ||
/// | R' | | a00 a01 a02 a03 a04 | | R | | ||
/// | G' | | a10 a11 a22 a33 a44 | | G | | ||
/// | B' | = | a20 a21 a22 a33 a44 | * | B | | ||
/// | A' | | a30 a31 a22 a33 a44 | | A | | ||
/// | 1 | | 0 0 0 0 1 | | 1 | | ||
/// ``` | ||
/// | ||
/// The matrix is in row-major order and the translation column is specified | ||
/// in unnormalized, 0...255, space. For example, the identity matrix is: | ||
/// | ||
/// ``` | ||
/// const ColorMatrix identity = ColorFilter.matrix(<double>[ | ||
/// 1, 0, 0, 0, 0, | ||
/// 0, 1, 0, 0, 0, | ||
/// 0, 0, 1, 0, 0, | ||
/// 0, 0, 0, 1, 0, | ||
/// ]); | ||
/// ``` | ||
/// | ||
/// ## Examples | ||
/// | ||
/// An inversion color matrix: | ||
/// | ||
/// ``` | ||
/// const ColorFilter invert = ColorFilter.matrix(<double>[ | ||
/// -1, 0, 0, 0, 255, | ||
/// 0, -1, 0, 0, 255, | ||
/// 0, 0, -1, 0, 255, | ||
/// 0, 0, 0, 1, 0, | ||
/// ]); | ||
/// ``` | ||
/// | ||
/// A sepia-toned color matrix (values based on the [Filter Effects Spec](https://www.w3.org/TR/filter-effects-1/#sepiaEquivalent)): | ||
/// | ||
/// ``` | ||
/// const ColorFilter sepia = ColorFilter.matrix(<double>[ | ||
/// 0.393, 0.769, 0.189, 0, 0, | ||
/// 0.349, 0.686, 0.168, 0, 0, | ||
/// 0.272, 0.534, 0.131, 0, 0, | ||
/// 0, 0, 0, 1, 0, | ||
/// ]); | ||
/// ``` | ||
/// | ||
/// A greyscale color filter (values based on the [Filter Effects Spec](https://www.w3.org/TR/filter-effects-1/#grayscaleEquivalent)): | ||
/// | ||
/// ``` | ||
/// const ColorFilter greyscale = ColorFilter.matrix(<double>[ | ||
/// 0.2126, 0.7152, 0.0722, 0, 0, | ||
/// 0.2126, 0.7152, 0.0722, 0, 0, | ||
/// 0.2126, 0.7152, 0.0722, 0, 0, | ||
/// 0, 0, 0, 1, 0, | ||
/// ]); | ||
/// ``` | ||
const EngineColorFilter.matrix(List<double> matrix) | ||
: _color = null, | ||
_blendMode = null, | ||
_matrix = matrix, | ||
_type = _TypeMatrix; | ||
|
||
/// Construct a color filter that applies the sRGB gamma curve to the RGB | ||
/// channels. | ||
const EngineColorFilter.linearToSrgbGamma() | ||
: _color = null, | ||
_blendMode = null, | ||
_matrix = null, | ||
_type = _TypeLinearToSrgbGamma; | ||
|
||
/// Creates a color filter that applies the inverse of the sRGB gamma curve | ||
/// to the RGB channels. | ||
const EngineColorFilter.srgbToLinearGamma() | ||
: _color = null, | ||
_blendMode = null, | ||
_matrix = null, | ||
_type = _TypeSrgbToLinearGamma; | ||
|
||
final ui.Color _color; | ||
final ui.BlendMode _blendMode; | ||
final List<double> _matrix; | ||
final int _type; | ||
|
||
// The type of SkColorFilter class to create for Skia. | ||
// These constants must be kept in sync with ColorFilterType in paint.cc. | ||
static const int _TypeNone = 0; // null | ||
static const int _TypeMode = 1; // MakeModeFilter | ||
static const int _TypeMatrix = 2; // MakeMatrixFilterRowMajor255 | ||
static const int _TypeLinearToSrgbGamma = 3; // MakeLinearToSRGBGamma | ||
static const int _TypeSrgbToLinearGamma = 4; // MakeSRGBToLinearGamma | ||
|
||
@override | ||
bool operator ==(dynamic other) { | ||
if (other is! EngineColorFilter) { | ||
return false; | ||
} | ||
final EngineColorFilter typedOther = other; | ||
|
||
if (_type != typedOther._type) { | ||
return false; | ||
} | ||
if (!_listEquals<double>(_matrix, typedOther._matrix)) { | ||
return false; | ||
} | ||
|
||
return _color == typedOther._color && _blendMode == typedOther._blendMode; | ||
} | ||
|
||
SkColorFilter _toSkColorFilter() { | ||
switch (_type) { | ||
case _TypeMode: | ||
if (_color == null || _blendMode == null) { | ||
return null; | ||
} | ||
return SkColorFilter.mode(this); | ||
case _TypeMatrix: | ||
if (_matrix == null) { | ||
return null; | ||
} | ||
assert(_matrix.length == 20, 'Color Matrix must have 20 entries.'); | ||
return SkColorFilter.matrix(this); | ||
case _TypeLinearToSrgbGamma: | ||
return SkColorFilter.linearToSrgbGamma(this); | ||
case _TypeSrgbToLinearGamma: | ||
return SkColorFilter.srgbToLinearGamma(this); | ||
default: | ||
throw StateError('Unknown mode $_type for ColorFilter.'); | ||
} | ||
} | ||
|
||
@override | ||
int get hashCode => ui.hashValues(_color, _blendMode, ui.hashList(_matrix), _type); | ||
|
||
@override | ||
String toString() { | ||
switch (_type) { | ||
case _TypeMode: | ||
return 'ColorFilter.mode($_color, $_blendMode)'; | ||
case _TypeMatrix: | ||
return 'ColorFilter.matrix($_matrix)'; | ||
case _TypeLinearToSrgbGamma: | ||
return 'ColorFilter.linearToSrgbGamma()'; | ||
case _TypeSrgbToLinearGamma: | ||
return 'ColorFilter.srgbToLinearGamma()'; | ||
default: | ||
return 'Unknown ColorFilter type. This is an error. If you\'re seeing this, please file an issue at https://github.com/flutter/flutter/issues/new.'; | ||
} | ||
} | ||
|
||
List<dynamic> webOnlySerializeToCssPaint() { | ||
throw UnsupportedError('ColorFilter for CSS paint not yet supported'); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// 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; | ||
|
||
/// A [ui.ColorFilter] backed by Skia's [SkColorFilter]. | ||
class SkColorFilter { | ||
js.JsObject skColorFilter; | ||
|
||
SkColorFilter.mode(EngineColorFilter filter) { | ||
skColorFilter = | ||
canvasKit['SkColorFilter'].callMethod('MakeBlend', <dynamic>[ | ||
filter._color.value, | ||
makeSkBlendMode(filter._blendMode), | ||
]); | ||
} | ||
|
||
SkColorFilter.matrix(EngineColorFilter filter) { | ||
// TODO(het): Find a way to remove these array conversions. | ||
final js.JsArray colorMatrix = js.JsArray(); | ||
colorMatrix.length = 20; | ||
for (int i = 0; i < 20; i++) { | ||
colorMatrix[i] = filter._matrix[i]; | ||
} | ||
skColorFilter = canvasKit['SkColorFilter'] | ||
.callMethod('MakeMatrix', <js.JsArray>[colorMatrix]); | ||
} | ||
|
||
SkColorFilter.linearToSrgbGamma(EngineColorFilter filter) { | ||
skColorFilter = canvasKit['SkColorFilter'].callMethod('MakeLinearToSRGBGamma'); | ||
} | ||
|
||
SkColorFilter.srgbToLinearGamma(EngineColorFilter filter) { | ||
skColorFilter = canvasKit['SkColorFilter'].callMethod('MakeSRGBToLinearGamma'); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's leave a todo to remove this double-conversion. This should, at least, convert straight to a
Float64List
, but ideally we should write to WASM memory directly./cc @kjlubick - here's an example for excessive conversion that we talked about at the last sync.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.