summaryrefslogtreecommitdiffstats
path: root/canvas/source/directx/dx_spritecanvashelper.cxx
blob: 416c66ba5fcaedec27d127405fec0f6e5d6a750f (plain)
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
/* -*- 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 <sal/config.h>

#include <boost/cast.hpp>

#include <basegfx/range/b2drectangle.hxx>
#include <basegfx/utils/canvastools.hxx>
#include <comphelper/scopeguard.hxx>
#include <tools/diagnose_ex.h>

#include <canvas/canvastools.hxx>

#include "dx_canvascustomsprite.hxx"
#include "dx_spritecanvashelper.hxx"

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

namespace dxcanvas
{
    namespace
    {
        void repaintBackground( const ::basegfx::B2DRange&      rUpdateArea,
                                const ::basegfx::B2IRange&      rOutputArea,
                                const DXSurfaceBitmapSharedPtr& rBackBuffer )
        {
            // TODO(E1): Use numeric_cast to catch overflow here
            ::basegfx::B2IRange aActualArea( 0, 0,
                                             static_cast<sal_Int32>(rOutputArea.getWidth()),
                                             static_cast<sal_Int32>(rOutputArea.getHeight()) );
            aActualArea.intersect( fround( rUpdateArea ) );

            // repaint the given area of the screen with background content
            rBackBuffer->draw(aActualArea);
        }

        void spriteRedraw( const ::canvas::Sprite::Reference& rSprite )
        {
            // downcast to derived dxcanvas::Sprite interface, which
            // provides the actual redraw methods.
            ::boost::polymorphic_downcast< Sprite* >(
                rSprite.get() )->redraw();
        }
    }

    SpriteCanvasHelper::SpriteCanvasHelper() :
        mpSpriteSurface( nullptr ),
        mpRedrawManager( nullptr ),
        mpRenderModule(),
        mpSurfaceProxy(),
        mpBackBuffer(),
        maUpdateRect(),
        maScrapRect(),
        mbShowSpriteBounds( false )
    {
#if OSL_DEBUG_LEVEL > 0
        // inverse default for verbose debug mode
        mbShowSpriteBounds = true;
#endif
    }

    void SpriteCanvasHelper::init( SpriteCanvas&                                    rParent,
                                   ::canvas::SpriteRedrawManager&                   rManager,
                                   const IDXRenderModuleSharedPtr&                  rRenderModule,
                                   const std::shared_ptr<canvas::ISurfaceProxyManager>&   rSurfaceProxy,
                                   const DXSurfaceBitmapSharedPtr&                  rBackBuffer,
                                   const ::basegfx::B2ISize&                        rOutputOffset )
    {
        // init base
        setDevice( rParent );
        setTarget( rBackBuffer, rOutputOffset );

        mpSpriteSurface = &rParent;
        mpRedrawManager = &rManager;
        mpRenderModule  = rRenderModule;
        mpSurfaceProxy  = rSurfaceProxy;
        mpBackBuffer    = rBackBuffer;
    }

    void SpriteCanvasHelper::disposing()
    {
        if(mpRenderModule)
            mpRenderModule->disposing();

        mpBackBuffer.reset();
        mpRenderModule.reset();
        mpRedrawManager = nullptr;
        mpSpriteSurface = nullptr;

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

    uno::Reference< rendering::XAnimatedSprite > SpriteCanvasHelper::createSpriteFromAnimation(
        const uno::Reference< rendering::XAnimation >& /*animation*/ )
    {
        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,
                                    mpSpriteSurface,
                                    mpRenderModule,
                                    mpSurfaceProxy,
                                    mbShowSpriteBounds ) );
    }

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

    bool SpriteCanvasHelper::updateScreen( const ::basegfx::B2IRectangle& rCurrArea,
                                           bool                           bUpdateAll,
                                           bool&                          io_bSurfaceDirty )
    {
        if( !mpRedrawManager ||
            !mpRenderModule ||
            !mpBackBuffer )
        {
            return false; // disposed, or otherwise dysfunctional
        }

        // store current output area (need to tunnel that to the
        // background, scroll, opaque and general sprite repaint
        // routines)
        maScrapRect = rCurrArea;

        // clear area that needs to be blitted to screen beforehand
        maUpdateRect.reset();

        // TODO(P1): Might be worthwhile to track areas of background
        // changes, too.

        // TODO(P2): Might be worthwhile to use page-flipping only if
        // a certain percentage of screen area has changed - and
        // compose directly to the front buffer otherwise.
        if( !bUpdateAll && !io_bSurfaceDirty )
        {
            // 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 );

            // flip primary surface to screen
            // ==============================

            // perform buffer flipping
            mpRenderModule->flip( maUpdateRect,
                                  rCurrArea );
        }
        else
        {
            // limit update to parent window area (ignored for fullscreen)
            // TODO(E1): Use numeric_cast to catch overflow here
            const ::basegfx::B2IRectangle aUpdateArea( 0,0,
                                                       static_cast<sal_Int32>(rCurrArea.getWidth()),
                                                       static_cast<sal_Int32>(rCurrArea.getHeight()) );

            // background has changed, or called requested full
            // update, or we're performing double buffering via page
            // flipping, so we currently have no choice but repaint
            // everything

            // repaint the whole screen with background content
            mpBackBuffer->draw(aUpdateArea);

            // redraw sprites
            mpRedrawManager->forEachSprite( &spriteRedraw );

            // flip primary surface to screen
            // ==============================

            // perform buffer flipping
            mpRenderModule->flip( aUpdateArea,
                                  rCurrArea );
        }

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

        io_bSurfaceDirty = false;

        return true;
    }

    void SpriteCanvasHelper::backgroundPaint( const ::basegfx::B2DRange& rUpdateRect )
    {
        ENSURE_OR_THROW( mpRenderModule &&
                          mpBackBuffer,
                          "SpriteCanvasHelper::backgroundPaint(): NULL device pointer " );

        repaintBackground( rUpdateRect,
                           maScrapRect,
                           mpBackBuffer );
    }

    void SpriteCanvasHelper::scrollUpdate( const ::basegfx::B2DRange&                       /*rMoveStart*/,
                                           const ::basegfx::B2DRange&                       rMoveEnd,
                                           const ::canvas::SpriteRedrawManager::UpdateArea& rUpdateArea )
    {
        ENSURE_OR_THROW( mpRenderModule &&
                          mpBackBuffer,
                          "SpriteCanvasHelper::scrollUpdate(): NULL device pointer " );

        // 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.
        const ::basegfx::B2IRange& rDestRect(
            ::canvas::tools::spritePixelAreaFromB2DRange( rMoveEnd ) );

        // not much sense in really implementing scrollUpdate here,
        // since outputting a sprite only partially would result in
        // expensive clipping. Furthermore, we cannot currently render
        // 3D directly to the front buffer, thus, would have to blit
        // the full sprite area, anyway. But at least optimized in the
        // sense that unnecessary background paints behind the sprites
        // are avoided.
        for( const auto& rComponent : rUpdateArea.maComponentList )
        {
            const ::canvas::Sprite::Reference& rSprite( rComponent.second.getSprite() );

            if( rSprite.is() )
            {
                // downcast to derived dxcanvas::Sprite interface, which
                // provides the actual redraw methods.
                ::boost::polymorphic_downcast< Sprite* >( rSprite.get() )->redraw();
            }
        }

        // 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 ) );
        for( const auto& rUncoveredArea : aUncoveredAreas )
            repaintBackground( rUncoveredArea, maScrapRect, mpBackBuffer );

        // TODO(E1): Use numeric_cast to catch overflow here
        ::basegfx::B2IRange aActualArea( 0, 0,
                                         static_cast<sal_Int32>(maScrapRect.getWidth()),
                                         static_cast<sal_Int32>(maScrapRect.getHeight()) );
        aActualArea.intersect( fround( rUpdateArea.maTotalBounds ) );

        // add given update area to the 'blit to foreground' rect
        maUpdateRect.expand( aActualArea );
    }

    void SpriteCanvasHelper::opaqueUpdate( const ::basegfx::B2DRange&                          rTotalArea,
                                           const std::vector< ::canvas::Sprite::Reference >& rSortedUpdateSprites )
    {
        ENSURE_OR_THROW( mpRenderModule &&
                          mpBackBuffer,
                          "SpriteCanvasHelper::opaqueUpdate(): NULL device pointer " );

        // TODO(P2): optimize this by truly rendering to the front
        // buffer. Currently, we've the 3D device only for the back
        // buffer.
        for( const auto& rSprite : rSortedUpdateSprites )
        {
            if( rSprite.is() )
            {
                // downcast to derived dxcanvas::Sprite interface, which
                // provides the actual redraw methods.
                ::boost::polymorphic_downcast< Sprite* >( rSprite.get() )->redraw();
            }
        }

        // TODO(E1): Use numeric_cast to catch overflow here
        ::basegfx::B2IRange aActualArea( 0, 0,
                                         static_cast<sal_Int32>(maScrapRect.getWidth()),
                                         static_cast<sal_Int32>(maScrapRect.getHeight()) );
        aActualArea.intersect( fround( rTotalArea ) );

        // add given update area to the 'blit to foreground' rect
        maUpdateRect.expand( aActualArea );
    }

    void SpriteCanvasHelper::genericUpdate( const ::basegfx::B2DRange&                          rTotalArea,
                                            const std::vector< ::canvas::Sprite::Reference >& rSortedUpdateSprites )
    {
        ENSURE_OR_THROW( mpRenderModule &&
                          mpBackBuffer,
                          "SpriteCanvasHelper::genericUpdate(): NULL device pointer " );

        // paint background
        // ================

        // TODO(E1): Use numeric_cast to catch overflow here
        ::basegfx::B2IRange aActualArea( 0, 0,
                                         static_cast<sal_Int32>(maScrapRect.getWidth()),
                                         static_cast<sal_Int32>(maScrapRect.getHeight()) );
        aActualArea.intersect( fround( rTotalArea ) );

        // repaint the given area of the screen with background content
        mpBackBuffer->draw(aActualArea);

        // paint sprite
        // ============

        for( const auto& rSprite : rSortedUpdateSprites )
        {
            if( rSprite.is() )
            {
                // downcast to derived dxcanvas::Sprite interface, which
                // provides the actual redraw methods.
                ::boost::polymorphic_downcast< Sprite* >( rSprite.get() )->redraw();
            }
        }

        // add given update area to the 'blit to foreground' rect
        maUpdateRect.expand( aActualArea );
    }
}

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