4254ef9097b8d23cc609d50376b39042b971b79e
[WebKit-https.git] / Source / WebCore / rendering / FilterEffectRenderer.cpp
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 "FilterEffectRenderer.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 "RenderLayer.h"
39 #include "SVGElement.h"
40 #include "SVGFilterBuilder.h"
41 #include "SVGFilterPrimitiveStandardAttributes.h"
42 #include "SourceGraphic.h"
43 #include <algorithm>
44 #include <wtf/MathExtras.h>
45
46 #if USE(DIRECT2D)
47 #include <d2d1.h>
48 #endif
49
50 namespace WebCore {
51
52 static inline void endMatrixRow(Vector<float>& parameters)
53 {
54     parameters.append(0);
55     parameters.append(0);
56 }
57
58 static inline void lastMatrixRow(Vector<float>& parameters)
59 {
60     parameters.append(0);
61     parameters.append(0);
62     parameters.append(0);
63     parameters.append(1);
64     parameters.append(0);
65 }
66
67 inline FilterEffectRenderer::FilterEffectRenderer()
68     : Filter(FloatSize { 1, 1 })
69     , m_sourceGraphic(SourceGraphic::create(*this))
70 {
71 }
72
73 Ref<FilterEffectRenderer> FilterEffectRenderer::create()
74 {
75     return adoptRef(*new FilterEffectRenderer);
76 }
77
78 FilterEffectRenderer::~FilterEffectRenderer()
79 {
80 }
81
82 GraphicsContext* FilterEffectRenderer::inputContext()
83 {
84     return sourceImage() ? &sourceImage()->context() : nullptr;
85 }
86
87 RefPtr<FilterEffect> FilterEffectRenderer::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
116     for (auto& effectElement : childrenOfType<SVGFilterPrimitiveStandardAttributes>(*filter)) {
117         effect = effectElement.build(builder.get(), *this);
118         if (!effect)
119             continue;
120
121         effectElement.setStandardAttributes(effect.get());
122         builder->add(effectElement.result(), effect);
123         m_effects.append(*effect);
124     }
125     return effect;
126 }
127
128 bool FilterEffectRenderer::build(RenderElement& renderer, const FilterOperations& operations, FilterConsumer consumer)
129 {
130     m_hasFilterThatMovesPixels = operations.hasFilterThatMovesPixels();
131     m_hasFilterThatShouldBeRestrictedBySecurityOrigin = operations.hasFilterThatShouldBeRestrictedBySecurityOrigin();
132     if (m_hasFilterThatMovesPixels)
133         m_outsets = operations.outsets();
134
135     m_effects.clear();
136
137     RefPtr<FilterEffect> previousEffect = m_sourceGraphic.ptr();
138     for (auto& operation : operations.operations()) {
139         RefPtr<FilterEffect> effect;
140         auto& filterOperation = *operation;
141         switch (filterOperation.type()) {
142         case FilterOperation::REFERENCE: {
143             auto& referenceOperation = downcast<ReferenceFilterOperation>(filterOperation);
144             effect = buildReferenceFilter(renderer, *previousEffect, referenceOperation);
145             referenceOperation.setFilterEffect(effect);
146             break;
147         }
148         case FilterOperation::GRAYSCALE: {
149             auto& colorMatrixOperation = downcast<BasicColorMatrixFilterOperation>(filterOperation);
150             Vector<float> inputParameters;
151             double oneMinusAmount = clampTo(1 - colorMatrixOperation.amount(), 0.0, 1.0);
152
153             // See https://dvcs.w3.org/hg/FXTF/raw-file/tip/filters/index.html#grayscaleEquivalent
154             // for information on parameters.
155
156             inputParameters.append(narrowPrecisionToFloat(0.2126 + 0.7874 * oneMinusAmount));
157             inputParameters.append(narrowPrecisionToFloat(0.7152 - 0.7152 * oneMinusAmount));
158             inputParameters.append(narrowPrecisionToFloat(0.0722 - 0.0722 * oneMinusAmount));
159             endMatrixRow(inputParameters);
160
161             inputParameters.append(narrowPrecisionToFloat(0.2126 - 0.2126 * oneMinusAmount));
162             inputParameters.append(narrowPrecisionToFloat(0.7152 + 0.2848 * oneMinusAmount));
163             inputParameters.append(narrowPrecisionToFloat(0.0722 - 0.0722 * oneMinusAmount));
164             endMatrixRow(inputParameters);
165
166             inputParameters.append(narrowPrecisionToFloat(0.2126 - 0.2126 * oneMinusAmount));
167             inputParameters.append(narrowPrecisionToFloat(0.7152 - 0.7152 * oneMinusAmount));
168             inputParameters.append(narrowPrecisionToFloat(0.0722 + 0.9278 * oneMinusAmount));
169             endMatrixRow(inputParameters);
170
171             lastMatrixRow(inputParameters);
172
173             effect = FEColorMatrix::create(*this, FECOLORMATRIX_TYPE_MATRIX, inputParameters);
174             break;
175         }
176         case FilterOperation::SEPIA: {
177             auto& colorMatrixOperation = downcast<BasicColorMatrixFilterOperation>(filterOperation);
178             Vector<float> inputParameters;
179             double oneMinusAmount = clampTo(1 - colorMatrixOperation.amount(), 0.0, 1.0);
180
181             // See https://dvcs.w3.org/hg/FXTF/raw-file/tip/filters/index.html#sepiaEquivalent
182             // for information on parameters.
183
184             inputParameters.append(narrowPrecisionToFloat(0.393 + 0.607 * oneMinusAmount));
185             inputParameters.append(narrowPrecisionToFloat(0.769 - 0.769 * oneMinusAmount));
186             inputParameters.append(narrowPrecisionToFloat(0.189 - 0.189 * oneMinusAmount));
187             endMatrixRow(inputParameters);
188
189             inputParameters.append(narrowPrecisionToFloat(0.349 - 0.349 * oneMinusAmount));
190             inputParameters.append(narrowPrecisionToFloat(0.686 + 0.314 * oneMinusAmount));
191             inputParameters.append(narrowPrecisionToFloat(0.168 - 0.168 * oneMinusAmount));
192             endMatrixRow(inputParameters);
193
194             inputParameters.append(narrowPrecisionToFloat(0.272 - 0.272 * oneMinusAmount));
195             inputParameters.append(narrowPrecisionToFloat(0.534 - 0.534 * oneMinusAmount));
196             inputParameters.append(narrowPrecisionToFloat(0.131 + 0.869 * oneMinusAmount));
197             endMatrixRow(inputParameters);
198
199             lastMatrixRow(inputParameters);
200
201             effect = FEColorMatrix::create(*this, FECOLORMATRIX_TYPE_MATRIX, inputParameters);
202             break;
203         }
204         case FilterOperation::SATURATE: {
205             auto& colorMatrixOperation = downcast<BasicColorMatrixFilterOperation>(filterOperation);
206             Vector<float> inputParameters;
207             inputParameters.append(narrowPrecisionToFloat(colorMatrixOperation.amount()));
208             effect = FEColorMatrix::create(*this, FECOLORMATRIX_TYPE_SATURATE, inputParameters);
209             break;
210         }
211         case FilterOperation::HUE_ROTATE: {
212             auto& colorMatrixOperation = downcast<BasicColorMatrixFilterOperation>(filterOperation);
213             Vector<float> inputParameters;
214             inputParameters.append(narrowPrecisionToFloat(colorMatrixOperation.amount()));
215             effect = FEColorMatrix::create(*this, FECOLORMATRIX_TYPE_HUEROTATE, inputParameters);
216             break;
217         }
218         case FilterOperation::INVERT: {
219             auto& componentTransferOperation = downcast<BasicComponentTransferFilterOperation>(filterOperation);
220             ComponentTransferFunction transferFunction;
221             transferFunction.type = FECOMPONENTTRANSFER_TYPE_TABLE;
222             Vector<float> transferParameters;
223             transferParameters.append(narrowPrecisionToFloat(componentTransferOperation.amount()));
224             transferParameters.append(narrowPrecisionToFloat(1 - componentTransferOperation.amount()));
225             transferFunction.tableValues = transferParameters;
226
227             ComponentTransferFunction nullFunction;
228             effect = FEComponentTransfer::create(*this, transferFunction, transferFunction, transferFunction, nullFunction);
229             break;
230         }
231         case FilterOperation::OPACITY: {
232             auto& componentTransferOperation = downcast<BasicComponentTransferFilterOperation>(filterOperation);
233             ComponentTransferFunction transferFunction;
234             transferFunction.type = FECOMPONENTTRANSFER_TYPE_TABLE;
235             Vector<float> transferParameters;
236             transferParameters.append(0);
237             transferParameters.append(narrowPrecisionToFloat(componentTransferOperation.amount()));
238             transferFunction.tableValues = transferParameters;
239
240             ComponentTransferFunction nullFunction;
241             effect = FEComponentTransfer::create(*this, nullFunction, nullFunction, nullFunction, transferFunction);
242             break;
243         }
244         case FilterOperation::BRIGHTNESS: {
245             auto& componentTransferOperation = downcast<BasicComponentTransferFilterOperation>(filterOperation);
246             ComponentTransferFunction transferFunction;
247             transferFunction.type = FECOMPONENTTRANSFER_TYPE_LINEAR;
248             transferFunction.slope = narrowPrecisionToFloat(componentTransferOperation.amount());
249             transferFunction.intercept = 0;
250
251             ComponentTransferFunction nullFunction;
252             effect = FEComponentTransfer::create(*this, transferFunction, transferFunction, transferFunction, nullFunction);
253             break;
254         }
255         case FilterOperation::CONTRAST: {
256             auto& componentTransferOperation = downcast<BasicComponentTransferFilterOperation>(filterOperation);
257             ComponentTransferFunction transferFunction;
258             transferFunction.type = FECOMPONENTTRANSFER_TYPE_LINEAR;
259             float amount = narrowPrecisionToFloat(componentTransferOperation.amount());
260             transferFunction.slope = amount;
261             transferFunction.intercept = -0.5 * amount + 0.5;
262             
263             ComponentTransferFunction nullFunction;
264             effect = FEComponentTransfer::create(*this, transferFunction, transferFunction, transferFunction, nullFunction);
265             break;
266         }
267         case FilterOperation::BLUR: {
268             auto& blurOperation = downcast<BlurFilterOperation>(filterOperation);
269             float stdDeviation = floatValueForLength(blurOperation.stdDeviation(), 0);
270             effect = FEGaussianBlur::create(*this, stdDeviation, stdDeviation, consumer == FilterProperty ? EDGEMODE_NONE : EDGEMODE_DUPLICATE);
271             break;
272         }
273         case FilterOperation::DROP_SHADOW: {
274             auto& dropShadowOperation = downcast<DropShadowFilterOperation>(filterOperation);
275             effect = FEDropShadow::create(*this, dropShadowOperation.stdDeviation(), dropShadowOperation.stdDeviation(),
276                 dropShadowOperation.x(), dropShadowOperation.y(), dropShadowOperation.color(), 1);
277             break;
278         }
279         default:
280             break;
281         }
282
283         if (effect) {
284             // Unlike SVG Filters and CSSFilterImages, filter functions on the filter
285             // property applied here should not clip to their primitive subregions.
286             effect->setClipsToBounds(consumer == FilterFunction);
287             effect->setOperatingColorSpace(ColorSpaceSRGB);
288             
289             if (filterOperation.type() != FilterOperation::REFERENCE) {
290                 effect->inputEffects().append(WTFMove(previousEffect));
291                 m_effects.append(*effect);
292             }
293             previousEffect = WTFMove(effect);
294         }
295     }
296
297     // If we didn't make any effects, tell our caller we are not valid.
298     if (m_effects.isEmpty())
299         return false;
300
301     setMaxEffectRects(m_sourceDrawingRegion);
302     return true;
303 }
304
305 bool FilterEffectRenderer::updateBackingStoreRect(const FloatRect& filterRect)
306 {
307     if (filterRect.isEmpty() || ImageBuffer::sizeNeedsClamping(filterRect.size()))
308         return false;
309
310     if (filterRect == sourceImageRect())
311         return false;
312
313     setSourceImageRect(filterRect);
314     return true;
315 }
316
317 void FilterEffectRenderer::allocateBackingStoreIfNeeded(const GraphicsContext& targetContext)
318 {
319     // At this point the effect chain has been built, and the
320     // source image sizes set. We just need to attach the graphic
321     // buffer if we have not yet done so.
322
323     if (m_graphicsBufferAttached)
324         return;
325
326     IntSize logicalSize { m_sourceDrawingRegion.size() };
327     if (!sourceImage() || sourceImage()->logicalSize() != logicalSize) {
328 #if USE(DIRECT2D)
329         setSourceImage(ImageBuffer::create(logicalSize, renderingMode(), &targetContext, filterScale()));
330 #else
331         UNUSED_PARAM(targetContext);
332         setSourceImage(ImageBuffer::create(logicalSize, renderingMode(), filterScale()));
333 #endif
334     }
335     m_graphicsBufferAttached = true;
336 }
337
338 void FilterEffectRenderer::clearIntermediateResults()
339 {
340     m_sourceGraphic->clearResult();
341     for (auto& effect : m_effects)
342         effect->clearResult();
343 }
344
345 void FilterEffectRenderer::apply()
346 {
347     auto& effect = m_effects.last().get();
348     effect.apply();
349     effect.transformResultColorSpace(ColorSpaceSRGB);
350 }
351
352 LayoutRect FilterEffectRenderer::computeSourceImageRectForDirtyRect(const LayoutRect& filterBoxRect, const LayoutRect& dirtyRect)
353 {
354     // The result of this function is the area in the "filterBoxRect" that needs to be repainted, so that we fully cover the "dirtyRect".
355     auto rectForRepaint = dirtyRect;
356     if (hasFilterThatMovesPixels()) {
357         // Note that the outsets are reversed here because we are going backwards -> we have the dirty rect and
358         // need to find out what is the rectangle that might influence the result inside that dirty rect.
359         rectForRepaint.move(-m_outsets.right(), -m_outsets.bottom());
360         rectForRepaint.expand(m_outsets.left() + m_outsets.right(), m_outsets.top() + m_outsets.bottom());
361     }
362     rectForRepaint.intersect(filterBoxRect);
363     return rectForRepaint;
364 }
365
366 ImageBuffer* FilterEffectRenderer::output() const
367 {
368     return m_effects.last()->asImageBuffer();
369 }
370
371 void FilterEffectRenderer::setMaxEffectRects(const FloatRect& effectRect)
372 {
373     for (auto& effect : m_effects)
374         effect->setMaxEffectRect(effectRect);
375 }
376
377 IntRect FilterEffectRenderer::outputRect() const
378 {
379     auto& lastEffect = m_effects.last().get();
380     if (!lastEffect.hasResult())
381         return { };
382     return lastEffect.requestedRegionOfInputImageData(IntRect { m_filterRegion });
383 }
384
385 bool FilterEffectRendererHelper::prepareFilterEffect(RenderLayer& layer, const LayoutRect& filterBoxRect, const LayoutRect& dirtyRect, const LayoutRect& layerRepaintRect)
386 {
387     ASSERT(m_haveFilterEffect);
388     ASSERT(layer.filterRenderer());
389
390     auto& filter = *layer.filterRenderer();
391     auto filterSourceRect = filter.computeSourceImageRectForDirtyRect(filterBoxRect, dirtyRect);
392
393     if (filterSourceRect.isEmpty()) {
394         // The dirty rect is not in view, just bail out.
395         m_haveFilterEffect = false;
396         return false;
397     }
398
399     bool hasUpdatedBackingStore = filter.updateBackingStoreRect(filterSourceRect);
400
401     m_renderLayer = &layer;
402     if (!filter.hasFilterThatMovesPixels())
403         m_repaintRect = dirtyRect;
404     else {
405         if (hasUpdatedBackingStore)
406             m_repaintRect = filterSourceRect;
407         else {
408             m_repaintRect = dirtyRect;
409             m_repaintRect.unite(layerRepaintRect);
410             m_repaintRect.intersect(filterSourceRect);
411         }
412     }
413     m_paintOffset = filterSourceRect.location();
414
415     return true;
416 }
417
418 GraphicsContext* FilterEffectRendererHelper::filterContext() const
419 {
420     if (!m_haveFilterEffect)
421         return nullptr;
422     return m_renderLayer->filterRenderer()->inputContext();
423 }
424
425 bool FilterEffectRendererHelper::beginFilterEffect()
426 {
427     ASSERT(m_renderLayer);
428     ASSERT(m_renderLayer->filterRenderer());
429
430     auto& filter = *m_renderLayer->filterRenderer();
431     filter.allocateBackingStoreIfNeeded(m_targetContext);
432     // Paint into the context that represents the SourceGraphic of the filter.
433     auto* sourceGraphicsContext = filter.inputContext();
434     if (!sourceGraphicsContext || filter.filterRegion().isEmpty() || ImageBuffer::sizeNeedsClamping(filter.filterRegion().size())) {
435         // Disable the filters and continue.
436         m_haveFilterEffect = false;
437         return false;
438     }
439
440     // Translate the context so that the contents of the layer is captured in the offscreen memory buffer.
441     sourceGraphicsContext->save();
442     sourceGraphicsContext->translate(-m_paintOffset.x(), -m_paintOffset.y());
443     sourceGraphicsContext->clearRect(m_repaintRect);
444     sourceGraphicsContext->clip(m_repaintRect);
445
446     m_startedFilterEffect = true;
447     return true;
448 }
449
450 void FilterEffectRendererHelper::applyFilterEffect(GraphicsContext& destinationContext)
451 {
452     ASSERT(m_haveFilterEffect);
453     ASSERT(m_renderLayer);
454     ASSERT(m_renderLayer->filterRenderer());
455     ASSERT(m_renderLayer->filterRenderer()->inputContext());
456
457     auto& filter = *m_renderLayer->filterRenderer();
458     filter.inputContext()->restore();
459
460     filter.apply();
461
462     // Get the filtered output and draw it in place.
463     LayoutRect destRect = filter.outputRect();
464     destRect.move(m_paintOffset.x(), m_paintOffset.y());
465
466     if (auto* outputBuffer = filter.output())
467         destinationContext.drawImageBuffer(*outputBuffer, snapRectToDevicePixels(destRect, m_renderLayer->renderer().document().deviceScaleFactor()));
468
469     filter.clearIntermediateResults();
470 }
471
472 } // namespace WebCore