Skip to content

[Impeller] 3.13.2: CustomPaint with saveLayer/restore renders incorrectly on iOS. #134068

@reimager

Description

@reimager

Is there an existing issue for this?

Steps to reproduce

I have reduced the issue to a simple paint program (code below) when you can draw inside a CustomPainter.
The CustomPainter uses saveLayer.

I expect to be able to draw freely within the black box. This works in flutter 3.10.6 on both android and ios.
However on iOS with 3.13.2 it does not render to the screen correctly and only shows a small bounded box.

Some things of note:

  • Only happens if I use saveLayer (which my full application needs)
  • If I save the image using the repaint boundary the image is correct, so I suspect it is purely a rendering issue
  • The weird bounding box appears to move sometimes. not sure what the size of the bounding box is or where that artifact comes from
  • This does not happen with flutter 3.10.2 and prior on iOS. I am testing by using flutter downgrade and then flutter upgrade
  • Happens on iOS emulator or real device

Expected results

I expect to be able to 'draw' within the black box and have it render in real time as I apply new points.

Actual results

With 3.13.2 on iOS the paint is not rendered to the black box. It only appears in a small subset box of the full black box.

Code sample

Code sample
import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class TestPage extends StatefulWidget {
  const TestPage({Key? key}) : super(key: key);

  @override
  State<TestPage> createState() => _TestPageState();
}

class CanvasPath {
  final Paint paint;
  List<Offset> path = [];

  CanvasPath({List<Offset>? path, required this.paint}) {
    this.path = path ?? [];
  }
}

class CanvasPaths with ChangeNotifier {
  List<CanvasPath> list = [];
  CanvasPaths(this.list);

  void notify() {
    notifyListeners();
  }

  set length(int newLength) {
    list.length = newLength;
  }

  int get length => list.length;
  CanvasPath operator [](int index) => list[index];
  void operator []=(int index, CanvasPath value) {
    list[index] = value;
  }

  bool get isEmpty => list.isEmpty;
  bool get isNotEmpty => list.isNotEmpty;
  CanvasPath get last => list.last;
  CanvasPath removeLast() => list.removeLast();
  void add(CanvasPath p) => list.add(p);

  CanvasPaths clone() {
    return CanvasPaths(List.from(list));
  }
}

class _TestPageState extends State<TestPage> {
  // the key for the repaint boundary
  final GlobalKey _repaintBoundaryKey = GlobalKey();

  // the transformation controller for the interactive viewer
  final TransformationController _transformationController = TransformationController();

  // the canvas paths for painting/masking
  final CanvasPaths _paths = CanvasPaths([]);

