Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
dl_color_filter.h
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef FLUTTER_DISPLAY_LIST_EFFECTS_DL_COLOR_FILTER_H_
6#define FLUTTER_DISPLAY_LIST_EFFECTS_DL_COLOR_FILTER_H_
7
8#include "flutter/display_list/dl_attributes.h"
9#include "flutter/display_list/dl_blend_mode.h"
10#include "flutter/display_list/dl_color.h"
11#include "flutter/fml/logging.h"
12
13namespace flutter {
14
15class DlBlendColorFilter;
16class DlMatrixColorFilter;
17
18// The DisplayList ColorFilter class. This class implements all of the
19// facilities and adheres to the design goals of the |DlAttribute| base
20// class.
21
22// An enumerated type for the supported ColorFilter operations.
29
30class DlColorFilter : public DlAttribute<DlColorFilter, DlColorFilterType> {
31 public:
32 // Return a boolean indicating whether the color filtering operation will
33 // modify transparent black. This is typically used to determine if applying
34 // the ColorFilter to a temporary saveLayer buffer will turn the surrounding
35 // pixels non-transparent and therefore expand the bounds.
36 virtual bool modifies_transparent_black() const = 0;
37
38 // Return a boolean indicating whether the color filtering operation can
39 // be applied either before or after modulating the pixels with an opacity
40 // value without changing the operation.
41 virtual bool can_commute_with_opacity() const { return false; }
42
43 // Return a DlBlendColorFilter pointer to this object iff it is a Blend
44 // type of ColorFilter, otherwise return nullptr.
45 virtual const DlBlendColorFilter* asBlend() const { return nullptr; }
46
47 // Return a DlMatrixColorFilter pointer to this object iff it is a Matrix
48 // type of ColorFilter, otherwise return nullptr.
49 virtual const DlMatrixColorFilter* asMatrix() const { return nullptr; }
50
51 // asSrgb<->Linear is not needed because it has no properties to query.
52 // Its type fully specifies its operation.
53};
54
55// The Blend type of ColorFilter which specifies modifying the
56// colors as if the color specified in the Blend filter is the
57// source color and the color drawn by the rendering operation
58// is the destination color. The mode parameter of the Blend
59// filter is then used to combine those colors.
60class DlBlendColorFilter final : public DlColorFilter {
61 public:
65 : DlBlendColorFilter(filter.color_, filter.mode_) {}
66 explicit DlBlendColorFilter(const DlBlendColorFilter* filter)
67 : DlBlendColorFilter(filter->color_, filter->mode_) {}
68
69 static std::shared_ptr<DlColorFilter> Make(DlColor color, DlBlendMode mode);
70
72 size_t size() const override { return sizeof(*this); }
73
74 bool modifies_transparent_black() const override;
75 bool can_commute_with_opacity() const override;
76
77 std::shared_ptr<DlColorFilter> shared() const override {
78 return std::make_shared<DlBlendColorFilter>(this);
79 }
80
81 const DlBlendColorFilter* asBlend() const override { return this; }
82
83 DlColor color() const { return color_; }
84 DlBlendMode mode() const { return mode_; }
85
86 protected:
87 bool equals_(DlColorFilter const& other) const override {
89 auto that = static_cast<DlBlendColorFilter const*>(&other);
90 return color_ == that->color_ && mode_ == that->mode_;
91 }
92
93 private:
94 DlColor color_;
95 DlBlendMode mode_;
96};
97
98// The Matrix type of ColorFilter which runs every pixel drawn by
99// the rendering operation [iR,iG,iB,iA] through a vector/matrix
100// multiplication, as in:
101//
102// [ oR ] [ m[ 0] m[ 1] m[ 2] m[ 3] m[ 4] ] [ iR ]
103// [ oG ] [ m[ 5] m[ 6] m[ 7] m[ 8] m[ 9] ] [ iG ]
104// [ oB ] = [ m[10] m[11] m[12] m[13] m[14] ] x [ iB ]
105// [ oA ] [ m[15] m[16] m[17] m[18] m[19] ] [ iA ]
106// [ 1 ]
107//
108// The resulting color [oR,oG,oB,oA] is then clamped to the range of
109// valid pixel components before storing in the output.
110//
111// The incoming and outgoing [iR,iG,iB,iA] and [oR,oG,oB,oA] are
112// considered to be non-premultiplied. When working on premultiplied
113// pixel data, the necessary pre<->non-pre conversions must be performed.
114class DlMatrixColorFilter final : public DlColorFilter {
115 public:
116 explicit DlMatrixColorFilter(const float matrix[20]) {
117 memcpy(matrix_, matrix, sizeof(matrix_));
118 }
120 : DlMatrixColorFilter(filter.matrix_) {}
122 : DlMatrixColorFilter(filter->matrix_) {}
123
124 static std::shared_ptr<DlColorFilter> Make(const float matrix[20]);
125
127 size_t size() const override { return sizeof(*this); }
128
129 bool modifies_transparent_black() const override;
130 bool can_commute_with_opacity() const override;
131
132 std::shared_ptr<DlColorFilter> shared() const override {
133 return std::make_shared<DlMatrixColorFilter>(this);
134 }
135
136 const DlMatrixColorFilter* asMatrix() const override { return this; }
137
138 const float& operator[](int index) const { return matrix_[index]; }
139 void get_matrix(float matrix[20]) const {
140 memcpy(matrix, matrix_, sizeof(matrix_));
141 }
142
143 protected:
144 bool equals_(const DlColorFilter& other) const override {
146 auto that = static_cast<DlMatrixColorFilter const*>(&other);
147 return memcmp(matrix_, that->matrix_, sizeof(matrix_)) == 0;
148 }
149
150 private:
151 float matrix_[20];
152};
153
154// The SrgbToLinear type of ColorFilter that applies the inverse of the sRGB
155// gamma curve to the rendered pixels.
157 public:
158 static const std::shared_ptr<DlSrgbToLinearGammaColorFilter> kInstance;
159
166
170 size_t size() const override { return sizeof(*this); }
171 bool modifies_transparent_black() const override { return false; }
172 bool can_commute_with_opacity() const override { return true; }
173
174 std::shared_ptr<DlColorFilter> shared() const override { return kInstance; }
175
176 protected:
177 bool equals_(const DlColorFilter& other) const override {
179 return true;
180 }
181
182 private:
183 friend class DlColorFilter;
184};
185
186// The LinearToSrgb type of ColorFilter that applies the sRGB gamma curve
187// to the rendered pixels.
189 public:
190 static const std::shared_ptr<DlLinearToSrgbGammaColorFilter> kInstance;
191
198
202 size_t size() const override { return sizeof(*this); }
203 bool modifies_transparent_black() const override { return false; }
204 bool can_commute_with_opacity() const override { return true; }
205
206 std::shared_ptr<DlColorFilter> shared() const override { return kInstance; }
207
208 protected:
209 bool equals_(const DlColorFilter& other) const override {
211 return true;
212 }
213
214 private:
215 friend class DlColorFilter;
216};
217
218} // namespace flutter
219
220#endif // FLUTTER_DISPLAY_LIST_EFFECTS_DL_COLOR_FILTER_H_
virtual T type() const =0
bool equals_(DlColorFilter const &other) const override
static std::shared_ptr< DlColorFilter > Make(DlColor color, DlBlendMode mode)
DlBlendColorFilter(DlColor color, DlBlendMode mode)
bool can_commute_with_opacity() const override
const DlBlendColorFilter * asBlend() const override
DlBlendColorFilter(const DlBlendColorFilter *filter)
size_t size() const override
DlColorFilterType type() const override
std::shared_ptr< DlColorFilter > shared() const override
bool modifies_transparent_black() const override
DlBlendColorFilter(const DlBlendColorFilter &filter)
virtual const DlBlendColorFilter * asBlend() const
virtual const DlMatrixColorFilter * asMatrix() const
virtual bool modifies_transparent_black() const =0
virtual bool can_commute_with_opacity() const
std::shared_ptr< DlColorFilter > shared() const override
DlLinearToSrgbGammaColorFilter(const DlLinearToSrgbGammaColorFilter &filter)
bool equals_(const DlColorFilter &other) const override
DlColorFilterType type() const override
static const std::shared_ptr< DlLinearToSrgbGammaColorFilter > kInstance
DlLinearToSrgbGammaColorFilter(const DlLinearToSrgbGammaColorFilter *filter)
bool modifies_transparent_black() const override
DlColorFilterType type() const override
size_t size() const override
std::shared_ptr< DlColorFilter > shared() const override
DlMatrixColorFilter(const DlMatrixColorFilter &filter)
bool equals_(const DlColorFilter &other) const override
void get_matrix(float matrix[20]) const
DlMatrixColorFilter(const float matrix[20])
static std::shared_ptr< DlColorFilter > Make(const float matrix[20])
bool can_commute_with_opacity() const override
DlMatrixColorFilter(const DlMatrixColorFilter *filter)
const DlMatrixColorFilter * asMatrix() const override
bool modifies_transparent_black() const override
const float & operator[](int index) const
bool modifies_transparent_black() const override
static const std::shared_ptr< DlSrgbToLinearGammaColorFilter > kInstance
DlSrgbToLinearGammaColorFilter(const DlSrgbToLinearGammaColorFilter *filter)
std::shared_ptr< DlColorFilter > shared() const override
DlColorFilterType type() const override
DlSrgbToLinearGammaColorFilter(const DlSrgbToLinearGammaColorFilter &filter)
bool equals_(const DlColorFilter &other) const override
#define FML_DCHECK(condition)
Definition logging.h:103