forked from qt/qtwebkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilterEffectRenderer.cpp
More file actions
438 lines (371 loc) · 18.5 KB
/
Copy pathFilterEffectRenderer.cpp
File metadata and controls
438 lines (371 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
* Copyright (C) 2011 Apple Inc. All rights reserved.
* Copyright (C) 2013 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "FilterEffectRenderer.h"
#include "CachedSVGDocument.h"
#include "CachedSVGDocumentReference.h"
#include "ColorSpace.h"
#include "Document.h"
#include "ElementIterator.h"
#include "FEColorMatrix.h"
#include "FEComponentTransfer.h"
#include "FEDropShadow.h"
#include "FEGaussianBlur.h"
#include "FEMerge.h"
#include "FloatConversion.h"
#include "Frame.h"
#include "RenderLayer.h"
#include "SVGElement.h"
#include "SVGFilterPrimitiveStandardAttributes.h"
#include "Settings.h"
#include <algorithm>
#include <wtf/MathExtras.h>
namespace WebCore {
static inline void endMatrixRow(Vector<float>& parameters)
{
parameters.append(0);
parameters.append(0);
}
static inline void lastMatrixRow(Vector<float>& parameters)
{
parameters.append(0);
parameters.append(0);
parameters.append(0);
parameters.append(1);
parameters.append(0);
}
FilterEffectRenderer::FilterEffectRenderer()
: Filter(AffineTransform())
, m_graphicsBufferAttached(false)
, m_hasFilterThatMovesPixels(false)
{
setFilterResolution(FloatSize(1, 1));
m_sourceGraphic = SourceGraphic::create(*this);
}
FilterEffectRenderer::~FilterEffectRenderer()
{
}
GraphicsContext* FilterEffectRenderer::inputContext()
{
return sourceImage() ? &sourceImage()->context() : nullptr;
}
PassRefPtr<FilterEffect> FilterEffectRenderer::buildReferenceFilter(RenderElement* renderer, PassRefPtr<FilterEffect> previousEffect, ReferenceFilterOperation* filterOperation)
{
if (!renderer)
return nullptr;
Document* document = &renderer->document();
CachedSVGDocumentReference* cachedSVGDocumentReference = filterOperation->cachedSVGDocumentReference();
CachedSVGDocument* cachedSVGDocument = cachedSVGDocumentReference ? cachedSVGDocumentReference->document() : 0;
// If we have an SVG document, this is an external reference. Otherwise
// we look up the referenced node in the current document.
if (cachedSVGDocument)
document = cachedSVGDocument->document();
if (!document)
return nullptr;
Element* filter = document->getElementById(filterOperation->fragment());
if (!filter) {
// Although we did not find the referenced filter, it might exist later in the document.
// FIXME: This skips anonymous RenderObjects. <https://webkit.org/b/131085>
if (Element* element = renderer->element())
document->accessSVGExtensions().addPendingResource(filterOperation->fragment(), element);
return nullptr;
}
RefPtr<FilterEffect> effect;
auto builder = std::make_unique<SVGFilterBuilder>(previousEffect);
for (auto& effectElement : childrenOfType<SVGFilterPrimitiveStandardAttributes>(*filter)) {
effect = effectElement.build(builder.get(), *this);
if (!effect)
continue;
effectElement.setStandardAttributes(effect.get());
builder->add(effectElement.result(), effect);
m_effects.append(effect);
}
return effect;
}
bool FilterEffectRenderer::build(RenderElement* renderer, const FilterOperations& operations, FilterConsumer consumer)
{
m_hasFilterThatMovesPixels = operations.hasFilterThatMovesPixels();
if (m_hasFilterThatMovesPixels)
m_outsets = operations.outsets();
m_effects.clear();
RefPtr<FilterEffect> previousEffect = m_sourceGraphic;
for (size_t i = 0; i < operations.operations().size(); ++i) {
RefPtr<FilterEffect> effect;
FilterOperation& filterOperation = *operations.operations().at(i);
switch (filterOperation.type()) {
case FilterOperation::REFERENCE: {
ReferenceFilterOperation& referenceOperation = downcast<ReferenceFilterOperation>(filterOperation);
effect = buildReferenceFilter(renderer, previousEffect, &referenceOperation);
referenceOperation.setFilterEffect(effect);
break;
}
case FilterOperation::GRAYSCALE: {
BasicColorMatrixFilterOperation& colorMatrixOperation = downcast<BasicColorMatrixFilterOperation>(filterOperation);
Vector<float> inputParameters;
double oneMinusAmount = clampTo(1 - colorMatrixOperation.amount(), 0.0, 1.0);
// See https://dvcs.w3.org/hg/FXTF/raw-file/tip/filters/index.html#grayscaleEquivalent
// for information on parameters.
inputParameters.append(narrowPrecisionToFloat(0.2126 + 0.7874 * oneMinusAmount));
inputParameters.append(narrowPrecisionToFloat(0.7152 - 0.7152 * oneMinusAmount));
inputParameters.append(narrowPrecisionToFloat(0.0722 - 0.0722 * oneMinusAmount));
endMatrixRow(inputParameters);
inputParameters.append(narrowPrecisionToFloat(0.2126 - 0.2126 * oneMinusAmount));
inputParameters.append(narrowPrecisionToFloat(0.7152 + 0.2848 * oneMinusAmount));
inputParameters.append(narrowPrecisionToFloat(0.0722 - 0.0722 * oneMinusAmount));
endMatrixRow(inputParameters);
inputParameters.append(narrowPrecisionToFloat(0.2126 - 0.2126 * oneMinusAmount));
inputParameters.append(narrowPrecisionToFloat(0.7152 - 0.7152 * oneMinusAmount));
inputParameters.append(narrowPrecisionToFloat(0.0722 + 0.9278 * oneMinusAmount));
endMatrixRow(inputParameters);
lastMatrixRow(inputParameters);
effect = FEColorMatrix::create(*this, FECOLORMATRIX_TYPE_MATRIX, inputParameters);
break;
}
case FilterOperation::SEPIA: {
BasicColorMatrixFilterOperation& colorMatrixOperation = downcast<BasicColorMatrixFilterOperation>(filterOperation);
Vector<float> inputParameters;
double oneMinusAmount = clampTo(1 - colorMatrixOperation.amount(), 0.0, 1.0);
// See https://dvcs.w3.org/hg/FXTF/raw-file/tip/filters/index.html#sepiaEquivalent
// for information on parameters.
inputParameters.append(narrowPrecisionToFloat(0.393 + 0.607 * oneMinusAmount));
inputParameters.append(narrowPrecisionToFloat(0.769 - 0.769 * oneMinusAmount));
inputParameters.append(narrowPrecisionToFloat(0.189 - 0.189 * oneMinusAmount));
endMatrixRow(inputParameters);
inputParameters.append(narrowPrecisionToFloat(0.349 - 0.349 * oneMinusAmount));
inputParameters.append(narrowPrecisionToFloat(0.686 + 0.314 * oneMinusAmount));
inputParameters.append(narrowPrecisionToFloat(0.168 - 0.168 * oneMinusAmount));
endMatrixRow(inputParameters);
inputParameters.append(narrowPrecisionToFloat(0.272 - 0.272 * oneMinusAmount));
inputParameters.append(narrowPrecisionToFloat(0.534 - 0.534 * oneMinusAmount));
inputParameters.append(narrowPrecisionToFloat(0.131 + 0.869 * oneMinusAmount));
endMatrixRow(inputParameters);
lastMatrixRow(inputParameters);
effect = FEColorMatrix::create(*this, FECOLORMATRIX_TYPE_MATRIX, inputParameters);
break;
}
case FilterOperation::SATURATE: {
BasicColorMatrixFilterOperation& colorMatrixOperation = downcast<BasicColorMatrixFilterOperation>(filterOperation);
Vector<float> inputParameters;
inputParameters.append(narrowPrecisionToFloat(colorMatrixOperation.amount()));
effect = FEColorMatrix::create(*this, FECOLORMATRIX_TYPE_SATURATE, inputParameters);
break;
}
case FilterOperation::HUE_ROTATE: {
BasicColorMatrixFilterOperation& colorMatrixOperation = downcast<BasicColorMatrixFilterOperation>(filterOperation);
Vector<float> inputParameters;
inputParameters.append(narrowPrecisionToFloat(colorMatrixOperation.amount()));
effect = FEColorMatrix::create(*this, FECOLORMATRIX_TYPE_HUEROTATE, inputParameters);
break;
}
case FilterOperation::INVERT: {
BasicComponentTransferFilterOperation& componentTransferOperation = downcast<BasicComponentTransferFilterOperation>(filterOperation);
ComponentTransferFunction transferFunction;
transferFunction.type = FECOMPONENTTRANSFER_TYPE_TABLE;
Vector<float> transferParameters;
transferParameters.append(narrowPrecisionToFloat(componentTransferOperation.amount()));
transferParameters.append(narrowPrecisionToFloat(1 - componentTransferOperation.amount()));
transferFunction.tableValues = transferParameters;
ComponentTransferFunction nullFunction;
effect = FEComponentTransfer::create(*this, transferFunction, transferFunction, transferFunction, nullFunction);
break;
}
case FilterOperation::OPACITY: {
BasicComponentTransferFilterOperation& componentTransferOperation = downcast<BasicComponentTransferFilterOperation>(filterOperation);
ComponentTransferFunction transferFunction;
transferFunction.type = FECOMPONENTTRANSFER_TYPE_TABLE;
Vector<float> transferParameters;
transferParameters.append(0);
transferParameters.append(narrowPrecisionToFloat(componentTransferOperation.amount()));
transferFunction.tableValues = transferParameters;
ComponentTransferFunction nullFunction;
effect = FEComponentTransfer::create(*this, nullFunction, nullFunction, nullFunction, transferFunction);
break;
}
case FilterOperation::BRIGHTNESS: {
BasicComponentTransferFilterOperation& componentTransferOperation = downcast<BasicComponentTransferFilterOperation>(filterOperation);
ComponentTransferFunction transferFunction;
transferFunction.type = FECOMPONENTTRANSFER_TYPE_LINEAR;
transferFunction.slope = narrowPrecisionToFloat(componentTransferOperation.amount());
transferFunction.intercept = 0;
ComponentTransferFunction nullFunction;
effect = FEComponentTransfer::create(*this, transferFunction, transferFunction, transferFunction, nullFunction);
break;
}
case FilterOperation::CONTRAST: {
BasicComponentTransferFilterOperation& componentTransferOperation = downcast<BasicComponentTransferFilterOperation>(filterOperation);
ComponentTransferFunction transferFunction;
transferFunction.type = FECOMPONENTTRANSFER_TYPE_LINEAR;
float amount = narrowPrecisionToFloat(componentTransferOperation.amount());
transferFunction.slope = amount;
transferFunction.intercept = -0.5 * amount + 0.5;
ComponentTransferFunction nullFunction;
effect = FEComponentTransfer::create(*this, transferFunction, transferFunction, transferFunction, nullFunction);
break;
}
case FilterOperation::BLUR: {
BlurFilterOperation& blurOperation = downcast<BlurFilterOperation>(filterOperation);
float stdDeviation = floatValueForLength(blurOperation.stdDeviation(), 0);
effect = FEGaussianBlur::create(*this, stdDeviation, stdDeviation, consumer == FilterProperty ? EDGEMODE_NONE : EDGEMODE_DUPLICATE);
break;
}
case FilterOperation::DROP_SHADOW: {
DropShadowFilterOperation& dropShadowOperation = downcast<DropShadowFilterOperation>(filterOperation);
effect = FEDropShadow::create(*this, dropShadowOperation.stdDeviation(), dropShadowOperation.stdDeviation(),
dropShadowOperation.x(), dropShadowOperation.y(), dropShadowOperation.color(), 1);
break;
}
default:
break;
}
if (effect) {
// Unlike SVG Filters and CSSFilterImages, filter functions on the filter
// property applied here should not clip to their primitive subregions.
effect->setClipsToBounds(consumer == FilterFunction);
effect->setOperatingColorSpace(ColorSpaceSRGB);
if (filterOperation.type() != FilterOperation::REFERENCE) {
effect->inputEffects().append(previousEffect);
m_effects.append(effect);
}
previousEffect = effect.release();
}
}
// If we didn't make any effects, tell our caller we are not valid
if (!m_effects.size())
return false;
setMaxEffectRects(m_sourceDrawingRegion);
return true;
}
bool FilterEffectRenderer::updateBackingStoreRect(const FloatRect& filterRect)
{
if (filterRect.isEmpty() || ImageBuffer::sizeNeedsClamping(filterRect.size()))
return false;
if (filterRect == sourceImageRect())
return false;
setSourceImageRect(filterRect);
return true;
}
void FilterEffectRenderer::allocateBackingStoreIfNeeded()
{
// At this point the effect chain has been built, and the
// source image sizes set. We just need to attach the graphic
// buffer if we have not yet done so.
if (!m_graphicsBufferAttached) {
IntSize logicalSize(m_sourceDrawingRegion.width(), m_sourceDrawingRegion.height());
if (!sourceImage() || sourceImage()->logicalSize() != logicalSize)
setSourceImage(ImageBuffer::create(logicalSize, renderingMode(), filterScale()));
m_graphicsBufferAttached = true;
}
}
void FilterEffectRenderer::clearIntermediateResults()
{
m_sourceGraphic->clearResult();
for (size_t i = 0; i < m_effects.size(); ++i)
m_effects[i]->clearResult();
}
void FilterEffectRenderer::apply()
{
RefPtr<FilterEffect> effect = lastEffect();
effect->apply();
effect->transformResultColorSpace(ColorSpaceSRGB);
}
LayoutRect FilterEffectRenderer::computeSourceImageRectForDirtyRect(const LayoutRect& filterBoxRect, const LayoutRect& dirtyRect)
{
// The result of this function is the area in the "filterBoxRect" that needs to be repainted, so that we fully cover the "dirtyRect".
LayoutRect rectForRepaint = dirtyRect;
if (hasFilterThatMovesPixels()) {
// Note that the outsets are reversed here because we are going backwards -> we have the dirty rect and
// need to find out what is the rectangle that might influence the result inside that dirty rect.
rectForRepaint.move(-m_outsets.right(), -m_outsets.bottom());
rectForRepaint.expand(m_outsets.left() + m_outsets.right(), m_outsets.top() + m_outsets.bottom());
}
rectForRepaint.intersect(filterBoxRect);
return rectForRepaint;
}
bool FilterEffectRendererHelper::prepareFilterEffect(RenderLayer* renderLayer, const LayoutRect& filterBoxRect, const LayoutRect& dirtyRect, const LayoutRect& layerRepaintRect)
{
ASSERT(m_haveFilterEffect && renderLayer->filterRenderer());
m_renderLayer = renderLayer;
m_repaintRect = dirtyRect;
FilterEffectRenderer* filter = renderLayer->filterRenderer();
LayoutRect filterSourceRect = filter->computeSourceImageRectForDirtyRect(filterBoxRect, dirtyRect);
m_paintOffset = filterSourceRect.location();
if (filterSourceRect.isEmpty()) {
// The dirty rect is not in view, just bail out.
m_haveFilterEffect = false;
return false;
}
bool hasUpdatedBackingStore = filter->updateBackingStoreRect(filterSourceRect);
if (filter->hasFilterThatMovesPixels()) {
if (hasUpdatedBackingStore)
m_repaintRect = filterSourceRect;
else {
m_repaintRect.unite(layerRepaintRect);
m_repaintRect.intersect(filterSourceRect);
}
}
return true;
}
GraphicsContext* FilterEffectRendererHelper::filterContext() const
{
if (!m_haveFilterEffect)
return 0;
FilterEffectRenderer* filter = m_renderLayer->filterRenderer();
return filter->inputContext();
}
bool FilterEffectRendererHelper::beginFilterEffect()
{
ASSERT(m_renderLayer);
FilterEffectRenderer* filter = m_renderLayer->filterRenderer();
filter->allocateBackingStoreIfNeeded();
// Paint into the context that represents the SourceGraphic of the filter.
GraphicsContext* sourceGraphicsContext = filter->inputContext();
if (!sourceGraphicsContext || filter->filterRegion().isEmpty() || ImageBuffer::sizeNeedsClamping(filter->filterRegion().size())) {
// Disable the filters and continue.
m_haveFilterEffect = false;
return false;
}
// Translate the context so that the contents of the layer is captuterd in the offscreen memory buffer.
sourceGraphicsContext->save();
sourceGraphicsContext->translate(-m_paintOffset.x(), -m_paintOffset.y());
sourceGraphicsContext->clearRect(m_repaintRect);
sourceGraphicsContext->clip(m_repaintRect);
m_startedFilterEffect = true;
return true;
}
void FilterEffectRendererHelper::applyFilterEffect(GraphicsContext& destinationContext)
{
ASSERT(m_haveFilterEffect && m_renderLayer->filterRenderer());
FilterEffectRenderer* filter = m_renderLayer->filterRenderer();
filter->inputContext()->restore();
filter->apply();
// Get the filtered output and draw it in place.
LayoutRect destRect = filter->outputRect();
destRect.move(m_paintOffset.x(), m_paintOffset.y());
if (ImageBuffer* outputBuffer = filter->output())
destinationContext.drawImageBuffer(*outputBuffer, snapRectToDevicePixels(destRect, m_renderLayer->renderer().document().deviceScaleFactor()));
filter->clearIntermediateResults();
}
} // namespace WebCore