Bug 158014 - Skia adds filled white areas in .svg when exporting to PDF
Summary: Skia adds filled white areas in .svg when exporting to PDF
Status: RESOLVED FIXED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Printing and PDF export (show other bugs)
Version:
(earliest affected)
24.2.0.0 alpha0+
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard: target:24.2.0
Keywords:
Depends on:
Blocks:
 
Reported: 2023-10-31 16:36 UTC by Patrick (volunteer)
Modified: 2023-12-02 18:05 UTC (History)
3 users (show)

See Also:
Crash report or crash signature:


Attachments
Export to PDF of https://bugs.documentfoundation.org/attachment.cgi?id=190138 (244.72 KB, application/pdf)
2023-10-31 16:36 UTC, Patrick (volunteer)
Details
Export as PDF on macOS after comment 8 patch (245.74 KB, application/pdf)
2023-12-02 18:05 UTC, Patrick (volunteer)
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Patrick (volunteer) 2023-10-31 16:36:06 UTC
Created attachment 190567 [details]
Export to PDF of https://bugs.documentfoundation.org/attachment.cgi?id=190138

Steps to reproduce:

1. Open the LibreOffice Options dialog and check one or both Skia checkboxes and restart.

2. Open the following .odp file that contains the LibreOffice .svg logo image:

https://bugs.documentfoundation.org/attachment.cgi?id=190138

3. Export the document to PDF

The resulting PDF will have a white filled shapes near the center upper-right area of the image.
Comment 1 Patrick (volunteer) 2023-11-01 14:17:10 UTC
Did a git bisect and the following seems to be the bad commit. I remember some additional commits after this one were needed to fix some rendering errors in slideshows. I'll look for those and see if I can apply the same fixes when exporting to PDF: 
 e9dfa816f7002c686ad155682a21a06ec6041b5f is the first bad commit
commit e9dfa816f7002c686ad155682a21a06ec6041b5f
Author: Noel Grandin <noel.grandin@collabora.co.uk>
Date:   Mon Aug 28 14:19:58 2023 +0200

    tdf#156808 soft edge effect makes image (except for soft edge) disappear
    
    This is clearly fallout from
         commit 81994cb2b8b32453a92bcb011830fcb884f22ff3
         Date:   Fri Apr 16 20:33:10 2021 +0200
         Convert internal vcl bitmap formats transparency->alpha (II)
    And it clearly fixes the problem, but I'm not sure __why__ it fixes the
    problem.
    
    Change-Id: I805ed85dd22b2124328e6b4dba098c5f093aec55
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/156198
    Tested-by: Jenkins
    Reviewed-by: Patrick Luby <plubius@neooffice.org>
    Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>

 drawinglayer/source/primitive2d/softedgeprimitive2d.cxx | 1 +
 1 file changed, 1 insertion(+)
Comment 2 Patrick (volunteer) 2023-11-01 15:13:15 UTC
Update: if I comment out any AlphaMask::Invert() calls in drawinglayer/source/primitive2d/softedgeprimitive2d.cxx, this bug does not occur. So, I hoping that I can find some attribute that I can use as to determine whether AlphaMask::Invert() is needed or not.
Comment 3 Patrick (volunteer) 2023-11-01 19:05:07 UTC
Below is a debug patch that stops this bug from occurring. I still don't know what is so special about this particular bitmap. So far, I only know that it only happens with Skia enabled and this particular image (i.e. the one single image that is 442x593 pixels. Looks like I will need to think of some other way to debug this:

diff --git a/drawinglayer/source/primitive2d/softedgeprimitive2d.cxx b/drawinglayer/source/primitive2d/softedgeprimitive2d.cxx
index bfd8ebf71656..a9c893e4fc85 100644
--- a/drawinglayer/source/primitive2d/softedgeprimitive2d.cxx
+++ b/drawinglayer/source/primitive2d/softedgeprimitive2d.cxx
@@ -24,6 +24,7 @@
 #include <basegfx/matrix/b2dhommatrixtools.hxx>
 #include <toolkit/helper/vclunohelper.hxx>
 #include <drawinglayer/converters.hxx>
+#include <vcl/skia/SkiaHelper.hxx>
 #include "GlowSoftEgdeShadowTools.hxx"
 
 #ifdef DBG_UTIL
