summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/svg/svg_fe_convolve_matrix_element.cc
blob: 12d4fed52eefdba2039c0bf4cade5d6040129eca (plain)
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
/*
 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "third_party/blink/renderer/core/svg/svg_fe_convolve_matrix_element.h"

#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/svg/graphics/filters/svg_filter_builder.h"
#include "third_party/blink/renderer/core/svg/svg_animated_boolean.h"
#include "third_party/blink/renderer/core/svg/svg_animated_integer.h"
#include "third_party/blink/renderer/core/svg/svg_animated_integer_optional_integer.h"
#include "third_party/blink/renderer/core/svg/svg_animated_number.h"
#include "third_party/blink/renderer/core/svg/svg_animated_number_list.h"
#include "third_party/blink/renderer/core/svg/svg_animated_number_optional_number.h"
#include "third_party/blink/renderer/core/svg/svg_animated_string.h"
#include "third_party/blink/renderer/core/svg/svg_enumeration_map.h"
#include "third_party/blink/renderer/core/svg_names.h"
#include "third_party/blink/renderer/platform/geometry/int_point.h"
#include "third_party/blink/renderer/platform/geometry/int_size.h"
#include "third_party/blink/renderer/platform/heap/heap.h"

namespace blink {

template <>
CORE_EXPORT const SVGEnumerationMap&
GetEnumerationMap<FEConvolveMatrix::EdgeModeType>() {
  static const SVGEnumerationMap::Entry enum_items[] = {
      {FEConvolveMatrix::EDGEMODE_DUPLICATE, "duplicate"},
      {FEConvolveMatrix::EDGEMODE_WRAP, "wrap"},
      {FEConvolveMatrix::EDGEMODE_NONE, "none"},
  };
  static const SVGEnumerationMap entries(enum_items);
  return entries;
}

class SVGAnimatedOrder : public SVGAnimatedIntegerOptionalInteger {
 public:
  SVGAnimatedOrder(SVGElement* context_element)
      : SVGAnimatedIntegerOptionalInteger(context_element,
                                          svg_names::kOrderAttr,
                                          3) {}

  SVGParsingError AttributeChanged(const String&) override;

 protected:
  static SVGParsingError CheckValue(SVGParsingError parse_status, int value) {
    if (parse_status != SVGParseStatus::kNoError)
      return parse_status;
    if (value < 0)
      return SVGParseStatus::kNegativeValue;
    if (value == 0)
      return SVGParseStatus::kZeroValue;
    return SVGParseStatus::kNoError;
  }
};

SVGParsingError SVGAnimatedOrder::AttributeChanged(const String& value) {
  SVGParsingError parse_status =
      SVGAnimatedIntegerOptionalInteger::AttributeChanged(value);
  // Check for semantic errors.
  parse_status = CheckValue(parse_status, FirstInteger()->BaseValue()->Value());
  parse_status =
      CheckValue(parse_status, SecondInteger()->BaseValue()->Value());
  return parse_status;
}

SVGFEConvolveMatrixElement::SVGFEConvolveMatrixElement(Document& document)
    : SVGFilterPrimitiveStandardAttributes(svg_names::kFEConvolveMatrixTag,
                                           document),
      bias_(MakeGarbageCollected<SVGAnimatedNumber>(this,
                                                    svg_names::kBiasAttr,
                                                    0.0f)),
      divisor_(MakeGarbageCollected<SVGAnimatedNumber>(this,
                                                       svg_names::kDivisorAttr,
                                                       1)),
      in1_(MakeGarbageCollected<SVGAnimatedString>(this, svg_names::kInAttr)),
      edge_mode_(MakeGarbageCollected<
                 SVGAnimatedEnumeration<FEConvolveMatrix::EdgeModeType>>(
          this,
          svg_names::kEdgeModeAttr,
          FEConvolveMatrix::EDGEMODE_DUPLICATE)),
      kernel_matrix_(MakeGarbageCollected<SVGAnimatedNumberList>(
          this,
          svg_names::kKernelMatrixAttr)),
      kernel_unit_length_(MakeGarbageCollected<SVGAnimatedNumberOptionalNumber>(
          this,
          svg_names::kKernelUnitLengthAttr,
          0.0f)),
      order_(MakeGarbageCollected<SVGAnimatedOrder>(this)),
      preserve_alpha_(MakeGarbageCollected<SVGAnimatedBoolean>(
          this,
          svg_names::kPreserveAlphaAttr)),
      target_x_(
          MakeGarbageCollected<SVGAnimatedInteger>(this,
                                                   svg_names::kTargetXAttr,
                                                   0)),
      target_y_(
          MakeGarbageCollected<SVGAnimatedInteger>(this,
                                                   svg_names::kTargetYAttr,
                                                   0)) {
  AddToPropertyMap(preserve_alpha_);
  AddToPropertyMap(divisor_);
  AddToPropertyMap(bias_);
  AddToPropertyMap(kernel_unit_length_);
  AddToPropertyMap(kernel_matrix_);
  AddToPropertyMap(in1_);
  AddToPropertyMap(edge_mode_);
  AddToPropertyMap(order_);
  AddToPropertyMap(target_x_);
  AddToPropertyMap(target_y_);
}

SVGAnimatedNumber* SVGFEConvolveMatrixElement::kernelUnitLengthX() {
  return kernel_unit_length_->FirstNumber();
}

SVGAnimatedNumber* SVGFEConvolveMatrixElement::kernelUnitLengthY() {
  return kernel_unit_length_->SecondNumber();
}

SVGAnimatedInteger* SVGFEConvolveMatrixElement::orderX() const {
  return order_->FirstInteger();
}

SVGAnimatedInteger* SVGFEConvolveMatrixElement::orderY() const {
  return order_->SecondInteger();
}

void SVGFEConvolveMatrixElement::Trace(Visitor* visitor) const {
  visitor->Trace(bias_);
  visitor->Trace(divisor_);
  visitor->Trace(in1_);
  visitor->Trace(edge_mode_);
  visitor->Trace(kernel_matrix_);
  visitor->Trace(kernel_unit_length_);
  visitor->Trace(order_);
  visitor->Trace(preserve_alpha_);
  visitor->Trace(target_x_);
  visitor->Trace(target_y_);
  SVGFilterPrimitiveStandardAttributes::Trace(visitor);
}

IntSize SVGFEConvolveMatrixElement::MatrixOrder() const {
  if (!order_->IsSpecified())
    return IntSize(3, 3);
  return IntSize(orderX()->CurrentValue()->Value(),
                 orderY()->CurrentValue()->Value());
}

IntPoint SVGFEConvolveMatrixElement::TargetPoint() const {
  IntSize order = MatrixOrder();
  IntPoint target(target_x_->CurrentValue()->Value(),
                  target_y_->CurrentValue()->Value());
  // The spec says the default value is: targetX = floor ( orderX / 2 ))
  if (!target_x_->IsSpecified())
    target.SetX(order.Width() / 2);
  // The spec says the default value is: targetY = floor ( orderY / 2 ))
  if (!target_y_->IsSpecified())
    target.SetY(order.Height() / 2);
  return target;
}

float SVGFEConvolveMatrixElement::ComputeDivisor() const {
  if (divisor_->IsSpecified())
    return divisor_->CurrentValue()->Value();
  float divisor_value = 0;
  SVGNumberList* kernel_matrix = kernel_matrix_->CurrentValue();
  uint32_t kernel_matrix_size = kernel_matrix->length();
  for (uint32_t i = 0; i < kernel_matrix_size; ++i)
    divisor_value += kernel_matrix->at(i)->Value();
  return divisor_value ? divisor_value : 1;
}

bool SVGFEConvolveMatrixElement::SetFilterEffectAttribute(
    FilterEffect* effect,
    const QualifiedName& attr_name) {
  FEConvolveMatrix* convolve_matrix = static_cast<FEConvolveMatrix*>(effect);
  if (attr_name == svg_names::kEdgeModeAttr)
    return convolve_matrix->SetEdgeMode(edge_mode_->CurrentEnumValue());
  if (attr_name == svg_names::kDivisorAttr)
    return convolve_matrix->SetDivisor(ComputeDivisor());
  if (attr_name == svg_names::kBiasAttr)
    return convolve_matrix->SetBias(bias_->CurrentValue()->Value());
  if (attr_name == svg_names::kTargetXAttr ||
      attr_name == svg_names::kTargetYAttr)
    return convolve_matrix->SetTargetOffset(TargetPoint());
  if (attr_name == svg_names::kPreserveAlphaAttr)
    return convolve_matrix->SetPreserveAlpha(
        preserve_alpha_->CurrentValue()->Value());
  return SVGFilterPrimitiveStandardAttributes::SetFilterEffectAttribute(
      effect, attr_name);
}

void SVGFEConvolveMatrixElement::SvgAttributeChanged(
    const SvgAttributeChangedParams& params) {
  const QualifiedName& attr_name = params.name;
  if (attr_name == svg_names::kEdgeModeAttr ||
      attr_name == svg_names::kDivisorAttr ||
      attr_name == svg_names::kBiasAttr ||
      attr_name == svg_names::kTargetXAttr ||
      attr_name == svg_names::kTargetYAttr ||
      attr_name == svg_names::kPreserveAlphaAttr) {
    SVGElement::InvalidationGuard invalidation_guard(this);
    PrimitiveAttributeChanged(attr_name);
    return;
  }

  if (attr_name == svg_names::kInAttr || attr_name == svg_names::kOrderAttr ||
      attr_name == svg_names::kKernelMatrixAttr) {
    SVGElement::InvalidationGuard invalidation_guard(this);
    Invalidate();
    return;
  }

  SVGFilterPrimitiveStandardAttributes::SvgAttributeChanged(params);
}

FilterEffect* SVGFEConvolveMatrixElement::Build(
    SVGFilterBuilder* filter_builder,
    Filter* filter) {
  FilterEffect* input1 = filter_builder->GetEffectById(
      AtomicString(in1_->CurrentValue()->Value()));
  DCHECK(input1);

  auto* effect = MakeGarbageCollected<FEConvolveMatrix>(
      filter, MatrixOrder(), ComputeDivisor(), bias_->CurrentValue()->Value(),
      TargetPoint(), edge_mode_->CurrentEnumValue(),
      preserve_alpha_->CurrentValue()->Value(),
      kernel_matrix_->CurrentValue()->ToFloatVector());
  effect->InputEffects().push_back(input1);
  return effect;
}

}  // namespace blink