5757e9a85183c07680ba19f4a6a321e6576d1c46
[WebKit-https.git] / Source / WebCore / rendering / FilterEffectRenderer.cpp
1 /*
2  * Copyright (C) 2011 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
29 #include "FilterEffectRenderer.h"
30
31 #include "CachedSVGDocument.h"
32 #include "CachedSVGDocumentReference.h"
33 #include "ColorSpace.h"
34 #include "Document.h"
35 #include "ElementIterator.h"
36 #include "FEColorMatrix.h"
37 #include "FEComponentTransfer.h"
38 #include "FEDropShadow.h"
39 #include "FEGaussianBlur.h"
40 #include "FEMerge.h"
41 #include "FloatConversion.h"
42 #include "Frame.h"
43 #include "RenderLayer.h"
44 #include "SVGElement.h"
45 #include "SVGFilterPrimitiveStandardAttributes.h"
46 #include "Settings.h"
47
48 #include <algorithm>
49 #include <wtf/MathExtras.h>
50
51 namespace WebCore {
52
53 static inline void endMatrixRow(Vector<float>& parameters)
54 {
55     parameters.append(0);
56     parameters.append(0);
57 }
58
59 static inline void lastMatrixRow(Vector<float>& parameters)
60 {
61     parameters.append(0);
62     parameters.append(0);
63     parameters.append(0);
64     parameters.append(1);
65     parameters.append(0);
66 }
67
68 FilterEffectRenderer::FilterEffectRenderer()
69     : Filter(AffineTransform())
70     , m_graphicsBufferAttached(false)
71     , m_hasFilterThatMovesPixels(false)
72 {
73     setFilterResolution(FloatSize(1, 1));
74     m_sourceGraphic = SourceGraphic::create(*this);
75 }
76
77 FilterEffectRenderer::~FilterEffectRenderer()
78 {
79 }
80
81 GraphicsContext* FilterEffectRenderer::inputContext()
82 {
83     return sourceImage() ? &sourceImage()->context() : nullptr;
84 }
85
86 PassRefPtr<FilterEffect> FilterEffectRenderer::buildReferenceFilter(RenderElement* renderer, PassRefPtr<FilterEffect> previousEffect, ReferenceFilterOperation* filterOperation)
87 {
88     if (!renderer)
89         return nullptr;
90
91     Document* document = &renderer->document();
92
93     CachedSVGDocumentReference* cachedSVGDocumentReference = filterOperation->cachedSVGDocumentReference();
94     CachedSVGDocument* cachedSVGDocument = cachedSVGDocumentReference ? cachedSVGDocumentReference->document() : 0;
95
96     // If we have an SVG document, this is an external reference. Otherwise
97     // we look up the referenced node in the current document.
98     if (cachedSVGDocument)
99         document = cachedSVGDocument->document();
100
101     if (!document)
102         return nullptr;
103
104     Element* filter = document->getElementById(filterOperation->fragment());
105     if (!filter) {
106         // Although we did not find the referenced filter, it might exist later in the document.
107         // FIXME: This skips anonymous RenderObjects. <https://webkit.org/b/131085>
108         if (Element* element = renderer->element())
109             document->accessSVGExtensions().addPendingResource(filterOperation->fragment(), element);
110         return nullptr;
111     }
112
113     RefPtr<FilterEffect> effect;
114
115     auto builder = std::make_unique<SVGFilterBuilder>(previousEffect);
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         builder->add(effectElement.result(), effect);
124         m_effects.append(effect);
125     }
126     return effect;
127 }
128
129 bool FilterEffectRenderer::build(RenderElement* renderer, const FilterOperations& operations, FilterConsumer consumer)
130 {
131     m_hasFilterThatMovesPixels = operations.hasFilterThatMovesPixels();
132     if (m_hasFilterThatMovesPixels)
133         m_outsets = operations.outsets();
134
135     m_effects.clear();
136
137     RefPtr<FilterEffect> previousEffect = m_sourceGraphic;
138     for (size_t i = 0; i < operations.operations().size(); ++i) {
139         RefPtr<FilterEffect> effect;
140         FilterOperation& filterOperation = *operations.operations().at(i);
141         switch (filterOperation.type()) {
142         case FilterOperation::REFERENCE: {
143             ReferenceFilterOperation& referenceOperation = downcast<ReferenceFilterOperation>(filterOperation);
144             effect = buildReferenceFilter(renderer, previousEffect, &referenceOperation);
145             referenceOperation.setFilterEffect(effect);
146             break;
147         }
148         case FilterOperation::GRAYSCALE: {
149             BasicColorMatrixFilterOperation& 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             BasicColorMatrixFilterOperation& 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             BasicColorMatrixFilterOperation& 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             BasicColorMatrixFilterOperation& 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             BasicComponentTransferFilterOperation& 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             BasicComponentTransferFilterOperation& 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             BasicComponentTransferFilterOperation& 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             BasicComponentTransferFilterOperation& 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             BlurFilterOperation& 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             DropShadowFilterOperation& 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(ColorSpaceDeviceRGB);
288             
289             if (filterOperation.type() != FilterOperation::REFERENCE) {
290                 effect->inputEffects().append(previousEffect);
291                 m_effects.append(effect);
292             }
293             previousEffect = effect.release();
294         }
295     }
296
297     // If we didn't make any effects, tell our caller we are not valid
298     if (!m_effects.size())
299         return false;
300
301     setMaxEffectRects(m_sourceDrawingRegion);
302     
303     return true;
304 }
305
306 bool FilterEffectRenderer::updateBackingStoreRect(const FloatRect& filterRect)
307 {
308     if (filterRect.isEmpty() || ImageBuffer::sizeNeedsClamping(filterRect.size()))
309         return false;
310
311     if (filterRect == sourceImageRect())
312         return false;
313
314     setSourceImageRect(filterRect);
315     return true;
316 }
317
318 void FilterEffectRenderer::allocateBackingStoreIfNeeded()
319 {
320     // At this point the effect chain has been built, and the
321     // source image sizes set. We just need to attach the graphic
322     // buffer if we have not yet done so.
323     if (!m_graphicsBufferAttached) {
324         IntSize logicalSize(m_sourceDrawingRegion.width(), m_sourceDrawingRegion.height());
325         if (!sourceImage() || sourceImage()->logicalSize() != logicalSize)
326             setSourceImage(ImageBuffer::create(logicalSize, filterScale(), ColorSpaceDeviceRGB, renderingMode()));
327         m_graphicsBufferAttached = true;
328     }
329 }
330
331 void FilterEffectRenderer::clearIntermediateResults()
332 {
333     m_sourceGraphic->clearResult();
334     for (size_t i = 0; i < m_effects.size(); ++i)
335         m_effects[i]->clearResult();
336 }
337
338 void FilterEffectRenderer::apply()
339 {
340     RefPtr<FilterEffect> effect = lastEffect();
341     effect->apply();
342     effect->transformResultColorSpace(ColorSpaceDeviceRGB);
343 }
344
345 LayoutRect FilterEffectRenderer::computeSourceImageRectForDirtyRect(const LayoutRect& filterBoxRect, const LayoutRect& dirtyRect)
346 {
347     // The result of this function is the area in the "filterBoxRect" that needs to be repainted, so that we fully cover the "dirtyRect".
348     LayoutRect rectForRepaint = dirtyRect;
349     if (hasFilterThatMovesPixels()) {
350         // Note that the outsets are reversed here because we are going backwards -> we have the dirty rect and
351         // need to find out what is the rectangle that might influence the result inside that dirty rect.
352         rectForRepaint.move(-m_outsets.right(), -m_outsets.bottom());
353         rectForRepaint.expand(m_outsets.left() + m_outsets.right(), m_outsets.top() + m_outsets.bottom());
354     }
355     rectForRepaint.intersect(filterBoxRect);
356     return rectForRepaint;
357 }
358
359 bool FilterEffectRendererHelper::prepareFilterEffect(RenderLayer* renderLayer, const LayoutRect& filterBoxRect, const LayoutRect& dirtyRect, const LayoutRect& layerRepaintRect)
360 {
361     ASSERT(m_haveFilterEffect && renderLayer->filterRenderer());
362     m_renderLayer = renderLayer;
363     m_repaintRect = dirtyRect;
364
365     FilterEffectRenderer* filter = renderLayer->filterRenderer();
366     LayoutRect filterSourceRect = filter->computeSourceImageRectForDirtyRect(filterBoxRect, dirtyRect);
367     m_paintOffset = filterSourceRect.location();
368
369     if (filterSourceRect.isEmpty()) {
370         // The dirty rect is not in view, just bail out.
371         m_haveFilterEffect = false;
372         return false;
373     }
374     
375     bool hasUpdatedBackingStore = filter->updateBackingStoreRect(filterSourceRect);
376     if (filter->hasFilterThatMovesPixels()) {
377         if (hasUpdatedBackingStore)
378             m_repaintRect = filterSourceRect;
379         else {
380             m_repaintRect.unite(layerRepaintRect);
381             m_repaintRect.intersect(filterSourceRect);
382         }
383     }
384     return true;
385 }
386
387 GraphicsContext* FilterEffectRendererHelper::filterContext() const
388 {
389     if (!m_haveFilterEffect)
390         return 0;
391
392     FilterEffectRenderer* filter = m_renderLayer->filterRenderer();
393     return filter->inputContext();
394 }
395
396 bool FilterEffectRendererHelper::beginFilterEffect()
397 {
398     ASSERT(m_renderLayer);
399     
400     FilterEffectRenderer* filter = m_renderLayer->filterRenderer();
401     filter->allocateBackingStoreIfNeeded();
402     // Paint into the context that represents the SourceGraphic of the filter.
403     GraphicsContext* sourceGraphicsContext = filter->inputContext();
404     if (!sourceGraphicsContext || filter->filterRegion().isEmpty() || ImageBuffer::sizeNeedsClamping(filter->filterRegion().size())) {
405         // Disable the filters and continue.
406         m_haveFilterEffect = false;
407         return false;
408     }
409     
410     // Translate the context so that the contents of the layer is captuterd in the offscreen memory buffer.
411     sourceGraphicsContext->save();
412     sourceGraphicsContext->translate(-m_paintOffset.x(), -m_paintOffset.y());
413     sourceGraphicsContext->clearRect(m_repaintRect);
414     sourceGraphicsContext->clip(m_repaintRect);
415
416     m_startedFilterEffect = true;
417     return true;
418 }
419
420 void FilterEffectRendererHelper::applyFilterEffect(GraphicsContext& destinationContext)
421 {
422     ASSERT(m_haveFilterEffect && m_renderLayer->filterRenderer());
423     FilterEffectRenderer* filter = m_renderLayer->filterRenderer();
424     filter->inputContext()->restore();
425
426     filter->apply();
427     
428     // Get the filtered output and draw it in place.
429     LayoutRect destRect = filter->outputRect();
430     destRect.move(m_paintOffset.x(), m_paintOffset.y());
431
432     destinationContext.drawImageBuffer(filter->output(), m_renderLayer->renderer().style().colorSpace(),
433         snapRectToDevicePixels(destRect, m_renderLayer->renderer().document().deviceScaleFactor()));
434
435     filter->clearIntermediateResults();
436 }
437
438 } // namespace WebCore