  // the paint properties
  Paint _paint = Paint()
    ..strokeCap = StrokeCap.round
    ..strokeJoin = StrokeJoin.round
    ..strokeWidth = 40.0
    ..isAntiAlias = true
    ..color = Colors.white
    ..blendMode = BlendMode.srcOver;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _buildPaintWidget(context),
    );
  }

  Widget _buildPaintWidget(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).size.width,
      color: Colors.black,
      child: InteractiveViewer(
        transformationController: _transformationController,
        panEnabled: false,
        scaleEnabled: false,
        minScale: 0.1,
        maxScale: 10.0,
        onInteractionStart: _onScaleStart,
        onInteractionEnd: _onScaleEnd,
        onInteractionUpdate: _onScaleUpdate,
        constrained: false,
        child: Opacity(
          opacity: 0.6,
          child: ClipRect(
            child: RepaintBoundary(
              key: _repaintBoundaryKey,
              child: ChangeNotifierProvider<CanvasPaths>.value(
                value: _paths,
                child: Consumer<CanvasPaths>(
                  builder: (_, points, __) {
                    return Stack(
                      children: [
                        CustomPaint(
                          size: const Size(768.0, 768.0),
                          painter: MyPainter(
                            paths: _paths,
                          ),
                        ),
                      ],
                    );
                  },
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }

  void _onScaleStart(ScaleStartDetails details) {
    var localPosition = _transformationController.toScene(details.localFocalPoint);
    var imageLocalPosition = localPosition.translate(0, 0);

    Paint p = copyPaint(_paint)..strokeWidth = (_paint.strokeWidth / _transformationController.value.getMaxScaleOnAxis());
    _paths.add(CanvasPath(path: [imageLocalPosition], paint: p));
    _paths.notify();
  }

  void _onScaleUpdate(ScaleUpdateDetails details) {
    var localPosition = _transformationController.toScene(details.localFocalPoint);
    var imageLocalPosition = localPosition.translate(0, 0);
    _paths.last.path.add(imageLocalPosition);
    _paths.notify();
  }

  void _onScaleEnd(ScaleEndDetails details) {
    _paths.notify();
  }

  static Paint copyPaint(Paint p) {
    Paint newPaint = Paint();
    newPaint.blendMode = p.blendMode;
    newPaint.color = p.color;
    newPaint.colorFilter = p.colorFilter;
    newPaint.filterQuality = p.filterQuality;
    newPaint.imageFilter = p.imageFilter;
    newPaint.invertColors = p.invertColors;
    newPaint.isAntiAlias = p.isAntiAlias;
    newPaint.maskFilter = p.maskFilter;
    newPaint.shader = p.shader;
    newPaint.strokeCap = p.strokeCap;
    newPaint.strokeJoin = p.strokeJoin;
    newPaint.strokeMiterLimit = p.strokeMiterLimit;
    newPaint.strokeWidth = p.strokeWidth;
    newPaint.style = p.style;
    return newPaint;
  }
}

class MyPainter extends CustomPainter {
  CanvasPaths paths;

  MyPainter({required this.paths});

  @override
  void paint(Canvas canvas, Size size) async {
    // draw all points as a single layer
    canvas.saveLayer(Offset.zero & size, Paint());
    for (int i = 0; i < paths.length; i++) {
      if (paths[i].path.isEmpty) continue;
      Paint paint = paths[i].paint;
      canvas.drawPoints(ui.PointMode.points, paths[i].path, paint);
    }
    canvas.restore();
  }

  @override
  bool shouldRepaint(MyPainter oldDelegate) => true;
}

Screenshots or Video

Screenshots / Video demonstration

good
bad

video of expected results:

Simulator.Screen.Recording.-.iPhone.14.Pro.Max.-.2023-09-05.at.13.15.44.mp4

video of unexpected results:

Simulator.Screen.Recording.-.iPhone.14.Pro.Max.-.2023-09-05.at.13.09.34.mp4

Logs

No response

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.13.2, on macOS 13.5.1 22G90 darwin-x64,
locale en-US)
    • Flutter version 3.13.2 on channel stable at
/Users/dmorris/development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ff5b5b5fa6 (12 days ago), 2023-08-24 08:12:28 -0500
    • Engine revision b20183e040
    • Dart version 3.1.0
    • DevTools version 2.25.0

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at /Users/dmorris/Library/Android/sdk
    • Platform android-33, build-tools 33.0.0
    • Java binary at: /Applications/Android
Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build
11.0.12+0-b1504.28-7817840)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14E300c
    • CocoaPods version 1.12.1

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build
11.0.12+0-b1504.28-7817840)

[✓] VS Code (version 1.81.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.72.0

[✓] Connected device (3 available)
    • iPhone 14 Pro Max (mobile) •
7743D211-BFD8-4E19-B401-9CFED14AF7B2 • ios            •
com.apple.CoreSimulator.SimRuntime.iOS-16-4 (simulator)
    • macOS (desktop)            • macos
 • darwin-x64     • macOS 13.5.1 22G90 darwin-x64
    • Chrome (web)               • chrome
 • web-javascript • Google Chrome 116.0.5845.179

[✓] Network resources
    • All expected network resources are available.

• No issues found!

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work liste: impellerImpeller rendering backend issues and features requeststeam-engineOwned by Engine teamtriaged-engineTriaged by Engine team

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions