Line data Source code
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 <canvas/debug.hxx>
21 : #include <canvas/verbosetrace.hxx>
22 : #include <tools/diagnose_ex.h>
23 :
24 : #include <rtl/logfile.hxx>
25 : #include <rtl/math.hxx>
26 :
27 : #include <canvas/canvastools.hxx>
28 :
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>
37 :
38 : #include "cairo_canvascustomsprite.hxx"
39 : #include "cairo_spritehelper.hxx"
40 :
41 : #include <memory>
42 :
43 :
44 : using namespace ::cairo;
45 : using namespace ::com::sun::star;
46 :
47 : namespace cairocanvas
48 : {
49 0 : SpriteHelper::SpriteHelper() :
50 : mpSpriteCanvas(),
51 : mpBufferSurface(),
52 0 : mbTextureDirty(true)
53 0 : {}
54 :
55 0 : void SpriteHelper::init( const geometry::RealSize2D& rSpriteSize,
56 : const SpriteCanvasRef& rSpriteCanvas )
57 : {
58 0 : ENSURE_OR_THROW( rSpriteCanvas.get(),
59 : "SpriteHelper::init(): Invalid device, sprite canvas or surface" );
60 :
61 0 : mpSpriteCanvas = rSpriteCanvas;
62 0 : mbTextureDirty = true;
63 :
64 : // also init base class
65 : CanvasCustomSpriteHelper::init( rSpriteSize,
66 0 : rSpriteCanvas.get() );
67 0 : }
68 :
69 0 : void SpriteHelper::setSurface( const SurfaceSharedPtr& pBufferSurface )
70 : {
71 0 : mpBufferSurface = pBufferSurface;
72 0 : }
73 :
74 0 : void SpriteHelper::disposing()
75 : {
76 0 : mpBufferSurface.reset();
77 0 : mpSpriteCanvas.clear();
78 :
79 : // forward to parent
80 0 : CanvasCustomSpriteHelper::disposing();
81 0 : }
82 :
83 0 : void SpriteHelper::redraw( const CairoSharedPtr& pCairo,
84 : const ::basegfx::B2DPoint& rPos,
85 : bool& /*io_bSurfacesDirty*/,
86 : bool /*bBufferedUpdate*/ ) const
87 : {
88 : #ifdef CAIRO_CANVAS_PERF_TRACE
89 : struct timespec aTimer;
90 : mxDevice->startPerfTrace( &aTimer );
91 : #endif
92 :
93 0 : const double fAlpha( getAlpha() );
94 0 : const ::basegfx::B2DHomMatrix aTransform( getTransformation() );
95 :
96 0 : if( isActive() && !::basegfx::fTools::equalZero( fAlpha ) )
97 : {
98 : OSL_TRACE ("CanvasCustomSprite::redraw called");
99 0 : if( pCairo )
100 : {
101 0 : basegfx::B2DVector aSize = getSizePixel();
102 0 : cairo_save( pCairo.get() );
103 :
104 : double fX, fY;
105 :
106 0 : fX = rPos.getX();
107 0 : fY = rPos.getY();
108 :
109 0 : if( !aTransform.isIdentity() )
110 : {
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 0 : aTransform.get( 1, 1 ), aTransform.get( 0, 2 ), aTransform.get( 1, 2 ) );
115 :
116 0 : aMatrix.x0 = basegfx::fround( aMatrix.x0 );
117 0 : aMatrix.y0 = basegfx::fround( aMatrix.y0 );
118 :
119 0 : cairo_matrix_init( &aInverseMatrix, aMatrix.xx, aMatrix.yx, aMatrix.xy, aMatrix.yy, aMatrix.x0, aMatrix.y0 );
120 0 : cairo_matrix_invert( &aInverseMatrix );
121 0 : cairo_matrix_transform_distance( &aInverseMatrix, &fX, &fY );
122 :
123 0 : cairo_set_matrix( pCairo.get(), &aMatrix );
124 : }
125 :
126 0 : fX = basegfx::fround( fX );
127 0 : fY = basegfx::fround( fY );
128 :
129 : cairo_matrix_t aOrigMatrix;
130 0 : cairo_get_matrix( pCairo.get(), &aOrigMatrix );
131 0 : cairo_translate( pCairo.get(), fX, fY );
132 :
133 0 : if( getClip().is() )
134 : {
135 0 : const uno::Reference<rendering::XPolyPolygon2D>& rClip( getClip() );
136 :
137 : ::basegfx::B2DPolyPolygon aClipPoly(
138 : ::basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(
139 0 : rClip ));
140 :
141 : doPolyPolygonImplementation( aClipPoly, Clip, pCairo.get(),
142 0 : NULL, SurfaceProviderRef(mpSpriteCanvas.get()),
143 0 : rClip->getFillRule() );
144 : }
145 :
146 : OSL_TRACE ("aSize %f x %f position: %f,%f", aSize.getX(), aSize.getY(), fX, fY );
147 0 : cairo_rectangle( pCairo.get(), 0, 0, floor( aSize.getX() ), floor( aSize.getY() ) );
148 0 : cairo_clip( pCairo.get() );
149 0 : cairo_set_matrix( pCairo.get(), &aOrigMatrix );
150 :
151 0 : if( isContentFullyOpaque() )
152 0 : cairo_set_operator( pCairo.get(), CAIRO_OPERATOR_SOURCE );
153 : cairo_set_source_surface( pCairo.get(),
154 0 : mpBufferSurface->getCairoSurface().get(),
155 0 : fX, fY );
156 0 : if( ::rtl::math::approxEqual( fAlpha, 1.0 ) )
157 0 : cairo_paint( pCairo.get() );
158 : else
159 0 : cairo_paint_with_alpha( pCairo.get(), fAlpha );
160 :
161 0 : cairo_restore( pCairo.get() );
162 : }
163 0 : }
164 :
165 : #ifdef CAIRO_CANVAS_PERF_TRACE
166 : mxDevice->stopPerfTrace( &aTimer, "sprite redraw" );
167 : #endif
168 0 : }
169 :
170 0 : ::basegfx::B2DPolyPolygon SpriteHelper::polyPolygonFromXPolyPolygon2D( uno::Reference< rendering::XPolyPolygon2D >& xPoly ) const
171 : {
172 0 : return ::basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(xPoly);
173 : }
174 0 : }
175 :
176 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|