1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_canvas.hxx"
26 
27 #include <canvas/debug.hxx>
28 #include <canvas/verbosetrace.hxx>
29 #include <canvas/canvastools.hxx>
30 #include <tools/diagnose_ex.h>
31 
32 #include <vcl/canvastools.hxx>
33 
34 #include <comphelper/scopeguard.hxx>
35 
36 #include <basegfx/range/b2drectangle.hxx>
37 #include <basegfx/tools/canvastools.hxx>
38 
39 #include <boost/cast.hpp>
40 
41 #include "cairo_spritecanvashelper.hxx"
42 #include "cairo_canvascustomsprite.hxx"
43 
44 using namespace ::cairo;
45 using namespace ::com::sun::star;
46 
47 namespace cairocanvas
48 {
49     namespace
50     {
51         /** Sprite redraw at original position
52 
53             Used to repaint the whole canvas (background and all
54             sprites)
55          */
spriteRedraw(const CairoSharedPtr & pCairo,const::canvas::Sprite::Reference & rSprite)56         void spriteRedraw( const CairoSharedPtr& pCairo,
57                            const ::canvas::Sprite::Reference& rSprite )
58         {
59             // downcast to derived cairocanvas::Sprite interface, which
60             // provides the actual redraw methods.
61             ::boost::polymorphic_downcast< Sprite* >(rSprite.get())->redraw( pCairo, true);
62         }
63 
repaintBackground(const CairoSharedPtr & pCairo,const SurfaceSharedPtr & pBackgroundSurface,const::basegfx::B2DRange & rArea)64         void repaintBackground( const CairoSharedPtr&      pCairo,
65                                 const SurfaceSharedPtr&    pBackgroundSurface,
66                                 const ::basegfx::B2DRange& rArea )
67         {
68             cairo_save( pCairo.get() );
69             cairo_rectangle( pCairo.get(), ceil( rArea.getMinX() ), ceil( rArea.getMinY() ),
70                              floor( rArea.getWidth() ), floor( rArea.getHeight() ) );
71             cairo_clip( pCairo.get() );
72             cairo_set_source_surface( pCairo.get(), pBackgroundSurface->getCairoSurface().get(), 0, 0 );
73             cairo_set_operator( pCairo.get(), CAIRO_OPERATOR_SOURCE );
74             cairo_paint( pCairo.get() );
75             cairo_restore( pCairo.get() );
76         }
77 
opaqueUpdateSpriteArea(const::canvas::Sprite::Reference & rSprite,const CairoSharedPtr & pCairo,const::basegfx::B2IRange & rArea)78         void opaqueUpdateSpriteArea( const ::canvas::Sprite::Reference& rSprite,
79                                      const CairoSharedPtr&              pCairo,
80                                      const ::basegfx::B2IRange&         rArea )
81         {
82             // clip output to actual update region (otherwise a)
83             // wouldn't save much render time, and b) will clutter
84             // scrolled sprite content outside this area)
85             cairo_save( pCairo.get() );
86             cairo_rectangle( pCairo.get(), rArea.getMinX(), rArea.getMinY(),
87                              sal::static_int_cast<sal_Int32>(rArea.getWidth()),
88                              sal::static_int_cast<sal_Int32>(rArea.getHeight()) );
89             cairo_clip( pCairo.get() );
90 
91             // repaint affected sprite directly to output device (at
92             // the actual screen output position)
93             // rendering directly to device buffer
94             ::boost::polymorphic_downcast< Sprite* >( rSprite.get() )->redraw( pCairo, false );
95 
96             cairo_restore( pCairo.get() );
97         }
98 
99         /** Repaint sprite at original position
100 
101             Used for opaque updates, which render directly to the
102             device buffer.
103          */
spriteRedrawStub(const CairoSharedPtr & pCairo,const::canvas::Sprite::Reference & rSprite)104         void spriteRedrawStub( const CairoSharedPtr& pCairo,
105                                const ::canvas::Sprite::Reference& rSprite )
106         {
107             if( rSprite.is() )
108             {
109                 ::boost::polymorphic_downcast< Sprite* >( rSprite.get() )->redraw( pCairo, false );
110             }
111         }
112 
113         /** Repaint sprite at given position
114 
115             Used for generic update, which renders into device buffer.
116          */
spriteRedrawStub2(const CairoSharedPtr & pCairo,const::canvas::Sprite::Reference & rSprite)117         void spriteRedrawStub2( const CairoSharedPtr& pCairo,
118                                 const ::canvas::Sprite::Reference&	rSprite )
119         {
120             if( rSprite.is() )
121             {
122                 ::boost::polymorphic_downcast< Sprite* >( rSprite.get() )->redraw( pCairo, true );;
123             }
124         }
125 
126         /** Repaint sprite at original position
127 
128             Used for opaque updates from scrollUpdate(), which render
129             directly to the front buffer.
130          */
spriteRedrawStub3(const CairoSharedPtr & pCairo,const::canvas::SpriteRedrawManager::AreaComponent & rComponent)131         void spriteRedrawStub3( const CairoSharedPtr& pCairo,
132                                 const ::canvas::SpriteRedrawManager::AreaComponent& rComponent )
133         {
134             const ::canvas::Sprite::Reference& rSprite( rComponent.second.getSprite() );
135 
136             if( rSprite.is() )
137                 ::boost::polymorphic_downcast< Sprite* >( rSprite.get() )->redraw( pCairo, false );
138         }
139     }
140 
SpriteCanvasHelper()141     SpriteCanvasHelper::SpriteCanvasHelper() :
142         mpRedrawManager( NULL ),
143         mpOwningSpriteCanvas( NULL ),
144         mpCompositingSurface(),
145         maCompositingSurfaceSize()
146     {
147     }
148 
init(::canvas::SpriteRedrawManager & rManager,SpriteCanvas & rDevice,const::basegfx::B2ISize & rSize)149     void SpriteCanvasHelper::init( ::canvas::SpriteRedrawManager& rManager,
150                                    SpriteCanvas&                  rDevice,
151                                    const ::basegfx::B2ISize&      rSize )
152     {
153         mpRedrawManager = &rManager;
154         mpOwningSpriteCanvas = &rDevice;
155 
156         CanvasHelper::init( rSize, rDevice, &rDevice );
157     }
158 
disposing()159     void SpriteCanvasHelper::disposing()
160     {
161         mpCompositingSurface.reset();
162         mpOwningSpriteCanvas = NULL;
163         mpRedrawManager = NULL;
164 
165         // forward to base
166         CanvasHelper::disposing();
167     }
168 
createSpriteFromAnimation(const uno::Reference<rendering::XAnimation> &)169     uno::Reference< rendering::XAnimatedSprite > SpriteCanvasHelper::createSpriteFromAnimation(
170         const uno::Reference< rendering::XAnimation >& )
171     {
172         return uno::Reference< rendering::XAnimatedSprite >();
173     }
174 
createSpriteFromBitmaps(const uno::Sequence<uno::Reference<rendering::XBitmap>> &,sal_Int8)175     uno::Reference< rendering::XAnimatedSprite > SpriteCanvasHelper::createSpriteFromBitmaps(
176         const uno::Sequence< uno::Reference< rendering::XBitmap > >& /*animationBitmaps*/,
177         sal_Int8                                                     /*interpolationMode*/ )
178     {
179         return uno::Reference< rendering::XAnimatedSprite >();
180     }
181 
createCustomSprite(const geometry::RealSize2D & spriteSize)182     uno::Reference< rendering::XCustomSprite > SpriteCanvasHelper::createCustomSprite( const geometry::RealSize2D& spriteSize )
183     {
184         if( !mpRedrawManager )
185             return uno::Reference< rendering::XCustomSprite >(); // we're disposed
186 
187         return uno::Reference< rendering::XCustomSprite >(
188             new CanvasCustomSprite( spriteSize,
189                                     mpOwningSpriteCanvas ) );
190     }
191 
createClonedSprite(const uno::Reference<rendering::XSprite> &)192     uno::Reference< rendering::XSprite > SpriteCanvasHelper::createClonedSprite(
193         const uno::Reference< rendering::XSprite >& )
194     {
195         return uno::Reference< rendering::XSprite >();
196     }
197 
updateScreen(const::basegfx::B2IRange &,sal_Bool bUpdateAll,bool & io_bSurfaceDirty)198     sal_Bool SpriteCanvasHelper::updateScreen( const ::basegfx::B2IRange& /*rCurrArea*/,
199                                                sal_Bool                   bUpdateAll,
200                                                bool&                      io_bSurfaceDirty )
201     {
202         if( !mpRedrawManager ||
203             !mpOwningSpriteCanvas ||
204             !mpOwningSpriteCanvas->getWindowSurface() ||
205             !mpOwningSpriteCanvas->getBufferSurface() )
206         {
207             return sal_False; // disposed, or otherwise dysfunctional
208         }
209 
210         OSL_TRACE("SpriteCanvasHelper::updateScreen called");
211 
212         const ::basegfx::B2ISize& rSize = mpOwningSpriteCanvas->getSizePixel();
213 
214         // force compositing surface to be available before using it
215         // inside forEachSpriteArea
216         SurfaceSharedPtr pCompositingSurface = getCompositingSurface(rSize);
217         SurfaceSharedPtr pWindowSurface = mpOwningSpriteCanvas->getWindowSurface();
218         CairoSharedPtr pCompositingCairo = pCompositingSurface->getCairo();
219         CairoSharedPtr pWindowCairo = pWindowSurface->getCairo();
220 
221         // TODO(P1): Might be worthwile to track areas of background
222         // changes, too.
223         if( !bUpdateAll && !io_bSurfaceDirty )
224         {
225             // background has not changed, so we're free to optimize
226             // repaint to areas where a sprite has changed
227 
228             // process each independent area of overlapping sprites
229             // separately.
230             mpRedrawManager->forEachSpriteArea( *this );
231         }
232         else
233         {
234             OSL_TRACE("SpriteCanvasHelper::updateScreen update ALL");
235 
236             // background has changed, so we currently have no choice
237             // but repaint everything (or caller requested that)
238 
239             cairo_rectangle( pCompositingCairo.get(), 0, 0, rSize.getX(), rSize.getY() );
240             cairo_clip( pCompositingCairo.get() );
241             cairo_save( pCompositingCairo.get() );
242             cairo_set_source_surface( pCompositingCairo.get(),
243                                       mpOwningSpriteCanvas->getBufferSurface()->getCairoSurface().get(),
244                                       0, 0 );
245             cairo_set_operator( pCompositingCairo.get(), CAIRO_OPERATOR_SOURCE );
246             cairo_paint( pCompositingCairo.get() );
247             cairo_restore( pCompositingCairo.get() );
248 
249             // repaint all active sprites on top of background into
250             // VDev.
251             mpRedrawManager->forEachSprite(
252                 ::boost::bind(
253                     &spriteRedraw,
254                     boost::cref(pCompositingCairo),
255                     _1 ) );
256 
257             // flush to screen
258             cairo_rectangle( pWindowCairo.get(), 0, 0, rSize.getX(), rSize.getY() );
259             cairo_clip( pWindowCairo.get() );
260             cairo_set_source_surface( pWindowCairo.get(),
261                                       pCompositingSurface->getCairoSurface().get(),
262                                       0, 0 );
263             cairo_set_operator( pWindowCairo.get(), CAIRO_OPERATOR_SOURCE );
264             cairo_paint( pWindowCairo.get() );
265         }
266 
267         // change record vector must be cleared, for the next turn of
268         // rendering and sprite changing
269         mpRedrawManager->clearChangeRecords();
270 
271         io_bSurfaceDirty = false;
272 
273         // commit to screen
274         mpOwningSpriteCanvas->flush();
275 
276         return sal_True;
277     }
278 
backgroundPaint(const::basegfx::B2DRange & rUpdateRect)279     void SpriteCanvasHelper::backgroundPaint( const ::basegfx::B2DRange& rUpdateRect )
280     {
281         if( mpOwningSpriteCanvas && mpCompositingSurface )
282             repaintBackground( mpCompositingSurface->getCairo(),
283                                mpOwningSpriteCanvas->getBufferSurface(),
284                                rUpdateRect );
285     }
286 
scrollUpdate(const::basegfx::B2DRange & rMoveStart,const::basegfx::B2DRange & rMoveEnd,const::canvas::SpriteRedrawManager::UpdateArea & rUpdateArea)287     void SpriteCanvasHelper::scrollUpdate( const ::basegfx::B2DRange& 						rMoveStart,
288                                            const ::basegfx::B2DRange& 						rMoveEnd,
289                                            const ::canvas::SpriteRedrawManager::UpdateArea& rUpdateArea )
290     {
291         ENSURE_OR_THROW( mpOwningSpriteCanvas &&
292                           mpOwningSpriteCanvas->getBufferSurface(),
293                           "SpriteCanvasHelper::scrollUpdate(): NULL device pointer " );
294 
295         OSL_TRACE("SpriteCanvasHelper::scrollUpdate called");
296 
297         const ::basegfx::B2ISize& rSize = mpOwningSpriteCanvas->getSizePixel();
298         const ::basegfx::B2IRange  aOutputBounds( 0,0,
299                                                   rSize.getX(),
300                                                   rSize.getY() );
301 
302         SurfaceSharedPtr pCompositingSurface = getCompositingSurface(rSize);
303         SurfaceSharedPtr pWindowSurface = mpOwningSpriteCanvas->getWindowSurface();
304         CairoSharedPtr pCompositingCairo = pCompositingSurface->getCairo();
305         CairoSharedPtr pWindowCairo = pWindowSurface->getCairo();
306 
307         // round rectangles to integer pixel. Note: have to be
308         // extremely careful here, to avoid off-by-one errors for
309         // the destination area: otherwise, the next scroll update
310         // would copy pixel that are not supposed to be part of
311         // the sprite.
312         ::basegfx::B2IRange aSourceRect(
313             ::canvas::tools::spritePixelAreaFromB2DRange( rMoveStart ) );
314         const ::basegfx::B2IRange& rDestRect(
315             ::canvas::tools::spritePixelAreaFromB2DRange( rMoveEnd ) );
316         ::basegfx::B2IPoint aDestPos( rDestRect.getMinimum() );
317 
318         ::std::vector< ::basegfx::B2IRange > aUnscrollableAreas;
319 
320         // TODO(E3): This is plain buggy (but copies the behaviour of
321         // the old Impress slideshow) - the scrolled area might
322         // actually lie _below_ another window!
323 
324         // clip to output bounds (cannot properly scroll stuff
325         // _outside_ our screen area)
326         if( !::canvas::tools::clipScrollArea( aSourceRect,
327                                               aDestPos,
328                                               aUnscrollableAreas,
329                                               aOutputBounds ) )
330         {
331             // fully clipped scroll area: cannot simply scroll
332             // then. Perform normal opaque update (can use that, since
333             // one of the preconditions for scrollable update is
334             // opaque sprite content)
335 
336             // repaint all affected sprites directly to output device
337             ::std::for_each( rUpdateArea.maComponentList.begin(),
338                              rUpdateArea.maComponentList.end(),
339                              ::boost::bind(
340                                  &spriteRedrawStub3,
341                                  boost::cref(pCompositingCairo),
342                                  _1 ) );
343         }
344         else
345         {
346             const ::basegfx::B2IVector aSourceUpperLeftPos( aSourceRect.getMinimum() );
347 
348             // clip dest area (which must be inside rDestBounds)
349             ::basegfx::B2IRange aDestRect( rDestRect );
350             aDestRect.intersect( aOutputBounds );
351 
352             cairo_save( pCompositingCairo.get() );
353             // scroll content in device back buffer
354             cairo_set_source_surface( pCompositingCairo.get(),
355                                       mpOwningSpriteCanvas->getBufferSurface()->getCairoSurface().get(),
356                                       aDestPos.getX() - aSourceUpperLeftPos.getX(),
357                                       aDestPos.getY() - aSourceUpperLeftPos.getY() );
358             cairo_rectangle( pCompositingCairo.get(),
359                              aDestPos.getX(), aDestPos.getY(),
360                              sal::static_int_cast<sal_Int32>(aDestRect.getWidth()),
361                              sal::static_int_cast<sal_Int32>(aDestRect.getHeight()) );
362             cairo_clip( pCompositingCairo.get() );
363             cairo_set_operator( pCompositingCairo.get(), CAIRO_OPERATOR_SOURCE );
364             cairo_paint( pCompositingCairo.get() );
365             cairo_restore( pCompositingCairo.get() );
366 
367             const ::canvas::SpriteRedrawManager::SpriteConnectedRanges::ComponentListType::const_iterator
368                 aFirst( rUpdateArea.maComponentList.begin() );
369             ::canvas::SpriteRedrawManager::SpriteConnectedRanges::ComponentListType::const_iterator
370                   aSecond( aFirst ); ++aSecond;
371 
372             ENSURE_OR_THROW( aFirst->second.getSprite().is(),
373                              "VCLCanvas::scrollUpdate(): no sprite" );
374 
375             // repaint uncovered areas from sprite. Need to actually
376             // clip here, since we're only repainting _parts_ of the
377             // sprite
378             ::std::for_each( aUnscrollableAreas.begin(),
379                              aUnscrollableAreas.end(),
380                              ::boost::bind( &opaqueUpdateSpriteArea,
381                                             ::boost::cref(aFirst->second.getSprite()),
382                                             boost::cref(pCompositingCairo),
383                                             _1 ) );
384         }
385 
386         // repaint uncovered areas from backbuffer - take the
387         // _rounded_ rectangles from above, to have the update
388         // consistent with the scroll above.
389         ::std::vector< ::basegfx::B2DRange > aUncoveredAreas;
390         ::basegfx::computeSetDifference( aUncoveredAreas,
391                                          rUpdateArea.maTotalBounds,
392                                          ::basegfx::B2DRange( rDestRect ) );
393         ::std::for_each( aUncoveredAreas.begin(),
394                          aUncoveredAreas.end(),
395                          ::boost::bind( &repaintBackground,
396                                         boost::cref(pCompositingCairo),
397                                         boost::cref(mpOwningSpriteCanvas->getBufferSurface()),
398                                         _1 ) );
399 
400         cairo_rectangle( pWindowCairo.get(), 0, 0, rSize.getX(), rSize.getY() );
401         cairo_clip( pWindowCairo.get() );
402         cairo_set_source_surface( pWindowCairo.get(),
403                                   pCompositingSurface->getCairoSurface().get(),
404                                   0, 0 );
405         cairo_set_operator( pWindowCairo.get(), CAIRO_OPERATOR_SOURCE );
406         cairo_paint( pWindowCairo.get() );
407     }
408 
opaqueUpdate(const::basegfx::B2DRange & rTotalArea,const::std::vector<::canvas::Sprite::Reference> & rSortedUpdateSprites)409     void SpriteCanvasHelper::opaqueUpdate( const ::basegfx::B2DRange&                          rTotalArea,
410                                            const ::std::vector< ::canvas::Sprite::Reference >& rSortedUpdateSprites )
411     {
412         ENSURE_OR_THROW( mpOwningSpriteCanvas &&
413                           mpOwningSpriteCanvas->getBufferSurface(),
414                           "SpriteCanvasHelper::opaqueUpdate(): NULL device pointer " );
415 
416         OSL_TRACE("SpriteCanvasHelper::opaqueUpdate called");
417 
418         const ::basegfx::B2ISize& rDeviceSize = mpOwningSpriteCanvas->getSizePixel();
419 
420         SurfaceSharedPtr pCompositingSurface = getCompositingSurface(rDeviceSize);
421         SurfaceSharedPtr pWindowSurface = mpOwningSpriteCanvas->getWindowSurface();
422         CairoSharedPtr pCompositingCairo = pCompositingSurface->getCairo();
423         CairoSharedPtr pWindowCairo = pWindowSurface->getCairo();
424 
425         cairo_rectangle( pCompositingCairo.get(), 0, 0, rDeviceSize.getX(), rDeviceSize.getY() );
426         cairo_clip( pCompositingCairo.get() );
427 
428         ::basegfx::B2DVector aPos( ceil( rTotalArea.getMinX() ), ceil( rTotalArea.getMinY() ) );
429         ::basegfx::B2DVector aSize( floor( rTotalArea.getMaxX() - aPos.getX() ), floor( rTotalArea.getMaxY() - aPos.getY() ) );
430 
431         cairo_rectangle( pCompositingCairo.get(), aPos.getX(), aPos.getY(), aSize.getX(), aSize.getY() );
432         cairo_clip( pCompositingCairo.get() );
433 
434         // repaint all affected sprites directly to output device
435         ::std::for_each( rSortedUpdateSprites.begin(),
436                          rSortedUpdateSprites.end(),
437                          ::boost::bind(
438                              &spriteRedrawStub,
439                              boost::cref(pCompositingCairo),
440                              _1 ) );
441 
442         // flush to screen
443         cairo_rectangle( pWindowCairo.get(), 0, 0, rDeviceSize.getX(), rDeviceSize.getY() );
444         cairo_clip( pWindowCairo.get() );
445         cairo_rectangle( pWindowCairo.get(), aPos.getX(), aPos.getY(), aSize.getX(), aSize.getY() );
446         cairo_clip( pWindowCairo.get() );
447         cairo_set_source_surface( pWindowCairo.get(),
448                                   pCompositingSurface->getCairoSurface().get(),
449                                   0, 0 );
450         cairo_set_operator( pWindowCairo.get(), CAIRO_OPERATOR_SOURCE );
451         cairo_paint( pWindowCairo.get() );
452     }
453 
genericUpdate(const::basegfx::B2DRange & rRequestedArea,const::std::vector<::canvas::Sprite::Reference> & rSortedUpdateSprites)454     void SpriteCanvasHelper::genericUpdate( const ::basegfx::B2DRange&                          rRequestedArea,
455                                             const ::std::vector< ::canvas::Sprite::Reference >& rSortedUpdateSprites )
456     {
457         // TODO
458         OSL_TRACE("SpriteCanvasHelper::genericUpdate called");
459 
460         ENSURE_OR_THROW( mpOwningSpriteCanvas &&
461                          mpOwningSpriteCanvas->getBufferSurface(),
462                          "SpriteCanvasHelper::genericUpdate(): NULL device pointer " );
463 
464         // limit size of update VDev to target outdev's size
465         const ::basegfx::B2ISize& rSize = mpOwningSpriteCanvas->getSizePixel();
466 
467         SurfaceSharedPtr pCompositingSurface = getCompositingSurface(rSize);
468         SurfaceSharedPtr pWindowSurface = mpOwningSpriteCanvas->getWindowSurface();
469         CairoSharedPtr pCompositingCairo = pCompositingSurface->getCairo();
470         CairoSharedPtr pWindowCairo = pWindowSurface->getCairo();
471 
472         // round output position towards zero. Don't want to truncate
473         // a fraction of a sprite pixel...  Clip position at origin,
474         // otherwise, truncation of size below might leave visible
475         // areas uncovered by VDev.
476         const Point aOutputPosition(
477             ::std::max( sal_Int32( 0 ),
478                         static_cast< sal_Int32 >(rRequestedArea.getMinX()) ),
479             ::std::max( sal_Int32( 0 ),
480                         static_cast< sal_Int32 >(rRequestedArea.getMinY()) ) );
481         // round output size towards +infty. Don't want to truncate a
482         // fraction of a sprite pixel... Limit size of VDev to output
483         // device's area.
484         const Size  aOutputSize(
485             ::std::min( rSize.getX(),
486                         ::canvas::tools::roundUp( rRequestedArea.getMaxX() - aOutputPosition.X()) ),
487             ::std::min( rSize.getY(),
488                         ::canvas::tools::roundUp( rRequestedArea.getMaxY() - aOutputPosition.Y()) ) );
489 
490         cairo_rectangle( pCompositingCairo.get(), aOutputPosition.X(), aOutputPosition.Y(), aOutputSize.Width(), aOutputSize.Height() );
491         cairo_clip( pCompositingCairo.get() );
492 
493         // paint background
494         cairo_save( pCompositingCairo.get() );
495         cairo_set_source_surface( pCompositingCairo.get(),
496                                   mpOwningSpriteCanvas->getBufferSurface()->getCairoSurface().get(),
497                                   0, 0 );
498         cairo_set_operator( pCompositingCairo.get(), CAIRO_OPERATOR_SOURCE );
499         cairo_paint( pCompositingCairo.get() );
500         cairo_restore( pCompositingCairo.get() );
501 
502         // repaint all affected sprites on top of background into
503         // VDev.
504         ::std::for_each( rSortedUpdateSprites.begin(),
505                          rSortedUpdateSprites.end(),
506                          ::boost::bind( &spriteRedrawStub2,
507                                         boost::cref(pCompositingCairo),
508                                         _1 ) );
509 
510         // flush to screen
511         cairo_rectangle( pWindowCairo.get(), aOutputPosition.X(), aOutputPosition.Y(), aOutputSize.Width(), aOutputSize.Height() );
512         cairo_clip( pWindowCairo.get() );
513         cairo_set_source_surface( pWindowCairo.get(),
514                                   pCompositingSurface->getCairoSurface().get(),
515                                   0, 0 );
516         cairo_set_operator( pWindowCairo.get(), CAIRO_OPERATOR_SOURCE );
517         cairo_paint( pWindowCairo.get() );
518     }
519 
getCompositingSurface(const::basegfx::B2ISize & rNeededSize)520     ::cairo::SurfaceSharedPtr SpriteCanvasHelper::getCompositingSurface( const ::basegfx::B2ISize& rNeededSize )
521     {
522         if( rNeededSize.getX() < maCompositingSurfaceSize.getX() ||
523             rNeededSize.getY() < maCompositingSurfaceSize.getY() )
524         {
525             // need to give buffer more size
526             mpCompositingSurface.reset();
527         }
528 
529         if( !mpCompositingSurface )
530         {
531             mpCompositingSurface =
532                 mpOwningSpriteCanvas->getWindowSurface()->getSimilar(
533                     CAIRO_CONTENT_COLOR,
534                     rNeededSize.getX(), rNeededSize.getY() );
535             maCompositingSurfaceSize = rNeededSize;
536         }
537 
538         return mpCompositingSurface;
539     }
540 }
541