/src/libreoffice/drawinglayer/source/primitive2d/shadowprimitive2d.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 <drawinglayer/primitive2d/shadowprimitive2d.hxx> |
21 | | #include <basegfx/color/bcolormodifier.hxx> |
22 | | #include <drawinglayer/primitive2d/modifiedcolorprimitive2d.hxx> |
23 | | #include <drawinglayer/primitive2d/transformprimitive2d.hxx> |
24 | | #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx> |
25 | | #include <basegfx/matrix/b2dhommatrixtools.hxx> |
26 | | #include <drawinglayer/primitive2d/bitmapprimitive2d.hxx> |
27 | | #include <toolkit/helper/vclunohelper.hxx> |
28 | | #include <drawinglayer/converters.hxx> |
29 | | #include "GlowSoftEgdeShadowTools.hxx" |
30 | | |
31 | | #ifdef DBG_UTIL |
32 | | #include <tools/stream.hxx> |
33 | | #include <vcl/filter/PngImageWriter.hxx> |
34 | | #endif |
35 | | |
36 | | #include <memory> |
37 | | #include <utility> |
38 | | |
39 | | using namespace com::sun::star; |
40 | | |
41 | | namespace drawinglayer::primitive2d |
42 | | { |
43 | | ShadowPrimitive2D::ShadowPrimitive2D(basegfx::B2DHomMatrix aShadowTransform, |
44 | | const basegfx::BColor& rShadowColor, double fShadowBlur, |
45 | | Primitive2DContainer&& aChildren) |
46 | 0 | : BufferedDecompositionGroupPrimitive2D(std::move(aChildren)) |
47 | 0 | , maShadowTransform(std::move(aShadowTransform)) |
48 | 0 | , maShadowColor(rShadowColor) |
49 | 0 | , mfShadowBlur(fShadowBlur) |
50 | 0 | , mfLastDiscreteBlurRadius(0.0) |
51 | 0 | , maLastClippedRange() |
52 | 0 | { |
53 | | // activate callback to flush buffered decomposition content |
54 | 0 | activateFlushOnTimer(); |
55 | 0 | } |
56 | | |
57 | | bool ShadowPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const |
58 | 0 | { |
59 | 0 | if (BufferedDecompositionGroupPrimitive2D::operator==(rPrimitive)) |
60 | 0 | { |
61 | 0 | const ShadowPrimitive2D& rCompare = static_cast<const ShadowPrimitive2D&>(rPrimitive); |
62 | |
|
63 | 0 | return (getShadowTransform() == rCompare.getShadowTransform() |
64 | 0 | && getShadowColor() == rCompare.getShadowColor() |
65 | 0 | && getShadowBlur() == rCompare.getShadowBlur()); |
66 | 0 | } |
67 | | |
68 | 0 | return false; |
69 | 0 | } |
70 | | |
71 | | // Helper to get the to-be-shadowed geometry completely embedded to |
72 | | // a ModifiedColorPrimitive2D (change to ShadowColor) and TransformPrimitive2D |
73 | | // (direction/offset/transformation of shadow). Since this is used pretty |
74 | | // often, pack into a helper |
75 | | void ShadowPrimitive2D::getFullyEmbeddedShadowPrimitives(Primitive2DContainer& rContainer) const |
76 | 0 | { |
77 | 0 | if (getChildren().empty()) |
78 | 0 | return; |
79 | | |
80 | | // create a modifiedColorPrimitive containing the shadow color and the content |
81 | 0 | const basegfx::BColorModifierSharedPtr aBColorModifier |
82 | 0 | = std::make_shared<basegfx::BColorModifier_replace>(getShadowColor()); |
83 | 0 | const Primitive2DReference xRefA( |
84 | 0 | new ModifiedColorPrimitive2D(Primitive2DContainer(getChildren()), aBColorModifier)); |
85 | 0 | Primitive2DContainer aSequenceB{ xRefA }; |
86 | | |
87 | | // build transformed primitiveVector with shadow offset and add to target |
88 | 0 | rContainer.visit(new TransformPrimitive2D(getShadowTransform(), std::move(aSequenceB))); |
89 | 0 | } |
90 | | |
91 | | bool ShadowPrimitive2D::prepareValuesAndcheckValidity( |
92 | | basegfx::B2DRange& rBlurRange, basegfx::B2DRange& rClippedRange, |
93 | | basegfx::B2DVector& rDiscreteBlurSize, double& rfDiscreteBlurRadius, |
94 | | const geometry::ViewInformation2D& rViewInformation) const |
95 | 0 | { |
96 | | // no BlurRadius defined, done |
97 | 0 | if (getShadowBlur() <= 0.0) |
98 | 0 | return false; |
99 | | |
100 | | // no geometry, done |
101 | 0 | if (getChildren().empty()) |
102 | 0 | return false; |
103 | | |
104 | | // no pixel target, done |
105 | 0 | if (rViewInformation.getObjectToViewTransformation().isIdentity()) |
106 | 0 | return false; |
107 | | |
108 | | // get fully embedded ShadowPrimitive |
109 | 0 | Primitive2DContainer aEmbedded; |
110 | 0 | getFullyEmbeddedShadowPrimitives(aEmbedded); |
111 | | |
112 | | // get geometry range that defines area that needs to be pixelated |
113 | 0 | rBlurRange = aEmbedded.getB2DRange(rViewInformation); |
114 | | |
115 | | // no range of geometry, done |
116 | 0 | if (rBlurRange.isEmpty()) |
117 | 0 | return false; |
118 | | |
119 | | // extend range by BlurRadius in all directions |
120 | 0 | rBlurRange.grow(getShadowBlur()); |
121 | | |
122 | | // initialize ClippedRange to full BlurRange -> all is visible |
123 | 0 | rClippedRange = rBlurRange; |
124 | | |
125 | | // get Viewport and check if used. If empty, all is visible (see |
126 | | // ViewInformation2D definition in viewinformation2d.hxx) |
127 | 0 | if (!rViewInformation.getViewport().isEmpty()) |
128 | 0 | { |
129 | | // if used, extend by BlurRadius to ensure needed parts are included |
130 | 0 | basegfx::B2DRange aVisibleArea(rViewInformation.getViewport()); |
131 | 0 | aVisibleArea.grow(getShadowBlur()); |
132 | | |
133 | | // calculate ClippedRange |
134 | 0 | rClippedRange.intersect(aVisibleArea); |
135 | | |
136 | | // if BlurRange is completely outside of VisibleArea, ClippedRange |
137 | | // will be empty and we are done |
138 | 0 | if (rClippedRange.isEmpty()) |
139 | 0 | return false; |
140 | 0 | } |
141 | | |
142 | | // calculate discrete pixel size of BlurRange. If it's too small to visualize, we are done |
143 | 0 | rDiscreteBlurSize = rViewInformation.getObjectToViewTransformation() * rBlurRange.getRange(); |
144 | 0 | if (ceil(rDiscreteBlurSize.getX()) < 2.0 || ceil(rDiscreteBlurSize.getY()) < 2.0) |
145 | 0 | return false; |
146 | | |
147 | | // calculate discrete pixel size of BlurRadius. If it's too small to visualize, we are done |
148 | 0 | rfDiscreteBlurRadius = ceil( |
149 | 0 | (rViewInformation.getObjectToViewTransformation() * basegfx::B2DVector(getShadowBlur(), 0)) |
150 | 0 | .getLength()); |
151 | 0 | if (rfDiscreteBlurRadius < 1.0) |
152 | 0 | return false; |
153 | | |
154 | 0 | return true; |
155 | 0 | } |
156 | | |
157 | | void ShadowPrimitive2D::create2DDecomposition( |
158 | | Primitive2DContainer& rContainer, const geometry::ViewInformation2D& rViewInformation) const |
159 | 0 | { |
160 | 0 | if (getShadowBlur() <= 0.0) |
161 | 0 | { |
162 | | // Normal (non-blurred) shadow is already completely |
163 | | // handled by get2DDecomposition and not buffered. It |
164 | | // does not need to be since it's a simple embedding |
165 | | // to a ModifiedColorPrimitive2D and TransformPrimitive2D |
166 | 0 | return; |
167 | 0 | } |
168 | | |
169 | | // from here on we process a blurred shadow |
170 | 0 | basegfx::B2DRange aBlurRange; |
171 | 0 | basegfx::B2DRange aClippedRange; |
172 | 0 | basegfx::B2DVector aDiscreteBlurSize; |
173 | 0 | double fDiscreteBlurRadius(0.0); |
174 | | |
175 | | // Check various validity details and calculate/prepare values. If false, we are done |
176 | 0 | if (!prepareValuesAndcheckValidity(aBlurRange, aClippedRange, aDiscreteBlurSize, |
177 | 0 | fDiscreteBlurRadius, rViewInformation)) |
178 | 0 | return; |
179 | | |
180 | | // Create embedding transformation from object to top-left zero-aligned |
181 | | // target pixel geometry (discrete form of ClippedRange) |
182 | | // First, move to top-left of BlurRange |
183 | 0 | const sal_uInt32 nDiscreteBlurWidth(ceil(aDiscreteBlurSize.getX())); |
184 | 0 | const sal_uInt32 nDiscreteBlurHeight(ceil(aDiscreteBlurSize.getY())); |
185 | 0 | basegfx::B2DHomMatrix aEmbedding(basegfx::utils::createTranslateB2DHomMatrix( |
186 | 0 | -aClippedRange.getMinX(), -aClippedRange.getMinY())); |
187 | | // Second, scale to discrete bitmap size |
188 | | // Even when using the offset from ClippedRange, we need to use the |
189 | | // scaling from the full representation, thus from BlurRange |
190 | 0 | aEmbedding.scale(nDiscreteBlurWidth / aBlurRange.getWidth(), |
191 | 0 | nDiscreteBlurHeight / aBlurRange.getHeight()); |
192 | | |
193 | | // Get fully embedded ShadowPrimitives. This will also embed to |
194 | | // ModifiedColorPrimitive2D (what is not urgently needed) to create |
195 | | // the alpha channel, but a paint with all colors set to a single |
196 | | // one (like shadowColor here) is often less expensive due to possible |
197 | | // simplifications painting the primitives (e.g. gradient) |
198 | 0 | Primitive2DContainer aEmbedded; |
199 | 0 | getFullyEmbeddedShadowPrimitives(aEmbedded); |
200 | | |
201 | | // Embed content graphics to TransformPrimitive2D |
202 | 0 | const primitive2d::Primitive2DReference xEmbedRef( |
203 | 0 | new primitive2d::TransformPrimitive2D(aEmbedding, std::move(aEmbedded))); |
204 | 0 | primitive2d::Primitive2DContainer xEmbedSeq{ xEmbedRef }; |
205 | | |
206 | | // Create BitmapEx using drawinglayer tooling, including a MaximumQuadraticPixel |
207 | | // limitation to be safe and not go runtime/memory havoc. Use a pretty small |
208 | | // limit due to this is Blurred Shadow functionality and will look good with bitmap |
209 | | // scaling anyways. The value of 250.000 square pixels below maybe adapted as needed. |
210 | 0 | const basegfx::B2DVector aDiscreteClippedSize(rViewInformation.getObjectToViewTransformation() |
211 | 0 | * aClippedRange.getRange()); |
212 | 0 | const sal_uInt32 nDiscreteClippedWidth(ceil(aDiscreteClippedSize.getX())); |
213 | 0 | const sal_uInt32 nDiscreteClippedHeight(ceil(aDiscreteClippedSize.getY())); |
214 | 0 | const geometry::ViewInformation2D aViewInformation2D; |
215 | 0 | const sal_uInt32 nMaximumQuadraticPixels(250000); |
216 | | |
217 | | // I have now added a helper that just creates the mask without having |
218 | | // to render the content, use it, it's faster |
219 | 0 | const AlphaMask aAlpha(::drawinglayer::createAlphaMask( |
220 | 0 | std::move(xEmbedSeq), aViewInformation2D, nDiscreteClippedWidth, nDiscreteClippedHeight, |
221 | 0 | nMaximumQuadraticPixels)); |
222 | | |
223 | | // if we have no shadow, we are done |
224 | 0 | if (aAlpha.IsEmpty()) |
225 | 0 | return; |
226 | | |
227 | 0 | const Size aBitmapExSizePixel(aAlpha.GetSizePixel()); |
228 | 0 | if (!(aBitmapExSizePixel.Width() > 0 && aBitmapExSizePixel.Height() > 0)) |
229 | 0 | return; |
230 | | |
231 | | // We may have to take a corrective scaling into account when the |
232 | | // MaximumQuadraticPixel limit was used/triggered |
233 | 0 | double fScale(1.0); |
234 | |
|
235 | 0 | if (static_cast<sal_uInt32>(aBitmapExSizePixel.Width()) != nDiscreteClippedWidth |
236 | 0 | || static_cast<sal_uInt32>(aBitmapExSizePixel.Height()) != nDiscreteClippedHeight) |
237 | 0 | { |
238 | | // scale in X and Y should be the same (see fReduceFactor in createAlphaMask), |
239 | | // so adapt numerically to a single scale value, they are integer rounded values |
240 | 0 | const double fScaleX(static_cast<double>(aBitmapExSizePixel.Width()) |
241 | 0 | / static_cast<double>(nDiscreteClippedWidth)); |
242 | 0 | const double fScaleY(static_cast<double>(aBitmapExSizePixel.Height()) |
243 | 0 | / static_cast<double>(nDiscreteClippedHeight)); |
244 | |
|
245 | 0 | fScale = (fScaleX + fScaleY) * 0.5; |
246 | 0 | } |
247 | | |
248 | | // Use the Alpha as base to blur and apply the effect |
249 | 0 | const AlphaMask mask(drawinglayer::primitive2d::ProcessAndBlurAlphaMask( |
250 | 0 | aAlpha, 0, fDiscreteBlurRadius * fScale, 0, false)); |
251 | | |
252 | | // The end result is the bitmap filled with blur color and blurred 8-bit alpha mask |
253 | 0 | Bitmap bmp(aAlpha.GetSizePixel(), vcl::PixelFormat::N24_BPP); |
254 | 0 | bmp.Erase(Color(getShadowColor())); |
255 | 0 | BitmapEx result(bmp, mask); |
256 | |
|
257 | | #ifdef DBG_UTIL |
258 | | static bool bDoSaveForVisualControl(false); // loplugin:constvars:ignore |
259 | | if (bDoSaveForVisualControl) |
260 | | { |
261 | | // VCL_DUMP_BMP_PATH should be like C:/path/ or ~/path/ |
262 | | static const OUString sDumpPath( |
263 | | OUString::createFromAscii(std::getenv("VCL_DUMP_BMP_PATH"))); |
264 | | if (!sDumpPath.isEmpty()) |
265 | | { |
266 | | SvFileStream aNew(sDumpPath + "test_shadowblur.png", |
267 | | StreamMode::WRITE | StreamMode::TRUNC); |
268 | | vcl::PngImageWriter aPNGWriter(aNew); |
269 | | aPNGWriter.write(result); |
270 | | } |
271 | | } |
272 | | #endif |
273 | | |
274 | | // Independent from discrete sizes of blur alpha creation, always |
275 | | // map and project blur result to geometry range extended by blur |
276 | | // radius, but to the eventually clipped instance (ClippedRange) |
277 | 0 | const primitive2d::Primitive2DReference xEmbedRefBitmap( |
278 | 0 | new BitmapPrimitive2D(result, basegfx::utils::createScaleTranslateB2DHomMatrix( |
279 | 0 | aClippedRange.getWidth(), aClippedRange.getHeight(), |
280 | 0 | aClippedRange.getMinX(), aClippedRange.getMinY()))); |
281 | |
|
282 | 0 | rContainer = primitive2d::Primitive2DContainer{ xEmbedRefBitmap }; |
283 | 0 | } |
284 | | |
285 | | void ShadowPrimitive2D::get2DDecomposition( |
286 | | Primitive2DDecompositionVisitor& rVisitor, |
287 | | const geometry::ViewInformation2D& rViewInformation) const |
288 | 0 | { |
289 | 0 | if (getShadowBlur() <= 0.0) |
290 | 0 | { |
291 | | // normal (non-blurred) shadow |
292 | 0 | if (getChildren().empty()) |
293 | 0 | return; |
294 | | |
295 | | // get fully embedded ShadowPrimitives |
296 | 0 | Primitive2DContainer aEmbedded; |
297 | 0 | getFullyEmbeddedShadowPrimitives(aEmbedded); |
298 | |
|
299 | 0 | rVisitor.visit(aEmbedded); |
300 | 0 | return; |
301 | 0 | } |
302 | | |
303 | | // here we have a blurred shadow, check conditions of last |
304 | | // buffered decompose and decide re-use or re-create by using |
305 | | // setBuffered2DDecomposition to reset local buffered version |
306 | 0 | basegfx::B2DRange aBlurRange; |
307 | 0 | basegfx::B2DRange aClippedRange; |
308 | 0 | basegfx::B2DVector aDiscreteBlurSize; |
309 | 0 | double fDiscreteBlurRadius(0.0); |
310 | | |
311 | | // Check various validity details and calculate/prepare values. If false, we are done |
312 | 0 | if (!prepareValuesAndcheckValidity(aBlurRange, aClippedRange, aDiscreteBlurSize, |
313 | 0 | fDiscreteBlurRadius, rViewInformation)) |
314 | 0 | return; |
315 | | |
316 | 0 | if (hasBuffered2DDecomposition()) |
317 | 0 | { |
318 | | // First check is to detect if the last created decompose is capable |
319 | | // to represent the now requested visualization (see similar |
320 | | // implementation at GlowPrimitive2D). |
321 | 0 | if (!maLastClippedRange.isEmpty() && !maLastClippedRange.isInside(aClippedRange)) |
322 | 0 | { |
323 | 0 | basegfx::B2DRange aLastClippedRangeAndHairline(maLastClippedRange); |
324 | |
|
325 | 0 | if (!rViewInformation.getObjectToViewTransformation().isIdentity()) |
326 | 0 | { |
327 | | // Grow by view-dependent size of 1/2 pixel |
328 | 0 | const double fHalfPixel((rViewInformation.getInverseObjectToViewTransformation() |
329 | 0 | * basegfx::B2DVector(0.5, 0)) |
330 | 0 | .getLength()); |
331 | 0 | aLastClippedRangeAndHairline.grow(fHalfPixel); |
332 | 0 | } |
333 | |
|
334 | 0 | if (!aLastClippedRangeAndHairline.isInside(aClippedRange)) |
335 | 0 | { |
336 | | // Conditions of last local decomposition have changed, delete |
337 | 0 | const_cast<ShadowPrimitive2D*>(this)->setBuffered2DDecomposition( |
338 | 0 | Primitive2DContainer()); |
339 | 0 | } |
340 | 0 | } |
341 | 0 | } |
342 | |
|
343 | 0 | if (hasBuffered2DDecomposition()) |
344 | 0 | { |
345 | | // Second check is to react on changes of the DiscreteSoftRadius when |
346 | | // zooming in/out (see similar implementation at ShadowPrimitive2D). |
347 | 0 | bool bFree(mfLastDiscreteBlurRadius <= 0.0 || fDiscreteBlurRadius <= 0.0); |
348 | |
|
349 | 0 | if (!bFree) |
350 | 0 | { |
351 | 0 | const double fDiff(fabs(mfLastDiscreteBlurRadius - fDiscreteBlurRadius)); |
352 | 0 | const double fLen(fabs(mfLastDiscreteBlurRadius) + fabs(fDiscreteBlurRadius)); |
353 | 0 | const double fRelativeChange(fDiff / fLen); |
354 | | |
355 | | // Use lower fixed values here to change more often, higher to change less often. |
356 | | // Value is in the range of ]0.0 .. 1.0] |
357 | 0 | bFree = fRelativeChange >= 0.15; |
358 | 0 | } |
359 | |
|
360 | 0 | if (bFree) |
361 | 0 | { |
362 | | // Conditions of last local decomposition have changed, delete |
363 | 0 | const_cast<ShadowPrimitive2D*>(this)->setBuffered2DDecomposition( |
364 | 0 | Primitive2DContainer()); |
365 | 0 | } |
366 | 0 | } |
367 | |
|
368 | 0 | if (!hasBuffered2DDecomposition()) |
369 | 0 | { |
370 | | // refresh last used DiscreteBlurRadius and ClippedRange to new remembered values |
371 | 0 | const_cast<ShadowPrimitive2D*>(this)->mfLastDiscreteBlurRadius = fDiscreteBlurRadius; |
372 | 0 | const_cast<ShadowPrimitive2D*>(this)->maLastClippedRange = aClippedRange; |
373 | 0 | } |
374 | | |
375 | | // call parent, that will check for empty, call create2DDecomposition and |
376 | | // set as decomposition |
377 | 0 | BufferedDecompositionGroupPrimitive2D::get2DDecomposition(rVisitor, rViewInformation); |
378 | 0 | } |
379 | | |
380 | | basegfx::B2DRange |
381 | | ShadowPrimitive2D::getB2DRange(const geometry::ViewInformation2D& rViewInformation) const |
382 | 0 | { |
383 | | // Hint: Do *not* use GroupPrimitive2D::getB2DRange, that will (unnecessarily) |
384 | | // use the decompose - what works, but is not needed here. |
385 | | // We know the to-be-visualized geometry and the radius it needs to be extended, |
386 | | // so simply calculate the exact needed range. |
387 | 0 | basegfx::B2DRange aRetval(getChildren().getB2DRange(rViewInformation)); |
388 | |
|
389 | 0 | if (getShadowBlur() > 0.0) |
390 | 0 | { |
391 | | // blurred shadow, that extends the geometry |
392 | 0 | aRetval.grow(getShadowBlur()); |
393 | 0 | } |
394 | |
|
395 | 0 | aRetval.transform(getShadowTransform()); |
396 | 0 | return aRetval; |
397 | 0 | } |
398 | | |
399 | | // provide unique ID |
400 | 0 | sal_uInt32 ShadowPrimitive2D::getPrimitive2DID() const { return PRIMITIVE2D_ID_SHADOWPRIMITIVE2D; } |
401 | | |
402 | | } // end of namespace |
403 | | |
404 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |