update dev300-m58
[ooovba.git] / canvas / source / cairo / cairo_spritecanvashelper.cxx
blob2513cdb7fdfd07dd43442686b08c6007e82a9e69
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: cairo_spritecanvashelper.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_canvas.hxx"
34 #include <canvas/debug.hxx>
35 #include <canvas/verbosetrace.hxx>
36 #include <canvas/canvastools.hxx>
37 #include <tools/diagnose_ex.h>
39 #include <vcl/canvastools.hxx>
41 #include <comphelper/scopeguard.hxx>
43 #include <basegfx/range/b2drectangle.hxx>
44 #include <basegfx/tools/canvastools.hxx>
46 #include <boost/cast.hpp>
48 #include "cairo_spritecanvashelper.hxx"
49 #include "cairo_canvascustomsprite.hxx"
51 using namespace ::cairo;
52 using namespace ::com::sun::star;
54 namespace cairocanvas
56 namespace
58 /** Sprite redraw at original position
60 Used to repaint the whole canvas (background and all
61 sprites)
63 void spriteRedraw( const CairoSharedPtr& pCairo,
64 const ::canvas::Sprite::Reference& rSprite )
66 // downcast to derived cairocanvas::Sprite interface, which
67 // provides the actual redraw methods.
68 ::boost::polymorphic_downcast< Sprite* >(rSprite.get())->redraw( pCairo, true);
71 void repaintBackground( const CairoSharedPtr& pCairo,
72 const SurfaceSharedPtr& pBackgroundSurface,
73 const ::basegfx::B2DRange& rArea )
75 cairo_save( pCairo.get() );
76 cairo_rectangle( pCairo.get(), ceil( rArea.getMinX() ), ceil( rArea.getMinY() ),
77 floor( rArea.getWidth() ), floor( rArea.getHeight() ) );
78 cairo_clip( pCairo.get() );
79 cairo_set_source_surface( pCairo.get(), pBackgroundSurface->getCairoSurface().get(), 0, 0 );
80 cairo_set_operator( pCairo.get(), CAIRO_OPERATOR_SOURCE );
81 cairo_paint( pCairo.get() );
82 cairo_restore( pCairo.get() );
85 void opaqueUpdateSpriteArea( const ::canvas::Sprite::Reference& rSprite,
86 const CairoSharedPtr& pCairo,
87 const ::basegfx::B2IRange& rArea )
89 // clip output to actual update region (otherwise a)
90 // wouldn't save much render time, and b) will clutter
91 // scrolled sprite content outside this area)
92 cairo_save( pCairo.get() );
93 cairo_rectangle( pCairo.get(), rArea.getMinX(), rArea.getMinY(),
94 sal::static_int_cast<sal_Int32>(rArea.getWidth()),
95 sal::static_int_cast<sal_Int32>(rArea.getHeight()) );
96 cairo_clip( pCairo.get() );
98 // repaint affected sprite directly to output device (at
99 // the actual screen output position)
100 // rendering directly to device buffer
101 ::boost::polymorphic_downcast< Sprite* >( rSprite.get() )->redraw( pCairo, false );
103 cairo_restore( pCairo.get() );
106 /** Repaint sprite at original position
108 Used for opaque updates, which render directly to the
109 device buffer.
111 void spriteRedrawStub( const CairoSharedPtr& pCairo,
112 const ::canvas::Sprite::Reference& rSprite )
114 if( rSprite.is() )
116 ::boost::polymorphic_downcast< Sprite* >( rSprite.get() )->redraw( pCairo, false );
120 /** Repaint sprite at given position
122 Used for generic update, which renders into device buffer.
124 void spriteRedrawStub2( const CairoSharedPtr& pCairo,
125 const ::canvas::Sprite::Reference& rSprite )
127 if( rSprite.is() )
129 ::boost::polymorphic_downcast< Sprite* >( rSprite.get() )->redraw( pCairo, true );;
133 /** Repaint sprite at original position
135 Used for opaque updates from scrollUpdate(), which render
136 directly to the front buffer.
138 void spriteRedrawStub3( const CairoSharedPtr& pCairo,
139 const ::canvas::SpriteRedrawManager::AreaComponent& rComponent )
141 const ::canvas::Sprite::Reference& rSprite( rComponent.second.getSprite() );
143 if( rSprite.is() )
144 ::boost::polymorphic_downcast< Sprite* >( rSprite.get() )->redraw( pCairo, false );
148 SpriteCanvasHelper::SpriteCanvasHelper() :
149 mpRedrawManager( NULL ),
150 mpOwningSpriteCanvas( NULL ),
151 mpCompositingSurface(),
152 maCompositingSurfaceSize()
156 void SpriteCanvasHelper::init( ::canvas::SpriteRedrawManager& rManager,
157 SpriteCanvas& rDevice,
158 const ::basegfx::B2ISize& rSize )
160 mpRedrawManager = &rManager;
161 mpOwningSpriteCanvas = &rDevice;
163 CanvasHelper::init( rSize, rDevice, &rDevice );
166 void SpriteCanvasHelper::disposing()
168 mpCompositingSurface.reset();
169 mpOwningSpriteCanvas = NULL;
170 mpRedrawManager = NULL;
172 // forward to base
173 CanvasHelper::disposing();
176 uno::Reference< rendering::XAnimatedSprite > SpriteCanvasHelper::createSpriteFromAnimation(
177 const uno::Reference< rendering::XAnimation >& )
179 return uno::Reference< rendering::XAnimatedSprite >();
182 uno::Reference< rendering::XAnimatedSprite > SpriteCanvasHelper::createSpriteFromBitmaps(
183 const uno::Sequence< uno::Reference< rendering::XBitmap > >& /*animationBitmaps*/,
184 sal_Int8 /*interpolationMode*/ )
186 return uno::Reference< rendering::XAnimatedSprite >();
189 uno::Reference< rendering::XCustomSprite > SpriteCanvasHelper::createCustomSprite( const geometry::RealSize2D& spriteSize )
191 if( !mpRedrawManager )
192 return uno::Reference< rendering::XCustomSprite >(); // we're disposed
194 return uno::Reference< rendering::XCustomSprite >(
195 new CanvasCustomSprite( spriteSize,
196 mpOwningSpriteCanvas ) );
199 uno::Reference< rendering::XSprite > SpriteCanvasHelper::createClonedSprite(
200 const uno::Reference< rendering::XSprite >& )
202 return uno::Reference< rendering::XSprite >();
205 sal_Bool SpriteCanvasHelper::updateScreen( const ::basegfx::B2IRange& /*rCurrArea*/,
206 sal_Bool bUpdateAll,
207 bool& io_bSurfaceDirty )
209 if( !mpRedrawManager ||
210 !mpOwningSpriteCanvas ||
211 !mpOwningSpriteCanvas->getWindowSurface() ||
212 !mpOwningSpriteCanvas->getBufferSurface() )
214 return sal_False; // disposed, or otherwise dysfunctional
217 OSL_TRACE("SpriteCanvasHelper::updateScreen called");
219 const ::basegfx::B2ISize& rSize = mpOwningSpriteCanvas->getSizePixel();
221 // force compositing surface to be available before using it
222 // inside forEachSpriteArea
223 SurfaceSharedPtr pCompositingSurface = getCompositingSurface(rSize);
224 SurfaceSharedPtr pWindowSurface = mpOwningSpriteCanvas->getWindowSurface();
225 CairoSharedPtr pCompositingCairo = pCompositingSurface->getCairo();
226 CairoSharedPtr pWindowCairo = pWindowSurface->getCairo();
228 // TODO(P1): Might be worthwile to track areas of background
229 // changes, too.
230 if( !bUpdateAll && !io_bSurfaceDirty )
232 // background has not changed, so we're free to optimize
233 // repaint to areas where a sprite has changed
235 // process each independent area of overlapping sprites
236 // separately.
237 mpRedrawManager->forEachSpriteArea( *this );
239 else
241 OSL_TRACE("SpriteCanvasHelper::updateScreen update ALL");
243 // background has changed, so we currently have no choice
244 // but repaint everything (or caller requested that)
246 cairo_rectangle( pCompositingCairo.get(), 0, 0, rSize.getX(), rSize.getY() );
247 cairo_clip( pCompositingCairo.get() );
248 cairo_save( pCompositingCairo.get() );
249 cairo_set_source_surface( pCompositingCairo.get(),
250 mpOwningSpriteCanvas->getBufferSurface()->getCairoSurface().get(),
251 0, 0 );
252 cairo_set_operator( pCompositingCairo.get(), CAIRO_OPERATOR_SOURCE );
253 cairo_paint( pCompositingCairo.get() );
254 cairo_restore( pCompositingCairo.get() );
256 // repaint all active sprites on top of background into
257 // VDev.
258 mpRedrawManager->forEachSprite(
259 ::boost::bind(
260 &spriteRedraw,
261 boost::cref(pCompositingCairo),
262 _1 ) );
264 // flush to screen
265 cairo_rectangle( pWindowCairo.get(), 0, 0, rSize.getX(), rSize.getY() );
266 cairo_clip( pWindowCairo.get() );
267 cairo_set_source_surface( pWindowCairo.get(),
268 pCompositingSurface->getCairoSurface().get(),
269 0, 0 );
270 cairo_set_operator( pWindowCairo.get(), CAIRO_OPERATOR_SOURCE );
271 cairo_paint( pWindowCairo.get() );
274 // change record vector must be cleared, for the next turn of
275 // rendering and sprite changing
276 mpRedrawManager->clearChangeRecords();
278 io_bSurfaceDirty = false;
280 // commit to screen
281 mpOwningSpriteCanvas->flush();
283 return sal_True;
286 void SpriteCanvasHelper::backgroundPaint( const ::basegfx::B2DRange& rUpdateRect )
288 if( mpOwningSpriteCanvas && mpCompositingSurface )
289 repaintBackground( mpCompositingSurface->getCairo(),
290 mpOwningSpriteCanvas->getBufferSurface(),
291 rUpdateRect );
294 void SpriteCanvasHelper::scrollUpdate( const ::basegfx::B2DRange& rMoveStart,
295 const ::basegfx::B2DRange& rMoveEnd,
296 const ::canvas::SpriteRedrawManager::UpdateArea& rUpdateArea )
298 ENSURE_OR_THROW( mpOwningSpriteCanvas &&
299 mpOwningSpriteCanvas->getBufferSurface(),
300 "SpriteCanvasHelper::scrollUpdate(): NULL device pointer " );
302 OSL_TRACE("SpriteCanvasHelper::scrollUpdate called");
304 const ::basegfx::B2ISize& rSize = mpOwningSpriteCanvas->getSizePixel();
305 const ::basegfx::B2IRange aOutputBounds( 0,0,
306 rSize.getX(),
307 rSize.getY() );
309 SurfaceSharedPtr pCompositingSurface = getCompositingSurface(rSize);
310 SurfaceSharedPtr pWindowSurface = mpOwningSpriteCanvas->getWindowSurface();
311 CairoSharedPtr pCompositingCairo = pCompositingSurface->getCairo();
312 CairoSharedPtr pWindowCairo = pWindowSurface->getCairo();
314 // round rectangles to integer pixel. Note: have to be
315 // extremely careful here, to avoid off-by-one errors for
316 // the destination area: otherwise, the next scroll update
317 // would copy pixel that are not supposed to be part of
318 // the sprite.
319 ::basegfx::B2IRange aSourceRect(
320 ::canvas::tools::spritePixelAreaFromB2DRange( rMoveStart ) );
321 const ::basegfx::B2IRange& rDestRect(
322 ::canvas::tools::spritePixelAreaFromB2DRange( rMoveEnd ) );
323 ::basegfx::B2IPoint aDestPos( rDestRect.getMinimum() );
325 ::std::vector< ::basegfx::B2IRange > aUnscrollableAreas;
327 // TODO(E3): This is plain buggy (but copies the behaviour of
328 // the old Impress slideshow) - the scrolled area might
329 // actually lie _below_ another window!
331 // clip to output bounds (cannot properly scroll stuff
332 // _outside_ our screen area)
333 if( !::canvas::tools::clipScrollArea( aSourceRect,
334 aDestPos,
335 aUnscrollableAreas,
336 aOutputBounds ) )
338 // fully clipped scroll area: cannot simply scroll
339 // then. Perform normal opaque update (can use that, since
340 // one of the preconditions for scrollable update is
341 // opaque sprite content)
343 // repaint all affected sprites directly to output device
344 ::std::for_each( rUpdateArea.maComponentList.begin(),
345 rUpdateArea.maComponentList.end(),
346 ::boost::bind(
347 &spriteRedrawStub3,
348 boost::cref(pCompositingCairo),
349 _1 ) );
351 else
353 const ::basegfx::B2IVector aSourceUpperLeftPos( aSourceRect.getMinimum() );
355 // clip dest area (which must be inside rDestBounds)
356 ::basegfx::B2IRange aDestRect( rDestRect );
357 aDestRect.intersect( aOutputBounds );
359 cairo_save( pCompositingCairo.get() );
360 // scroll content in device back buffer
361 cairo_set_source_surface( pCompositingCairo.get(),
362 mpOwningSpriteCanvas->getBufferSurface()->getCairoSurface().get(),
363 aDestPos.getX() - aSourceUpperLeftPos.getX(),
364 aDestPos.getY() - aSourceUpperLeftPos.getY() );
365 cairo_rectangle( pCompositingCairo.get(),
366 aDestPos.getX(), aDestPos.getY(),
367 sal::static_int_cast<sal_Int32>(aDestRect.getWidth()),
368 sal::static_int_cast<sal_Int32>(aDestRect.getHeight()) );
369 cairo_clip( pCompositingCairo.get() );
370 cairo_set_operator( pCompositingCairo.get(), CAIRO_OPERATOR_SOURCE );
371 cairo_paint( pCompositingCairo.get() );
372 cairo_restore( pCompositingCairo.get() );
374 const ::canvas::SpriteRedrawManager::SpriteConnectedRanges::ComponentListType::const_iterator
375 aFirst( rUpdateArea.maComponentList.begin() );
376 ::canvas::SpriteRedrawManager::SpriteConnectedRanges::ComponentListType::const_iterator
377 aSecond( aFirst ); ++aSecond;
379 ENSURE_OR_THROW( aFirst->second.getSprite().is(),
380 "VCLCanvas::scrollUpdate(): no sprite" );
382 // repaint uncovered areas from sprite. Need to actually
383 // clip here, since we're only repainting _parts_ of the
384 // sprite
385 ::std::for_each( aUnscrollableAreas.begin(),
386 aUnscrollableAreas.end(),
387 ::boost::bind( &opaqueUpdateSpriteArea,
388 ::boost::cref(aFirst->second.getSprite()),
389 boost::cref(pCompositingCairo),
390 _1 ) );
393 // repaint uncovered areas from backbuffer - take the
394 // _rounded_ rectangles from above, to have the update
395 // consistent with the scroll above.
396 ::std::vector< ::basegfx::B2DRange > aUncoveredAreas;
397 ::basegfx::computeSetDifference( aUncoveredAreas,
398 rUpdateArea.maTotalBounds,
399 ::basegfx::B2DRange( rDestRect ) );
400 ::std::for_each( aUncoveredAreas.begin(),
401 aUncoveredAreas.end(),
402 ::boost::bind( &repaintBackground,
403 boost::cref(pCompositingCairo),
404 boost::cref(mpOwningSpriteCanvas->getBufferSurface()),
405 _1 ) );
407 cairo_rectangle( pWindowCairo.get(), 0, 0, rSize.getX(), rSize.getY() );
408 cairo_clip( pWindowCairo.get() );
409 cairo_set_source_surface( pWindowCairo.get(),
410 pCompositingSurface->getCairoSurface().get(),
411 0, 0 );
412 cairo_set_operator( pWindowCairo.get(), CAIRO_OPERATOR_SOURCE );
413 cairo_paint( pWindowCairo.get() );
416 void SpriteCanvasHelper::opaqueUpdate( const ::basegfx::B2DRange& rTotalArea,
417 const ::std::vector< ::canvas::Sprite::Reference >& rSortedUpdateSprites )
419 ENSURE_OR_THROW( mpOwningSpriteCanvas &&
420 mpOwningSpriteCanvas->getBufferSurface(),
421 "SpriteCanvasHelper::opaqueUpdate(): NULL device pointer " );
423 OSL_TRACE("SpriteCanvasHelper::opaqueUpdate called");
425 const ::basegfx::B2ISize& rDeviceSize = mpOwningSpriteCanvas->getSizePixel();
427 SurfaceSharedPtr pCompositingSurface = getCompositingSurface(rDeviceSize);
428 SurfaceSharedPtr pWindowSurface = mpOwningSpriteCanvas->getWindowSurface();
429 CairoSharedPtr pCompositingCairo = pCompositingSurface->getCairo();
430 CairoSharedPtr pWindowCairo = pWindowSurface->getCairo();
432 cairo_rectangle( pCompositingCairo.get(), 0, 0, rDeviceSize.getX(), rDeviceSize.getY() );
433 cairo_clip( pCompositingCairo.get() );
435 ::basegfx::B2DVector aPos( ceil( rTotalArea.getMinX() ), ceil( rTotalArea.getMinY() ) );
436 ::basegfx::B2DVector aSize( floor( rTotalArea.getMaxX() - aPos.getX() ), floor( rTotalArea.getMaxY() - aPos.getY() ) );
438 cairo_rectangle( pCompositingCairo.get(), aPos.getX(), aPos.getY(), aSize.getX(), aSize.getY() );
439 cairo_clip( pCompositingCairo.get() );
441 // repaint all affected sprites directly to output device
442 ::std::for_each( rSortedUpdateSprites.begin(),
443 rSortedUpdateSprites.end(),
444 ::boost::bind(
445 &spriteRedrawStub,
446 boost::cref(pCompositingCairo),
447 _1 ) );
449 // flush to screen
450 cairo_rectangle( pWindowCairo.get(), 0, 0, rDeviceSize.getX(), rDeviceSize.getY() );
451 cairo_clip( pWindowCairo.get() );
452 cairo_rectangle( pWindowCairo.get(), aPos.getX(), aPos.getY(), aSize.getX(), aSize.getY() );
453 cairo_clip( pWindowCairo.get() );
454 cairo_set_source_surface( pWindowCairo.get(),
455 pCompositingSurface->getCairoSurface().get(),
456 0, 0 );
457 cairo_set_operator( pWindowCairo.get(), CAIRO_OPERATOR_SOURCE );
458 cairo_paint( pWindowCairo.get() );
461 void SpriteCanvasHelper::genericUpdate( const ::basegfx::B2DRange& rRequestedArea,
462 const ::std::vector< ::canvas::Sprite::Reference >& rSortedUpdateSprites )
464 // TODO
465 OSL_TRACE("SpriteCanvasHelper::genericUpdate called");
467 ENSURE_OR_THROW( mpOwningSpriteCanvas &&
468 mpOwningSpriteCanvas->getBufferSurface(),
469 "SpriteCanvasHelper::genericUpdate(): NULL device pointer " );
471 // limit size of update VDev to target outdev's size
472 const ::basegfx::B2ISize& rSize = mpOwningSpriteCanvas->getSizePixel();
474 SurfaceSharedPtr pCompositingSurface = getCompositingSurface(rSize);
475 SurfaceSharedPtr pWindowSurface = mpOwningSpriteCanvas->getWindowSurface();
476 CairoSharedPtr pCompositingCairo = pCompositingSurface->getCairo();
477 CairoSharedPtr pWindowCairo = pWindowSurface->getCairo();
479 // round output position towards zero. Don't want to truncate
480 // a fraction of a sprite pixel... Clip position at origin,
481 // otherwise, truncation of size below might leave visible
482 // areas uncovered by VDev.
483 const Point aOutputPosition(
484 ::std::max( sal_Int32( 0 ),
485 static_cast< sal_Int32 >(rRequestedArea.getMinX()) ),
486 ::std::max( sal_Int32( 0 ),
487 static_cast< sal_Int32 >(rRequestedArea.getMinY()) ) );
488 // round output size towards +infty. Don't want to truncate a
489 // fraction of a sprite pixel... Limit size of VDev to output
490 // device's area.
491 const Size aOutputSize(
492 ::std::min( rSize.getX(),
493 ::canvas::tools::roundUp( rRequestedArea.getMaxX() - aOutputPosition.X()) ),
494 ::std::min( rSize.getY(),
495 ::canvas::tools::roundUp( rRequestedArea.getMaxY() - aOutputPosition.Y()) ) );
497 cairo_rectangle( pCompositingCairo.get(), aOutputPosition.X(), aOutputPosition.Y(), aOutputSize.Width(), aOutputSize.Height() );
498 cairo_clip( pCompositingCairo.get() );
500 // paint background
501 cairo_save( pCompositingCairo.get() );
502 cairo_set_source_surface( pCompositingCairo.get(),
503 mpOwningSpriteCanvas->getBufferSurface()->getCairoSurface().get(),
504 0, 0 );
505 cairo_set_operator( pCompositingCairo.get(), CAIRO_OPERATOR_SOURCE );
506 cairo_paint( pCompositingCairo.get() );
507 cairo_restore( pCompositingCairo.get() );
509 // repaint all affected sprites on top of background into
510 // VDev.
511 ::std::for_each( rSortedUpdateSprites.begin(),
512 rSortedUpdateSprites.end(),
513 ::boost::bind( &spriteRedrawStub2,
514 boost::cref(pCompositingCairo),
515 _1 ) );
517 // flush to screen
518 cairo_rectangle( pWindowCairo.get(), aOutputPosition.X(), aOutputPosition.Y(), aOutputSize.Width(), aOutputSize.Height() );
519 cairo_clip( pWindowCairo.get() );
520 cairo_set_source_surface( pWindowCairo.get(),
521 pCompositingSurface->getCairoSurface().get(),
522 0, 0 );
523 cairo_set_operator( pWindowCairo.get(), CAIRO_OPERATOR_SOURCE );
524 cairo_paint( pWindowCairo.get() );
527 ::cairo::SurfaceSharedPtr SpriteCanvasHelper::getCompositingSurface( const ::basegfx::B2ISize& rNeededSize )
529 if( rNeededSize.getX() < maCompositingSurfaceSize.getX() ||
530 rNeededSize.getY() < maCompositingSurfaceSize.getY() )
532 // need to give buffer more size
533 mpCompositingSurface.reset();
536 if( !mpCompositingSurface )
538 mpCompositingSurface =
539 mpOwningSpriteCanvas->getWindowSurface()->getSimilar(
540 CAIRO_CONTENT_COLOR,
541 rNeededSize.getX(), rNeededSize.getY() );
542 maCompositingSurfaceSize = rNeededSize;
545 return mpCompositingSurface;