Iterating a HashMap<String, X> involves a string equality comparison to check for...
[WebKit-https.git] / Source / WebCore / rendering / FilterEffectRenderer.cpp
1 /*
2  * Copyright (C) 2011 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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 COMPUTER, 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. 
24  */
25
26 #include "config.h"
27
28 #if ENABLE(CSS_FILTERS)
29
30 #include "FilterEffectRenderer.h"
31
32 #include "Document.h"
33 #include "FEColorMatrix.h"
34 #include "FEComponentTransfer.h"
35 #include "FEDropShadow.h"
36 #include "FEGaussianBlur.h"
37 #include "FEMerge.h"
38 #include "FilterEffectObserver.h"
39 #include "FloatConversion.h"
40 #include "RenderLayer.h"
41
42 #include <algorithm>
43 #include <wtf/MathExtras.h>
44
45 #if ENABLE(CSS_SHADERS) && ENABLE(WEBGL)
46 #include "CustomFilterProgram.h"
47 #include "CustomFilterOperation.h"
48 #include "FECustomFilter.h"
49 #include "FrameView.h"
50 #include "Settings.h"
51 #endif
52
53 namespace WebCore {
54
55 static inline void endMatrixRow(Vector<float>& parameters)
56 {
57     parameters.append(0);
58     parameters.append(0);
59 }
60
61 static inline void lastMatrixRow(Vector<float>& parameters)
62 {
63     parameters.append(0);
64     parameters.append(0);
65     parameters.append(0);
66     parameters.append(1);
67     parameters.append(0);
68 }
69
70 inline bool isFilterSizeValid(FloatRect rect)
71 {
72     if (rect.width() < 0 || rect.width() > kMaxFilterSize
73         || rect.height() < 0 || rect.height() > kMaxFilterSize)
74         return false;
75     return true;
76 }
77
78 #if ENABLE(CSS_SHADERS) && ENABLE(WEBGL)
79 static bool isCSSCustomFilterEnabled(Document* document)
80 {
81     // We only want to enable shaders if WebGL is also enabled on this platform.
82     Settings* settings = document->settings();
83     return settings && settings->isCSSCustomFilterEnabled() && settings->webGLEnabled();
84 }
85 #endif
86
87 FilterEffectRenderer::FilterEffectRenderer(FilterEffectObserver* observer)
88     : m_observer(observer)
89     , m_topOutset(0)
90     , m_rightOutset(0)
91     , m_bottomOutset(0)
92     , m_leftOutset(0)
93     , m_graphicsBufferAttached(false)
94     , m_hasFilterThatMovesPixels(false)
95 {
96     setFilterResolution(FloatSize(1, 1));
97     m_sourceGraphic = SourceGraphic::create(this);
98 }
99
100 FilterEffectRenderer::~FilterEffectRenderer()
101 {
102 #if ENABLE(CSS_SHADERS)
103     removeCustomFilterClients();
104 #endif
105 }
106
107 GraphicsContext* FilterEffectRenderer::inputContext()
108 {
109     return sourceImage() ? sourceImage()->context() : 0;
110 }
111
112 bool FilterEffectRenderer::build(Document* document, const FilterOperations& operations)
113 {
114 #if !ENABLE(CSS_SHADERS) || !ENABLE(WEBGL)
115     UNUSED_PARAM(document);
116 #else
117     CustomFilterProgramList cachedCustomFilterPrograms;
118 #endif
119
120     m_hasFilterThatMovesPixels = operations.hasFilterThatMovesPixels();
121     if (m_hasFilterThatMovesPixels)
122         operations.getOutsets(m_topOutset, m_rightOutset, m_bottomOutset, m_leftOutset);
123     m_effects.clear();
124
125     RefPtr<FilterEffect> previousEffect;
126     for (size_t i = 0; i < operations.operations().size(); ++i) {
127         RefPtr<FilterEffect> effect;
128         FilterOperation* filterOperation = operations.operations().at(i).get();
129         switch (filterOperation->getOperationType()) {
130         case FilterOperation::REFERENCE: {
131             // FIXME: Not yet implemented.
132             // https://bugs.webkit.org/show_bug.cgi?id=72443
133             break;
134         }
135         case FilterOperation::GRAYSCALE: {
136             BasicColorMatrixFilterOperation* colorMatrixOperation = static_cast<BasicColorMatrixFilterOperation*>(filterOperation);
137             Vector<float> inputParameters;
138             double oneMinusAmount = clampTo(1 - colorMatrixOperation->amount(), 0.0, 1.0);
139
140             // See https://dvcs.w3.org/hg/FXTF/raw-file/tip/filters/index.html#grayscaleEquivalent
141             // for information on parameters.
142
143             inputParameters.append(narrowPrecisionToFloat(0.2126 + 0.7874 * oneMinusAmount));
144             inputParameters.append(narrowPrecisionToFloat(0.7152 - 0.7152 * oneMinusAmount));
145             inputParameters.append(narrowPrecisionToFloat(0.0722 - 0.0722 * oneMinusAmount));
146             endMatrixRow(inputParameters);
147
148             inputParameters.append(narrowPrecisionToFloat(0.2126 - 0.2126 * oneMinusAmount));
149             inputParameters.append(narrowPrecisionToFloat(0.7152 + 0.2848 * oneMinusAmount));
150             inputParameters.append(narrowPrecisionToFloat(0.0722 - 0.0722 * oneMinusAmount));
151             endMatrixRow(inputParameters);
152
153             inputParameters.append(narrowPrecisionToFloat(0.2126 - 0.2126 * oneMinusAmount));
154             inputParameters.append(narrowPrecisionToFloat(0.7152 - 0.7152 * oneMinusAmount));
155             inputParameters.append(narrowPrecisionToFloat(0.0722 + 0.9278 * oneMinusAmount));
156             endMatrixRow(inputParameters);
157
158             lastMatrixRow(inputParameters);
159
160             effect = FEColorMatrix::create(this, FECOLORMATRIX_TYPE_MATRIX, inputParameters);
161             break;
162         }
163         case FilterOperation::SEPIA: {
164             BasicColorMatrixFilterOperation* colorMatrixOperation = static_cast<BasicColorMatrixFilterOperation*>(filterOperation);
165             Vector<float> inputParameters;
166             double oneMinusAmount = clampTo(1 - colorMatrixOperation->amount(), 0.0, 1.0);
167
168             // See https://dvcs.w3.org/hg/FXTF/raw-file/tip/filters/index.html#sepiaEquivalent
169             // for information on parameters.
170
171             inputParameters.append(narrowPrecisionToFloat(0.393 + 0.607 * oneMinusAmount));
172             inputParameters.append(narrowPrecisionToFloat(0.769 - 0.769 * oneMinusAmount));
173             inputParameters.append(narrowPrecisionToFloat(0.189 - 0.189 * oneMinusAmount));
174             endMatrixRow(inputParameters);
175
176             inputParameters.append(narrowPrecisionToFloat(0.349 - 0.349 * oneMinusAmount));
177             inputParameters.append(narrowPrecisionToFloat(0.686 + 0.314 * oneMinusAmount));
178             inputParameters.append(narrowPrecisionToFloat(0.168 - 0.168 * oneMinusAmount));
179             endMatrixRow(inputParameters);
180
181             inputParameters.append(narrowPrecisionToFloat(0.272 - 0.272 * oneMinusAmount));
182             inputParameters.append(narrowPrecisionToFloat(0.534 - 0.534 * oneMinusAmount));
183             inputParameters.append(narrowPrecisionToFloat(0.131 + 0.869 * oneMinusAmount));
184             endMatrixRow(inputParameters);
185
186             lastMatrixRow(inputParameters);
187
188             effect = FEColorMatrix::create(this, FECOLORMATRIX_TYPE_MATRIX, inputParameters);
189             break;
190         }
191         case FilterOperation::SATURATE: {
192             BasicColorMatrixFilterOperation* colorMatrixOperation = static_cast<BasicColorMatrixFilterOperation*>(filterOperation);
193             Vector<float> inputParameters;
194             inputParameters.append(narrowPrecisionToFloat(colorMatrixOperation->amount()));
195             effect = FEColorMatrix::create(this, FECOLORMATRIX_TYPE_SATURATE, inputParameters);
196             break;
197         }
198         case FilterOperation::HUE_ROTATE: {
199             BasicColorMatrixFilterOperation* colorMatrixOperation = static_cast<BasicColorMatrixFilterOperation*>(filterOperation);
200             Vector<float> inputParameters;
201             inputParameters.append(narrowPrecisionToFloat(colorMatrixOperation->amount()));
202             effect = FEColorMatrix::create(this, FECOLORMATRIX_TYPE_HUEROTATE, inputParameters);
203             break;
204         }
205         case FilterOperation::INVERT: {
206             BasicComponentTransferFilterOperation* componentTransferOperation = static_cast<BasicComponentTransferFilterOperation*>(filterOperation);
207             ComponentTransferFunction transferFunction;
208             transferFunction.type = FECOMPONENTTRANSFER_TYPE_TABLE;
209             Vector<float> transferParameters;
210             transferParameters.append(narrowPrecisionToFloat(componentTransferOperation->amount()));
211             transferParameters.append(narrowPrecisionToFloat(1 - componentTransferOperation->amount()));
212             transferFunction.tableValues = transferParameters;
213
214             ComponentTransferFunction nullFunction;
215             effect = FEComponentTransfer::create(this, transferFunction, transferFunction, transferFunction, nullFunction);
216             break;
217         }
218         case FilterOperation::OPACITY: {
219             BasicComponentTransferFilterOperation* componentTransferOperation = static_cast<BasicComponentTransferFilterOperation*>(filterOperation);
220             ComponentTransferFunction transferFunction;
221             transferFunction.type = FECOMPONENTTRANSFER_TYPE_TABLE;
222             Vector<float> transferParameters;
223             transferParameters.append(0);
224             transferParameters.append(narrowPrecisionToFloat(componentTransferOperation->amount()));
225             transferFunction.tableValues = transferParameters;
226
227             ComponentTransferFunction nullFunction;
228             effect = FEComponentTransfer::create(this, nullFunction, nullFunction, nullFunction, transferFunction);
229             break;
230         }
231         case FilterOperation::BRIGHTNESS: {
232             BasicComponentTransferFilterOperation* componentTransferOperation = static_cast<BasicComponentTransferFilterOperation*>(filterOperation);
233             ComponentTransferFunction transferFunction;
234             transferFunction.type = FECOMPONENTTRANSFER_TYPE_LINEAR;
235             transferFunction.slope = 1;
236             transferFunction.intercept = narrowPrecisionToFloat(componentTransferOperation->amount());
237
238             ComponentTransferFunction nullFunction;
239             effect = FEComponentTransfer::create(this, transferFunction, transferFunction, transferFunction, nullFunction);
240             break;
241         }
242         case FilterOperation::CONTRAST: {
243             BasicComponentTransferFilterOperation* componentTransferOperation = static_cast<BasicComponentTransferFilterOperation*>(filterOperation);
244             ComponentTransferFunction transferFunction;
245             transferFunction.type = FECOMPONENTTRANSFER_TYPE_LINEAR;
246             float amount = narrowPrecisionToFloat(componentTransferOperation->amount());
247             transferFunction.slope = amount;
248             transferFunction.intercept = -0.5 * amount + 0.5;
249             
250             ComponentTransferFunction nullFunction;
251             effect = FEComponentTransfer::create(this, transferFunction, transferFunction, transferFunction, nullFunction);
252             break;
253         }
254         case FilterOperation::BLUR: {
255             BlurFilterOperation* blurOperation = static_cast<BlurFilterOperation*>(filterOperation);
256             float stdDeviation = floatValueForLength(blurOperation->stdDeviation(), 0);
257             effect = FEGaussianBlur::create(this, stdDeviation, stdDeviation);
258             break;
259         }
260         case FilterOperation::DROP_SHADOW: {
261             DropShadowFilterOperation* dropShadowOperation = static_cast<DropShadowFilterOperation*>(filterOperation);
262             effect = FEDropShadow::create(this, dropShadowOperation->stdDeviation(), dropShadowOperation->stdDeviation(),
263                                                 dropShadowOperation->x(), dropShadowOperation->y(), dropShadowOperation->color(), 1);
264             break;
265         }
266 #if ENABLE(CSS_SHADERS)
267         case FilterOperation::CUSTOM: {
268 #if ENABLE(WEBGL)
269             if (!isCSSCustomFilterEnabled(document))
270                 continue;
271             
272             CustomFilterOperation* customFilterOperation = static_cast<CustomFilterOperation*>(filterOperation);
273             RefPtr<CustomFilterProgram> program = customFilterOperation->program();
274             cachedCustomFilterPrograms.append(program);
275             program->addClient(this);
276             if (program->isLoaded()) {
277                 effect = FECustomFilter::create(this, document->view()->root()->hostWindow(), program, customFilterOperation->parameters(),
278                                                 customFilterOperation->meshRows(), customFilterOperation->meshColumns(),
279                                                 customFilterOperation->meshBoxType(), customFilterOperation->meshType());
280             }
281 #endif
282             break;
283         }
284 #endif
285         default:
286             break;
287         }
288
289         if (effect) {
290             // Unlike SVG, filters applied here should not clip to their primitive subregions.
291             effect->setClipsToBounds(false);
292             
293             if (previousEffect)
294                 effect->inputEffects().append(previousEffect);
295             m_effects.append(effect);
296             previousEffect = effect.release();
297         }
298     }
299
300 #if ENABLE(CSS_SHADERS) && ENABLE(WEBGL)
301     removeCustomFilterClients();
302     m_cachedCustomFilterPrograms.swap(cachedCustomFilterPrograms);
303 #endif
304
305     // If we didn't make any effects, tell our caller we are not valid
306     if (!previousEffect)
307         return false;
308
309     m_effects.first()->inputEffects().append(m_sourceGraphic);
310     setMaxEffectRects(m_sourceDrawingRegion);
311     
312     return true;
313 }
314
315 bool FilterEffectRenderer::updateBackingStoreRect(const FloatRect& filterRect)
316 {
317     if (!filterRect.isZero() && isFilterSizeValid(filterRect)) {
318         FloatRect currentSourceRect = sourceImageRect();
319         if (filterRect != currentSourceRect) {
320             setSourceImageRect(filterRect);
321             return true;
322         }
323     }
324     return false;
325 }
326
327 #if ENABLE(CSS_SHADERS)
328 void FilterEffectRenderer::notifyCustomFilterProgramLoaded(CustomFilterProgram*)
329 {
330     m_observer->filterNeedsRepaint();
331 }
332
333 void FilterEffectRenderer::removeCustomFilterClients()
334 {
335     for (CustomFilterProgramList::iterator iter = m_cachedCustomFilterPrograms.begin(), end = m_cachedCustomFilterPrograms.end(); iter != end; ++iter)
336         iter->get()->removeClient(this);
337 }
338 #endif
339
340 void FilterEffectRenderer::allocateBackingStoreIfNeeded()
341 {
342     // At this point the effect chain has been built, and the
343     // source image sizes set. We just need to attach the graphic
344     // buffer if we have not yet done so.
345     if (!m_graphicsBufferAttached) {
346         IntSize logicalSize(m_sourceDrawingRegion.width(), m_sourceDrawingRegion.height());
347         if (!sourceImage() || sourceImage()->logicalSize() != logicalSize)
348             setSourceImage(ImageBuffer::create(logicalSize, 1, ColorSpaceDeviceRGB, renderingMode()));
349         m_graphicsBufferAttached = true;
350     }
351 }
352
353 void FilterEffectRenderer::clearIntermediateResults()
354 {
355     m_sourceGraphic->clearResult();
356     for (size_t i = 0; i < m_effects.size(); ++i)
357         m_effects[i]->clearResult();
358 }
359
360 void FilterEffectRenderer::apply()
361 {
362     lastEffect()->apply();
363 }
364
365 LayoutRect FilterEffectRenderer::computeSourceImageRectForDirtyRect(const LayoutRect& filterBoxRect, const LayoutRect& dirtyRect)
366 {
367     // The result of this function is the area in the "filterBoxRect" that needs to be repainted, so that we fully cover the "dirtyRect".
368     LayoutRect rectForRepaint = dirtyRect;
369     if (hasFilterThatMovesPixels()) {
370         // Note that the outsets are reversed here because we are going backwards -> we have the dirty rect and
371         // need to find out what is the rectangle that might influence the result inside that dirty rect.
372         rectForRepaint.move(-m_rightOutset, -m_bottomOutset);
373         rectForRepaint.expand(m_leftOutset + m_rightOutset, m_topOutset + m_bottomOutset);
374     }
375     rectForRepaint.intersect(filterBoxRect);
376     return rectForRepaint;
377 }
378
379 bool FilterEffectRendererHelper::prepareFilterEffect(RenderLayer* renderLayer, const LayoutRect& filterBoxRect, const LayoutRect& dirtyRect, const LayoutRect& layerRepaintRect)
380 {
381     ASSERT(m_haveFilterEffect && renderLayer->filter());
382     m_renderLayer = renderLayer;
383     m_repaintRect = dirtyRect;
384
385     FilterEffectRenderer* filter = renderLayer->filter();
386     LayoutRect filterSourceRect = filter->computeSourceImageRectForDirtyRect(filterBoxRect, dirtyRect);
387     m_paintOffset = filterSourceRect.location();
388
389     if (filterSourceRect.isEmpty()) {
390         // The dirty rect is not in view, just bail out.
391         m_haveFilterEffect = false;
392         return false;
393     }
394     
395     bool hasUpdatedBackingStore = filter->updateBackingStoreRect(filterSourceRect);
396     if (filter->hasFilterThatMovesPixels()) {
397         if (hasUpdatedBackingStore)
398             m_repaintRect = filterSourceRect;
399         else {
400             m_repaintRect.unite(layerRepaintRect);
401             m_repaintRect.intersect(filterSourceRect);
402         }
403     }
404     return true;
405 }
406    
407 GraphicsContext* FilterEffectRendererHelper::beginFilterEffect(GraphicsContext* oldContext)
408 {
409     ASSERT(m_renderLayer);
410     
411     FilterEffectRenderer* filter = m_renderLayer->filter();
412     filter->allocateBackingStoreIfNeeded();
413     // Paint into the context that represents the SourceGraphic of the filter.
414     GraphicsContext* sourceGraphicsContext = filter->inputContext();
415     if (!sourceGraphicsContext || !isFilterSizeValid(filter->filterRegion())) {
416         // Disable the filters and continue.
417         m_haveFilterEffect = false;
418         return oldContext;
419     }
420     
421     m_savedGraphicsContext = oldContext;
422     
423     // Translate the context so that the contents of the layer is captuterd in the offscreen memory buffer.
424     sourceGraphicsContext->save();
425     sourceGraphicsContext->translate(-m_paintOffset.x(), -m_paintOffset.y());
426     sourceGraphicsContext->clearRect(m_repaintRect);
427     sourceGraphicsContext->clip(m_repaintRect);
428     
429     return sourceGraphicsContext;
430 }
431
432 GraphicsContext* FilterEffectRendererHelper::applyFilterEffect()
433 {
434     ASSERT(m_haveFilterEffect && m_renderLayer->filter());
435     FilterEffectRenderer* filter = m_renderLayer->filter();
436     filter->inputContext()->restore();
437
438     filter->apply();
439     
440     // Get the filtered output and draw it in place.
441     LayoutRect destRect = filter->outputRect();
442     destRect.move(m_paintOffset.x(), m_paintOffset.y());
443     
444     m_savedGraphicsContext->drawImageBuffer(filter->output(), m_renderLayer->renderer()->style()->colorSpace(), pixelSnappedIntRect(destRect), CompositeSourceOver);
445     
446     filter->clearIntermediateResults();
447     
448     return m_savedGraphicsContext;
449 }
450
451 } // namespace WebCore
452
453 #endif // ENABLE(CSS_FILTERS)