2 * Copyright (C) 2011 Apple Inc. All rights reserved.
3 * Copyright (C) 2013 Google Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 #include "FilterEffectRenderer.h"
31 #include "CachedSVGDocument.h"
32 #include "CachedSVGDocumentReference.h"
33 #include "ColorSpace.h"
35 #include "ElementIterator.h"
36 #include "FEColorMatrix.h"
37 #include "FEComponentTransfer.h"
38 #include "FEDropShadow.h"
39 #include "FEGaussianBlur.h"
41 #include "FloatConversion.h"
43 #include "RenderLayer.h"
44 #include "SVGElement.h"
45 #include "SVGFilterPrimitiveStandardAttributes.h"
49 #include <wtf/MathExtras.h>
53 static inline void endMatrixRow(Vector<float>& parameters)
59 static inline void lastMatrixRow(Vector<float>& parameters)
68 FilterEffectRenderer::FilterEffectRenderer()
69 : Filter(AffineTransform())
70 , m_graphicsBufferAttached(false)
71 , m_hasFilterThatMovesPixels(false)
73 setFilterResolution(FloatSize(1, 1));
74 m_sourceGraphic = SourceGraphic::create(*this);
77 FilterEffectRenderer::~FilterEffectRenderer()
81 GraphicsContext* FilterEffectRenderer::inputContext()
83 return sourceImage() ? sourceImage()->context() : 0;
86 PassRefPtr<FilterEffect> FilterEffectRenderer::buildReferenceFilter(RenderElement* renderer, PassRefPtr<FilterEffect> previousEffect, ReferenceFilterOperation* filterOperation)
91 Document* document = &renderer->document();
93 CachedSVGDocumentReference* cachedSVGDocumentReference = filterOperation->cachedSVGDocumentReference();
94 CachedSVGDocument* cachedSVGDocument = cachedSVGDocumentReference ? cachedSVGDocumentReference->document() : 0;
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();
104 Element* filter = document->getElementById(filterOperation->fragment());
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);
113 RefPtr<FilterEffect> effect;
115 auto builder = std::make_unique<SVGFilterBuilder>(previousEffect);
117 for (auto& effectElement : childrenOfType<SVGFilterPrimitiveStandardAttributes>(*filter)) {
118 effect = effectElement.build(builder.get(), *this);
122 effectElement.setStandardAttributes(effect.get());
123 builder->add(effectElement.result(), effect);
124 m_effects.append(effect);
129 bool FilterEffectRenderer::build(RenderElement* renderer, const FilterOperations& operations, FilterConsumer consumer)
131 m_hasFilterThatMovesPixels = operations.hasFilterThatMovesPixels();
132 if (m_hasFilterThatMovesPixels)
133 m_outsets = operations.outsets();
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);
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);
153 // See https://dvcs.w3.org/hg/FXTF/raw-file/tip/filters/index.html#grayscaleEquivalent
154 // for information on parameters.
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);
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);
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);
171 lastMatrixRow(inputParameters);
173 effect = FEColorMatrix::create(*this, FECOLORMATRIX_TYPE_MATRIX, inputParameters);
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);
181 // See https://dvcs.w3.org/hg/FXTF/raw-file/tip/filters/index.html#sepiaEquivalent
182 // for information on parameters.
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);
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);
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);
199 lastMatrixRow(inputParameters);
201 effect = FEColorMatrix::create(*this, FECOLORMATRIX_TYPE_MATRIX, inputParameters);
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);
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);
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;
227 ComponentTransferFunction nullFunction;
228 effect = FEComponentTransfer::create(*this, transferFunction, transferFunction, transferFunction, nullFunction);
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;
240 ComponentTransferFunction nullFunction;
241 effect = FEComponentTransfer::create(*this, nullFunction, nullFunction, nullFunction, transferFunction);
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;
251 ComponentTransferFunction nullFunction;
252 effect = FEComponentTransfer::create(*this, transferFunction, transferFunction, transferFunction, nullFunction);
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;
263 ComponentTransferFunction nullFunction;
264 effect = FEComponentTransfer::create(*this, transferFunction, transferFunction, transferFunction, nullFunction);
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);
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);
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);
289 if (filterOperation.type() != FilterOperation::REFERENCE) {
290 effect->inputEffects().append(previousEffect);
291 m_effects.append(effect);
293 previousEffect = effect.release();
297 // If we didn't make any effects, tell our caller we are not valid
298 if (!m_effects.size())
301 setMaxEffectRects(m_sourceDrawingRegion);
306 bool FilterEffectRenderer::updateBackingStoreRect(const FloatRect& filterRect)
308 if (filterRect.isEmpty() || !ImageBuffer::isSizeClamped(filterRect.size()))
311 if (filterRect == sourceImageRect())
314 setSourceImageRect(filterRect);
318 void FilterEffectRenderer::allocateBackingStoreIfNeeded()
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;
331 void FilterEffectRenderer::clearIntermediateResults()
333 m_sourceGraphic->clearResult();
334 for (size_t i = 0; i < m_effects.size(); ++i)
335 m_effects[i]->clearResult();
338 void FilterEffectRenderer::apply()
340 RefPtr<FilterEffect> effect = lastEffect();
342 effect->transformResultColorSpace(ColorSpaceDeviceRGB);
345 LayoutRect FilterEffectRenderer::computeSourceImageRectForDirtyRect(const LayoutRect& filterBoxRect, const LayoutRect& dirtyRect)
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());
355 rectForRepaint.intersect(filterBoxRect);
356 return rectForRepaint;
359 bool FilterEffectRendererHelper::prepareFilterEffect(RenderLayer* renderLayer, const LayoutRect& filterBoxRect, const LayoutRect& dirtyRect, const LayoutRect& layerRepaintRect)
361 ASSERT(m_haveFilterEffect && renderLayer->filterRenderer());
362 m_renderLayer = renderLayer;
363 m_repaintRect = dirtyRect;
365 FilterEffectRenderer* filter = renderLayer->filterRenderer();
366 LayoutRect filterSourceRect = filter->computeSourceImageRectForDirtyRect(filterBoxRect, dirtyRect);
367 m_paintOffset = filterSourceRect.location();
369 if (filterSourceRect.isEmpty()) {
370 // The dirty rect is not in view, just bail out.
371 m_haveFilterEffect = false;
375 bool hasUpdatedBackingStore = filter->updateBackingStoreRect(filterSourceRect);
376 if (filter->hasFilterThatMovesPixels()) {
377 if (hasUpdatedBackingStore)
378 m_repaintRect = filterSourceRect;
380 m_repaintRect.unite(layerRepaintRect);
381 m_repaintRect.intersect(filterSourceRect);
387 GraphicsContext* FilterEffectRendererHelper::filterContext() const
389 if (!m_haveFilterEffect)
392 FilterEffectRenderer* filter = m_renderLayer->filterRenderer();
393 return filter->inputContext();
396 bool FilterEffectRendererHelper::beginFilterEffect()
398 ASSERT(m_renderLayer);
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::isSizeClamped(filter->filterRegion().size())) {
405 // Disable the filters and continue.
406 m_haveFilterEffect = false;
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);
416 m_startedFilterEffect = true;
420 void FilterEffectRendererHelper::applyFilterEffect(GraphicsContext* destinationContext)
422 ASSERT(m_haveFilterEffect && m_renderLayer->filterRenderer());
423 FilterEffectRenderer* filter = m_renderLayer->filterRenderer();
424 filter->inputContext()->restore();
428 // Get the filtered output and draw it in place.
429 LayoutRect destRect = filter->outputRect();
430 destRect.move(m_paintOffset.x(), m_paintOffset.y());
432 destinationContext->drawImageBuffer(filter->output(), m_renderLayer->renderer().style().colorSpace(),
433 snapRectToDevicePixels(destRect, m_renderLayer->renderer().document().deviceScaleFactor()));
435 filter->clearIntermediateResults();
438 } // namespace WebCore