/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 | AlphaMask aAlphaMask(rAnimationFrame.maBitmap.CreateAlphaMask()); |
353 | 0 | Bitmap aExpandVisibilityMask(aAlphaMask.GetBitmap(), aAlphaMask); |
354 | 0 | maVirtualDeviceMask->DrawBitmap(rAnimationFrame.maPositionPixel, aExpandVisibilityMask); |
355 | 0 | break; |
356 | 0 | } |
357 | 0 | } |
358 | | |
359 | | // to not waste created data, check adding to buffers |
360 | 0 | checkSafeToBuffer(mnNextFrameToPrepare); |
361 | |
|
362 | 0 | mnNextFrameToPrepare++; |
363 | 0 | } |
364 | 0 | } |
365 | | |
366 | | Primitive2DReference tryTogetFromBuffer(sal_uInt32 nIndex) const |
367 | 0 | { |
368 | 0 | if (mbBufferingAllowed) |
369 | 0 | { |
370 | | // all frames buffered, check if available |
371 | 0 | if (!maBufferedPrimitives.empty() && nIndex < maBufferedPrimitives.size()) |
372 | 0 | { |
373 | 0 | if (maBufferedPrimitives[nIndex].is()) |
374 | 0 | { |
375 | 0 | return maBufferedPrimitives[nIndex]; |
376 | 0 | } |
377 | 0 | } |
378 | 0 | } |
379 | 0 | else |
380 | 0 | { |
381 | | // always buffer first frame, it's sometimes requested out-of-order |
382 | 0 | if (0 == nIndex && maBufferedFirstFrame.is()) |
383 | 0 | { |
384 | 0 | return maBufferedFirstFrame; |
385 | 0 | } |
386 | 0 | } |
387 | | |
388 | 0 | return Primitive2DReference(); |
389 | 0 | } |
390 | | |
391 | | public: |
392 | | /// constructor |
393 | | AnimatedGraphicPrimitive2D( |
394 | | const Graphic& rGraphic, |
395 | | const drawinglayer::attribute::FillGraphicAttribute* pFillGraphicAttribute, |
396 | | basegfx::B2DHomMatrix aTransform, |
397 | | double fTransparency = 0.0); |
398 | | virtual ~AnimatedGraphicPrimitive2D(); |
399 | | |
400 | | /// data read access |
401 | 0 | const basegfx::B2DHomMatrix& getTransform() const { return maTransform; } |
402 | 0 | double getTransparency() const { return mfTransparency; } |
403 | | |
404 | | /// provide unique ID |
405 | 0 | virtual sal_uInt32 getPrimitive2DID() const override { return PRIMITIVE2D_ID_ANIMATEDGRAPHICPRIMITIVE2D; } |
406 | | |
407 | | /// compare operator |
408 | | virtual bool operator==(const BasePrimitive2D& rPrimitive) const override; |
409 | | |
410 | | /// override to deliver the correct expected frame dependent of timing |
411 | | virtual void get2DDecomposition(Primitive2DDecompositionVisitor& rVisitor, const geometry::ViewInformation2D& rViewInformation) const override; |
412 | | |
413 | | /// get range |
414 | | virtual basegfx::B2DRange getB2DRange(const geometry::ViewInformation2D& rViewInformation) const override; |
415 | | }; |
416 | | |
417 | | } |
418 | | |
419 | | AnimatedGraphicPrimitive2D::AnimatedGraphicPrimitive2D( |
420 | | const Graphic& rGraphic, |
421 | | const drawinglayer::attribute::FillGraphicAttribute* pFillGraphicAttribute, |
422 | | basegfx::B2DHomMatrix aTransform, |
423 | | double fTransparency) |
424 | 0 | : AnimatedSwitchPrimitive2D( |
425 | 0 | animation::AnimationEntryList(), |
426 | 0 | Primitive2DContainer(), |
427 | 0 | false), |
428 | 0 | maTransform(std::move(aTransform)), |
429 | 0 | maGraphic(rGraphic), |
430 | 0 | maFillGraphicAttribute(), |
431 | 0 | maAnimation(rGraphic.GetAnimation()), |
432 | 0 | mfTransparency(std::max(0.0, std::min(1.0, fTransparency))), |
433 | 0 | maVirtualDevice(*Application::GetDefaultDevice()), |
434 | 0 | maVirtualDeviceMask(*Application::GetDefaultDevice()), |
435 | 0 | mnNextFrameToPrepare(SAL_MAX_UINT32), |
436 | 0 | mbBufferingAllowed(false), |
437 | 0 | mbHugeSize(false) |
438 | 0 | { |
439 | | // if FillGraphicAttribute copy it -> FillGraphicPrimitive2D is intended |
440 | 0 | if (nullptr != pFillGraphicAttribute) |
441 | 0 | maFillGraphicAttribute = *pFillGraphicAttribute; |
442 | | |
443 | | // initialize AnimationTiming, needed to detect which frame is requested |
444 | | // in get2DDecomposition |
445 | 0 | createAndSetAnimationTiming(); |
446 | | |
447 | | // check if we allow buffering |
448 | 0 | if (isValidData()) |
449 | 0 | { |
450 | | // allow buffering up to a size of: |
451 | | // - 64 frames |
452 | | // - sizes of 256x256 pixels |
453 | | // This may be offered in option values if needed |
454 | 0 | static const sal_uInt64 nAllowedSize(64 * 256 * 256); |
455 | 0 | static const sal_uInt64 nHugeSize(10000000); |
456 | 0 | const Size aTarget(maAnimation.GetDisplaySizePixel()); |
457 | 0 | const sal_uInt64 nUsedSize(static_cast<sal_uInt64>(maAnimation.Count()) * aTarget.Width() * aTarget.Height()); |
458 | |
|
459 | 0 | if (nUsedSize < nAllowedSize) |
460 | 0 | { |
461 | 0 | mbBufferingAllowed = true; |
462 | 0 | } |
463 | |
|
464 | 0 | if (nUsedSize > nHugeSize) |
465 | 0 | { |
466 | 0 | mbHugeSize = true; |
467 | 0 | } |
468 | 0 | } |
469 | | |
470 | | // prepare buffer space |
471 | 0 | if (mbBufferingAllowed && isValidData()) |
472 | 0 | { |
473 | 0 | maBufferedPrimitives.resize(maAnimation.Count()); |
474 | 0 | } |
475 | 0 | } |
476 | | |
477 | | AnimatedGraphicPrimitive2D::~AnimatedGraphicPrimitive2D() |
478 | 0 | { |
479 | | // Related: tdf#158807 mutex must be locked when disposing a VirtualDevice |
480 | | // If the following .ppt document is opened in a debug build |
481 | | // and the document is left open for a minute or two without |
482 | | // changing any content, this destructor will be called on a |
483 | | // non-main thread with the mutex unlocked: |
484 | | // https://bugs.documentfoundation.org/attachment.cgi?id=46801 |
485 | | // This hits an assert in VirtualDevice::ReleaseGraphics() so |
486 | | // explicitly lock the mutex and explicitly dispose and clear |
487 | | // the VirtualDevice instances variables. |
488 | 0 | const SolarMutexGuard aSolarGuard; |
489 | |
|
490 | 0 | maVirtualDevice.disposeAndClear(); |
491 | 0 | maVirtualDeviceMask.disposeAndClear(); |
492 | 0 | maAnimation.Clear(); |
493 | 0 | maGraphic.Clear(); |
494 | 0 | } |
495 | | |
496 | | bool AnimatedGraphicPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const |
497 | 0 | { |
498 | | // do not use 'GroupPrimitive2D::operator==' here, that would compare |
499 | | // the children. Also do not use 'BasePrimitive2D::operator==', that would |
500 | | // check the ID-Type. Since we are a simple derivation without own ID, |
501 | | // use the dynamic_cast RTTI directly |
502 | 0 | const AnimatedGraphicPrimitive2D* pCompare = dynamic_cast<const AnimatedGraphicPrimitive2D*>(&rPrimitive); |
503 | | |
504 | | // use operator== of Graphic - if that is equal, the basic definition is equal |
505 | 0 | return (nullptr != pCompare |
506 | 0 | && getTransform() == pCompare->getTransform() |
507 | 0 | && maGraphic == pCompare->maGraphic); |
508 | 0 | } |
509 | | |
510 | | void AnimatedGraphicPrimitive2D::get2DDecomposition(Primitive2DDecompositionVisitor& rVisitor, const geometry::ViewInformation2D& rViewInformation) const |
511 | 0 | { |
512 | 0 | if (!isValidData()) |
513 | 0 | return; |
514 | | |
515 | 0 | Primitive2DReference aRetval; |
516 | 0 | const double fState(getAnimationEntry().getStateAtTime(rViewInformation.getViewTime())); |
517 | 0 | const sal_uInt32 nLen(maAnimation.Count()); |
518 | 0 | sal_uInt32 nIndex(basegfx::fround(fState * static_cast<double>(nLen))); |
519 | | |
520 | | // nIndex is the requested frame - it is in range [0..nLen[ |
521 | | // create frame representation in VirtualDevices |
522 | 0 | if (nIndex >= nLen) |
523 | 0 | { |
524 | 0 | nIndex = nLen - 1; |
525 | 0 | } |
526 | | |
527 | | // check buffering shortcuts, may already be created |
528 | 0 | aRetval = tryTogetFromBuffer(nIndex); |
529 | |
|
530 | 0 | if (aRetval.is()) |
531 | 0 | { |
532 | 0 | rVisitor.visit(aRetval); |
533 | 0 | return; |
534 | 0 | } |
535 | | |
536 | | // if huge size (and not the buffered 1st frame) simply |
537 | | // create next frame |
538 | 0 | if (mbHugeSize && 0 != nIndex && mnNextFrameToPrepare <= nIndex) |
539 | 0 | { |
540 | 0 | nIndex = mnNextFrameToPrepare % nLen; |
541 | 0 | } |
542 | | |
543 | | // frame not (yet) buffered or no buffering allowed, create it |
544 | 0 | const_cast<AnimatedGraphicPrimitive2D*>(this)->createFrame(nIndex); |
545 | | |
546 | | // try to get from buffer again, may have been added from createFrame |
547 | 0 | aRetval = tryTogetFromBuffer(nIndex); |
548 | |
|
549 | 0 | if (aRetval.is()) |
550 | 0 | { |
551 | 0 | rVisitor.visit(aRetval); |
552 | 0 | return; |
553 | 0 | } |
554 | | |
555 | | // did not work (not buffered and not 1st frame), create from buffer |
556 | 0 | aRetval = createFromBuffer(); |
557 | |
|
558 | 0 | rVisitor.visit(aRetval); |
559 | 0 | } |
560 | | |
561 | | basegfx::B2DRange AnimatedGraphicPrimitive2D::getB2DRange(const geometry::ViewInformation2D& rViewInformation) const |
562 | 0 | { |
563 | | // get object's range |
564 | 0 | basegfx::B2DRange aUnitRange(0.0, 0.0, 1.0, 1.0); |
565 | 0 | aUnitRange.transform(getTransform()); |
566 | | |
567 | | // intersect with visible part |
568 | 0 | aUnitRange.intersect(rViewInformation.getViewport()); |
569 | |
|
570 | 0 | return aUnitRange; |
571 | 0 | } |
572 | | |
573 | | } // end of namespace |
574 | | |
575 | | namespace drawinglayer::primitive2d |
576 | | { |
577 | | Primitive2DReference createFillGraphicPrimitive2D( |
578 | | const basegfx::B2DHomMatrix& rTransform, |
579 | | const drawinglayer::attribute::FillGraphicAttribute& rFillGraphicAttribute, |
580 | | double fTransparency) |
581 | 0 | { |
582 | 0 | if (basegfx::fTools::equal(fTransparency, 1.0)) |
583 | 0 | { |
584 | | // completely transparent, done |
585 | 0 | return nullptr; |
586 | 0 | } |
587 | | |
588 | 0 | const Graphic& rGraphic(rFillGraphicAttribute.getGraphic()); |
589 | 0 | const GraphicType aType(rGraphic.GetType()); |
590 | |
|
591 | 0 | if (GraphicType::Bitmap == aType && rGraphic.IsAnimated()) |
592 | 0 | { |
593 | 0 | return new AnimatedGraphicPrimitive2D( |
594 | 0 | rGraphic, |
595 | 0 | &rFillGraphicAttribute, |
596 | 0 | rTransform, |
597 | 0 | fTransparency); |
598 | 0 | } |
599 | | |
600 | 0 | return new FillGraphicPrimitive2D( |
601 | 0 | rTransform, |
602 | 0 | rFillGraphicAttribute, |
603 | 0 | fTransparency); |
604 | 0 | } |
605 | | |
606 | | void create2DDecompositionOfGraphic( |
607 | | Primitive2DContainer& rContainer, |
608 | | const Graphic& rGraphic, |
609 | | const basegfx::B2DHomMatrix& rTransform, |
610 | | double fTransparency) |
611 | 548 | { |
612 | 548 | if (basegfx::fTools::equal(fTransparency, 1.0)) |
613 | 0 | { |
614 | | // completely transparent, done |
615 | 0 | return; |
616 | 0 | } |
617 | | |
618 | 548 | switch(rGraphic.GetType()) |
619 | 548 | { |
620 | 96 | case GraphicType::Bitmap : |
621 | 96 | { |
622 | 96 | if(rGraphic.IsAnimated()) |
623 | 0 | { |
624 | | // prepare specialized AnimatedGraphicPrimitive2D, now with |
625 | | // support for alpha |
626 | 0 | rContainer.append(new AnimatedGraphicPrimitive2D( |
627 | 0 | rGraphic, |
628 | 0 | nullptr, |
629 | 0 | rTransform, |
630 | 0 | fTransparency)); |
631 | 0 | } |
632 | 96 | else if(rGraphic.getVectorGraphicData()) |
633 | 96 | { |
634 | | // embedded Vector Graphic Data fill, create embed transform |
635 | 96 | const basegfx::B2DRange& rSvgRange(rGraphic.getVectorGraphicData()->getRange()); |
636 | | |
637 | 96 | if(rSvgRange.getWidth() > 0.0 && rSvgRange.getHeight() > 0.0) |
638 | 50 | { |
639 | | // translate back to origin, scale to unit coordinates |
640 | 50 | basegfx::B2DHomMatrix aEmbedVectorGraphic( |
641 | 50 | basegfx::utils::createTranslateB2DHomMatrix( |
642 | 50 | -rSvgRange.getMinX(), |
643 | 50 | -rSvgRange.getMinY())); |
644 | | |
645 | 50 | aEmbedVectorGraphic.scale( |
646 | 50 | 1.0 / rSvgRange.getWidth(), |
647 | 50 | 1.0 / rSvgRange.getHeight()); |
648 | | |
649 | | // apply created object transformation |
650 | 50 | aEmbedVectorGraphic = rTransform * aEmbedVectorGraphic; |
651 | | |
652 | | // add Vector Graphic Data primitives embedded |
653 | 50 | rtl::Reference<BasePrimitive2D> aPrimitive( |
654 | 50 | new TransformPrimitive2D( |
655 | 50 | aEmbedVectorGraphic, |
656 | 50 | Primitive2DContainer(rGraphic.getVectorGraphicData()->getPrimitive2DSequence()))); |
657 | | |
658 | | // if needed embed to UnifiedTransparencePrimitive2D |
659 | 50 | if (!basegfx::fTools::equalZero(fTransparency, 0.0)) |
660 | 0 | aPrimitive = new UnifiedTransparencePrimitive2D( |
661 | 0 | Primitive2DContainer { aPrimitive }, fTransparency); |
662 | | |
663 | 50 | rContainer.append(aPrimitive); |
664 | 50 | } |
665 | 96 | } |
666 | 0 | else |
667 | 0 | { |
668 | | // dependent of transparency used create the needed bitmap primitive |
669 | 0 | if (basegfx::fTools::equalZero(fTransparency)) |
670 | 0 | { |
671 | 0 | rContainer.append( |
672 | 0 | new BitmapPrimitive2D( |
673 | 0 | rGraphic.GetBitmap(), |
674 | 0 | rTransform)); |
675 | 0 | } |
676 | 0 | else |
677 | 0 | { |
678 | 0 | rContainer.append( |
679 | 0 | new BitmapAlphaPrimitive2D( |
680 | 0 | rGraphic.GetBitmap(), |
681 | 0 | rTransform, |
682 | 0 | fTransparency)); |
683 | 0 | } |
684 | 0 | } |
685 | | |
686 | 96 | break; |
687 | 0 | } |
688 | | |
689 | 0 | case GraphicType::GdiMetafile : |
690 | 0 | { |
691 | | // create MetafilePrimitive2D |
692 | 0 | const GDIMetaFile& rMetafile = rGraphic.GetGDIMetaFile(); |
693 | |
|
694 | 0 | rtl::Reference<BasePrimitive2D> aPrimitive( |
695 | 0 | new MetafilePrimitive2D( |
696 | 0 | rTransform, |
697 | 0 | rMetafile)); |
698 | | |
699 | | // #i100357# find out if clipping is needed for this primitive. Unfortunately, |
700 | | // there exist Metafiles who's content is bigger than the proposed PrefSize set |
701 | | // at them. This is an error, but we need to work around this |
702 | 0 | const Size aMetaFilePrefSize(rMetafile.GetPrefSize()); |
703 | 0 | const Size aMetaFileRealSize( |
704 | 0 | rMetafile.GetBoundRect( |
705 | 0 | *Application::GetDefaultDevice()).GetSize()); |
706 | |
|
707 | 0 | if(aMetaFileRealSize.getWidth() > aMetaFilePrefSize.getWidth() |
708 | 0 | || aMetaFileRealSize.getHeight() > aMetaFilePrefSize.getHeight()) |
709 | 0 | { |
710 | | // clipping needed. Embed to MaskPrimitive2D. Create children and mask polygon |
711 | 0 | basegfx::B2DPolygon aMaskPolygon(basegfx::utils::createUnitPolygon()); |
712 | 0 | aMaskPolygon.transform(rTransform); |
713 | |
|
714 | 0 | aPrimitive = new MaskPrimitive2D( |
715 | 0 | basegfx::B2DPolyPolygon(aMaskPolygon), |
716 | 0 | Primitive2DContainer { aPrimitive }); |
717 | 0 | } |
718 | | |
719 | | // if needed embed to UnifiedTransparencePrimitive2D |
720 | 0 | if (!basegfx::fTools::equalZero(fTransparency, 0.0)) |
721 | 0 | aPrimitive = new UnifiedTransparencePrimitive2D( |
722 | 0 | Primitive2DContainer { aPrimitive }, fTransparency); |
723 | |
|
724 | 0 | rContainer.append(aPrimitive); |
725 | 0 | break; |
726 | 0 | } |
727 | | |
728 | 452 | default: |
729 | 452 | { |
730 | | // nothing to create |
731 | 452 | break; |
732 | 0 | } |
733 | 548 | } |
734 | 548 | } |
735 | | |
736 | | Primitive2DContainer create2DColorModifierEmbeddingsAsNeeded( |
737 | | Primitive2DContainer&& rChildren, |
738 | | GraphicDrawMode aGraphicDrawMode, |
739 | | double fLuminance, |
740 | | double fContrast, |
741 | | double fRed, |
742 | | double fGreen, |
743 | | double fBlue, |
744 | | double fGamma, |
745 | | bool bInvert) |
746 | 0 | { |
747 | 0 | Primitive2DContainer aRetval; |
748 | |
|
749 | 0 | if(rChildren.empty()) |
750 | 0 | { |
751 | | // no child content, done |
752 | 0 | return aRetval; |
753 | 0 | } |
754 | | |
755 | | // set child content as retval; that is what will be used as child content in all |
756 | | // embeddings from here |
757 | 0 | aRetval = std::move(rChildren); |
758 | |
|
759 | 0 | if(GraphicDrawMode::Watermark == aGraphicDrawMode) |
760 | 0 | { |
761 | | // this is solved by applying fixed values additionally to luminance |
762 | | // and contrast, do it here and reset DrawMode to GraphicDrawMode::Standard |
763 | | // original in svtools uses: |
764 | | // #define WATERMARK_LUM_OFFSET 50 |
765 | | // #define WATERMARK_CON_OFFSET -70 |
766 | 0 | fLuminance = std::clamp(fLuminance + 0.5, -1.0, 1.0); |
767 | 0 | fContrast = std::clamp(fContrast - 0.7, -1.0, 1.0); |
768 | 0 | aGraphicDrawMode = GraphicDrawMode::Standard; |
769 | 0 | } |
770 | | |
771 | | // DrawMode (GraphicDrawMode::Watermark already handled) |
772 | 0 | switch(aGraphicDrawMode) |
773 | 0 | { |
774 | 0 | case GraphicDrawMode::Greys: |
775 | 0 | { |
776 | | // convert to grey |
777 | 0 | const Primitive2DReference aPrimitiveGrey( |
778 | 0 | new ModifiedColorPrimitive2D( |
779 | 0 | std::move(aRetval), |
780 | 0 | std::make_shared<basegfx::BColorModifier_gray>())); |
781 | |
|
782 | 0 | aRetval = Primitive2DContainer { aPrimitiveGrey }; |
783 | 0 | break; |
784 | 0 | } |
785 | 0 | case GraphicDrawMode::Mono: |
786 | 0 | { |
787 | | // convert to mono (black/white with threshold 0.5) |
788 | 0 | const Primitive2DReference aPrimitiveBlackAndWhite( |
789 | 0 | new ModifiedColorPrimitive2D( |
790 | 0 | std::move(aRetval), |
791 | 0 | std::make_shared<basegfx::BColorModifier_black_and_white>(0.5))); |
792 | |
|
793 | 0 | aRetval = Primitive2DContainer { aPrimitiveBlackAndWhite }; |
794 | 0 | break; |
795 | 0 | } |
796 | 0 | default: // case GraphicDrawMode::Standard: |
797 | 0 | { |
798 | 0 | assert( |
799 | 0 | aGraphicDrawMode != GraphicDrawMode::Watermark |
800 | 0 | && "OOps, GraphicDrawMode::Watermark should already be handled (see above)"); |
801 | | // nothing to do |
802 | 0 | break; |
803 | 0 | } |
804 | 0 | } |
805 | | |
806 | | // mnContPercent, mnLumPercent, mnRPercent, mnGPercent, mnBPercent |
807 | | // handled in a single call |
808 | 0 | if(!basegfx::fTools::equalZero(fLuminance) |
809 | 0 | || !basegfx::fTools::equalZero(fContrast) |
810 | 0 | || !basegfx::fTools::equalZero(fRed) |
811 | 0 | || !basegfx::fTools::equalZero(fGreen) |
812 | 0 | || !basegfx::fTools::equalZero(fBlue)) |
813 | 0 | { |
814 | 0 | const Primitive2DReference aPrimitiveRGBLuminannceContrast( |
815 | 0 | new ModifiedColorPrimitive2D( |
816 | 0 | std::move(aRetval), |
817 | 0 | std::make_shared<basegfx::BColorModifier_RGBLuminanceContrast>( |
818 | 0 | fRed, |
819 | 0 | fGreen, |
820 | 0 | fBlue, |
821 | 0 | fLuminance, |
822 | 0 | fContrast))); |
823 | |
|
824 | 0 | aRetval = Primitive2DContainer { aPrimitiveRGBLuminannceContrast }; |
825 | 0 | } |
826 | | |
827 | | // gamma (boolean) |
828 | 0 | if(!basegfx::fTools::equal(fGamma, 1.0)) |
829 | 0 | { |
830 | 0 | const Primitive2DReference aPrimitiveGamma( |
831 | 0 | new ModifiedColorPrimitive2D( |
832 | 0 | std::move(aRetval), |
833 | 0 | std::make_shared<basegfx::BColorModifier_gamma>( |
834 | 0 | fGamma))); |
835 | |
|
836 | 0 | aRetval = Primitive2DContainer { aPrimitiveGamma }; |
837 | 0 | } |
838 | | |
839 | | // invert (boolean) |
840 | 0 | if(bInvert) |
841 | 0 | { |
842 | 0 | const Primitive2DReference aPrimitiveInvert( |
843 | 0 | new ModifiedColorPrimitive2D( |
844 | 0 | std::move(aRetval), |
845 | 0 | std::make_shared<basegfx::BColorModifier_invert>())); |
846 | |
|
847 | 0 | aRetval = Primitive2DContainer { aPrimitiveInvert }; |
848 | 0 | } |
849 | |
|
850 | 0 | return aRetval; |
851 | 0 | } |
852 | | |
853 | | } // end of namespace |
854 | | |
855 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |