1 | /* |
2 | * Copyright (C) 2011-2017 Apple Inc. All rights reserved. |
3 | * Copyright (C) 2013 Google Inc. All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * 1. Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. |
10 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. |
13 | * |
14 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
18 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
19 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
20 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | */ |
26 | |
27 | #include "config.h" |
28 | #include "CSSFilter.h" |
29 | |
30 | #include "CachedSVGDocument.h" |
31 | #include "CachedSVGDocumentReference.h" |
32 | #include "ElementIterator.h" |
33 | #include "FEColorMatrix.h" |
34 | #include "FEComponentTransfer.h" |
35 | #include "FEDropShadow.h" |
36 | #include "FEGaussianBlur.h" |
37 | #include "FEMerge.h" |
38 | #include "Logging.h" |
39 | #include "RenderLayer.h" |
40 | #include "SVGElement.h" |
41 | #include "SVGFilterBuilder.h" |
42 | #include "SVGFilterPrimitiveStandardAttributes.h" |
43 | #include "SourceAlpha.h" |
44 | #include "SourceGraphic.h" |
45 | #include <algorithm> |
46 | #include <wtf/MathExtras.h> |
47 | |
48 | #if USE(DIRECT2D) |
49 | #include <d2d1.h> |
50 | #endif |
51 | |
52 | namespace WebCore { |
53 | |
54 | static inline void endMatrixRow(Vector<float>& parameters) |
55 | { |
56 | parameters.append(0); |
57 | parameters.append(0); |
58 | } |
59 | |
60 | static inline void lastMatrixRow(Vector<float>& parameters) |
61 | { |
62 | parameters.append(0); |
63 | parameters.append(0); |
64 | parameters.append(0); |
65 | parameters.append(1); |
66 | parameters.append(0); |
67 | } |
68 | |
69 | Ref<CSSFilter> CSSFilter::create() |
70 | { |
71 | return adoptRef(*new CSSFilter); |
72 | } |
73 | |
74 | CSSFilter::CSSFilter() |
75 | : Filter(FloatSize { 1, 1 }) |
76 | , m_sourceGraphic(SourceGraphic::create(*this)) |
77 | { |
78 | } |
79 | |
80 | CSSFilter::~CSSFilter() = default; |
81 | |
82 | GraphicsContext* CSSFilter::inputContext() |
83 | { |
84 | return sourceImage() ? &sourceImage()->context() : nullptr; |
85 | } |
86 | |
87 | RefPtr<FilterEffect> CSSFilter::buildReferenceFilter(RenderElement& renderer, FilterEffect& previousEffect, ReferenceFilterOperation& filterOperation) |
88 | { |
89 | auto* cachedSVGDocumentReference = filterOperation.cachedSVGDocumentReference(); |
90 | auto* cachedSVGDocument = cachedSVGDocumentReference ? cachedSVGDocumentReference->document() : nullptr; |
91 | |
92 | // If we have an SVG document, this is an external reference. Otherwise |
93 | // we look up the referenced node in the current document. |
94 | Document* document; |
95 | if (!cachedSVGDocument) |
96 | document = &renderer.document(); |
97 | else { |
98 | document = cachedSVGDocument->document(); |
99 | if (!document) |
100 | return nullptr; |
101 | } |
102 | |
103 | auto* filter = document->getElementById(filterOperation.fragment()); |
104 | if (!filter) { |
105 | // Although we did not find the referenced filter, it might exist later in the document. |
106 | // FIXME: This skips anonymous RenderObjects. <https://webkit.org/b/131085> |
107 | if (auto* element = renderer.element()) |
108 | document->accessSVGExtensions().addPendingResource(filterOperation.fragment(), *element); |
109 | return nullptr; |
110 | } |
111 | |
112 | RefPtr<FilterEffect> effect; |
113 | |
114 | auto builder = std::make_unique<SVGFilterBuilder>(&previousEffect); |
115 | m_sourceAlpha = builder->getEffectById(SourceAlpha::effectName()); |
116 | |
117 | for (auto& effectElement : childrenOfType<SVGFilterPrimitiveStandardAttributes>(*filter)) { |
118 | effect = effectElement.build(builder.get(), *this); |
119 | if (!effect) |
120 | continue; |
121 | |
122 | effectElement.setStandardAttributes(effect.get()); |
123 | if (effectElement.renderer()) |
124 | effect->setOperatingColorSpace(effectElement.renderer()->style().svgStyle().colorInterpolationFilters() == ColorInterpolation::LinearRGB ? ColorSpaceLinearRGB : ColorSpaceSRGB); |
125 | |
126 | builder->add(effectElement.result(), effect); |
127 | m_effects.append(*effect); |
128 | } |
129 | return effect; |
130 | } |
131 | |
132 | bool CSSFilter::build(RenderElement& renderer, const FilterOperations& operations, FilterConsumer consumer) |
133 | { |
134 | m_hasFilterThatMovesPixels = operations.hasFilterThatMovesPixels(); |
135 | m_hasFilterThatShouldBeRestrictedBySecurityOrigin = operations.hasFilterThatShouldBeRestrictedBySecurityOrigin(); |
136 | if (m_hasFilterThatMovesPixels) |
137 | m_outsets = operations.outsets(); |
138 | |
139 | m_effects.clear(); |
140 | |
141 | RefPtr<FilterEffect> previousEffect = m_sourceGraphic.ptr(); |
142 | for (auto& operation : operations.operations()) { |
143 | RefPtr<FilterEffect> effect; |
144 | auto& filterOperation = *operation; |
145 | switch (filterOperation.type()) { |
146 | case FilterOperation::REFERENCE: { |
147 | auto& referenceOperation = downcast<ReferenceFilterOperation>(filterOperation); |
148 | effect = buildReferenceFilter(renderer, *previousEffect, referenceOperation); |
149 | break; |
150 | } |
151 | case FilterOperation::GRAYSCALE: { |
152 | auto& colorMatrixOperation = downcast<BasicColorMatrixFilterOperation>(filterOperation); |
153 | Vector<float> inputParameters; |
154 | double oneMinusAmount = clampTo(1 - colorMatrixOperation.amount(), 0.0, 1.0); |
155 | |
156 | // See https://dvcs.w3.org/hg/FXTF/raw-file/tip/filters/index.html#grayscaleEquivalent |
157 | // for information on parameters. |
158 | |
159 | inputParameters.append(narrowPrecisionToFloat(0.2126 + 0.7874 * oneMinusAmount)); |
160 | inputParameters.append(narrowPrecisionToFloat(0.7152 - 0.7152 * oneMinusAmount)); |
161 | inputParameters.append(narrowPrecisionToFloat(0.0722 - 0.0722 * oneMinusAmount)); |
162 | endMatrixRow(inputParameters); |
163 | |
164 | inputParameters.append(narrowPrecisionToFloat(0.2126 - 0.2126 * oneMinusAmount)); |
165 | inputParameters.append(narrowPrecisionToFloat(0.7152 + 0.2848 * oneMinusAmount)); |
166 | inputParameters.append(narrowPrecisionToFloat(0.0722 - 0.0722 * oneMinusAmount)); |
167 | endMatrixRow(inputParameters); |
168 | |
169 | inputParameters.append(narrowPrecisionToFloat(0.2126 - 0.2126 * oneMinusAmount)); |
170 | inputParameters.append(narrowPrecisionToFloat(0.7152 - 0.7152 * oneMinusAmount)); |
171 | inputParameters.append(narrowPrecisionToFloat(0.0722 + 0.9278 * oneMinusAmount)); |
172 | endMatrixRow(inputParameters); |
173 | |
174 | lastMatrixRow(inputParameters); |
175 | |
176 | effect = FEColorMatrix::create(*this, FECOLORMATRIX_TYPE_MATRIX, inputParameters); |
177 | break; |
178 | } |
179 | case FilterOperation::SEPIA: { |
180 | auto& colorMatrixOperation = downcast<BasicColorMatrixFilterOperation>(filterOperation); |
181 | Vector<float> inputParameters; |
182 | double oneMinusAmount = clampTo(1 - colorMatrixOperation.amount(), 0.0, 1.0); |
183 | |
184 | // See https://dvcs.w3.org/hg/FXTF/raw-file/tip/filters/index.html#sepiaEquivalent |
185 | // for information on parameters. |
186 | |
187 | inputParameters.append(narrowPrecisionToFloat(0.393 + 0.607 * oneMinusAmount)); |
188 | inputParameters.append(narrowPrecisionToFloat(0.769 - 0.769 * oneMinusAmount)); |
189 | inputParameters.append(narrowPrecisionToFloat(0.189 - 0.189 * oneMinusAmount)); |
190 | endMatrixRow(inputParameters); |
191 | |
192 | inputParameters.append(narrowPrecisionToFloat(0.349 - 0.349 * oneMinusAmount)); |
193 | inputParameters.append(narrowPrecisionToFloat(0.686 + 0.314 * oneMinusAmount)); |
194 | inputParameters.append(narrowPrecisionToFloat(0.168 - 0.168 * oneMinusAmount)); |
195 | endMatrixRow(inputParameters); |
196 | |
197 | inputParameters.append(narrowPrecisionToFloat(0.272 - 0.272 * oneMinusAmount)); |
198 | inputParameters.append(narrowPrecisionToFloat(0.534 - 0.534 * oneMinusAmount)); |
199 | inputParameters.append(narrowPrecisionToFloat(0.131 + 0.869 * oneMinusAmount)); |
200 | endMatrixRow(inputParameters); |
201 | |
202 | lastMatrixRow(inputParameters); |
203 | |
204 | effect = FEColorMatrix::create(*this, FECOLORMATRIX_TYPE_MATRIX, inputParameters); |
205 | break; |
206 | } |
207 | case FilterOperation::SATURATE: { |
208 | auto& colorMatrixOperation = downcast<BasicColorMatrixFilterOperation>(filterOperation); |
209 | Vector<float> inputParameters; |
210 | inputParameters.append(narrowPrecisionToFloat(colorMatrixOperation.amount())); |
211 | effect = FEColorMatrix::create(*this, FECOLORMATRIX_TYPE_SATURATE, inputParameters); |
212 | break; |
213 | } |
214 | case FilterOperation::HUE_ROTATE: { |
215 | auto& colorMatrixOperation = downcast<BasicColorMatrixFilterOperation>(filterOperation); |
216 | Vector<float> inputParameters; |
217 | inputParameters.append(narrowPrecisionToFloat(colorMatrixOperation.amount())); |
218 | effect = FEColorMatrix::create(*this, FECOLORMATRIX_TYPE_HUEROTATE, inputParameters); |
219 | break; |
220 | } |
221 | case FilterOperation::INVERT: { |
222 | auto& componentTransferOperation = downcast<BasicComponentTransferFilterOperation>(filterOperation); |
223 | ComponentTransferFunction transferFunction; |
224 | transferFunction.type = FECOMPONENTTRANSFER_TYPE_TABLE; |
225 | Vector<float> transferParameters; |
226 | transferParameters.append(narrowPrecisionToFloat(componentTransferOperation.amount())); |
227 | transferParameters.append(narrowPrecisionToFloat(1 - componentTransferOperation.amount())); |
228 | transferFunction.tableValues = transferParameters; |
229 | |
230 | ComponentTransferFunction nullFunction; |
231 | effect = FEComponentTransfer::create(*this, transferFunction, transferFunction, transferFunction, nullFunction); |
232 | break; |
233 | } |
234 | case FilterOperation::APPLE_INVERT_LIGHTNESS: |
235 | ASSERT_NOT_REACHED(); // APPLE_INVERT_LIGHTNESS is only used in -apple-color-filter. |
236 | break; |
237 | case FilterOperation::OPACITY: { |
238 | auto& componentTransferOperation = downcast<BasicComponentTransferFilterOperation>(filterOperation); |
239 | ComponentTransferFunction transferFunction; |
240 | transferFunction.type = FECOMPONENTTRANSFER_TYPE_TABLE; |
241 | Vector<float> transferParameters; |
242 | transferParameters.append(0); |
243 | transferParameters.append(narrowPrecisionToFloat(componentTransferOperation.amount())); |
244 | transferFunction.tableValues = transferParameters; |
245 | |
246 | ComponentTransferFunction nullFunction; |
247 | effect = FEComponentTransfer::create(*this, nullFunction, nullFunction, nullFunction, transferFunction); |
248 | break; |
249 | } |
250 | case FilterOperation::BRIGHTNESS: { |
251 | auto& componentTransferOperation = downcast<BasicComponentTransferFilterOperation>(filterOperation); |
252 | ComponentTransferFunction transferFunction; |
253 | transferFunction.type = FECOMPONENTTRANSFER_TYPE_LINEAR; |
254 | transferFunction.slope = narrowPrecisionToFloat(componentTransferOperation.amount()); |
255 | transferFunction.intercept = 0; |
256 | |
257 | ComponentTransferFunction nullFunction; |
258 | effect = FEComponentTransfer::create(*this, transferFunction, transferFunction, transferFunction, nullFunction); |
259 | break; |
260 | } |
261 | case FilterOperation::CONTRAST: { |
262 | auto& componentTransferOperation = downcast<BasicComponentTransferFilterOperation>(filterOperation); |
263 | ComponentTransferFunction transferFunction; |
264 | transferFunction.type = FECOMPONENTTRANSFER_TYPE_LINEAR; |
265 | float amount = narrowPrecisionToFloat(componentTransferOperation.amount()); |
266 | transferFunction.slope = amount; |
267 | transferFunction.intercept = -0.5 * amount + 0.5; |
268 | |
269 | ComponentTransferFunction nullFunction; |
270 | effect = FEComponentTransfer::create(*this, transferFunction, transferFunction, transferFunction, nullFunction); |
271 | break; |
272 | } |
273 | case FilterOperation::BLUR: { |
274 | auto& blurOperation = downcast<BlurFilterOperation>(filterOperation); |
275 | float stdDeviation = floatValueForLength(blurOperation.stdDeviation(), 0); |
276 | effect = FEGaussianBlur::create(*this, stdDeviation, stdDeviation, consumer == FilterConsumer::FilterProperty ? EDGEMODE_NONE : EDGEMODE_DUPLICATE); |
277 | break; |
278 | } |
279 | case FilterOperation::DROP_SHADOW: { |
280 | auto& dropShadowOperation = downcast<DropShadowFilterOperation>(filterOperation); |
281 | effect = FEDropShadow::create(*this, dropShadowOperation.stdDeviation(), dropShadowOperation.stdDeviation(), |
282 | dropShadowOperation.x(), dropShadowOperation.y(), dropShadowOperation.color(), 1); |
283 | break; |
284 | } |
285 | default: |
286 | break; |
287 | } |
288 | |
289 | if (effect) { |
290 | // Unlike SVG Filters and CSSFilterImages, filter functions on the filter |
291 | // property applied here should not clip to their primitive subregions. |
292 | effect->setClipsToBounds(consumer == FilterConsumer::FilterFunction); |
293 | effect->setOperatingColorSpace(ColorSpaceSRGB); |
294 | |
295 | if (filterOperation.type() != FilterOperation::REFERENCE) { |
296 | effect->inputEffects().append(WTFMove(previousEffect)); |
297 | m_effects.append(*effect); |
298 | } |
299 | previousEffect = WTFMove(effect); |
300 | } |
301 | } |
302 | |
303 | // If we didn't make any effects, tell our caller we are not valid. |
304 | if (m_effects.isEmpty()) |
305 | return false; |
306 | |
307 | setMaxEffectRects(m_sourceDrawingRegion); |
308 | return true; |
309 | } |
310 | |
311 | bool CSSFilter::updateBackingStoreRect(const FloatRect& filterRect) |
312 | { |
313 | if (filterRect.isEmpty() || ImageBuffer::sizeNeedsClamping(filterRect.size())) |
314 | return false; |
315 | |
316 | if (filterRect == sourceImageRect()) |
317 | return false; |
318 | |
319 | setSourceImageRect(filterRect); |
320 | return true; |
321 | } |
322 | |
323 | void CSSFilter::allocateBackingStoreIfNeeded(const GraphicsContext& targetContext) |
324 | { |
325 | // At this point the effect chain has been built, and the |
326 | // source image sizes set. We just need to attach the graphic |
327 | // buffer if we have not yet done so. |
328 | |
329 | if (m_graphicsBufferAttached) |
330 | return; |
331 | |
332 | IntSize logicalSize { m_sourceDrawingRegion.size() }; |
333 | if (!sourceImage() || sourceImage()->logicalSize() != logicalSize) { |
334 | #if USE(DIRECT2D) |
335 | setSourceImage(ImageBuffer::create(logicalSize, renderingMode(), &targetContext, filterScale())); |
336 | #else |
337 | UNUSED_PARAM(targetContext); |
338 | setSourceImage(ImageBuffer::create(logicalSize, renderingMode(), filterScale())); |
339 | #endif |
340 | } |
341 | m_graphicsBufferAttached = true; |
342 | } |
343 | |
344 | void CSSFilter::determineFilterPrimitiveSubregion() |
345 | { |
346 | auto& lastEffect = m_effects.last().get(); |
347 | lastEffect.determineFilterPrimitiveSubregion(); |
348 | FloatRect subRegion = lastEffect.maxEffectRect(); |
349 | // At least one FilterEffect has a too big image size, recalculate the effect sizes with new scale factors. |
350 | FloatSize scale; |
351 | if (ImageBuffer::sizeNeedsClamping(subRegion.size(), scale)) { |
352 | setFilterResolution(scale); |
353 | lastEffect.determineFilterPrimitiveSubregion(); |
354 | } |
355 | } |
356 | |
357 | void CSSFilter::clearIntermediateResults() |
358 | { |
359 | m_sourceGraphic->clearResult(); |
360 | if (m_sourceAlpha) |
361 | m_sourceAlpha->clearResult(); |
362 | for (auto& effect : m_effects) |
363 | effect->clearResult(); |
364 | } |
365 | |
366 | void CSSFilter::apply() |
367 | { |
368 | auto& effect = m_effects.last().get(); |
369 | effect.apply(); |
370 | effect.transformResultColorSpace(ColorSpaceSRGB); |
371 | } |
372 | |
373 | LayoutRect CSSFilter::computeSourceImageRectForDirtyRect(const LayoutRect& filterBoxRect, const LayoutRect& dirtyRect) |
374 | { |
375 | // The result of this function is the area in the "filterBoxRect" that needs to be repainted, so that we fully cover the "dirtyRect". |
376 | auto rectForRepaint = dirtyRect; |
377 | if (hasFilterThatMovesPixels()) { |
378 | // Note that the outsets are reversed here because we are going backwards -> we have the dirty rect and |
379 | // need to find out what is the rectangle that might influence the result inside that dirty rect. |
380 | rectForRepaint.move(-m_outsets.right(), -m_outsets.bottom()); |
381 | rectForRepaint.expand(m_outsets.left() + m_outsets.right(), m_outsets.top() + m_outsets.bottom()); |
382 | } |
383 | rectForRepaint.intersect(filterBoxRect); |
384 | return rectForRepaint; |
385 | } |
386 | |
387 | ImageBuffer* CSSFilter::output() const |
388 | { |
389 | return m_effects.last()->imageBufferResult(); |
390 | } |
391 | |
392 | void CSSFilter::setSourceImageRect(const FloatRect& sourceImageRect) |
393 | { |
394 | m_sourceDrawingRegion = sourceImageRect; |
395 | setMaxEffectRects(sourceImageRect); |
396 | setFilterRegion(sourceImageRect); |
397 | m_graphicsBufferAttached = false; |
398 | } |
399 | |
400 | |
401 | void CSSFilter::setMaxEffectRects(const FloatRect& effectRect) |
402 | { |
403 | for (auto& effect : m_effects) |
404 | effect->setMaxEffectRect(effectRect); |
405 | } |
406 | |
407 | IntRect CSSFilter::outputRect() const |
408 | { |
409 | auto& lastEffect = m_effects.last().get(); |
410 | if (!lastEffect.hasResult()) |
411 | return { }; |
412 | return lastEffect.requestedRegionOfInputImageData(IntRect { m_filterRegion }); |
413 | } |
414 | |
415 | } // namespace WebCore |
416 | |