[Libreoffice-commits] core.git: basegfx/source canvas/source include/basegfx vcl/unx

Libreoffice Gerrit user logerrit at kemper.freedesktop.org
Thu Sep 20 18:01:32 UTC 2018


 basegfx/source/polygon/b2dlinegeometry.cxx         |  128 ++++++++-
 basegfx/source/polygon/b2dpolygontools.cxx         |   11 
 basegfx/source/polygon/b2dpolygontriangulator.cxx  |   27 +-
 canvas/source/opengl/ogl_canvascustomsprite.cxx    |   31 +-
 canvas/source/opengl/ogl_canvastools.cxx           |   29 +-
 canvas/source/tools/surfaceproxy.cxx               |   19 +
 include/basegfx/polygon/b2dlinegeometry.hxx        |    9 
 include/basegfx/polygon/b2dpolygontools.hxx        |    5 
 include/basegfx/polygon/b2dpolygontriangulator.hxx |   35 ++
 vcl/unx/generic/gdi/gdiimpl.cxx                    |  274 +++++++++++++++------
 vcl/unx/generic/gdi/gdiimpl.hxx                    |    6 
 vcl/unx/generic/gdi/salgdi.cxx                     |   10 
 vcl/unx/generic/gdi/xrender_peer.hxx               |   11 
 13 files changed, 461 insertions(+), 134 deletions(-)

New commits:
commit 66232248ff55639052ddb76918d555e21dc9c46b
Author:     Armin Le Grand <Armin.Le.Grand at cib.de>
AuthorDate: Sat Sep 15 13:48:12 2018 +0200
Commit:     Armin Le Grand <Armin.Le.Grand at cib.de>
CommitDate: Thu Sep 20 20:01:07 2018 +0200

    Support buffering SystemDependent GraphicData (III)
    
    This change is for speedup of fat line drawing when using
    X11. This is a long-term problem which never really progressed,
    but is avoided using Cairo in the future. Still - if used,
    speedup using current state and buffering possibilities.
    
    Two speedup steps will be used:
    (1) The tesselation is no longer done using trapezoids. That
    works (but was done wrong leaving artifacts) but is not fast
    and done every time. It is even not done with FatLines and
    more than 1000 points.
    New version will use triangulation. Dspite using the existing
    triangulator (that works but is slow) extend the FatLine
    geometry creator to directly create triangles.
    This is also necessary since for buffering that data a
    transformation-invariant version is needed (in device coordinates
    the data changes all the time when scrolling). Trapezoids are
    by definition *not* transformation-invariant (e.g. rotation)
    
    (2) Buffer that triangulation - with the needed care and watch.
    It is e.g. necessary to react on 'hairlines' since these change
    their logical LineWidth view-dependent (zoom). In those cases, the
    buffered data *has* to be removed due to the base for buffering is
    the created FatLine geometry based on one stable logical LineWidth
    
    Also took the time to adapt B2DPolygonTriangulator to use an
    own data type (B2DTriangle) and a vector of these for better
    understandability and security. Adapted all usages as needed.
    
    Change-Id: Iedb2932b094a8786fd9c32d0d0ab1ca603a1a7b2
    Reviewed-on: https://gerrit.libreoffice.org/60818
    Tested-by: Jenkins
    Reviewed-by: Armin Le Grand <Armin.Le.Grand at cib.de>

diff --git a/basegfx/source/polygon/b2dlinegeometry.cxx b/basegfx/source/polygon/b2dlinegeometry.cxx
index ca4b88238383..78f569970df3 100644
--- a/basegfx/source/polygon/b2dlinegeometry.cxx
+++ b/basegfx/source/polygon/b2dlinegeometry.cxx
@@ -30,6 +30,7 @@
 #include <basegfx/matrix/b2dhommatrixtools.hxx>
 #include <com/sun/star/drawing/LineCap.hpp>
 #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
+#include <basegfx/polygon/b2dpolygontriangulator.hxx>
 
 namespace basegfx
 {
@@ -337,7 +338,8 @@ namespace basegfx
             bool bStartRound,
             bool bEndRound,
             bool bStartSquare,
-            bool bEndSquare)
+            bool bEndSquare,
+            basegfx::triangulator::B2DTriangleVector* pTriangles)
         {
             // create polygon for edge
             // Unfortunately, while it would be geometrically correct to not add
@@ -566,6 +568,15 @@ namespace basegfx
                     }
                 }
 
+                if(nullptr != pTriangles)
+                {
+                    const basegfx::triangulator::B2DTriangleVector aResult(
+                        basegfx::triangulator::triangulate(
+                            aBezierPolygon));
+                    pTriangles->insert(pTriangles->end(), aResult.begin(), aResult.end());
+                    aBezierPolygon.clear();
+                }
+
                 // return
                 return aBezierPolygon;
             }
@@ -664,6 +675,15 @@ namespace basegfx
                 // close and return
                 aEdgePolygon.setClosed(true);
 
+                if(nullptr != pTriangles)
+                {
+                    const basegfx::triangulator::B2DTriangleVector aResult(
+                        basegfx::triangulator::triangulate(
+                            aEdgePolygon));
+                    pTriangles->insert(pTriangles->end(), aResult.begin(), aResult.end());
+                    aEdgePolygon.clear();
+                }
+
                 return aEdgePolygon;
             }
         }
@@ -676,7 +696,8 @@ namespace basegfx
             const B2DPoint& rPoint,
             double fHalfLineWidth,
             B2DLineJoin eJoin,
-            double fMiterMinimumAngle)
+            double fMiterMinimumAngle,
+            basegfx::triangulator::B2DTriangleVector* pTriangles)
         {
             OSL_ENSURE(fHalfLineWidth > 0.0, "createAreaGeometryForJoin: LineWidth too small (!)");
             OSL_ENSURE(eJoin != B2DLineJoin::NONE, "createAreaGeometryForJoin: B2DLineJoin::NONE not allowed (!)");
@@ -703,9 +724,19 @@ namespace basegfx
             {
                 case B2DLineJoin::Miter :
                 {
-                    aEdgePolygon.append(aEndPoint);
-                    aEdgePolygon.append(rPoint);
-                    aEdgePolygon.append(aStartPoint);
+                    if(nullptr != pTriangles)
+                    {
+                        pTriangles->emplace_back(
+                            aEndPoint,
+                            rPoint,
+                            aStartPoint);
+                    }
+                    else
+                    {
+                        aEdgePolygon.append(aEndPoint);
+                        aEdgePolygon.append(rPoint);
+                        aEdgePolygon.append(aStartPoint);
+                    }
 
                     // Look for the cut point between start point along rTangentPrev and
                     // end point along rTangentEdge. -rTangentEdge should be used, but since
@@ -718,7 +749,18 @@ namespace basegfx
                     if(fCutPos != 0.0)
                     {
                         const B2DPoint aCutPoint(aStartPoint + (rTangentPrev * fCutPos));
-                        aEdgePolygon.append(aCutPoint);
+
+                        if(nullptr != pTriangles)
+                        {
+                            pTriangles->emplace_back(
+                                aStartPoint,
+                                aCutPoint,
+                                aEndPoint);
+                        }
+                        else
+                        {
+                            aEdgePolygon.append(aCutPoint);
+                        }
                     }
 
                     break;
@@ -744,14 +786,27 @@ namespace basegfx
 
                     if(aBow.count() > 1)
                     {
-                        // #i101491#
-                        // use the original start/end positions; the ones from bow creation may be numerically
-                        // different due to their different creation. To guarantee good merging quality with edges
-                        // and edge roundings (and to reduce point count)
-                        aEdgePolygon = aBow;
-                        aEdgePolygon.setB2DPoint(0, aStartPoint);
-                        aEdgePolygon.setB2DPoint(aEdgePolygon.count() - 1, aEndPoint);
-                        aEdgePolygon.append(rPoint);
+                        if(nullptr != pTriangles)
+                        {
+                            for(sal_uInt32 a(0); a < aBow.count() - 1; a++)
+                            {
+                                pTriangles->emplace_back(
+                                    0 == a ? aStartPoint : aBow.getB2DPoint(a),
+                                    rPoint,
+                                    aBow.count() - 1 == a + 1 ? aEndPoint : aBow.getB2DPoint(a + 1));
+                            }
+                        }
+                        else
+                        {
+                            // #i101491#
+                            // use the original start/end positions; the ones from bow creation may be numerically
+                            // different due to their different creation. To guarantee good merging quality with edges
+                            // and edge roundings (and to reduce point count)
+                            aEdgePolygon = aBow;
+                            aEdgePolygon.setB2DPoint(0, aStartPoint);
+                            aEdgePolygon.setB2DPoint(aEdgePolygon.count() - 1, aEndPoint);
+                            aEdgePolygon.append(rPoint);
+                        }
 
                         break;
                     }
@@ -762,9 +817,19 @@ namespace basegfx
                 }
                 default: // B2DLineJoin::Bevel
                 {
-                    aEdgePolygon.append(aEndPoint);
-                    aEdgePolygon.append(rPoint);
-                    aEdgePolygon.append(aStartPoint);
+                    if(nullptr != pTriangles)
+                    {
+                        pTriangles->emplace_back(
+                            aEndPoint,
+                            rPoint,
+                            aStartPoint);
+                    }
+                    else
+                    {
+                        aEdgePolygon.append(aEndPoint);
+                        aEdgePolygon.append(rPoint);
+                        aEdgePolygon.append(aStartPoint);
+                    }
 
                     break;
                 }
@@ -786,7 +851,8 @@ namespace basegfx
             css::drawing::LineCap eCap,
             double fMaxAllowedAngle,
             double fMaxPartOfEdge,
-            double fMiterMinimumAngle)
+            double fMiterMinimumAngle,
+            basegfx::triangulator::B2DTriangleVector* pTriangles)
         {
             if(fMaxAllowedAngle > F_PI2)
             {
@@ -895,7 +961,8 @@ namespace basegfx
                                         aEdge.getStartPoint(),
                                         fHalfLineWidth,
                                         eJoin,
-                                        fMiterMinimumAngle));
+                                        fMiterMinimumAngle,
+                                        pTriangles));
                             }
                             else if(aOrientation == B2VectorOrientation::Negative)
                             {
@@ -911,7 +978,8 @@ namespace basegfx
                                         aEdge.getStartPoint(),
                                         fHalfLineWidth,
                                         eJoin,
-                                        fMiterMinimumAngle));
+                                        fMiterMinimumAngle,
+                                        pTriangles));
                             }
                         }
 
@@ -929,7 +997,8 @@ namespace basegfx
                                     bFirst && eCap == css::drawing::LineCap_ROUND,
                                     bLast && eCap == css::drawing::LineCap_ROUND,
                                     bFirst && eCap == css::drawing::LineCap_SQUARE,
-                                    bLast && eCap == css::drawing::LineCap_SQUARE));
+                                    bLast && eCap == css::drawing::LineCap_SQUARE,
+                                    pTriangles));
                         }
                         else
                         {
@@ -940,7 +1009,8 @@ namespace basegfx
                                     false,
                                     false,
                                     false,
-                                    false));
+                                    false,
+                                    pTriangles));
                         }
 
                         // prepare next step
@@ -958,10 +1028,22 @@ namespace basegfx
                 else
                 {
                     // point count, but no edge count -> single point
-                    aRetval.append(
+                    const basegfx::B2DPolygon aCircle(
                         createPolygonFromCircle(
                             aCandidate.getB2DPoint(0),
                             fHalfLineWidth));
+
+                    if(nullptr != pTriangles)
+                    {
+                        const basegfx::triangulator::B2DTriangleVector aResult(
+                            basegfx::triangulator::triangulate(
+                                aCircle));
+                        pTriangles->insert(pTriangles->end(), aResult.begin(), aResult.end());
+                    }
+                    else
+                    {
+                        aRetval.append(aCircle);
+                    }
                 }
 
                 return aRetval;
diff --git a/basegfx/source/polygon/b2dpolygontools.cxx b/basegfx/source/polygon/b2dpolygontools.cxx
index b5d2abf7e4be..27bfa56d8ced 100644
--- a/basegfx/source/polygon/b2dpolygontools.cxx
+++ b/basegfx/source/polygon/b2dpolygontools.cxx
@@ -2133,7 +2133,9 @@ namespace basegfx
             return ((fCrossA > 0.0) == (fCrossB > 0.0));
         }
 
