Branch libreoffice-5-0-4
[LibreOffice.git] / canvas / source / directx / dx_spritehelper.cxx
blob6b45d3866ceff69d8bfce73bdbd591158abc5bae
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 .
21 #include <canvas/debug.hxx>
22 #include <canvas/verbosetrace.hxx>
23 #include <tools/diagnose_ex.h>
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 "dx_canvascustomsprite.hxx"
39 #include "dx_spritehelper.hxx"
40 #include "dx_impltools.hxx"
42 using namespace ::com::sun::star;
44 namespace dxcanvas
46 SpriteHelper::SpriteHelper() :
47 mpSpriteCanvas(),
48 mpBitmap(),
49 mbTextureDirty( true ),
50 mbShowSpriteBounds( false )
54 void SpriteHelper::init( const geometry::RealSize2D& rSpriteSize,
55 const SpriteCanvasRef& rSpriteCanvas,
56 const IDXRenderModuleSharedPtr& rRenderModule,
57 const DXSurfaceBitmapSharedPtr& rBitmap,
58 bool bShowSpriteBounds )
60 ENSURE_OR_THROW( rSpriteCanvas.get() &&
61 rRenderModule &&
62 rBitmap,
63 "SpriteHelper::init(): Invalid device, sprite canvas or surface" );
65 mpSpriteCanvas = rSpriteCanvas;
66 mpBitmap = rBitmap;
67 mbTextureDirty = true;
68 mbShowSpriteBounds = bShowSpriteBounds;
70 // also init base class
71 CanvasCustomSpriteHelper::init( rSpriteSize,
72 rSpriteCanvas.get() );
75 void SpriteHelper::disposing()
77 mpBitmap.reset();
78 mpSpriteCanvas.clear();
80 // forward to parent
81 CanvasCustomSpriteHelper::disposing();
84 ::basegfx::B2DPolyPolygon SpriteHelper::polyPolygonFromXPolyPolygon2D( uno::Reference< rendering::XPolyPolygon2D >& xPoly ) const
86 return tools::polyPolygonFromXPolyPolygon2D( xPoly );
89 bool SpriteHelper::needRedraw() const
91 if( !mpBitmap ||
92 !mpSpriteCanvas.get() )
94 return false; // we're disposed, no redraw necessary
97 if( !isActive() ||
98 ::basegfx::fTools::equalZero( getAlpha() ) )
100 return false; // sprite is invisible
103 return true;
106 void SpriteHelper::redraw( bool& io_bSurfaceDirty ) const
108 if( !mpBitmap ||
109 !mpSpriteCanvas.get() )
111 return; // we're disposed
114 const ::basegfx::B2DPoint& rPos( getPosPixel() );
115 const double fAlpha( getAlpha() );
117 if( isActive() &&
118 !::basegfx::fTools::equalZero( fAlpha ) )
121 // TODO(Q2): For the time being, Device does not take a target
122 // surface, but always unconditionally renders to the
123 // background buffer.
125 // log output pos in device pixel
126 VERBOSE_TRACE( "SpriteHelper::redraw(): output pos is (%f, %f)",
127 rPos.getX(),
128 rPos.getY() );
130 const double fAlpha( getAlpha() );
131 const ::basegfx::B2DVector& rSize( getSizePixel() );
132 const ::basegfx::B2DHomMatrix& rTransform( getTransformation() );
133 const uno::Reference< rendering::XPolyPolygon2D >& xClip( getClip() );
135 mbTextureDirty = false;
136 io_bSurfaceDirty = false; // state taken, and processed.
138 ::basegfx::B2DPolyPolygon aClipPath; // empty for no clip
139 bool bIsClipRectangular( false ); // false, if no
140 // clip, or clip
141 // is complex
143 // setup and apply clip (if any)
144 // =================================
146 if( xClip.is() )
148 aClipPath = tools::polyPolygonFromXPolyPolygon2D( xClip );
150 const sal_Int32 nNumClipPolygons( aClipPath.count() );
151 if( nNumClipPolygons )
153 // TODO(P2): hold rectangle attribute directly
154 // at the XPolyPolygon2D
156 // check whether the clip is rectangular
157 if( nNumClipPolygons == 1 )
158 if( ::basegfx::tools::isRectangle( aClipPath.getB2DPolygon( 0 ) ) )
159 bIsClipRectangular = true;
163 const ::basegfx::B2DRectangle aSourceRect( 0.0,
164 0.0,
165 rSize.getX(),
166 rSize.getY() );
168 // draw simple rectangular area if no clip is set.
169 if( !aClipPath.count() )
171 mpBitmap->draw(fAlpha,rPos,rTransform);
173 else if( bIsClipRectangular )
175 // apply a simple rect clip
176 // ========================
178 ::basegfx::B2DRectangle aClipBounds(
179 ::basegfx::tools::getRange( aClipPath ) );
180 aClipBounds.intersect( aSourceRect );
182 mpBitmap->draw(fAlpha,rPos,aClipBounds,rTransform);
184 else
186 // apply clip the hard way
187 // =======================
189 mpBitmap->draw(fAlpha,rPos,aClipPath,rTransform);
192 if( mbShowSpriteBounds )
194 if( aClipPath.count() )
196 // TODO(F2): Re-enable debug output
203 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */