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 <sal/config.h>
21 #include <sal/log.hxx>
23 #include <boost/cast.hpp>
25 #include <basegfx/range/b2irange.hxx>
26 #include <comphelper/diagnose_ex.hxx>
28 #include <canvas/canvastools.hxx>
32 #include "cairo_canvascustomsprite.hxx"
33 #include "cairo_spritecanvashelper.hxx"
35 using namespace ::cairo
;
36 using namespace ::com::sun::star
;
42 /** Sprite redraw at original position
44 Used to repaint the whole canvas (background and all
47 void spriteRedraw( const CairoSharedPtr
& pCairo
,
48 const ::canvas::Sprite::Reference
& rSprite
)
50 // downcast to derived cairocanvas::Sprite interface, which
51 // provides the actual redraw methods.
52 ::boost::polymorphic_downcast
< Sprite
* >(rSprite
.get())->redraw( pCairo
, true);
55 void repaintBackground( const CairoSharedPtr
& pCairo
,
56 const SurfaceSharedPtr
& pBackgroundSurface
,
57 const ::basegfx::B2DRange
& rArea
)
59 cairo_save( pCairo
.get() );
60 cairo_rectangle( pCairo
.get(), ceil( rArea
.getMinX() ), ceil( rArea
.getMinY() ),
61 floor( rArea
.getWidth() ), floor( rArea
.getHeight() ) );
62 cairo_clip( pCairo
.get() );
63 cairo_set_source_surface( pCairo
.get(), pBackgroundSurface
->getCairoSurface().get(), 0, 0 );
64 cairo_set_operator( pCairo
.get(), CAIRO_OPERATOR_SOURCE
);
65 cairo_paint( pCairo
.get() );
66 cairo_restore( pCairo
.get() );
69 void opaqueUpdateSpriteArea( const ::canvas::Sprite::Reference
& rSprite
,
70 const CairoSharedPtr
& pCairo
,
71 const ::basegfx::B2IRange
& rArea
)
73 // clip output to actual update region (otherwise a)
74 // wouldn't save much render time, and b) will clutter
75 // scrolled sprite content outside this area)
76 cairo_save( pCairo
.get() );
77 cairo_rectangle( pCairo
.get(), rArea
.getMinX(), rArea
.getMinY(),
78 sal::static_int_cast
<sal_Int32
>(rArea
.getWidth()),
79 sal::static_int_cast
<sal_Int32
>(rArea
.getHeight()) );
80 cairo_clip( pCairo
.get() );
82 // repaint affected sprite directly to output device (at
83 // the actual screen output position)
84 // rendering directly to device buffer
85 ::boost::polymorphic_downcast
< Sprite
* >( rSprite
.get() )->redraw( pCairo
, false );
87 cairo_restore( pCairo
.get() );
91 SpriteCanvasHelper::SpriteCanvasHelper() :
92 mpRedrawManager( nullptr ),
93 mpOwningSpriteCanvas( nullptr ),
94 mbCompositingSurfaceDirty(true)
98 void SpriteCanvasHelper::init( ::canvas::SpriteRedrawManager
& rManager
,
99 SpriteCanvas
& rDevice
,
100 const ::basegfx::B2ISize
& rSize
)
102 mpRedrawManager
= &rManager
;
103 mpOwningSpriteCanvas
= &rDevice
;
105 CanvasHelper::init( rSize
, rDevice
, &rDevice
);
108 void SpriteCanvasHelper::disposing()
110 mpCompositingSurface
.reset();
111 mpOwningSpriteCanvas
= nullptr;
112 mpRedrawManager
= nullptr;
115 CanvasHelper::disposing();
118 uno::Reference
< rendering::XAnimatedSprite
> SpriteCanvasHelper::createSpriteFromAnimation(
119 const uno::Reference
< rendering::XAnimation
>& )
121 return uno::Reference
< rendering::XAnimatedSprite
>();
124 uno::Reference
< rendering::XAnimatedSprite
> SpriteCanvasHelper::createSpriteFromBitmaps(
125 const uno::Sequence
< uno::Reference
< rendering::XBitmap
> >& /*animationBitmaps*/,
126 sal_Int8
/*interpolationMode*/ )
128 return uno::Reference
< rendering::XAnimatedSprite
>();
131 uno::Reference
< rendering::XCustomSprite
> SpriteCanvasHelper::createCustomSprite( const geometry::RealSize2D
& spriteSize
)
133 if( !mpRedrawManager
)
134 return uno::Reference
< rendering::XCustomSprite
>(); // we're disposed
136 return uno::Reference
< rendering::XCustomSprite
>(
137 new CanvasCustomSprite( spriteSize
,
138 mpOwningSpriteCanvas
) );
141 uno::Reference
< rendering::XSprite
> SpriteCanvasHelper::createClonedSprite(
142 const uno::Reference
< rendering::XSprite
>& )
144 return uno::Reference
< rendering::XSprite
>();
147 bool SpriteCanvasHelper::updateScreen( const ::basegfx::B2IRange
& /*rCurrArea*/,
149 bool& io_bSurfaceDirty
)
151 if( !mpRedrawManager
||
152 !mpOwningSpriteCanvas
||
153 !mpOwningSpriteCanvas
->getWindowSurface() ||
154 !mpOwningSpriteCanvas
->getBufferSurface() )
156 return false; // disposed, or otherwise dysfunctional
159 SAL_INFO("canvas.cairo", "SpriteCanvasHelper::updateScreen called");
161 const ::basegfx::B2ISize
& rSize
= mpOwningSpriteCanvas
->getSizePixel();
163 // force compositing surface to be available before using it
164 // inside forEachSpriteArea
165 SurfaceSharedPtr pCompositingSurface
= getCompositingSurface(rSize
);
166 SurfaceSharedPtr pWindowSurface
= mpOwningSpriteCanvas
->getWindowSurface();
167 CairoSharedPtr pCompositingCairo
= pCompositingSurface
->getCairo();
168 CairoSharedPtr pWindowCairo
= pWindowSurface
->getCairo();
170 // TODO(P1): Might be worthwhile to track areas of background
172 if( !bUpdateAll
&& !io_bSurfaceDirty
&& !mbCompositingSurfaceDirty
)
174 // background has not changed, so we're free to optimize
175 // repaint to areas where a sprite has changed
177 // process each independent area of overlapping sprites
179 mpRedrawManager
->forEachSpriteArea( *this );
183 SAL_INFO("canvas.cairo", "SpriteCanvasHelper::updateScreen update ALL");
185 // background has changed, so we currently have no choice
186 // but repaint everything (or caller requested that)
188 cairo_rectangle( pCompositingCairo
.get(), 0, 0, rSize
.getWidth(), rSize
.getHeight() );
189 cairo_clip( pCompositingCairo
.get() );
190 cairo_save( pCompositingCairo
.get() );
191 cairo_set_source_surface( pCompositingCairo
.get(),
192 mpOwningSpriteCanvas
->getBufferSurface()->getCairoSurface().get(),
194 cairo_set_operator( pCompositingCairo
.get(), CAIRO_OPERATOR_SOURCE
);
195 cairo_paint( pCompositingCairo
.get() );
196 cairo_restore( pCompositingCairo
.get() );
198 // repaint all active sprites on top of background into
200 mpRedrawManager
->forEachSprite(
201 [&pCompositingCairo
]( const Sprite::Reference rSprite
)
202 { spriteRedraw( pCompositingCairo
, rSprite
); }
206 cairo_rectangle( pWindowCairo
.get(), 0, 0, rSize
.getWidth(), rSize
.getHeight() );
207 cairo_clip( pWindowCairo
.get() );
208 cairo_set_source_surface( pWindowCairo
.get(),
209 pCompositingSurface
->getCairoSurface().get(),
211 cairo_set_operator( pWindowCairo
.get(), CAIRO_OPERATOR_SOURCE
);
212 cairo_paint( pWindowCairo
.get() );
215 // change record vector must be cleared, for the next turn of
216 // rendering and sprite changing
217 mpRedrawManager
->clearChangeRecords();
219 mbCompositingSurfaceDirty
= false;
220 io_bSurfaceDirty
= false;
223 mpOwningSpriteCanvas
->flush();
228 void SpriteCanvasHelper::backgroundPaint( const ::basegfx::B2DRange
& rUpdateRect
)
230 if( mpOwningSpriteCanvas
&& mpCompositingSurface
)
231 repaintBackground( mpCompositingSurface
->getCairo(),
232 mpOwningSpriteCanvas
->getBufferSurface(),
236 void SpriteCanvasHelper::scrollUpdate( const ::basegfx::B2DRange
& rMoveStart
,
237 const ::basegfx::B2DRange
& rMoveEnd
,
238 const ::canvas::SpriteRedrawManager::UpdateArea
& rUpdateArea
)
240 ENSURE_OR_THROW( mpOwningSpriteCanvas
&&
241 mpOwningSpriteCanvas
->getBufferSurface(),
242 "SpriteCanvasHelper::scrollUpdate(): NULL device pointer " );
244 SAL_INFO("canvas.cairo", "SpriteCanvasHelper::scrollUpdate called");
246 const ::basegfx::B2ISize
& rSize
= mpOwningSpriteCanvas
->getSizePixel();
247 const ::basegfx::B2IRange
aOutputBounds( 0,0,
251 SurfaceSharedPtr pCompositingSurface
= getCompositingSurface(rSize
);
252 SurfaceSharedPtr pWindowSurface
= mpOwningSpriteCanvas
->getWindowSurface();
253 CairoSharedPtr pCompositingCairo
= pCompositingSurface
->getCairo();
254 CairoSharedPtr pWindowCairo
= pWindowSurface
->getCairo();
256 // round rectangles to integer pixel. Note: have to be
257 // extremely careful here, to avoid off-by-one errors for
258 // the destination area: otherwise, the next scroll update
259 // would copy pixel that are not supposed to be part of
261 ::basegfx::B2IRange
aSourceRect(
262 ::canvas::tools::spritePixelAreaFromB2DRange( rMoveStart
) );
263 const ::basegfx::B2IRange
aDestRect(
264 ::canvas::tools::spritePixelAreaFromB2DRange( rMoveEnd
) );
265 ::basegfx::B2IPoint
aDestPos( aDestRect
.getMinimum() );
267 std::vector
< ::basegfx::B2IRange
> aUnscrollableAreas
;
269 // TODO(E3): This is plain buggy (but copies the behaviour of
270 // the old Impress slideshow) - the scrolled area might
271 // actually lie _below_ another window!
273 // clip to output bounds (cannot properly scroll stuff
274 // _outside_ our screen area)
275 if( !::canvas::tools::clipScrollArea( aSourceRect
,
280 // fully clipped scroll area: cannot simply scroll
281 // then. Perform normal opaque update (can use that, since
282 // one of the preconditions for scrollable update is
283 // opaque sprite content)
285 // repaint all affected sprites directly to output device
286 for( const auto& rComponent
: rUpdateArea
.maComponentList
)
288 const ::canvas::Sprite::Reference
& rSprite( rComponent
.second
.getSprite() );
290 ::boost::polymorphic_downcast
< Sprite
* >( rSprite
.get() )->redraw(
291 pCompositingCairo
, true );
296 const ::basegfx::B2IVector
aSourceUpperLeftPos( aSourceRect
.getMinimum() );
298 // clip dest area (which must be inside rDestBounds)
299 ::basegfx::B2IRange
aClipDestRect( aDestRect
);
300 aClipDestRect
.intersect( aOutputBounds
);
302 ::basegfx::B2ISize
aScrollSize( aClipDestRect
.getWidth(), aClipDestRect
.getHeight() );
303 SurfaceSharedPtr
pScrollSurface( getTemporarySurface() );
304 CairoSharedPtr
pScrollCairo( pScrollSurface
->getCairo() );
306 cairo_save( pScrollCairo
.get() );
307 // scroll the current content of the compositing surface (and,
308 // thus, of the window) in temp. surface
309 cairo_set_source_surface( pScrollCairo
.get(),
310 pCompositingSurface
->getCairoSurface().get(),
311 aDestPos
.getX() - aSourceUpperLeftPos
.getX(),
312 aDestPos
.getY() - aSourceUpperLeftPos
.getY() );
313 cairo_rectangle( pScrollCairo
.get(),
314 aDestPos
.getX(), aDestPos
.getY(),
315 aScrollSize
.getWidth(), aScrollSize
.getHeight() );
316 cairo_clip( pScrollCairo
.get() );
317 cairo_set_operator( pScrollCairo
.get(), CAIRO_OPERATOR_SOURCE
);
318 cairo_paint( pScrollCairo
.get() );
319 cairo_restore( pScrollCairo
.get() );
321 cairo_save( pCompositingCairo
.get() );
322 // copy the scrolled area back onto the compositing surface
323 cairo_set_source_surface( pCompositingCairo
.get(),
324 pScrollSurface
->getCairoSurface().get(),
326 cairo_rectangle( pCompositingCairo
.get(),
327 aDestPos
.getX(), aDestPos
.getY(),
328 aScrollSize
.getWidth(), aScrollSize
.getHeight() );
329 cairo_clip( pCompositingCairo
.get() );
330 cairo_set_operator( pCompositingCairo
.get(), CAIRO_OPERATOR_SOURCE
);
331 cairo_paint( pCompositingCairo
.get() );
332 cairo_restore( pCompositingCairo
.get() );
334 const ::canvas::SpriteRedrawManager::SpriteConnectedRanges::ComponentListType::const_iterator
335 aFirst( rUpdateArea
.maComponentList
.begin() );
336 ::canvas::SpriteRedrawManager::SpriteConnectedRanges::ComponentListType::const_iterator
340 ENSURE_OR_THROW( aFirst
->second
.getSprite().is(),
341 "VCLCanvas::scrollUpdate(): no sprite" );
343 // repaint uncovered areas from sprite. Need to actually
344 // clip here, since we're only repainting _parts_ of the
346 for( const auto& rArea
: aUnscrollableAreas
)
347 opaqueUpdateSpriteArea( aFirst
->second
.getSprite(),
348 pCompositingCairo
, rArea
);
351 // repaint uncovered areas from backbuffer - take the
352 // _rounded_ rectangles from above, to have the update
353 // consistent with the scroll above.
354 std::vector
< ::basegfx::B2DRange
> aUncoveredAreas
;
355 ::basegfx::computeSetDifference( aUncoveredAreas
,
356 rUpdateArea
.maTotalBounds
,
357 ::basegfx::B2DRange( aDestRect
) );
358 for( const auto& rArea
: aUncoveredAreas
)
359 repaintBackground( pCompositingCairo
,
360 mpOwningSpriteCanvas
->getBufferSurface(), rArea
);
362 cairo_rectangle( pWindowCairo
.get(), 0, 0, rSize
.getWidth(), rSize
.getHeight() );
363 cairo_clip( pWindowCairo
.get() );
364 cairo_set_source_surface( pWindowCairo
.get(),
365 pCompositingSurface
->getCairoSurface().get(),
367 cairo_set_operator( pWindowCairo
.get(), CAIRO_OPERATOR_SOURCE
);
368 cairo_paint( pWindowCairo
.get() );
371 void SpriteCanvasHelper::opaqueUpdate( const ::basegfx::B2DRange
& rTotalArea
,
372 const std::vector
< ::canvas::Sprite::Reference
>& rSortedUpdateSprites
)
374 ENSURE_OR_THROW( mpOwningSpriteCanvas
&&
375 mpOwningSpriteCanvas
->getBufferSurface(),
376 "SpriteCanvasHelper::opaqueUpdate(): NULL device pointer " );
378 SAL_INFO("canvas.cairo", "SpriteCanvasHelper::opaqueUpdate called");
380 const ::basegfx::B2ISize
& rDeviceSize
= mpOwningSpriteCanvas
->getSizePixel();
382 SurfaceSharedPtr pCompositingSurface
= getCompositingSurface(rDeviceSize
);
383 SurfaceSharedPtr pWindowSurface
= mpOwningSpriteCanvas
->getWindowSurface();
384 CairoSharedPtr pCompositingCairo
= pCompositingSurface
->getCairo();
385 CairoSharedPtr pWindowCairo
= pWindowSurface
->getCairo();
387 cairo_rectangle( pCompositingCairo
.get(), 0, 0, rDeviceSize
.getWidth(), rDeviceSize
.getHeight() );
388 cairo_clip( pCompositingCairo
.get() );
390 ::basegfx::B2DVector
aPos( ceil( rTotalArea
.getMinX() ), ceil( rTotalArea
.getMinY() ) );
391 ::basegfx::B2DVector
aSize( floor( rTotalArea
.getMaxX() - aPos
.getX() ), floor( rTotalArea
.getMaxY() - aPos
.getY() ) );
393 cairo_rectangle( pCompositingCairo
.get(), aPos
.getX(), aPos
.getY(), aSize
.getX(), aSize
.getY() );
394 cairo_clip( pCompositingCairo
.get() );
396 // repaint all affected sprites directly to output device
397 for( const auto& rSprite
: rSortedUpdateSprites
)
400 ::boost::polymorphic_downcast
< Sprite
* >( rSprite
.get() )->redraw(
401 pCompositingCairo
, false );
405 cairo_rectangle( pWindowCairo
.get(), 0, 0, rDeviceSize
.getWidth(), rDeviceSize
.getHeight() );
406 cairo_clip( pWindowCairo
.get() );
407 cairo_rectangle( pWindowCairo
.get(), aPos
.getX(), aPos
.getY(), aSize
.getX(), aSize
.getY() );
408 cairo_clip( pWindowCairo
.get() );
409 cairo_set_source_surface( pWindowCairo
.get(),
410 pCompositingSurface
->getCairoSurface().get(),
412 cairo_set_operator( pWindowCairo
.get(), CAIRO_OPERATOR_SOURCE
);
413 cairo_paint( pWindowCairo
.get() );
416 void SpriteCanvasHelper::genericUpdate( const ::basegfx::B2DRange
& rRequestedArea
,
417 const std::vector
< ::canvas::Sprite::Reference
>& rSortedUpdateSprites
)
420 SAL_INFO("canvas.cairo", "SpriteCanvasHelper::genericUpdate called");
422 ENSURE_OR_THROW( mpOwningSpriteCanvas
&&
423 mpOwningSpriteCanvas
->getBufferSurface(),
424 "SpriteCanvasHelper::genericUpdate(): NULL device pointer " );
426 // limit size of update VDev to target outdev's size
427 const ::basegfx::B2ISize
& rSize
= mpOwningSpriteCanvas
->getSizePixel();
429 SurfaceSharedPtr pCompositingSurface
= getCompositingSurface(rSize
);
430 SurfaceSharedPtr pWindowSurface
= mpOwningSpriteCanvas
->getWindowSurface();
431 CairoSharedPtr pCompositingCairo
= pCompositingSurface
->getCairo();
432 CairoSharedPtr pWindowCairo
= pWindowSurface
->getCairo();
434 // round output position towards zero. Don't want to truncate
435 // a fraction of a sprite pixel... Clip position at origin,
436 // otherwise, truncation of size below might leave visible
437 // areas uncovered by VDev.
438 const Point
aOutputPosition(
439 std::max( sal_Int32( 0 ),
440 static_cast< sal_Int32
>(rRequestedArea
.getMinX()) ),
441 std::max( sal_Int32( 0 ),
442 static_cast< sal_Int32
>(rRequestedArea
.getMinY()) ) );
443 // round output size towards +infty. Don't want to truncate a
444 // fraction of a sprite pixel... Limit size of VDev to output
446 const Size
aOutputSize(
447 std::min( rSize
.getWidth(),
448 ::canvas::tools::roundUp( rRequestedArea
.getMaxX() - aOutputPosition
.X()) ),
449 std::min( rSize
.getHeight(),
450 ::canvas::tools::roundUp( rRequestedArea
.getMaxY() - aOutputPosition
.Y()) ) );
452 cairo_rectangle( pCompositingCairo
.get(), aOutputPosition
.X(), aOutputPosition
.Y(), aOutputSize
.Width(), aOutputSize
.Height() );
453 cairo_clip( pCompositingCairo
.get() );
456 cairo_save( pCompositingCairo
.get() );
457 cairo_set_source_surface( pCompositingCairo
.get(),
458 mpOwningSpriteCanvas
->getBufferSurface()->getCairoSurface().get(),
460 cairo_set_operator( pCompositingCairo
.get(), CAIRO_OPERATOR_SOURCE
);
461 cairo_paint( pCompositingCairo
.get() );
462 cairo_restore( pCompositingCairo
.get() );
464 // repaint all affected sprites on top of background into
466 for( const auto& rSprite
: rSortedUpdateSprites
)
469 ::boost::polymorphic_downcast
< Sprite
* >( rSprite
.get() )->redraw(
470 pCompositingCairo
, true );
474 cairo_rectangle( pWindowCairo
.get(), aOutputPosition
.X(), aOutputPosition
.Y(), aOutputSize
.Width(), aOutputSize
.Height() );
475 cairo_clip( pWindowCairo
.get() );
476 cairo_set_source_surface( pWindowCairo
.get(),
477 pCompositingSurface
->getCairoSurface().get(),
479 cairo_set_operator( pWindowCairo
.get(), CAIRO_OPERATOR_SOURCE
);
480 cairo_paint( pWindowCairo
.get() );
483 ::cairo::SurfaceSharedPtr
const & SpriteCanvasHelper::getCompositingSurface( const ::basegfx::B2ISize
& rNeededSize
)
485 if( rNeededSize
.getWidth() > maCompositingSurfaceSize
.getWidth() ||
486 rNeededSize
.getHeight() > maCompositingSurfaceSize
.getHeight() )
488 // need to give buffer more size
489 mpCompositingSurface
.reset();
492 if( !mpCompositingSurface
)
494 mpCompositingSurface
= createSurface( rNeededSize
);
495 maCompositingSurfaceSize
= rNeededSize
;
496 mbCompositingSurfaceDirty
= true;
497 mpTemporarySurface
.reset();
500 return mpCompositingSurface
;
503 ::cairo::SurfaceSharedPtr
const & SpriteCanvasHelper::getTemporarySurface()
505 if ( !mpTemporarySurface
)
506 mpTemporarySurface
= createSurface( maCompositingSurfaceSize
);
507 return mpTemporarySurface
;
510 ::cairo::SurfaceSharedPtr
SpriteCanvasHelper::createSurface( const ::basegfx::B2ISize
& rNeededSize
) const
512 return mpOwningSpriteCanvas
->getWindowSurface()->getSimilar(
514 rNeededSize
.getWidth(), rNeededSize
.getHeight() );
518 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */