Branch libreoffice-5-0-4
[LibreOffice.git] / canvas / source / cairo / cairo_spritehelper.cxx
blobbb06b55a197460a160cbf27141cdae346f98f38b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <canvas/debug.hxx>
21 #include <canvas/verbosetrace.hxx>
22 #include <tools/diagnose_ex.h>
24 #include <rtl/math.hxx>
26 #include <canvas/canvastools.hxx>
28 #include <basegfx/matrix/b2dhommatrix.hxx>
29 #include <basegfx/point/b2dpoint.hxx>
30 #include <basegfx/tools/canvastools.hxx>
31 #include <basegfx/numeric/ftools.hxx>
32 #include <basegfx/polygon/b2dpolypolygontools.hxx>
33 #include <basegfx/polygon/b2dpolygontools.hxx>
34 #include <basegfx/polygon/b2dpolygontriangulator.hxx>
35 #include <basegfx/polygon/b2dpolygoncutandtouch.hxx>
37 #include "cairo_canvascustomsprite.hxx"
38 #include "cairo_spritehelper.hxx"
40 using namespace ::cairo;
41 using namespace ::com::sun::star;
43 namespace cairocanvas
45 SpriteHelper::SpriteHelper() :
46 mpSpriteCanvas(),
47 mpBufferSurface(),
48 mbTextureDirty(true)
51 void SpriteHelper::init( const geometry::RealSize2D& rSpriteSize,
52 const SpriteCanvasRef& rSpriteCanvas )
54 ENSURE_OR_THROW( rSpriteCanvas.get(),
55 "SpriteHelper::init(): Invalid device, sprite canvas or surface" );
57 mpSpriteCanvas = rSpriteCanvas;
58 mbTextureDirty = true;
60 // also init base class
61 CanvasCustomSpriteHelper::init( rSpriteSize,
62 rSpriteCanvas.get() );
65 void SpriteHelper::setSurface( const SurfaceSharedPtr& pBufferSurface )
67 mpBufferSurface = pBufferSurface;
70 void SpriteHelper::disposing()
72 mpBufferSurface.reset();
73 mpSpriteCanvas.clear();
75 // forward to parent
76 CanvasCustomSpriteHelper::disposing();
79 void SpriteHelper::redraw( const CairoSharedPtr& pCairo,
80 const ::basegfx::B2DPoint& rPos,
81 bool& /*io_bSurfacesDirty*/,
82 bool /*bBufferedUpdate*/ ) const
84 #ifdef CAIRO_CANVAS_PERF_TRACE
85 struct timespec aTimer;
86 mxDevice->startPerfTrace( &aTimer );
87 #endif
89 const double fAlpha( getAlpha() );
90 const ::basegfx::B2DHomMatrix aTransform( getTransformation() );
92 if( isActive() && !::basegfx::fTools::equalZero( fAlpha ) )
94 SAL_INFO( "canvas.cairo", "CanvasCustomSprite::redraw called");
95 if( pCairo )
97 basegfx::B2DVector aSize = getSizePixel();
98 cairo_save( pCairo.get() );
100 double fX, fY;
102 fX = rPos.getX();
103 fY = rPos.getY();
105 if( !aTransform.isIdentity() )
107 cairo_matrix_t aMatrix, aInverseMatrix;
108 cairo_matrix_init( &aMatrix,
109 aTransform.get( 0, 0 ), aTransform.get( 1, 0 ), aTransform.get( 0, 1 ),
110 aTransform.get( 1, 1 ), aTransform.get( 0, 2 ), aTransform.get( 1, 2 ) );
112 aMatrix.x0 = basegfx::fround( aMatrix.x0 );
113 aMatrix.y0 = basegfx::fround( aMatrix.y0 );
115 cairo_matrix_init( &aInverseMatrix, aMatrix.xx, aMatrix.yx, aMatrix.xy, aMatrix.yy, aMatrix.x0, aMatrix.y0 );
116 cairo_matrix_invert( &aInverseMatrix );
117 cairo_matrix_transform_distance( &aInverseMatrix, &fX, &fY );
119 cairo_set_matrix( pCairo.get(), &aMatrix );
122 fX = basegfx::fround( fX );
123 fY = basegfx::fround( fY );
125 cairo_matrix_t aOrigMatrix;
126 cairo_get_matrix( pCairo.get(), &aOrigMatrix );
127 cairo_translate( pCairo.get(), fX, fY );
129 if( getClip().is() )
131 const uno::Reference<rendering::XPolyPolygon2D>& rClip( getClip() );
133 ::basegfx::B2DPolyPolygon aClipPoly(
134 ::basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(
135 rClip ));
137 doPolyPolygonImplementation( aClipPoly, Clip, pCairo.get(),
138 NULL, SurfaceProviderRef(mpSpriteCanvas.get()),
139 rClip->getFillRule() );
142 SAL_INFO( "canvas.cairo","aSize " << aSize.getX() << " x " << aSize.getY() << " position: " << fX << "," << fY );
143 cairo_rectangle( pCairo.get(), 0, 0, floor( aSize.getX() ), floor( aSize.getY() ) );
144 cairo_clip( pCairo.get() );
145 cairo_set_matrix( pCairo.get(), &aOrigMatrix );
147 if( isContentFullyOpaque() )
148 cairo_set_operator( pCairo.get(), CAIRO_OPERATOR_SOURCE );
149 cairo_set_source_surface( pCairo.get(),
150 mpBufferSurface->getCairoSurface().get(),
151 fX, fY );
152 if( ::rtl::math::approxEqual( fAlpha, 1.0 ) )
153 cairo_paint( pCairo.get() );
154 else
155 cairo_paint_with_alpha( pCairo.get(), fAlpha );
157 cairo_restore( pCairo.get() );
161 #ifdef CAIRO_CANVAS_PERF_TRACE
162 mxDevice->stopPerfTrace( &aTimer, "sprite redraw" );
163 #endif
166 ::basegfx::B2DPolyPolygon SpriteHelper::polyPolygonFromXPolyPolygon2D( uno::Reference< rendering::XPolyPolygon2D >& xPoly ) const
168 return ::basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(xPoly);
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */