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 COMPUTER, 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 "core/rendering/FilterEffectRenderer.h"
31 #include "core/fetch/DocumentResource.h"
32 #include "core/fetch/DocumentResourceReference.h"
33 #include "core/page/Page.h"
34 #include "core/rendering/RenderLayer.h"
35 #include "core/rendering/RenderView.h"
36 #include "core/rendering/svg/ReferenceFilterBuilder.h"
37 #include "core/svg/SVGElement.h"
38 #include "core/svg/SVGFilterPrimitiveStandardAttributes.h"
39 #include "platform/FloatConversion.h"
40 #include "platform/LengthFunctions.h"
41 #include "platform/graphics/ColorSpace.h"
42 #include "platform/graphics/UnacceleratedImageBufferSurface.h"
43 #include "platform/graphics/filters/FEColorMatrix.h"
44 #include "platform/graphics/filters/FEComponentTransfer.h"
45 #include "platform/graphics/filters/FEDropShadow.h"
46 #include "platform/graphics/filters/FEGaussianBlur.h"
47 #include "wtf/MathExtras.h"
52 static inline void endMatrixRow(Vector<float>& parameters)
58 static inline void lastMatrixRow(Vector<float>& parameters)
67 FilterEffectRenderer::FilterEffectRenderer()
68 : Filter(AffineTransform())
69 , m_graphicsBufferAttached(false)
70 , m_hasFilterThatMovesPixels(false)
72 m_sourceGraphic = SourceGraphic::create(this);
75 FilterEffectRenderer::~FilterEffectRenderer()
79 GraphicsContext* FilterEffectRenderer::inputContext()
81 return sourceImage() ? sourceImage()->context() : 0;
84 bool FilterEffectRenderer::build(RenderObject* renderer, const FilterOperations& operations)
86 m_hasFilterThatMovesPixels = operations.hasFilterThatMovesPixels();
88 // Inverse zoom the pre-zoomed CSS shorthand filters, so that they are in the same zoom as the unzoomed reference filters.
89 const RenderStyle* style = renderer->style();
90 float invZoom = style ? 1.0f / style->effectiveZoom() : 1.0f;
92 RefPtr<FilterEffect> previousEffect = m_sourceGraphic;
93 for (size_t i = 0; i < operations.operations().size(); ++i) {
94 RefPtr<FilterEffect> effect;
95 FilterOperation* filterOperation = operations.operations().at(i).get();
96 switch (filterOperation->type()) {
97 case FilterOperation::REFERENCE: {
98 effect = ReferenceFilterBuilder::build(this, renderer, previousEffect.get(), toReferenceFilterOperation(filterOperation));
101 case FilterOperation::GRAYSCALE: {
102 Vector<float> inputParameters;
103 double oneMinusAmount = clampTo(1 - toBasicColorMatrixFilterOperation(filterOperation)->amount(), 0.0, 1.0);
105 // See https://dvcs.w3.org/hg/FXTF/raw-file/tip/filters/index.html#grayscaleEquivalent
106 // for information on parameters.
108 inputParameters.append(narrowPrecisionToFloat(0.2126 + 0.7874 * oneMinusAmount));
109 inputParameters.append(narrowPrecisionToFloat(0.7152 - 0.7152 * oneMinusAmount));
110 inputParameters.append(narrowPrecisionToFloat(0.0722 - 0.0722 * oneMinusAmount));
111 endMatrixRow(inputParameters);
113 inputParameters.append(narrowPrecisionToFloat(0.2126 - 0.2126 * oneMinusAmount));
114 inputParameters.append(narrowPrecisionToFloat(0.7152 + 0.2848 * oneMinusAmount));
115 inputParameters.append(narrowPrecisionToFloat(0.0722 - 0.0722 * oneMinusAmount));
116 endMatrixRow(inputParameters);
118 inputParameters.append(narrowPrecisionToFloat(0.2126 - 0.2126 * oneMinusAmount));
119 inputParameters.append(narrowPrecisionToFloat(0.7152 - 0.7152 * oneMinusAmount));
120 inputParameters.append(narrowPrecisionToFloat(0.0722 + 0.9278 * oneMinusAmount));
121 endMatrixRow(inputParameters);
123 lastMatrixRow(inputParameters);
125 effect = FEColorMatrix::create(this, FECOLORMATRIX_TYPE_MATRIX, inputParameters);
128 case FilterOperation::SEPIA: {
129 Vector<float> inputParameters;
130 double oneMinusAmount = clampTo(1 - toBasicColorMatrixFilterOperation(filterOperation)->amount(), 0.0, 1.0);
132 // See https://dvcs.w3.org/hg/FXTF/raw-file/tip/filters/index.html#sepiaEquivalent
133 // for information on parameters.
135 inputParameters.append(narrowPrecisionToFloat(0.393 + 0.607 * oneMinusAmount));
136 inputParameters.append(narrowPrecisionToFloat(0.769 - 0.769 * oneMinusAmount));
137 inputParameters.append(narrowPrecisionToFloat(0.189 - 0.189 * oneMinusAmount));
138 endMatrixRow(inputParameters);
140 inputParameters.append(narrowPrecisionToFloat(0.349 - 0.349 * oneMinusAmount));
141 inputParameters.append(narrowPrecisionToFloat(0.686 + 0.314 * oneMinusAmount));
142 inputParameters.append(narrowPrecisionToFloat(0.168 - 0.168 * oneMinusAmount));
143 endMatrixRow(inputParameters);
145 inputParameters.append(narrowPrecisionToFloat(0.272 - 0.272 * oneMinusAmount));
146 inputParameters.append(narrowPrecisionToFloat(0.534 - 0.534 * oneMinusAmount));
147 inputParameters.append(narrowPrecisionToFloat(0.131 + 0.869 * oneMinusAmount));
148 endMatrixRow(inputParameters);
150 lastMatrixRow(inputParameters);
152 effect = FEColorMatrix::create(this, FECOLORMATRIX_TYPE_MATRIX, inputParameters);
155 case FilterOperation::SATURATE: {
156 Vector<float> inputParameters;
157 inputParameters.append(narrowPrecisionToFloat(toBasicColorMatrixFilterOperation(filterOperation)->amount()));
158 effect = FEColorMatrix::create(this, FECOLORMATRIX_TYPE_SATURATE, inputParameters);
161 case FilterOperation::HUE_ROTATE: {
162 Vector<float> inputParameters;
163 inputParameters.append(narrowPrecisionToFloat(toBasicColorMatrixFilterOperation(filterOperation)->amount()));
164 effect = FEColorMatrix::create(this, FECOLORMATRIX_TYPE_HUEROTATE, inputParameters);
167 case FilterOperation::INVERT: {
168 BasicComponentTransferFilterOperation* componentTransferOperation = toBasicComponentTransferFilterOperation(filterOperation);
169 ComponentTransferFunction transferFunction;
170 transferFunction.type = FECOMPONENTTRANSFER_TYPE_TABLE;
171 Vector<float> transferParameters;
172 transferParameters.append(narrowPrecisionToFloat(componentTransferOperation->amount()));
173 transferParameters.append(narrowPrecisionToFloat(1 - componentTransferOperation->amount()));
174 transferFunction.tableValues = transferParameters;
176 ComponentTransferFunction nullFunction;
177 effect = FEComponentTransfer::create(this, transferFunction, transferFunction, transferFunction, nullFunction);
180 case FilterOperation::OPACITY: {
181 ComponentTransferFunction transferFunction;
182 transferFunction.type = FECOMPONENTTRANSFER_TYPE_TABLE;
183 Vector<float> transferParameters;
184 transferParameters.append(0);
185 transferParameters.append(narrowPrecisionToFloat(toBasicComponentTransferFilterOperation(filterOperation)->amount()));
186 transferFunction.tableValues = transferParameters;
188 ComponentTransferFunction nullFunction;
189 effect = FEComponentTransfer::create(this, nullFunction, nullFunction, nullFunction, transferFunction);
192 case FilterOperation::BRIGHTNESS: {
193 ComponentTransferFunction transferFunction;
194 transferFunction.type = FECOMPONENTTRANSFER_TYPE_LINEAR;
195 transferFunction.slope = narrowPrecisionToFloat(toBasicComponentTransferFilterOperation(filterOperation)->amount());
196 transferFunction.intercept = 0;
198 ComponentTransferFunction nullFunction;
199 effect = FEComponentTransfer::create(this, transferFunction, transferFunction, transferFunction, nullFunction);
202 case FilterOperation::CONTRAST: {
203 ComponentTransferFunction transferFunction;
204 transferFunction.type = FECOMPONENTTRANSFER_TYPE_LINEAR;
205 float amount = narrowPrecisionToFloat(toBasicComponentTransferFilterOperation(filterOperation)->amount());
206 transferFunction.slope = amount;
207 transferFunction.intercept = -0.5 * amount + 0.5;
209 ComponentTransferFunction nullFunction;
210 effect = FEComponentTransfer::create(this, transferFunction, transferFunction, transferFunction, nullFunction);
213 case FilterOperation::BLUR: {
214 float stdDeviation = floatValueForLength(toBlurFilterOperation(filterOperation)->stdDeviation(), 0) * invZoom;
215 effect = FEGaussianBlur::create(this, stdDeviation, stdDeviation);
218 case FilterOperation::DROP_SHADOW: {
219 DropShadowFilterOperation* dropShadowOperation = toDropShadowFilterOperation(filterOperation);
220 float stdDeviation = dropShadowOperation->stdDeviation() * invZoom;
221 float x = dropShadowOperation->x() * invZoom;
222 float y = dropShadowOperation->y() * invZoom;
223 effect = FEDropShadow::create(this, stdDeviation, stdDeviation, x, y, dropShadowOperation->color(), 1);
231 if (filterOperation->type() != FilterOperation::REFERENCE) {
232 // Unlike SVG, filters applied here should not clip to their primitive subregions.
233 effect->setClipsToBounds(false);
234 effect->setOperatingColorSpace(ColorSpaceDeviceRGB);
235 effect->inputEffects().append(previousEffect);
237 previousEffect = effect.release();
241 // We need to keep the old effects alive until this point, so that SVG reference filters
242 // can share cached resources across frames.
243 m_lastEffect = previousEffect;
245 // If we didn't make any effects, tell our caller we are not valid
246 if (!m_lastEffect.get())
252 bool FilterEffectRenderer::updateBackingStoreRect(const FloatRect& floatFilterRect)
254 IntRect filterRect = enclosingIntRect(floatFilterRect);
255 if (!filterRect.isEmpty() && FilterEffect::isFilterSizeValid(filterRect)) {
256 FloatRect currentSourceRect = sourceImageRect();
257 if (filterRect != currentSourceRect) {
258 setSourceImageRect(filterRect);
265 void FilterEffectRenderer::allocateBackingStoreIfNeeded()
267 // At this point the effect chain has been built, and the
268 // source image sizes set. We just need to attach the graphic
269 // buffer if we have not yet done so.
270 if (!m_graphicsBufferAttached) {
271 IntSize logicalSize(m_sourceDrawingRegion.width(), m_sourceDrawingRegion.height());
272 if (!sourceImage() || sourceImage()->size() != logicalSize) {
273 OwnPtr<ImageBufferSurface> surface = adoptPtr(new UnacceleratedImageBufferSurface(logicalSize));
274 setSourceImage(ImageBuffer::create(surface.release()));
276 m_graphicsBufferAttached = true;
280 void FilterEffectRenderer::clearIntermediateResults()
282 if (m_lastEffect.get())
283 m_lastEffect->clearResultsRecursive();
286 void FilterEffectRenderer::apply()
288 RefPtr<FilterEffect> effect = lastEffect();
290 effect->transformResultColorSpace(ColorSpaceDeviceRGB);
293 LayoutRect FilterEffectRenderer::computeSourceImageRectForDirtyRect(const LayoutRect& filterBoxRect, const LayoutRect& dirtyRect)
295 // The result of this function is the area in the "filterBoxRect" that needs to be repainted, so that we fully cover the "dirtyRect".
296 FloatRect rectForRepaint = dirtyRect;
297 float inf = std::numeric_limits<float>::infinity();
298 FloatRect clipRect = FloatRect(FloatPoint(-inf, -inf), FloatSize(inf, inf));
299 rectForRepaint = lastEffect()->getSourceRect(rectForRepaint, clipRect);
300 rectForRepaint.intersect(filterBoxRect);
301 return LayoutRect(rectForRepaint);
304 bool FilterEffectRendererHelper::prepareFilterEffect(RenderLayer* renderLayer, const LayoutRect& filterBoxRect, const LayoutRect& dirtyRect)
306 ASSERT(m_haveFilterEffect && renderLayer->filterRenderer());
307 m_renderLayer = renderLayer;
308 m_repaintRect = dirtyRect;
310 // Get the zoom factor to scale the filterSourceRect input
311 const RenderLayerModelObject* renderer = renderLayer->renderer();
312 const RenderStyle* style = renderer ? renderer->style() : 0;
313 float zoom = style ? style->effectiveZoom() : 1.0f;
315 // Prepare a transformation that brings the coordinates into the space
316 // filter coordinates are defined in.
317 AffineTransform absoluteTransform;
318 // FIXME: Should these really be upconverted to doubles and not rounded? crbug.com/350474
319 absoluteTransform.translate(filterBoxRect.x().toDouble(), filterBoxRect.y().toDouble());
320 absoluteTransform.scale(zoom, zoom);
322 FilterEffectRenderer* filter = renderLayer->filterRenderer();
323 filter->setAbsoluteTransform(absoluteTransform);
325 IntRect filterSourceRect = pixelSnappedIntRect(filter->computeSourceImageRectForDirtyRect(filterBoxRect, dirtyRect));
327 if (filterSourceRect.isEmpty()) {
328 // The dirty rect is not in view, just bail out.
329 m_haveFilterEffect = false;
333 filter->setFilterRegion(filter->mapAbsoluteRectToLocalRect(filterSourceRect));
334 filter->lastEffect()->determineFilterPrimitiveSubregion(MapRectForward);
336 bool hasUpdatedBackingStore = filter->updateBackingStoreRect(filterSourceRect);
337 if (filter->hasFilterThatMovesPixels()) {
338 if (hasUpdatedBackingStore)
339 m_repaintRect = filterSourceRect;
341 m_repaintRect.intersect(filterSourceRect);
346 GraphicsContext* FilterEffectRendererHelper::beginFilterEffect(GraphicsContext* oldContext)
348 ASSERT(m_renderLayer);
350 FilterEffectRenderer* filter = m_renderLayer->filterRenderer();
351 filter->allocateBackingStoreIfNeeded();
352 // Paint into the context that represents the SourceGraphic of the filter.
353 GraphicsContext* sourceGraphicsContext = filter->inputContext();
354 if (!sourceGraphicsContext || !FilterEffect::isFilterSizeValid(filter->absoluteFilterRegion())) {
355 // Disable the filters and continue.
356 m_haveFilterEffect = false;
360 m_savedGraphicsContext = oldContext;
362 // Translate the context so that the contents of the layer is captuterd in the offscreen memory buffer.
363 sourceGraphicsContext->save();
364 // FIXME: can we just use sourceImageRect for everything, and get rid of
366 FloatPoint offset = filter->sourceImageRect().location();
367 sourceGraphicsContext->translate(-offset.x(), -offset.y());
368 sourceGraphicsContext->clearRect(m_repaintRect);
369 sourceGraphicsContext->clip(m_repaintRect);
371 return sourceGraphicsContext;
374 GraphicsContext* FilterEffectRendererHelper::applyFilterEffect()
376 ASSERT(m_haveFilterEffect && m_renderLayer->filterRenderer());
377 FilterEffectRenderer* filter = m_renderLayer->filterRenderer();
378 filter->inputContext()->restore();
382 // Get the filtered output and draw it in place.
383 m_savedGraphicsContext->drawImageBuffer(filter->output(), filter->outputRect());
385 filter->clearIntermediateResults();
387 return m_savedGraphicsContext;