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 :
21 : #include <canvas/debug.hxx>
22 : #include <tools/diagnose_ex.h>
23 : #include <canvas/verbosetrace.hxx>
24 :
25 : #include <rtl/math.hxx>
26 :
27 : #include <vcl/outdev.hxx>
28 : #include <vcl/bitmap.hxx>
29 : #include <vcl/alpha.hxx>
30 : #include <vcl/bitmapex.hxx>
31 : #include <vcl/canvastools.hxx>
32 :
33 : #include <basegfx/matrix/b2dhommatrix.hxx>
34 : #include <basegfx/point/b2dpoint.hxx>
35 : #include <basegfx/tools/canvastools.hxx>
36 : #include <basegfx/polygon/b2dpolygon.hxx>
37 : #include <basegfx/polygon/b2dpolygontools.hxx>
38 : #include <basegfx/polygon/b2dpolypolygontools.hxx>
39 : #include <basegfx/polygon/b2dpolygoncutandtouch.hxx>
40 : #include <basegfx/polygon/b2dpolygontriangulator.hxx>
41 : #include <basegfx/polygon/b2dpolygonclipper.hxx>
42 : #include <basegfx/numeric/ftools.hxx>
43 :
44 : #include <canvas/canvastools.hxx>
45 :
46 : #include "spritehelper.hxx"
47 :
48 : using namespace ::com::sun::star;
49 :
50 :
51 : namespace vclcanvas
52 : {
53 0 : SpriteHelper::SpriteHelper() :
54 : mpBackBuffer(),
55 : mpBackBufferMask(),
56 : maContent(),
57 0 : mbShowSpriteBounds(false)
58 : {
59 0 : }
60 :
61 0 : void SpriteHelper::init( const geometry::RealSize2D& rSpriteSize,
62 : const ::canvas::SpriteSurface::Reference& rOwningSpriteCanvas,
63 : const BackBufferSharedPtr& rBackBuffer,
64 : const BackBufferSharedPtr& rBackBufferMask,
65 : bool bShowSpriteBounds )
66 : {
67 0 : ENSURE_OR_THROW( rOwningSpriteCanvas.get() && rBackBuffer && rBackBufferMask,
68 : "SpriteHelper::init(): Invalid sprite canvas or back buffer" );
69 :
70 0 : mpBackBuffer = rBackBuffer;
71 0 : mpBackBufferMask = rBackBufferMask;
72 0 : mbShowSpriteBounds = bShowSpriteBounds;
73 :
74 0 : init( rSpriteSize, rOwningSpriteCanvas );
75 0 : }
76 :
77 0 : void SpriteHelper::disposing()
78 : {
79 0 : mpBackBuffer.reset();
80 0 : mpBackBufferMask.reset();
81 :
82 : // forward to parent
83 0 : CanvasCustomSpriteHelper::disposing();
84 0 : }
85 :
86 0 : void SpriteHelper::redraw( OutputDevice& rTargetSurface,
87 : const ::basegfx::B2DPoint& rPos,
88 : bool& io_bSurfacesDirty,
89 : bool bBufferedUpdate ) const
90 : {
91 : (void)bBufferedUpdate; // not used on every platform
92 :
93 0 : if( !mpBackBuffer ||
94 0 : !mpBackBufferMask )
95 : {
96 0 : return; // we're disposed
97 : }
98 :
99 : // log output pos in device pixel
100 : VERBOSE_TRACE( "SpriteHelper::redraw(): output pos is (%f, %f)",
101 : rPos.getX(),
102 : rPos.getY() );
103 :
104 0 : const double fAlpha( getAlpha() );
105 :
106 0 : if( isActive() &&
107 0 : !::basegfx::fTools::equalZero( fAlpha ) )
108 : {
109 0 : const Point aEmptyPoint;
110 0 : const ::basegfx::B2DVector& rOrigOutputSize( getSizePixel() );
111 :
112 : // might get changed below (e.g. adapted for
113 : // transformations). IMPORTANT: both position and size are
114 : // rounded to integer values. From now on, only those
115 : // rounded values are used, to keep clip and content in
116 : // sync.
117 0 : ::Size aOutputSize( vcl::unotools::sizeFromB2DSize( rOrigOutputSize ) );
118 0 : ::Point aOutPos( vcl::unotools::pointFromB2DPoint( rPos ) );
119 :
120 :
121 : // TODO(F3): Support for alpha-VDev
122 :
123 : // Do we have to update our bitmaps (necessary if virdev
124 : // was painted to, or transformation changed)?
125 0 : const bool bNeedBitmapUpdate( io_bSurfacesDirty ||
126 0 : hasTransformChanged() ||
127 0 : maContent->IsEmpty() );
128 :
129 : // updating content of sprite cache - surface is no
130 : // longer dirty in relation to our cache
131 0 : io_bSurfacesDirty = false;
132 0 : transformUpdated();
133 :
134 0 : if( bNeedBitmapUpdate )
135 : {
136 0 : Bitmap aBmp( mpBackBuffer->getOutDev().GetBitmap( aEmptyPoint,
137 0 : aOutputSize ) );
138 :
139 0 : if( isContentFullyOpaque() )
140 : {
141 : // optimized case: content canvas is fully
142 : // opaque. Note: since we retrieved aBmp directly
143 : // from an OutDev, it's already a 'display bitmap'
144 : // on windows.
145 0 : maContent = BitmapEx( aBmp );
146 : }
147 : else
148 : {
149 : // sprite content might contain alpha, create
150 : // BmpEx, then.
151 0 : Bitmap aMask( mpBackBufferMask->getOutDev().GetBitmap( aEmptyPoint,
152 0 : aOutputSize ) );
153 :
154 : // bitmasks are much faster than alphamasks on some platforms
155 : // so convert to bitmask if useful
156 : #ifndef MACOSX
157 0 : if( aMask.GetBitCount() != 1 )
158 : {
159 : OSL_FAIL("CanvasCustomSprite::redraw(): Mask bitmap is not "
160 : "monochrome (performance!)");
161 0 : aMask.MakeMono(255);
162 : }
163 : #endif
164 :
165 : // Note: since we retrieved aBmp and aMask
166 : // directly from an OutDev, it's already a
167 : // 'display bitmap' on windows.
168 0 : maContent = BitmapEx( aBmp, aMask );
169 0 : }
170 : }
171 :
172 0 : ::basegfx::B2DHomMatrix aTransform( getTransformation() );
173 :
174 : // check whether matrix is "easy" to handle - pure
175 : // translations or scales are handled by OutputDevice
176 : // alone
177 0 : const bool bIdentityTransform( aTransform.isIdentity() );
178 :
179 : // make transformation absolute (put sprite to final
180 : // output position). Need to happen here, as we also have
181 : // to translate the clip polygon
182 0 : aTransform.translate( aOutPos.X(),
183 0 : aOutPos.Y() );
184 :
185 0 : if( !bIdentityTransform )
186 : {
187 0 : if( !::basegfx::fTools::equalZero( aTransform.get(0,1) ) ||
188 0 : !::basegfx::fTools::equalZero( aTransform.get(1,0) ) )
189 : {
190 : // "complex" transformation, employ affine
191 : // transformator
192 :
193 : // modify output position, to account for the fact
194 : // that transformBitmap() always normalizes its output
195 : // bitmap into the smallest enclosing box.
196 0 : ::basegfx::B2DRectangle aDestRect;
197 : ::canvas::tools::calcTransformedRectBounds( aDestRect,
198 : ::basegfx::B2DRectangle(0,
199 : 0,
200 : rOrigOutputSize.getX(),
201 : rOrigOutputSize.getY()),
202 0 : aTransform );
203 :
204 0 : aOutPos.X() = ::basegfx::fround( aDestRect.getMinX() );
205 0 : aOutPos.Y() = ::basegfx::fround( aDestRect.getMinY() );
206 :
207 : // TODO(P3): Use optimized bitmap transformation here.
208 :
209 : // actually re-create the bitmap ONLY if necessary
210 0 : if( bNeedBitmapUpdate )
211 0 : maContent = tools::transformBitmap( *maContent,
212 : aTransform,
213 : uno::Sequence<double>(),
214 0 : tools::MODULATE_NONE );
215 :
216 0 : aOutputSize = maContent->GetSizePixel();
217 : }
218 : else
219 : {
220 : // relatively 'simplistic' transformation -
221 : // retrieve scale and translational offset
222 : aOutputSize.setWidth (
223 0 : ::basegfx::fround( rOrigOutputSize.getX() * aTransform.get(0,0) ) );
224 : aOutputSize.setHeight(
225 0 : ::basegfx::fround( rOrigOutputSize.getY() * aTransform.get(1,1) ) );
226 :
227 0 : aOutPos.X() = ::basegfx::fround( aTransform.get(0,2) );
228 0 : aOutPos.Y() = ::basegfx::fround( aTransform.get(1,2) );
229 : }
230 : }
231 :
232 : // transformBitmap() might return empty bitmaps, for tiny
233 : // scales.
234 0 : if( !!(*maContent) )
235 : {
236 : // when true, fast path for slide transition has
237 : // already redrawn the sprite.
238 0 : bool bSpriteRedrawn( false );
239 :
240 0 : rTargetSurface.Push( PushFlags::CLIPREGION );
241 :
242 : // apply clip (if any)
243 0 : if( getClip().is() )
244 : {
245 : ::basegfx::B2DPolyPolygon aClipPoly(
246 : ::basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(
247 0 : getClip() ));
248 :
249 0 : if( aClipPoly.count() )
250 : {
251 : // aTransform already contains the
252 : // translational component, moving the clip to
253 : // the final sprite output position.
254 0 : aClipPoly.transform( aTransform );
255 :
256 0 : if( mbShowSpriteBounds )
257 : {
258 : // Paint green sprite clip area
259 0 : rTargetSurface.SetLineColor( Color( 0,255,0 ) );
260 0 : rTargetSurface.SetFillColor();
261 :
262 0 : rTargetSurface.DrawPolyPolygon(::tools::PolyPolygon(aClipPoly)); // #i76339#
263 : }
264 :
265 0 : vcl::Region aClipRegion( aClipPoly );
266 0 : rTargetSurface.SetClipRegion( aClipRegion );
267 0 : }
268 : }
269 :
270 0 : if( !bSpriteRedrawn )
271 : {
272 0 : if( ::rtl::math::approxEqual(fAlpha, 1.0) )
273 : {
274 : // no alpha modulation -> just copy to output
275 0 : if( maContent->IsTransparent() )
276 0 : rTargetSurface.DrawBitmapEx( aOutPos, aOutputSize, *maContent );
277 : else
278 0 : rTargetSurface.DrawBitmap( aOutPos, aOutputSize, maContent->GetBitmap() );
279 : }
280 : else
281 : {
282 : // TODO(P3): Switch to OutputDevice::DrawTransparent()
283 : // here
284 :
285 : // draw semi-transparent
286 0 : sal_uInt8 nColor( static_cast<sal_uInt8>( ::basegfx::fround( 255.0*(1.0 - fAlpha) + .5) ) );
287 0 : AlphaMask aAlpha( maContent->GetSizePixel(),
288 0 : &nColor );
289 :
290 : // mask out fully transparent areas
291 0 : if( maContent->IsTransparent() )
292 0 : aAlpha.Replace( maContent->GetMask(), 255 );
293 :
294 : // alpha-blend to output
295 : rTargetSurface.DrawBitmapEx( aOutPos, aOutputSize,
296 : BitmapEx( maContent->GetBitmap(),
297 0 : aAlpha ) );
298 : }
299 : }
300 :
301 0 : rTargetSurface.Pop();
302 :
303 0 : if( mbShowSpriteBounds )
304 : {
305 : ::tools::PolyPolygon aMarkerPoly(
306 : ::canvas::tools::getBoundMarksPolyPolygon(
307 0 : ::basegfx::B2DRectangle(aOutPos.X(),
308 0 : aOutPos.Y(),
309 0 : aOutPos.X() + aOutputSize.Width()-1,
310 0 : aOutPos.Y() + aOutputSize.Height()-1) ) );
311 :
312 : // Paint little red sprite area markers
313 0 : rTargetSurface.SetLineColor( COL_RED );
314 0 : rTargetSurface.SetFillColor();
315 :
316 0 : for( int i=0; i<aMarkerPoly.Count(); ++i )
317 : {
318 0 : rTargetSurface.DrawPolyLine( aMarkerPoly.GetObject((sal_uInt16)i) );
319 : }
320 :
321 : // paint sprite prio
322 0 : vcl::Font aVCLFont;
323 0 : aVCLFont.SetHeight( std::min(long(20),aOutputSize.Height()) );
324 0 : aVCLFont.SetColor( COL_RED );
325 :
326 0 : rTargetSurface.SetTextAlign(ALIGN_TOP);
327 0 : rTargetSurface.SetTextColor( COL_RED );
328 0 : rTargetSurface.SetFont( aVCLFont );
329 :
330 : OUString text( ::rtl::math::doubleToUString( getPriority(),
331 : rtl_math_StringFormat_F,
332 0 : 2,'.',NULL,' ') );
333 :
334 0 : rTargetSurface.DrawText( aOutPos+Point(2,2), text );
335 : SAL_INFO(
336 : "canvas.level2",
337 0 : "sprite " << this << " has prio " << getPriority());
338 : }
339 0 : }
340 : }
341 : }
342 :
343 0 : ::basegfx::B2DPolyPolygon SpriteHelper::polyPolygonFromXPolyPolygon2D( uno::Reference< rendering::XPolyPolygon2D >& xPoly ) const
344 : {
345 0 : return ::basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D( xPoly );
346 : }
347 :
348 0 : }
349 :
350 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|