Branch libreoffice-5-0-4
[LibreOffice.git] / canvas / source / vcl / spritecanvashelper.cxx
blobe6fa3e7f72800f208ea4cad66f0b2e8f36917f9e
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 <tools/diagnose_ex.h>
23 #include <canvas/verbosetrace.hxx>
24 #include <canvas/canvastools.hxx>
26 #include <vcl/canvastools.hxx>
27 #include <vcl/outdev.hxx>
28 #include <vcl/window.hxx>
29 #include <vcl/bitmapex.hxx>
31 #include <basegfx/range/b2drectangle.hxx>
32 #include <basegfx/tools/canvastools.hxx>
34 #include <boost/cast.hpp>
36 #include "spritecanvashelper.hxx"
37 #include "canvascustomsprite.hxx"
39 using namespace ::com::sun::star;
41 #define FPS_BOUNDS Rectangle(0,0,130,90)
42 #define INFO_COLOR COL_RED
44 namespace vclcanvas
46 namespace
48 /** Sprite redraw at original position
50 Used to repaint the whole canvas (background and all
51 sprites)
53 void spriteRedraw( OutputDevice& rOutDev,
54 const ::canvas::Sprite::Reference& rSprite )
56 // downcast to derived vclcanvas::Sprite interface, which
57 // provides the actual redraw methods.
58 ::boost::polymorphic_downcast< Sprite* >(rSprite.get())->redraw(rOutDev,
59 true);
62 double calcNumPixel( const ::canvas::Sprite::Reference& rSprite )
64 const ::basegfx::B2DSize& rSize(
65 ::boost::polymorphic_downcast< Sprite* >(rSprite.get())->getSizePixel() );
67 return rSize.getX() * rSize.getY();
70 void repaintBackground( OutputDevice& rOutDev,
71 OutputDevice& rBackBuffer,
72 const ::basegfx::B2DRange& rArea )
74 const ::Point& rPos( vcl::unotools::pointFromB2DPoint( rArea.getMinimum()) );
75 const ::Size& rSize( vcl::unotools::sizeFromB2DSize( rArea.getRange()) );
77 rOutDev.DrawOutDev( rPos, rSize, rPos, rSize, rBackBuffer );
80 void opaqueUpdateSpriteArea( const ::canvas::Sprite::Reference& rSprite,
81 OutputDevice& rOutDev,
82 const ::basegfx::B2IRange& rArea )
84 const Rectangle& rRequestedArea(
85 vcl::unotools::rectangleFromB2IRectangle( rArea ) );
87 // clip output to actual update region (otherwise a)
88 // wouldn't save much render time, and b) will clutter
89 // scrolled sprite content outside this area)
90 rOutDev.EnableMapMode( false );
91 rOutDev.SetAntialiasing( AntialiasingFlags::EnableB2dDraw );
92 rOutDev.SetClipRegion(vcl::Region(rRequestedArea));
94 // repaint affected sprite directly to output device (at
95 // the actual screen output position)
96 ::boost::polymorphic_downcast< Sprite* >(
97 rSprite.get() )->redraw( rOutDev,
98 false ); // rendering
99 // directly to
100 // frontbuffer
103 /** Repaint sprite at original position
105 Used for opaque updates, which render directly to the
106 front buffer.
108 void spriteRedrawStub( OutputDevice& rOutDev,
109 const ::canvas::Sprite::Reference& rSprite )
111 if( rSprite.is() )
113 ::boost::polymorphic_downcast< Sprite* >(
114 rSprite.get() )->redraw( rOutDev,
115 false );
119 /** Repaint sprite at given position
121 Used for generic update, which renders into vdev of
122 adapted size.
124 void spriteRedrawStub2( OutputDevice& rOutDev,
125 const ::basegfx::B2DPoint& rOutPos,
126 const ::canvas::Sprite::Reference& rSprite )
128 if( rSprite.is() )
130 Sprite* pSprite = ::boost::polymorphic_downcast< Sprite* >(
131 rSprite.get() );
133 // calc relative sprite position in rUpdateArea (which
134 // need not be the whole screen!)
135 const ::basegfx::B2DPoint& rSpriteScreenPos( pSprite->getPosPixel() );
136 const ::basegfx::B2DPoint& rSpriteRenderPos( rSpriteScreenPos - rOutPos );
138 pSprite->redraw( rOutDev, rSpriteRenderPos, true );
142 /** Repaint sprite at original position
144 Used for opaque updates from scrollUpdate(), which render
145 directly to the front buffer.
147 void spriteRedrawStub3( OutputDevice& rOutDev,
148 const ::canvas::SpriteRedrawManager::AreaComponent& rComponent )
150 const ::canvas::Sprite::Reference& rSprite( rComponent.second.getSprite() );
152 if( rSprite.is() )
154 ::boost::polymorphic_downcast< Sprite* >(
155 rSprite.get() )->redraw( rOutDev,
156 false );
160 void renderInfoText( OutputDevice& rOutDev,
161 const OUString& rStr,
162 const Point& rPos )
164 vcl::Font aVCLFont;
165 aVCLFont.SetHeight( 20 );
166 aVCLFont.SetColor( Color( INFO_COLOR ) );
168 rOutDev.SetTextAlign(ALIGN_TOP);
169 rOutDev.SetTextColor( Color( INFO_COLOR ) );
170 rOutDev.SetFont( aVCLFont );
172 rOutDev.DrawText( rPos, rStr );
177 SpriteCanvasHelper::SpriteCanvasHelper() :
178 mpRedrawManager( NULL ),
179 mpOwningSpriteCanvas( NULL ),
180 maVDev(VclPtr<VirtualDevice>::Create()),
181 maLastUpdate(),
182 mbShowFrameInfo( false ),
183 mbShowSpriteBounds( false ),
184 mbIsUnsafeScrolling( false )
186 #if OSL_DEBUG_LEVEL > 2
187 // inverse defaults for verbose debug mode
188 mbShowSpriteBounds = mbShowFrameInfo = true;
189 #endif
192 SpriteCanvasHelper::~SpriteCanvasHelper()
194 SolarMutexGuard aGuard;
195 maVDev.disposeAndClear();
198 void SpriteCanvasHelper::init( const OutDevProviderSharedPtr& rOutDev,
199 SpriteCanvas& rOwningSpriteCanvas,
200 ::canvas::SpriteRedrawManager& rManager,
201 bool bProtect,
202 bool bHaveAlpha )
204 mpOwningSpriteCanvas = &rOwningSpriteCanvas;
205 mpRedrawManager = &rManager;
207 CanvasHelper::init(rOwningSpriteCanvas,rOutDev,bProtect,bHaveAlpha);
210 void SpriteCanvasHelper::disposing()
212 mpRedrawManager = NULL;
213 mpOwningSpriteCanvas = NULL;
215 // forward to base
216 CanvasHelper::disposing();
219 uno::Reference< rendering::XAnimatedSprite > SpriteCanvasHelper::createSpriteFromAnimation(
220 const uno::Reference< rendering::XAnimation >& )
222 return uno::Reference< rendering::XAnimatedSprite >();
225 uno::Reference< rendering::XAnimatedSprite > SpriteCanvasHelper::createSpriteFromBitmaps(
226 const uno::Sequence< uno::Reference< rendering::XBitmap > >& ,
227 sal_Int8 )
229 return uno::Reference< rendering::XAnimatedSprite >();
232 uno::Reference< rendering::XCustomSprite > SpriteCanvasHelper::createCustomSprite( const geometry::RealSize2D& spriteSize )
234 if( !mpRedrawManager || !mpDevice )
235 return uno::Reference< rendering::XCustomSprite >(); // we're disposed
237 return uno::Reference< rendering::XCustomSprite >(
238 new CanvasCustomSprite( spriteSize,
239 *mpDevice,
240 mpOwningSpriteCanvas,
241 mpOwningSpriteCanvas->getFrontBuffer(),
242 mbShowSpriteBounds ) );
245 uno::Reference< rendering::XSprite > SpriteCanvasHelper::createClonedSprite( const uno::Reference< rendering::XSprite >& )
247 return uno::Reference< rendering::XSprite >();
250 bool SpriteCanvasHelper::updateScreen( bool bUpdateAll,
251 bool& io_bSurfaceDirty )
253 if( !mpRedrawManager ||
254 !mpOwningSpriteCanvas ||
255 !mpOwningSpriteCanvas->getFrontBuffer() ||
256 !mpOwningSpriteCanvas->getBackBuffer() )
258 return false; // disposed, or otherwise dysfunctional
261 // commit to backbuffer
262 flush();
264 OutputDevice& rOutDev( mpOwningSpriteCanvas->getFrontBuffer()->getOutDev() );
265 BackBufferSharedPtr pBackBuffer( mpOwningSpriteCanvas->getBackBuffer() );
266 OutputDevice& rBackOutDev( pBackBuffer->getOutDev() );
268 // actual OutputDevice is a shared resource - restore its
269 // state when done.
270 tools::OutDevStateKeeper aStateKeeper( rOutDev );
272 const Size aOutDevSize( rBackOutDev.GetOutputSizePixel() );
273 const Point aEmptyPoint(0,0);
275 vcl::Window* pTargetWindow = NULL;
276 if( rOutDev.GetOutDevType() == OUTDEV_WINDOW )
278 pTargetWindow = &static_cast<vcl::Window&>(rOutDev); // TODO(Q3): Evil downcast.
280 // we're double-buffered, thus no need for paint area-limiting
281 // clips. besides that, will interfere with animations (as for
282 // Window-invalidate repaints, only parts of the window will
283 // be redrawn otherwise)
284 const vcl::Region aFullWindowRegion( Rectangle(aEmptyPoint,
285 aOutDevSize) );
286 pTargetWindow->ExpandPaintClipRegion(aFullWindowRegion);
289 // TODO(P1): Might be worthwile to track areas of background
290 // changes, too.
291 if( !bUpdateAll && !io_bSurfaceDirty )
293 if( mbShowFrameInfo )
295 // also repaint background below frame counter (fake
296 // that as a sprite vanishing in this area)
297 mpRedrawManager->updateSprite( ::canvas::Sprite::Reference(),
298 ::basegfx::B2DPoint(),
299 ::basegfx::B2DRectangle( 0.0, 0.0,
300 FPS_BOUNDS.Right(),
301 FPS_BOUNDS.Bottom() ) );
304 // background has not changed, so we're free to optimize
305 // repaint to areas where a sprite has changed
307 // process each independent area of overlapping sprites
308 // separately.
309 mpRedrawManager->forEachSpriteArea( *this );
311 else
313 // background has changed, so we currently have no choice
314 // but repaint everything (or caller requested that)
316 maVDev->SetOutputSizePixel( aOutDevSize );
317 maVDev->EnableMapMode( false );
318 maVDev->DrawOutDev( aEmptyPoint, aOutDevSize,
319 aEmptyPoint, aOutDevSize,
320 rBackOutDev );
322 // repaint all active sprites on top of background into
323 // VDev.
324 mpRedrawManager->forEachSprite(
325 ::boost::bind(
326 &spriteRedraw,
327 ::boost::ref( *maVDev.get() ),
328 _1 ) );
330 // flush to screen
331 rOutDev.EnableMapMode( false );
332 rOutDev.SetAntialiasing( AntialiasingFlags::EnableB2dDraw );
333 rOutDev.SetClipRegion();
334 rOutDev.DrawOutDev( aEmptyPoint, aOutDevSize,
335 aEmptyPoint, aOutDevSize,
336 *maVDev );
339 // change record vector must be cleared, for the next turn of
340 // rendering and sprite changing
341 mpRedrawManager->clearChangeRecords();
343 io_bSurfaceDirty = false;
345 if( mbShowFrameInfo )
347 renderFrameCounter( rOutDev );
348 renderSpriteCount( rOutDev );
349 renderMemUsage( rOutDev );
352 #if OSL_DEBUG_LEVEL > 2
353 static ::canvas::tools::ElapsedTime aElapsedTime;
355 // log time immediately after surface flip
356 OSL_TRACE( "SpriteCanvasHelper::updateScreen(): flip done at %f",
357 aElapsedTime.getElapsedTime() );
358 #endif
360 // sync output with screen, to ensure that we don't queue up
361 // render requests (calling code might rely on timing,
362 // i.e. assume that things are visible on screen after
363 // updateScreen() returns).
364 if( pTargetWindow )
366 // commit to screen
367 pTargetWindow->Sync();
370 return true;
373 void SpriteCanvasHelper::backgroundPaint( const ::basegfx::B2DRange& rUpdateRect )
375 ENSURE_OR_THROW( mpOwningSpriteCanvas &&
376 mpOwningSpriteCanvas->getBackBuffer() &&
377 mpOwningSpriteCanvas->getFrontBuffer(),
378 "SpriteCanvasHelper::backgroundPaint(): NULL device pointer " );
380 OutputDevice& rOutDev( mpOwningSpriteCanvas->getFrontBuffer()->getOutDev() );
381 BackBufferSharedPtr pBackBuffer( mpOwningSpriteCanvas->getBackBuffer() );
382 OutputDevice& rBackOutDev( pBackBuffer->getOutDev() );
384 repaintBackground( rOutDev, rBackOutDev, rUpdateRect );
387 void SpriteCanvasHelper::scrollUpdate( const ::basegfx::B2DRange& rMoveStart,
388 const ::basegfx::B2DRange& rMoveEnd,
389 const ::canvas::SpriteRedrawManager::UpdateArea& rUpdateArea )
391 ENSURE_OR_THROW( mpOwningSpriteCanvas &&
392 mpOwningSpriteCanvas->getBackBuffer() &&
393 mpOwningSpriteCanvas->getFrontBuffer(),
394 "SpriteCanvasHelper::scrollUpdate(): NULL device pointer " );
396 OutputDevice& rOutDev( mpOwningSpriteCanvas->getFrontBuffer()->getOutDev() );
397 BackBufferSharedPtr pBackBuffer( mpOwningSpriteCanvas->getBackBuffer() );
398 OutputDevice& rBackOutDev( pBackBuffer->getOutDev() );
400 const Size& rTargetSizePixel( rOutDev.GetOutputSizePixel() );
401 const ::basegfx::B2IRange aOutputBounds( 0,0,
402 rTargetSizePixel.Width(),
403 rTargetSizePixel.Height() );
405 // round rectangles to integer pixel. Note: have to be
406 // extremely careful here, to avoid off-by-one errors for
407 // the destination area: otherwise, the next scroll update
408 // would copy pixel that are not supposed to be part of
409 // the sprite.
410 ::basegfx::B2IRange aSourceRect(
411 ::canvas::tools::spritePixelAreaFromB2DRange( rMoveStart ) );
412 const ::basegfx::B2IRange& rDestRect(
413 ::canvas::tools::spritePixelAreaFromB2DRange( rMoveEnd ) );
414 ::basegfx::B2IPoint aDestPos( rDestRect.getMinimum() );
416 ::std::vector< ::basegfx::B2IRange > aUnscrollableAreas;
418 // Since strictly speaking, this scroll algorithm is plain
419 // buggy, the scrolled area might actually lie _below_ another
420 // window - we've made this feature configurable via
421 // mbIsUnsafeScrolling.
423 // clip to output bounds (cannot properly scroll stuff
424 // _outside_ our screen area)
425 if( !mbIsUnsafeScrolling ||
426 !::canvas::tools::clipScrollArea( aSourceRect,
427 aDestPos,
428 aUnscrollableAreas,
429 aOutputBounds ) )
431 // fully clipped scroll area: cannot simply scroll
432 // then. Perform normal opaque update (can use that, since
433 // one of the preconditions for scrollable update is
434 // opaque sprite content)
436 // repaint all affected sprites directly to output device
437 ::std::for_each( rUpdateArea.maComponentList.begin(),
438 rUpdateArea.maComponentList.end(),
439 ::boost::bind(
440 &spriteRedrawStub3,
441 ::boost::ref( rOutDev ),
442 _1 ) );
444 else
446 // scroll rOutDev content
447 rOutDev.CopyArea( vcl::unotools::pointFromB2IPoint( aDestPos ),
448 vcl::unotools::pointFromB2IPoint( aSourceRect.getMinimum() ),
449 // TODO(Q2): use numeric_cast to check range
450 ::Size( static_cast<sal_Int32>(aSourceRect.getRange().getX()),
451 static_cast<sal_Int32>(aSourceRect.getRange().getY()) ) );
453 const ::canvas::SpriteRedrawManager::SpriteConnectedRanges::ComponentListType::const_iterator
454 aFirst( rUpdateArea.maComponentList.begin() );
456 ENSURE_OR_THROW( aFirst->second.getSprite().is(),
457 "VCLCanvas::scrollUpdate(): no sprite" );
459 // repaint uncovered areas from sprite. Need to actually
460 // clip here, since we're only repainting _parts_ of the
461 // sprite
462 rOutDev.Push( PushFlags::CLIPREGION );
463 ::std::for_each( aUnscrollableAreas.begin(),
464 aUnscrollableAreas.end(),
465 ::boost::bind( &opaqueUpdateSpriteArea,
466 ::boost::cref(aFirst->second.getSprite()),
467 ::boost::ref(rOutDev),
468 _1 ) );
469 rOutDev.Pop();
472 // repaint uncovered areas from backbuffer - take the
473 // _rounded_ rectangles from above, to have the update
474 // consistent with the scroll above.
475 ::std::vector< ::basegfx::B2DRange > aUncoveredAreas;
476 ::basegfx::computeSetDifference( aUncoveredAreas,
477 rUpdateArea.maTotalBounds,
478 ::basegfx::B2DRange( rDestRect ) );
479 ::std::for_each( aUncoveredAreas.begin(),
480 aUncoveredAreas.end(),
481 ::boost::bind( &repaintBackground,
482 ::boost::ref(rOutDev),
483 ::boost::ref(rBackOutDev),
484 _1 ) );
487 void SpriteCanvasHelper::opaqueUpdate( SAL_UNUSED_PARAMETER const ::basegfx::B2DRange&,
488 const ::std::vector< ::canvas::Sprite::Reference >& rSortedUpdateSprites )
490 ENSURE_OR_THROW( mpOwningSpriteCanvas &&
491 mpOwningSpriteCanvas->getBackBuffer() &&
492 mpOwningSpriteCanvas->getFrontBuffer(),
493 "SpriteCanvasHelper::opaqueUpdate(): NULL device pointer " );
495 OutputDevice& rOutDev( mpOwningSpriteCanvas->getFrontBuffer()->getOutDev() );
497 // no need to clip output to actual update region - there will
498 // always be ALL sprites contained in the rectangular update
499 // area containd in rTotalArea (that's the way
500 // B2DConnectedRanges work). If rTotalArea appears to be
501 // smaller than the sprite - then this sprite carries a clip,
502 // and the update will be constrained to that rect.
504 // repaint all affected sprites directly to output device
505 ::std::for_each( rSortedUpdateSprites.begin(),
506 rSortedUpdateSprites.end(),
507 ::boost::bind(
508 &spriteRedrawStub,
509 ::boost::ref( rOutDev ),
510 _1 ) );
513 void SpriteCanvasHelper::genericUpdate( const ::basegfx::B2DRange& rRequestedArea,
514 const ::std::vector< ::canvas::Sprite::Reference >& rSortedUpdateSprites )
516 ENSURE_OR_THROW( mpOwningSpriteCanvas &&
517 mpOwningSpriteCanvas->getBackBuffer() &&
518 mpOwningSpriteCanvas->getFrontBuffer(),
519 "SpriteCanvasHelper::genericUpdate(): NULL device pointer " );
521 OutputDevice& rOutDev( mpOwningSpriteCanvas->getFrontBuffer()->getOutDev() );
522 BackBufferSharedPtr pBackBuffer( mpOwningSpriteCanvas->getBackBuffer() );
523 OutputDevice& rBackOutDev( pBackBuffer->getOutDev() );
525 // limit size of update VDev to target outdev's size
526 const Size& rTargetSizePixel( rOutDev.GetOutputSizePixel() );
528 // round output position towards zero. Don't want to truncate
529 // a fraction of a sprite pixel... Clip position at origin,
530 // otherwise, truncation of size below might leave visible
531 // areas uncovered by VDev.
532 const ::Point aOutputPosition(
533 ::std::max( sal_Int32( 0 ),
534 static_cast< sal_Int32 >(rRequestedArea.getMinX()) ),
535 ::std::max( sal_Int32( 0 ),
536 static_cast< sal_Int32 >(rRequestedArea.getMinY()) ) );
537 // round output size towards +infty. Don't want to truncate a
538 // fraction of a sprite pixel... Limit coverage of VDev to
539 // output device's area (i.e. not only to total size, but to
540 // cover _only_ the visible parts).
541 const ::Size aOutputSize(
542 ::std::max( sal_Int32( 0 ),
543 ::std::min( static_cast< sal_Int32 >(rTargetSizePixel.Width() - aOutputPosition.X()),
544 ::canvas::tools::roundUp( rRequestedArea.getMaxX() - aOutputPosition.X() ))),
545 ::std::max( sal_Int32( 0 ),
546 ::std::min( static_cast< sal_Int32 >(rTargetSizePixel.Height() - aOutputPosition.Y()),
547 ::canvas::tools::roundUp( rRequestedArea.getMaxY() - aOutputPosition.Y() ))));
549 // early exit for empty output area.
550 if( aOutputSize.Width() == 0 &&
551 aOutputSize.Height() == 0 )
553 return;
556 const Point aEmptyPoint(0,0);
557 const Size aCurrOutputSize( maVDev->GetOutputSizePixel() );
559 // adapt maVDev's size to the area that actually needs the
560 // repaint.
561 if( aCurrOutputSize.Width() < aOutputSize.Width() ||
562 aCurrOutputSize.Height() < aOutputSize.Height() )
564 // TODO(P1): Come up with a clever tactic to reduce maVDev
565 // from time to time. Reduction with threshold (say, if
566 // maVDev is more than twice too large) is not wise, as
567 // this might then toggle within the same updateScreen(),
568 // but for different disjunct sprite areas.
569 maVDev->SetOutputSizePixel( aOutputSize );
572 // paint background
573 maVDev->EnableMapMode( false );
574 maVDev->SetAntialiasing( AntialiasingFlags::EnableB2dDraw );
575 maVDev->SetClipRegion();
576 maVDev->DrawOutDev( aEmptyPoint, aOutputSize,
577 aOutputPosition, aOutputSize,
578 rBackOutDev );
580 // repaint all affected sprites on top of background into
581 // VDev.
582 ::std::for_each( rSortedUpdateSprites.begin(),
583 rSortedUpdateSprites.end(),
584 ::boost::bind( &spriteRedrawStub2,
585 ::boost::ref( *maVDev.get() ),
586 vcl::unotools::b2DPointFromPoint(
587 aOutputPosition),
588 _1 ) );
590 // flush to screen
591 rOutDev.EnableMapMode( false );
592 rOutDev.SetAntialiasing( AntialiasingFlags::EnableB2dDraw );
593 rOutDev.DrawOutDev( aOutputPosition, aOutputSize,
594 aEmptyPoint, aOutputSize,
595 *maVDev );
598 void SpriteCanvasHelper::renderFrameCounter( OutputDevice& rOutDev )
600 const double denominator( maLastUpdate.getElapsedTime() );
601 maLastUpdate.reset();
603 OUString text( ::rtl::math::doubleToUString( denominator == 0.0 ? 100.0 : 1.0/denominator,
604 rtl_math_StringFormat_F,
605 2,'.',NULL,' ') );
607 // pad with leading space
608 while( text.getLength() < 6 )
609 text = " " + text;
611 text += " fps";
613 renderInfoText( rOutDev,
614 text,
615 Point(0, 0) );
618 namespace
620 template< typename T > struct Adder
622 typedef void result_type;
624 Adder( T& rAdderTarget,
625 T nIncrement ) :
626 mpTarget( &rAdderTarget ),
627 mnIncrement( nIncrement )
631 void operator()() { *mpTarget += mnIncrement; }
632 void operator()( const ::canvas::Sprite::Reference& ) { *mpTarget += mnIncrement; }
633 void operator()( T nIncrement ) { *mpTarget += nIncrement; }
635 T* mpTarget;
636 T mnIncrement;
639 template< typename T> Adder<T> makeAdder( T& rAdderTarget,
640 T nIncrement )
642 return Adder<T>(rAdderTarget, nIncrement);
646 void SpriteCanvasHelper::renderSpriteCount( OutputDevice& rOutDev )
648 if( mpRedrawManager )
650 sal_Int32 nCount(0);
652 mpRedrawManager->forEachSprite( makeAdder(nCount,sal_Int32(1)) );
653 OUString text( OUString::number(nCount) );
655 // pad with leading space
656 while( text.getLength() < 3 )
657 text = " " + text;
659 text = "Sprites: " + text;
661 renderInfoText( rOutDev,
662 text,
663 Point(0, 30) );
667 void SpriteCanvasHelper::renderMemUsage( OutputDevice& rOutDev )
669 BackBufferSharedPtr pBackBuffer( mpOwningSpriteCanvas->getBackBuffer() );
671 if( mpRedrawManager &&
672 pBackBuffer )
674 double nPixel(0.0);
676 // accumulate pixel count for each sprite into fCount
677 mpRedrawManager->forEachSprite( ::boost::bind(
678 makeAdder(nPixel,1.0),
679 ::boost::bind(
680 &calcNumPixel,
681 _1 ) ) );
683 static const int NUM_VIRDEV(2);
684 static const int BYTES_PER_PIXEL(3);
686 const Size& rVDevSize( maVDev->GetOutputSizePixel() );
687 const Size& rBackBufferSize( pBackBuffer->getOutDev().GetOutputSizePixel() );
689 const double nMemUsage( nPixel * NUM_VIRDEV * BYTES_PER_PIXEL +
690 rVDevSize.Width()*rVDevSize.Height() * BYTES_PER_PIXEL +
691 rBackBufferSize.Width()*rBackBufferSize.Height() * BYTES_PER_PIXEL );
693 OUString text( ::rtl::math::doubleToUString( nMemUsage / 1048576.0,
694 rtl_math_StringFormat_F,
695 2,'.',NULL,' ') );
697 // pad with leading space
698 while( text.getLength() < 4 )
699 text = " " + text;
701 text = "Mem: " + text + "MB";
703 renderInfoText( rOutDev,
704 text,
705 Point(0, 60) );
710 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */