1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_canvas.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <ctype.h> // don't ask. msdev breaks otherwise...
32*cdf0e10cSrcweir #include <canvas/debug.hxx>
33*cdf0e10cSrcweir #include <canvas/verbosetrace.hxx>
34*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <rtl/logfile.hxx>
37*cdf0e10cSrcweir #include <rtl/math.hxx>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #include <canvas/canvastools.hxx>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrix.hxx>
42*cdf0e10cSrcweir #include <basegfx/point/b2dpoint.hxx>
43*cdf0e10cSrcweir #include <basegfx/tools/canvastools.hxx>
44*cdf0e10cSrcweir #include <basegfx/numeric/ftools.hxx>
45*cdf0e10cSrcweir #include <basegfx/polygon/b2dpolypolygontools.hxx>
46*cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygontools.hxx>
47*cdf0e10cSrcweir #include <basegfx/polygon/b2dpolypolygonrasterconverter.hxx>
48*cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygontriangulator.hxx>
49*cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygoncutandtouch.hxx>
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir #include "dx_canvascustomsprite.hxx"
52*cdf0e10cSrcweir #include "dx_spritehelper.hxx"
53*cdf0e10cSrcweir #include "dx_impltools.hxx"
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir #include <memory>
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir using namespace ::com::sun::star;
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir namespace dxcanvas
60*cdf0e10cSrcweir {
61*cdf0e10cSrcweir     SpriteHelper::SpriteHelper() :
62*cdf0e10cSrcweir         mpSpriteCanvas(),
63*cdf0e10cSrcweir         mpBitmap(),
64*cdf0e10cSrcweir         mbTextureDirty( true ),
65*cdf0e10cSrcweir         mbShowSpriteBounds( false )
66*cdf0e10cSrcweir     {
67*cdf0e10cSrcweir     }
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir     void SpriteHelper::init( const geometry::RealSize2D&	 rSpriteSize,
70*cdf0e10cSrcweir                              const SpriteCanvasRef&			 rSpriteCanvas,
71*cdf0e10cSrcweir                              const IDXRenderModuleSharedPtr& rRenderModule,
72*cdf0e10cSrcweir                              const DXSurfaceBitmapSharedPtr	 rBitmap,
73*cdf0e10cSrcweir                              bool							 bShowSpriteBounds )
74*cdf0e10cSrcweir     {
75*cdf0e10cSrcweir         ENSURE_OR_THROW( rSpriteCanvas.get() &&
76*cdf0e10cSrcweir                           rRenderModule &&
77*cdf0e10cSrcweir                           rBitmap,
78*cdf0e10cSrcweir                           "SpriteHelper::init(): Invalid device, sprite canvas or surface" );
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir         mpSpriteCanvas     = rSpriteCanvas;
81*cdf0e10cSrcweir         mpBitmap           = rBitmap;
82*cdf0e10cSrcweir         mbTextureDirty     = true;
83*cdf0e10cSrcweir         mbShowSpriteBounds = bShowSpriteBounds;
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir         // also init base class
86*cdf0e10cSrcweir         CanvasCustomSpriteHelper::init( rSpriteSize,
87*cdf0e10cSrcweir                                         rSpriteCanvas.get() );
88*cdf0e10cSrcweir     }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir     void SpriteHelper::disposing()
91*cdf0e10cSrcweir     {
92*cdf0e10cSrcweir         mpBitmap.reset();
93*cdf0e10cSrcweir         mpSpriteCanvas.clear();
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir         // forward to parent
96*cdf0e10cSrcweir         CanvasCustomSpriteHelper::disposing();
97*cdf0e10cSrcweir     }
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir     ::basegfx::B2DPolyPolygon SpriteHelper::polyPolygonFromXPolyPolygon2D( uno::Reference< rendering::XPolyPolygon2D >& xPoly ) const
100*cdf0e10cSrcweir     {
101*cdf0e10cSrcweir         return tools::polyPolygonFromXPolyPolygon2D( xPoly );
102*cdf0e10cSrcweir     }
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir     bool SpriteHelper::needRedraw() const
105*cdf0e10cSrcweir     {
106*cdf0e10cSrcweir         if( !mpBitmap ||
107*cdf0e10cSrcweir             !mpSpriteCanvas.get() )
108*cdf0e10cSrcweir         {
109*cdf0e10cSrcweir             return false; // we're disposed, no redraw necessary
110*cdf0e10cSrcweir         }
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir         if( !isActive() ||
113*cdf0e10cSrcweir             ::basegfx::fTools::equalZero( getAlpha() ) )
114*cdf0e10cSrcweir         {
115*cdf0e10cSrcweir             return false; // sprite is invisible
116*cdf0e10cSrcweir         }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir         return true;
119*cdf0e10cSrcweir     }
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir     void SpriteHelper::redraw( bool& io_bSurfaceDirty ) const
122*cdf0e10cSrcweir     {
123*cdf0e10cSrcweir         if( !mpBitmap ||
124*cdf0e10cSrcweir             !mpSpriteCanvas.get() )
125*cdf0e10cSrcweir         {
126*cdf0e10cSrcweir             return; // we're disposed
127*cdf0e10cSrcweir         }
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir         const ::basegfx::B2DPoint& rPos( getPosPixel() );
130*cdf0e10cSrcweir         const double               fAlpha( getAlpha() );
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 		if( isActive() &&
133*cdf0e10cSrcweir             !::basegfx::fTools::equalZero( fAlpha ) )
134*cdf0e10cSrcweir         {
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir 			// TODO(Q2): For the time being, Device does not take a target
137*cdf0e10cSrcweir 			// surface, but always unconditionally renders to the
138*cdf0e10cSrcweir 			// background buffer.
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir 			// log output pos in device pixel
141*cdf0e10cSrcweir 			VERBOSE_TRACE( "SpriteHelper::redraw(): output pos is (%f, %f)",
142*cdf0e10cSrcweir                            rPos.getX(),
143*cdf0e10cSrcweir                            rPos.getY() );
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 			const double                                       fAlpha( getAlpha() );
146*cdf0e10cSrcweir 			const ::basegfx::B2DVector&                        rSize( getSizePixel() );
147*cdf0e10cSrcweir 			const ::basegfx::B2DHomMatrix&                     rTransform( getTransformation() );
148*cdf0e10cSrcweir 			const uno::Reference< rendering::XPolyPolygon2D >& xClip( getClip() );
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir 			mbTextureDirty   = false;
151*cdf0e10cSrcweir 			io_bSurfaceDirty = false; // state taken, and processed.
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir 			::basegfx::B2DPolyPolygon	aClipPath; // empty for no clip
154*cdf0e10cSrcweir 			bool						bIsClipRectangular( false ); // false, if no
155*cdf0e10cSrcweir 																	// clip, or clip
156*cdf0e10cSrcweir 																	// is complex
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir 			// setup and apply clip (if any)
159*cdf0e10cSrcweir 			// =================================
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 			if( xClip.is() )
162*cdf0e10cSrcweir 			{
163*cdf0e10cSrcweir 				aClipPath = tools::polyPolygonFromXPolyPolygon2D( xClip );
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir 				const sal_Int32 nNumClipPolygons( aClipPath.count() );
166*cdf0e10cSrcweir 				if( nNumClipPolygons )
167*cdf0e10cSrcweir 				{
168*cdf0e10cSrcweir 					// TODO(P2): hold rectangle attribute directly
169*cdf0e10cSrcweir 					// at the XPolyPolygon2D
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir 					// check whether the clip is rectangular
172*cdf0e10cSrcweir 					if( nNumClipPolygons == 1 )
173*cdf0e10cSrcweir 						if( ::basegfx::tools::isRectangle( aClipPath.getB2DPolygon( 0 ) ) )
174*cdf0e10cSrcweir 							bIsClipRectangular = true;
175*cdf0e10cSrcweir 				}
176*cdf0e10cSrcweir 			}
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir 			const ::basegfx::B2DRectangle aSourceRect( 0.0,
179*cdf0e10cSrcweir                                                        0.0,
180*cdf0e10cSrcweir                                                        rSize.getX(),
181*cdf0e10cSrcweir                                                        rSize.getY() );
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir 			// draw simple rectangular area if no clip is set.
184*cdf0e10cSrcweir 			if( !aClipPath.count() )
185*cdf0e10cSrcweir 			{
186*cdf0e10cSrcweir 				mpBitmap->draw(fAlpha,rPos,rTransform);
187*cdf0e10cSrcweir 			}
188*cdf0e10cSrcweir 			else if( bIsClipRectangular )
189*cdf0e10cSrcweir 			{
190*cdf0e10cSrcweir 				// apply a simple rect clip
191*cdf0e10cSrcweir 				// ========================
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 				::basegfx::B2DRectangle aClipBounds(
194*cdf0e10cSrcweir 					::basegfx::tools::getRange( aClipPath ) );
195*cdf0e10cSrcweir 				aClipBounds.intersect( aSourceRect );
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir 				mpBitmap->draw(fAlpha,rPos,aClipBounds,rTransform);
198*cdf0e10cSrcweir 			}
199*cdf0e10cSrcweir 			else
200*cdf0e10cSrcweir 			{
201*cdf0e10cSrcweir 				// apply clip the hard way
202*cdf0e10cSrcweir 				// =======================
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir 				mpBitmap->draw(fAlpha,rPos,aClipPath,rTransform);
205*cdf0e10cSrcweir 			}
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 			if( mbShowSpriteBounds )
208*cdf0e10cSrcweir 			{
209*cdf0e10cSrcweir 				if( aClipPath.count() )
210*cdf0e10cSrcweir 				{
211*cdf0e10cSrcweir 					// TODO(F2): Re-enable debug output
212*cdf0e10cSrcweir                 }
213*cdf0e10cSrcweir 			}
214*cdf0e10cSrcweir 		}
215*cdf0e10cSrcweir 	}
216*cdf0e10cSrcweir }
217