@@ -212,6 +213,12 @@ void SoftEdgePrimitive2D::create2DDecomposition(
         blurMask.Invert();
         aMask.BlendWith(blurMask);
 
+if (SkiaHelper::isVCLSkiaEnabled())
+{
+    if (aMask.GetSizePixel().Width() == 422 && aMask.GetSizePixel().Height() == 593)
+        aMask.Invert();
+}
+
         // The end result is the original bitmap with blurred 8-bit alpha mask
         BitmapEx result(aBitmapEx.GetBitmap(), aMask);
Comment 4 Noel Grandin 2023-11-02 10:15:56 UTC
It has something to do with this block of code

        AlphaMask blurMask(drawinglayer::primitive2d::ProcessAndBlurAlphaMask(
            aMask, -fDiscreteSoftRadius * fScale, fDiscreteSoftRadius * fScale, 0));
        // tdf#157086 invert the blur mask instead of the alpha mask
        // An invert is needed to fix tdf#156808 but inverting the alpha mask
        // causes tdf#157086 so invert the blur mask instead.
        blurMask.Invert();
        aMask.BlendWith(blurMask);


Removing the 
   blurMask.Invert()
fixes this bug, and tdf#157086 stays fixed, but tdf#157086 is then broken.

Not sure what is going on, still looking.
Comment 5 Noel Grandin 2023-11-02 10:59:44 UTC
Hmmm, gerrit seems to be down. Anyhow, I have a fix which I will push to gerrit later. Looks like:

commit 8a8be3b8ceea2b419c7528ffe5df64449149a76e (HEAD -> master)
Author: Noel Grandin <noel.grandin@collabora.co.uk>
Date:   Thu Nov 2 12:32:57 2023 +0200

    tdf#158014 Skia adds filled white areas in .svg when exporting to PDF
    
    Removing the call to
        blurMask.Invert()
    and passing false for the bConvertTo1Bit parameter in the call to
    ProcessAndBlurAlphaMask fixes and keeps tdf#157086 and tdf#157086 happy
    in both skia and non-skia modes
    
    Change-Id: If4c4d4c5351a4ec55897bed96b57d28eda88f5dd

diff --git a/drawinglayer/source/primitive2d/softedgeprimitive2d.cxx b/drawinglayer/source/primitive2d/softedgeprimitive2d.cxx
index bfd8ebf71656..b5f32c958c7f 100644
--- a/drawinglayer/source/primitive2d/softedgeprimitive2d.cxx
+++ b/drawinglayer/source/primitive2d/softedgeprimitive2d.cxx
@@ -205,11 +205,7 @@ void SoftEdgePrimitive2D::create2DDecomposition(
         if (aMask.IsEmpty()) // There is no mask, fully opaque
             break;
         AlphaMask blurMask(drawinglayer::primitive2d::ProcessAndBlurAlphaMask(
-            aMask, -fDiscreteSoftRadius * fScale, fDiscreteSoftRadius * fScale, 0));
-        // tdf#157086 invert the blur mask instead of the alpha mask
-        // An invert is needed to fix tdf#156808 but inverting the alpha mask
-        // causes tdf#157086 so invert the blur mask instead.
-        blurMask.Invert();
+            aMask, -fDiscreteSoftRadius * fScale, fDiscreteSoftRadius * fScale, 0, false));
         aMask.BlendWith(blurMask);
 
         // The end result is the original bitmap with blurred 8-bit alpha mask
Comment 6 Commit Notification 2023-11-03 17:34:06 UTC
Patrick Luby committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/e4821ce3e0eb814f8942b9451183b4589e901ec3

tdf#158014 make image immutable after using Skia to invert

It will be available in 24.2.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 7 Patrick (volunteer) 2023-11-03 17:39:43 UTC
I think that I have fixed this bug on macOS and the fix should be in tomorrow's (04 November 2023) nightly master build.

Apparently, this bug does not occur on Windows. Does anyone know if it occurs on Linux?
Comment 8 Commit Notification 2023-11-05 05:32:54 UTC
Noel Grandin committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/cfd7cffb00f9f4ee934006a706184fefc8cb8d9d

tdf#158014 Skia adds filled white areas in .svg when exporting to PDF

It will be available in 24.2.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 9 Buovjaga 2023-12-02 17:24:04 UTC
I still repro on Linux with

Version: 24.2.0.0.alpha1+ (X86_64) / LibreOffice Community
Build ID: 55097433ed766a2ced6b87021a71c8a31cde9d99
CPU threads: 8; OS: Linux 6.6; UI render: Skia/Raster; VCL: x11
Locale: fi-FI (fi_FI.UTF-8); UI: en-US
Calc: threaded
Comment 10 Patrick (volunteer) 2023-12-02 18:05:04 UTC
I cannot reproduce this bug since the patch in comment 8 on macOS with Skia/Metal, Skia/Raster, or Skia disabled. I've tested a recent nightly master build and my local master build:

Version: 24.2.0.0.alpha1+ (AARCH64) / LibreOffice Community
Build ID: 4f928a5823b0bc7cd8af1d588e2084ae1ff1543f
CPU threads: 8; OS: macOS 14.1.2; UI render: Skia/Raster; VCL: osx
Locale: en-CA (en_CA.UTF-8); UI: en-US
Calc: threaded
Comment 11 Patrick (volunteer) 2023-12-02 18:05:55 UTC
Created attachment 191195 [details]
Export as PDF on macOS after comment 8 patch