1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include <canvas/debug.hxx>
21 #include <canvas/verbosetrace.hxx>
22 #include <canvas/canvastools.hxx>
23 #include <tools/diagnose_ex.h>
25 #include <vcl/canvastools.hxx>
27 #include <comphelper/scopeguard.hxx>
29 #include <basegfx/range/b2drectangle.hxx>
30 #include <basegfx/tools/canvastools.hxx>
32 #include <boost/cast.hpp>
34 #include "cairo_spritecanvashelper.hxx"
35 #include "cairo_canvascustomsprite.hxx"
37 using namespace ::cairo
;
38 using namespace ::com::sun::star
;
44 /** Sprite redraw at original position
46 Used to repaint the whole canvas (background and all
49 void spriteRedraw( const CairoSharedPtr
& pCairo
,
50 const ::canvas::Sprite::Reference
& rSprite
)
52 // downcast to derived cairocanvas::Sprite interface, which
53 // provides the actual redraw methods.
54 ::boost::polymorphic_downcast
< Sprite
* >(rSprite
.get())->redraw( pCairo
, true);
57 void repaintBackground( const CairoSharedPtr
& pCairo
,
58 const SurfaceSharedPtr
& pBackgroundSurface
,
59 const ::basegfx::B2DRange
& rArea
)
61 cairo_save( pCairo
.get() );
62 cairo_rectangle( pCairo
.get(), ceil( rArea
.getMinX() ), ceil( rArea
.getMinY() ),
63 floor( rArea
.getWidth() ), floor( rArea
.getHeight() ) );
64 cairo_clip( pCairo
.get() );
65 cairo_set_source_surface( pCairo
.get(), pBackgroundSurface
->getCairoSurface().get(), 0, 0 );
66 cairo_set_operator( pCairo
.get(), CAIRO_OPERATOR_SOURCE
);
67 cairo_paint( pCairo
.get() );
68 cairo_restore( pCairo
.get() );
71 void opaqueUpdateSpriteArea( const ::canvas::Sprite::Reference
& rSprite
,
72 const CairoSharedPtr
& pCairo
,
73 const ::basegfx::B2IRange
& rArea
)
75 // clip output to actual update region (otherwise a)
76 // wouldn't save much render time, and b) will clutter
77 // scrolled sprite content outside this area)
78 cairo_save( pCairo
.get() );
79 cairo_rectangle( pCairo
.get(), rArea
.getMinX(), rArea
.getMinY(),
80 sal::static_int_cast
<sal_Int32
>(rArea
.getWidth()),
81 sal::static_int_cast
<sal_Int32
>(rArea
.getHeight()) );
82 cairo_clip( pCairo
.get() );
84 // repaint affected sprite directly to output device (at
85 // the actual screen output position)
86 // rendering directly to device buffer
87 ::boost::polymorphic_downcast
< Sprite
* >( rSprite
.get() )->redraw( pCairo
, false );
89 cairo_restore( pCairo
.get() );
92 /** Repaint sprite at original position
94 Used for opaque updates, which render directly to the
97 void spriteRedrawStub( const CairoSharedPtr
& pCairo
,
98 const ::canvas::Sprite::Reference
& rSprite
)
102 ::boost::polymorphic_downcast
< Sprite
* >( rSprite
.get() )->redraw( pCairo
, false );
106 /** Repaint sprite at given position
108 Used for generic update, which renders into device buffer.
110 void spriteRedrawStub2( const CairoSharedPtr
& pCairo
,
111 const ::canvas::Sprite::Reference
& rSprite
)
115 ::boost::polymorphic_downcast
< Sprite
* >( rSprite
.get() )->redraw( pCairo
, true );
119 /** Repaint sprite at original position
121 Used for opaque updates from scrollUpdate(), which render
122 directly to the front buffer.
124 void spriteRedrawStub3( const CairoSharedPtr
& pCairo
,
125 const ::canvas::SpriteRedrawManager::AreaComponent
& rComponent
)
127 const ::canvas::Sprite::Reference
& rSprite( rComponent
.second
.getSprite() );
130 ::boost::polymorphic_downcast
< Sprite
* >( rSprite
.get() )->redraw( pCairo
, false );
134 SpriteCanvasHelper::SpriteCanvasHelper() :
135 mpRedrawManager( NULL
),
136 mpOwningSpriteCanvas( NULL
),
137 mpCompositingSurface(),
138 maCompositingSurfaceSize(),
139 mbCompositingSurfaceDirty(true)
143 void SpriteCanvasHelper::init( ::canvas::SpriteRedrawManager
& rManager
,
144 SpriteCanvas
& rDevice
,
145 const ::basegfx::B2ISize
& rSize
)
147 mpRedrawManager
= &rManager
;
148 mpOwningSpriteCanvas
= &rDevice
;
150 CanvasHelper::init( rSize
, rDevice
, &rDevice
);
153 void SpriteCanvasHelper::disposing()
155 mpCompositingSurface
.reset();
156 mpOwningSpriteCanvas
= NULL
;
157 mpRedrawManager
= NULL
;
160 CanvasHelper::disposing();
163 uno::Reference
< rendering::XAnimatedSprite
> SpriteCanvasHelper::createSpriteFromAnimation(
164 const uno::Reference
< rendering::XAnimation
>& )
166 return uno::Reference
< rendering::XAnimatedSprite
>();
169 uno::Reference
< rendering::XAnimatedSprite
> SpriteCanvasHelper::createSpriteFromBitmaps(
170 const uno::Sequence
< uno::Reference
< rendering::XBitmap
> >& /*animationBitmaps*/,
171 sal_Int8
/*interpolationMode*/ )
173 return uno::Reference
< rendering::XAnimatedSprite
>();
176 uno::Reference
< rendering::XCustomSprite
> SpriteCanvasHelper::createCustomSprite( const geometry::RealSize2D
& spriteSize
)
178 if( !mpRedrawManager
)
179 return uno::Reference
< rendering::XCustomSprite
>(); // we're disposed
181 return uno::Reference
< rendering::XCustomSprite
>(
182 new CanvasCustomSprite( spriteSize
,
183 mpOwningSpriteCanvas
) );
186 uno::Reference
< rendering::XSprite
> SpriteCanvasHelper::createClonedSprite(
187 const uno::Reference
< rendering::XSprite
>& )
189 return uno::Reference
< rendering::XSprite
>();
192 bool SpriteCanvasHelper::updateScreen( const ::basegfx::B2IRange
& /*rCurrArea*/,
194 bool& io_bSurfaceDirty
)
196 if( !mpRedrawManager
||
197 !mpOwningSpriteCanvas
||
198 !mpOwningSpriteCanvas
->getWindowSurface() ||
199 !mpOwningSpriteCanvas
->getBufferSurface() )
201 return false; // disposed, or otherwise dysfunctional
204 SAL_INFO("canvas.cairo", "SpriteCanvasHelper::updateScreen called");
206 const ::basegfx::B2ISize
& rSize
= mpOwningSpriteCanvas
->getSizePixel();
208 // force compositing surface to be available before using it
209 // inside forEachSpriteArea
210 SurfaceSharedPtr pCompositingSurface
= getCompositingSurface(rSize
);
211 SurfaceSharedPtr pWindowSurface
= mpOwningSpriteCanvas
->getWindowSurface();
212 CairoSharedPtr pCompositingCairo
= pCompositingSurface
->getCairo();
213 CairoSharedPtr pWindowCairo
= pWindowSurface
->getCairo();
215 // TODO(P1): Might be worthwile to track areas of background
217 if( !bUpdateAll
&& !io_bSurfaceDirty
&& !mbCompositingSurfaceDirty
)
219 // background has not changed, so we're free to optimize
220 // repaint to areas where a sprite has changed
222 // process each independent area of overlapping sprites
224 mpRedrawManager
->forEachSpriteArea( *this );
228 SAL_INFO("canvas.cairo", "SpriteCanvasHelper::updateScreen update ALL");
230 // background has changed, so we currently have no choice
231 // but repaint everything (or caller requested that)
233 cairo_rectangle( pCompositingCairo
.get(), 0, 0, rSize
.getX(), rSize
.getY() );
234 cairo_clip( pCompositingCairo
.get() );
235 cairo_save( pCompositingCairo
.get() );
236 cairo_set_source_surface( pCompositingCairo
.get(),
237 mpOwningSpriteCanvas
->getBufferSurface()->getCairoSurface().get(),
239 cairo_set_operator( pCompositingCairo
.get(), CAIRO_OPERATOR_SOURCE
);
240 cairo_paint( pCompositingCairo
.get() );
241 cairo_restore( pCompositingCairo
.get() );
243 // repaint all active sprites on top of background into
245 mpRedrawManager
->forEachSprite(
248 boost::cref(pCompositingCairo
),
252 cairo_rectangle( pWindowCairo
.get(), 0, 0, rSize
.getX(), rSize
.getY() );
253 cairo_clip( pWindowCairo
.get() );
254 cairo_set_source_surface( pWindowCairo
.get(),
255 pCompositingSurface
->getCairoSurface().get(),
257 cairo_set_operator( pWindowCairo
.get(), CAIRO_OPERATOR_SOURCE
);
258 cairo_paint( pWindowCairo
.get() );
261 // change record vector must be cleared, for the next turn of
262 // rendering and sprite changing
263 mpRedrawManager
->clearChangeRecords();
265 mbCompositingSurfaceDirty
= false;
266 io_bSurfaceDirty
= false;
269 mpOwningSpriteCanvas
->flush();
274 void SpriteCanvasHelper::backgroundPaint( const ::basegfx::B2DRange
& rUpdateRect
)
276 if( mpOwningSpriteCanvas
&& mpCompositingSurface
)
277 repaintBackground( mpCompositingSurface
->getCairo(),
278 mpOwningSpriteCanvas
->getBufferSurface(),
282 void SpriteCanvasHelper::scrollUpdate( const ::basegfx::B2DRange
& rMoveStart
,
283 const ::basegfx::B2DRange
& rMoveEnd
,
284 const ::canvas::SpriteRedrawManager::UpdateArea
& rUpdateArea
)
286 ENSURE_OR_THROW( mpOwningSpriteCanvas
&&
287 mpOwningSpriteCanvas
->getBufferSurface(),
288 "SpriteCanvasHelper::scrollUpdate(): NULL device pointer " );
290 SAL_INFO("canvas.cairo", "SpriteCanvasHelper::scrollUpdate called");
292 const ::basegfx::B2ISize
& rSize
= mpOwningSpriteCanvas
->getSizePixel();
293 const ::basegfx::B2IRange
aOutputBounds( 0,0,
297 SurfaceSharedPtr pCompositingSurface
= getCompositingSurface(rSize
);
298 SurfaceSharedPtr pWindowSurface
= mpOwningSpriteCanvas
->getWindowSurface();
299 CairoSharedPtr pCompositingCairo
= pCompositingSurface
->getCairo();
300 CairoSharedPtr pWindowCairo
= pWindowSurface
->getCairo();
302 // round rectangles to integer pixel. Note: have to be
303 // extremely careful here, to avoid off-by-one errors for
304 // the destination area: otherwise, the next scroll update
305 // would copy pixel that are not supposed to be part of
307 ::basegfx::B2IRange
aSourceRect(
308 ::canvas::tools::spritePixelAreaFromB2DRange( rMoveStart
) );
309 const ::basegfx::B2IRange
& rDestRect(
310 ::canvas::tools::spritePixelAreaFromB2DRange( rMoveEnd
) );
311 ::basegfx::B2IPoint
aDestPos( rDestRect
.getMinimum() );
313 ::std::vector
< ::basegfx::B2IRange
> aUnscrollableAreas
;
315 // TODO(E3): This is plain buggy (but copies the behaviour of
316 // the old Impress slideshow) - the scrolled area might
317 // actually lie _below_ another window!
319 // clip to output bounds (cannot properly scroll stuff
320 // _outside_ our screen area)
321 if( !::canvas::tools::clipScrollArea( aSourceRect
,
326 // fully clipped scroll area: cannot simply scroll
327 // then. Perform normal opaque update (can use that, since
328 // one of the preconditions for scrollable update is
329 // opaque sprite content)
331 // repaint all affected sprites directly to output device
332 ::std::for_each( rUpdateArea
.maComponentList
.begin(),
333 rUpdateArea
.maComponentList
.end(),
336 boost::cref(pCompositingCairo
),
341 const ::basegfx::B2IVector
aSourceUpperLeftPos( aSourceRect
.getMinimum() );
343 // clip dest area (which must be inside rDestBounds)
344 ::basegfx::B2IRange
aDestRect( rDestRect
);
345 aDestRect
.intersect( aOutputBounds
);
347 ::basegfx::B2ISize
aScrollSize( aDestRect
.getWidth(), aDestRect
.getHeight() );
348 SurfaceSharedPtr
pScrollSurface( getTemporarySurface() );
349 CairoSharedPtr
pScrollCairo( pScrollSurface
->getCairo() );
351 cairo_save( pScrollCairo
.get() );
352 // scroll the current content of the compositing surface (and,
353 // thus, of the window) in temp. surface
354 cairo_set_source_surface( pScrollCairo
.get(),
355 pCompositingSurface
->getCairoSurface().get(),
356 aDestPos
.getX() - aSourceUpperLeftPos
.getX(),
357 aDestPos
.getY() - aSourceUpperLeftPos
.getY() );
358 cairo_rectangle( pScrollCairo
.get(),
359 aDestPos
.getX(), aDestPos
.getY(),
360 aScrollSize
.getX(), aScrollSize
.getY() );
361 cairo_clip( pScrollCairo
.get() );
362 cairo_set_operator( pScrollCairo
.get(), CAIRO_OPERATOR_SOURCE
);
363 cairo_paint( pScrollCairo
.get() );
364 cairo_restore( pScrollCairo
.get() );
366 cairo_save( pCompositingCairo
.get() );
367 // copy the scrolled area back onto the compositing surface
368 cairo_set_source_surface( pCompositingCairo
.get(),
369 pScrollSurface
->getCairoSurface().get(),
371 cairo_rectangle( pCompositingCairo
.get(),
372 aDestPos
.getX(), aDestPos
.getY(),
373 aScrollSize
.getX(), aScrollSize
.getY() );
374 cairo_clip( pCompositingCairo
.get() );
375 cairo_set_operator( pCompositingCairo
.get(), CAIRO_OPERATOR_SOURCE
);
376 cairo_paint( pCompositingCairo
.get() );
377 cairo_restore( pCompositingCairo
.get() );
379 const ::canvas::SpriteRedrawManager::SpriteConnectedRanges::ComponentListType::const_iterator
380 aFirst( rUpdateArea
.maComponentList
.begin() );
381 ::canvas::SpriteRedrawManager::SpriteConnectedRanges::ComponentListType::const_iterator
382 aSecond( aFirst
); ++aSecond
;
384 ENSURE_OR_THROW( aFirst
->second
.getSprite().is(),
385 "VCLCanvas::scrollUpdate(): no sprite" );
387 // repaint uncovered areas from sprite. Need to actually
388 // clip here, since we're only repainting _parts_ of the
390 ::std::for_each( aUnscrollableAreas
.begin(),
391 aUnscrollableAreas
.end(),
392 ::boost::bind( &opaqueUpdateSpriteArea
,
393 ::boost::cref(aFirst
->second
.getSprite()),
394 boost::cref(pCompositingCairo
),
398 // repaint uncovered areas from backbuffer - take the
399 // _rounded_ rectangles from above, to have the update
400 // consistent with the scroll above.
401 ::std::vector
< ::basegfx::B2DRange
> aUncoveredAreas
;
402 ::basegfx::computeSetDifference( aUncoveredAreas
,
403 rUpdateArea
.maTotalBounds
,
404 ::basegfx::B2DRange( rDestRect
) );
405 ::std::for_each( aUncoveredAreas
.begin(),
406 aUncoveredAreas
.end(),
407 ::boost::bind( &repaintBackground
,
408 boost::cref(pCompositingCairo
),
409 mpOwningSpriteCanvas
->getBufferSurface(),
412 cairo_rectangle( pWindowCairo
.get(), 0, 0, rSize
.getX(), rSize
.getY() );
413 cairo_clip( pWindowCairo
.get() );
414 cairo_set_source_surface( pWindowCairo
.get(),
415 pCompositingSurface
->getCairoSurface().get(),
417 cairo_set_operator( pWindowCairo
.get(), CAIRO_OPERATOR_SOURCE
);
418 cairo_paint( pWindowCairo
.get() );
421 void SpriteCanvasHelper::opaqueUpdate( const ::basegfx::B2DRange
& rTotalArea
,
422 const ::std::vector
< ::canvas::Sprite::Reference
>& rSortedUpdateSprites
)
424 ENSURE_OR_THROW( mpOwningSpriteCanvas
&&
425 mpOwningSpriteCanvas
->getBufferSurface(),
426 "SpriteCanvasHelper::opaqueUpdate(): NULL device pointer " );
428 SAL_INFO("canvas.cairo", "SpriteCanvasHelper::opaqueUpdate called");
430 const ::basegfx::B2ISize
& rDeviceSize
= mpOwningSpriteCanvas
->getSizePixel();
432 SurfaceSharedPtr pCompositingSurface
= getCompositingSurface(rDeviceSize
);
433 SurfaceSharedPtr pWindowSurface
= mpOwningSpriteCanvas
->getWindowSurface();
434 CairoSharedPtr pCompositingCairo
= pCompositingSurface
->getCairo();
435 CairoSharedPtr pWindowCairo
= pWindowSurface
->getCairo();
437 cairo_rectangle( pCompositingCairo
.get(), 0, 0, rDeviceSize
.getX(), rDeviceSize
.getY() );
438 cairo_clip( pCompositingCairo
.get() );
440 ::basegfx::B2DVector
aPos( ceil( rTotalArea
.getMinX() ), ceil( rTotalArea
.getMinY() ) );
441 ::basegfx::B2DVector
aSize( floor( rTotalArea
.getMaxX() - aPos
.getX() ), floor( rTotalArea
.getMaxY() - aPos
.getY() ) );
443 cairo_rectangle( pCompositingCairo
.get(), aPos
.getX(), aPos
.getY(), aSize
.getX(), aSize
.getY() );
444 cairo_clip( pCompositingCairo
.get() );
446 // repaint all affected sprites directly to output device
447 ::std::for_each( rSortedUpdateSprites
.begin(),
448 rSortedUpdateSprites
.end(),
451 boost::cref(pCompositingCairo
),
455 cairo_rectangle( pWindowCairo
.get(), 0, 0, rDeviceSize
.getX(), rDeviceSize
.getY() );
456 cairo_clip( pWindowCairo
.get() );
457 cairo_rectangle( pWindowCairo
.get(), aPos
.getX(), aPos
.getY(), aSize
.getX(), aSize
.getY() );
458 cairo_clip( pWindowCairo
.get() );
459 cairo_set_source_surface( pWindowCairo
.get(),
460 pCompositingSurface
->getCairoSurface().get(),
462 cairo_set_operator( pWindowCairo
.get(), CAIRO_OPERATOR_SOURCE
);
463 cairo_paint( pWindowCairo
.get() );
466 void SpriteCanvasHelper::genericUpdate( const ::basegfx::B2DRange
& rRequestedArea
,
467 const ::std::vector
< ::canvas::Sprite::Reference
>& rSortedUpdateSprites
)
470 SAL_INFO("canvas.cairo", "SpriteCanvasHelper::genericUpdate called");
472 ENSURE_OR_THROW( mpOwningSpriteCanvas
&&
473 mpOwningSpriteCanvas
->getBufferSurface(),
474 "SpriteCanvasHelper::genericUpdate(): NULL device pointer " );
476 // limit size of update VDev to target outdev's size
477 const ::basegfx::B2ISize
& rSize
= mpOwningSpriteCanvas
->getSizePixel();
479 SurfaceSharedPtr pCompositingSurface
= getCompositingSurface(rSize
);
480 SurfaceSharedPtr pWindowSurface
= mpOwningSpriteCanvas
->getWindowSurface();
481 CairoSharedPtr pCompositingCairo
= pCompositingSurface
->getCairo();
482 CairoSharedPtr pWindowCairo
= pWindowSurface
->getCairo();
484 // round output position towards zero. Don't want to truncate
485 // a fraction of a sprite pixel... Clip position at origin,
486 // otherwise, truncation of size below might leave visible
487 // areas uncovered by VDev.
488 const Point
aOutputPosition(
489 ::std::max( sal_Int32( 0 ),
490 static_cast< sal_Int32
>(rRequestedArea
.getMinX()) ),
491 ::std::max( sal_Int32( 0 ),
492 static_cast< sal_Int32
>(rRequestedArea
.getMinY()) ) );
493 // round output size towards +infty. Don't want to truncate a
494 // fraction of a sprite pixel... Limit size of VDev to output
496 const Size
aOutputSize(
497 ::std::min( rSize
.getX(),
498 ::canvas::tools::roundUp( rRequestedArea
.getMaxX() - aOutputPosition
.X()) ),
499 ::std::min( rSize
.getY(),
500 ::canvas::tools::roundUp( rRequestedArea
.getMaxY() - aOutputPosition
.Y()) ) );
502 cairo_rectangle( pCompositingCairo
.get(), aOutputPosition
.X(), aOutputPosition
.Y(), aOutputSize
.Width(), aOutputSize
.Height() );
503 cairo_clip( pCompositingCairo
.get() );
506 cairo_save( pCompositingCairo
.get() );
507 cairo_set_source_surface( pCompositingCairo
.get(),
508 mpOwningSpriteCanvas
->getBufferSurface()->getCairoSurface().get(),
510 cairo_set_operator( pCompositingCairo
.get(), CAIRO_OPERATOR_SOURCE
);
511 cairo_paint( pCompositingCairo
.get() );
512 cairo_restore( pCompositingCairo
.get() );
514 // repaint all affected sprites on top of background into
516 ::std::for_each( rSortedUpdateSprites
.begin(),
517 rSortedUpdateSprites
.end(),
518 ::boost::bind( &spriteRedrawStub2
,
519 boost::cref(pCompositingCairo
),
523 cairo_rectangle( pWindowCairo
.get(), aOutputPosition
.X(), aOutputPosition
.Y(), aOutputSize
.Width(), aOutputSize
.Height() );
524 cairo_clip( pWindowCairo
.get() );
525 cairo_set_source_surface( pWindowCairo
.get(),
526 pCompositingSurface
->getCairoSurface().get(),
528 cairo_set_operator( pWindowCairo
.get(), CAIRO_OPERATOR_SOURCE
);
529 cairo_paint( pWindowCairo
.get() );
532 ::cairo::SurfaceSharedPtr
SpriteCanvasHelper::getCompositingSurface( const ::basegfx::B2ISize
& rNeededSize
)
534 if( rNeededSize
.getX() > maCompositingSurfaceSize
.getX() ||
535 rNeededSize
.getY() > maCompositingSurfaceSize
.getY() )
537 // need to give buffer more size
538 mpCompositingSurface
.reset();
541 if( !mpCompositingSurface
)
543 mpCompositingSurface
= createSurface( rNeededSize
);
544 maCompositingSurfaceSize
= rNeededSize
;
545 mbCompositingSurfaceDirty
= true;
546 mpTemporarySurface
.reset();
549 return mpCompositingSurface
;
552 ::cairo::SurfaceSharedPtr
SpriteCanvasHelper::getTemporarySurface()
554 if ( !mpTemporarySurface
)
555 mpTemporarySurface
= createSurface( maCompositingSurfaceSize
);
556 return mpTemporarySurface
;
559 ::cairo::SurfaceSharedPtr
SpriteCanvasHelper::createSurface( const ::basegfx::B2ISize
& rNeededSize
) const
561 return mpOwningSpriteCanvas
->getWindowSurface()->getSimilar(
563 rNeededSize
.getX(), rNeededSize
.getY() );
567 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */