1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
21 #include <sal/log.hxx>
23 #include <basegfx/matrix/b2dhommatrix.hxx>
24 #include <basegfx/numeric/ftools.hxx>
25 #include <basegfx/point/b2dpoint.hxx>
26 #include <basegfx/utils/canvastools.hxx>
27 #include <rtl/math.hxx>
28 #include <comphelper/diagnose_ex.hxx>
33 #include "cairo_spritehelper.hxx"
35 using namespace ::cairo
;
36 using namespace ::com::sun::star
;
40 SpriteHelper::SpriteHelper() :
44 void SpriteHelper::init( const geometry::RealSize2D
& rSpriteSize
,
45 const SpriteCanvasRef
& rSpriteCanvas
)
47 ENSURE_OR_THROW( rSpriteCanvas
,
48 "SpriteHelper::init(): Invalid device, sprite canvas or surface" );
50 mpSpriteCanvas
= rSpriteCanvas
;
51 mbTextureDirty
= true;
53 // also init base class
54 CanvasCustomSpriteHelper::init( rSpriteSize
, rSpriteCanvas
);
57 void SpriteHelper::setSurface( const SurfaceSharedPtr
& pBufferSurface
)
59 mpBufferSurface
= pBufferSurface
;
62 void SpriteHelper::disposing()
64 mpBufferSurface
.reset();
65 mpSpriteCanvas
.clear();
68 CanvasCustomSpriteHelper::disposing();
71 void SpriteHelper::redraw( const CairoSharedPtr
& pCairo
,
72 const ::basegfx::B2DPoint
& rPos
,
73 bool& /*io_bSurfacesDirty*/,
74 bool /*bBufferedUpdate*/ ) const
76 #ifdef CAIRO_CANVAS_PERF_TRACE
77 struct timespec aTimer
;
78 mxDevice
->startPerfTrace( &aTimer
);
81 const double fAlpha( getAlpha() );
82 const ::basegfx::B2DHomMatrix
aTransform( getTransformation() );
84 if( !isActive() || ::basegfx::fTools::equalZero( fAlpha
) )
87 SAL_INFO( "canvas.cairo", "CanvasCustomSprite::redraw called");
91 basegfx::B2DVector aSize
= getSizePixel();
92 cairo_save( pCairo
.get() );
99 if( !aTransform
.isIdentity() )
101 cairo_matrix_t aMatrix
, aInverseMatrix
;
102 cairo_matrix_init( &aMatrix
,
103 aTransform
.get( 0, 0 ), aTransform
.get( 1, 0 ), aTransform
.get( 0, 1 ),
104 aTransform
.get( 1, 1 ), aTransform
.get( 0, 2 ), aTransform
.get( 1, 2 ) );
106 aMatrix
.x0
= basegfx::fround( aMatrix
.x0
);
107 aMatrix
.y0
= basegfx::fround( aMatrix
.y0
);
109 cairo_matrix_init( &aInverseMatrix
, aMatrix
.xx
, aMatrix
.yx
, aMatrix
.xy
, aMatrix
.yy
, aMatrix
.x0
, aMatrix
.y0
);
110 cairo_matrix_invert( &aInverseMatrix
);
111 cairo_matrix_transform_distance( &aInverseMatrix
, &fX
, &fY
);
113 cairo_set_matrix( pCairo
.get(), &aMatrix
);
116 fX
= basegfx::fround( fX
);
117 fY
= basegfx::fround( fY
);
119 cairo_matrix_t aOrigMatrix
;
120 cairo_get_matrix( pCairo
.get(), &aOrigMatrix
);
121 cairo_translate( pCairo
.get(), fX
, fY
);
125 const uno::Reference
<rendering::XPolyPolygon2D
>& rClip( getClip() );
127 ::basegfx::B2DPolyPolygon
aClipPoly(
128 ::basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(
131 doPolyPolygonImplementation( aClipPoly
, Clip
, pCairo
.get(),
132 nullptr, SurfaceProviderRef(mpSpriteCanvas
),
133 rClip
->getFillRule() );
136 SAL_INFO( "canvas.cairo","aSize " << aSize
.getX() << " x " << aSize
.getY() << " position: " << fX
<< "," << fY
);
137 cairo_rectangle( pCairo
.get(), 0, 0, floor( aSize
.getX() ), floor( aSize
.getY() ) );
138 cairo_clip( pCairo
.get() );
139 cairo_set_matrix( pCairo
.get(), &aOrigMatrix
);
141 cairo_matrix_t aInverseMatrix
= aOrigMatrix
;
142 bool matrixProblem
= false;
143 // tdf#125949: Cairo internally uses the pixman library, and _cairo_matrix_to_pixman_matrix()
144 // checks all matrix components to fix PIXMAN_MAX_INT, which is about 32k. Which means that
145 // if our transformation is large, such as an initial step of some zooming animations,
146 // the conversion will fail. To make things worse, once something in cairo fails, it's treated
147 // as a fatal error, the error status of that cairo_t gets set, and there's no way to reset it
148 // besides recreating the whole cairo_t
149 // (https://lists.cairographics.org/archives/cairo/2006-September/007892.html).
150 // So copy&paste PIXMAN_MAX_INT here, and if our matrix could fail, bail out.
151 #define PIXMAN_MAX_INT ((pixman_fixed_1 >> 1) - pixman_fixed_e) /* need to ensure deltas also fit */
152 if(cairo_matrix_invert(&aInverseMatrix
) == CAIRO_STATUS_SUCCESS
)
154 if(abs(aOrigMatrix
.xx
) > PIXMAN_MAX_INT
|| abs(aOrigMatrix
.xx
) > PIXMAN_MAX_INT
155 || abs(aOrigMatrix
.xy
) > PIXMAN_MAX_INT
|| abs(aOrigMatrix
.yx
) > PIXMAN_MAX_INT
156 || abs(aOrigMatrix
.x0
) > PIXMAN_MAX_INT
|| abs(aOrigMatrix
.y0
) > PIXMAN_MAX_INT
157 || abs(aInverseMatrix
.xx
) > PIXMAN_MAX_INT
|| abs(aInverseMatrix
.xx
) > PIXMAN_MAX_INT
158 || abs(aInverseMatrix
.xy
) > PIXMAN_MAX_INT
|| abs(aInverseMatrix
.yx
) > PIXMAN_MAX_INT
159 || abs(aInverseMatrix
.x0
) > PIXMAN_MAX_INT
|| abs(aInverseMatrix
.y0
) > PIXMAN_MAX_INT
)
160 matrixProblem
= true;
161 #undef PIXMAN_MAX_INT
164 matrixProblem
= true;
167 SAL_WARN( "canvas.cairo", "matrix would overflow PIXMAN_MAX_INT, avoiding drawing" );
168 cairo_restore( pCairo
.get() );
172 if( isContentFullyOpaque() )
173 cairo_set_operator( pCairo
.get(), CAIRO_OPERATOR_SOURCE
);
174 cairo_set_source_surface( pCairo
.get(),
175 mpBufferSurface
->getCairoSurface().get(),
177 if( ::rtl::math::approxEqual( fAlpha
, 1.0 ) )
178 cairo_paint( pCairo
.get() );
180 cairo_paint_with_alpha( pCairo
.get(), fAlpha
);
182 cairo_restore( pCairo
.get() );
184 #ifdef CAIRO_CANVAS_PERF_TRACE
185 mxDevice
->stopPerfTrace( &aTimer
, "sprite redraw" );
189 ::basegfx::B2DPolyPolygon
SpriteHelper::polyPolygonFromXPolyPolygon2D( uno::Reference
< rendering::XPolyPolygon2D
>& xPoly
) const
191 return ::basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(xPoly
);
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */