2 * Copyright (C) 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #if ENABLE(CSS_FILTERS)
30 #include "FilterEffectRenderer.h"
32 #include "CachedSVGDocument.h"
33 #include "CachedSVGDocumentReference.h"
34 #include "ColorSpace.h"
36 #include "ElementIterator.h"
37 #include "FEColorMatrix.h"
38 #include "FEComponentTransfer.h"
39 #include "FEDropShadow.h"
40 #include "FEGaussianBlur.h"
42 #include "FloatConversion.h"
44 #include "RenderLayer.h"
45 #include "SVGElement.h"
46 #include "SVGFilterPrimitiveStandardAttributes.h"
48 #include "SourceAlpha.h"
51 #include <wtf/MathExtras.h>
55 static inline void endMatrixRow(Vector<float>& parameters)
61 static inline void lastMatrixRow(Vector<float>& parameters)
70 FilterEffectRenderer::FilterEffectRenderer()
71 : m_graphicsBufferAttached(false)
72 , m_hasFilterThatMovesPixels(false)
74 setFilterResolution(FloatSize(1, 1));
75 m_sourceGraphic = SourceGraphic::create(this);
78 FilterEffectRenderer::~FilterEffectRenderer()
82 GraphicsContext* FilterEffectRenderer::inputContext()
84 return sourceImage() ? sourceImage()->context() : 0;
87 PassRefPtr<FilterEffect> FilterEffectRenderer::buildReferenceFilter(RenderElement* renderer, PassRefPtr<FilterEffect> previousEffect, ReferenceFilterOperation* filterOperation)
92 Document* document = &renderer->document();
94 CachedSVGDocumentReference* cachedSVGDocumentReference = filterOperation->cachedSVGDocumentReference();
95 CachedSVGDocument* cachedSVGDocument = cachedSVGDocumentReference ? cachedSVGDocumentReference->document() : 0;
97 // If we have an SVG document, this is an external reference. Otherwise
98 // we look up the referenced node in the current document.
99 if (cachedSVGDocument)
100 document = cachedSVGDocument->document();
105 Element* filter = document->getElementById(filterOperation->fragment());
107 // Although we did not find the referenced filter, it might exist later in the document.
108 // FIXME: This skips anonymous RenderObjects. <https://webkit.org/b/131085>
109 if (Element* element = renderer->element())
110 document->accessSVGExtensions()->addPendingResource(filterOperation->fragment(), element);
114 RefPtr<FilterEffect> effect;
116 // FIXME: Figure out what to do with SourceAlpha. Right now, we're
117 // using the alpha of the original input layer, which is obviously
118 // wrong. We should probably be extracting the alpha from the
119 // previousEffect, but this requires some more processing.
120 // This may need a spec clarification.
121 auto builder = std::make_unique<SVGFilterBuilder>(previousEffect, SourceAlpha::create(this));
123 for (auto& effectElement : childrenOfType<SVGFilterPrimitiveStandardAttributes>(*filter)) {
124 effect = effectElement.build(builder.get(), this);
128 effectElement.setStandardAttributes(effect.get());
129 builder->add(effectElement.result(), effect);
130 m_effects.append(effect);
135 bool FilterEffectRenderer::build(RenderElement* renderer, const FilterOperations& operations, FilterConsumer consumer)
137 m_hasFilterThatMovesPixels = operations.hasFilterThatMovesPixels();
138 if (m_hasFilterThatMovesPixels)
139 m_outsets = operations.outsets();
143 RefPtr<FilterEffect> previousEffect = m_sourceGraphic;
144 for (size_t i = 0; i < operations.operations().size(); ++i) {
145 RefPtr<FilterEffect> effect;
146 FilterOperation* filterOperation = operations.operations().at(i).get();
147 switch (filterOperation->type()) {
148 case FilterOperation::REFERENCE: {
149 ReferenceFilterOperation* referenceOperation = toReferenceFilterOperation(filterOperation);
150 effect = buildReferenceFilter(renderer, previousEffect, referenceOperation);
151 referenceOperation->setFilterEffect(effect);
154 case FilterOperation::GRAYSCALE: {
155 BasicColorMatrixFilterOperation* colorMatrixOperation = toBasicColorMatrixFilterOperation(filterOperation);
156 Vector<float> inputParameters;
157 double oneMinusAmount = clampTo(1 - colorMatrixOperation->amount(), 0.0, 1.0);
159 // See https://dvcs.w3.org/hg/FXTF/raw-file/tip/filters/index.html#grayscaleEquivalent
160 // for information on parameters.
162 inputParameters.append(narrowPrecisionToFloat(0.2126 + 0.7874 * oneMinusAmount));
163 inputParameters.append(narrowPrecisionToFloat(0.7152 - 0.7152 * oneMinusAmount));
164 inputParameters.append(narrowPrecisionToFloat(0.0722 - 0.0722 * oneMinusAmount));
165 endMatrixRow(inputParameters);
167 inputParameters.append(narrowPrecisionToFloat(0.2126 - 0.2126 * oneMinusAmount));
168 inputParameters.append(narrowPrecisionToFloat(0.7152 + 0.2848 * oneMinusAmount));
169 inputParameters.append(narrowPrecisionToFloat(0.0722 - 0.0722 * oneMinusAmount));
170 endMatrixRow(inputParameters);
172 inputParameters.append(narrowPrecisionToFloat(0.2126 - 0.2126 * oneMinusAmount));
173 inputParameters.append(narrowPrecisionToFloat(0.7152 - 0.7152 * oneMinusAmount));
174 inputParameters.append(narrowPrecisionToFloat(0.0722 + 0.9278 * oneMinusAmount));
175 endMatrixRow(inputParameters);
177 lastMatrixRow(inputParameters);
179 effect = FEColorMatrix::create(this, FECOLORMATRIX_TYPE_MATRIX, inputParameters);
182 case FilterOperation::SEPIA: {
183 BasicColorMatrixFilterOperation* colorMatrixOperation = toBasicColorMatrixFilterOperation(filterOperation);
184 Vector<float> inputParameters;
185 double oneMinusAmount = clampTo(1 - colorMatrixOperation->amount(), 0.0, 1.0);
187 // See https://dvcs.w3.org/hg/FXTF/raw-file/tip/filters/index.html#sepiaEquivalent
188 // for information on parameters.
190 inputParameters.append(narrowPrecisionToFloat(0.393 + 0.607 * oneMinusAmount));
191 inputParameters.append(narrowPrecisionToFloat(0.769 - 0.769 * oneMinusAmount));
192 inputParameters.append(narrowPrecisionToFloat(0.189 - 0.189 * oneMinusAmount));
193 endMatrixRow(inputParameters);
195 inputParameters.append(narrowPrecisionToFloat(0.349 - 0.349 * oneMinusAmount));
196 inputParameters.append(narrowPrecisionToFloat(0.686 + 0.314 * oneMinusAmount));
197 inputParameters.append(narrowPrecisionToFloat(0.168 - 0.168 * oneMinusAmount));
198 endMatrixRow(inputParameters);
200 inputParameters.append(narrowPrecisionToFloat(0.272 - 0.272 * oneMinusAmount));
201 inputParameters.append(narrowPrecisionToFloat(0.534 - 0.534 * oneMinusAmount));
202 inputParameters.append(narrowPrecisionToFloat(0.131 + 0.869 * oneMinusAmount));
203 endMatrixRow(inputParameters);
205 lastMatrixRow(inputParameters);
207 effect = FEColorMatrix::create(this, FECOLORMATRIX_TYPE_MATRIX, inputParameters);
210 case FilterOperation::SATURATE: {
211 BasicColorMatrixFilterOperation* colorMatrixOperation = toBasicColorMatrixFilterOperation(filterOperation);
212 Vector<float> inputParameters;
213 inputParameters.append(narrowPrecisionToFloat(colorMatrixOperation->amount()));
214 effect = FEColorMatrix::create(this, FECOLORMATRIX_TYPE_SATURATE, inputParameters);
217 case FilterOperation::HUE_ROTATE: {
218 BasicColorMatrixFilterOperation* colorMatrixOperation = toBasicColorMatrixFilterOperation(filterOperation);
219 Vector<float> inputParameters;
220 inputParameters.append(narrowPrecisionToFloat(colorMatrixOperation->amount()));
221 effect = FEColorMatrix::create(this, FECOLORMATRIX_TYPE_HUEROTATE, inputParameters);
224 case FilterOperation::INVERT: {
225 BasicComponentTransferFilterOperation* componentTransferOperation = toBasicComponentTransferFilterOperation(filterOperation);
226 ComponentTransferFunction transferFunction;
227 transferFunction.type = FECOMPONENTTRANSFER_TYPE_TABLE;
228 Vector<float> transferParameters;
229 transferParameters.append(narrowPrecisionToFloat(componentTransferOperation->amount()));
230 transferParameters.append(narrowPrecisionToFloat(1 - componentTransferOperation->amount()));
231 transferFunction.tableValues = transferParameters;
233 ComponentTransferFunction nullFunction;
234 effect = FEComponentTransfer::create(this, transferFunction, transferFunction, transferFunction, nullFunction);
237 case FilterOperation::OPACITY: {
238 BasicComponentTransferFilterOperation* componentTransferOperation = toBasicComponentTransferFilterOperation(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;
246 ComponentTransferFunction nullFunction;
247 effect = FEComponentTransfer::create(this, nullFunction, nullFunction, nullFunction, transferFunction);
250 case FilterOperation::BRIGHTNESS: {
251 BasicComponentTransferFilterOperation* componentTransferOperation = toBasicComponentTransferFilterOperation(filterOperation);
252 ComponentTransferFunction transferFunction;
253 transferFunction.type = FECOMPONENTTRANSFER_TYPE_LINEAR;
254 transferFunction.slope = narrowPrecisionToFloat(componentTransferOperation->amount());
255 transferFunction.intercept = 0;
257 ComponentTransferFunction nullFunction;
258 effect = FEComponentTransfer::create(this, transferFunction, transferFunction, transferFunction, nullFunction);
261 case FilterOperation::CONTRAST: {
262 BasicComponentTransferFilterOperation* componentTransferOperation = toBasicComponentTransferFilterOperation(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;
269 ComponentTransferFunction nullFunction;
270 effect = FEComponentTransfer::create(this, transferFunction, transferFunction, transferFunction, nullFunction);
273 case FilterOperation::BLUR: {
274 BlurFilterOperation* blurOperation = toBlurFilterOperation(filterOperation);
275 float stdDeviation = floatValueForLength(blurOperation->stdDeviation(), 0);
276 effect = FEGaussianBlur::create(this, stdDeviation, stdDeviation, consumer == FilterProperty ? EDGEMODE_NONE : EDGEMODE_DUPLICATE);
279 case FilterOperation::DROP_SHADOW: {
280 DropShadowFilterOperation* dropShadowOperation = toDropShadowFilterOperation(filterOperation);
281 effect = FEDropShadow::create(this, dropShadowOperation->stdDeviation(), dropShadowOperation->stdDeviation(),
282 dropShadowOperation->x(), dropShadowOperation->y(), dropShadowOperation->color(), 1);
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 == FilterFunction);
293 effect->setOperatingColorSpace(ColorSpaceDeviceRGB);
295 if (filterOperation->type() != FilterOperation::REFERENCE) {
296 effect->inputEffects().append(previousEffect);
297 m_effects.append(effect);
299 previousEffect = effect.release();
303 // If we didn't make any effects, tell our caller we are not valid
304 if (!m_effects.size())
307 setMaxEffectRects(m_sourceDrawingRegion);
312 bool FilterEffectRenderer::updateBackingStoreRect(const FloatRect& filterRect)
314 if (!filterRect.isZero() && FilterEffect::isFilterSizeValid(filterRect)) {
315 FloatRect currentSourceRect = sourceImageRect();
316 if (filterRect != currentSourceRect) {
317 setSourceImageRect(filterRect);
324 void FilterEffectRenderer::allocateBackingStoreIfNeeded()
326 // At this point the effect chain has been built, and the
327 // source image sizes set. We just need to attach the graphic
328 // buffer if we have not yet done so.
329 if (!m_graphicsBufferAttached) {
330 IntSize logicalSize(m_sourceDrawingRegion.width(), m_sourceDrawingRegion.height());
331 if (!sourceImage() || sourceImage()->logicalSize() != logicalSize)
332 setSourceImage(ImageBuffer::create(logicalSize, 1, ColorSpaceDeviceRGB, renderingMode()));
333 m_graphicsBufferAttached = true;
337 void FilterEffectRenderer::clearIntermediateResults()
339 m_sourceGraphic->clearResult();
340 for (size_t i = 0; i < m_effects.size(); ++i)
341 m_effects[i]->clearResult();
344 void FilterEffectRenderer::apply()
346 RefPtr<FilterEffect> effect = lastEffect();
348 effect->transformResultColorSpace(ColorSpaceDeviceRGB);
351 LayoutRect FilterEffectRenderer::computeSourceImageRectForDirtyRect(const LayoutRect& filterBoxRect, const LayoutRect& dirtyRect)
353 // The result of this function is the area in the "filterBoxRect" that needs to be repainted, so that we fully cover the "dirtyRect".
354 LayoutRect rectForRepaint = dirtyRect;
355 if (hasFilterThatMovesPixels()) {
356 // Note that the outsets are reversed here because we are going backwards -> we have the dirty rect and
357 // need to find out what is the rectangle that might influence the result inside that dirty rect.
358 rectForRepaint.move(-m_outsets.right(), -m_outsets.bottom());
359 rectForRepaint.expand(m_outsets.left() + m_outsets.right(), m_outsets.top() + m_outsets.bottom());
361 rectForRepaint.intersect(filterBoxRect);
362 return rectForRepaint;
365 bool FilterEffectRendererHelper::prepareFilterEffect(RenderLayer* renderLayer, const LayoutRect& filterBoxRect, const LayoutRect& dirtyRect, const LayoutRect& layerRepaintRect)
367 ASSERT(m_haveFilterEffect && renderLayer->filterRenderer());
368 m_renderLayer = renderLayer;
369 m_repaintRect = dirtyRect;
371 FilterEffectRenderer* filter = renderLayer->filterRenderer();
372 LayoutRect filterSourceRect = filter->computeSourceImageRectForDirtyRect(filterBoxRect, dirtyRect);
373 m_paintOffset = filterSourceRect.location();
375 if (filterSourceRect.isEmpty()) {
376 // The dirty rect is not in view, just bail out.
377 m_haveFilterEffect = false;
381 bool hasUpdatedBackingStore = filter->updateBackingStoreRect(filterSourceRect);
382 if (filter->hasFilterThatMovesPixels()) {
383 if (hasUpdatedBackingStore)
384 m_repaintRect = filterSourceRect;
386 m_repaintRect.unite(layerRepaintRect);
387 m_repaintRect.intersect(filterSourceRect);
393 GraphicsContext* FilterEffectRendererHelper::filterContext() const
395 if (!m_haveFilterEffect)
398 FilterEffectRenderer* filter = m_renderLayer->filterRenderer();
399 return filter->inputContext();
402 bool FilterEffectRendererHelper::beginFilterEffect()
404 ASSERT(m_renderLayer);
406 FilterEffectRenderer* filter = m_renderLayer->filterRenderer();
407 filter->allocateBackingStoreIfNeeded();
408 // Paint into the context that represents the SourceGraphic of the filter.
409 GraphicsContext* sourceGraphicsContext = filter->inputContext();
410 if (!sourceGraphicsContext || !FilterEffect::isFilterSizeValid(filter->filterRegion())) {
411 // Disable the filters and continue.
412 m_haveFilterEffect = false;
416 // Translate the context so that the contents of the layer is captuterd in the offscreen memory buffer.
417 sourceGraphicsContext->save();
418 sourceGraphicsContext->translate(-m_paintOffset.x(), -m_paintOffset.y());
419 sourceGraphicsContext->clearRect(m_repaintRect);
420 sourceGraphicsContext->clip(m_repaintRect);
422 m_startedFilterEffect = true;
426 void FilterEffectRendererHelper::applyFilterEffect(GraphicsContext* destinationContext)
428 ASSERT(m_haveFilterEffect && m_renderLayer->filterRenderer());
429 FilterEffectRenderer* filter = m_renderLayer->filterRenderer();
430 filter->inputContext()->restore();
434 // Get the filtered output and draw it in place.
435 LayoutRect destRect = filter->outputRect();
436 destRect.move(m_paintOffset.x(), m_paintOffset.y());
438 destinationContext->drawImageBuffer(filter->output(), m_renderLayer->renderer().style().colorSpace(),
439 pixelSnappedForPainting(destRect, m_renderLayer->renderer().document().deviceScaleFactor()), CompositeSourceOver);
441 filter->clearIntermediateResults();
444 } // namespace WebCore
446 #endif // ENABLE(CSS_FILTERS)