SkiaSharp icon indicating copy to clipboard operation
SkiaSharp copied to clipboard

[BUG] Using SKCanvas.ClipPath for larger float path have issue

Open JasonLiuLab opened this issue 3 years ago • 3 comments

Description

When configuring a larger float type rect path with SKCanvas.ClipPath, it may result in missing parts of the final rendered image. However, if it is of type int, this issue does not exist.

using SkiaSharp;

using var skPictureRecorder = new SkiaSharp.SKPictureRecorder();
using var skCanvas = skPictureRecorder.BeginRecording(new SkiaSharp.SKRect(0, 0, 20000, 5000));
var path = new SkiaSharp.SKPath();

//float type have issue 
path.AddRect(new(0, 0, 20000, 4999.6f));

skCanvas.ClipPath(path, SKClipOperation.Intersect, true);
var path2 = new SKPath();
path2.AddRect(new(0, 0, 20000, 5000));
skCanvas.DrawPath(path2, new SKPaint()
{
    Style = SkiaSharp.SKPaintStyle.Fill,
    Color = new(255, 0, 0, 225),
    BlendMode = SkiaSharp.SKBlendMode.SrcOver
});
var pic = skPictureRecorder.EndRecording();
using var stream = System.IO.File.OpenWrite("C:\\testspace\\test.png");
var bitmap = new SKBitmap(20000, 5000);
using (var canvas = new SKCanvas(bitmap))
{
    canvas.Clear(SKColors.Transparent);
    canvas.DrawPicture(pic);
}
bitmap.Encode(stream, SKEncodedImageFormat.Png, 100);

Expected Behavior

The rendered image should be almost entirely filled with red.

Actual Behavior

There will be a large missing in the image that is not rendered properly.

Basic Information

  • Version with issue: 2.88.3
  • Last known good version:
  • IDE: Visula studio 2022
  • Platform Target Frameworks:
    • Windows Classic: Windows 11
  • Target Devices:
    • windows 11 PC
Detailed IDE/OS information (click to expand)

.NET Version: .NET 6 , if use following code, the result will be normal, the only chenge of this is replace 4999.6f with 4999


using SkiaSharp;

using var skPictureRecorder = new SkiaSharp.SKPictureRecorder();
using var skCanvas = skPictureRecorder.BeginRecording(new SkiaSharp.SKRect(0, 0, 20000, 5000));
var path = new SkiaSharp.SKPath();

//int type not have issue
path.AddRect(new(0, 0, 20000, 4999));
skCanvas.ClipPath(path, SKClipOperation.Intersect, true);
var path2 = new SKPath();
path2.AddRect(new(0, 0, 20000, 5000));
skCanvas.DrawPath(path2, new SKPaint()
{
    Style = SkiaSharp.SKPaintStyle.Fill,
    Color = new(255, 0, 0, 225),
    BlendMode = SkiaSharp.SKBlendMode.SrcOver
});
var pic = skPictureRecorder.EndRecording();
using var stream = System.IO.File.OpenWrite("C:\\testspace\\test.png");
var bitmap = new SKBitmap(20000, 5000);
using (var canvas = new SKCanvas(bitmap))
{
    canvas.Clear(SKColors.Transparent);
    canvas.DrawPicture(pic);
}
bitmap.Encode(stream, SKEncodedImageFormat.Png, 100);

Screenshots

image

image

Reproduction Link

JasonLiuLab avatar Jul 25 '23 10:07 JasonLiuLab

Hi Team, this issue has been causing disruptions to our business. Unfortunately, we have limited expertise in debugging Skia source code related problems. If you have some free time, could you please assist us by taking a quick look at it?

JasonLiuLab avatar Jul 27 '23 01:07 JasonLiuLab

I reproduced this issue when directly calling Skia using C++ code, so it should be a bug from Skia.

int main()
{
    SkPictureRecorder skPictureRecorder;
    SkCanvas* skCanvas = skPictureRecorder.beginRecording(SkRect{ 0, 0, 20000, 5000 });
    SkPath path;

    //float type have issue 
    path.addRect(SkRect{ 0, 0, 20000, 4999.7f});

    skCanvas->clipPath(path, SkClipOp::kIntersect, true);
    SkPath path2;
    path2.addRect(SkRect{ 0, 0, 20000, 5000 });
    SkPaint paint;
    paint.setStyle(SkPaint::kFill_Style);
    paint.setColor(SkColorSetRGB(225, 255, 0, 0));
    paint.setBlendMode(SkBlendMode::kSrcOver);
    skCanvas->drawPath(path2, paint);
    sk_sp<SkPicture> pic = skPictureRecorder.finishRecordingAsPicture();
    SkFILEWStream stream("C:\\testspace\\test.png");
    SkBitmap bitmap;
    bitmap.allocN32Pixels(20000, 5000);
    SkCanvas canvas(bitmap);
    canvas.clear(SK_ColorTRANSPARENT);
    canvas.drawPicture(pic);
    SkEncodeImage(&stream, bitmap, SkEncodedImageFormat::kPNG, 100);

    return 0;
}

I will report this to Skia team.

JasonLiuLab avatar Aug 05 '23 14:08 JasonLiuLab

https://issues.skia.org/issues/294565262

this is the issue have reported to skia

JasonLiuLab avatar Aug 05 '23 15:08 JasonLiuLab