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