summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/graphics/filters/filter_effect.h
blob: 1f3e99d19fe85e55b1bf2d2901866a1eadb4af2c (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
/*
 * 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) 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.
 */

#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_FILTERS_FILTER_EFFECT_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_FILTERS_FILTER_EFFECT_H_

#include "base/macros.h"
#include "third_party/blink/renderer/platform/geometry/float_rect.h"
#include "third_party/blink/renderer/platform/geometry/int_rect.h"
#include "third_party/blink/renderer/platform/graphics/color.h"
#include "third_party/blink/renderer/platform/graphics/interpolation_space.h"
#include "third_party/blink/renderer/platform/graphics/paint/paint_filter.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/wtf/forward.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"

namespace blink {

class Filter;
class FilterEffect;

typedef HeapVector<Member<FilterEffect>> FilterEffectVector;

enum FilterEffectType {
  kFilterEffectTypeUnknown,
  kFilterEffectTypeImage,
  kFilterEffectTypeTile,
  kFilterEffectTypeSourceInput
};

class PLATFORM_EXPORT FilterEffect : public GarbageCollected<FilterEffect> {
 public:
  virtual ~FilterEffect();
  virtual void Trace(Visitor*);

  void DisposeImageFilters();
  void DisposeImageFiltersRecursive();

  FilterEffectVector& InputEffects() { return input_effects_; }
  FilterEffect* InputEffect(unsigned) const;
  unsigned NumberOfEffectInputs() const { return input_effects_.size(); }

  inline bool HasImageFilter() const {
    return image_filters_[0] || image_filters_[1] || image_filters_[2] ||
           image_filters_[3];
  }

  // Clipped primitive subregion in the coordinate space of the target.
  FloatRect AbsoluteBounds() const;

  // Mapping a rect forwards to determine which which destination pixels a
  // given source rect would affect.
  FloatRect MapRect(const FloatRect&) const;

  virtual sk_sp<PaintFilter> CreateImageFilter();
  virtual sk_sp<PaintFilter> CreateImageFilterWithoutValidation();

  virtual FilterEffectType GetFilterEffectType() const {
    return kFilterEffectTypeUnknown;
  }

  virtual WTF::TextStream& ExternalRepresentation(WTF::TextStream&,
                                                  int indention = 0) const;

  FloatRect FilterPrimitiveSubregion() const {
    return filter_primitive_subregion_;
  }
  void SetFilterPrimitiveSubregion(
      const FloatRect& filter_primitive_subregion) {
    filter_primitive_subregion_ = filter_primitive_subregion;
  }

  Filter* GetFilter() { return filter_; }
  const Filter* GetFilter() const { return filter_; }

  bool ClipsToBounds() const { return clips_to_bounds_; }
  void SetClipsToBounds(bool value) { clips_to_bounds_ = value; }

  InterpolationSpace OperatingInterpolationSpace() const {
    return operating_interpolation_space_;
  }
  virtual void SetOperatingInterpolationSpace(
      InterpolationSpace interpolation_space) {
    operating_interpolation_space_ = interpolation_space;
  }

  virtual bool AffectsTransparentPixels() const { return false; }

  // Return false if the filter will only operate correctly on valid RGBA
  // values, with alpha in [0,255] and each color component in [0, alpha].
  virtual bool MayProduceInvalidPreMultipliedPixels() { return false; }

  PaintFilter* GetImageFilter(InterpolationSpace,
                              bool requires_pm_color_validation) const;
  void SetImageFilter(InterpolationSpace,
                      bool requires_pm_color_validation,
                      sk_sp<PaintFilter>);

  bool OriginTainted() const { return origin_tainted_; }
  void SetOriginTainted() { origin_tainted_ = true; }

  bool InputsTaintOrigin() const;

 protected:
  FilterEffect(Filter*);

  // Determine the contribution from the filter effect's inputs.
  virtual FloatRect MapInputs(const FloatRect&) const;

  // Apply the contribution from the filter effect's itself. (Like
  // expanding with the blur radius etc.)
  virtual FloatRect MapEffect(const FloatRect&) const;

  // Apply the clip bounds and factor in the effect of
  // affectsTransparentPixels().
  FloatRect ApplyBounds(const FloatRect&) const;

  sk_sp<PaintFilter> CreateTransparentBlack() const;

  Color AdaptColorToOperatingInterpolationSpace(const Color& device_color);

  PaintFilter::CropRect GetCropRect() const;

 private:
  FilterEffectVector input_effects_;

  Member<Filter> filter_;

  // The following member variables are SVG specific and will move to
  // LayoutSVGResourceFilterPrimitive.
  // See bug https://bugs.webkit.org/show_bug.cgi?id=45614.

  // The subregion of a filter primitive according to the SVG Filter
  // specification in local coordinates.
  FloatRect filter_primitive_subregion_;

  // Whether the effect should clip to its primitive region, or expand to use
  // the combined region of its inputs.
  bool clips_to_bounds_;

  bool origin_tainted_;

  InterpolationSpace operating_interpolation_space_;

  sk_sp<PaintFilter> image_filters_[4];

  DISALLOW_COPY_AND_ASSIGN(FilterEffect);
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_FILTERS_FILTER_EFFECT_H_