-        void addTriangleFan(const B2DPolygon& rCandidate, B2DPolygon& rTarget)
+        void addTriangleFan(
+            const B2DPolygon& rCandidate,
+            triangulator::B2DTriangleVector& rTarget)
         {
             const sal_uInt32 nCount(rCandidate.count());
 
@@ -2145,9 +2147,10 @@ namespace basegfx
                 for(sal_uInt32 a(2); a < nCount; a++)
                 {
                     const B2DPoint aCurrent(rCandidate.getB2DPoint(a));
-                    rTarget.append(aStart);
-                    rTarget.append(aLast);
-                    rTarget.append(aCurrent);
+                    rTarget.emplace_back(
+                        aStart,
+                        aLast,
+                        aCurrent);
 
                     // prepare next
                     aLast = aCurrent;
diff --git a/basegfx/source/polygon/b2dpolygontriangulator.cxx b/basegfx/source/polygon/b2dpolygontriangulator.cxx
index ab97419144d4..c24a78f579b8 100644
--- a/basegfx/source/polygon/b2dpolygontriangulator.cxx
+++ b/basegfx/source/polygon/b2dpolygontriangulator.cxx
@@ -110,7 +110,7 @@ namespace basegfx
             EdgeEntry*                                      mpList;
             EdgeEntries                                     maStartEntries;
             std::vector< std::unique_ptr<EdgeEntry> >       maNewEdgeEntries;
-            B2DPolygon                                      maResult;
+            triangulator::B2DTriangleVector                 maResult;
 
             void handleClosingEdge(const B2DPoint& rStart, const B2DPoint& rEnd);
             bool CheckPointInTriangle(EdgeEntry* pEdgeA, EdgeEntry const * pEdgeB, const B2DPoint& rTestPoint);
@@ -119,7 +119,7 @@ namespace basegfx
         public:
             explicit Triangulator(const B2DPolyPolygon& rCandidate);
 
-            const B2DPolygon& getResult() const { return maResult; }
+            const triangulator::B2DTriangleVector& getResult() const { return maResult; }
         };
 
         void Triangulator::handleClosingEdge(const B2DPoint& rStart, const B2DPoint& rEnd)
@@ -203,9 +203,10 @@ namespace basegfx
 
         void Triangulator::createTriangle(const B2DPoint& rA, const B2DPoint& rB, const B2DPoint& rC)
         {
-            maResult.append(rA);
-            maResult.append(rB);
-            maResult.append(rC);
+            maResult.emplace_back(
+                rA,
+                rB,
+                rC);
         }
 
         // consume as long as there are edges
@@ -370,9 +371,9 @@ namespace basegfx
 {
     namespace triangulator
     {
-        B2DPolygon triangulate(const B2DPolygon& rCandidate)
+        B2DTriangleVector triangulate(const B2DPolygon& rCandidate)
         {
-            B2DPolygon aRetval;
+            B2DTriangleVector aRetval;
 
             // subdivide locally (triangulate does not work with beziers), remove double and neutral points
             B2DPolygon aCandidate(rCandidate.areControlPointsUsed() ? utils::adaptiveSubdivideByAngle(rCandidate) : rCandidate);
@@ -382,7 +383,10 @@ namespace basegfx
             if(aCandidate.count() == 2)
             {
                 // candidate IS a triangle, just append
-                aRetval.append(aCandidate);
+                aRetval.emplace_back(
+                    aCandidate.getB2DPoint(0),
+                    aCandidate.getB2DPoint(1),
+                    aCandidate.getB2DPoint(2));
             }
             else if(aCandidate.count() > 2)
             {
@@ -396,6 +400,7 @@ namespace basegfx
                     // polygon is concave.
                     const B2DPolyPolygon aCandPolyPoly(aCandidate);
                     Triangulator aTriangulator(aCandPolyPoly);
+
                     aRetval = aTriangulator.getResult();
                 }
             }
@@ -403,9 +408,9 @@ namespace basegfx
             return aRetval;
         }
 
-        B2DPolygon triangulate(const B2DPolyPolygon& rCandidate)
+        B2DTriangleVector triangulate(const B2DPolyPolygon& rCandidate)
         {
-            B2DPolygon aRetval;
+            B2DTriangleVector aRetval;
 
             // subdivide locally (triangulate does not work with beziers)
             B2DPolyPolygon aCandidate(rCandidate.areControlPointsUsed() ? utils::adaptiveSubdivideByAngle(rCandidate) : rCandidate);
@@ -414,11 +419,13 @@ namespace basegfx
             {
                 // single polygon -> single polygon triangulation
                 const B2DPolygon aSinglePolygon(aCandidate.getB2DPolygon(0));
+
                 aRetval = triangulate(aSinglePolygon);
             }
             else
             {
                 Triangulator aTriangulator(aCandidate);
+
                 aRetval = aTriangulator.getResult();
             }
 
diff --git a/canvas/source/opengl/ogl_canvascustomsprite.cxx b/canvas/source/opengl/ogl_canvascustomsprite.cxx
index 5f53ab4ece46..3cb5ec9fe217 100644
--- a/canvas/source/opengl/ogl_canvascustomsprite.cxx
+++ b/canvas/source/opengl/ogl_canvascustomsprite.cxx
@@ -191,18 +191,35 @@ namespace oglcanvas
                     const double fHeight=maSize.Height;
 
                     // TODO(P3): buffer triangulation
-                    const ::basegfx::B2DPolygon& rTriangulatedPolygon(
+                    const ::basegfx::triangulator::B2DTriangleVector rTriangulatedPolygon(
                         ::basegfx::triangulator::triangulate(
                             ::basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(mxClip)));
 
                     glBegin(GL_TRIANGLES);
-                    for( sal_uInt32 i=0; i<rTriangulatedPolygon.count(); i++ )
+                    for( size_t i=0; i<rTriangulatedPolygon.size(); i++ )
                     {
-                        const ::basegfx::B2DPoint& rPt( rTriangulatedPolygon.getB2DPoint(i) );
-                        const double s(rPt.getX()/fWidth);
-                        const double t(rPt.getY()/fHeight);
-                        glTexCoord2f(s,t); glVertex2d(rPt.getX(), rPt.getY());
-                    }
+                        const::basegfx::triangulator::B2DTriangle& rCandidate(rTriangulatedPolygon[i]);
+                        glTexCoord2f(
+                            rCandidate.getA().getX()/fWidth,
+                            rCandidate.getA().getY()/fHeight);
+                        glVertex2d(
+                            rCandidate.getA().getX(),
+                            rCandidate.getA().getY());
+
+                        glTexCoord2f(
+                            rCandidate.getB().getX()/fWidth,
+                            rCandidate.getB().getY()/fHeight);
+                        glVertex2d(
+                            rCandidate.getB().getX(),
+                            rCandidate.getB().getY());
+
+                        glTexCoord2f(
+                            rCandidate.getC().getX()/fWidth,
+                            rCandidate.getC().getY()/fHeight);
+                        glVertex2d(
+                            rCandidate.getC().getX(),
+                            rCandidate.getC().getY());
+                     }
                     glEnd();
                 }
                 else
diff --git a/canvas/source/opengl/ogl_canvastools.cxx b/canvas/source/opengl/ogl_canvastools.cxx
index cbf20db3a1e5..56fa47fbf303 100644
--- a/canvas/source/opengl/ogl_canvastools.cxx
+++ b/canvas/source/opengl/ogl_canvastools.cxx
@@ -35,15 +35,32 @@ namespace oglcanvas
         const ::basegfx::B2DRange& rBounds(aPolyPoly.getB2DRange());
         const double nWidth=rBounds.getWidth();
         const double nHeight=rBounds.getHeight();
-        const ::basegfx::B2DPolygon& rTriangulatedPolygon(
+        const ::basegfx::triangulator::B2DTriangleVector rTriangulatedPolygon(
             ::basegfx::triangulator::triangulate(aPolyPoly));
 
-        for( sal_uInt32 i=0; i<rTriangulatedPolygon.count(); i++ )
+        for( size_t i=0; i<rTriangulatedPolygon.size(); i++ )
         {
-            const ::basegfx::B2DPoint& rPt( rTriangulatedPolygon.getB2DPoint(i) );
-            const double s(rPt.getX()/nWidth);
-            const double t(rPt.getY()/nHeight);
-            glTexCoord2f(s,t); glVertex2d(rPt.getX(), rPt.getY());
+            const::basegfx::triangulator::B2DTriangle& rCandidate(rTriangulatedPolygon[i]);
+            glTexCoord2f(
+                rCandidate.getA().getX()/nWidth,
+                rCandidate.getA().getY()/nHeight);
+            glVertex2d(
+                rCandidate.getA().getX(),
+                rCandidate.getA().getY());
+
+            glTexCoord2f(
+                rCandidate.getB().getX()/nWidth,
+                rCandidate.getB().getY()/nHeight);
+            glVertex2d(
+                rCandidate.getB().getX(),
+                rCandidate.getB().getY());
+
+            glTexCoord2f(
+                rCandidate.getC().getX()/nWidth,
+                rCandidate.getC().getY()/nHeight);
+            glVertex2d(
+                rCandidate.getC().getX(),
+                rCandidate.getC().getY());
         }
     }
 
diff --git a/canvas/source/tools/surfaceproxy.cxx b/canvas/source/tools/surfaceproxy.cxx
index 5c1bff2a5950..164fb781b4d9 100644
--- a/canvas/source/tools/surfaceproxy.cxx
+++ b/canvas/source/tools/surfaceproxy.cxx
@@ -111,15 +111,28 @@ namespace canvas
                              const ::basegfx::B2DPolyPolygon& rClipPoly,
                              const ::basegfx::B2DHomMatrix&   rTransform )
     {
-        const ::basegfx::B2DPolygon& rTriangulatedPolygon(
+        const ::basegfx::triangulator::B2DTriangleVector& rTriangulatedVector(
             ::basegfx::triangulator::triangulate(rClipPoly));
 
+        // we have now an explicit ::B2DTriangle and ::B2DTriangleVector,
+        // but I do not know enough about 'drawWithClip' or 'clipTriangleListOnRange'
+        // to adapt to that. Convert back to old three-point-in-polygon convention
+        ::basegfx::B2DPolygon aTriangulatedPolygon;
+        aTriangulatedPolygon.reserve(rTriangulatedVector.size() * 3);
+
+        for(const auto& rCandidate : rTriangulatedVector)
+        {
+            aTriangulatedPolygon.append(rCandidate.getA());
+            aTriangulatedPolygon.append(rCandidate.getB());
+            aTriangulatedPolygon.append(rCandidate.getC());
+        }
+
         // dump polygons
         SAL_INFO("canvas", "Original clip polygon: " <<  basegfx::utils::exportToSvgD( rClipPoly, true, true, false ));
-        SAL_INFO("canvas", "Triangulated polygon: " <<  basegfx::utils::exportToSvgD(basegfx::B2DPolyPolygon(rTriangulatedPolygon), true, true, false ));
+        SAL_INFO("canvas", "Triangulated polygon: " <<  basegfx::utils::exportToSvgD(basegfx::B2DPolyPolygon(aTriangulatedPolygon), true, true, false ));
 
         for( const auto& rSurfacePtr : maSurfaceList )
-            rSurfacePtr->drawWithClip( fAlpha, rPos, rTriangulatedPolygon, rTransform );
+            rSurfacePtr->drawWithClip( fAlpha, rPos, aTriangulatedPolygon, rTransform );
 
         return true;
     }
diff --git a/include/basegfx/polygon/b2dlinegeometry.hxx b/include/basegfx/polygon/b2dlinegeometry.hxx
index 29be9934ede0..cdfad4a0ddc5 100644
--- a/include/basegfx/polygon/b2dlinegeometry.hxx
+++ b/include/basegfx/polygon/b2dlinegeometry.hxx
@@ -26,7 +26,7 @@
 #include <basegfx/polygon/b2dpolygon.hxx>
 #include <com/sun/star/drawing/LineCap.hpp>
 #include <basegfx/basegfxdllapi.h>
-
+#include <basegfx/polygon/b2dpolygontriangulator.hxx>
 
 namespace basegfx
 {
@@ -124,6 +124,10 @@ namespace basegfx
             the usual fallback to bevel is used. Allowed range is cropped
             to [F_PI .. 0.01 * F_PI].
 
+            @param pTriangles
+            If given, the methjod will additionally add the created geometry as
+            B2DTriangle's
+
             @return
             The tools::PolyPolygon containing the geometry of the extended line by
             it's line width. Contains bezier segments and edge roundings as
@@ -136,7 +140,8 @@ namespace basegfx
             css::drawing::LineCap eCap,
             double fMaxAllowedAngle = basegfx::deg2rad(12.5),
             double fMaxPartOfEdge = 0.4,
-            double fMiterMinimumAngle = basegfx::deg2rad(15.0));
+            double fMiterMinimumAngle = basegfx::deg2rad(15.0),
+            basegfx::triangulator::B2DTriangleVector* pTriangles = nullptr);
 
     } // end of namespace utils
 } // end of namespace basegfx
diff --git a/include/basegfx/polygon/b2dpolygontools.hxx b/include/basegfx/polygon/b2dpolygontools.hxx
index 039000f053d1..62430d323cef 100644
--- a/include/basegfx/polygon/b2dpolygontools.hxx
+++ b/include/basegfx/polygon/b2dpolygontools.hxx
@@ -25,6 +25,7 @@
 #include <basegfx/range/b2drectangle.hxx>
 #include <basegfx/polygon/b2dpolypolygon.hxx>
 #include <basegfx/polygon/b3dpolygon.hxx>
+#include <basegfx/polygon/b2dpolygontriangulator.hxx>
 #include <com/sun/star/drawing/PointSequence.hpp>
 #include <com/sun/star/drawing/FlagSequence.hpp>
 #include <vector>
@@ -356,7 +357,9 @@ namespace basegfx
         // add triangles for given rCandidate to rTarget. For each triangle, 3 points will be added to rCandidate.
         // All triangles will go from the start point of rCandidate to two consecutive points, building (rCandidate.count() - 2)
         // triangles.
-        BASEGFX_DLLPUBLIC void addTriangleFan(const B2DPolygon& rCandidate, B2DPolygon& rTarget);
+        BASEGFX_DLLPUBLIC void addTriangleFan(
+            const B2DPolygon& rCandidate,
+            triangulator::B2DTriangleVector& rTarget);
 
         // grow for polygon. Move all geometry in each point in the direction of the normal in that point
         // with the given amount. Value may be negative.
