New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[web] Implement tilemode for gradient shaders. #22597
Conversation
assert(colors != null), | ||
// ignore: unnecessary_null_comparison | ||
assert(tileMode != null), | ||
// ignore: unnecessary_null_comparison |
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.
Do these // ignore
still apply correctly after formatting?
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.
|
||
// Render gradient into a bitmap and create a canvas pattern. | ||
_OffScreenCanvas offScreenCanvas = | ||
_OffScreenCanvas(widthInPixels, heightInPixels); |
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.
nit: this line needs an extra indent
_GlContext gl = _OffScreenCanvas.supported | ||
? _GlContext.fromOffscreenCanvas(offScreenCanvas._canvas!) | ||
: _GlContext.fromCanvas(offScreenCanvas._glCanvas!, | ||
webGLVersion == WebGLVersion.webgl1); |
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.
nit: this line needs an extra indent
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.
// For now only GradientRotation is supported in flutter which is implemented | ||
// for linear gradient. | ||
// See https://github.com/flutter/flutter/issues/32819 | ||
/// Returns name of gradient treshold variable to use to compute color. |
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.
nit: the doc comment makes it sound like this function only returns the name and nothing else.
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.
_GlContext gl = _OffScreenCanvas.supported | ||
? _GlContext.fromOffscreenCanvas(offScreenCanvas._canvas!) | ||
: _GlContext.fromCanvas(offScreenCanvas._glCanvas!, | ||
webGLVersion == WebGLVersion.webgl1); |
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.
nit: this line needs an extra indent
return gradient; | ||
_GlProgram glProgram = gl.useAndCacheProgram( | ||
_WebGlRenderer.writeBaseVertexShader(), | ||
_createLinearFragmentShader(normalizedGradient, tileMode))!; |
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.
I think we can make useAndCacheProgram
return a non-null value instead of null-asserting at call sites.
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.
|
||
_GlProgram glProgram = gl.useAndCacheProgram( | ||
_WebGlRenderer.writeBaseVertexShader(), | ||
_createRadialFragmentShader(normalizedGradient, tileMode))!; |
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.
ditto (non-null useAndCacheProgram
)
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.
lib/web_ui/lib/src/ui/painting.dart
Outdated
@@ -280,7 +280,8 @@ abstract class Gradient extends Shader { | |||
Float64List? matrix4, | |||
]) => engine.useCanvasKit | |||
? engine.CkGradientLinear(from, to, colors, colorStops, tileMode, matrix4) | |||
: engine.GradientLinear(from, to, colors, colorStops, tileMode, matrix4); | |||
: engine.GradientLinear(from, to, colors, colorStops, tileMode, | |||
matrix4 == null ? null : engine.toMatrix32(matrix4)); |
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.
nit: this line needs an extra indent
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.
final String probeName = | ||
_writeSharedGradientShader(builder, method, gradient, tileMode); | ||
method.addStatement('${fragColor.name} = ${probeName} * scale + bias;'); | ||
String shader = builder.build(); |
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.
nit: shader
variable unnecessary, can return builder.build();
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.
You may want to add more shader code in the future after you calculate fragColor.
normalizedGradient.setupUniforms(gl, glProgram); | ||
Object gradientMatrix = gl.getUniformLocation( | ||
glProgram.program, 'm_gradient'); | ||
gl.setUniformMatrix4fv(gradientMatrix, false, matrix4 == null ? Matrix4.identity().storage : matrix4!); |
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.
Is identity or null matrix a common case? If so, perhaps we should reuse a static identity matrix instead of instantiating a new one every time. JS-engines are still pretty bad at allocating large typed arrays.
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.
Noticed it too. I'm working on a separate CL to go through a lot of other identity() calls we have and optimize identity itself. Matrix4.zero().setIdentity() is not efficient either since Float32List does get already initialized with 0s.
* 53fc019 Split AOT Android Embedder and shell (flutter/engine#22179) * fc55814 Implement Scene.toImage() in CanvasKit mode. (flutter/engine#22085) * c45e02a Roll Dart SDK from 12fded61a2bc to a06d469024fd (1 revision) (flutter/engine#22623) * 550c750 Remove opt outs for dart:ui (flutter/engine#22603) * f2803ac [fuchsia] shader warmup fixes (flutter/engine#22439) * ce94c4e Roll Dart SDK from a06d469024fd to b8fea79a2549 (1 revision) (flutter/engine#22630) * 76b6acb Roll Fuchsia Linux SDK from aAb3NJv_h... to X1ue-JZsc... (flutter/engine#22631) * 976e887 Roll Skia from ed289e777cfa to 9dce4d081f8a (3 revisions) (flutter/engine#22632) * 885bd65 Roll Fuchsia Mac SDK from DQpWjEN59... to wGZWtwuY4... (flutter/engine#22633) * 8971b82 Roll Dart SDK from b8fea79a2549 to 861ebcb175b6 (1 revision) (flutter/engine#22634) * a09cdfd Roll Skia from 9dce4d081f8a to 8c5889937172 (1 revision) (flutter/engine#22635) * a9f332c Roll Dart SDK from 861ebcb175b6 to 1adf3d5fa9d0 (1 revision) (flutter/engine#22636) * 1bf5c8b [web] Implement tilemode for gradient shaders. (flutter/engine#22597) * 97cacfb Add more runtime intrinsic symbols to the export checker script (flutter/engine#22641)
Description
Implements tilemode for gradient shaders.
Related Issues
flutter/flutter#46833
Tests
I added the following tests:
linear_gradient_golden_test.dart
radial_gradient_golden_test.dart
Checklist
Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes (
[x]
). This will ensure a smooth and quick review process.Reviewer Checklist
Breaking Change
Did any tests fail when you ran them? Please read [handling breaking changes].