Skip to content

Commit bbdbf0f

Browse files
Use named uniforms instead of setFloat in StretchEffect (flutter#195372)
IMoves `StretchEffect` from the index-based `FragmentShader.setFloat` to `getUniformFloat`, as part of deprecating `setFloat` (flutter#195357). `StretchEffect` creates a new shader on every build, so it looks up its four uniforms by name right after creating it, using a local variable instead of repeating `_fragmentShader!`. `u_size` and `u_texture` are still set by the engine for `ImageFilter.shader`. Every uniform gets the same value as before, so there is no visual change. The `InkSparkle` part of flutter#195358 is in flutter/packages#13445, since Material is frozen in this repo (flutter#188444). Part of flutter#195358 This is a refactor with no semantic change, so I think it qualifies for `test-exempt`. It's covered by the existing golden test in `stretch_effect_test.dart`, which runs in the Impeller shard, and a misspelled uniform name would throw when that test runs. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/CONTRIBUTING.ai.md [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: gaaclarke <[email protected]>
1 parent 797e08d commit bbdbf0f

1 file changed

Lines changed: 44 additions & 14 deletions

File tree

‎packages/flutter/lib/src/widgets/stretch_effect.dart‎

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class _StretchOverscrollEffect extends StatefulWidget {
152152
}
153153

154154
class _StretchOverscrollEffectState extends State<_StretchOverscrollEffect> {
155-
ui.FragmentShader? _fragmentShader;
155+
_StretchEffectUniforms? _uniforms;
156156

157157
/// The maximum scale multiplier applied during a stretch effect.
158158
static const double maxStretchIntensity = 1.0;
@@ -165,7 +165,7 @@ class _StretchOverscrollEffectState extends State<_StretchOverscrollEffect> {
165165

166166
@override
167167
void dispose() {
168-
_fragmentShader?.dispose();
168+
_uniforms?.dispose();
169169
super.dispose();
170170
}
171171

@@ -182,23 +182,23 @@ class _StretchOverscrollEffectState extends State<_StretchOverscrollEffect> {
182182
final ui.ImageFilter imageFilter;
183183

184184
if (_StretchEffectShader._initialized) {
185-
_fragmentShader?.dispose();
186-
_fragmentShader = _StretchEffectShader._program!.fragmentShader();
187-
_fragmentShader!.setFloat(2, maxStretchIntensity);
185+
final _StretchEffectUniforms uniforms = _uniforms ??= _StretchEffectUniforms(
186+
_StretchEffectShader._program!,
187+
);
188+
uniforms.maxStretchIntensity.set(maxStretchIntensity);
188189
if (widget.axis == Axis.vertical) {
189-
_fragmentShader!.setFloat(3, 0.0);
190-
_fragmentShader!.setFloat(4, widget.stretchStrength);
190+
uniforms.overscrollX.set(0.0);
191+
uniforms.overscrollY.set(widget.stretchStrength);
191192
} else {
192-
_fragmentShader!.setFloat(3, widget.stretchStrength);
193-
_fragmentShader!.setFloat(4, 0.0);
193+
uniforms.overscrollX.set(widget.stretchStrength);
194+
uniforms.overscrollY.set(0.0);
194195
}
195-
_fragmentShader!.setFloat(5, interpolationStrength);
196+
uniforms.interpolationStrength.set(interpolationStrength);
196197

197-
imageFilter = ui.ImageFilter.shader(_fragmentShader!);
198+
// The shader is reused across builds. ImageFilter.shader copies the
199+
// uniform values, so each build's filter reflects that build's values.
200+
imageFilter = ui.ImageFilter.shader(uniforms.shader);
198201
} else {
199-
_fragmentShader?.dispose();
200-
_fragmentShader = null;
201-
202202
imageFilter = _emptyFilter;
203203
}
204204

@@ -238,6 +238,36 @@ class _StretchEffectPainter extends CustomPainter {
238238
bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
239239
}
240240

241+
/// A [ui.FragmentShader] for `shaders/stretch_effect.frag`, together with the
242+
/// uniforms that [StretchEffect] sets.
243+
///
244+
/// The uniforms are looked up by name once, so that each build only has to set
245+
/// their values. `u_size` and `u_texture` are set by the engine for
246+
/// [ui.ImageFilter.shader].
247+
///
248+
/// This object owns [shader]. Call [dispose] when it's no longer needed.
249+
class _StretchEffectUniforms {
250+
/// Creates a shader from [program] and looks up its uniforms.
251+
_StretchEffectUniforms(ui.FragmentProgram program) : this._(program.fragmentShader());
252+
253+
_StretchEffectUniforms._(this.shader)
254+
: maxStretchIntensity = shader.getUniformFloat('u_max_stretch_intensity'),
255+
overscrollX = shader.getUniformFloat('u_overscroll_x'),
256+
overscrollY = shader.getUniformFloat('u_overscroll_y'),
257+
interpolationStrength = shader.getUniformFloat('u_interpolation_strength');
258+
259+
final ui.FragmentShader shader;
260+
final ui.UniformFloatSlot maxStretchIntensity;
261+
final ui.UniformFloatSlot overscrollX;
262+
final ui.UniformFloatSlot overscrollY;
263+
final ui.UniformFloatSlot interpolationStrength;
264+
265+
/// Releases [shader].
266+
void dispose() {
267+
shader.dispose();
268+
}
269+
}
270+
241271
class _StretchEffectShader {
242272
static bool _initCalled = false;
243273
static bool _initialized = false;

0 commit comments

Comments
 (0)