Coverage Report

Created: 2025-12-08 09:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/vcl/source/animate/AnimationRenderer.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
11
 *   Licensed to the Apache Software Foundation (ASF) under one or more
12
 *   contributor license agreements. See the NOTICE file distributed
13
 *   with this work for additional information regarding copyright
14
 *   ownership. The ASF licenses this file to you under the Apache
15
 *   License, Version 2.0 (the "License"); you may not use this file
16
 *   except in compliance with the License. You may obtain a copy of
17
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18
 */
19
20
#include <memory>
21
#include <animate/AnimationRenderer.hxx>
22
23
#include <vcl/virdev.hxx>
24
#include <vcl/window.hxx>
25
#include <tools/helpers.hxx>
26
27
#include <window.h>
28
29
AnimationRenderer::AnimationRenderer( Animation* pParent, OutputDevice* pOut,
30
                            const Point& rPt, const Size& rSz,
31
                            sal_uLong nRendererId,
32
                            OutputDevice* pFirstFrameOutDev ) :
33
0
        mpParent        ( pParent ),
34
0
        mpRenderContext ( pFirstFrameOutDev ? pFirstFrameOutDev : pOut ),
35
0
        mnRendererId    ( nRendererId ),
36
0
        maOriginPt      ( rPt ),
37
0
        maLogicalSize   ( rSz ),
38
0
        maSizePx        ( mpRenderContext->LogicToPixel( maLogicalSize ) ),
39
0
        maClip          ( mpRenderContext->GetClipRegion() ),
40
0
        mpBackground    ( VclPtr<VirtualDevice>::Create() ),
41
0
        mpRestore       ( VclPtr<VirtualDevice>::Create() ),
42
0
        mnActIndex      ( 0 ),
43
0
        meLastDisposal  ( Disposal::Back ),
44
0
        mbIsPaused      ( false ),
45
0
        mbIsMarked      ( false ),
46
0
        mbIsMirroredHorizontally( maLogicalSize.Width() < 0 ),
47
0
        mbIsMirroredVertically( maLogicalSize.Height() < 0 )
