fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / canvas / source / cairo / cairo_spritehelper.cxx
blob7c86da13f5050bd364ca21c9bd3508708fdf2f3e
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/logfile.hxx>
25 #include <rtl/math.hxx>
27 #include <canvas/canvastools.hxx>
29 #include <basegfx/matrix/b2dhommatrix.hxx>
30 #include <basegfx/point/b2dpoint.hxx>
31 #include <basegfx/tools/canvastools.hxx>
32 #include <basegfx/numeric/ftools.hxx>
33 #include <basegfx/polygon/b2dpolypolygontools.hxx>
34 #include <basegfx/polygon/b2dpolygontools.hxx>
35 #include <basegfx/polygon/b2dpolygontriangulator.hxx>
36 #include <basegfx/polygon/b2dpolygoncutandtouch.hxx>
38 #include "cairo_canvascustomsprite.hxx"
39 #include "cairo_spritehelper.hxx"
41 #include <memory>
44 using namespace ::cairo;
45 using namespace ::com::sun::star;
47 namespace cairocanvas
49 SpriteHelper::SpriteHelper() :
50 mpSpriteCanvas(),
51 mpBufferSurface(),
52 mbTextureDirty(true)
55 void SpriteHelper::init( const geometry::RealSize2D& rSpriteSize,
56 const SpriteCanvasRef& rSpriteCanvas )
58 ENSURE_OR_THROW( rSpriteCanvas.get(),
59 "SpriteHelper::init(): Invalid device, sprite canvas or surface" );
61 mpSpriteCanvas = rSpriteCanvas;
62 mbTextureDirty = true;
64 // also init base class
65 CanvasCustomSpriteHelper::init( rSpriteSize,
66 rSpriteCanvas.get() );
69 void SpriteHelper::setSurface( const SurfaceSharedPtr& pBufferSurface )
71 mpBufferSurface = pBufferSurface;
74 void SpriteHelper::disposing()
76 mpBufferSurface.reset();
77 mpSpriteCanvas.clear();
79 // forward to parent
80 CanvasCustomSpriteHelper::disposing();
83 void SpriteHelper::redraw( const CairoSharedPtr& pCairo,
84 const ::basegfx::B2DPoint& rPos,
85 bool& /*io_bSurfacesDirty*/,
86 bool /*bBufferedUpdate*/ ) const
88 #ifdef CAIRO_CANVAS_PERF_TRACE
89 struct timespec aTimer;
90 mxDevice->startPerfTrace( &aTimer );
91 #endif
93 const double fAlpha( getAlpha() );
94 const ::basegfx::B2DHomMatrix aTransform( getTransformation() );
96 if( isActive() && !::basegfx::fTools::equalZero( fAlpha ) )
98 OSL_TRACE ("CanvasCustomSprite::redraw called");
99 if( pCairo )
101 basegfx::B2DVector aSize = getSizePixel();
102 cairo_save( pCairo.get() );
104 double fX, fY;
106 fX = rPos.getX();
107 fY = rPos.getY();
109 if( !aTransform.isIdentity() )
111 cairo_matrix_t aMatrix, aInverseMatrix;
112 cairo_matrix_init( &aMatrix,
113 aTransform.get( 0, 0 ), aTransform.get( 1, 0 ), aTransform.get( 0, 1 ),
114 aTransform.get( 1, 1 ), aTransform.get( 0, 2 ), aTransform.get( 1, 2 ) );
116 aMatrix.x0 = basegfx::fround( aMatrix.x0 );
117 aMatrix.y0 = basegfx::fround( aMatrix.y0 );
119 cairo_matrix_init( &aInverseMatrix, aMatrix.xx, aMatrix.yx, aMatrix.xy, aMatrix.yy, aMatrix.x0, aMatrix.y0 );
120 cairo_matrix_invert( &aInverseMatrix );
121 cairo_matrix_transform_distance( &aInverseMatrix, &fX, &fY );
123 cairo_set_matrix( pCairo.get(), &aMatrix );
126 fX = basegfx::fround( fX );
127 fY = basegfx::fround( fY );
129 cairo_matrix_t aOrigMatrix;
130 cairo_get_matrix( pCairo.get(), &aOrigMatrix );
131 cairo_translate( pCairo.get(), fX, fY );
133 if( getClip().is() )
135 const uno::Reference<rendering::XPolyPolygon2D>& rClip( getClip() );
137 ::basegfx::B2DPolyPolygon aClipPoly(
138 ::basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(
139 rClip ));
141 doPolyPolygonImplementation( aClipPoly, Clip, pCairo.get(),
142 NULL, SurfaceProviderRef(mpSpriteCanvas.get()),
143 rClip->getFillRule() );
146 OSL_TRACE ("aSize %f x %f position: %f,%f", aSize.getX(), aSize.getY(), fX, fY );
147 cairo_rectangle( pCairo.get(), 0, 0, floor( aSize.getX() ), floor( aSize.getY() ) );
148 cairo_clip( pCairo.get() );
149 cairo_set_matrix( pCairo.get(), &aOrigMatrix );
151 if( isContentFullyOpaque() )
152 cairo_set_operator( pCairo.get(), CAIRO_OPERATOR_SOURCE );
153 cairo_set_source_surface( pCairo.get(),
154 mpBufferSurface->getCairoSurface().get(),
155 fX, fY );
156 if( ::rtl::math::approxEqual( fAlpha, 1.0 ) )
157 cairo_paint( pCairo.get() );
158 else
159 cairo_paint_with_alpha( pCairo.get(), fAlpha );
161 cairo_restore( pCairo.get() );
165 #ifdef CAIRO_CANVAS_PERF_TRACE
166 mxDevice->stopPerfTrace( &aTimer, "sprite redraw" );
167 #endif
170 ::basegfx::B2DPolyPolygon SpriteHelper::polyPolygonFromXPolyPolygon2D( uno::Reference< rendering::XPolyPolygon2D >& xPoly ) const
172 return ::basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(xPoly);
176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */