LibreOffice Module canvas (master) 1
cairo_spritehelper.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21#include <sal/log.hxx>
22
27#include <rtl/math.hxx>
29
30#include <cairo.h>
31#include <pixman.h>
32
34
35using namespace ::cairo;
36using namespace ::com::sun::star;
37
38namespace cairocanvas
39{
41 mbTextureDirty(true)
42 {}
43
44 void SpriteHelper::init( const geometry::RealSize2D& rSpriteSize,
45 const SpriteCanvasRef& rSpriteCanvas )
46 {
47 ENSURE_OR_THROW( rSpriteCanvas,
48 "SpriteHelper::init(): Invalid device, sprite canvas or surface" );
49
50 mpSpriteCanvas = rSpriteCanvas;
51 mbTextureDirty = true;
52
53 // also init base class
54 CanvasCustomSpriteHelper::init( rSpriteSize, rSpriteCanvas );
55 }
56
57 void SpriteHelper::setSurface( const SurfaceSharedPtr& pBufferSurface )
58 {
59 mpBufferSurface = pBufferSurface;
60 }
61
63 {
64 mpBufferSurface.reset();
65 mpSpriteCanvas.clear();
66
67 // forward to parent
68 CanvasCustomSpriteHelper::disposing();
69 }
70
71 void SpriteHelper::redraw( const CairoSharedPtr& pCairo,
72 const ::basegfx::B2DPoint& rPos,
73 bool& /*io_bSurfacesDirty*/,
74 bool /*bBufferedUpdate*/ ) const
75 {
76#ifdef CAIRO_CANVAS_PERF_TRACE
77 struct timespec aTimer;
78 mxDevice->startPerfTrace( &aTimer );
79#endif
80
81 const double fAlpha( getAlpha() );
82 const ::basegfx::B2DHomMatrix aTransform( getTransformation() );
83
84 if( !isActive() || ::basegfx::fTools::equalZero( fAlpha ) )
85 return;
86
87 SAL_INFO( "canvas.cairo", "CanvasCustomSprite::redraw called");
88 if( !pCairo )
89 return;
90
92 cairo_save( pCairo.get() );
93
94 double fX, fY;
95
96 fX = rPos.getX();
97 fY = rPos.getY();
98
99 if( !aTransform.isIdentity() )
100 {
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 ) );
105
106 aMatrix.x0 = basegfx::fround( aMatrix.x0 );
107 aMatrix.y0 = basegfx::fround( aMatrix.y0 );
108
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 );
112
113 cairo_set_matrix( pCairo.get(), &aMatrix );
114 }
115
116 fX = basegfx::fround( fX );
117 fY = basegfx::fround( fY );
118
119 cairo_matrix_t aOrigMatrix;
120 cairo_get_matrix( pCairo.get(), &aOrigMatrix );
121 cairo_translate( pCairo.get(), fX, fY );
122
123 if( getClip().is() )
124 {
125 const uno::Reference<rendering::XPolyPolygon2D>& rClip( getClip() );
126
128 ::basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(
129 rClip ));
130
131 doPolyPolygonImplementation( aClipPoly, Clip, pCairo.get(),
133 rClip->getFillRule() );
134 }
135
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 );
140
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)
153 {
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
162 }
163 else
164 matrixProblem = true;
165 if(matrixProblem)
166 {
167 SAL_WARN( "canvas.cairo", "matrix would overflow PIXMAN_MAX_INT, avoiding drawing" );
168 cairo_restore( pCairo.get() );
169 return;
170 }
171
173 cairo_set_operator( pCairo.get(), CAIRO_OPERATOR_SOURCE );
174 cairo_set_source_surface( pCairo.get(),
175 mpBufferSurface->getCairoSurface().get(),
176 fX, fY );
177 if( ::rtl::math::approxEqual( fAlpha, 1.0 ) )
178 cairo_paint( pCairo.get() );
179 else
180 cairo_paint_with_alpha( pCairo.get(), fAlpha );
181
182 cairo_restore( pCairo.get() );
183
184#ifdef CAIRO_CANVAS_PERF_TRACE
185 mxDevice->stopPerfTrace( &aTimer, "sprite redraw" );
186#endif
187 }
188
189 ::basegfx::B2DPolyPolygon SpriteHelper::polyPolygonFromXPolyPolygon2D( uno::Reference< rendering::XPolyPolygon2D >& xPoly ) const
190 {
191 return ::basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(xPoly);
192 }
193}
194
195/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define PIXMAN_MAX_INT
TYPE getX() const
TYPE getY() const
void init(const css::geometry::RealSize2D &rSpriteSize, const SpriteCanvasRef &rSpriteCanvas)
Late-init the sprite helper.
void setSurface(const ::cairo::SurfaceSharedPtr &pBufferSurface)
virtual ::basegfx::B2DPolyPolygon polyPolygonFromXPolyPolygon2D(css::uno::Reference< css::rendering::XPolyPolygon2D > &xPoly) const override
Called to convert an API polygon to a basegfx polygon.
void redraw(const ::cairo::CairoSharedPtr &pCairo, const ::basegfx::B2DPoint &rPos, bool &bSurfacesDirty, bool bBufferedUpdate) const
Repaint sprite content to associated sprite canvas.
::cairo::SurfaceSharedPtr mpBufferSurface
SpriteHelper()
Create sprite helper.
bool isContentFullyOpaque() const
Returns true, if sprite content bitmap is fully opaque.
const css::uno::Reference< css::rendering::XPolyPolygon2D > & getClip() const
Retrieve current clip.
bool isActive() const
Retrieve current activation state.
const ::basegfx::B2DHomMatrix & getTransformation() const
double getAlpha() const
Retrieve current alpha value.
const ::basegfx::B2DVector & getSizePixel() const
#define ENSURE_OR_THROW(c, m)
#define SAL_WARN(area, stream)
#define SAL_INFO(area, stream)
B2IRange fround(const B2DRange &rRange)
std::shared_ptr< Surface > SurfaceSharedPtr
std::shared_ptr< cairo_t > CairoSharedPtr
void doPolyPolygonImplementation(const ::basegfx::B2DPolyPolygon &aPolyPolygon, Operation aOperation, cairo_t *pCairo, const uno::Sequence< rendering::Texture > *pTextures, const SurfaceProviderRef &pDevice, rendering::FillRule eFillrule)
class SAL_LOPLUGIN_ANNOTATE("crosscast") SurfaceProvider typedef ::rtl::Reference< SurfaceProvider > SurfaceProviderRef
Target interface for XCachedPrimitive implementations.
SwNodeOffset abs(const SwNodeOffset &a)