# Cppcheck report - LibreOffice: canvas/source/cairo/cairo_spritecanvashelper.cxx

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567

    /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
    /*
     * This file is part of the LibreOffice project.
     *
     * This Source Code Form is subject to the terms of the Mozilla Public
     * License, v. 2.0. If a copy of the MPL was not distributed with this
     * file, You can obtain one at http://mozilla.org/MPL/2.0/.
     *
     * This file incorporates work covered by the following license notice:
     *
     *   Licensed to the Apache Software Foundation (ASF) under one or more
     *   contributor license agreements. See the NOTICE file distributed
     *   with this work for additional information regarding copyright
     *   ownership. The ASF licenses this file to you under the Apache
     *   License, Version 2.0 (the "License"); you may not use this file
     *   except in compliance with the License. You may obtain a copy of
     *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
     */

    #include <canvas/debug.hxx>
    #include <canvas/verbosetrace.hxx>
    #include <canvas/canvastools.hxx>
    #include <tools/diagnose_ex.h>

    #include <vcl/canvastools.hxx>

    #include <comphelper/scopeguard.hxx>

    #include <basegfx/range/b2drectangle.hxx>
    #include <basegfx/tools/canvastools.hxx>

    #include <boost/cast.hpp>

    #include "cairo_spritecanvashelper.hxx"
    #include "cairo_canvascustomsprite.hxx"

    using namespace ::cairo;
    using namespace ::com::sun::star;

    namespace cairocanvas
    {
        namespace
        {
            /** Sprite redraw at original position

                Used to repaint the whole canvas (background and all
                sprites)
             */
            void spriteRedraw( const CairoSharedPtr& pCairo,
                               const ::canvas::Sprite::Reference& rSprite )
            {
                // downcast to derived cairocanvas::Sprite interface, which
                // provides the actual redraw methods.
                ::boost::polymorphic_downcast< Sprite* >(rSprite.get())->redraw( pCairo, true);
            }

            void repaintBackground( const CairoSharedPtr&      pCairo,
                                    const SurfaceSharedPtr&    pBackgroundSurface,
                                    const ::basegfx::B2DRange& rArea )
            {
                cairo_save( pCairo.get() );
                cairo_rectangle( pCairo.get(), ceil( rArea.getMinX() ), ceil( rArea.getMinY() ),
                                 floor( rArea.getWidth() ), floor( rArea.getHeight() ) );
                cairo_clip( pCairo.get() );
                cairo_set_source_surface( pCairo.get(), pBackgroundSurface->getCairoSurface().get(), 0, 0 );
                cairo_set_operator( pCairo.get(), CAIRO_OPERATOR_SOURCE );
                cairo_paint( pCairo.get() );
                cairo_restore( pCairo.get() );
            }

            void opaqueUpdateSpriteArea( const ::canvas::Sprite::Reference& rSprite,
                                         const CairoSharedPtr&              pCairo,
                                         const ::basegfx::B2IRange&         rArea )
            {
                // clip output to actual update region (otherwise a)
                // wouldn't save much render time, and b) will clutter
                // scrolled sprite content outside this area)
                cairo_save( pCairo.get() );
                cairo_rectangle( pCairo.get(), rArea.getMinX(), rArea.getMinY(),
                                 sal::static_int_cast<sal_Int32>(rArea.getWidth()),
                                 sal::static_int_cast<sal_Int32>(rArea.getHeight()) );
                cairo_clip( pCairo.get() );

                // repaint affected sprite directly to output device (at
                // the actual screen output position)
                // rendering directly to device buffer
                ::boost::polymorphic_downcast< Sprite* >( rSprite.get() )->redraw( pCairo, false );

                cairo_restore( pCairo.get() );
            }

            /** Repaint sprite at original position

                Used for opaque updates, which render directly to the
                device buffer.
             */
            void spriteRedrawStub( const CairoSharedPtr& pCairo,
                                   const ::canvas::Sprite::Reference& rSprite )
            {
                if( rSprite.is() )
                {
                    ::boost::polymorphic_downcast< Sprite* >( rSprite.get() )->redraw( pCairo, false );
                }
            }

            /** Repaint sprite at given position

                Used for generic update, which renders into device buffer.
             */
            void spriteRedrawStub2( const CairoSharedPtr& pCairo,
                                    const ::canvas::Sprite::Reference&  rSprite )
            {
                if( rSprite.is() )
                {
                    ::boost::polymorphic_downcast< Sprite* >( rSprite.get() )->redraw( pCairo, true );
                }
            }

            /** Repaint sprite at original position

                Used for opaque updates from scrollUpdate(), which render
                directly to the front buffer.
             */
            void spriteRedrawStub3( const CairoSharedPtr& pCairo,
                                    const ::canvas::SpriteRedrawManager::AreaComponent& rComponent )
            {
                const ::canvas::Sprite::Reference& rSprite( rComponent.second.getSprite() );

                if( rSprite.is() )
                    ::boost::polymorphic_downcast< Sprite* >( rSprite.get() )->redraw( pCairo, false );
            }
        }

        SpriteCanvasHelper::SpriteCanvasHelper() :
            mpRedrawManager( NULL ),
            mpOwningSpriteCanvas( NULL ),
            mpCompositingSurface(),
            maCompositingSurfaceSize(),
            mbCompositingSurfaceDirty(true)
        {
        }

        void SpriteCanvasHelper::init( ::canvas::SpriteRedrawManager& rManager,
                                       SpriteCanvas&                  rDevice,
                                       const ::basegfx::B2ISize&      rSize )
        {
            mpRedrawManager = &rManager;
            mpOwningSpriteCanvas = &rDevice;

            CanvasHelper::init( rSize, rDevice, &rDevice );
        }

        void SpriteCanvasHelper::disposing()
        {
            mpCompositingSurface.reset();
            mpOwningSpriteCanvas = NULL;
            mpRedrawManager = NULL;

            // forward to base
            CanvasHelper::disposing();
        }

        uno::Reference< rendering::XAnimatedSprite > SpriteCanvasHelper::createSpriteFromAnimation(
            const uno::Reference< rendering::XAnimation >& )
        {
            return uno::Reference< rendering::XAnimatedSprite >();
        }

        uno::Reference< rendering::XAnimatedSprite > SpriteCanvasHelper::createSpriteFromBitmaps(
            const uno::Sequence< uno::Reference< rendering::XBitmap > >& /*animationBitmaps*/,
            sal_Int8                                                     /*interpolationMode*/ )
        {
            return uno::Reference< rendering::XAnimatedSprite >();
        }

        uno::Reference< rendering::XCustomSprite > SpriteCanvasHelper::createCustomSprite( const geometry::RealSize2D& spriteSize )
        {
            if( !mpRedrawManager )
                return uno::Reference< rendering::XCustomSprite >(); // we're disposed

            return uno::Reference< rendering::XCustomSprite >(
                new CanvasCustomSprite( spriteSize,
                                        mpOwningSpriteCanvas ) );
        }

        uno::Reference< rendering::XSprite > SpriteCanvasHelper::createClonedSprite(
            const uno::Reference< rendering::XSprite >& )
        {
            return uno::Reference< rendering::XSprite >();
        }

        bool SpriteCanvasHelper::updateScreen( const ::basegfx::B2IRange& /*rCurrArea*/,
                                                     bool                   bUpdateAll,
                                                     bool&                  io_bSurfaceDirty )
        {
            if( !mpRedrawManager ||
                !mpOwningSpriteCanvas ||
                !mpOwningSpriteCanvas->getWindowSurface() ||
                !mpOwningSpriteCanvas->getBufferSurface() )
            {
                return false; // disposed, or otherwise dysfunctional
            }

            SAL_INFO("canvas.cairo", "SpriteCanvasHelper::updateScreen called");

            const ::basegfx::B2ISize& rSize = mpOwningSpriteCanvas->getSizePixel();

            // force compositing surface to be available before using it
            // inside forEachSpriteArea
            SurfaceSharedPtr pCompositingSurface = getCompositingSurface(rSize);
            SurfaceSharedPtr pWindowSurface = mpOwningSpriteCanvas->getWindowSurface();
            CairoSharedPtr pCompositingCairo = pCompositingSurface->getCairo();
            CairoSharedPtr pWindowCairo = pWindowSurface->getCairo();

            // TODO(P1): Might be worthwile to track areas of background
            // changes, too.
            if( !bUpdateAll && !io_bSurfaceDirty && !mbCompositingSurfaceDirty )
            {
                // background has not changed, so we're free to optimize
                // repaint to areas where a sprite has changed

                // process each independent area of overlapping sprites
                // separately.
                mpRedrawManager->forEachSpriteArea( *this );
            }
            else
            {
                SAL_INFO("canvas.cairo", "SpriteCanvasHelper::updateScreen update ALL");

                // background has changed, so we currently have no choice
                // but repaint everything (or caller requested that)

                cairo_rectangle( pCompositingCairo.get(), 0, 0, rSize.getX(), rSize.getY() );
                cairo_clip( pCompositingCairo.get() );
                cairo_save( pCompositingCairo.get() );
                cairo_set_source_surface( pCompositingCairo.get(),
                                          mpOwningSpriteCanvas->getBufferSurface()->getCairoSurface().get(),
                                          0, 0 );
                cairo_set_operator( pCompositingCairo.get(), CAIRO_OPERATOR_SOURCE );
                cairo_paint( pCompositingCairo.get() );
                cairo_restore( pCompositingCairo.get() );

                // repaint all active sprites on top of background into
                // VDev.
                mpRedrawManager->forEachSprite(
                    ::boost::bind(
                        &spriteRedraw,
                        boost::cref(pCompositingCairo),
                        _1 ) );

                // flush to screen
                cairo_rectangle( pWindowCairo.get(), 0, 0, rSize.getX(), rSize.getY() );
                cairo_clip( pWindowCairo.get() );
                cairo_set_source_surface( pWindowCairo.get(),
                                          pCompositingSurface->getCairoSurface().get(),
                                          0, 0 );
                cairo_set_operator( pWindowCairo.get(), CAIRO_OPERATOR_SOURCE );
                cairo_paint( pWindowCairo.get() );
            }

            // change record vector must be cleared, for the next turn of
            // rendering and sprite changing
            mpRedrawManager->clearChangeRecords();

            mbCompositingSurfaceDirty = false;
            io_bSurfaceDirty = false;

            // commit to screen
            mpOwningSpriteCanvas->flush();

            return true;
        }

        void SpriteCanvasHelper::backgroundPaint( const ::basegfx::B2DRange& rUpdateRect )<--- The function 'backgroundPaint' is never used.
        {
            if( mpOwningSpriteCanvas && mpCompositingSurface )
                repaintBackground( mpCompositingSurface->getCairo(),
                                   mpOwningSpriteCanvas->getBufferSurface(),
                                   rUpdateRect );
        }

        void SpriteCanvasHelper::scrollUpdate( const ::basegfx::B2DRange&                       rMoveStart,<--- The function 'scrollUpdate' is never used.
                                               const ::basegfx::B2DRange&                       rMoveEnd,
                                               const ::canvas::SpriteRedrawManager::UpdateArea& rUpdateArea )
        {
            ENSURE_OR_THROW( mpOwningSpriteCanvas &&
                              mpOwningSpriteCanvas->getBufferSurface(),
                              "SpriteCanvasHelper::scrollUpdate(): NULL device pointer " );

            SAL_INFO("canvas.cairo", "SpriteCanvasHelper::scrollUpdate called");

            const ::basegfx::B2ISize& rSize = mpOwningSpriteCanvas->getSizePixel();
            const ::basegfx::B2IRange  aOutputBounds( 0,0,
                                                      rSize.getX(),
                                                      rSize.getY() );

            SurfaceSharedPtr pCompositingSurface = getCompositingSurface(rSize);
            SurfaceSharedPtr pWindowSurface = mpOwningSpriteCanvas->getWindowSurface();
            CairoSharedPtr pCompositingCairo = pCompositingSurface->getCairo();
            CairoSharedPtr pWindowCairo = pWindowSurface->getCairo();

            // round rectangles to integer pixel. Note: have to be
            // extremely careful here, to avoid off-by-one errors for
            // the destination area: otherwise, the next scroll update
            // would copy pixel that are not supposed to be part of
            // the sprite.
            ::basegfx::B2IRange aSourceRect(
                ::canvas::tools::spritePixelAreaFromB2DRange( rMoveStart ) );
            const ::basegfx::B2IRange& rDestRect(
                ::canvas::tools::spritePixelAreaFromB2DRange( rMoveEnd ) );
            ::basegfx::B2IPoint aDestPos( rDestRect.getMinimum() );

            ::std::vector< ::basegfx::B2IRange > aUnscrollableAreas;

            // TODO(E3): This is plain buggy (but copies the behaviour of
            // the old Impress slideshow) - the scrolled area might
            // actually lie _below_ another window!

            // clip to output bounds (cannot properly scroll stuff
            // _outside_ our screen area)
            if( !::canvas::tools::clipScrollArea( aSourceRect,
                                                  aDestPos,
                                                  aUnscrollableAreas,
                                                  aOutputBounds ) )
            {
                // fully clipped scroll area: cannot simply scroll
                // then. Perform normal opaque update (can use that, since
                // one of the preconditions for scrollable update is
                // opaque sprite content)

                // repaint all affected sprites directly to output device
                ::std::for_each( rUpdateArea.maComponentList.begin(),
                                 rUpdateArea.maComponentList.end(),
                                 ::boost::bind(
                                     &spriteRedrawStub3,
                                     boost::cref(pCompositingCairo),
                                     _1 ) );
            }
            else
            {
                const ::basegfx::B2IVector aSourceUpperLeftPos( aSourceRect.getMinimum() );

                // clip dest area (which must be inside rDestBounds)
                ::basegfx::B2IRange aDestRect( rDestRect );
                aDestRect.intersect( aOutputBounds );

                ::basegfx::B2ISize aScrollSize( aDestRect.getWidth(), aDestRect.getHeight() );
                SurfaceSharedPtr pScrollSurface( getTemporarySurface() );
                CairoSharedPtr pScrollCairo( pScrollSurface->getCairo() );

                cairo_save( pScrollCairo.get() );
                // scroll the current content of the compositing surface (and,
                // thus, of the window) in temp. surface
                cairo_set_source_surface( pScrollCairo.get(),
                                          pCompositingSurface->getCairoSurface().get(),
                                          aDestPos.getX() - aSourceUpperLeftPos.getX(),
                                          aDestPos.getY() - aSourceUpperLeftPos.getY() );
                cairo_rectangle( pScrollCairo.get(),
                        aDestPos.getX(), aDestPos.getY(),
                        aScrollSize.getX(), aScrollSize.getY() );
                cairo_clip( pScrollCairo.get() );
                cairo_set_operator( pScrollCairo.get(), CAIRO_OPERATOR_SOURCE );
                cairo_paint( pScrollCairo.get() );
                cairo_restore( pScrollCairo.get() );

                cairo_save( pCompositingCairo.get() );
                // copy the scrolled area back onto the compositing surface
                cairo_set_source_surface( pCompositingCairo.get(),
                                          pScrollSurface->getCairoSurface().get(),
                                          0, 0 );
                cairo_rectangle( pCompositingCairo.get(),
                                 aDestPos.getX(), aDestPos.getY(),
                                 aScrollSize.getX(), aScrollSize.getY() );
                cairo_clip( pCompositingCairo.get() );
                cairo_set_operator( pCompositingCairo.get(), CAIRO_OPERATOR_SOURCE );
                cairo_paint( pCompositingCairo.get() );
                cairo_restore( pCompositingCairo.get() );

                const ::canvas::SpriteRedrawManager::SpriteConnectedRanges::ComponentListType::const_iterator
                    aFirst( rUpdateArea.maComponentList.begin() );
                ::canvas::SpriteRedrawManager::SpriteConnectedRanges::ComponentListType::const_iterator
                      aSecond( aFirst ); ++aSecond;

                ENSURE_OR_THROW( aFirst->second.getSprite().is(),
                                 "VCLCanvas::scrollUpdate(): no sprite" );

                // repaint uncovered areas from sprite. Need to actually
                // clip here, since we're only repainting _parts_ of the
                // sprite
                ::std::for_each( aUnscrollableAreas.begin(),
                                 aUnscrollableAreas.end(),
                                 ::boost::bind( &opaqueUpdateSpriteArea,
                                                ::boost::cref(aFirst->second.getSprite()),
                                                boost::cref(pCompositingCairo),
                                                _1 ) );
            }

            // repaint uncovered areas from backbuffer - take the
            // _rounded_ rectangles from above, to have the update
            // consistent with the scroll above.
            ::std::vector< ::basegfx::B2DRange > aUncoveredAreas;
            ::basegfx::computeSetDifference( aUncoveredAreas,
                                             rUpdateArea.maTotalBounds,
                                             ::basegfx::B2DRange( rDestRect ) );
            ::std::for_each( aUncoveredAreas.begin(),
                             aUncoveredAreas.end(),
                             ::boost::bind( &repaintBackground,
                                            boost::cref(pCompositingCairo),
                                            mpOwningSpriteCanvas->getBufferSurface(),
                                            _1 ) );

            cairo_rectangle( pWindowCairo.get(), 0, 0, rSize.getX(), rSize.getY() );
            cairo_clip( pWindowCairo.get() );
            cairo_set_source_surface( pWindowCairo.get(),
                                      pCompositingSurface->getCairoSurface().get(),
                                      0, 0 );
            cairo_set_operator( pWindowCairo.get(), CAIRO_OPERATOR_SOURCE );
            cairo_paint( pWindowCairo.get() );
        }

        void SpriteCanvasHelper::opaqueUpdate( const ::basegfx::B2DRange&                          rTotalArea,<--- The function 'opaqueUpdate' is never used.
                                               const ::std::vector< ::canvas::Sprite::Reference >& rSortedUpdateSprites )
        {
            ENSURE_OR_THROW( mpOwningSpriteCanvas &&
                              mpOwningSpriteCanvas->getBufferSurface(),
                              "SpriteCanvasHelper::opaqueUpdate(): NULL device pointer " );

            SAL_INFO("canvas.cairo", "SpriteCanvasHelper::opaqueUpdate called");

            const ::basegfx::B2ISize& rDeviceSize = mpOwningSpriteCanvas->getSizePixel();

            SurfaceSharedPtr pCompositingSurface = getCompositingSurface(rDeviceSize);
            SurfaceSharedPtr pWindowSurface = mpOwningSpriteCanvas->getWindowSurface();
            CairoSharedPtr pCompositingCairo = pCompositingSurface->getCairo();
            CairoSharedPtr pWindowCairo = pWindowSurface->getCairo();

            cairo_rectangle( pCompositingCairo.get(), 0, 0, rDeviceSize.getX(), rDeviceSize.getY() );
            cairo_clip( pCompositingCairo.get() );

            ::basegfx::B2DVector aPos( ceil( rTotalArea.getMinX() ), ceil( rTotalArea.getMinY() ) );
            ::basegfx::B2DVector aSize( floor( rTotalArea.getMaxX() - aPos.getX() ), floor( rTotalArea.getMaxY() - aPos.getY() ) );

            cairo_rectangle( pCompositingCairo.get(), aPos.getX(), aPos.getY(), aSize.getX(), aSize.getY() );
            cairo_clip( pCompositingCairo.get() );

            // repaint all affected sprites directly to output device
            ::std::for_each( rSortedUpdateSprites.begin(),
                             rSortedUpdateSprites.end(),
                             ::boost::bind(
                                 &spriteRedrawStub,
                                 boost::cref(pCompositingCairo),
                                 _1 ) );

            // flush to screen
            cairo_rectangle( pWindowCairo.get(), 0, 0, rDeviceSize.getX(), rDeviceSize.getY() );
            cairo_clip( pWindowCairo.get() );
            cairo_rectangle( pWindowCairo.get(), aPos.getX(), aPos.getY(), aSize.getX(), aSize.getY() );
            cairo_clip( pWindowCairo.get() );
            cairo_set_source_surface( pWindowCairo.get(),
                                      pCompositingSurface->getCairoSurface().get(),
                                      0, 0 );
            cairo_set_operator( pWindowCairo.get(), CAIRO_OPERATOR_SOURCE );
            cairo_paint( pWindowCairo.get() );
        }

        void SpriteCanvasHelper::genericUpdate( const ::basegfx::B2DRange&                          rRequestedArea,<--- The function 'genericUpdate' is never used.
                                                const ::std::vector< ::canvas::Sprite::Reference >& rSortedUpdateSprites )
        {
            // TODO
            SAL_INFO("canvas.cairo", "SpriteCanvasHelper::genericUpdate called");

            ENSURE_OR_THROW( mpOwningSpriteCanvas &&
                             mpOwningSpriteCanvas->getBufferSurface(),
                             "SpriteCanvasHelper::genericUpdate(): NULL device pointer " );

            // limit size of update VDev to target outdev's size
            const ::basegfx::B2ISize& rSize = mpOwningSpriteCanvas->getSizePixel();

            SurfaceSharedPtr pCompositingSurface = getCompositingSurface(rSize);
            SurfaceSharedPtr pWindowSurface = mpOwningSpriteCanvas->getWindowSurface();
            CairoSharedPtr pCompositingCairo = pCompositingSurface->getCairo();
            CairoSharedPtr pWindowCairo = pWindowSurface->getCairo();

            // round output position towards zero. Don't want to truncate
            // a fraction of a sprite pixel...  Clip position at origin,
            // otherwise, truncation of size below might leave visible
            // areas uncovered by VDev.
            const Point aOutputPosition(
                ::std::max( sal_Int32( 0 ),
                            static_cast< sal_Int32 >(rRequestedArea.getMinX()) ),
                ::std::max( sal_Int32( 0 ),
                            static_cast< sal_Int32 >(rRequestedArea.getMinY()) ) );
            // round output size towards +infty. Don't want to truncate a
            // fraction of a sprite pixel... Limit size of VDev to output
            // device's area.
            const Size  aOutputSize(
                ::std::min( rSize.getX(),
                            ::canvas::tools::roundUp( rRequestedArea.getMaxX() - aOutputPosition.X()) ),
                ::std::min( rSize.getY(),
                            ::canvas::tools::roundUp( rRequestedArea.getMaxY() - aOutputPosition.Y()) ) );

            cairo_rectangle( pCompositingCairo.get(), aOutputPosition.X(), aOutputPosition.Y(), aOutputSize.Width(), aOutputSize.Height() );
            cairo_clip( pCompositingCairo.get() );

            // paint background
            cairo_save( pCompositingCairo.get() );
            cairo_set_source_surface( pCompositingCairo.get(),
                                      mpOwningSpriteCanvas->getBufferSurface()->getCairoSurface().get(),
                                      0, 0 );
            cairo_set_operator( pCompositingCairo.get(), CAIRO_OPERATOR_SOURCE );
            cairo_paint( pCompositingCairo.get() );
            cairo_restore( pCompositingCairo.get() );

            // repaint all affected sprites on top of background into
            // VDev.
            ::std::for_each( rSortedUpdateSprites.begin(),
                             rSortedUpdateSprites.end(),
                             ::boost::bind( &spriteRedrawStub2,
                                            boost::cref(pCompositingCairo),
                                            _1 ) );

            // flush to screen
            cairo_rectangle( pWindowCairo.get(), aOutputPosition.X(), aOutputPosition.Y(), aOutputSize.Width(), aOutputSize.Height() );
            cairo_clip( pWindowCairo.get() );
            cairo_set_source_surface( pWindowCairo.get(),
                                      pCompositingSurface->getCairoSurface().get(),
                                      0, 0 );
            cairo_set_operator( pWindowCairo.get(), CAIRO_OPERATOR_SOURCE );
            cairo_paint( pWindowCairo.get() );
        }

        ::cairo::SurfaceSharedPtr SpriteCanvasHelper::getCompositingSurface( const ::basegfx::B2ISize& rNeededSize )
        {
            if( rNeededSize.getX() > maCompositingSurfaceSize.getX() ||
                rNeededSize.getY() > maCompositingSurfaceSize.getY() )
            {
                // need to give buffer more size
                mpCompositingSurface.reset();
            }

            if( !mpCompositingSurface )
            {
                mpCompositingSurface = createSurface( rNeededSize );
                maCompositingSurfaceSize = rNeededSize;
                mbCompositingSurfaceDirty = true;
                mpTemporarySurface.reset();
            }

            return mpCompositingSurface;
        }

        ::cairo::SurfaceSharedPtr SpriteCanvasHelper::getTemporarySurface()
        {
            if ( !mpTemporarySurface )
                mpTemporarySurface = createSurface( maCompositingSurfaceSize );
            return mpTemporarySurface;
        }

        ::cairo::SurfaceSharedPtr SpriteCanvasHelper::createSurface( const ::basegfx::B2ISize& rNeededSize ) const
        {
            return mpOwningSpriteCanvas->getWindowSurface()->getSimilar(
                        CAIRO_CONTENT_COLOR,
                        rNeededSize.getX(), rNeededSize.getY() );
        }
    }

    /* vim:set shiftwidth=4 softtabstop=4 expandtab: */