48
0
{
49
0
    Animation::ImplIncAnimCount();
50
51
    // Mirrored horizontally?
52
0
    if( mbIsMirroredHorizontally )
53
0
    {
54
0
        maDispPt.setX( maOriginPt.X() + maLogicalSize.Width() + 1 );
55
0
        maDispSz.setWidth( -maLogicalSize.Width() );
56
0
        maSizePx.setWidth( -maSizePx.Width() );
57
0
    }
58
0
    else
59
0
    {
60
0
        maDispPt.setX( maOriginPt.X() );
61
0
        maDispSz.setWidth( maLogicalSize.Width() );
62
0
    }
63
64
    // Mirrored vertically?
65
0
    if( mbIsMirroredVertically )
66
0
    {
67
0
        maDispPt.setY( maOriginPt.Y() + maLogicalSize.Height() + 1 );
68
0
        maDispSz.setHeight( -maLogicalSize.Height() );
69
0
        maSizePx.setHeight( -maSizePx.Height() );
70
0
    }
71
0
    else
72
0
    {
73
0
        maDispPt.setY( maOriginPt.Y() );
74
0
        maDispSz.setHeight( maLogicalSize.Height() );
75
0
    }
76
77
    // save background
78
0
    mpBackground->SetOutputSizePixel( maSizePx );
79
0
    mpRenderContext->SaveBackground(*mpBackground, maDispPt, maDispSz, maSizePx);
80
81
    // Initialize drawing to actual position
82
0
    drawToIndex( mpParent->ImplGetCurPos() );
83
84
    // If first frame OutputDevice is set, update variables now for real OutputDevice
85
0
    if( pFirstFrameOutDev )
86
0
    {
87
0
        mpRenderContext = pOut;
88
0
        maClip = mpRenderContext->GetClipRegion();
89
0
    }
90
0
}
91
92
AnimationRenderer::~AnimationRenderer()
93
0
{
94
0
    mpBackground.disposeAndClear();
95
0
    mpRestore.disposeAndClear();
96
97
0
    Animation::ImplDecAnimCount();
98
0
}
99
100
bool AnimationRenderer::matches(const OutputDevice* pOut, tools::Long nRendererId) const
101
0
{
102
0
    return (!pOut || pOut == mpRenderContext) && (nRendererId == 0 || nRendererId == mnRendererId);
103
0
}
104
105
void AnimationRenderer::getPosSize( const AnimationFrame& rAnimationFrame, Point& rPosPix, Size& rSizePix )
106
0
{
107
0
    const Size& rAnmSize = mpParent->GetDisplaySizePixel();
108
0
    Point       aPt2( rAnimationFrame.maPositionPixel.X() + rAnimationFrame.maSizePixel.Width() - 1,
109
0
                      rAnimationFrame.maPositionPixel.Y() + rAnimationFrame.maSizePixel.Height() - 1 );
110
0
    double      fFactX, fFactY;
111
112
    // calculate x scaling
113
0
    if( rAnmSize.Width() > 1 )
114
0
        fFactX = static_cast<double>( maSizePx.Width() - 1 ) / ( rAnmSize.Width() - 1 );
115
0
    else
116
0
        fFactX = 1.0;
117
118
    // calculate y scaling
119
0
    if( rAnmSize.Height() > 1 )
120
0
        fFactY = static_cast<double>( maSizePx.Height() - 1 ) / ( rAnmSize.Height() - 1 );
121
0
    else
122
0
        fFactY = 1.0;
123
124
0
    rPosPix.setX(basegfx::fround<tools::Long>(rAnimationFrame.maPositionPixel.X() * fFactX));
125
0
    rPosPix.setY(basegfx::fround<tools::Long>(rAnimationFrame.maPositionPixel.Y() * fFactY));
126
127
0
    aPt2.setX(basegfx::fround<tools::Long>(aPt2.X() * fFactX));
128
0
    aPt2.setY(basegfx::fround<tools::Long>(aPt2.Y() * fFactY));
129
130
0
    rSizePix.setWidth( aPt2.X() - rPosPix.X() + 1 );
131
0
    rSizePix.setHeight( aPt2.Y() - rPosPix.Y() + 1 );
132
133
    // Mirrored horizontally?
134
0
    if( mbIsMirroredHorizontally )
135
0
        rPosPix.setX( maSizePx.Width() - 1 - aPt2.X() );
136
137
    // Mirrored vertically?
138
0
    if( mbIsMirroredVertically )
139
0
        rPosPix.setY( maSizePx.Height() - 1 - aPt2.Y() );
140
0
}
141
142
void AnimationRenderer::drawToIndex( sal_uLong nIndex )
143
0
{
144
0
    VclPtr<vcl::RenderContext> pRenderContext = mpRenderContext;
145
146
0
    vcl::PaintBufferGuardPtr pGuard;
147
0
    if (mpRenderContext->GetOutDevType() == OUTDEV_WINDOW)
148
0
    {
149
0
        vcl::Window* pWindow = static_cast<vcl::WindowOutputDevice*>(mpRenderContext.get())->GetOwnerWindow();
150
0
        pGuard.reset(new vcl::PaintBufferGuard(pWindow->ImplGetWindowImpl()->mpFrameData, pWindow));
151
0
        pRenderContext = pGuard->GetRenderContext();
152
0
    }
153
154
0
    ScopedVclPtrInstance<VirtualDevice> aVDev;
155
0
    std::optional<vcl::Region> xOldClip;
156
0
    if (!maClip.IsNull())
157
0
        xOldClip = pRenderContext->GetClipRegion();
158
159
0
    aVDev->SetOutputSizePixel( maSizePx, false );
160
0
    nIndex = std::min( nIndex, static_cast<sal_uLong>(mpParent->Count()) - 1 );
161
162
0
    for( sal_uLong i = 0; i <= nIndex; i++ )
163
0
        draw( i, aVDev.get() );
164
165
0
    if (xOldClip)
166
0
        pRenderContext->SetClipRegion( maClip );
167
168
0
    pRenderContext->DrawOutDev( maDispPt, maDispSz, Point(), maSizePx, *aVDev );
169
0
    if (pGuard)
170
0
        pGuard->SetPaintRect(tools::Rectangle(maDispPt, maDispSz));
171
172
0
    if (xOldClip)
173
0
        pRenderContext->SetClipRegion(*xOldClip);
174
0
}
175
176
void AnimationRenderer::draw( sal_uLong nIndex, VirtualDevice* pVDev )
177
0
{
178
0
    VclPtr<vcl::RenderContext> pRenderContext = mpRenderContext;
179
180
0
    vcl::PaintBufferGuardPtr pGuard;
181
0
    if (!pVDev && mpRenderContext->GetOutDevType() == OUTDEV_WINDOW)
182
0
    {
183
0
        vcl::Window* pWindow = static_cast<vcl::WindowOutputDevice*>(mpRenderContext.get())->GetOwnerWindow();
184
0
        pGuard.reset(new vcl::PaintBufferGuard(pWindow->ImplGetWindowImpl()->mpFrameData, pWindow));
185
0
        pRenderContext = pGuard->GetRenderContext();
186
0
    }
187
188
0
    tools::Rectangle aOutRect( pRenderContext->PixelToLogic( Point() ), pRenderContext->GetOutputSize() );
189
190
    // check, if output lies out of display
191
0
    if( aOutRect.Intersection( tools::Rectangle( maDispPt, maDispSz ) ).IsEmpty() )
192
0
    {
193
0
        setMarked( true );
194
0
        return;
195
0
    }
196
197
0
    if(mbIsPaused)
198
0
        return;
199
200
0
    mnActIndex = std::min(nIndex, static_cast<sal_uLong>(mpParent->Count() - 1));
201
202
0
    const AnimationFrame&  rAnimationFrame = mpParent->Get( static_cast<sal_uInt16>( mnActIndex ) );
203
204
0
    Point aPosPix;
205
0
    Size aSizePix;
206
207
0
    getPosSize( rAnimationFrame, aPosPix, aSizePix );
208
209
0
    Point aBmpPosPix;
210
0
    Size aBmpSizePix;
211
212
    // Mirrored horizontally?
213
0
    if( mbIsMirroredHorizontally )
214
0
    {
215
0
        aBmpPosPix.setX( aPosPix.X() + aSizePix.Width() - 1 );
216
0
        aBmpSizePix.setWidth( -aSizePix.Width() );
217
0
    }
218
0
    else
219
0
    {
220
0
        aBmpPosPix.setX( aPosPix.X() );
221
0
        aBmpSizePix.setWidth( aSizePix.Width() );
222
0
    }
223
224
    // Mirrored vertically?
225
0
    if( mbIsMirroredVertically )
226
0
    {
227
0
        aBmpPosPix.setY( aPosPix.Y() + aSizePix.Height() - 1 );
228
0
        aBmpSizePix.setHeight( -aSizePix.Height() );
229
0
    }
230
0
    else
231
0
    {
232
0
        aBmpPosPix.setY( aPosPix.Y() );
233
0
        aBmpSizePix.setHeight( aSizePix.Height() );
234
0
    }
235
236
0
    VclPtr<VirtualDevice> pDev;
237
238
    // get output device
239
0
    if( !pVDev )
240
0
    {
241
0
        pDev = VclPtr<VirtualDevice>::Create();
242
0
        pDev->SetOutputSizePixel( maSizePx, false );
243
0
        pDev->DrawOutDev( Point(), maSizePx, maDispPt, maDispSz, *pRenderContext );
244
0
    }
245
0
    else
246
0
        pDev = pVDev;
247
248
    // restore background after each run
249
0
    if( !nIndex )
250
0
    {
251
0
        meLastDisposal = Disposal::Back;
252
0
        maRestPt = Point();
253
0
        maRestSz = maSizePx;
254
0
    }
255
256
    // restore
257
0
    if( ( Disposal::Not != meLastDisposal ) && maRestSz.Width() && maRestSz.Height() )
258
0
    {
259
0
        if( Disposal::Back == meLastDisposal )
260
0
            pDev->DrawOutDev( maRestPt, maRestSz, maRestPt, maRestSz, *mpBackground );
261
0
        else
262
0
            pDev->DrawOutDev( maRestPt, maRestSz, Point(), maRestSz, *mpRestore );
263
0
    }
264
265
0
    meLastDisposal = rAnimationFrame.meDisposal;
266
0
    maRestPt = aPosPix;
267
0
    maRestSz = aSizePix;
268
269
    // What do we need to restore the next time?
270
    // Put it into a bitmap if needed, else delete
271
    // SaveBitmap to conserve memory
272
0
    if( ( meLastDisposal == Disposal::Back ) || ( meLastDisposal == Disposal::Not ) )
273
0
        mpRestore->SetOutputSizePixel( Size( 1, 1 ), false );
274
0
    else
275
0
    {
276
0
        mpRestore->SetOutputSizePixel( maRestSz, false );
277
0
        mpRestore->DrawOutDev( Point(), maRestSz, aPosPix, aSizePix, *pDev );
278
0
    }
279
280
0
    pDev->DrawBitmap( aBmpPosPix, aBmpSizePix, rAnimationFrame.maBitmap );
281
282
0
    if (pVDev)
283
0
        return;
284
285
0
    std::optional<vcl::Region> xOldClip;
286
0
    if (!maClip.IsNull())
287
0
        xOldClip = pRenderContext->GetClipRegion();
288
289
0
    if (xOldClip)
290
0
        pRenderContext->SetClipRegion( maClip );
291
292
0
    pRenderContext->DrawOutDev( maDispPt, maDispSz, Point(), maSizePx, *pDev );
293
0
    if (pGuard)
294
0
        pGuard->SetPaintRect(tools::Rectangle(maDispPt, maDispSz));
295
296
0
    if( xOldClip)
297
0
    {
298
0
        pRenderContext->SetClipRegion(*xOldClip);
299
0
        xOldClip.reset();
300
0
    }
301
302
0
    pDev.disposeAndClear();
303
0
    pRenderContext->Flush();
304
0
}
305
306
void AnimationRenderer::repaint()
307
0
{
308
0
    const bool bOldPause = mbIsPaused;
309
310
0
    mpRenderContext->SaveBackground(*mpBackground, maDispPt, maDispSz, maSizePx);
311
312
0
    mbIsPaused = false;
313
0
    drawToIndex( mnActIndex );
314
0
    mbIsPaused = bOldPause;
315
0
}
316
317
AnimationData* AnimationRenderer::createAnimationData() const
318
0
{
319
0
    AnimationData* pDataItem = new AnimationData;
320
321
0
    pDataItem->maOriginStartPt = maOriginPt;
322
0
    pDataItem->maStartSize = maLogicalSize;
323
0
    pDataItem->mpRenderContext = mpRenderContext;
324
0
    pDataItem->mpRendererData = const_cast<AnimationRenderer *>(this);
325
0
    pDataItem->mnRendererId = mnRendererId;
326
0
    pDataItem->mbIsPaused = mbIsPaused;
327
328
0
    return pDataItem;
329
0
}
330
331
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */