/src/libreoffice/vcl/source/animate/AnimationRenderer.cxx
Line | Count | Source (jump to first uncovered line) |
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 | } |
195 | 0 | else if( !mbIsPaused ) |
196 | 0 | { |
197 | 0 | VclPtr<VirtualDevice> pDev; |
198 | 0 | Point aPosPix; |
199 | 0 | Point aBmpPosPix; |
200 | 0 | Size aSizePix; |
201 | 0 | Size aBmpSizePix; |
202 | 0 | const sal_uLong nLastPos = mpParent->Count() - 1; |
203 | 0 | mnActIndex = std::min( nIndex, nLastPos ); |
204 | 0 | const AnimationFrame& rAnimationFrame = mpParent->Get( static_cast<sal_uInt16>( mnActIndex ) ); |
205 | |
|
206 | 0 | getPosSize( rAnimationFrame, aPosPix, aSizePix ); |
207 | | |
208 | | // Mirrored horizontally? |
209 | 0 | if( mbIsMirroredHorizontally ) |
210 | 0 | { |
211 | 0 | aBmpPosPix.setX( aPosPix.X() + aSizePix.Width() - 1 ); |
212 | 0 | aBmpSizePix.setWidth( -aSizePix.Width() ); |
213 | 0 | } |
214 | 0 | else |
215 | 0 | { |
216 | 0 | aBmpPosPix.setX( aPosPix.X() ); |
217 | 0 | aBmpSizePix.setWidth( aSizePix.Width() ); |
218 | 0 | } |
219 | | |
220 | | // Mirrored vertically? |
221 | 0 | if( mbIsMirroredVertically ) |
222 | 0 | { |
223 | 0 | aBmpPosPix.setY( aPosPix.Y() + aSizePix.Height() - 1 ); |
224 | 0 | aBmpSizePix.setHeight( -aSizePix.Height() ); |
225 | 0 | } |
226 | 0 | else |
227 | 0 | { |
228 | 0 | aBmpPosPix.setY( aPosPix.Y() ); |
229 | 0 | aBmpSizePix.setHeight( aSizePix.Height() ); |
230 | 0 | } |
231 | | |
232 | | // get output device |
233 | 0 | if( !pVDev ) |
234 | 0 | { |
235 | 0 | pDev = VclPtr<VirtualDevice>::Create(); |
236 | 0 | pDev->SetOutputSizePixel( maSizePx, false ); |
237 | 0 | pDev->DrawOutDev( Point(), maSizePx, maDispPt, maDispSz, *pRenderContext ); |
238 | 0 | } |
239 | 0 | else |
240 | 0 | pDev = pVDev; |
241 | | |
242 | | // restore background after each run |
243 | 0 | if( !nIndex ) |
244 | 0 | { |
245 | 0 | meLastDisposal = Disposal::Back; |
246 | 0 | maRestPt = Point(); |
247 | 0 | maRestSz = maSizePx; |
248 | 0 | } |
249 | | |
250 | | // restore |
251 | 0 | if( ( Disposal::Not != meLastDisposal ) && maRestSz.Width() && maRestSz.Height() ) |
252 | 0 | { |
253 | 0 | if( Disposal::Back == meLastDisposal ) |
254 | 0 | pDev->DrawOutDev( maRestPt, maRestSz, maRestPt, maRestSz, *mpBackground ); |
255 | 0 | else |
256 | 0 | pDev->DrawOutDev( maRestPt, maRestSz, Point(), maRestSz, *mpRestore ); |
257 | 0 | } |
258 | |
|
259 | 0 | meLastDisposal = rAnimationFrame.meDisposal; |
260 | 0 | maRestPt = aPosPix; |
261 | 0 | maRestSz = aSizePix; |
262 | | |
263 | | // What do we need to restore the next time? |
264 | | // Put it into a bitmap if needed, else delete |
265 | | // SaveBitmap to conserve memory |
266 | 0 | if( ( meLastDisposal == Disposal::Back ) || ( meLastDisposal == Disposal::Not ) ) |
267 | 0 | mpRestore->SetOutputSizePixel( Size( 1, 1 ), false ); |
268 | 0 | else |
269 | 0 | { |
270 | 0 | mpRestore->SetOutputSizePixel( maRestSz, false ); |
271 | 0 | mpRestore->DrawOutDev( Point(), maRestSz, aPosPix, aSizePix, *pDev ); |
272 | 0 | } |
273 | |
|
274 | 0 | pDev->DrawBitmapEx( aBmpPosPix, aBmpSizePix, rAnimationFrame.maBitmapEx ); |
275 | |
|
276 | 0 | if( !pVDev ) |
277 | 0 | { |
278 | 0 | std::optional<vcl::Region> xOldClip; |
279 | 0 | if (!maClip.IsNull()) |
280 | 0 | xOldClip = pRenderContext->GetClipRegion(); |
281 | |
|
282 | 0 | if (xOldClip) |
283 | 0 | pRenderContext->SetClipRegion( maClip ); |
284 | |
|
285 | 0 | pRenderContext->DrawOutDev( maDispPt, maDispSz, Point(), maSizePx, *pDev ); |
286 | 0 | if (pGuard) |
287 | 0 | pGuard->SetPaintRect(tools::Rectangle(maDispPt, maDispSz)); |
288 | |
|
289 | 0 | if( xOldClip) |
290 | 0 | { |
291 | 0 | pRenderContext->SetClipRegion(*xOldClip); |
292 | 0 | xOldClip.reset(); |
293 | 0 | } |
294 | |
|
295 | 0 | pDev.disposeAndClear(); |
296 | 0 | pRenderContext->Flush(); |
297 | 0 | } |
298 | 0 | } |
299 | 0 | } |
300 | | |
301 | | void AnimationRenderer::repaint() |
302 | 0 | { |
303 | 0 | const bool bOldPause = mbIsPaused; |
304 | |
|
305 | 0 | mpRenderContext->SaveBackground(*mpBackground, maDispPt, maDispSz, maSizePx); |
306 | |
|
307 | 0 | mbIsPaused = false; |
308 | 0 | drawToIndex( mnActIndex ); |
309 | 0 | mbIsPaused = bOldPause; |
310 | 0 | } |
311 | | |
312 | | AnimationData* AnimationRenderer::createAnimationData() const |
313 | 0 | { |
314 | 0 | AnimationData* pDataItem = new AnimationData; |
315 | |
|
316 | 0 | pDataItem->maOriginStartPt = maOriginPt; |
317 | 0 | pDataItem->maStartSize = maLogicalSize; |
318 | 0 | pDataItem->mpRenderContext = mpRenderContext; |
319 | 0 | pDataItem->mpRendererData = const_cast<AnimationRenderer *>(this); |
320 | 0 | pDataItem->mnRendererId = mnRendererId; |
321 | 0 | pDataItem->mbIsPaused = mbIsPaused; |
322 | |
|
323 | 0 | return pDataItem; |
324 | 0 | } |
325 | | |
326 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |