/src/libreoffice/drawinglayer/source/primitive2d/graphicprimitivehelper2d.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 <sal/config.h> |
21 | | |
22 | | #include <algorithm> |
23 | | |
24 | | #include <drawinglayer/primitive2d/graphicprimitivehelper2d.hxx> |
25 | | #include <drawinglayer/animation/animationtiming.hxx> |
26 | | #include <drawinglayer/primitive2d/bitmapprimitive2d.hxx> |
27 | | #include <drawinglayer/primitive2d/BitmapAlphaPrimitive2D.hxx> |
28 | | #include <drawinglayer/primitive2d/unifiedtransparenceprimitive2d.hxx> |
29 | | #include <drawinglayer/primitive2d/animatedprimitive2d.hxx> |
30 | | #include <drawinglayer/primitive2d/metafileprimitive2d.hxx> |
31 | | #include <drawinglayer/primitive2d/transformprimitive2d.hxx> |
32 | | #include <drawinglayer/primitive2d/maskprimitive2d.hxx> |
33 | | #include <drawinglayer/primitive2d/modifiedcolorprimitive2d.hxx> |
34 | | #include <drawinglayer/primitive2d/fillgraphicprimitive2d.hxx> |
35 | | #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx> |
36 | | #include <drawinglayer/geometry/viewinformation2d.hxx> |
37 | | #include <basegfx/polygon/b2dpolygon.hxx> |
38 | | #include <basegfx/polygon/b2dpolygontools.hxx> |
39 | | #include <basegfx/numeric/ftools.hxx> |
40 | | |
41 | | // helper class for animated graphics |
42 | | |
43 | | #include <utility> |
44 | | #include <vcl/animate/Animation.hxx> |
45 | | #include <vcl/animate/AnimationFrame.hxx> |
46 | | #include <vcl/alpha.hxx> |
47 | | #include <vcl/graph.hxx> |
48 | | #include <vcl/GraphicAttributes.hxx> |
49 | | #include <vcl/virdev.hxx> |
50 | | #include <vcl/svapp.hxx> |
51 | | #include <vcl/skia/SkiaHelper.hxx> |
52 | | #include <vcl/vectorgraphicdata.hxx> |
53 | | |
54 | | namespace drawinglayer::primitive2d |
55 | | { |
56 | | namespace { |
57 | | |
58 | | class AnimatedGraphicPrimitive2D : public AnimatedSwitchPrimitive2D |
59 | | { |
60 | | private: |
61 | | /// the geometric definition |
62 | | basegfx::B2DHomMatrix maTransform; |
63 | | |
64 | | /** the Graphic with all its content possibilities, here only |
65 | | animated is allowed and gets checked by isValidData(). |
66 | | an instance of Graphic is used here since it's ref-counted |
67 | | and thus a safe copy for now |
68 | | */ |
69 | | Graphic maGraphic; |
70 | | |
71 | | /** defines parameters for tiling if this AnimatedGraphicPrimitive2D |
72 | | is to be used for a FillGraphicPrimitive2D. In that case, |
73 | | maFillGraphicAttribute.isDefault() will be false |
74 | | */ |
75 | | drawinglayer::attribute::FillGraphicAttribute maFillGraphicAttribute; |
76 | | |
77 | | /// local animation processing data, excerpt from maGraphic |
78 | | ::Animation maAnimation; |
79 | | |
80 | | /// the transparency in range [0.0 .. 1.0] |
81 | | double mfTransparency; |
82 | | |
83 | | /// the on-demand created VirtualDevices for frame creation |
84 | | ScopedVclPtrInstance< VirtualDevice > maVirtualDevice; |
85 | | ScopedVclPtrInstance< VirtualDevice > maVirtualDeviceMask; |
86 | | |
87 | | // index of the next frame that would be regularly prepared |
88 | | sal_uInt32 mnNextFrameToPrepare; |
89 | | |
90 | | /// buffering of 1st frame (always active) |
91 | | Primitive2DReference maBufferedFirstFrame; |
92 | | |
93 | | /// buffering of all frames |
94 | | std::vector<Primitive2DReference> maBufferedPrimitives; |
95 | | bool mbBufferingAllowed; |
96 | | |
97 | | /// set if the animation is huge so that just always the next frame |
98 | | /// is used instead of using timing |
99 | | bool mbHugeSize; |
100 | | |
101 | | /// helper methods |
102 | | bool isValidData() const |
103 | 0 | { |
104 | 0 | return (GraphicType::Bitmap == maGraphic.GetType() |
105 | 0 | && maGraphic.IsAnimated() |
106 | 0 | && maAnimation.Count() |
107 | 0 | && !basegfx::fTools::equal(getTransparency(), 1.0)); |
108 | 0 | } |
109 | | |
110 | | void ensureVirtualDeviceSizeAndState() |
111 | 0 | { |
112 | 0 | if (!isValidData()) |
113 | 0 | return; |
114 | | |
115 | 0 | const Size aCurrent(maVirtualDevice->GetOutputSizePixel()); |
116 | 0 | const Size aTarget(maAnimation.GetDisplaySizePixel()); |
117 | |
|
118 | 0 | if (aCurrent != aTarget) |
119 | 0 | { |
120 | 0 | maVirtualDevice->EnableMapMode(false); |
121 | 0 | maVirtualDeviceMask->EnableMapMode(false); |
122 | 0 | maVirtualDevice->SetOutputSizePixel(aTarget); |
123 | 0 | maVirtualDeviceMask->SetOutputSizePixel(aTarget); |
124 | | |
125 | | // tdf#156630 make erase calls fill with transparency |
126 | 0 | maVirtualDevice->SetBackground(COL_BLACK); |
127 | 0 | maVirtualDeviceMask->SetBackground(COL_ALPHA_TRANSPARENT); |
128 | 0 | } |
129 | |
|
130 | 0 | maVirtualDevice->Erase(); |
131 | 0 | maVirtualDeviceMask->Erase(); |
132 | 0 | const ::tools::Rectangle aRect(Point(0, 0), aTarget); |
133 | 0 | maVirtualDeviceMask->SetFillColor(COL_BLACK); |
134 | 0 | maVirtualDeviceMask->SetLineColor(); |
135 | 0 | maVirtualDeviceMask->DrawRect(aRect); |
136 | 0 | } |
137 | | |
138 | | sal_uInt32 generateStepTime(sal_uInt32 nIndex) const |
139 | 0 | { |
140 | 0 | const AnimationFrame& rAnimationFrame = maAnimation.Get(sal_uInt16(nIndex)); |
141 | 0 | sal_uInt32 nWaitTime(rAnimationFrame.mnWait * 10); |
142 | | |
143 | | // Take care of special value for MultiPage TIFFs. ATM these shall just |
144 | | // show their first page. Later we will offer some switching when object |
145 | | // is selected. |
146 | 0 | if (ANIMATION_TIMEOUT_ON_CLICK == rAnimationFrame.mnWait) |
147 | 0 | { |
148 | | // ATM the huge value would block the timer, so |
149 | | // use a long time to show first page (whole day) |
150 | 0 | nWaitTime = 100 * 60 * 60 * 24; |
151 | 0 | } |
152 | | |
153 | | // Bad trap: There are animated gifs with no set WaitTime (!). |
154 | | // In that case use a default value. |
155 | 0 | if (0 == nWaitTime) |
156 | 0 | { |
157 | 0 | nWaitTime = 100; |
158 | 0 | } |
159 | |
|
160 | 0 | return nWaitTime; |
161 | 0 | } |
162 | | |
163 | | void createAndSetAnimationTiming() |
164 | 0 | { |
165 | 0 | if (!isValidData()) |
166 | 0 | return; |
167 | | |
168 | 0 | animation::AnimationEntryLoop aAnimationLoop(maAnimation.GetLoopCount() ? maAnimation.GetLoopCount() : 0xffff); |
169 | 0 | const sal_uInt32 nCount(maAnimation.Count()); |
170 | |
|
171 | 0 | for (sal_uInt32 a(0); a < nCount; a++) |
172 | 0 | { |
173 | 0 | const sal_uInt32 aStepTime(generateStepTime(a)); |
174 | 0 | const animation::AnimationEntryFixed aTime(static_cast<double>(aStepTime), static_cast<double>(a) / static_cast<double>(nCount)); |
175 | |
|
176 | 0 | aAnimationLoop.append(aTime); |
177 | 0 | } |
178 | |
|
179 | 0 | animation::AnimationEntryList aAnimationEntryList; |
180 | 0 | aAnimationEntryList.append(aAnimationLoop); |
181 | |
|
182 | 0 | setAnimationEntry(aAnimationEntryList); |
183 | 0 | } |
184 | | |
185 | | Primitive2DReference createFromBuffer() const |
186 | 0 | { |
187 | | // create Bitmap by extracting from VirtualDevices |
188 | 0 | const Bitmap aMainBitmap(maVirtualDevice->GetBitmap(Point(), maVirtualDevice->GetOutputSizePixel())); |
189 | 0 | bool useAlphaMask = false; |
190 | | #if defined(MACOSX) || defined(IOS) |
191 | | useAlphaMask = true; |
192 | | #else |
193 | | // GetBitmap()-> AlphaMask is optimized with SkiaSalBitmap::InterpretAs8Bit(), 1bpp mask is not. |
194 | 0 | if( SkiaHelper::isVCLSkiaEnabled()) |
195 | 0 | useAlphaMask = true; |
196 | 0 | #endif |
197 | 0 | Bitmap bitmap; |
198 | 0 | if( useAlphaMask ) |
199 | 0 | { |
200 | 0 | const AlphaMask aMaskBitmap(maVirtualDeviceMask->GetBitmap(Point(), maVirtualDeviceMask->GetOutputSizePixel())); |
201 | 0 | bitmap = Bitmap(aMainBitmap, aMaskBitmap); |
202 | 0 | } |
203 | 0 | else |
204 | 0 | { |
205 | 0 | Bitmap aMaskBitmap(maVirtualDeviceMask->GetBitmap(Point(), maVirtualDeviceMask->GetOutputSizePixel())); |
206 | | // tdf#156630 invert the alpha mask |
207 | 0 | aMaskBitmap.Invert(); // convert from transparency to alpha |
208 | 0 | bitmap = Bitmap(aMainBitmap, aMaskBitmap); |
209 | 0 | } |
210 | |
|
211 | 0 | if (!maFillGraphicAttribute.isDefault()) |
212 | 0 | { |
213 | | // need to create FillGraphicPrimitive2D |
214 | 0 | const drawinglayer::attribute::FillGraphicAttribute aAttribute( |
215 | 0 | Graphic(bitmap), |
216 | 0 | maFillGraphicAttribute.getGraphicRange(), |
217 | 0 | maFillGraphicAttribute.getTiling(), |
218 | 0 | maFillGraphicAttribute.getOffsetX(), |
219 | 0 | maFillGraphicAttribute.getOffsetY()); |
220 | |
|
221 | 0 | return new FillGraphicPrimitive2D( |
222 | 0 | getTransform(), |
223 | 0 | aAttribute, |
224 | 0 | getTransparency()); |
225 | 0 | } |
226 | | |
227 | | // need to create BitmapAlphaPrimitive2D/BitmapPrimitive2D |
228 | 0 | if (basegfx::fTools::equal(getTransparency(), 0.0)) |
229 | 0 | return new BitmapPrimitive2D(bitmap, getTransform()); |
230 | | |
231 | 0 | return new BitmapAlphaPrimitive2D(bitmap, getTransform(), getTransparency()); |
232 | 0 | } |
233 | | |
234 | | void checkSafeToBuffer(sal_uInt32 nIndex) |
235 | 0 | { |
236 | 0 | if (mbBufferingAllowed) |
237 | 0 | { |
238 | | // all frames buffered |
239 | 0 | if (!maBufferedPrimitives.empty() && nIndex < maBufferedPrimitives.size()) |
240 | 0 | { |
241 | 0 | if (!maBufferedPrimitives[nIndex].is()) |
242 | 0 | { |
243 | 0 | maBufferedPrimitives[nIndex] = createFromBuffer(); |
244 | | |
245 | | // check if buffering is complete |
246 | 0 | bool bBufferingComplete(true); |
247 | |
|
248 | 0 | for (auto const & a: maBufferedPrimitives) |
249 | 0 | { |
250 | 0 | if (!a.is()) |
251 | 0 | { |
252 | 0 | bBufferingComplete = false; |
253 | 0 | break; |
254 | 0 | } |
255 | 0 | } |
256 | |
|
257 | 0 | if (bBufferingComplete) |
258 | 0 | { |
259 | 0 | maVirtualDevice.disposeAndClear(); |
260 | 0 | maVirtualDeviceMask.disposeAndClear(); |
261 | 0 | } |
262 | 0 | } |
263 | 0 | } |
264 | 0 | } |
265 | 0 | else |
266 | 0 | { |
267 | | // always buffer first frame |
268 | 0 | if (0 == nIndex && !maBufferedFirstFrame.is()) |
269 | 0 | { |
270 | 0 | maBufferedFirstFrame = createFromBuffer(); |
271 | 0 | } |
272 | 0 | } |
273 | 0 | } |
274 | | |
275 | | void createFrame(sal_uInt32 nTarget) |
276 | 0 | { |
277 | | // mnNextFrameToPrepare is the target frame to create next (which implies that |
278 | | // mnNextFrameToPrepare-1 *is* currently in the VirtualDevice when |
279 | | // 0 != mnNextFrameToPrepare. nTarget is the target frame. |
280 | 0 | if (!isValidData()) |
281 | 0 | return; |
282 | | |
283 | 0 | if (mnNextFrameToPrepare > nTarget) |
284 | 0 | { |
285 | | // we are ahead request, reset mechanism to start at frame zero |
286 | 0 | ensureVirtualDeviceSizeAndState(); |
287 | 0 | mnNextFrameToPrepare = 0; |
288 | 0 | } |
289 | |
|
290 | 0 | while (mnNextFrameToPrepare <= nTarget) |
291 | 0 | { |
292 | | // prepare step |
293 | 0 | const AnimationFrame& rAnimationFrame = maAnimation.Get(sal_uInt16(mnNextFrameToPrepare)); |
294 | |
|
295 | 0 | bool bSourceBlending = rAnimationFrame.meBlend == Blend::Source; |
296 | |
|
297 | 0 | if (bSourceBlending) |
298 | 0 | { |
299 | 0 | tools::Rectangle aArea(rAnimationFrame.maPositionPixel, rAnimationFrame.maBitmap.GetSizePixel()); |
300 | 0 | maVirtualDevice->Erase(aArea); |
301 | 0 | maVirtualDeviceMask->Erase(aArea); |
302 | 0 | } |
303 | |
|
304 | 0 | switch (rAnimationFrame.meDisposal) |
305 | 0 | { |
306 | 0 | case Disposal::Not: |
307 | 0 | { |
308 | 0 | maVirtualDevice->DrawBitmap(rAnimationFrame.maPositionPixel, rAnimationFrame.maBitmap); |
309 | |
|
310 | 0 | if (!rAnimationFrame.maBitmap.HasAlpha()) |
311 | 0 | { |
312 | 0 | const Point aEmpty; |
313 | 0 | const ::tools::Rectangle aRect(aEmpty, maVirtualDeviceMask->GetOutputSizePixel()); |
314 | 0 | const Wallpaper aWallpaper(COL_BLACK); |
315 | 0 | maVirtualDeviceMask->DrawWallpaper(aRect, aWallpaper); |
316 | 0 | } |
317 | 0 | else |
318 | 0 | { |
319 | 0 | AlphaMask aAlphaMask = rAnimationFrame.maBitmap.CreateAlphaMask(); |
320 | 0 | Bitmap aExpandVisibilityMask(aAlphaMask.GetBitmap(), aAlphaMask); |
321 | 0 | maVirtualDeviceMask->DrawBitmap(rAnimationFrame.maPositionPixel, aExpandVisibilityMask); |
322 | 0 | } |
323 | |
|
324 | 0 | break; |
325 | 0 | } |
326 | 0 | case Disposal::Back: |
327 | 0 | { |
328 | | // #i70772# react on no mask, for primitives, too. |
329 | |
|
330 | 0 | maVirtualDeviceMask->Erase(); |
331 | 0 | maVirtualDevice->DrawBitmap(rAnimationFrame.maPositionPixel, rAnimationFrame.maBitmap); |
332 | |
|
333 | 0 | if (!rAnimationFrame.maBitmap.HasAlpha()) |
334 | 0 | { |
335 | 0 | const ::tools::Rectangle aRect(rAnimationFrame.maPositionPixel, rAnimationFrame.maBitmap.GetSizePixel()); |
336 | 0 | maVirtualDeviceMask->SetFillColor(COL_BLACK); |
337 | 0 | maVirtualDeviceMask->SetLineColor(); |
338 | 0 | maVirtualDeviceMask->DrawRect(aRect); |
339 | 0 | } |
340 | 0 | else |
341 | 0 | { |
342 | 0 | const AlphaMask aMask(rAnimationFrame.maBitmap.CreateAlphaMask()); |
343 | 0 | Bitmap aExpandVisibilityMask(aMask.GetBitmap(), aMask); |
344 | 0 | maVirtualDeviceMask->DrawBitmap(rAnimationFrame.maPositionPixel, aExpandVisibilityMask); |
345 | 0 | } |
346 | |
|
347 | 0 | break; |
348 | 0 | } |
349 | 0 | case Disposal::Previous: |
350 | 0 | { |
351 | 0 | maVirtualDevice->DrawBitmap(rAnimationFrame.maPositionPixel, rAnimationFrame.maBitmap); |
352 | 0 | Bitmap aExpandVisibilityMask(rAnimationFrame.maBitmap.CreateAlphaMask().GetBitmap(), rAnimationFrame.maBitmap.CreateAlphaMask()); |
353 | 0 | maVirtualDeviceMask->DrawBitmap(rAnimationFrame.maPositionPixel, aExpandVisibilityMask); |
354 | 0 | break; |
355 | 0 | } |
356 | 0 | } |
357 | | |
358 | | // to not waste created data, check adding to buffers |
359 | 0 | checkSafeToBuffer(mnNextFrameToPrepare); |
360 | |
|
361 | 0 | mnNextFrameToPrepare++; |
362 | 0 | } |
363 | 0 | } |
364 | | |
365 | | Primitive2DReference tryTogetFromBuffer(sal_uInt32 nIndex) const |
366 | 0 | { |
367 | 0 | if (mbBufferingAllowed) |
368 | 0 | { |
369 | | // all frames buffered, check if available |
370 | 0 | if (!maBufferedPrimitives.empty() && nIndex < maBufferedPrimitives.size()) |
371 | 0 | { |
372 | 0 | if (maBufferedPrimitives[nIndex].is()) |
373 | 0 | { |
374 | 0 | return maBufferedPrimitives[nIndex]; |
375 | 0 | } |
376 | 0 | } |
377 | 0 | } |
378 | 0 | else |
379 | 0 | { |
380 | | // always buffer first frame, it's sometimes requested out-of-order |
381 | 0 | if (0 == nIndex && maBufferedFirstFrame.is()) |
382 | 0 | { |
383 | 0 | return maBufferedFirstFrame; |
384 | 0 | } |
385 | 0 | } |
386 | | |
387 | 0 | return Primitive2DReference(); |
388 | 0 | } |
389 | | |
390 | | public: |
391 | | /// constructor |
392 | | AnimatedGraphicPrimitive2D( |
393 | | const Graphic& rGraphic, |
394 | | const drawinglayer::attribute::FillGraphicAttribute* pFillGraphicAttribute, |
395 | | basegfx::B2DHomMatrix aTransform, |
396 | | double fTransparency = 0.0); |
397 | | virtual ~AnimatedGraphicPrimitive2D(); |
398 | | |
399 | | /// data read access |
400 | 0 | const basegfx::B2DHomMatrix& getTransform() const { return maTransform; } |
401 | 0 | double getTransparency() const { return mfTransparency; } |
402 | | |
403 | | /// provide unique ID |
404 | 0 | virtual sal_uInt32 getPrimitive2DID() const override { return PRIMITIVE2D_ID_ANIMATEDGRAPHICPRIMITIVE2D; } |
405 | | |
406 | | /// compare operator |
407 | | virtual bool operator==(const BasePrimitive2D& rPrimitive) const override; |
408 | | |
409 | | /// override to deliver the correct expected frame dependent of timing |
410 | | virtual void get2DDecomposition(Primitive2DDecompositionVisitor& rVisitor, const geometry::ViewInformation2D& rViewInformation) const override; |
411 | | |
412 | | /// get range |
413 | | virtual basegfx::B2DRange getB2DRange(const geometry::ViewInformation2D& rViewInformation) const override; |
414 | | }; |
415 | | |
416 | | } |
417 | | |
418 | | AnimatedGraphicPrimitive2D::AnimatedGraphicPrimitive2D( |
419 | | const Graphic& rGraphic, |
420 | | const drawinglayer::attribute::FillGraphicAttribute* pFillGraphicAttribute, |
421 | | basegfx::B2DHomMatrix aTransform, |
422 | | double fTransparency) |
423 | 0 | : AnimatedSwitchPrimitive2D( |
424 | 0 | animation::AnimationEntryList(), |
425 | 0 | Primitive2DContainer(), |
426 | 0 | false), |
427 | 0 | maTransform(std::move(aTransform)), |
428 | 0 | maGraphic(rGraphic), |
429 | 0 | maFillGraphicAttribute(), |
430 | 0 | maAnimation(rGraphic.GetAnimation()), |
431 | 0 | mfTransparency(std::max(0.0, std::min(1.0, fTransparency))), |
432 | 0 | maVirtualDevice(*Application::GetDefaultDevice()), |
433 | 0 | maVirtualDeviceMask(*Application::GetDefaultDevice()), |
434 | 0 | mnNextFrameToPrepare(SAL_MAX_UINT32), |
435 | 0 | mbBufferingAllowed(false), |
436 | 0 | mbHugeSize(false) |
437 | 0 | { |
438 | | // if FillGraphicAttribute copy it -> FillGraphicPrimitive2D is intended |
439 | 0 | if (nullptr != pFillGraphicAttribute) |
440 | 0 | maFillGraphicAttribute = *pFillGraphicAttribute; |
441 | | |
442 | | // initialize AnimationTiming, needed to detect which frame is requested |
443 | | // in get2DDecomposition |
444 | 0 | createAndSetAnimationTiming(); |
445 | | |
446 | | // check if we allow buffering |
447 | 0 | if (isValidData()) |
448 | 0 | { |
449 | | // allow buffering up to a size of: |
450 | | // - 64 frames |
451 | | // - sizes of 256x256 pixels |
452 | | // This may be offered in option values if needed |
453 | 0 | static const sal_uInt64 nAllowedSize(64 * 256 * 256); |
454 | 0 | static const sal_uInt64 nHugeSize(10000000); |
455 | 0 | const Size aTarget(maAnimation.GetDisplaySizePixel()); |
456 | 0 | const sal_uInt64 nUsedSize(static_cast<sal_uInt64>(maAnimation.Count()) * aTarget.Width() * aTarget.Height()); |
457 | |
|
458 | 0 | if (nUsedSize < nAllowedSize) |
459 | 0 | { |
460 | 0 | mbBufferingAllowed = true; |
461 | 0 | } |
462 | |
|
463 | 0 | if (nUsedSize > nHugeSize) |
464 | 0 | { |
465 | 0 | mbHugeSize = true; |
466 | 0 | } |
467 | 0 | } |
468 | | |
469 | | // prepare buffer space |
470 | 0 | if (mbBufferingAllowed && isValidData()) |
471 | 0 | { |
472 | 0 | maBufferedPrimitives.resize(maAnimation.Count()); |
473 | 0 | } |
474 | 0 | } |
475 | | |
476 | | AnimatedGraphicPrimitive2D::~AnimatedGraphicPrimitive2D() |
477 | 0 | { |
478 | | // Related: tdf#158807 mutex must be locked when disposing a VirtualDevice |
479 | | // If the following .ppt document is opened in a debug build |
480 | | // and the document is left open for a minute or two without |
481 | | // changing any content, this destructor will be called on a |
482 | | // non-main thread with the mutex unlocked: |
483 | | // https://bugs.documentfoundation.org/attachment.cgi?id=46801 |
484 | | // This hits an assert in VirtualDevice::ReleaseGraphics() so |
485 | | // explicitly lock the mutex and explicitly dispose and clear |
486 | | // the VirtualDevice instances variables. |
487 | 0 | const SolarMutexGuard aSolarGuard; |
488 | |
|
489 | 0 | maVirtualDevice.disposeAndClear(); |
490 | 0 | maVirtualDeviceMask.disposeAndClear(); |
491 | 0 | maAnimation.Clear(); |
492 | 0 | maGraphic.Clear(); |
493 | 0 | } |
494 | | |
495 | | bool AnimatedGraphicPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const |
496 | 0 | { |
497 | | // do not use 'GroupPrimitive2D::operator==' here, that would compare |
498 | | // the children. Also do not use 'BasePrimitive2D::operator==', that would |
499 | | // check the ID-Type. Since we are a simple derivation without own ID, |
500 | | // use the dynamic_cast RTTI directly |
501 | 0 | const AnimatedGraphicPrimitive2D* pCompare = dynamic_cast<const AnimatedGraphicPrimitive2D*>(&rPrimitive); |
502 | | |
503 | | // use operator== of Graphic - if that is equal, the basic definition is equal |
504 | 0 | return (nullptr != pCompare |
505 | 0 | && getTransform() == pCompare->getTransform() |
506 | 0 | && maGraphic == pCompare->maGraphic); |
507 | 0 | } |
508 | | |
509 | | void AnimatedGraphicPrimitive2D::get2DDecomposition(Primitive2DDecompositionVisitor& rVisitor, const geometry::ViewInformation2D& rViewInformation) const |
510 | 0 | { |
511 | 0 | if (!isValidData()) |
512 | 0 | return; |
513 | | |
514 | 0 | Primitive2DReference aRetval; |
515 | 0 | const double fState(getAnimationEntry().getStateAtTime(rViewInformation.getViewTime())); |
516 | 0 | const sal_uInt32 nLen(maAnimation.Count()); |
517 | 0 | sal_uInt32 nIndex(basegfx::fround(fState * static_cast<double>(nLen))); |
518 | | |
519 | | // nIndex is the requested frame - it is in range [0..nLen[ |
520 | | // create frame representation in VirtualDevices |
521 | 0 | if (nIndex >= nLen) |
522 | 0 | { |
523 | 0 | nIndex = nLen - 1; |
524 | 0 | } |
525 | | |
526 | | // check buffering shortcuts, may already be created |
527 | 0 | aRetval = tryTogetFromBuffer(nIndex); |
528 | |
|
529 | 0 | if (aRetval.is()) |
530 | 0 | { |
531 | 0 | rVisitor.visit(aRetval); |
532 | 0 | return; |
533 | 0 | } |
534 | | |
535 | | // if huge size (and not the buffered 1st frame) simply |
536 | | // create next frame |
537 | 0 | if (mbHugeSize && 0 != nIndex && mnNextFrameToPrepare <= nIndex) |
538 | 0 | { |
539 | 0 | nIndex = mnNextFrameToPrepare % nLen; |
540 | 0 | } |
541 | | |
542 | | // frame not (yet) buffered or no buffering allowed, create it |
543 | 0 | const_cast<AnimatedGraphicPrimitive2D*>(this)->createFrame(nIndex); |
544 | | |
545 | | // try to get from buffer again, may have been added from createFrame |
546 | 0 | aRetval = tryTogetFromBuffer(nIndex); |
547 | |
|
548 | 0 | if (aRetval.is()) |
549 | 0 | { |
550 | 0 | rVisitor.visit(aRetval); |
551 | 0 | return; |
552 | 0 | } |
553 | | |
554 | | // did not work (not buffered and not 1st frame), create from buffer |
555 | 0 | aRetval = createFromBuffer(); |
556 | |
|
557 | 0 | rVisitor.visit(aRetval); |
558 | 0 | } |
559 | | |
560 | | basegfx::B2DRange AnimatedGraphicPrimitive2D::getB2DRange(const geometry::ViewInformation2D& rViewInformation) const |
561 | 0 | { |
562 | | // get object's range |
563 | 0 | basegfx::B2DRange aUnitRange(0.0, 0.0, 1.0, 1.0); |
564 | 0 | aUnitRange.transform(getTransform()); |
565 | | |
566 | | // intersect with visible part |
567 | 0 | aUnitRange.intersect(rViewInformation.getViewport()); |
568 | |
|
569 | 0 | return aUnitRange; |
570 | 0 | } |
571 | | |
572 | | } // end of namespace |
573 | | |
574 | | namespace drawinglayer::primitive2d |
575 | | { |
576 | | Primitive2DReference createFillGraphicPrimitive2D( |
577 | | const basegfx::B2DHomMatrix& rTransform, |
578 | | const drawinglayer::attribute::FillGraphicAttribute& rFillGraphicAttribute, |
579 | | double fTransparency) |
580 | 0 | { |
581 | 0 | if (basegfx::fTools::equal(fTransparency, 1.0)) |
582 | 0 | { |
583 | | // completely transparent, done |
584 | 0 | return nullptr; |
585 | 0 | } |
586 | | |
587 | 0 | const Graphic& rGraphic(rFillGraphicAttribute.getGraphic()); |
588 | 0 | const GraphicType aType(rGraphic.GetType()); |
589 | |
|
590 | 0 | if (GraphicType::Bitmap == aType && rGraphic.IsAnimated()) |
591 | 0 | { |
592 | 0 | return new AnimatedGraphicPrimitive2D( |
593 | 0 | rGraphic, |
594 | 0 | &rFillGraphicAttribute, |
595 | 0 | rTransform, |
596 | 0 | fTransparency); |
597 | 0 | } |
598 | | |
599 | 0 | return new FillGraphicPrimitive2D( |
600 | 0 | rTransform, |
601 | 0 | rFillGraphicAttribute, |
602 | 0 | fTransparency); |
603 | 0 | } |
604 | | |
605 | | void create2DDecompositionOfGraphic( |
606 | | Primitive2DContainer& rContainer, |
607 | | const Graphic& rGraphic, |
608 | | const basegfx::B2DHomMatrix& rTransform, |
609 | | double fTransparency) |
610 | 379 | { |
611 | 379 | if (basegfx::fTools::equal(fTransparency, 1.0)) |
612 | 0 | { |
613 | | // completely transparent, done |
614 | 0 | return; |
615 | 0 | } |
616 | | |
617 | 379 | switch(rGraphic.GetType()) |
618 | 379 | { |
619 | 13 | case GraphicType::Bitmap : |
620 | 13 | { |
621 | 13 | if(rGraphic.IsAnimated()) |
622 | 0 | { |
623 | | // prepare specialized AnimatedGraphicPrimitive2D, now with |
624 | | // support for alpha |
625 | 0 | rContainer.append(new AnimatedGraphicPrimitive2D( |
626 | 0 | rGraphic, |
627 | 0 | nullptr, |
628 | 0 | rTransform, |
629 | 0 | fTransparency)); |
630 | 0 | } |
631 | 13 | else if(rGraphic.getVectorGraphicData()) |
632 | 13 | { |
633 | | // embedded Vector Graphic Data fill, create embed transform |
634 | 13 | const basegfx::B2DRange& rSvgRange(rGraphic.getVectorGraphicData()->getRange()); |
635 | | |
636 | 13 | if(rSvgRange.getWidth() > 0.0 && rSvgRange.getHeight() > 0.0) |
637 | 11 | { |
638 | | // translate back to origin, scale to unit coordinates |
639 | 11 | basegfx::B2DHomMatrix aEmbedVectorGraphic( |
640 | 11 | basegfx::utils::createTranslateB2DHomMatrix( |
641 | 11 | -rSvgRange.getMinX(), |
642 | 11 | -rSvgRange.getMinY())); |
643 | | |
644 | 11 | aEmbedVectorGraphic.scale( |
645 | 11 | 1.0 / rSvgRange.getWidth(), |
646 | 11 | 1.0 / rSvgRange.getHeight()); |
647 | | |
648 | | // apply created object transformation |
649 | 11 | aEmbedVectorGraphic = rTransform * aEmbedVectorGraphic; |
650 | | |
651 | | // add Vector Graphic Data primitives embedded |
652 | 11 | rtl::Reference<BasePrimitive2D> aPrimitive( |
653 | 11 | new TransformPrimitive2D( |
654 | 11 | aEmbedVectorGraphic, |
655 | 11 | Primitive2DContainer(rGraphic.getVectorGraphicData()->getPrimitive2DSequence()))); |
656 | | |
657 | | // if needed embed to UnifiedTransparencePrimitive2D |
658 | 11 | if (!basegfx::fTools::equalZero(fTransparency, 0.0)) |
659 | 0 | aPrimitive = new UnifiedTransparencePrimitive2D( |
660 | 0 | Primitive2DContainer { aPrimitive }, fTransparency); |
661 | | |
662 | 11 | rContainer.append(aPrimitive); |
663 | 11 | } |
664 | 13 | } |
665 | 0 | else |
666 | 0 | { |
667 | | // dependent of transparency used create the needed bitmap primitive |
668 | 0 | if (basegfx::fTools::equalZero(fTransparency)) |
669 | 0 | { |
670 | 0 | rContainer.append( |
671 | 0 | new BitmapPrimitive2D( |
672 | 0 | rGraphic.GetBitmap(), |
673 | 0 | rTransform)); |
674 | 0 | } |
675 | 0 | else |
676 | 0 | { |
677 | 0 | rContainer.append( |
678 | 0 | new BitmapAlphaPrimitive2D( |
679 | 0 | rGraphic.GetBitmap(), |
680 | 0 | rTransform, |
681 | 0 | fTransparency)); |
682 | 0 | } |
683 | 0 | } |
684 | | |
685 | 13 | break; |
686 | 0 | } |
687 | | |
688 | 0 | case GraphicType::GdiMetafile : |
689 | 0 | { |
690 | | // create MetafilePrimitive2D |
691 | 0 | const GDIMetaFile& rMetafile = rGraphic.GetGDIMetaFile(); |
692 | |
|
693 | 0 | rtl::Reference<BasePrimitive2D> aPrimitive( |
694 | 0 | new MetafilePrimitive2D( |
695 | 0 | rTransform, |
696 | 0 | rMetafile)); |
697 | | |
698 | | // #i100357# find out if clipping is needed for this primitive. Unfortunately, |
699 | | // there exist Metafiles who's content is bigger than the proposed PrefSize set |
700 | | // at them. This is an error, but we need to work around this |
701 | 0 | const Size aMetaFilePrefSize(rMetafile.GetPrefSize()); |
702 | 0 | const Size aMetaFileRealSize( |
703 | 0 | rMetafile.GetBoundRect( |
704 | 0 | *Application::GetDefaultDevice()).GetSize()); |
705 | |
|
706 | 0 | if(aMetaFileRealSize.getWidth() > aMetaFilePrefSize.getWidth() |
707 | 0 | || aMetaFileRealSize.getHeight() > aMetaFilePrefSize.getHeight()) |
708 | 0 | { |
709 | | // clipping needed. Embed to MaskPrimitive2D. Create children and mask polygon |
710 | 0 | basegfx::B2DPolygon aMaskPolygon(basegfx::utils::createUnitPolygon()); |
711 | 0 | aMaskPolygon.transform(rTransform); |
712 | |
|
713 | 0 | aPrimitive = new MaskPrimitive2D( |
714 | 0 | basegfx::B2DPolyPolygon(aMaskPolygon), |
715 | 0 | Primitive2DContainer { aPrimitive }); |
716 | 0 | } |
717 | | |
718 | | // if needed embed to UnifiedTransparencePrimitive2D |
719 | 0 | if (!basegfx::fTools::equalZero(fTransparency, 0.0)) |
720 | 0 | aPrimitive = new UnifiedTransparencePrimitive2D( |
721 | 0 | Primitive2DContainer { aPrimitive }, fTransparency); |
722 | |
|
723 | 0 | rContainer.append(aPrimitive); |
724 | 0 | break; |
725 | 0 | } |
726 | | |
727 | 366 | default: |
728 | 366 | { |
729 | | // nothing to create |
730 | 366 | break; |
731 | 0 | } |
732 | 379 | } |
733 | 379 | } |
734 | | |
735 | | Primitive2DContainer create2DColorModifierEmbeddingsAsNeeded( |
736 | | Primitive2DContainer&& rChildren, |
737 | | GraphicDrawMode aGraphicDrawMode, |
738 | | double fLuminance, |
739 | | double fContrast, |
740 | | double fRed, |
741 | | double fGreen, |
742 | | double fBlue, |
743 | | double fGamma, |
744 | | bool bInvert) |
745 | 0 | { |
746 | 0 | Primitive2DContainer aRetval; |
747 | |
|
748 | 0 | if(rChildren.empty()) |
749 | 0 | { |
750 | | // no child content, done |
751 | 0 | return aRetval; |
752 | 0 | } |
753 | | |
754 | | // set child content as retval; that is what will be used as child content in all |
755 | | // embeddings from here |
756 | 0 | aRetval = std::move(rChildren); |
757 | |
|
758 | 0 | if(GraphicDrawMode::Watermark == aGraphicDrawMode) |
759 | 0 | { |
760 | | // this is solved by applying fixed values additionally to luminance |
761 | | // and contrast, do it here and reset DrawMode to GraphicDrawMode::Standard |
762 | | // original in svtools uses: |
763 | | // #define WATERMARK_LUM_OFFSET 50 |
764 | | // #define WATERMARK_CON_OFFSET -70 |
765 | 0 | fLuminance = std::clamp(fLuminance + 0.5, -1.0, 1.0); |
766 | 0 | fContrast = std::clamp(fContrast - 0.7, -1.0, 1.0); |
767 | 0 | aGraphicDrawMode = GraphicDrawMode::Standard; |
768 | 0 | } |
769 | | |
770 | | // DrawMode (GraphicDrawMode::Watermark already handled) |
771 | 0 | switch(aGraphicDrawMode) |
772 | 0 | { |
773 | 0 | case GraphicDrawMode::Greys: |
774 | 0 | { |
775 | | // convert to grey |
776 | 0 | const Primitive2DReference aPrimitiveGrey( |
777 | 0 | new ModifiedColorPrimitive2D( |
778 | 0 | std::move(aRetval), |
779 | 0 | std::make_shared<basegfx::BColorModifier_gray>())); |
780 | |
|
781 | 0 | aRetval = Primitive2DContainer { aPrimitiveGrey }; |
782 | 0 | break; |
783 | 0 | } |
784 | 0 | case GraphicDrawMode::Mono: |
785 | 0 | { |
786 | | // convert to mono (black/white with threshold 0.5) |
787 | 0 | const Primitive2DReference aPrimitiveBlackAndWhite( |
788 | 0 | new ModifiedColorPrimitive2D( |
789 | 0 | std::move(aRetval), |
790 | 0 | std::make_shared<basegfx::BColorModifier_black_and_white>(0.5))); |
791 | |
|
792 | 0 | aRetval = Primitive2DContainer { aPrimitiveBlackAndWhite }; |
793 | 0 | break; |
794 | 0 | } |
795 | 0 | default: // case GraphicDrawMode::Standard: |
796 | 0 | { |
797 | 0 | assert( |
798 | 0 | aGraphicDrawMode != GraphicDrawMode::Watermark |
799 | 0 | && "OOps, GraphicDrawMode::Watermark should already be handled (see above)"); |
800 | | // nothing to do |
801 | 0 | break; |
802 | 0 | } |
803 | 0 | } |
804 | | |
805 | | // mnContPercent, mnLumPercent, mnRPercent, mnGPercent, mnBPercent |
806 | | // handled in a single call |
807 | 0 | if(!basegfx::fTools::equalZero(fLuminance) |
808 | 0 | || !basegfx::fTools::equalZero(fContrast) |
809 | 0 | || !basegfx::fTools::equalZero(fRed) |
810 | 0 | || !basegfx::fTools::equalZero(fGreen) |
811 | 0 | || !basegfx::fTools::equalZero(fBlue)) |
812 | 0 | { |
813 | 0 | const Primitive2DReference aPrimitiveRGBLuminannceContrast( |
814 | 0 | new ModifiedColorPrimitive2D( |
815 | 0 | std::move(aRetval), |
816 | 0 | std::make_shared<basegfx::BColorModifier_RGBLuminanceContrast>( |
817 | 0 | fRed, |
818 | 0 | fGreen, |
819 | 0 | fBlue, |
820 | 0 | fLuminance, |
821 | 0 | fContrast))); |
822 | |
|
823 | 0 | aRetval = Primitive2DContainer { aPrimitiveRGBLuminannceContrast }; |
824 | 0 | } |
825 | | |
826 | | // gamma (boolean) |
827 | 0 | if(!basegfx::fTools::equal(fGamma, 1.0)) |
828 | 0 | { |
829 | 0 | const Primitive2DReference aPrimitiveGamma( |
830 | 0 | new ModifiedColorPrimitive2D( |
831 | 0 | std::move(aRetval), |
832 | 0 | std::make_shared<basegfx::BColorModifier_gamma>( |
833 | 0 | fGamma))); |
834 | |
|
835 | 0 | aRetval = Primitive2DContainer { aPrimitiveGamma }; |
836 | 0 | } |
837 | | |
838 | | // invert (boolean) |
839 | 0 | if(bInvert) |
840 | 0 | { |
841 | 0 | const Primitive2DReference aPrimitiveInvert( |
842 | 0 | new ModifiedColorPrimitive2D( |
843 | 0 | std::move(aRetval), |
844 | 0 | std::make_shared<basegfx::BColorModifier_invert>())); |
845 | |
|
846 | 0 | aRetval = Primitive2DContainer { aPrimitiveInvert }; |
847 | 0 | } |
848 | |
|
849 | 0 | return aRetval; |
850 | 0 | } |
851 | | |
852 | | } // end of namespace |
853 | | |
854 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |