basegfx/source/polygon/b2dlinegeometry.cxx | 114 +++++++- basegfx/source/polygon/b2dpolygontools.cxx | 11 basegfx/source/polygon/b2dpolygontriangulator.cxx | 27 +- canvas/source/opengl/ogl_canvascustomsprite.cxx | 39 +-- 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 | 271 +++++++++++++++------ vcl/unx/generic/gdi/gdiimpl.hxx | 6 vcl/unx/generic/gdi/xrender_peer.hxx | 11 12 files changed, 443 insertions(+), 133 deletions(-)
New commits: commit 98fcc65a173d2f9ea2c2bba2c4670aaa7699c50e Author: Armin Le Grand <armin.le.gr...@cib.de> AuthorDate: Sat Sep 15 13:48:12 2018 +0200 Commit: Thorsten Behrens <thorsten.behr...@cib.de> CommitDate: Fri Sep 21 13:50:17 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.gr...@cib.de> Reviewed-on: https://gerrit.libreoffice.org/60857 Reviewed-by: Thorsten Behrens <thorsten.behr...@cib.de> Tested-by: Thorsten Behrens <thorsten.behr...@cib.de> diff --git a/basegfx/source/polygon/b2dlinegeometry.cxx b/basegfx/source/polygon/b2dlinegeometry.cxx index 799604f37014..cb9efd10b31e 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(B2DLineJoin::NONE != eJoin, "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(!rtl::math::approxEqual(0.0, fCutPos)) { 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(B2VectorOrientation::Negative == aOrientation) { @@ -911,7 +978,8 @@ namespace basegfx aEdge.getStartPoint(), fHalfLineWidth, eJoin, - fMiterMinimumAngle)); + fMiterMinimumAngle, + pTriangles)); } } @@ -929,7 +997,8 @@ namespace basegfx bFirst && css::drawing::LineCap_ROUND == eCap, bLast && css::drawing::LineCap_ROUND == eCap, bFirst && css::drawing::LineCap_SQUARE == eCap, - bLast && css::drawing::LineCap_SQUARE == eCap)); + bLast && css::drawing::LineCap_SQUARE == eCap, + pTriangles)); } else { @@ -940,7 +1009,8 @@ namespace basegfx false, false, false, - false)); + false, + pTriangles)); } // prepare next step diff --git a/basegfx/source/polygon/b2dpolygontools.cxx b/basegfx/source/polygon/b2dpolygontools.cxx index 4ec182ec939b..7325e3faba3a 100644 --- a/basegfx/source/polygon/b2dpolygontools.cxx +++ b/basegfx/source/polygon/b2dpolygontools.cxx @@ -2208,7 +2208,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()); @@ -2220,9 +2222,10 @@ namespace basegfx for(sal_uInt32 a(2L); 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 8d1948edaf9a..09cc31a213ec 100644 --- a/basegfx/source/polygon/b2dpolygontriangulator.cxx +++ b/basegfx/source/polygon/b2dpolygontriangulator.cxx @@ -116,7 +116,7 @@ namespace basegfx EdgeEntry* mpList; EdgeEntries maStartEntries; EdgeEntryPointers maNewEdgeEntries; - B2DPolygon maResult; + triangulator::B2DTriangleVector maResult; void handleClosingEdge(const B2DPoint& rStart, const B2DPoint& rEnd); bool CheckPointInTriangle(EdgeEntry* pEdgeA, EdgeEntry* pEdgeB, const B2DPoint& rTestPoint); @@ -126,7 +126,7 @@ namespace basegfx explicit Triangulator(const B2DPolyPolygon& rCandidate); ~Triangulator(); - const B2DPolygon& getResult() const { return maResult; } + const triangulator::B2DTriangleVector& getResult() const { return maResult; } }; void Triangulator::handleClosingEdge(const B2DPoint& rStart, const B2DPoint& rEnd) @@ -210,9 +210,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 @@ -387,9 +388,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() ? tools::adaptiveSubdivideByAngle(rCandidate) : rCandidate); @@ -399,7 +400,10 @@ namespace basegfx if(2L == aCandidate.count()) { // 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() > 2L) { @@ -413,6 +417,7 @@ namespace basegfx // polygon is concave. const B2DPolyPolygon aCandPolyPoly(aCandidate); Triangulator aTriangulator(aCandPolyPoly); + aRetval = aTriangulator.getResult(); } } @@ -420,9 +425,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() ? tools::adaptiveSubdivideByAngle(rCandidate) : rCandidate); @@ -431,11 +436,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 8d38177be946..adcd53a195a3 100644 --- a/canvas/source/opengl/ogl_canvascustomsprite.cxx +++ b/canvas/source/opengl/ogl_canvascustomsprite.cxx @@ -194,26 +194,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))); - basegfx::B2DPolygon rTriangleList( - basegfx::tools::clipTriangleListOnRange( - rTriangulatedPolygon, - basegfx::B2DRange( - 0,0, - aSpriteSizePixel.getX(), - aSpriteSizePixel.getY()))); - 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 657a22027ba7..52719f4a1e8f 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 a2ffd3204a42..78f38cdd3a2a 100644 --- a/canvas/source/tools/surfaceproxy.cxx +++ b/canvas/source/tools/surfaceproxy.cxx @@ -110,15 +110,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", "Orignal clip polygon: " << basegfx::tools::exportToSvgD( rClipPoly, true, true, false )); - SAL_INFO("canvas", "Triangulated polygon: " << basegfx::tools::exportToSvgD(basegfx::B2DPolyPolygon(rTriangulatedPolygon), true, true, false )); + SAL_INFO("canvas", "Triangulated polygon: " << basegfx::tools::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 5aefafbe679b..106096dcd8ed 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 = css::drawing::LineCap_BUTT, double fMaxAllowedAngle = (12.5 * F_PI180), double fMaxPartOfEdge = 0.4, - double fMiterMinimumAngle = (15.0 * F_PI180)); + double fMiterMinimumAngle = (15.0 * F_PI180), + basegfx::triangulator::B2DTriangleVector* pTriangles = nullptr); } // end of namespace tools } // end of namespace basegfx diff --git a/include/basegfx/polygon/b2dpolygontools.hxx b/include/basegfx/polygon/b2dpolygontools.hxx index 423cf8f30ef4..36b837525dc1 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> @@ -359,7 +360,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 0d12d15f3a8e..45c2e2384d39 100644 --- a/vcl/unx/generic/gdi/gdiimpl.cxx +++ b/vcl/unx/generic/gdi/gdiimpl.cxx @@ -49,6 +49,7 @@ #include "basegfx/matrix/b2dhommatrixtools.hxx" #include "basegfx/polygon/b2dpolypolygoncutter.hxx" #include "basegfx/polygon/b2dtrapezoid.hxx" +#include <basegfx/tools/systemdependentdata.hxx> #undef SALGDI2_TESTTRANS @@ -1592,6 +1593,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::tools::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, @@ -1602,92 +1707,122 @@ 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::tools::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::tools::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 SalColor 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::tools::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 tesselation - // e.g. for hairlines the linejoin style can be ignored - basegfx::B2DTrapezoidVector aB2DTrapVector; - basegfx::tools::createLineTrapezoidFromB2DPolygon( aB2DTrapVector, aPolyLine, aLineWidth.getX() ); - - // draw tesselation 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::tools::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::tools::createAreaGeometry( - aPolyLine, - fHalfWidth, - eLineJoin, - eLineCap, - fMiterMinimumAngle)); + aPolygon = basegfx::tools::snapPointsOfHorizontalOrVerticalEdges(aPolygon); - if( (aLineWidth.getX() != aLineWidth.getY()) && !basegfx::fTools::equalZero( aLineWidth.getX() ) ) - { - // postprocess createAreaGeometry() for anisotropic linewidth - aPolyLine.transform(basegfx::tools::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::tools::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>( + SalGraphics::getSystemDependentDataManager(), + 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 ) ); - bDrawnOk = drawPolyPolygon( aOnePoly, fTransparency ); - if( !bDrawnOk ) - break; + return false; } + // temporarily adjust brush color to pen color + // since the line is drawn as an area-polygon + const SalColor aKeepBrushColor = mnBrushColor; + mnBrushColor = mnPenColor; + + // create the area-polygon for the line + const bool bDrawnOk( + drawFilledTriangles( + rObjectToDevice, + pSystemDependentData_Triangulation->getTriangles(), + fTransparency, + true)); + // restore the original brush GC mnBrushColor = aKeepBrushColor; return bDrawnOk; diff --git a/vcl/unx/generic/gdi/gdiimpl.hxx b/vcl/unx/generic/gdi/gdiimpl.hxx index c8d0d79596fe..c5f2fb049d29 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> /* From <X11/Intrinsic.h> */ typedef unsigned long Pixel; @@ -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/xrender_peer.hxx b/vcl/unx/generic/gdi/xrender_peer.hxx index 1053bfd81b48..8716e2b6fd5f 100644 --- a/vcl/unx/generic/gdi/xrender_peer.hxx +++ b/vcl/unx/generic/gdi/xrender_peer.hxx @@ -64,6 +64,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 @@ -135,6 +138,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( const SalColor& rSalColor, double fTransparency = 0.0 ) { XRenderColor aRetVal; _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits