Fix issues with nested gradients in html renderer. (flutter/engine#31887)
* Fix issues with nested gradients in html renderer. https://github.com/flutter/flutter/issues/99045 The DOM canvas does need to be able to handle gradient shaders, since if we are inside of a context that is affected by an SVG filter we can't fall back to the using canvas. This is because webkit doesn't apply the SVG filters to canvas elements for some reason. In these situations, just do a full GL render of the gradient and apply it as the background image on the DOM element. * Add test that ensures we can render gradients from within an svg context. * Fix analyzer issue. * Change function name.
This commit is contained in:
@@ -11,7 +11,6 @@ import 'package:ui/ui.dart' as ui;
|
||||
|
||||
import '../browser_detection.dart';
|
||||
import '../engine_canvas.dart';
|
||||
import '../html_image_codec.dart';
|
||||
import '../text/canvas_paragraph.dart';
|
||||
import '../util.dart';
|
||||
import '../vector_math.dart';
|
||||
@@ -19,6 +18,7 @@ import 'painting.dart';
|
||||
import 'path/path.dart';
|
||||
import 'path/path_to_svg.dart';
|
||||
import 'shaders/image_shader.dart';
|
||||
import 'shaders/shader.dart';
|
||||
|
||||
/// A canvas that renders to DOM elements and CSS properties.
|
||||
class DomCanvas extends EngineCanvas with SaveElementStackTracking {
|
||||
@@ -162,7 +162,6 @@ ui.Color blurColor(ui.Color color, double sigma) {
|
||||
|
||||
html.HtmlElement buildDrawRectElement(
|
||||
ui.Rect rect, SurfacePaintData paint, String tagName, Matrix4 transform) {
|
||||
assert(paint.shader == null);
|
||||
final html.HtmlElement rectangle =
|
||||
html.document.createElement(tagName) as html.HtmlElement;
|
||||
assert(() {
|
||||
@@ -226,19 +225,28 @@ html.HtmlElement buildDrawRectElement(
|
||||
style
|
||||
..width = '${right - left}px'
|
||||
..height = '${bottom - top}px'
|
||||
..backgroundColor = cssColor;
|
||||
|
||||
if (paint.shader != null && paint.shader is EngineImageShader) {
|
||||
_applyImageShaderToElement(rectangle, paint.shader! as EngineImageShader);
|
||||
}
|
||||
..backgroundColor = cssColor
|
||||
..backgroundImage = _getBackgroundImageCssValue(paint.shader, rect);
|
||||
}
|
||||
return rectangle;
|
||||
}
|
||||
|
||||
void _applyImageShaderToElement(html.HtmlElement targetElement,
|
||||
EngineImageShader imageShader) {
|
||||
final HtmlImage image = imageShader.image;
|
||||
targetElement.style.backgroundImage = image.imgElement.src;
|
||||
String _getBackgroundImageCssValue(ui.Shader? shader, ui.Rect bounds) {
|
||||
final String url = _getBackgroundImageUrl(shader, bounds);
|
||||
return (url != '') ? "url('$url'": '';
|
||||
}
|
||||
|
||||
String _getBackgroundImageUrl(ui.Shader? shader, ui.Rect bounds) {
|
||||
if(shader != null) {
|
||||
if(shader is EngineImageShader) {
|
||||
return shader.image.imgElement.src ?? '';
|
||||
}
|
||||
|
||||
if(shader is EngineGradient) {
|
||||
return shader.createImageBitmap(bounds, 1, true) as String;
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
void applyRRectBorderRadius(html.CssStyleDeclaration style, ui.RRect rrect) {
|
||||
|
||||
@@ -108,8 +108,13 @@ class PersistedShaderMask extends PersistedContainerSurface
|
||||
void _applyGradientShader() {
|
||||
if (shader is EngineGradient) {
|
||||
final EngineGradient gradientShader = shader as EngineGradient;
|
||||
|
||||
// The gradient shader's bounds are in the context of the element itself,
|
||||
// rather than the global position, so translate it back to the origin.
|
||||
final ui.Rect translatedRect =
|
||||
maskRect.translate(-maskRect.left, -maskRect.top);
|
||||
final String imageUrl =
|
||||
gradientShader.createImageBitmap(maskRect, 1, true) as String;
|
||||
gradientShader.createImageBitmap(translatedRect, 1, true) as String;
|
||||
ui.BlendMode blendModeTemp = blendMode;
|
||||
switch (blendModeTemp) {
|
||||
case ui.BlendMode.clear:
|
||||
|
||||
@@ -234,14 +234,6 @@ class GradientLinear extends EngineGradient {
|
||||
_createLinearFragmentShader(normalizedGradient, tileMode));
|
||||
gl.useProgram(glProgram);
|
||||
|
||||
/// When creating an image to apply to a dom element, render
|
||||
/// contents at 0,0 and adjust gradient vector for shaderBounds.
|
||||
final bool translateToOrigin = createDataUrl;
|
||||
|
||||
if (translateToOrigin) {
|
||||
shaderBounds = shaderBounds.translate(-shaderBounds.left, -shaderBounds.top);
|
||||
}
|
||||
|
||||
// Setup from/to uniforms.
|
||||
//
|
||||
// From/to is relative to shaderBounds.
|
||||
|
||||
@@ -430,6 +430,40 @@ Future<void> testMain() async {
|
||||
canvas.restore();
|
||||
await _checkScreenshot(canvas, 'linear_gradient_rect_clamp_rotated');
|
||||
});
|
||||
|
||||
test('Paints linear gradient properly when within svg context', () async {
|
||||
final RecordingCanvas canvas =
|
||||
RecordingCanvas(const Rect.fromLTRB(0, 0, 500, 240));
|
||||
canvas.save();
|
||||
|
||||
canvas.renderStrategy.isInsideSvgFilterTree = true;
|
||||
|
||||
final SurfacePaint borderPaint = SurfacePaint()
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 1
|
||||
..color = const Color(0xFF000000);
|
||||
|
||||
const List<Color> colors = <Color>[
|
||||
Color(0xFFFF0000),
|
||||
Color(0xFF0000FF),
|
||||
];
|
||||
|
||||
final GradientLinear linearGradient = GradientLinear(const Offset(125, 75),
|
||||
const Offset(175, 125),
|
||||
colors, null, TileMode.clamp,
|
||||
Matrix4.identity().storage);
|
||||
|
||||
const double kBoxWidth = 150;
|
||||
const double kBoxHeight = 100;
|
||||
// Gradient with default center.
|
||||
const Rect rectBounds = Rect.fromLTWH(100, 50, kBoxWidth, kBoxHeight);
|
||||
canvas.drawRect(rectBounds,
|
||||
SurfacePaint()..shader = engineLinearGradientToShader(linearGradient, rectBounds));
|
||||
canvas.drawRect(rectBounds, borderPaint);
|
||||
|
||||
canvas.restore();
|
||||
await _checkScreenshot(canvas, 'linear_gradient_in_svg_context');
|
||||
});
|
||||
}
|
||||
|
||||
Shader engineGradientToShader(GradientSweep gradient, Rect rect) {
|
||||
|
||||
Reference in New Issue
Block a user