summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/graphics/filters/filter_effect.cc
blob: dcd2fe7b6c0da0a0b50527a08c80e816a155c67b (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
/*
 * Copyright (C) 2008 Alex Mathews <possessedpenguinbob@gmail.com>
 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
 * Copyright (C) 2012 University of Szeged
 * Copyright (C) 2013 Google Inc. All rights reserved.
 *
 * 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/platform/graphics/filters/filter_effect.h"

#include "third_party/blink/renderer/platform/graphics/filters/filter.h"
#include "third_party/skia/include/core/SkColorFilter.h"
#include "third_party/skia/include/effects/SkColorFilterImageFilter.h"

namespace blink {

FilterEffect::FilterEffect(Filter* filter)
    : filter_(filter),
      clips_to_bounds_(true),
      origin_tainted_(false),
      operating_interpolation_space_(kInterpolationSpaceLinear) {
  DCHECK(filter_);
}

FilterEffect::~FilterEffect() = default;

void FilterEffect::Trace(blink::Visitor* visitor) {
  visitor->Trace(input_effects_);
  visitor->Trace(filter_);
}

FloatRect FilterEffect::AbsoluteBounds() const {
  FloatRect computed_bounds = GetFilter()->FilterRegion();
  if (!FilterPrimitiveSubregion().IsEmpty())
    computed_bounds.Intersect(FilterPrimitiveSubregion());
  return GetFilter()->MapLocalRectToAbsoluteRect(computed_bounds);
}

FloatRect FilterEffect::MapInputs(const FloatRect& rect) const {
  if (!input_effects_.size()) {
    if (ClipsToBounds())
      return AbsoluteBounds();
    return rect;
  }
  FloatRect input_union;
  for (const auto& effect : input_effects_)
    input_union.Unite(effect->MapRect(rect));
  return input_union;
}

FloatRect FilterEffect::MapEffect(const FloatRect& rect) const {
  return rect;
}

FloatRect FilterEffect::ApplyBounds(const FloatRect& rect) const {
  // Filters in SVG clip to primitive subregion, while CSS doesn't.
  if (!ClipsToBounds())
    return rect;
  FloatRect bounds = AbsoluteBounds();
  if (AffectsTransparentPixels())
    return bounds;
  return Intersection(rect, bounds);
}

FloatRect FilterEffect::MapRect(const FloatRect& rect) const {
  FloatRect result = MapInputs(rect);
  result = MapEffect(result);
  return ApplyBounds(result);
}

FilterEffect* FilterEffect::InputEffect(unsigned number) const {
  SECURITY_DCHECK(number < input_effects_.size());
  return input_effects_.at(number).Get();
}

void FilterEffect::DisposeImageFilters() {
  for (int i = 0; i < 4; i++)
    image_filters_[i] = nullptr;
}

void FilterEffect::DisposeImageFiltersRecursive() {
  if (!HasImageFilter())
    return;
  DisposeImageFilters();
  for (auto& effect : input_effects_)
    effect->DisposeImageFiltersRecursive();
}

Color FilterEffect::AdaptColorToOperatingInterpolationSpace(
    const Color& device_color) {
  // |deviceColor| is assumed to be DeviceRGB.
  return interpolation_space_utilities::ConvertColor(
      device_color, OperatingInterpolationSpace());
}

WTF::TextStream& FilterEffect::ExternalRepresentation(WTF::TextStream& ts,
                                                      int) const {
  // FIXME: We should dump the subRegions of the filter primitives here later.
  // This isn't possible at the moment, because we need more detailed
  // information from the target object.
  return ts;
}

sk_sp<PaintFilter> FilterEffect::CreateImageFilter() {
  return nullptr;
}

sk_sp<PaintFilter> FilterEffect::CreateImageFilterWithoutValidation() {
  return CreateImageFilter();
}

bool FilterEffect::InputsTaintOrigin() const {
  for (const Member<FilterEffect>& effect : input_effects_) {
    if (effect->OriginTainted())
      return true;
  }
  return false;
}

sk_sp<PaintFilter> FilterEffect::CreateTransparentBlack() const {
  PaintFilter::CropRect rect = GetCropRect();
  sk_sp<SkColorFilter> color_filter =
      SkColorFilter::MakeModeFilter(0, SkBlendMode::kClear);
  return sk_make_sp<ColorFilterPaintFilter>(std::move(color_filter), nullptr,
                                            &rect);
}

PaintFilter::CropRect FilterEffect::GetCropRect() const {
  if (!FilterPrimitiveSubregion().IsEmpty()) {
    FloatRect rect =
        GetFilter()->MapLocalRectToAbsoluteRect(FilterPrimitiveSubregion());
    return PaintFilter::CropRect(rect);
  } else {
    return PaintFilter::CropRect(SkRect::MakeEmpty(), 0);
  }
}

static int GetImageFilterIndex(InterpolationSpace interpolation_space,
                               bool requires_pm_color_validation) {
  // Map the (colorspace, bool) tuple to an integer index as follows:
  // 0 == linear colorspace, no PM validation
  // 1 == device colorspace, no PM validation
  // 2 == linear colorspace, PM validation
  // 3 == device colorspace, PM validation
  return (interpolation_space == kInterpolationSpaceLinear ? 0x1 : 0x0) |
         (requires_pm_color_validation ? 0x2 : 0x0);
}

PaintFilter* FilterEffect::GetImageFilter(
    InterpolationSpace interpolation_space,
    bool requires_pm_color_validation) const {
  int index =
      GetImageFilterIndex(interpolation_space, requires_pm_color_validation);
  return image_filters_[index].get();
}

void FilterEffect::SetImageFilter(InterpolationSpace interpolation_space,
                                  bool requires_pm_color_validation,
                                  sk_sp<PaintFilter> image_filter) {
  int index =
      GetImageFilterIndex(interpolation_space, requires_pm_color_validation);
  image_filters_[index] = std::move(image_filter);
}

}  // namespace blink