Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[Impeller] Fix division by zero for transparent shadows (#41391) #42048

Conversation

gaaclarke
Copy link
Member

cherry-pick of #41391

A division by zero happens if the shadow color is fully transparent, and
the NaNs are getting assigned to the RGB values of the paint color being
handed to the rrect shadow draw. This doesn't cause a problem when wide
gamut is off because NaN output ends up being interpreted as zero (thus
making the shadow output fully transparent black, which happens to be
the expected and correct result). However, when a wide gamut attachment
is present, the NaN output ends up being interpreted as a negative
value.

Reproduction app:
```dart
import 'package:flutter/material.dart';

void main() => runApp(const GeneralDialogApp());

class EvilPainter extends CustomPainter {
  @OverRide
  void paint(Canvas canvas, Size size) {
    final Rect rect = Offset.zero & size;
    canvas.drawPaint(Paint()..color = Colors.white);
    canvas.saveLayer(null, Paint()..blendMode = BlendMode.srcOver);
    canvas.drawShadow(Path()..addRect(Rect.fromLTRB(100, 100, 300, 300)),
        Colors.black54, 15, false);
    canvas.drawShadow(Path()..addRect(Rect.fromLTRB(100, 100, 300, 300)),
        Colors.black54, 15, false);
    canvas.drawShadow(Path()..addRect(Rect.fromLTRB(100, 100, 300, 300)),
        Colors.transparent, 15, false);
    canvas.restore();
  }

  @OverRide
  bool shouldRepaint(EvilPainter oldDelegate) => false;
  @OverRide
  bool shouldRebuildSemantics(EvilPainter oldDelegate) => false;
}

class GeneralDialogApp extends StatelessWidget {
  const GeneralDialogApp({super.key});

  @OverRide
  Widget build(BuildContext context) {
    return MaterialApp(
      restorationScopeId: 'app',
      home: CustomPaint(painter: EvilPainter()),
    );
  }
}
```

Before:

![image](https://user-images.githubusercontent.com/919017/233558512-0bfdcdc0-017d-4df3-870f-bd8808ef73b7.png)

After:

![image](https://user-images.githubusercontent.com/919017/233558076-b56c7f43-c81e-4ca6-897d-246251dae930.png)
@zanderso
Copy link
Member

It looks like the includes in the test might need some adjustment to build cleanly.

@XilaiZhang
Copy link
Contributor

umm friendly ping on this pr, if we can fix the tests early we can make it for the stable hotfix this week

@XilaiZhang
Copy link
Contributor

cc @CaseyHillers

@gaaclarke
Copy link
Member Author

yep, one sec

@bdero
Copy link
Member

bdero commented May 15, 2023

I think this branch was cut before Jim's large DL refactor. These imports need to be renamed to display_list_*.h.

@gaaclarke
Copy link
Member Author

My GOMA setup has broken down, pushing speculative fix while I address it locally.

@gaaclarke gaaclarke force-pushed the cp-dd67063fc9219dc5eb5b989e89c784560c7ac483 branch from 69dd5f7 to e5667f7 Compare May 15, 2023 19:11
@gaaclarke
Copy link
Member Author

This should be good now, debug builds are passing.

@XilaiZhang XilaiZhang merged commit fa75008 into flutter:flutter-3.10-candidate.1 May 15, 2023
@XilaiZhang
Copy link
Contributor

Thanks for the fix! merged

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
No open projects
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants