Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include <canvas/debug.hxx>
31 : : #include <tools/diagnose_ex.h>
32 : : #include <canvas/verbosetrace.hxx>
33 : : #include <canvas/canvastools.hxx>
34 : :
35 : : #include <vcl/canvastools.hxx>
36 : : #include <vcl/outdev.hxx>
37 : : #include <vcl/window.hxx>
38 : : #include <vcl/bitmapex.hxx>
39 : :
40 : : #include <basegfx/range/b2drectangle.hxx>
41 : : #include <basegfx/tools/canvastools.hxx>
42 : :
43 : : #include <boost/cast.hpp>
44 : :
45 : : #include "spritecanvashelper.hxx"
46 : : #include "canvascustomsprite.hxx"
47 : :
48 : :
49 : : using namespace ::com::sun::star;
50 : :
51 : : #define FPS_BOUNDS Rectangle(0,0,130,90)
52 : : #define INFO_COLOR COL_RED
53 : :
54 : : namespace vclcanvas
55 : : {
56 : : namespace
57 : : {
58 : : /** Sprite redraw at original position
59 : :
60 : : Used to repaint the whole canvas (background and all
61 : : sprites)
62 : : */
63 : 0 : void spriteRedraw( OutputDevice& rOutDev,
64 : : const ::canvas::Sprite::Reference& rSprite )
65 : : {
66 : : // downcast to derived vclcanvas::Sprite interface, which
67 : : // provides the actual redraw methods.
68 : 0 : ::boost::polymorphic_downcast< Sprite* >(rSprite.get())->redraw(rOutDev,
69 : 0 : true);
70 : 0 : }
71 : :
72 : 0 : double calcNumPixel( const ::canvas::Sprite::Reference& rSprite )
73 : : {
74 : : const ::basegfx::B2DSize& rSize(
75 : 0 : ::boost::polymorphic_downcast< Sprite* >(rSprite.get())->getSizePixel() );
76 : :
77 : 0 : return rSize.getX() * rSize.getY();
78 : : }
79 : :
80 : 0 : void repaintBackground( OutputDevice& rOutDev,
81 : : OutputDevice& rBackBuffer,
82 : : const ::basegfx::B2DRange& rArea )
83 : : {
84 : 0 : const ::Point& rPos( ::vcl::unotools::pointFromB2DPoint( rArea.getMinimum()) );
85 : 0 : const ::Size& rSize( ::vcl::unotools::sizeFromB2DSize( rArea.getRange()) );
86 : :
87 : 0 : rOutDev.DrawOutDev( rPos, rSize, rPos, rSize, rBackBuffer );
88 : 0 : }
89 : :
90 : 0 : void opaqueUpdateSpriteArea( const ::canvas::Sprite::Reference& rSprite,
91 : : OutputDevice& rOutDev,
92 : : const ::basegfx::B2IRange& rArea )
93 : : {
94 : : const Rectangle& rRequestedArea(
95 : 0 : ::vcl::unotools::rectangleFromB2IRectangle( rArea ) );
96 : :
97 : : // clip output to actual update region (otherwise a)
98 : : // wouldn't save much render time, and b) will clutter
99 : : // scrolled sprite content outside this area)
100 : 0 : rOutDev.EnableMapMode( sal_False );
101 : 0 : rOutDev.SetClipRegion( rRequestedArea );
102 : :
103 : : // repaint affected sprite directly to output device (at
104 : : // the actual screen output position)
105 : : ::boost::polymorphic_downcast< Sprite* >(
106 : 0 : rSprite.get() )->redraw( rOutDev,
107 : 0 : false ); // rendering
108 : : // directly to
109 : : // frontbuffer
110 : 0 : }
111 : :
112 : : /** Repaint sprite at original position
113 : :
114 : : Used for opaque updates, which render directly to the
115 : : front buffer.
116 : : */
117 : 0 : void spriteRedrawStub( OutputDevice& rOutDev,
118 : : const ::canvas::Sprite::Reference& rSprite )
119 : : {
120 : 0 : if( rSprite.is() )
121 : : {
122 : : ::boost::polymorphic_downcast< Sprite* >(
123 : 0 : rSprite.get() )->redraw( rOutDev,
124 : 0 : false );
125 : : }
126 : 0 : }
127 : :
128 : : /** Repaint sprite at given position
129 : :
130 : : Used for generic update, which renders into vdev of
131 : : adapted size.
132 : : */
133 : 0 : void spriteRedrawStub2( OutputDevice& rOutDev,
134 : : const ::basegfx::B2DPoint& rOutPos,
135 : : const ::canvas::Sprite::Reference& rSprite )
136 : : {
137 : 0 : if( rSprite.is() )
138 : : {
139 : : Sprite* pSprite = ::boost::polymorphic_downcast< Sprite* >(
140 : 0 : rSprite.get() );
141 : :
142 : : // calc relative sprite position in rUpdateArea (which
143 : : // need not be the whole screen!)
144 : 0 : const ::basegfx::B2DPoint& rSpriteScreenPos( pSprite->getPosPixel() );
145 : 0 : const ::basegfx::B2DPoint& rSpriteRenderPos( rSpriteScreenPos - rOutPos );
146 : :
147 : 0 : pSprite->redraw( rOutDev, rSpriteRenderPos, true );
148 : : }
149 : 0 : }
150 : :
151 : : /** Repaint sprite at original position
152 : :
153 : : Used for opaque updates from scrollUpdate(), which render
154 : : directly to the front buffer.
155 : : */
156 : 0 : void spriteRedrawStub3( OutputDevice& rOutDev,
157 : : const ::canvas::SpriteRedrawManager::AreaComponent& rComponent )
158 : : {
159 : 0 : const ::canvas::Sprite::Reference& rSprite( rComponent.second.getSprite() );
160 : :
161 : 0 : if( rSprite.is() )
162 : : {
163 : : ::boost::polymorphic_downcast< Sprite* >(
164 : 0 : rSprite.get() )->redraw( rOutDev,
165 : 0 : false );
166 : : }
167 : 0 : }
168 : :
169 : 0 : void renderInfoText( OutputDevice& rOutDev,
170 : : const ::rtl::OUString& rStr,
171 : : const Point& rPos )
172 : : {
173 : 0 : Font aVCLFont;
174 : 0 : aVCLFont.SetHeight( 20 );
175 : 0 : aVCLFont.SetColor( Color( INFO_COLOR ) );
176 : :
177 : 0 : rOutDev.SetTextAlign(ALIGN_TOP);
178 : 0 : rOutDev.SetTextColor( Color( INFO_COLOR ) );
179 : 0 : rOutDev.SetFont( aVCLFont );
180 : :
181 : 0 : rOutDev.DrawText( rPos, rStr );
182 : 0 : }
183 : :
184 : : }
185 : :
186 : 0 : SpriteCanvasHelper::SpriteCanvasHelper() :
187 : : mpRedrawManager( NULL ),
188 : : mpOwningSpriteCanvas( NULL ),
189 : : maVDev(),
190 : : maLastUpdate(),
191 : : mbShowFrameInfo( false ),
192 : : mbShowSpriteBounds( false ),
193 : 0 : mbIsUnsafeScrolling( false )
194 : : {
195 : : #if OSL_DEBUG_LEVEL > 2
196 : : // inverse defaults for verbose debug mode
197 : : mbShowSpriteBounds = mbShowFrameInfo = true;
198 : : #endif
199 : 0 : }
200 : :
201 : 0 : void SpriteCanvasHelper::init( const OutDevProviderSharedPtr& rOutDev,
202 : : SpriteCanvas& rOwningSpriteCanvas,
203 : : ::canvas::SpriteRedrawManager& rManager,
204 : : bool bProtect,
205 : : bool bHaveAlpha )
206 : : {
207 : 0 : mpOwningSpriteCanvas = &rOwningSpriteCanvas;
208 : 0 : mpRedrawManager = &rManager;
209 : :
210 : 0 : CanvasHelper::init(rOwningSpriteCanvas,rOutDev,bProtect,bHaveAlpha);
211 : 0 : }
212 : :
213 : 0 : void SpriteCanvasHelper::disposing()
214 : : {
215 : 0 : mpRedrawManager = NULL;
216 : 0 : mpOwningSpriteCanvas = NULL;
217 : :
218 : : // forward to base
219 : 0 : CanvasHelper::disposing();
220 : 0 : }
221 : :
222 : 0 : uno::Reference< rendering::XAnimatedSprite > SpriteCanvasHelper::createSpriteFromAnimation(
223 : : const uno::Reference< rendering::XAnimation >& )
224 : : {
225 : 0 : return uno::Reference< rendering::XAnimatedSprite >();
226 : : }
227 : :
228 : 0 : uno::Reference< rendering::XAnimatedSprite > SpriteCanvasHelper::createSpriteFromBitmaps(
229 : : const uno::Sequence< uno::Reference< rendering::XBitmap > >& ,
230 : : sal_Int8 )
231 : : {
232 : 0 : return uno::Reference< rendering::XAnimatedSprite >();
233 : : }
234 : :
235 : 0 : uno::Reference< rendering::XCustomSprite > SpriteCanvasHelper::createCustomSprite( const geometry::RealSize2D& spriteSize )
236 : : {
237 : 0 : if( !mpRedrawManager || !mpDevice )
238 : 0 : return uno::Reference< rendering::XCustomSprite >(); // we're disposed
239 : :
240 : : return uno::Reference< rendering::XCustomSprite >(
241 : : new CanvasCustomSprite( spriteSize,
242 : : *mpDevice,
243 : : mpOwningSpriteCanvas,
244 : : mpOwningSpriteCanvas->getFrontBuffer(),
245 : 0 : mbShowSpriteBounds ) );
246 : : }
247 : :
248 : 0 : uno::Reference< rendering::XSprite > SpriteCanvasHelper::createClonedSprite( const uno::Reference< rendering::XSprite >& )
249 : : {
250 : 0 : return uno::Reference< rendering::XSprite >();
251 : : }
252 : :
253 : 0 : sal_Bool SpriteCanvasHelper::updateScreen( sal_Bool bUpdateAll,
254 : : bool& io_bSurfaceDirty )
255 : : {
256 : 0 : if( !mpRedrawManager ||
257 : 0 : !mpOwningSpriteCanvas ||
258 : 0 : !mpOwningSpriteCanvas->getFrontBuffer() ||
259 : 0 : !mpOwningSpriteCanvas->getBackBuffer() )
260 : : {
261 : 0 : return sal_False; // disposed, or otherwise dysfunctional
262 : : }
263 : :
264 : : // commit to backbuffer
265 : 0 : flush();
266 : :
267 : 0 : OutputDevice& rOutDev( mpOwningSpriteCanvas->getFrontBuffer()->getOutDev() );
268 : 0 : BackBufferSharedPtr pBackBuffer( mpOwningSpriteCanvas->getBackBuffer() );
269 : 0 : OutputDevice& rBackOutDev( pBackBuffer->getOutDev() );
270 : :
271 : : // actual OutputDevice is a shared resource - restore its
272 : : // state when done.
273 : 0 : tools::OutDevStateKeeper aStateKeeper( rOutDev );
274 : :
275 : 0 : const Size aOutDevSize( rBackOutDev.GetOutputSizePixel() );
276 : 0 : const Point aEmptyPoint(0,0);
277 : :
278 : 0 : Window* pTargetWindow = NULL;
279 : 0 : if( rOutDev.GetOutDevType() == OUTDEV_WINDOW )
280 : : {
281 : 0 : pTargetWindow = &static_cast<Window&>(rOutDev); // TODO(Q3): Evil downcast.
282 : :
283 : : // we're double-buffered, thus no need for paint area-limiting
284 : : // clips. besides that, will interfere with animations (as for
285 : : // Window-invalidate repaints, only parts of the window will
286 : : // be redrawn otherwise)
287 : : const Region aFullWindowRegion( Rectangle(aEmptyPoint,
288 : 0 : aOutDevSize) );
289 : 0 : pTargetWindow->ExpandPaintClipRegion(aFullWindowRegion);
290 : : }
291 : :
292 : : // TODO(P1): Might be worthwile to track areas of background
293 : : // changes, too.
294 : 0 : if( !bUpdateAll && !io_bSurfaceDirty )
295 : : {
296 : 0 : if( mbShowFrameInfo )
297 : : {
298 : : // also repaint background below frame counter (fake
299 : : // that as a sprite vanishing in this area)
300 : : mpRedrawManager->updateSprite( ::canvas::Sprite::Reference(),
301 : : ::basegfx::B2DPoint(),
302 : : ::basegfx::B2DRectangle( 0.0, 0.0,
303 : 0 : FPS_BOUNDS.Right(),
304 : 0 : FPS_BOUNDS.Bottom() ) );
305 : : }
306 : :
307 : : // background has not changed, so we're free to optimize
308 : : // repaint to areas where a sprite has changed
309 : :
310 : : // process each independent area of overlapping sprites
311 : : // separately.
312 : 0 : mpRedrawManager->forEachSpriteArea( *this );
313 : : }
314 : : else
315 : : {
316 : : // background has changed, so we currently have no choice
317 : : // but repaint everything (or caller requested that)
318 : :
319 : 0 : maVDev->SetOutputSizePixel( aOutDevSize );
320 : 0 : maVDev->EnableMapMode( sal_False );
321 : 0 : maVDev->DrawOutDev( aEmptyPoint, aOutDevSize,
322 : : aEmptyPoint, aOutDevSize,
323 : 0 : rBackOutDev );
324 : :
325 : : // repaint all active sprites on top of background into
326 : : // VDev.
327 : : mpRedrawManager->forEachSprite(
328 : : ::boost::bind(
329 : : &spriteRedraw,
330 : 0 : ::boost::ref( maVDev.get() ),
331 : 0 : _1 ) );
332 : :
333 : : // flush to screen
334 : 0 : rOutDev.EnableMapMode( sal_False );
335 : 0 : rOutDev.SetClipRegion();
336 : : rOutDev.DrawOutDev( aEmptyPoint, aOutDevSize,
337 : : aEmptyPoint, aOutDevSize,
338 : 0 : *maVDev );
339 : : }
340 : :
341 : : // change record vector must be cleared, for the next turn of
342 : : // rendering and sprite changing
343 : 0 : mpRedrawManager->clearChangeRecords();
344 : :
345 : 0 : io_bSurfaceDirty = false;
346 : :
347 : 0 : if( mbShowFrameInfo )
348 : : {
349 : 0 : renderFrameCounter( rOutDev );
350 : 0 : renderSpriteCount( rOutDev );
351 : 0 : renderMemUsage( rOutDev );
352 : : }
353 : :
354 : : #if OSL_DEBUG_LEVEL > 2
355 : : static ::canvas::tools::ElapsedTime aElapsedTime;
356 : :
357 : : // log time immediately after surface flip
358 : : OSL_TRACE( "SpriteCanvasHelper::updateScreen(): flip done at %f",
359 : : aElapsedTime.getElapsedTime() );
360 : : #endif
361 : :
362 : : // sync output with screen, to ensure that we don't queue up
363 : : // render requests (calling code might rely on timing,
364 : : // i.e. assume that things are visible on screen after
365 : : // updateScreen() returns).
366 : 0 : if( pTargetWindow )
367 : : {
368 : : // commit to screen
369 : 0 : pTargetWindow->Sync();
370 : : }
371 : :
372 : 0 : return sal_True;
373 : : }
374 : :
375 : 0 : void SpriteCanvasHelper::backgroundPaint( const ::basegfx::B2DRange& rUpdateRect )
376 : : {
377 : 0 : ENSURE_OR_THROW( mpOwningSpriteCanvas &&
378 : : mpOwningSpriteCanvas->getBackBuffer() &&
379 : : mpOwningSpriteCanvas->getFrontBuffer(),
380 : : "SpriteCanvasHelper::backgroundPaint(): NULL device pointer " );
381 : :
382 : 0 : OutputDevice& rOutDev( mpOwningSpriteCanvas->getFrontBuffer()->getOutDev() );
383 : 0 : BackBufferSharedPtr pBackBuffer( mpOwningSpriteCanvas->getBackBuffer() );
384 : 0 : OutputDevice& rBackOutDev( pBackBuffer->getOutDev() );
385 : :
386 : 0 : repaintBackground( rOutDev, rBackOutDev, rUpdateRect );
387 : 0 : }
388 : :
389 : 0 : void SpriteCanvasHelper::scrollUpdate( const ::basegfx::B2DRange& rMoveStart,
390 : : const ::basegfx::B2DRange& rMoveEnd,
391 : : const ::canvas::SpriteRedrawManager::UpdateArea& rUpdateArea )
392 : : {
393 : 0 : ENSURE_OR_THROW( mpOwningSpriteCanvas &&
394 : : mpOwningSpriteCanvas->getBackBuffer() &&
395 : : mpOwningSpriteCanvas->getFrontBuffer(),
396 : : "SpriteCanvasHelper::scrollUpdate(): NULL device pointer " );
397 : :
398 : 0 : OutputDevice& rOutDev( mpOwningSpriteCanvas->getFrontBuffer()->getOutDev() );
399 : 0 : BackBufferSharedPtr pBackBuffer( mpOwningSpriteCanvas->getBackBuffer() );
400 : 0 : OutputDevice& rBackOutDev( pBackBuffer->getOutDev() );
401 : :
402 : 0 : const Size& rTargetSizePixel( rOutDev.GetOutputSizePixel() );
403 : : const ::basegfx::B2IRange aOutputBounds( 0,0,
404 : : rTargetSizePixel.Width(),
405 : 0 : rTargetSizePixel.Height() );
406 : :
407 : : // round rectangles to integer pixel. Note: have to be
408 : : // extremely careful here, to avoid off-by-one errors for
409 : : // the destination area: otherwise, the next scroll update
410 : : // would copy pixel that are not supposed to be part of
411 : : // the sprite.
412 : : ::basegfx::B2IRange aSourceRect(
413 : 0 : ::canvas::tools::spritePixelAreaFromB2DRange( rMoveStart ) );
414 : : const ::basegfx::B2IRange& rDestRect(
415 : 0 : ::canvas::tools::spritePixelAreaFromB2DRange( rMoveEnd ) );
416 : 0 : ::basegfx::B2IPoint aDestPos( rDestRect.getMinimum() );
417 : :
418 : 0 : ::std::vector< ::basegfx::B2IRange > aUnscrollableAreas;
419 : :
420 : : // Since strictly speaking, this scroll algorithm is plain
421 : : // buggy, the scrolled area might actually lie _below_ another
422 : : // window - we've made this feature configurable via
423 : : // mbIsUnsafeScrolling.
424 : :
425 : : // clip to output bounds (cannot properly scroll stuff
426 : : // _outside_ our screen area)
427 : 0 : if( !mbIsUnsafeScrolling ||
428 : : !::canvas::tools::clipScrollArea( aSourceRect,
429 : : aDestPos,
430 : : aUnscrollableAreas,
431 : 0 : aOutputBounds ) )
432 : : {
433 : : // fully clipped scroll area: cannot simply scroll
434 : : // then. Perform normal opaque update (can use that, since
435 : : // one of the preconditions for scrollable update is
436 : : // opaque sprite content)
437 : :
438 : : // repaint all affected sprites directly to output device
439 : : ::std::for_each( rUpdateArea.maComponentList.begin(),
440 : : rUpdateArea.maComponentList.end(),
441 : : ::boost::bind(
442 : : &spriteRedrawStub3,
443 : : ::boost::ref( rOutDev ),
444 : 0 : _1 ) );
445 : : }
446 : : else
447 : : {
448 : : // scroll rOutDev content
449 : : rOutDev.CopyArea( ::vcl::unotools::pointFromB2IPoint( aDestPos ),
450 : 0 : ::vcl::unotools::pointFromB2IPoint( aSourceRect.getMinimum() ),
451 : : // TODO(Q2): use numeric_cast to check range
452 : 0 : ::Size( static_cast<sal_Int32>(aSourceRect.getRange().getX()),
453 : 0 : static_cast<sal_Int32>(aSourceRect.getRange().getY()) ) );
454 : :
455 : : const ::canvas::SpriteRedrawManager::SpriteConnectedRanges::ComponentListType::const_iterator
456 : 0 : aFirst( rUpdateArea.maComponentList.begin() );
457 : :
458 : 0 : ENSURE_OR_THROW( aFirst->second.getSprite().is(),
459 : : "VCLCanvas::scrollUpdate(): no sprite" );
460 : :
461 : : // repaint uncovered areas from sprite. Need to actually
462 : : // clip here, since we're only repainting _parts_ of the
463 : : // sprite
464 : 0 : rOutDev.Push( PUSH_CLIPREGION );
465 : : ::std::for_each( aUnscrollableAreas.begin(),
466 : : aUnscrollableAreas.end(),
467 : : ::boost::bind( &opaqueUpdateSpriteArea,
468 : 0 : ::boost::cref(aFirst->second.getSprite()),
469 : : ::boost::ref(rOutDev),
470 : 0 : _1 ) );
471 : 0 : rOutDev.Pop();
472 : : }
473 : :
474 : : // repaint uncovered areas from backbuffer - take the
475 : : // _rounded_ rectangles from above, to have the update
476 : : // consistent with the scroll above.
477 : 0 : ::std::vector< ::basegfx::B2DRange > aUncoveredAreas;
478 : : ::basegfx::computeSetDifference( aUncoveredAreas,
479 : : rUpdateArea.maTotalBounds,
480 : 0 : ::basegfx::B2DRange( rDestRect ) );
481 : : ::std::for_each( aUncoveredAreas.begin(),
482 : : aUncoveredAreas.end(),
483 : : ::boost::bind( &repaintBackground,
484 : : ::boost::ref(rOutDev),
485 : : ::boost::ref(rBackOutDev),
486 : 0 : _1 ) );
487 : 0 : }
488 : :
489 : 0 : void SpriteCanvasHelper::opaqueUpdate( SAL_UNUSED_PARAMETER const ::basegfx::B2DRange&,
490 : : const ::std::vector< ::canvas::Sprite::Reference >& rSortedUpdateSprites )
491 : : {
492 : 0 : ENSURE_OR_THROW( mpOwningSpriteCanvas &&
493 : : mpOwningSpriteCanvas->getBackBuffer() &&
494 : : mpOwningSpriteCanvas->getFrontBuffer(),
495 : : "SpriteCanvasHelper::opaqueUpdate(): NULL device pointer " );
496 : :
497 : 0 : OutputDevice& rOutDev( mpOwningSpriteCanvas->getFrontBuffer()->getOutDev() );
498 : :
499 : : // no need to clip output to actual update region - there will
500 : : // always be ALL sprites contained in the rectangular update
501 : : // area containd in rTotalArea (that's the way
502 : : // B2DConnectedRanges work). If rTotalArea appears to be
503 : : // smaller than the sprite - then this sprite carries a clip,
504 : : // and the update will be constrained to that rect.
505 : :
506 : : // repaint all affected sprites directly to output device
507 : : ::std::for_each( rSortedUpdateSprites.begin(),
508 : : rSortedUpdateSprites.end(),
509 : : ::boost::bind(
510 : : &spriteRedrawStub,
511 : : ::boost::ref( rOutDev ),
512 : 0 : _1 ) );
513 : 0 : }
514 : :
515 : 0 : void SpriteCanvasHelper::genericUpdate( const ::basegfx::B2DRange& rRequestedArea,
516 : : const ::std::vector< ::canvas::Sprite::Reference >& rSortedUpdateSprites )
517 : : {
518 : 0 : ENSURE_OR_THROW( mpOwningSpriteCanvas &&
519 : : mpOwningSpriteCanvas->getBackBuffer() &&
520 : : mpOwningSpriteCanvas->getFrontBuffer(),
521 : : "SpriteCanvasHelper::genericUpdate(): NULL device pointer " );
522 : :
523 : 0 : OutputDevice& rOutDev( mpOwningSpriteCanvas->getFrontBuffer()->getOutDev() );
524 : 0 : BackBufferSharedPtr pBackBuffer( mpOwningSpriteCanvas->getBackBuffer() );
525 : 0 : OutputDevice& rBackOutDev( pBackBuffer->getOutDev() );
526 : :
527 : : // limit size of update VDev to target outdev's size
528 : 0 : const Size& rTargetSizePixel( rOutDev.GetOutputSizePixel() );
529 : :
530 : : // round output position towards zero. Don't want to truncate
531 : : // a fraction of a sprite pixel... Clip position at origin,
532 : : // otherwise, truncation of size below might leave visible
533 : : // areas uncovered by VDev.
534 : : const ::Point aOutputPosition(
535 : : ::std::max( sal_Int32( 0 ),
536 : 0 : static_cast< sal_Int32 >(rRequestedArea.getMinX()) ),
537 : : ::std::max( sal_Int32( 0 ),
538 : 0 : static_cast< sal_Int32 >(rRequestedArea.getMinY()) ) );
539 : : // round output size towards +infty. Don't want to truncate a
540 : : // fraction of a sprite pixel... Limit coverage of VDev to
541 : : // output device's area (i.e. not only to total size, but to
542 : : // cover _only_ the visible parts).
543 : : const ::Size aOutputSize(
544 : : ::std::max( sal_Int32( 0 ),
545 : 0 : ::std::min( static_cast< sal_Int32 >(rTargetSizePixel.Width() - aOutputPosition.X()),
546 : 0 : ::canvas::tools::roundUp( rRequestedArea.getMaxX() - aOutputPosition.X() ))),
547 : : ::std::max( sal_Int32( 0 ),
548 : 0 : ::std::min( static_cast< sal_Int32 >(rTargetSizePixel.Height() - aOutputPosition.Y()),
549 : 0 : ::canvas::tools::roundUp( rRequestedArea.getMaxY() - aOutputPosition.Y() ))));
550 : :
551 : : // early exit for empty output area.
552 : 0 : if( aOutputSize.Width() == 0 &&
553 : 0 : aOutputSize.Height() == 0 )
554 : : {
555 : 0 : return;
556 : : }
557 : :
558 : 0 : const Point aEmptyPoint(0,0);
559 : 0 : const Size aCurrOutputSize( maVDev->GetOutputSizePixel() );
560 : :
561 : : // adapt maVDev's size to the area that actually needs the
562 : : // repaint.
563 : 0 : if( aCurrOutputSize.Width() < aOutputSize.Width() ||
564 : 0 : aCurrOutputSize.Height() < aOutputSize.Height() )
565 : : {
566 : : // TODO(P1): Come up with a clever tactic to reduce maVDev
567 : : // from time to time. Reduction with threshold (say, if
568 : : // maVDev is more than twice too large) is not wise, as
569 : : // this might then toggle within the same updateScreen(),
570 : : // but for different disjunct sprite areas.
571 : 0 : maVDev->SetOutputSizePixel( aOutputSize );
572 : : }
573 : :
574 : : // paint background
575 : 0 : maVDev->EnableMapMode( sal_False );
576 : 0 : maVDev->SetClipRegion();
577 : 0 : maVDev->DrawOutDev( aEmptyPoint, aOutputSize,
578 : : aOutputPosition, aOutputSize,
579 : 0 : rBackOutDev );
580 : :
581 : : // repaint all affected sprites on top of background into
582 : : // VDev.
583 : : ::std::for_each( rSortedUpdateSprites.begin(),
584 : : rSortedUpdateSprites.end(),
585 : : ::boost::bind( &spriteRedrawStub2,
586 : 0 : ::boost::ref( maVDev.get() ),
587 : : ::boost::cref(
588 : : ::vcl::unotools::b2DPointFromPoint(aOutputPosition)),
589 : 0 : _1 ) );
590 : :
591 : : // flush to screen
592 : 0 : rOutDev.EnableMapMode( sal_False );
593 : : rOutDev.DrawOutDev( aOutputPosition, aOutputSize,
594 : : aEmptyPoint, aOutputSize,
595 : 0 : *maVDev );
596 : : }
597 : :
598 : 0 : void SpriteCanvasHelper::renderFrameCounter( OutputDevice& rOutDev )
599 : : {
600 : 0 : const double denominator( maLastUpdate.getElapsedTime() );
601 : 0 : maLastUpdate.reset();
602 : :
603 : : ::rtl::OUString text( ::rtl::math::doubleToUString( denominator == 0.0 ? 100.0 : 1.0/denominator,
604 : : rtl_math_StringFormat_F,
605 : 0 : 2,'.',NULL,' ') );
606 : :
607 : : // pad with leading space
608 : 0 : while( text.getLength() < 6 )
609 : 0 : text = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM (" ")) + text;
610 : :
611 : 0 : text += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM (" fps"));
612 : :
613 : : renderInfoText( rOutDev,
614 : : text,
615 : 0 : Point(0, 0) );
616 : 0 : }
617 : :
618 : : namespace
619 : : {
620 : : template< typename T > struct Adder
621 : : {
622 : : typedef void result_type;
623 : :
624 : 0 : Adder( T& rAdderTarget,
625 : : T nIncrement ) :
626 : : mpTarget( &rAdderTarget ),
627 : 0 : mnIncrement( nIncrement )
628 : : {
629 : 0 : }
630 : :
631 : : void operator()() { *mpTarget += mnIncrement; }
632 : 0 : void operator()( const ::canvas::Sprite::Reference& ) { *mpTarget += mnIncrement; }
633 : 0 : void operator()( T nIncrement ) { *mpTarget += nIncrement; }
634 : :
635 : : T* mpTarget;
636 : : T mnIncrement;
637 : : };
638 : :
639 : 0 : template< typename T> Adder<T> makeAdder( T& rAdderTarget,
640 : : T nIncrement )
641 : : {
642 : 0 : return Adder<T>(rAdderTarget, nIncrement);
643 : : }
644 : : }
645 : :
646 : 0 : void SpriteCanvasHelper::renderSpriteCount( OutputDevice& rOutDev )
647 : : {
648 : 0 : if( mpRedrawManager )
649 : : {
650 : 0 : sal_Int32 nCount(0);
651 : :
652 : 0 : mpRedrawManager->forEachSprite( makeAdder(nCount,sal_Int32(1)) );
653 : : ::rtl::OUString text(
654 : : ::rtl::OUString::valueOf(
655 : : // disambiguate overload...
656 : 0 : static_cast<sal_Int64>(nCount) ) );
657 : :
658 : : // pad with leading space
659 : 0 : while( text.getLength() < 3 )
660 : 0 : text = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM (" ")) + text;
661 : :
662 : 0 : text = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM ("Sprites: ")) + text;
663 : :
664 : : renderInfoText( rOutDev,
665 : : text,
666 : 0 : Point(0, 30) );
667 : : }
668 : 0 : }
669 : :
670 : 0 : void SpriteCanvasHelper::renderMemUsage( OutputDevice& rOutDev )
671 : : {
672 : 0 : BackBufferSharedPtr pBackBuffer( mpOwningSpriteCanvas->getBackBuffer() );
673 : :
674 : 0 : if( mpRedrawManager &&
675 : 0 : pBackBuffer )
676 : : {
677 : 0 : double nPixel(0.0);
678 : :
679 : : // accumulate pixel count for each sprite into fCount
680 : : mpRedrawManager->forEachSprite( ::boost::bind(
681 : : makeAdder(nPixel,1.0),
682 : : ::boost::bind(
683 : : &calcNumPixel,
684 : 0 : _1 ) ) );
685 : :
686 : : static const int NUM_VIRDEV(2);
687 : : static const int BYTES_PER_PIXEL(3);
688 : :
689 : 0 : const Size& rVDevSize( maVDev->GetOutputSizePixel() );
690 : 0 : const Size& rBackBufferSize( pBackBuffer->getOutDev().GetOutputSizePixel() );
691 : :
692 : : const double nMemUsage( nPixel * NUM_VIRDEV * BYTES_PER_PIXEL +
693 : 0 : rVDevSize.Width()*rVDevSize.Height() * BYTES_PER_PIXEL +
694 : 0 : rBackBufferSize.Width()*rBackBufferSize.Height() * BYTES_PER_PIXEL );
695 : :
696 : : ::rtl::OUString text( ::rtl::math::doubleToUString( nMemUsage / 1048576.0,
697 : : rtl_math_StringFormat_F,
698 : 0 : 2,'.',NULL,' ') );
699 : :
700 : : // pad with leading space
701 : 0 : while( text.getLength() < 4 )
702 : 0 : text = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM (" ")) + text;
703 : :
704 : : text = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM ("Mem: ")) +
705 : : text +
706 : 0 : ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM ("MB"));
707 : :
708 : : renderInfoText( rOutDev,
709 : : text,
710 : 0 : Point(0, 60) );
711 : 0 : }
712 : 0 : }
713 : 0 : }
714 : :
715 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|