frosted method

Image? frosted(
  1. double sigma
)

The copy, blurred by sigma logical px, made once per copy and shared by every glass that frosts by that much. Glass samples it once per pixel rather than blurring the copy itself, which costs it dozens.

Implementation

ui.Image? frosted(double sigma) {
  final image = this.image;
  // On the web every copy is read back from the GPU, which stalls the page
  // (in Firefox, about half a second for a screenful). Glass blurs the copy
  // itself there, on the GPU, rather than wait for a frosted one.
  if (image == null || sigma < 0.25 || kIsWeb) return image;
  final key = (sigma * 4).round();
  final hit = _frosted[key];
  if (hit != null) return hit;
  if (_frosted.length >= 4) _clearFrosted();
  final watch = Stopwatch()..start();
  final deviation = key / 4 * _ratio;
  final width = image.width;
  final height = image.height;
  final recorder = ui.PictureRecorder();
  Canvas(recorder).drawImage(
    image,
    Offset.zero,
    Paint()
      ..filterQuality = FilterQuality.low
      ..imageFilter = ui.ImageFilter.blur(sigmaX: deviation, sigmaY: deviation, tileMode: TileMode.clamp),
  );
  final picture = recorder.endRecording();
  final frosted = picture.toImageSync(width, height);
  picture.dispose();
  _frostCost += watch.elapsed;
  return _frosted[key] = frosted;
}