diff --git a/include/basegfx/polygon/b2dpolygontriangulator.hxx b/include/basegfx/polygon/b2dpolygontriangulator.hxx
index d3e92a017d79..7ecb819b3241 100644
--- a/include/basegfx/polygon/b2dpolygontriangulator.hxx
+++ b/include/basegfx/polygon/b2dpolygontriangulator.hxx
@@ -28,11 +28,42 @@ namespace basegfx
 {
     namespace triangulator
     {
+        // Simple B2D-based triangle. Main reason is to
+        // keep the data types separated (before a B2DPolygon
+        // was used with the convention that three points in
+        // a row define a triangle)
+        class BASEGFX_DLLPUBLIC B2DTriangle
+        {
+            // positions
+            basegfx::B2DPoint                       maA;
+            basegfx::B2DPoint                       maB;
+            basegfx::B2DPoint                       maC;
+
+        public:
+            B2DTriangle(
+                const basegfx::B2DPoint& rA,
+                const basegfx::B2DPoint& rB,
+                const basegfx::B2DPoint& rC)
+            :   maA(rA),
+                maB(rB),
+                maC(rC)
+            {
+            }
+
+            // get positions
+            const basegfx::B2DPoint& getA() const { return maA; }
+            const basegfx::B2DPoint& getB() const { return maB; }
+            const basegfx::B2DPoint& getC() const { return maC; }
+        };
+
+        // typedef for a vector of B2DTriangle
+        typedef ::std::vector< B2DTriangle > B2DTriangleVector;
+
         // triangulate given polygon
-        BASEGFX_DLLPUBLIC ::basegfx::B2DPolygon triangulate(const ::basegfx::B2DPolygon& rCandidate);
+        BASEGFX_DLLPUBLIC B2DTriangleVector triangulate(const ::basegfx::B2DPolygon& rCandidate);
 
         // triangulate given PolyPolygon
-        BASEGFX_DLLPUBLIC ::basegfx::B2DPolygon triangulate(const ::basegfx::B2DPolyPolygon& rCandidate);
+        BASEGFX_DLLPUBLIC B2DTriangleVector triangulate(const ::basegfx::B2DPolyPolygon& rCandidate);
 
     } // end of namespace triangulator
 } // end of namespace basegfx
diff --git a/vcl/unx/generic/gdi/gdiimpl.cxx b/vcl/unx/generic/gdi/gdiimpl.cxx
index a5fcf86cd3a7..8b0742b4ec68 100644
--- a/vcl/unx/generic/gdi/gdiimpl.cxx
+++ b/vcl/unx/generic/gdi/gdiimpl.cxx
@@ -50,6 +50,7 @@
 #include <basegfx/matrix/b2dhommatrixtools.hxx>
 #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
 #include <basegfx/polygon/b2dtrapezoid.hxx>
+#include <basegfx/utils/systemdependentdata.hxx>
 #include <ControlCacheKey.hxx>
 
 #undef SALGDI2_TESTTRANS
@@ -1569,6 +1570,110 @@ bool X11SalGraphicsImpl::drawFilledTrapezoids( const basegfx::B2DTrapezoid* pB2D
     return true;
 }
 
