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