Skip to content

BackdropFilter renders offscreen even if not repainted. #27677

@ctrl-shift

Description

@ctrl-shift

Issue:

Bad performance with lots of blur BackdropFilters even if background below them is always the same.

Steps to Reproduce

  1. Add BackdropFilter anywhere in the app. (with visible child like text/etc)
  2. Repaint any other part of screen.
  3. Notice that BackdropFilter is rendered offscreen on every frame (via checkerboardOffscreenLayers and performance overlay).

Is this behavior intended?
If yes, is it possible to somehow cache the output to prevent offscreen re-rendering?

Minimal reproducing code

Should be fine to just drop into new flutter project main.dart

Code below tries to animate a small rectangle with background color and lags a lot due to GPU thread being completely filled with BackdropFilters rendering offscreen.
Note that they are not repainted and RepaintBoundary is not helping with the issue. Animation is not required, absolutely anything that causes frame to paint has the issue (like scrolling scrollview in some other part of screen)

import 'dart:ui';

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      checkerboardOffscreenLayers: true,
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
  AnimationController animation;

  @override
  void initState() {
    super.initState();
    animation = AnimationController(vsync: this, duration: Duration(seconds: 1));
    _startAnimation();
  }

  Future _startAnimation() async {
    try {
      await animation
          .repeat()
          .orCancel;
    } on TickerCanceled {}
  }

  @override
  Widget build(BuildContext context) {
    Widget text() {
      return ClipRect(
        child: BackdropFilter(
            filter: ImageFilter.blur(sigmaX: 5, sigmaY: 5),
            child: Column(
              children: <Widget>[
                Container(padding: EdgeInsets.all(5), child: Text("txt")),
              ],
            )),
      );
    }

    Widget col() {
      return Column(children: List.generate(17, (i) => text()));
    }

    return Scaffold(
      backgroundColor: Colors.grey,
      body: Container(
        child: Column(
          children: [
            Expanded(
              child: RepaintBoundary(
                  child: Center(
                child: AnimatedBuilder(
                    animation: animation,
                    builder: (c, w) {
                      final val = (animation.value * 255).round();
                      return Container(
                        width: 50,
                        height: 50,
                        color: Color.fromARGB(255, val, val, val));
                    }),
              )),
            ),
            SizedBox(height: 50),
            RepaintBoundary(
              child: Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: List.generate(5, (i) => col())),
            ),
            SizedBox(height: 50),
          ],
        ),
      ),
    );
  }
}

Logs

flutter doctor -v:

[√] Flutter (Channel dev, v1.2.0, on Microsoft Windows [Version 10.0.17763.292], locale en-US)
    • Flutter version 1.2.0 at D:\programs\flutter
    • Framework revision 06b979c4d5 (2 weeks ago), 2019-01-25 14:27:35 -0500
    • Engine revision 36acd02c94
    • Dart version 2.1.1 (build 2.1.1-dev.3.2 f4afaee422)

[√] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at D:\programs\androidsdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-28, build-tools 28.0.3
    • ANDROID_HOME = D:\programs\androidsdk
    • ANDROID_SDK_ROOT = D:\programs\androidsdk
    • Java binary at: D:\programs\android-studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)
    • All Android licenses accepted.

[√] Android Studio (version 3.3)
    • Android Studio at D:\programs\android-studio
    • Flutter plugin version 32.0.1
    • Dart plugin version 182.5124
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)

[!] VS Code, 64-bit edition (version 1.25.0)
    • VS Code at C:\Program Files\Microsoft VS Code
    X Flutter extension not installed; install from
      https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[√] Connected device (1 available)
    • Mi A2 Lite • 192.168.0.161:5555 • android-arm64 • Android 9 (API 28)

! Doctor found issues in 1 category.

Flutter analyze output

Analyzing flutter_bug_test...
No issues found! (ran in 2.0s)

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listc: performanceRelates to speed or footprint issues (see "perf:" labels)engineflutter/engine related. See also e: labels.frameworkflutter/packages/flutter repository. See also f: labels.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions