Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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 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.
25  */
26
27 #include "config.h"
28
29 #include "core/rendering/FilterEffectRenderer.h"
30
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"
48 #include <algorithm>
49
50 namespace blink {
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 FilterEffectRenderer::FilterEffectRenderer()
68     : Filter(AffineTransform())
69     , m_graphicsBufferAttached(false)
70     , m_hasFilterThatMovesPixels(false)
71 {
72     m_sourceGraphic = SourceGraphic::create(this);
73 }
74
75 FilterEffectRenderer::~FilterEffectRenderer()
76 {
77 }
78
79 GraphicsContext* FilterEffectRenderer::inputContext()
80 {
81     return sourceImage() ? sourceImage()->context() : 0;
82 }
83
84 bool FilterEffectRenderer::build(RenderObject* renderer, const FilterOperations& operations)
85 {
86     m_hasFilterThatMovesPixels = operations.hasFilterThatMovesPixels();
87
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;
91
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));
99             break;
100         }
101         case FilterOperation::GRAYSCALE: {
102             Vector<float> inputParameters;
103             double oneMinusAmount = clampTo(1 - toBasicColorMatrixFilterOperation(filterOperation)->amount(), 0.0, 1.0);
104
105             // See https://dvcs.w3.org/hg/FXTF/raw-file/tip/filters/index.html#grayscaleEquivalent
106             // for information on parameters.
107
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);
112
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);
117
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);
122
123             lastMatrixRow(inputParameters);
124
125             effect = FEColorMatrix::create(this, FECOLORMATRIX_TYPE_MATRIX, inputParameters);
126             break;
127         }
128         case FilterOperation::SEPIA: {
129             Vector<float> inputParameters;
130             double oneMinusAmount = clampTo(1 - toBasicColorMatrixFilterOperation(filterOperation)->amount(), 0.0, 1.0);
131
132             // See https://dvcs.w3.org/hg/FXTF/raw-file/tip/filters/index.html#sepiaEquivalent
133             // for information on parameters.
134
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);
139
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);
144
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);
149
150             lastMatrixRow(inputParameters);
151
152             effect = FEColorMatrix::create(this, FECOLORMATRIX_TYPE_MATRIX, inputParameters);
153             break;
154         }
155         case FilterOperation::SATURATE: {
156             Vector<float> inputParameters;
157             inputParameters.append(narrowPrecisionToFloat(toBasicColorMatrixFilterOperation(filterOperation)->amount()));
158             effect = FEColorMatrix::create(this, FECOLORMATRIX_TYPE_SATURATE, inputParameters);
159             break;
160         }
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);
165             break;
166         }
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;
175
176             ComponentTransferFunction nullFunction;
177             effect = FEComponentTransfer::create(this, transferFunction, transferFunction, transferFunction, nullFunction);
178             break;
179         }
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;
187
188             ComponentTransferFunction nullFunction;
189             effect = FEComponentTransfer::create(this, nullFunction, nullFunction, nullFunction, transferFunction);
190             break;
191         }
192         case FilterOperation::BRIGHTNESS: {
193             ComponentTransferFunction transferFunction;
194             transferFunction.type = FECOMPONENTTRANSFER_TYPE_LINEAR;
195             transferFunction.slope = narrowPrecisionToFloat(toBasicComponentTransferFilterOperation(filterOperation)->amount());
196             transferFunction.intercept = 0;
197
198             ComponentTransferFunction nullFunction;
199             effect = FEComponentTransfer::create(this, transferFunction, transferFunction, transferFunction, nullFunction);
200             break;
201         }
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;
208
209             ComponentTransferFunction nullFunction;
210             effect = FEComponentTransfer::create(this, transferFunction, transferFunction, transferFunction, nullFunction);
211             break;
212         }
213         case FilterOperation::BLUR: {
214             float stdDeviation = floatValueForLength(toBlurFilterOperation(filterOperation)->stdDeviation(), 0) * invZoom;
215             effect = FEGaussianBlur::create(this, stdDeviation, stdDeviation);
216             break;
217         }
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);
224             break;
225         }
226         default:
227             break;
228         }
229
230         if (effect) {
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);
236             }
237             previousEffect = effect.release();
238         }
239     }
240
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;
244
245     // If we didn't make any effects, tell our caller we are not valid
246     if (!m_lastEffect.get())
247         return false;
248
249     return true;
250 }
251
252 bool FilterEffectRenderer::updateBackingStoreRect(const FloatRect& floatFilterRect)
253 {
254     IntRect filterRect = enclosingIntRect(floatFilterRect);
255     if (!filterRect.isEmpty() && FilterEffect::isFilterSizeValid(filterRect)) {
256         FloatRect currentSourceRect = sourceImageRect();
257         if (filterRect != currentSourceRect) {
258             setSourceImageRect(filterRect);
259             return true;
260         }
261     }
262     return false;
263 }
264
265 void FilterEffectRenderer::allocateBackingStoreIfNeeded()
266 {
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()));
275         }
276         m_graphicsBufferAttached = true;
277     }
278 }
279
280 void FilterEffectRenderer::clearIntermediateResults()
281 {
282     if (m_lastEffect.get())
283         m_lastEffect->clearResultsRecursive();
284 }
285
286 void FilterEffectRenderer::apply()
287 {
288     RefPtr<FilterEffect> effect = lastEffect();
289     effect->apply();
290     effect->transformResultColorSpace(ColorSpaceDeviceRGB);
291 }
292
293 LayoutRect FilterEffectRenderer::computeSourceImageRectForDirtyRect(const LayoutRect& filterBoxRect, const LayoutRect& dirtyRect)
294 {
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);
302 }
303
304 bool FilterEffectRendererHelper::prepareFilterEffect(RenderLayer* renderLayer, const LayoutRect& filterBoxRect, const LayoutRect& dirtyRect)
305 {
306     ASSERT(m_haveFilterEffect && renderLayer->filterRenderer());
307     m_renderLayer = renderLayer;
308     m_repaintRect = dirtyRect;
309
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;
314
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);
321
322     FilterEffectRenderer* filter = renderLayer->filterRenderer();
323     filter->setAbsoluteTransform(absoluteTransform);
324
325     IntRect filterSourceRect = pixelSnappedIntRect(filter->computeSourceImageRectForDirtyRect(filterBoxRect, dirtyRect));
326
327     if (filterSourceRect.isEmpty()) {
328         // The dirty rect is not in view, just bail out.
329         m_haveFilterEffect = false;
330         return false;
331     }
332
333     filter->setFilterRegion(filter->mapAbsoluteRectToLocalRect(filterSourceRect));
334     filter->lastEffect()->determineFilterPrimitiveSubregion(MapRectForward);
335
336     bool hasUpdatedBackingStore = filter->updateBackingStoreRect(filterSourceRect);
337     if (filter->hasFilterThatMovesPixels()) {
338         if (hasUpdatedBackingStore)
339             m_repaintRect = filterSourceRect;
340         else
341             m_repaintRect.intersect(filterSourceRect);
342     }
343     return true;
344 }
345
346 GraphicsContext* FilterEffectRendererHelper::beginFilterEffect(GraphicsContext* oldContext)
347 {
348     ASSERT(m_renderLayer);
349
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;
357         return oldContext;
358     }
359
360     m_savedGraphicsContext = oldContext;
361
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
365     // m_repaintRect?
366     FloatPoint offset = filter->sourceImageRect().location();
367     sourceGraphicsContext->translate(-offset.x(), -offset.y());
368     sourceGraphicsContext->clearRect(m_repaintRect);
369     sourceGraphicsContext->clip(m_repaintRect);
370
371     return sourceGraphicsContext;
372 }
373
374 GraphicsContext* FilterEffectRendererHelper::applyFilterEffect()
375 {
376     ASSERT(m_haveFilterEffect && m_renderLayer->filterRenderer());
377     FilterEffectRenderer* filter = m_renderLayer->filterRenderer();
378     filter->inputContext()->restore();
379
380     filter->apply();
381
382     // Get the filtered output and draw it in place.
383     m_savedGraphicsContext->drawImageBuffer(filter->output(), filter->outputRect());
384
385     filter->clearIntermediateResults();
386
387     return m_savedGraphicsContext;
388 }
389
390 } // namespace blink
391