Flutter Engine
The Flutter Engine
SkBlendMode.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
9
13#include "src/base/SkVx.h"
18
19#include <optional>
20
22 // The most important things we do here are:
23 // 1) never pre-scale with rgb coverage if the blend mode involves a source-alpha term;
24 // 2) always pre-scale Plus.
25 //
26 // When we pre-scale with rgb coverage, we scale each of source r,g,b, with a distinct value,
27 // and source alpha with one of those three values. This process destructively updates the
28 // source-alpha term, so we can't evaluate blend modes that need its original value.
29 //
30 // Plus always requires pre-scaling as a specific quirk of its implementation in
31 // SkRasterPipeline. This lets us put the clamp inside the blend mode itself rather
32 // than as a separate stage that'd come after the lerp.
33 //
34 // This function is a finer-grained breakdown of SkBlendMode_SupportsCoverageAsAlpha().
35 switch (mode) {
36 case SkBlendMode::kDst: // d --> no sa term, ok!
37 case SkBlendMode::kDstOver: // d + s*inv(da) --> no sa term, ok!
38 case SkBlendMode::kPlus: // clamp(s+d) --> no sa term, ok!
39 return true;
40
41 case SkBlendMode::kDstOut: // d * inv(sa)
42 case SkBlendMode::kSrcATop: // s*da + d*inv(sa)
43 case SkBlendMode::kSrcOver: // s + d*inv(sa)
44 case SkBlendMode::kXor: // s*inv(da) + d*inv(sa)
45 return !rgb_coverage;
46
47 default: break;
48 }
49 return false;
50}
51
52// Users of this function may want to switch to the rgb-coverage aware version above.
55}
56
58 struct CoeffRec {
61 };
62
63 static constexpr CoeffRec kCoeffs[] = {
64 // For Porter-Duff blend functions, color = src * src coeff + dst * dst coeff
65 // src coeff dst coeff blend func
66 // ---------------------- ----------------------- ----------
79
83 };
84
86 return false;
87 }
88 if (src) {
89 *src = kCoeffs[static_cast<int>(mode)].fSrc;
90 }
91 if (dst) {
92 *dst = kCoeffs[static_cast<int>(mode)].fDst;
93 }
94 return true;
95}
96
98 auto stage = SkRasterPipelineOp::srcover;
99 switch (mode) {
100 case SkBlendMode::kClear: stage = SkRasterPipelineOp::clear; break;
101 case SkBlendMode::kSrc: return; // This stage is a no-op.
102 case SkBlendMode::kDst: stage = SkRasterPipelineOp::move_dst_src; break;
103 case SkBlendMode::kSrcOver: stage = SkRasterPipelineOp::srcover; break;
104 case SkBlendMode::kDstOver: stage = SkRasterPipelineOp::dstover; break;
105 case SkBlendMode::kSrcIn: stage = SkRasterPipelineOp::srcin; break;
106 case SkBlendMode::kDstIn: stage = SkRasterPipelineOp::dstin; break;
107 case SkBlendMode::kSrcOut: stage = SkRasterPipelineOp::srcout; break;
108 case SkBlendMode::kDstOut: stage = SkRasterPipelineOp::dstout; break;
109 case SkBlendMode::kSrcATop: stage = SkRasterPipelineOp::srcatop; break;
110 case SkBlendMode::kDstATop: stage = SkRasterPipelineOp::dstatop; break;
111 case SkBlendMode::kXor: stage = SkRasterPipelineOp::xor_; break;
112 case SkBlendMode::kPlus: stage = SkRasterPipelineOp::plus_; break;
113 case SkBlendMode::kModulate: stage = SkRasterPipelineOp::modulate; break;
114
115 case SkBlendMode::kScreen: stage = SkRasterPipelineOp::screen; break;
116 case SkBlendMode::kOverlay: stage = SkRasterPipelineOp::overlay; break;
117 case SkBlendMode::kDarken: stage = SkRasterPipelineOp::darken; break;
118 case SkBlendMode::kLighten: stage = SkRasterPipelineOp::lighten; break;
119 case SkBlendMode::kColorDodge: stage = SkRasterPipelineOp::colordodge; break;
120 case SkBlendMode::kColorBurn: stage = SkRasterPipelineOp::colorburn; break;
121 case SkBlendMode::kHardLight: stage = SkRasterPipelineOp::hardlight; break;
122 case SkBlendMode::kSoftLight: stage = SkRasterPipelineOp::softlight; break;
124 case SkBlendMode::kExclusion: stage = SkRasterPipelineOp::exclusion; break;
126
127 case SkBlendMode::kHue: stage = SkRasterPipelineOp::hue; break;
131 }
132 p->append(stage);
133}
134
136 // special-case simple/common modes...
137 switch (mode) {
139 case SkBlendMode::kSrc: return src;
140 case SkBlendMode::kDst: return dst;
142 SkPMColor4f r;
143 (skvx::float4::Load(src.vec()) + skvx::float4::Load(dst.vec()) * (1-src.fA)).store(&r);
144 return r;
145 }
146 default:
147 break;
148 }
149
151 SkPMColor4f src_storage = src,
152 dst_storage = dst,
153 res_storage;
154 SkRasterPipeline_MemoryCtx src_ctx = { &src_storage, 0 },
155 dst_ctx = { &dst_storage, 0 },
156 res_ctx = { &res_storage, 0 };
157
158 p.append(SkRasterPipelineOp::load_f32, &dst_ctx);
159 p.append(SkRasterPipelineOp::move_src_dst);
160 p.append(SkRasterPipelineOp::load_f32, &src_ctx);
162 p.append(SkRasterPipelineOp::store_f32, &res_ctx);
163 p.run(0,0, 1,1);
164 return res_storage;
165}
166
168 switch (bm) {
169 case SkBlendMode::kClear: return "Clear";
170 case SkBlendMode::kSrc: return "Src";
171 case SkBlendMode::kDst: return "Dst";
172 case SkBlendMode::kSrcOver: return "SrcOver";
173 case SkBlendMode::kDstOver: return "DstOver";
174 case SkBlendMode::kSrcIn: return "SrcIn";
175 case SkBlendMode::kDstIn: return "DstIn";
176 case SkBlendMode::kSrcOut: return "SrcOut";
177 case SkBlendMode::kDstOut: return "DstOut";
178 case SkBlendMode::kSrcATop: return "SrcATop";
179 case SkBlendMode::kDstATop: return "DstATop";
180 case SkBlendMode::kXor: return "Xor";
181 case SkBlendMode::kPlus: return "Plus";
182 case SkBlendMode::kModulate: return "Modulate";
183 case SkBlendMode::kScreen: return "Screen";
184
185 case SkBlendMode::kOverlay: return "Overlay";
186 case SkBlendMode::kDarken: return "Darken";
187 case SkBlendMode::kLighten: return "Lighten";
188 case SkBlendMode::kColorDodge: return "ColorDodge";
189 case SkBlendMode::kColorBurn: return "ColorBurn";
190 case SkBlendMode::kHardLight: return "HardLight";
191 case SkBlendMode::kSoftLight: return "SoftLight";
192 case SkBlendMode::kDifference: return "Difference";
193 case SkBlendMode::kExclusion: return "Exclusion";
194 case SkBlendMode::kMultiply: return "Multiply";
195
196 case SkBlendMode::kHue: return "Hue";
197 case SkBlendMode::kSaturation: return "Saturation";
198 case SkBlendMode::kColor: return "Color";
199 case SkBlendMode::kLuminosity: return "Luminosity";
200 }
202}
203
204static bool just_solid_color(const SkPaint& p) {
205 return SK_AlphaOPAQUE == p.getAlpha() && !p.getColorFilter() && !p.getShader();
206}
207
208SkBlendFastPath CheckFastPath(const SkPaint& paint, bool dstIsOpaque) {
209 const auto bm = paint.asBlendMode();
210 if (!bm) {
212 }
213 switch (bm.value()) {
217 if (just_solid_color(paint)) {
219 }
224 if (dstIsOpaque) {
226 }
229 if (dstIsOpaque && just_solid_color(paint)) {
231 }
234 if (just_solid_color(paint)) {
236 }
238 default:
240 }
241}
SkRect fDst
Definition: LatticeOp.cpp:381
#define SkUNREACHABLE
Definition: SkAssert.h:135
SkBlendFastPath
bool SkBlendMode_AsCoeff(SkBlendMode mode, SkBlendModeCoeff *src, SkBlendModeCoeff *dst)
Definition: SkBlendMode.cpp:57
static bool just_solid_color(const SkPaint &p)
void SkBlendMode_AppendStages(SkBlendMode mode, SkRasterPipeline *p)
Definition: SkBlendMode.cpp:97
bool SkBlendMode_SupportsCoverageAsAlpha(SkBlendMode mode)
Definition: SkBlendMode.cpp:53
SkBlendFastPath CheckFastPath(const SkPaint &paint, bool dstIsOpaque)
bool SkBlendMode_ShouldPreScaleCoverage(SkBlendMode mode, bool rgb_coverage)
Definition: SkBlendMode.cpp:21
const char * SkBlendMode_Name(SkBlendMode bm)
SkPMColor4f SkBlendMode_Apply(SkBlendMode mode, const SkPMColor4f &src, const SkPMColor4f &dst)
SkBlendMode
Definition: SkBlendMode.h:38
@ kSrcOut
r = s * (1-da)
@ kExclusion
rc = s + d - two(s*d), ra = kSrcOver
@ kSaturation
saturation of source with hue and luminosity of destination
@ kColorBurn
darken destination to reflect source
@ kPlus
r = min(s + d, 1)
@ kLighten
rc = s + d - min(s*da, d*sa), ra = kSrcOver
@ kHue
hue of source with saturation and luminosity of destination
@ kDstIn
r = d * sa
@ kModulate
r = s*d
@ kMultiply
r = s*(1-da) + d*(1-sa) + s*d
@ kColorDodge
brighten destination to reflect source
@ kScreen
r = s + d - s*d
@ kSrcOver
r = s + (1-sa)*d
@ kXor
r = s*(1-da) + d*(1-sa)
@ kLuminosity
luminosity of source with hue and saturation of destination
@ kSoftLight
lighten or darken, depending on source
@ kDifference
rc = s + d - 2*(min(s*da, d*sa)), ra = kSrcOver
@ kOverlay
multiply or screen, depending on destination
@ kSrcATop
r = s*da + d*(1-sa)
@ kDstATop
r = d*sa + s*(1-da)
@ kDstOver
r = d + (1-da)*s
@ kColor
hue and saturation of source with luminosity of destination
@ kHardLight
multiply or screen, depending on source
@ kDstOut
r = d * (1-sa)
@ kDarken
rc = s + d - max(s*da, d*sa), ra = kSrcOver
@ kSrcIn
r = s * da
@ kClear
r = 0
SkBlendModeCoeff
Definition: SkBlendMode.h:84
constexpr SkPMColor4f SK_PMColor4fTRANSPARENT
Definition: SkColorData.h:378
constexpr SkAlpha SK_AlphaOPAQUE
Definition: SkColor.h:94
static size_t difference(size_t minuend, size_t subtrahend)
const Paint & paint
Definition: color_source.cc:38
DlColor color
static void luminosity(float dr, float dg, float db, float *sr, float *sg, float *sb)
Definition: hsl.cpp:130
static void hue(float dr, float dg, float db, float *sr, float *sg, float *sb)
Definition: hsl.cpp:92
static void saturation(float dr, float dg, float db, float *sr, float *sg, float *sb)
Definition: hsl.cpp:105
Int96 multiply(int64_t a, int32_t b)
Definition: Int96.cpp:41
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive mode
Definition: switches.h:228
dst
Definition: cp.py:12
static SKVX_ALWAYS_INLINE Vec Load(const void *ptr)
Definition: SkVx.h:109