Refactor gradient renderer to produce image. Implement gradient based Shadermask (flutter/engine#24029)
This commit is contained in:
@@ -502,6 +502,7 @@ FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/recording_canvas.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/render_vertices.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/scene.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/scene_builder.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/shader_mask.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/shaders/normalized_gradient.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/shaders/shader.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/shaders/shader_builder.dart
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
repository: https://github.com/flutter/goldens.git
|
||||
revision: ec80c8042759905a5215ab1cd87ad280e8ef3cd7
|
||||
revision: cb8e029f9a5ebe0608f78fcdf20754f6ef261c81
|
||||
|
||||
@@ -91,6 +91,7 @@ part 'engine/html/recording_canvas.dart';
|
||||
part 'engine/html/render_vertices.dart';
|
||||
part 'engine/html/scene.dart';
|
||||
part 'engine/html/scene_builder.dart';
|
||||
part 'engine/html/shader_mask.dart';
|
||||
part 'engine/html/shaders/normalized_gradient.dart';
|
||||
part 'engine/html/shaders/shader.dart';
|
||||
part 'engine/html/shaders/shader_builder.dart';
|
||||
|
||||
@@ -71,7 +71,8 @@ class BitmapCanvas extends EngineCanvas {
|
||||
|
||||
/// Keeps track of what device pixel ratio was used when this [BitmapCanvas]
|
||||
/// was created.
|
||||
final double _devicePixelRatio = EnginePlatformDispatcher.browserDevicePixelRatio;
|
||||
final double _devicePixelRatio =
|
||||
EnginePlatformDispatcher.browserDevicePixelRatio;
|
||||
|
||||
// Compensation for [_initializeViewport] snapping canvas position to 1 pixel.
|
||||
int? _canvasPositionX, _canvasPositionY;
|
||||
@@ -177,13 +178,15 @@ class BitmapCanvas extends EngineCanvas {
|
||||
|
||||
static int _widthToPhysical(double width) {
|
||||
final double boundsWidth = width + 1;
|
||||
return (boundsWidth * EnginePlatformDispatcher.browserDevicePixelRatio).ceil() +
|
||||
return (boundsWidth * EnginePlatformDispatcher.browserDevicePixelRatio)
|
||||
.ceil() +
|
||||
2 * kPaddingPixels;
|
||||
}
|
||||
|
||||
static int _heightToPhysical(double height) {
|
||||
final double boundsHeight = height + 1;
|
||||
return (boundsHeight * EnginePlatformDispatcher.browserDevicePixelRatio).ceil() +
|
||||
return (boundsHeight * EnginePlatformDispatcher.browserDevicePixelRatio)
|
||||
.ceil() +
|
||||
2 * kPaddingPixels;
|
||||
}
|
||||
|
||||
@@ -227,7 +230,8 @@ class BitmapCanvas extends EngineCanvas {
|
||||
/// * [PersistedPicture._recycleCanvas] which also uses this method
|
||||
/// for the same reason.
|
||||
bool isReusable() {
|
||||
return _devicePixelRatio == EnginePlatformDispatcher.browserDevicePixelRatio;
|
||||
return _devicePixelRatio ==
|
||||
EnginePlatformDispatcher.browserDevicePixelRatio;
|
||||
}
|
||||
|
||||
/// Returns a "data://" URI containing a representation of the image in this
|
||||
@@ -336,8 +340,10 @@ class BitmapCanvas extends EngineCanvas {
|
||||
/// prefer DOM if canvas has not been allocated yet.
|
||||
///
|
||||
bool _useDomForRenderingFill(SurfacePaintData paint) =>
|
||||
_renderStrategy.isInsideShaderMask ||
|
||||
(_preserveImageData == false && _contains3dTransform) ||
|
||||
(_childOverdraw && _canvasPool._canvas == null &&
|
||||
(_childOverdraw &&
|
||||
_canvasPool._canvas == null &&
|
||||
paint.maskFilter == null &&
|
||||
paint.shader == null &&
|
||||
paint.style != ui.PaintingStyle.stroke);
|
||||
@@ -346,12 +352,14 @@ class BitmapCanvas extends EngineCanvas {
|
||||
///
|
||||
/// DOM canvas is generated for simple strokes using borders.
|
||||
bool _useDomForRenderingFillAndStroke(SurfacePaintData paint) =>
|
||||
_renderStrategy.isInsideShaderMask ||
|
||||
(_preserveImageData == false && _contains3dTransform) ||
|
||||
((_childOverdraw || _renderStrategy.hasImageElements ||
|
||||
((_childOverdraw ||
|
||||
_renderStrategy.hasImageElements ||
|
||||
_renderStrategy.hasParagraphs) &&
|
||||
_canvasPool._canvas == null &&
|
||||
paint.maskFilter == null &&
|
||||
paint.shader == null);
|
||||
_canvasPool._canvas == null &&
|
||||
paint.maskFilter == null &&
|
||||
paint.shader == null);
|
||||
|
||||
@override
|
||||
void drawColor(ui.Color color, ui.BlendMode blendMode) {
|
||||
@@ -373,8 +381,8 @@ class BitmapCanvas extends EngineCanvas {
|
||||
..lineTo(p2.dx, p2.dy);
|
||||
drawPath(path, paint);
|
||||
} else {
|
||||
ui.Rect? shaderBounds = (paint.shader != null) ?
|
||||
ui.Rect.fromPoints(p1, p2) : null;
|
||||
ui.Rect? shaderBounds =
|
||||
(paint.shader != null) ? ui.Rect.fromPoints(p1, p2) : null;
|
||||
_setUpPaint(paint, shaderBounds);
|
||||
_canvasPool.strokeLine(p1, p2);
|
||||
_tearDownPaint();
|
||||
@@ -386,8 +394,8 @@ class BitmapCanvas extends EngineCanvas {
|
||||
if (_useDomForRenderingFill(paint)) {
|
||||
drawRect(_computeScreenBounds(_canvasPool._currentTransform), paint);
|
||||
} else {
|
||||
ui.Rect? shaderBounds = (paint.shader != null) ?
|
||||
_computePictureBounds() : null;
|
||||
ui.Rect? shaderBounds =
|
||||
(paint.shader != null) ? _computePictureBounds() : null;
|
||||
_setUpPaint(paint, shaderBounds);
|
||||
_canvasPool.fill();
|
||||
_tearDownPaint();
|
||||
@@ -450,8 +458,8 @@ class BitmapCanvas extends EngineCanvas {
|
||||
math.min(rect.left, rect.right), math.min(rect.top, rect.bottom)),
|
||||
paint);
|
||||
} else {
|
||||
_setUpPaint(paint, rrect.outerRect);
|
||||
_canvasPool.drawRRect(rrect, paint.style);
|
||||
_setUpPaint(paint, rrect.outerRect);
|
||||
_canvasPool.drawRRect(rrect, paint.style);
|
||||
_tearDownPaint();
|
||||
}
|
||||
}
|
||||
@@ -495,8 +503,11 @@ class BitmapCanvas extends EngineCanvas {
|
||||
paint);
|
||||
element.style.borderRadius = '50%';
|
||||
} else {
|
||||
_setUpPaint(paint, paint.shader != null
|
||||
? ui.Rect.fromCircle(center: c, radius: radius) : null);
|
||||
_setUpPaint(
|
||||
paint,
|
||||
paint.shader != null
|
||||
? ui.Rect.fromCircle(center: c, radius: radius)
|
||||
: null);
|
||||
_canvasPool.drawCircle(c, radius, paint.style);
|
||||
_tearDownPaint();
|
||||
}
|
||||
@@ -509,16 +520,18 @@ class BitmapCanvas extends EngineCanvas {
|
||||
final SurfacePath surfacePath = path as SurfacePath;
|
||||
final ui.Rect? pathAsLine = surfacePath.toStraightLine();
|
||||
if (pathAsLine != null) {
|
||||
final ui.Rect rect = (pathAsLine.top == pathAsLine.bottom) ?
|
||||
ui.Rect.fromLTWH(pathAsLine.left, pathAsLine.top, pathAsLine.width, 1)
|
||||
: ui.Rect.fromLTWH(pathAsLine.left, pathAsLine.top, 1, pathAsLine.height);
|
||||
final ui.Rect rect = (pathAsLine.top == pathAsLine.bottom)
|
||||
? ui.Rect.fromLTWH(
|
||||
pathAsLine.left, pathAsLine.top, pathAsLine.width, 1)
|
||||
: ui.Rect.fromLTWH(
|
||||
pathAsLine.left, pathAsLine.top, 1, pathAsLine.height);
|
||||
|
||||
html.HtmlElement element = _buildDrawRectElement(
|
||||
rect, paint, 'draw-rect', _canvasPool._currentTransform);
|
||||
_drawElement(
|
||||
element,
|
||||
ui.Offset(
|
||||
math.min(rect.left, rect.right), math.min(rect.top, rect.bottom)),
|
||||
ui.Offset(math.min(rect.left, rect.right),
|
||||
math.min(rect.top, rect.bottom)),
|
||||
paint);
|
||||
return;
|
||||
}
|
||||
@@ -527,7 +540,7 @@ class BitmapCanvas extends EngineCanvas {
|
||||
drawRect(pathAsRect, paint);
|
||||
return;
|
||||
}
|
||||
final ui.RRect ? pathAsRRect = surfacePath.toRoundedRect();
|
||||
final ui.RRect? pathAsRRect = surfacePath.toRoundedRect();
|
||||
if (pathAsRRect != null) {
|
||||
drawRRect(pathAsRRect, paint);
|
||||
return;
|
||||
@@ -560,8 +573,10 @@ class BitmapCanvas extends EngineCanvas {
|
||||
void _applyFilter(html.Element element, SurfacePaintData paint) {
|
||||
if (paint.maskFilter != null) {
|
||||
final bool isStroke = paint.style == ui.PaintingStyle.stroke;
|
||||
String cssColor =
|
||||
paint.color == null ? '#000000' : colorToCssString(paint.color)!;// ignore: unnecessary_null_comparison
|
||||
String cssColor = paint.color == null
|
||||
? '#000000'
|
||||
: colorToCssString(
|
||||
paint.color)!; // ignore: unnecessary_null_comparison
|
||||
final double sigma = paint.maskFilter!.webOnlySigma;
|
||||
if (browserEngine == BrowserEngine.webkit && !isStroke) {
|
||||
// A bug in webkit leaves artifacts when this element is animated
|
||||
@@ -616,7 +631,8 @@ class BitmapCanvas extends EngineCanvas {
|
||||
ui.Image image, ui.Offset p, SurfacePaintData paint) {
|
||||
final HtmlImage htmlImage = image as HtmlImage;
|
||||
final ui.BlendMode? blendMode = paint.blendMode;
|
||||
final EngineColorFilter? colorFilter = paint.colorFilter as EngineColorFilter?;
|
||||
final EngineColorFilter? colorFilter =
|
||||
paint.colorFilter as EngineColorFilter?;
|
||||
html.HtmlElement imgElement;
|
||||
if (colorFilter is _CkBlendModeColorFilter) {
|
||||
switch (colorFilter.blendMode) {
|
||||
@@ -633,12 +649,12 @@ class BitmapCanvas extends EngineCanvas {
|
||||
case ui.BlendMode.color:
|
||||
case ui.BlendMode.luminosity:
|
||||
case ui.BlendMode.xor:
|
||||
imgElement = _createImageElementWithSvgFilter(image,
|
||||
colorFilter.color, colorFilter.blendMode, paint);
|
||||
imgElement = _createImageElementWithSvgFilter(
|
||||
image, colorFilter.color, colorFilter.blendMode, paint);
|
||||
break;
|
||||
default:
|
||||
imgElement = _createBackgroundImageWithBlend(image,
|
||||
colorFilter.color, colorFilter.blendMode, paint);
|
||||
imgElement = _createBackgroundImageWithBlend(
|
||||
image, colorFilter.color, colorFilter.blendMode, paint);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
@@ -976,8 +992,8 @@ class BitmapCanvas extends EngineCanvas {
|
||||
|
||||
final double dpr = ui.window.devicePixelRatio;
|
||||
// Use hairline (device pixel when strokeWidth is not specified).
|
||||
final double strokeWidth = paint.strokeWidth == null ? 1.0 / dpr
|
||||
: paint.strokeWidth!;
|
||||
final double strokeWidth =
|
||||
paint.strokeWidth == null ? 1.0 / dpr : paint.strokeWidth!;
|
||||
_drawPointsPaint.strokeWidth = strokeWidth;
|
||||
_setUpPaint(_drawPointsPaint, null);
|
||||
// Draw point using circle with half radius.
|
||||
@@ -1020,13 +1036,13 @@ class BitmapCanvas extends EngineCanvas {
|
||||
Vector3 bottomLeft = inverted.perspectiveTransform(Vector3(0, height, 0));
|
||||
return ui.Rect.fromLTRB(
|
||||
math.min(topLeft.x,
|
||||
math.min(topRight.x, math.min(bottomRight.x, bottomLeft.x))),
|
||||
math.min(topRight.x, math.min(bottomRight.x, bottomLeft.x))),
|
||||
math.min(topLeft.y,
|
||||
math.min(topRight.y, math.min(bottomRight.y, bottomLeft.y))),
|
||||
math.min(topRight.y, math.min(bottomRight.y, bottomLeft.y))),
|
||||
math.max(topLeft.x,
|
||||
math.max(topRight.x, math.max(bottomRight.x, bottomLeft.x))),
|
||||
math.max(topRight.x, math.max(bottomRight.x, bottomLeft.x))),
|
||||
math.max(topLeft.y,
|
||||
math.max(topRight.y, math.max(bottomRight.y, bottomLeft.y))),
|
||||
math.max(topRight.y, math.max(bottomRight.y, bottomLeft.y))),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -351,9 +351,30 @@ class ImageFilterEngineLayer extends ContainerLayer implements ui.ImageFilterEng
|
||||
}
|
||||
|
||||
class ShaderMaskEngineLayer extends ContainerLayer implements ui.ShaderMaskEngineLayer {
|
||||
ShaderMaskEngineLayer(this.shader, this.maskRect, this.blendMode);
|
||||
|
||||
final ui.Shader shader;
|
||||
final ui.Rect maskRect;
|
||||
final ui.BlendMode blendMode;
|
||||
|
||||
@override
|
||||
void paint(PaintContext paintContext) {
|
||||
// TODO(yjbanov): this needs to be implemented
|
||||
assert(needsPainting);
|
||||
|
||||
paintContext.internalNodesCanvas.saveLayer(paintBounds, null);
|
||||
paintChildren(paintContext);
|
||||
|
||||
CkPaint paint = CkPaint();
|
||||
paint.shader = shader;
|
||||
paint.blendMode = blendMode;
|
||||
|
||||
paintContext.leafNodesCanvas!.save();
|
||||
paintContext.leafNodesCanvas!.translate(maskRect.left, maskRect.top);
|
||||
|
||||
paintContext.leafNodesCanvas!.drawRect(maskRect, paint);
|
||||
paintContext.leafNodesCanvas!.restore();
|
||||
|
||||
paintContext.internalNodesCanvas.restore();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -193,7 +193,8 @@ class LayerSceneBuilder implements ui.SceneBuilder {
|
||||
ui.BlendMode blendMode, {
|
||||
ui.EngineLayer? oldLayer,
|
||||
}) {
|
||||
throw UnimplementedError();
|
||||
return pushLayer<ShaderMaskEngineLayer>(ShaderMaskEngineLayer(
|
||||
shader, maskRect, blendMode));
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -255,7 +255,7 @@ html.Element _pathToSvgElement(SurfacePath path, SurfacePaintData paint,
|
||||
final ui.Color color = paint.color ?? const ui.Color(0xFF000000);
|
||||
if (paint.style == ui.PaintingStyle.stroke ||
|
||||
(paint.style != ui.PaintingStyle.fill &&
|
||||
paint.strokeWidth != 0)) {
|
||||
paint.strokeWidth != 0 && paint.strokeWidth != null)) {
|
||||
sb.write('stroke="${colorToCssString(color)}" ');
|
||||
sb.write('stroke-width="${paint.strokeWidth ?? 1.0}" ');
|
||||
sb.write('fill="none" ');
|
||||
|
||||
@@ -96,6 +96,7 @@ class PersistedPicture extends PersistedLeafSurface {
|
||||
final ui.Rect? localPaintBounds;
|
||||
final int hints;
|
||||
double _density = 1.0;
|
||||
|
||||
/// Cull rect changes and density changes due to transforms should
|
||||
/// call applyPaint for picture when retain() or update() is called after
|
||||
/// preroll is complete.
|
||||
@@ -110,6 +111,14 @@ class PersistedPicture extends PersistedLeafSurface {
|
||||
return defaultCreateElement('flt-picture');
|
||||
}
|
||||
|
||||
@override
|
||||
void preroll() {
|
||||
if (PersistedShaderMask.activeShaderMaskCount != 0) {
|
||||
picture.recordingCanvas?.renderStrategy.isInsideShaderMask = true;
|
||||
}
|
||||
super.preroll();
|
||||
}
|
||||
|
||||
@override
|
||||
void recomputeTransformAndClip() {
|
||||
_transform = parent!._transform;
|
||||
@@ -119,8 +128,10 @@ class PersistedPicture extends PersistedLeafSurface {
|
||||
}
|
||||
final double paintWidth = localPaintBounds!.width;
|
||||
final double paintHeight = localPaintBounds!.height;
|
||||
final double newDensity = localPaintBounds == null || paintWidth == 0 || paintHeight == 0
|
||||
? 1.0 : _computePixelDensity(_transform, paintWidth, paintHeight);
|
||||
final double newDensity =
|
||||
localPaintBounds == null || paintWidth == 0 || paintHeight == 0
|
||||
? 1.0
|
||||
: _computePixelDensity(_transform, paintWidth, paintHeight);
|
||||
if (newDensity != _density) {
|
||||
_density = newDensity;
|
||||
_requiresRepaint = true;
|
||||
@@ -353,8 +364,8 @@ class PersistedPicture extends PersistedLeafSurface {
|
||||
return 1.0;
|
||||
}
|
||||
|
||||
final bool didRequireBitmap =
|
||||
existingSurface.picture.recordingCanvas!.renderStrategy.hasArbitraryPaint;
|
||||
final bool didRequireBitmap = existingSurface
|
||||
.picture.recordingCanvas!.renderStrategy.hasArbitraryPaint;
|
||||
final bool requiresBitmap =
|
||||
picture.recordingCanvas!.renderStrategy.hasArbitraryPaint;
|
||||
if (didRequireBitmap != requiresBitmap) {
|
||||
@@ -524,8 +535,9 @@ class PersistedPicture extends PersistedLeafSurface {
|
||||
if (_debugShowCanvasReuseStats) {
|
||||
DebugCanvasReuseOverlay.instance.createdCount++;
|
||||
}
|
||||
final BitmapCanvas canvas = BitmapCanvas(bounds,
|
||||
picture.recordingCanvas!.renderStrategy, density: _density);
|
||||
final BitmapCanvas canvas = BitmapCanvas(
|
||||
bounds, picture.recordingCanvas!.renderStrategy,
|
||||
density: _density);
|
||||
canvas.setElementCache(_elementCache);
|
||||
if (_debugExplainSurfaceStats) {
|
||||
_surfaceStatsFor(this)
|
||||
@@ -568,9 +580,8 @@ class PersistedPicture extends PersistedLeafSurface {
|
||||
|
||||
_computeOptimalCullRect(oldSurface);
|
||||
if (identical(picture, oldSurface.picture)) {
|
||||
bool densityChanged =
|
||||
(_canvas is BitmapCanvas &&
|
||||
_density != (_canvas as BitmapCanvas)._density);
|
||||
bool densityChanged = (_canvas is BitmapCanvas &&
|
||||
_density != (_canvas as BitmapCanvas)._density);
|
||||
|
||||
// The picture is the same. Attempt to avoid repaint.
|
||||
if (_requiresRepaint || densityChanged) {
|
||||
|
||||
@@ -20,8 +20,8 @@ double _measureBorderRadius(double x, double y) {
|
||||
}
|
||||
|
||||
class RawRecordingCanvas extends BitmapCanvas implements ui.PictureRecorder {
|
||||
RawRecordingCanvas(ui.Size size) : super(ui.Offset.zero & size,
|
||||
RenderStrategy());
|
||||
RawRecordingCanvas(ui.Size size)
|
||||
: super(ui.Offset.zero & size, RenderStrategy());
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
@@ -1934,5 +1934,12 @@ class RenderStrategy {
|
||||
/// This is used to decide whether to use simplified DomCanvas.
|
||||
bool hasArbitraryPaint = false;
|
||||
|
||||
/// Whether commands are executed within a shadermask.
|
||||
///
|
||||
/// Webkit doesn't apply filters to canvas elements in its child
|
||||
/// element tree. When this is set to true, we prevent canvas usage in
|
||||
/// bitmap canvas and instead render using dom primitives and svg only.
|
||||
bool isInsideShaderMask = false;
|
||||
|
||||
RenderStrategy();
|
||||
}
|
||||
|
||||
@@ -72,6 +72,9 @@ abstract class _GlRenderer {
|
||||
Object? drawRect(ui.Rect targetRect, _GlContext gl, _GlProgram glProgram,
|
||||
NormalizedGradient gradient, int widthInPixels, int heightInPixels);
|
||||
|
||||
String drawRectToImageUrl(ui.Rect targetRect, _GlContext gl, _GlProgram glProgram,
|
||||
NormalizedGradient gradient, int widthInPixels, int heightInPixels);
|
||||
|
||||
void drawHairline(html.CanvasRenderingContext2D? _ctx, Float32List positions);
|
||||
}
|
||||
|
||||
@@ -196,6 +199,30 @@ class _WebGlRenderer implements _GlRenderer {
|
||||
/// will return ImageBitmap, otherwise will return CanvasElement.
|
||||
Object? drawRect(ui.Rect targetRect, _GlContext gl, _GlProgram glProgram,
|
||||
NormalizedGradient gradient, int widthInPixels, int heightInPixels) {
|
||||
drawRectToGl(targetRect, gl, glProgram, gradient, widthInPixels, heightInPixels);
|
||||
Object? image = gl.readPatternData();
|
||||
gl.bindArrayBuffer(null);
|
||||
gl.bindElementArrayBuffer(null);
|
||||
return image;
|
||||
}
|
||||
|
||||
/// Renders a rectangle using given program into an image resource and returns
|
||||
/// url.
|
||||
String drawRectToImageUrl(ui.Rect targetRect, _GlContext gl, _GlProgram glProgram,
|
||||
NormalizedGradient gradient, int widthInPixels, int heightInPixels) {
|
||||
drawRectToGl(targetRect, gl, glProgram, gradient, widthInPixels, heightInPixels);
|
||||
final String imageUrl = gl.toImageUrl();
|
||||
// Cleanup buffers.
|
||||
gl.bindArrayBuffer(null);
|
||||
gl.bindElementArrayBuffer(null);
|
||||
return imageUrl;
|
||||
}
|
||||
|
||||
/// Renders a rectangle using given program into [_GlContext].
|
||||
///
|
||||
/// Caller has to cleanup gl array and element array buffers.
|
||||
void drawRectToGl(ui.Rect targetRect, _GlContext gl, _GlProgram glProgram,
|
||||
NormalizedGradient gradient, int widthInPixels, int heightInPixels) {
|
||||
// Setup rectangle coordinates.
|
||||
final double left = targetRect.left;
|
||||
final double top = targetRect.top;
|
||||
@@ -261,13 +288,6 @@ class _WebGlRenderer implements _GlRenderer {
|
||||
gl.viewport(0, 0, widthInPixels.toDouble(), heightInPixels.toDouble());
|
||||
|
||||
gl.drawElements(gl.kTriangles, _vertexIndicesForRect.length, gl.kUnsignedShort);
|
||||
|
||||
Object? image = gl.readPatternData();
|
||||
|
||||
gl.bindArrayBuffer(null);
|
||||
gl.bindElementArrayBuffer(null);
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
/// Creates a vertex shader transforms pixel space [Vertices.positions] to
|
||||
@@ -789,6 +809,17 @@ class _GlContext {
|
||||
return canvas;
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns image data in data url format.
|
||||
String toImageUrl() {
|
||||
html.CanvasElement canvas = html.CanvasElement(width: _widthInPixels, height: _heightInPixels);
|
||||
final html.CanvasRenderingContext2D ctx = canvas.context2D;
|
||||
drawImage(ctx, 0, 0);
|
||||
final String dataUrl = canvas.toDataUrl();
|
||||
canvas.width = 0;
|
||||
canvas.height = 0;
|
||||
return dataUrl;
|
||||
}
|
||||
}
|
||||
|
||||
/// Polyfill for html.OffscreenCanvas that is not supported on some browsers.
|
||||
|
||||
@@ -228,7 +228,10 @@ class SurfaceSceneBuilder implements ui.SceneBuilder {
|
||||
ui.BlendMode blendMode, {
|
||||
ui.ShaderMaskEngineLayer? oldLayer,
|
||||
}) {
|
||||
throw UnimplementedError();
|
||||
assert(shader != null && maskRect != null && blendMode != null); // ignore: unnecessary_null_comparison
|
||||
return _pushSurface<PersistedShaderMask>(PersistedShaderMask(
|
||||
oldLayer as PersistedShaderMask?,
|
||||
shader, maskRect, blendMode));
|
||||
}
|
||||
|
||||
/// Pushes a physical layer operation for an arbitrary shape onto the
|
||||
|
||||
@@ -0,0 +1,355 @@
|
||||
// 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.
|
||||
|
||||
// @dart = 2.12
|
||||
part of engine;
|
||||
|
||||
/// A surface that applies a shader to its children.
|
||||
///
|
||||
/// Currently there are 2 types of shaders:
|
||||
/// - Gradients
|
||||
/// - ImageShader
|
||||
///
|
||||
/// Gradients
|
||||
/// The gradients can be applied to the child tree by rendering the gradient
|
||||
/// into an image and referencing the image in an svg filter to apply
|
||||
/// to DOM tree.
|
||||
///
|
||||
class PersistedShaderMask extends PersistedContainerSurface
|
||||
implements ui.ShaderMaskEngineLayer {
|
||||
PersistedShaderMask(
|
||||
PersistedShaderMask? oldLayer,
|
||||
this.shader,
|
||||
this.maskRect,
|
||||
this.blendMode,
|
||||
) : super(oldLayer);
|
||||
|
||||
html.Element? _childContainer;
|
||||
final ui.Shader shader;
|
||||
final ui.Rect maskRect;
|
||||
final ui.BlendMode blendMode;
|
||||
html.Element? _shaderElement;
|
||||
static int activeShaderMaskCount = 0;
|
||||
final bool isWebKit = browserEngine == BrowserEngine.webkit;
|
||||
|
||||
@override
|
||||
void adoptElements(PersistedShaderMask oldSurface) {
|
||||
super.adoptElements(oldSurface);
|
||||
_childContainer = oldSurface._childContainer;
|
||||
_shaderElement = oldSurface._shaderElement;
|
||||
oldSurface._childContainer = null;
|
||||
oldSurface._shaderElement = null;
|
||||
}
|
||||
|
||||
@override
|
||||
html.Element? get childContainer => _childContainer;
|
||||
|
||||
@override
|
||||
void discard() {
|
||||
super.discard();
|
||||
// Do not detach the child container from the root. It is permanently
|
||||
// attached. The elements are reused together and are detached from the DOM
|
||||
// together.
|
||||
_childContainer = null;
|
||||
}
|
||||
|
||||
@override
|
||||
void preroll() {
|
||||
++activeShaderMaskCount;
|
||||
super.preroll();
|
||||
--activeShaderMaskCount;
|
||||
}
|
||||
|
||||
@override
|
||||
html.Element createElement() {
|
||||
html.Element element = defaultCreateElement('flt-shader-mask');
|
||||
html.Element container = html.Element.tag('flt-mask-interior');
|
||||
container.style..position = 'absolute';
|
||||
_childContainer = container;
|
||||
element.append(_childContainer!);
|
||||
return element;
|
||||
}
|
||||
|
||||
@override
|
||||
void apply() {
|
||||
_shaderElement?.remove();
|
||||
_shaderElement = null;
|
||||
if (shader is ui.Gradient) {
|
||||
rootElement!.style
|
||||
..left = '${maskRect.left}px'
|
||||
..top = '${maskRect.top}px'
|
||||
..width = '${maskRect.width}px'
|
||||
..height = '${maskRect.height}px';
|
||||
_childContainer!.style
|
||||
..left = '${-maskRect.left}px'
|
||||
..top = '${-maskRect.top}px';
|
||||
// Prevent ShaderMask from failing inside animations that size
|
||||
// area to empty.
|
||||
if (maskRect.width > 0 && maskRect.height > 0) {
|
||||
_applyGradientShader();
|
||||
}
|
||||
return;
|
||||
}
|
||||
// TODO: Implement _applyImageShader();
|
||||
throw Exception('Shader type not supported for ShaderMask');
|
||||
}
|
||||
|
||||
void _applyGradientShader() {
|
||||
if (shader is EngineGradient) {
|
||||
EngineGradient gradientShader = shader as EngineGradient;
|
||||
final String imageUrl =
|
||||
gradientShader.createImageBitmap(maskRect, 1, true) as String;
|
||||
ui.BlendMode blendModeTemp = blendMode;
|
||||
switch (blendModeTemp) {
|
||||
case ui.BlendMode.clear:
|
||||
case ui.BlendMode.dstOut:
|
||||
case ui.BlendMode.srcOut:
|
||||
childContainer?.style.visibility = 'hidden';
|
||||
return;
|
||||
case ui.BlendMode.dst:
|
||||
case ui.BlendMode.dstIn:
|
||||
// Noop. Should render existing destination.
|
||||
rootElement!.style.filter = '';
|
||||
return;
|
||||
case ui.BlendMode.srcOver:
|
||||
// Uses source filter color.
|
||||
// Since we don't have a size, we can't use background color.
|
||||
// Use svg filter srcIn instead.
|
||||
blendModeTemp = ui.BlendMode.srcIn;
|
||||
break;
|
||||
case ui.BlendMode.src:
|
||||
case ui.BlendMode.dstOver:
|
||||
case ui.BlendMode.srcIn:
|
||||
case ui.BlendMode.srcATop:
|
||||
case ui.BlendMode.dstATop:
|
||||
case ui.BlendMode.xor:
|
||||
case ui.BlendMode.plus:
|
||||
case ui.BlendMode.modulate:
|
||||
case ui.BlendMode.screen:
|
||||
case ui.BlendMode.overlay:
|
||||
case ui.BlendMode.darken:
|
||||
case ui.BlendMode.lighten:
|
||||
case ui.BlendMode.colorDodge:
|
||||
case ui.BlendMode.colorBurn:
|
||||
case ui.BlendMode.hardLight:
|
||||
case ui.BlendMode.softLight:
|
||||
case ui.BlendMode.difference:
|
||||
case ui.BlendMode.exclusion:
|
||||
case ui.BlendMode.multiply:
|
||||
case ui.BlendMode.hue:
|
||||
case ui.BlendMode.saturation:
|
||||
case ui.BlendMode.color:
|
||||
case ui.BlendMode.luminosity:
|
||||
break;
|
||||
}
|
||||
|
||||
String code = svgMaskFilterFromImageAndBlendMode(
|
||||
imageUrl, blendModeTemp, maskRect.width, maskRect.height)!;
|
||||
|
||||
_shaderElement =
|
||||
html.Element.html(code, treeSanitizer: _NullTreeSanitizer());
|
||||
if (isWebKit) {
|
||||
_childContainer!.style.filter = 'url(#_fmf${_maskFilterIdCounter}';
|
||||
} else {
|
||||
rootElement!.style.filter = 'url(#_fmf${_maskFilterIdCounter}';
|
||||
}
|
||||
rootElement!.append(_shaderElement!);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void update(PersistedShaderMask oldSurface) {
|
||||
super.update(oldSurface);
|
||||
if (shader != oldSurface.shader ||
|
||||
maskRect != oldSurface.maskRect ||
|
||||
blendMode != oldSurface.blendMode) {
|
||||
apply();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String? svgMaskFilterFromImageAndBlendMode(
|
||||
String imageUrl, ui.BlendMode blendMode, double width, double height) {
|
||||
String? svgFilter;
|
||||
switch (blendMode) {
|
||||
case ui.BlendMode.src:
|
||||
svgFilter = _srcImageToSvg(imageUrl, width, height);
|
||||
break;
|
||||
case ui.BlendMode.srcIn:
|
||||
case ui.BlendMode.srcATop:
|
||||
svgFilter = _srcInImageToSvg(imageUrl, width, height);
|
||||
break;
|
||||
case ui.BlendMode.srcOut:
|
||||
svgFilter = _srcOutImageToSvg(imageUrl, width, height);
|
||||
break;
|
||||
case ui.BlendMode.xor:
|
||||
svgFilter = _xorImageToSvg(imageUrl, width, height);
|
||||
break;
|
||||
case ui.BlendMode.plus:
|
||||
// Porter duff source + destination.
|
||||
svgFilter = _compositeImageToSvg(imageUrl, 0, 1, 1, 0, width, height);
|
||||
break;
|
||||
case ui.BlendMode.modulate:
|
||||
// Porter duff source * destination but preserves alpha.
|
||||
svgFilter = _modulateImageToSvg(imageUrl, width, height);
|
||||
break;
|
||||
case ui.BlendMode.overlay:
|
||||
// Since overlay is the same as hard-light by swapping layers,
|
||||
// pass hard-light blend function.
|
||||
svgFilter = _blendImageToSvg(imageUrl, 'hard-light', width, height,
|
||||
swapLayers: true);
|
||||
break;
|
||||
// Several of the filters below (although supported) do not render the
|
||||
// same (close but not exact) as native flutter when used as blend mode
|
||||
// for a background-image with a background color. They only look
|
||||
// identical when feBlend is used within an svg filter definition.
|
||||
//
|
||||
// Saturation filter uses destination when source is transparent.
|
||||
// cMax = math.max(r, math.max(b, g));
|
||||
// cMin = math.min(r, math.min(b, g));
|
||||
// delta = cMax - cMin;
|
||||
// lightness = (cMax + cMin) / 2.0;
|
||||
// saturation = delta / (1.0 - (2 * lightness - 1.0).abs());
|
||||
case ui.BlendMode.saturation:
|
||||
case ui.BlendMode.colorDodge:
|
||||
case ui.BlendMode.colorBurn:
|
||||
case ui.BlendMode.hue:
|
||||
case ui.BlendMode.color:
|
||||
case ui.BlendMode.luminosity:
|
||||
case ui.BlendMode.multiply:
|
||||
case ui.BlendMode.screen:
|
||||
case ui.BlendMode.overlay:
|
||||
case ui.BlendMode.darken:
|
||||
case ui.BlendMode.lighten:
|
||||
case ui.BlendMode.colorDodge:
|
||||
case ui.BlendMode.colorBurn:
|
||||
case ui.BlendMode.hardLight:
|
||||
case ui.BlendMode.softLight:
|
||||
case ui.BlendMode.difference:
|
||||
case ui.BlendMode.exclusion:
|
||||
svgFilter = _blendImageToSvg(
|
||||
imageUrl, _stringForBlendMode(blendMode), width, height);
|
||||
break;
|
||||
case ui.BlendMode.src:
|
||||
case ui.BlendMode.dst:
|
||||
case ui.BlendMode.dstATop:
|
||||
case ui.BlendMode.dstIn:
|
||||
case ui.BlendMode.dstOut:
|
||||
case ui.BlendMode.dstOver:
|
||||
case ui.BlendMode.clear:
|
||||
case ui.BlendMode.srcOver:
|
||||
throw UnsupportedError(
|
||||
'Invalid svg filter request for blend-mode $blendMode');
|
||||
}
|
||||
return svgFilter;
|
||||
}
|
||||
|
||||
int _maskFilterIdCounter = 0;
|
||||
|
||||
String _svgFilterWrapper(String content) {
|
||||
_maskFilterIdCounter++;
|
||||
return '$kSvgResourceHeader<filter id="_fmf$_maskFilterIdCounter" '
|
||||
'filterUnits="objectBoundingBox">' +
|
||||
content +
|
||||
'</filter></svg>';
|
||||
}
|
||||
|
||||
/// SVG filters that contain feImage, will fail on several browsers
|
||||
/// (Firefox etc) if bounds are not specified. On Firefox percentage
|
||||
/// width/height 100% works however fails in Chrome 88. Webkit will
|
||||
/// not render if x/y/width/height is specified. So we return explicit size
|
||||
/// here unless running on webkit.
|
||||
String _buildFeBlend(double width, double height) =>
|
||||
browserEngine == BrowserEngine.webkit
|
||||
? ''
|
||||
: 'x="0" y="0" width="${width}px" height="${height}px"';
|
||||
|
||||
// The color matrix for feColorMatrix element changes colors based on
|
||||
// the following:
|
||||
//
|
||||
// | R' | | r1 r2 r3 r4 r5 | | R |
|
||||
// | G' | | g1 g2 g3 g4 g5 | | G |
|
||||
// | B' | = | b1 b2 b3 b4 b5 | * | B |
|
||||
// | A' | | a1 a2 a3 a4 a5 | | A |
|
||||
// | 1 | | 0 0 0 0 1 | | 1 |
|
||||
//
|
||||
// R' = r1*R + r2*G + r3*B + r4*A + r5
|
||||
// G' = g1*R + g2*G + g3*B + g4*A + g5
|
||||
// B' = b1*R + b2*G + b3*B + b4*A + b5
|
||||
// A' = a1*R + a2*G + a3*B + a4*A + a5
|
||||
String _srcInImageToSvg(String imageUrl, double width, double height) {
|
||||
return _svgFilterWrapper(
|
||||
'<feColorMatrix values="0 0 0 0 1 ' // Ignore input, set it to absolute.
|
||||
'0 0 0 0 1 '
|
||||
'0 0 0 0 1 '
|
||||
'0 0 0 1 0" result="destalpha"/>' // Just take alpha channel of destination
|
||||
'<feImage href="$imageUrl" result="image"'
|
||||
' ${_buildFeBlend(width, height)}>'
|
||||
'</feImage>'
|
||||
'<feComposite in="image" in2="destalpha" '
|
||||
'operator="arithmetic" k1="1" k2="0" k3="0" k4="0" result="comp">'
|
||||
'</feComposite>',
|
||||
);
|
||||
}
|
||||
|
||||
String _srcImageToSvg(String imageUrl, double width, double height) {
|
||||
return _svgFilterWrapper('<feImage href="$imageUrl" result="comp"'
|
||||
' ${_buildFeBlend(width, height)}>'
|
||||
'</feImage>');
|
||||
}
|
||||
|
||||
String _srcOutImageToSvg(String imageUrl, double width, double height) {
|
||||
return _svgFilterWrapper('<feImage href="$imageUrl" result="image"'
|
||||
' ${_buildFeBlend(width, height)}>'
|
||||
'</feImage>'
|
||||
'<feComposite in="image" in2="SourceGraphic" operator="out" result="comp">'
|
||||
'</feComposite>');
|
||||
}
|
||||
|
||||
String _xorImageToSvg(String imageUrl, double width, double height) {
|
||||
return _svgFilterWrapper('<feImage href="$imageUrl" result="image"'
|
||||
' ${_buildFeBlend(width, height)}>'
|
||||
'</feImage>'
|
||||
'<feComposite in="image" in2="SourceGraphic" operator="xor" result="comp">'
|
||||
'</feComposite>');
|
||||
}
|
||||
|
||||
// The source image and color are composited using :
|
||||
// result = k1 *in*in2 + k2*in + k3*in2 + k4.
|
||||
String _compositeImageToSvg(String imageUrl, double k1, double k2, double k3,
|
||||
double k4, double width, double height) {
|
||||
return _svgFilterWrapper(
|
||||
'<feImage href="$imageUrl" result="image" ${_buildFeBlend(width, height)}>'
|
||||
'</feImage>'
|
||||
'<feComposite in="image" in2="SourceGraphic" '
|
||||
'operator="arithmetic" k1="$k1" k2="$k2" k3="$k3" k4="$k4" result="comp">'
|
||||
'</feComposite>',
|
||||
);
|
||||
}
|
||||
|
||||
// Porter duff source * destination , keep source alpha.
|
||||
// First apply color filter to source to change it to [color], then
|
||||
// composite using multiplication.
|
||||
String _modulateImageToSvg(String imageUrl, double width, double height) {
|
||||
return _svgFilterWrapper(
|
||||
'<feImage href="$imageUrl" result="image" ${_buildFeBlend(width, height)}>'
|
||||
'</feImage>'
|
||||
'<feComposite in="image" in2="SourceGraphic" '
|
||||
'operator="arithmetic" k1="1" k2="0" k3="0" k4="0" result="comp">'
|
||||
'</feComposite>',
|
||||
);
|
||||
}
|
||||
|
||||
// Uses feBlend element to blend source image with a color.
|
||||
String _blendImageToSvg(
|
||||
String imageUrl, String? feBlend, double width, double height,
|
||||
{bool swapLayers = false}) {
|
||||
return _svgFilterWrapper(
|
||||
'<feImage href="$imageUrl" result="image" ${_buildFeBlend(width, height)}>'
|
||||
'</feImage>' +
|
||||
(swapLayers
|
||||
? '<feBlend in="SourceGraphic" in2="image" mode="$feBlend"/>'
|
||||
: '<feBlend in="image" in2="SourceGraphic" mode="$feBlend"/>'),
|
||||
);
|
||||
}
|
||||
@@ -12,6 +12,10 @@ abstract class EngineGradient implements ui.Gradient {
|
||||
/// Creates a fill style to be used in painting.
|
||||
Object createPaintStyle(html.CanvasRenderingContext2D? ctx,
|
||||
ui.Rect? shaderBounds, double density);
|
||||
|
||||
/// Creates a CanvasImageSource to paint gradient.
|
||||
Object createImageBitmap(ui.Rect? shaderBounds, double density,
|
||||
bool createDataUrl);
|
||||
}
|
||||
|
||||
class GradientSweep extends EngineGradient {
|
||||
@@ -28,8 +32,8 @@ class GradientSweep extends EngineGradient {
|
||||
}
|
||||
|
||||
@override
|
||||
Object createPaintStyle(html.CanvasRenderingContext2D? ctx,
|
||||
ui.Rect? shaderBounds, double density) {
|
||||
Object createImageBitmap(ui.Rect? shaderBounds, double density,
|
||||
bool createDataUrl) {
|
||||
assert(shaderBounds != null);
|
||||
int widthInPixels = shaderBounds!.width.ceil();
|
||||
int heightInPixels = shaderBounds.height.ceil();
|
||||
@@ -66,11 +70,22 @@ class GradientSweep extends EngineGradient {
|
||||
glProgram.program, 'm_gradient');
|
||||
gl.setUniformMatrix4fv(gradientMatrix, false, matrix4!);
|
||||
}
|
||||
if (createDataUrl) {
|
||||
return _glRenderer!.drawRectToImageUrl(
|
||||
ui.Rect.fromLTWH(0, 0, shaderBounds.width, shaderBounds.height),
|
||||
gl, glProgram, normalizedGradient, widthInPixels, heightInPixels);
|
||||
} else {
|
||||
return _glRenderer!.drawRect(
|
||||
ui.Rect.fromLTWH(0, 0, shaderBounds.width, shaderBounds.height),
|
||||
gl, glProgram, normalizedGradient, widthInPixels, heightInPixels)!;
|
||||
}
|
||||
}
|
||||
|
||||
Object? imageBitmap = _glRenderer!.drawRect(ui.Rect.fromLTWH(0, 0, shaderBounds.width, shaderBounds.height),
|
||||
gl, glProgram, normalizedGradient, widthInPixels, heightInPixels);
|
||||
|
||||
return ctx!.createPattern(imageBitmap!, 'no-repeat')!;
|
||||
@override
|
||||
Object createPaintStyle(html.CanvasRenderingContext2D? ctx,
|
||||
ui.Rect? shaderBounds, double density) {
|
||||
Object? imageBitmap = createImageBitmap(shaderBounds, density, false);
|
||||
return ctx!.createPattern(imageBitmap, 'no-repeat')!;
|
||||
}
|
||||
|
||||
String _createSweepFragmentShader(NormalizedGradient gradient,
|
||||
@@ -146,7 +161,6 @@ class GradientLinear extends EngineGradient {
|
||||
if (tileMode == ui.TileMode.clamp || tileMode == ui.TileMode.decal) {
|
||||
return _createCanvasGradient(ctx, shaderBounds, density);
|
||||
} else {
|
||||
initWebGl();
|
||||
return _createGlGradient(ctx, shaderBounds, density);
|
||||
}
|
||||
}
|
||||
@@ -177,14 +191,14 @@ class GradientLinear extends EngineGradient {
|
||||
return gradient;
|
||||
}
|
||||
|
||||
/// Creates a linear gradient with tiling repeat or mirror.
|
||||
html.CanvasPattern _createGlGradient(html.CanvasRenderingContext2D? ctx,
|
||||
ui.Rect? shaderBounds, double density) {
|
||||
@override
|
||||
Object createImageBitmap(ui.Rect? shaderBounds, double density,
|
||||
bool createDataUrl) {
|
||||
assert(shaderBounds != null);
|
||||
int widthInPixels = shaderBounds!.width.ceil();
|
||||
int heightInPixels = shaderBounds.height.ceil();
|
||||
assert(widthInPixels > 0 && heightInPixels > 0);
|
||||
|
||||
initWebGl();
|
||||
// Render gradient into a bitmap and create a canvas pattern.
|
||||
_OffScreenCanvas offScreenCanvas =
|
||||
_OffScreenCanvas(widthInPixels, heightInPixels);
|
||||
@@ -279,13 +293,26 @@ class GradientLinear extends EngineGradient {
|
||||
gl.setUniform2f(
|
||||
uRes, widthInPixels.toDouble(), heightInPixels.toDouble());
|
||||
|
||||
// Draw gradient and convert to pattern.
|
||||
Object? imageBitmap = _glRenderer!.drawRect(ui.Rect.fromLTWH(
|
||||
0, 0, shaderBounds.width, shaderBounds.height) /* !! shaderBounds */,
|
||||
gl,
|
||||
glProgram, normalizedGradient, widthInPixels, heightInPixels,
|
||||
);
|
||||
return ctx!.createPattern(imageBitmap!, 'no-repeat')!;
|
||||
if (createDataUrl) {
|
||||
return _glRenderer!.drawRectToImageUrl(ui.Rect.fromLTWH(
|
||||
0, 0, shaderBounds.width, shaderBounds.height) /* !! shaderBounds */,
|
||||
gl,
|
||||
glProgram, normalizedGradient, widthInPixels, heightInPixels,
|
||||
);
|
||||
} else {
|
||||
return _glRenderer!.drawRect(ui.Rect.fromLTWH(
|
||||
0, 0, shaderBounds.width, shaderBounds.height) /* !! shaderBounds */,
|
||||
gl,
|
||||
glProgram, normalizedGradient, widthInPixels, heightInPixels,
|
||||
)!;
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a linear gradient with tiling repeat or mirror.
|
||||
html.CanvasPattern _createGlGradient(html.CanvasRenderingContext2D? ctx,
|
||||
ui.Rect? shaderBounds, double density) {
|
||||
final Object imageBitmap = createImageBitmap(shaderBounds, density, false);
|
||||
return ctx!.createPattern(imageBitmap, 'no-repeat')!;
|
||||
}
|
||||
|
||||
String _createLinearFragmentShader(NormalizedGradient gradient,
|
||||
@@ -388,7 +415,8 @@ String _writeSharedGradientShader(ShaderBuilder builder,
|
||||
class GradientRadial extends EngineGradient {
|
||||
GradientRadial(this.center, this.radius, this.colors, this.colorStops,
|
||||
this.tileMode, this.matrix4)
|
||||
: super._();
|
||||
: super._() {
|
||||
}
|
||||
|
||||
final ui.Offset center;
|
||||
final double radius;
|
||||
@@ -403,7 +431,6 @@ class GradientRadial extends EngineGradient {
|
||||
if (tileMode == ui.TileMode.clamp || tileMode == ui.TileMode.decal) {
|
||||
return _createCanvasGradient(ctx, shaderBounds, density);
|
||||
} else {
|
||||
initWebGl();
|
||||
return _createGlGradient(ctx, shaderBounds, density);
|
||||
}
|
||||
}
|
||||
@@ -419,9 +446,9 @@ class GradientRadial extends EngineGradient {
|
||||
return gradient;
|
||||
}
|
||||
|
||||
/// Creates a radial gradient with tiling repeat or mirror.
|
||||
html.CanvasPattern _createGlGradient(html.CanvasRenderingContext2D? ctx,
|
||||
ui.Rect? shaderBounds, double density) {
|
||||
@override
|
||||
Object createImageBitmap(ui.Rect? shaderBounds, double density,
|
||||
bool createDataUrl) {
|
||||
assert(shaderBounds != null);
|
||||
int widthInPixels = shaderBounds!.width.ceil();
|
||||
int heightInPixels = shaderBounds.height.ceil();
|
||||
@@ -434,7 +461,7 @@ class GradientRadial extends EngineGradient {
|
||||
_GlContext gl = _OffScreenCanvas.supported
|
||||
? _GlContext.fromOffscreenCanvas(offScreenCanvas._canvas!)
|
||||
: _GlContext.fromCanvas(offScreenCanvas._glCanvas!,
|
||||
webGLVersion == WebGLVersion.webgl1);
|
||||
webGLVersion == WebGLVersion.webgl1);
|
||||
gl.setViewportSize(widthInPixels, heightInPixels);
|
||||
|
||||
NormalizedGradient normalizedGradient = NormalizedGradient(
|
||||
@@ -457,10 +484,22 @@ class GradientRadial extends EngineGradient {
|
||||
glProgram.program, 'm_gradient');
|
||||
gl.setUniformMatrix4fv(gradientMatrix, false, matrix4 == null ? Matrix4.identity().storage : matrix4!);
|
||||
|
||||
Object? imageBitmap = _glRenderer!.drawRect(ui.Rect.fromLTWH(0, 0, shaderBounds.width, shaderBounds.height),
|
||||
gl, glProgram, normalizedGradient, widthInPixels, heightInPixels);
|
||||
if (createDataUrl) {
|
||||
return _glRenderer!.drawRectToImageUrl(ui.Rect.fromLTWH(0, 0, shaderBounds.width,
|
||||
shaderBounds.height),
|
||||
gl, glProgram, normalizedGradient, widthInPixels, heightInPixels);
|
||||
} else {
|
||||
return _glRenderer!.drawRect(ui.Rect.fromLTWH(0, 0, shaderBounds.width,
|
||||
shaderBounds.height),
|
||||
gl, glProgram, normalizedGradient, widthInPixels, heightInPixels)!;
|
||||
}
|
||||
}
|
||||
|
||||
return ctx!.createPattern(imageBitmap!, 'no-repeat')!;
|
||||
/// Creates a radial gradient with tiling repeat or mirror.
|
||||
html.CanvasPattern _createGlGradient(html.CanvasRenderingContext2D? ctx,
|
||||
ui.Rect? shaderBounds, double density) {
|
||||
final Object imageBitmap = createImageBitmap(shaderBounds, density, false);
|
||||
return ctx!.createPattern(imageBitmap, 'no-repeat')!;
|
||||
}
|
||||
|
||||
String _createRadialFragmentShader(NormalizedGradient gradient,
|
||||
@@ -510,6 +549,12 @@ class GradientConical extends EngineGradient {
|
||||
ui.Rect? shaderBounds, double density) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
Object createImageBitmap(ui.Rect? shaderBounds, double density,
|
||||
bool createDataUrl) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
}
|
||||
|
||||
/// Backend implementation of [ui.ImageFilter].
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
// 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.
|
||||
|
||||
import 'dart:math' as math;
|
||||
import 'package:test/bootstrap/browser.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'package:ui/src/engine.dart';
|
||||
import 'package:ui/ui.dart' as ui;
|
||||
|
||||
import 'package:web_engine_tester/golden_tester.dart';
|
||||
|
||||
import 'common.dart';
|
||||
|
||||
void main() {
|
||||
internalBootstrapBrowserTest(() => testMain);
|
||||
}
|
||||
|
||||
const ui.Rect region = const ui.Rect.fromLTRB(0, 0, 500, 250);
|
||||
|
||||
Future<void> matchSceneGolden(String goldenFile, LayerScene scene,
|
||||
{bool write = false}) async {
|
||||
final EnginePlatformDispatcher dispatcher =
|
||||
ui.window.platformDispatcher as EnginePlatformDispatcher;
|
||||
dispatcher.rasterizer!.draw(scene.layerTree);
|
||||
await matchGoldenFile(goldenFile, region: region, write: write);
|
||||
}
|
||||
|
||||
void testMain() {
|
||||
group('ShaderMask', () {
|
||||
setUpCanvasKitTest();
|
||||
|
||||
test('Renders sweep gradient with color blend', () async {
|
||||
final LayerSceneBuilder builder = LayerSceneBuilder();
|
||||
|
||||
builder.pushOffset(0, 0);
|
||||
|
||||
// Draw a red circle and apply it to the scene.
|
||||
final CkPictureRecorder recorder = CkPictureRecorder();
|
||||
final CkCanvas canvas = recorder.beginRecording(region);
|
||||
|
||||
canvas.drawCircle(
|
||||
ui.Offset(425, 125),
|
||||
50,
|
||||
CkPaint()..color = ui.Color.fromARGB(255, 255, 0, 0),
|
||||
);
|
||||
final CkPicture redCircle = recorder.endRecording();
|
||||
|
||||
builder.addPicture(ui.Offset.zero, redCircle);
|
||||
|
||||
final CkGradientSweep shader = CkGradientSweep(
|
||||
ui.Offset(250, 125),
|
||||
<ui.Color>[
|
||||
ui.Color(0xFF4285F4),
|
||||
ui.Color(0xFF34A853),
|
||||
ui.Color(0xFFFBBC05),
|
||||
ui.Color(0xFFEA4335),
|
||||
ui.Color(0xFF4285F4),
|
||||
],
|
||||
<double>[
|
||||
0.0,
|
||||
0.25,
|
||||
0.5,
|
||||
0.75,
|
||||
1.0,
|
||||
],
|
||||
ui.TileMode.clamp,
|
||||
-(math.pi / 2),
|
||||
math.pi * 2 - (math.pi / 2),
|
||||
null);
|
||||
|
||||
final ui.Path clipPath = ui.Path()
|
||||
..addOval(ui.Rect.fromLTWH(25, 75, 100, 100));
|
||||
builder.pushClipPath(clipPath);
|
||||
|
||||
// Apply a shader mask.
|
||||
builder.pushShaderMask(shader, ui.Rect.fromLTRB(0, 0, 200, 250),
|
||||
ui.BlendMode.color);
|
||||
|
||||
// Draw another red circle and apply it to the scene.
|
||||
// This one should be grey since we have the color filter.
|
||||
final CkPictureRecorder recorder2 = CkPictureRecorder();
|
||||
final CkCanvas canvas2 = recorder2.beginRecording(region);
|
||||
|
||||
canvas2.drawRect(ui.Rect.fromLTWH(25, 75, 100, 100),
|
||||
CkPaint()..color = ui.Color.fromARGB(255, 0, 255, 0),
|
||||
);
|
||||
|
||||
final CkPicture sweepCircle = recorder2.endRecording();
|
||||
|
||||
builder.addPicture(ui.Offset.zero, sweepCircle);
|
||||
|
||||
await matchSceneGolden('canvaskit_shadermask_linear.png',
|
||||
builder.build());
|
||||
});
|
||||
// TODO: https://github.com/flutter/flutter/issues/60040
|
||||
// TODO: https://github.com/flutter/flutter/issues/71520
|
||||
}, skip: isIosSafari || isFirefox);
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
// 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.
|
||||
|
||||
// @dart = 2.6
|
||||
import 'dart:html' as html;
|
||||
|
||||
import 'package:test/bootstrap/browser.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'package:ui/ui.dart';
|
||||
import 'package:ui/src/engine.dart'
|
||||
hide ClipRectEngineLayer, BackdropFilterEngineLayer;
|
||||
|
||||
import 'package:web_engine_tester/golden_tester.dart';
|
||||
|
||||
/// To debug compositing failures on browsers, set this flag to true and run
|
||||
/// flutter run -d chrome --web-renderer=html
|
||||
/// test/golden_tests/engine/shader_mask_golden_test.dart --profile
|
||||
const bool debugTest = false;
|
||||
|
||||
void main() async {
|
||||
if (!debugTest) {
|
||||
internalBootstrapBrowserTest(() => testMain);
|
||||
} else {
|
||||
_renderScene(BlendMode.color);
|
||||
}
|
||||
}
|
||||
|
||||
void testMain() async {
|
||||
setUp(() async {
|
||||
debugShowClipLayers = true;
|
||||
SurfaceSceneBuilder.debugForgetFrameScene();
|
||||
for (html.Node scene in html.document.querySelectorAll('flt-scene')) {
|
||||
scene.remove();
|
||||
}
|
||||
initWebGl();
|
||||
await webOnlyInitializePlatform();
|
||||
webOnlyFontCollection.debugRegisterTestFonts();
|
||||
await webOnlyFontCollection.ensureFontsLoaded();
|
||||
});
|
||||
|
||||
/// Should render the picture unmodified.
|
||||
test('Renders shader mask with linear gradient BlendMode dst', () async {
|
||||
_renderScene(BlendMode.dst);
|
||||
await matchGoldenFile('shadermask_linear_dst.png',
|
||||
region: Rect.fromLTWH(0, 0, 360, 200));
|
||||
});
|
||||
|
||||
/// Should render the gradient only where circles have alpha channel.
|
||||
test('Renders shader mask with linear gradient BlendMode srcIn', () async {
|
||||
_renderScene(BlendMode.srcIn);
|
||||
await matchGoldenFile('shadermask_linear_srcin.png',
|
||||
region: Rect.fromLTWH(0, 0, 360, 200));
|
||||
});
|
||||
|
||||
test('Renders shader mask with linear gradient BlendMode color', () async {
|
||||
_renderScene(BlendMode.color);
|
||||
await matchGoldenFile('shadermask_linear_color.png',
|
||||
region: Rect.fromLTWH(0, 0, 360, 200));
|
||||
});
|
||||
|
||||
test('Renders shader mask with linear gradient BlendMode xor', () async {
|
||||
_renderScene(BlendMode.xor);
|
||||
await matchGoldenFile('shadermask_linear_xor.png',
|
||||
region: Rect.fromLTWH(0, 0, 360, 200));
|
||||
});
|
||||
|
||||
test('Renders shader mask with linear gradient BlendMode plus', () async {
|
||||
_renderScene(BlendMode.plus);
|
||||
await matchGoldenFile('shadermask_linear_plus.png',
|
||||
region: Rect.fromLTWH(0, 0, 360, 200));
|
||||
});
|
||||
|
||||
test('Renders shader mask with linear gradient BlendMode modulate', () async {
|
||||
_renderScene(BlendMode.modulate);
|
||||
await matchGoldenFile('shadermask_linear_modulate.png',
|
||||
region: Rect.fromLTWH(0, 0, 360, 200));
|
||||
});
|
||||
|
||||
test('Renders shader mask with linear gradient BlendMode overlay', () async {
|
||||
_renderScene(BlendMode.overlay);
|
||||
await matchGoldenFile('shadermask_linear_overlay.png',
|
||||
region: Rect.fromLTWH(0, 0, 360, 200));
|
||||
});
|
||||
|
||||
/// Should render the gradient opaque on top of content.
|
||||
test('Renders shader mask with linear gradient BlendMode src', () async {
|
||||
_renderScene(BlendMode.src);
|
||||
await matchGoldenFile('shadermask_linear_src.png',
|
||||
region: Rect.fromLTWH(0, 0, 360, 200));
|
||||
});
|
||||
}
|
||||
|
||||
Picture _drawTestPictureWithCircles(
|
||||
Rect region, double offsetX, double offsetY) {
|
||||
final EnginePictureRecorder recorder = PictureRecorder();
|
||||
final RecordingCanvas canvas = recorder.beginRecording(region);
|
||||
canvas.drawCircle(Offset(offsetX + 30, offsetY + 30), 30,
|
||||
Paint()..style = PaintingStyle.fill);
|
||||
canvas.drawCircle(
|
||||
Offset(offsetX + 110, offsetY + 30),
|
||||
30,
|
||||
Paint()
|
||||
..style = PaintingStyle.fill
|
||||
..color = const Color(0xFFFF0000));
|
||||
canvas.drawCircle(
|
||||
Offset(offsetX + 30, offsetY + 110),
|
||||
30,
|
||||
Paint()
|
||||
..style = PaintingStyle.fill
|
||||
..color = const Color(0xFF00FF00));
|
||||
canvas.drawCircle(
|
||||
Offset(offsetX + 110, offsetY + 110),
|
||||
30,
|
||||
Paint()
|
||||
..style = PaintingStyle.fill
|
||||
..color = const Color(0xFF0000FF));
|
||||
return recorder.endRecording();
|
||||
}
|
||||
|
||||
void _renderScene(BlendMode blendMode) {
|
||||
final Rect region = Rect.fromLTWH(0, 0, 400, 400);
|
||||
|
||||
final SurfaceSceneBuilder builder = SurfaceSceneBuilder();
|
||||
final Picture circles1 = _drawTestPictureWithCircles(region, 10, 10);
|
||||
builder.addPicture(Offset.zero, circles1);
|
||||
|
||||
List<Color> colors = <Color>[
|
||||
Color(0xFF000000),
|
||||
Color(0xFFFF3C38),
|
||||
Color(0xFFFF8C42),
|
||||
Color(0xFFFFF275),
|
||||
Color(0xFF6699CC),
|
||||
Color(0xFF656D78),
|
||||
];
|
||||
List<double> stops = <double>[0.0, 0.05, 0.4, 0.6, 0.9, 1.0];
|
||||
|
||||
EngineGradient shader = GradientLinear(Offset(200, 30), Offset(320, 150),
|
||||
colors, stops, TileMode.clamp, Matrix4.identity().storage);
|
||||
|
||||
builder.pushShaderMask(shader, Rect.fromLTWH(180, 10, 140, 140), blendMode,
|
||||
oldLayer: null);
|
||||
final Picture circles2 = _drawTestPictureWithCircles(region, 180, 10);
|
||||
builder.addPicture(Offset.zero, circles2);
|
||||
builder.pop();
|
||||
|
||||
html.document.body.append(builder.build().webOnlyRootElement);
|
||||
}
|
||||
Reference in New Issue
Block a user