+bool X11SalGraphicsImpl::drawFilledTriangles(
+    const basegfx::B2DHomMatrix& rObjectToDevice,
+     const basegfx::triangulator::B2DTriangleVector& rTriangles,
+     double fTransparency,
+     bool bPixelOffset)
+{
+    if(rTriangles.empty())
+        return true;
+
+    Picture aDstPic = GetXRenderPicture();
+    // check xrender support for this drawable
+    if( !aDstPic )
+    {
+        return false;
+    }
+
+    // prepare transformation for ObjectToDevice coordinate system
+    basegfx::B2DHomMatrix aObjectToDevice(rObjectToDevice);
+
+    if(bPixelOffset)
+    {
+        aObjectToDevice = basegfx::utils::createTranslateB2DHomMatrix(0.5, 0.5) * aObjectToDevice;
+    }
+
+     // convert the Triangles into XRender-Triangles
+    std::vector<XTriangle> aTriVector(rTriangles.size());
+    sal_uInt32 nIndex(0);
+
+    for(const auto& rCandidate : rTriangles)
+    {
+        const basegfx::B2DPoint aP1(aObjectToDevice * rCandidate.getA());
+        const basegfx::B2DPoint aP2(aObjectToDevice * rCandidate.getB());
+        const basegfx::B2DPoint aP3(aObjectToDevice * rCandidate.getC());
+        XTriangle& rTri(aTriVector[nIndex++]);
+
+        rTri.p1.x = XDoubleToFixed(aP1.getX());
+        rTri.p1.y = XDoubleToFixed(aP1.getY());
+
+        rTri.p2.x = XDoubleToFixed(aP2.getX());
+        rTri.p2.y = XDoubleToFixed(aP2.getY());
+
+        rTri.p3.x = XDoubleToFixed(aP3.getX());
+        rTri.p3.y = XDoubleToFixed(aP3.getY());
+    }
+
+    // get xrender Picture for polygon foreground
+    // TODO: cache it like the target picture which uses GetXRenderPicture()
+    XRenderPeer& rRenderPeer = XRenderPeer::GetInstance();
+    SalDisplay::RenderEntry& rEntry = mrParent.GetDisplay()->GetRenderEntries( mrParent.m_nXScreen )[ 32 ];
+    if( !rEntry.m_aPicture )
+    {
+        Display* pXDisplay = mrParent.GetXDisplay();
+
+        rEntry.m_aPixmap = limitXCreatePixmap( pXDisplay, mrParent.hDrawable_, 1, 1, 32 );
+        XRenderPictureAttributes aAttr;
+        aAttr.repeat = int(true);
+
+        XRenderPictFormat* pXRPF = rRenderPeer.FindStandardFormat( PictStandardARGB32 );
+        rEntry.m_aPicture = rRenderPeer.CreatePicture( rEntry.m_aPixmap, pXRPF, CPRepeat, &aAttr );
+    }
+
+    // set polygon foreground color and opacity
+    XRenderColor aRenderColor = GetXRenderColor( mnBrushColor , fTransparency );
+    rRenderPeer.FillRectangle( PictOpSrc, rEntry.m_aPicture, &aRenderColor, 0, 0, 1, 1 );
+
+    // set clipping
+    // TODO: move into GetXRenderPicture?
+    if( mrParent.mpClipRegion && !XEmptyRegion( mrParent.mpClipRegion ) )
+        rRenderPeer.SetPictureClipRegion( aDstPic, mrParent.mpClipRegion );
+
+    // render the trapezoids
+    const XRenderPictFormat* pMaskFormat = rRenderPeer.GetStandardFormatA8();
+    rRenderPeer.CompositeTriangles( PictOpOver,
+        rEntry.m_aPicture, aDstPic, pMaskFormat, 0, 0, &aTriVector[0], aTriVector.size() );
+
+    return true;
+}
+
+class SystemDependentData_Triangulation : public basegfx::SystemDependentData
+{
+private:
+    basegfx::triangulator::B2DTriangleVector    maTriangles;
+    basegfx::B2DVector                          maLineWidth;
+
+public:
+    SystemDependentData_Triangulation(
+        basegfx::SystemDependentDataManager& rSystemDependentDataManager,
+        const basegfx::triangulator::B2DTriangleVector& rTriangles,
+        const basegfx::B2DVector& rLineWidth);
+
+    const basegfx::triangulator::B2DTriangleVector& getTriangles() const { return maTriangles; }
+    const basegfx::B2DVector& getLineWidth() const { return maLineWidth; }
+};
+
+SystemDependentData_Triangulation::SystemDependentData_Triangulation(
+    basegfx::SystemDependentDataManager& rSystemDependentDataManager,
+    const basegfx::triangulator::B2DTriangleVector& rTriangles,
+    const basegfx::B2DVector& rLineWidth)
+:   basegfx::SystemDependentData(rSystemDependentDataManager),
+    maTriangles(rTriangles),
+    maLineWidth(rLineWidth)
+{
+}
+
 bool X11SalGraphicsImpl::drawPolyLine(
     const basegfx::B2DHomMatrix& rObjectToDevice,
     const basegfx::B2DPolygon& rPolygon,
@@ -1579,96 +1684,121 @@ bool X11SalGraphicsImpl::drawPolyLine(
     double fMiterMinimumAngle,
     bool bPixelSnapHairline)
 {
-    // Transform to DeviceCoordinates, get DeviceLineWidth, execute PixelSnapHairline
-    const basegfx::B2DVector aLineWidth(rObjectToDevice * rLineWidth);
-    const bool bIsHairline((aLineWidth.getX() == aLineWidth.getY()) && (aLineWidth.getX() <= 1.2));
-
-    // #i101491#
-    if( !bIsHairline && (rPolygon.count() > 1000) )
+    // short circuit if there is nothing to do
+    if(0 == rPolygon.count() || fTransparency < 0.0 || fTransparency >= 1.0)
     {
-        // the used basegfx::utils::createAreaGeometry is simply too
-        // expensive with very big polygons; fallback to caller (who
-        // should use ImplLineConverter normally)
-        // AW: ImplLineConverter had to be removed since it does not even
-        // know LineJoins, so the fallback will now prepare the line geometry
-        // the same way.
-        return false;
+        return true;
     }
 
-    // Transform to DeviceCoordinates, get DeviceLineWidth, execute PixelSnapHairline
-    basegfx::B2DPolygon aPolyLine(rPolygon);
-    aPolyLine.transform(rObjectToDevice);
-    if(bPixelSnapHairline) { aPolyLine = basegfx::utils::snapPointsOfHorizontalOrVerticalEdges(aPolyLine); }
+    // need to check/handle LineWidth when ObjectToDevice transformation is used
+    basegfx::B2DVector aLineWidth(rLineWidth);
+    const bool bObjectToDeviceIsIdentity(rObjectToDevice.isIdentity());
+    const basegfx::B2DVector aDeviceLineWidths(bObjectToDeviceIsIdentity ? rLineWidth : rObjectToDevice * rLineWidth);
+    const bool bCorrectLineWidth(!bObjectToDeviceIsIdentity && aDeviceLineWidths.getX() < 1.0 && aLineWidth.getX() >= 1.0);
+    basegfx::B2DHomMatrix aObjectToDeviceInv;
+    basegfx::B2DPolygon aPolygon(rPolygon);
 
-    // temporarily adjust brush color to pen color
-    // since the line is drawn as an area-polygon
-    const Color aKeepBrushColor = mnBrushColor;
-    mnBrushColor = mnPenColor;
+    if(bCorrectLineWidth)
+    {
+        if(aObjectToDeviceInv.isIdentity())
+        {
+            aObjectToDeviceInv = rObjectToDevice;
+            aObjectToDeviceInv.invert();
+        }
 
-    // #i11575#desc5#b adjust B2D tessellation result to raster positions
-    // basegfx::B2DPolygon aPolygon = rPolygon;
-    const double fHalfWidth = 0.5 * aLineWidth.getX();
+        // calculate-back logical LineWidth for a hairline
+        aLineWidth = aObjectToDeviceInv * basegfx::B2DVector(1.0, 1.0);
+    }
 
-    // #i122456# This is probably thought to happen to align hairlines to pixel positions, so
-    // it should be a 0.5 translation, not more. It will definitely go wrong with fat lines
-    aPolyLine.transform( basegfx::utils::createTranslateB2DHomMatrix(0.5, 0.5) );
+    // try to access buffered data
+    std::shared_ptr<SystemDependentData_Triangulation> pSystemDependentData_Triangulation(
+        rPolygon.getSystemDependentData<SystemDependentData_Triangulation>());
 
-    // shortcut for hairline drawing to improve performance
-    bool bDrawnOk = true;
-    if( bIsHairline )
+    if(pSystemDependentData_Triangulation)
     {
-        // hairlines can benefit from a simplified tessellation
-        // e.g. for hairlines the linejoin style can be ignored
-        basegfx::B2DTrapezoidVector aB2DTrapVector;
-        basegfx::utils::createLineTrapezoidFromB2DPolygon( aB2DTrapVector, aPolyLine, aLineWidth.getX() );
-
-        // draw tessellation result
-        const int nTrapCount = aB2DTrapVector.size();
-        if( nTrapCount > 0 )
-            bDrawnOk = drawFilledTrapezoids( &aB2DTrapVector[0], nTrapCount, fTransparency );
-
-        // restore the original brush GC
-        mnBrushColor = aKeepBrushColor;
-        return bDrawnOk;
+        // check data validity
+        if(pSystemDependentData_Triangulation->getLineWidth() != aLineWidth)
+        {
+            // sometimes small inconsistencies, use a percentage tolerance
+            const double fFactorX(basegfx::fTools::equalZero(aLineWidth.getX())
+                ? 0.0
+                : fabs(1.0 - (pSystemDependentData_Triangulation->getLineWidth().getX() / aLineWidth.getX())));
+            const double fFactorY(basegfx::fTools::equalZero(aLineWidth.getY())
+                ? 0.0
+                : fabs(1.0 - (pSystemDependentData_Triangulation->getLineWidth().getY() / aLineWidth.getY())));
+
+            // compare with 5.0% tolerance
+            if(basegfx::fTools::more(fFactorX, 0.05) || basegfx::fTools::more(fFactorY, 0.05))
+            {
+                // data invalid, forget
+                pSystemDependentData_Triangulation.reset();
+            }
+        }
     }
 
-    // get the area polygon for the line polygon
-    if( (aLineWidth.getX() != aLineWidth.getY()) && !basegfx::fTools::equalZero( aLineWidth.getY() ) )
+    if(!pSystemDependentData_Triangulation)
     {
-        // prepare for createAreaGeometry() with anisotropic linewidth
-        aPolyLine.transform( basegfx::utils::createScaleB2DHomMatrix(1.0, aLineWidth.getX() / aLineWidth.getY()));
-    }
+        // try to create data
+        if(bPixelSnapHairline)
+        {
+            if(!bObjectToDeviceIsIdentity)
+            {
+                aPolygon.transform(rObjectToDevice);
+            }
 
-    // create the area-polygon for the line
-    const basegfx::B2DPolyPolygon aAreaPolyPoly(
-        basegfx::utils::createAreaGeometry(
-            aPolyLine,
-            fHalfWidth,
-            eLineJoin,
-            eLineCap,
-            fMiterMinimumAngle));
+            aPolygon = basegfx::utils::snapPointsOfHorizontalOrVerticalEdges(aPolygon);
 
-    if( (aLineWidth.getX() != aLineWidth.getY()) && !basegfx::fTools::equalZero( aLineWidth.getX() ) )
-    {
-        // postprocess createAreaGeometry() for anisotropic linewidth
-        aPolyLine.transform(basegfx::utils::createScaleB2DHomMatrix(1.0, aLineWidth.getY() / aLineWidth.getX()));
+            if(!bObjectToDeviceIsIdentity)
+            {
+                if(aObjectToDeviceInv.isIdentity())
+                {
+                    aObjectToDeviceInv = rObjectToDevice;
+                    aObjectToDeviceInv.invert();
+                }
+
+                aPolygon.transform(aObjectToDeviceInv);
+            }
+        }
+
+        basegfx::triangulator::B2DTriangleVector aTriangles;
+        const basegfx::B2DPolyPolygon aAreaPolyPoly(
+            basegfx::utils::createAreaGeometry(
+                aPolygon,
+                0.5 * aLineWidth.getX(),
+                eLineJoin,
+                eLineCap,
+                basegfx::deg2rad(12.5),
+                0.4,
+                fMiterMinimumAngle,
+                &aTriangles));
+
+        if(!aTriangles.empty())
+        {
+            // add to buffering mechanism
+            pSystemDependentData_Triangulation = rPolygon.addOrReplaceSystemDependentData<SystemDependentData_Triangulation>(
+                ImplGetSystemDependentDataManager(),
+                aTriangles,
+                aLineWidth);
+        }
     }
 
-    // draw each area polypolygon component individually
-    // to emulate the polypolygon winding rule "non-zero"
-    const int nPolyCount = aAreaPolyPoly.count();
-    for( int nPolyIdx = 0; nPolyIdx < nPolyCount; ++nPolyIdx )
+    if(!pSystemDependentData_Triangulation)
     {
-        const basegfx::B2DPolyPolygon aOnePoly( aAreaPolyPoly.getB2DPolygon( nPolyIdx ) );
+        return false;
+    }
 
-        bDrawnOk = drawPolyPolygon(
-            basegfx::B2DHomMatrix(),
-            aOnePoly,
-            fTransparency);
+    // temporarily adjust brush color to pen color
+    // since the line is drawn as an area-polygon
+    const Color aKeepBrushColor = mnBrushColor;
+    mnBrushColor = mnPenColor;
 
-        if( !bDrawnOk )
-            break;
-    }
+    // create the area-polygon for the line
+    const bool bDrawnOk(
+        drawFilledTriangles(
+            rObjectToDevice,
+            pSystemDependentData_Triangulation->getTriangles(),
+            fTransparency,
+            true));
 
     // restore the original brush GC
     mnBrushColor = aKeepBrushColor;
diff --git a/vcl/unx/generic/gdi/gdiimpl.hxx b/vcl/unx/generic/gdi/gdiimpl.hxx
index ac80ec6f0c40..db13696c4ffc 100644
--- a/vcl/unx/generic/gdi/gdiimpl.hxx
+++ b/vcl/unx/generic/gdi/gdiimpl.hxx
@@ -30,6 +30,7 @@
 #include <salgdiimpl.hxx>
 
 #include <basegfx/polygon/b2dtrapezoid.hxx>
+#include <basegfx/polygon/b2dpolygontriangulator.hxx>
 #include <ControlCacheKey.hxx>
 
 /* From <X11/Intrinsic.h> */
@@ -93,6 +94,11 @@ private:
 
     XID GetXRenderPicture();
     bool drawFilledTrapezoids( const basegfx::B2DTrapezoid*, int nTrapCount, double fTransparency );
+    bool drawFilledTriangles(
+        const basegfx::B2DHomMatrix& rObjectToDevice,
+        const basegfx::triangulator::B2DTriangleVector& rTriangles,
+        double fTransparency,
+        bool bPixelOffset);
 
     long GetGraphicsHeight() const;
 
diff --git a/vcl/unx/generic/gdi/salgdi.cxx b/vcl/unx/generic/gdi/salgdi.cxx
index 632d08d1a35b..fd82c3929c88 100644
--- a/vcl/unx/generic/gdi/salgdi.cxx
+++ b/vcl/unx/generic/gdi/salgdi.cxx
@@ -604,9 +604,10 @@ bool X11SalGraphics::drawPolyPolygon(
         return true;
     }
 
-    static bool bUseCairoForPolygons = false;
+    // enable by setting to something
+    static const char* pUseCairoForPolygons(getenv("SAL_ENABLE_USE_CAIRO_FOR_POLYGONS"));
 
-    if (!m_bOpenGL && bUseCairoForPolygons && SupportsCairo())
+    if (!m_bOpenGL && nullptr != pUseCairoForPolygons && SupportsCairo())
     {
         // snap to raster if requested
         const bool bSnapPoints(!getAntiAliasB2DDraw());
@@ -724,9 +725,10 @@ bool X11SalGraphics::drawPolyLine(
     }
 
 #if ENABLE_CAIRO_CANVAS
-    static bool bUseCairoForFatLines = true;
+    // disable by setting to something
+    static const char* pUseCairoForFatLines(getenv("SAL_DISABLE_USE_CAIRO_FOR_FATLINES"));
 
-    if (!m_bOpenGL && bUseCairoForFatLines && SupportsCairo())
+    if (!m_bOpenGL && nullptr == pUseCairoForFatLines && SupportsCairo())
     {
         cairo_t* cr = getCairoContext();
         clipRegion(cr);
diff --git a/vcl/unx/generic/gdi/xrender_peer.hxx b/vcl/unx/generic/gdi/xrender_peer.hxx
index 492924889595..afe793b248da 100644
--- a/vcl/unx/generic/gdi/xrender_peer.hxx
+++ b/vcl/unx/generic/gdi/xrender_peer.hxx
@@ -62,6 +62,9 @@ public:
     void        CompositeTrapezoids( int nOp, Picture aSrc, Picture aDst,
                     const XRenderPictFormat*, int nXSrc, int nYSrc,
                     const XTrapezoid*, int nCount ) const;
+    void        CompositeTriangles( int nOp, Picture aSrc, Picture aDst,
+                    const XRenderPictFormat*, int nXSrc, int nYSrc,
+                    const XTriangle*, int nCount ) const;
 };
 
 inline XRenderPictFormat* XRenderPeer::GetStandardFormatA8() const
@@ -127,6 +130,14 @@ inline void XRenderPeer::CompositeTrapezoids( int nOp,
         nXSrc, nYSrc, pXT, nCount );
 }
 
+inline void XRenderPeer::CompositeTriangles( int nOp,
+    Picture aSrc, Picture aDst, const XRenderPictFormat* pXRPF,
+    int nXSrc, int nYSrc, const XTriangle* pXT, int nCount ) const
+{
+    XRenderCompositeTriangles( mpDisplay, nOp, aSrc, aDst, pXRPF,
+        nXSrc, nYSrc, pXT, nCount );
+}
+
 inline XRenderColor GetXRenderColor( Color rColor, double fTransparency )
 {
     XRenderColor aRetVal;


More information about the Libreoffice-commits mailing list