OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 #include "SkXfermode.h" | 9 #include "SkXfermode.h" |
10 #include "SkXfermode_opts_SSE2.h" | 10 #include "SkXfermode_opts_SSE2.h" |
11 #include "SkXfermode_proccoeff.h" | 11 #include "SkXfermode_proccoeff.h" |
12 #include "Sk4px.h" | 12 #include "Sk4pxXfermode.h" |
13 #include "SkColorPriv.h" | 13 #include "SkColorPriv.h" |
14 #include "SkLazyPtr.h" | 14 #include "SkLazyPtr.h" |
15 #include "SkMathPriv.h" | 15 #include "SkMathPriv.h" |
16 #include "SkPMFloat.h" | 16 #include "SkPMFloat.h" |
17 #include "SkReadBuffer.h" | 17 #include "SkReadBuffer.h" |
18 #include "SkString.h" | 18 #include "SkString.h" |
19 #include "SkUtilsArm.h" | 19 #include "SkUtilsArm.h" |
20 #include "SkWriteBuffer.h" | 20 #include "SkWriteBuffer.h" |
21 | 21 |
22 #if SK_CPU_X86 && SK_CPU_SSE_LEVEL < SK_CPU_SSE_LEVEL_SSE2 | |
23 #warning "SkXfermode will be much faster if you compile with support for SSE
2." | |
24 #endif | |
25 | |
26 #if SK_CPU_X86 || defined(SK_ARM_HAS_NEON) | |
27 #define SK_USE_4PX_XFERMODES | |
28 #endif | |
29 | |
30 #if !SK_ARM_NEON_IS_NONE | 22 #if !SK_ARM_NEON_IS_NONE |
31 #include "SkXfermode_opts_arm_neon.h" | 23 #include "SkXfermode_opts_arm_neon.h" |
32 #endif | 24 #endif |
33 | 25 |
34 #define SkAlphaMulAlpha(a, b) SkMulDiv255Round(a, b) | 26 #define SkAlphaMulAlpha(a, b) SkMulDiv255Round(a, b) |
35 | 27 |
36 static inline unsigned saturated_add(unsigned a, unsigned b) { | 28 static inline unsigned saturated_add(unsigned a, unsigned b) { |
37 SkASSERT(a <= 255); | 29 SkASSERT(a <= 255); |
38 SkASSERT(b <= 255); | 30 SkASSERT(b <= 255); |
39 unsigned sum = a + b; | 31 unsigned sum = a + b; |
(...skipping 1134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1174 } | 1166 } |
1175 | 1167 |
1176 #ifndef SK_IGNORE_TO_STRING | 1168 #ifndef SK_IGNORE_TO_STRING |
1177 void SkDstInXfermode::toString(SkString* str) const { | 1169 void SkDstInXfermode::toString(SkString* str) const { |
1178 this->INHERITED::toString(str); | 1170 this->INHERITED::toString(str); |
1179 } | 1171 } |
1180 #endif | 1172 #endif |
1181 | 1173 |
1182 /////////////////////////////////////////////////////////////////////////////// | 1174 /////////////////////////////////////////////////////////////////////////////// |
1183 | 1175 |
1184 #define XFERMODE(Name) \ | |
1185 struct Name { \ | |
1186 static Sk4px Xfer(const Sk4px&, const Sk4px&); \ | |
1187 static const SkXfermode::Mode kMode = SkXfermode::k##Name##_Mode; \ | |
1188 }; \ | |
1189 inline Sk4px Name::Xfer(const Sk4px& s, const Sk4px& d) | |
1190 | |
1191 XFERMODE(Clear) { return Sk4px((SkPMColor)0); } | |
1192 XFERMODE(Src) { return s; } | |
1193 XFERMODE(Dst) { return d; } | |
1194 XFERMODE(SrcIn) { return s.fastMulDiv255Round(d.alphas() ); } | |
1195 XFERMODE(SrcOut) { return s.fastMulDiv255Round(d.alphas().inv()); } | |
1196 XFERMODE(SrcOver) { return s + d.fastMulDiv255Round(s.alphas().inv()); } | |
1197 XFERMODE(DstIn) { return SrcIn ::Xfer(d,s); } | |
1198 XFERMODE(DstOut) { return SrcOut ::Xfer(d,s); } | |
1199 XFERMODE(DstOver) { return SrcOver::Xfer(d,s); } | |
1200 | |
1201 // [ S * Da + (1 - Sa) * D] | |
1202 XFERMODE(SrcATop) { | |
1203 return Sk4px::Wide(s.mulWiden(d.alphas()) + d.mulWiden(s.alphas().inv())) | |
1204 .div255RoundNarrow(); | |
1205 } | |
1206 XFERMODE(DstATop) { return SrcATop::Xfer(d,s); } | |
1207 //[ S * (1 - Da) + (1 - Sa) * D ] | |
1208 XFERMODE(Xor) { | |
1209 return Sk4px::Wide(s.mulWiden(d.alphas().inv()) + d.mulWiden(s.alphas().inv(
))) | |
1210 .div255RoundNarrow(); | |
1211 } | |
1212 // [S + D ] | |
1213 XFERMODE(Plus) { return s.saturatedAdd(d); } | |
1214 // [S * D ] | |
1215 XFERMODE(Modulate) { return s.fastMulDiv255Round(d); } | |
1216 // [S + D - S * D] | |
1217 XFERMODE(Screen) { | |
1218 // Doing the math as S + (1-S)*D or S + (D - S*D) means the add and subtract
can be done | |
1219 // in 8-bit space without overflow. S + (1-S)*D is a touch faster because i
nv() is cheap. | |
1220 return s + d.fastMulDiv255Round(s.inv()); | |
1221 } | |
1222 XFERMODE(Multiply) { | |
1223 return Sk4px::Wide(s.mulWiden(d.alphas().inv()) + | |
1224 d.mulWiden(s.alphas().inv()) + | |
1225 s.mulWiden(d)) | |
1226 .div255RoundNarrow(); | |
1227 } | |
1228 // [ Sa + Da - Sa*Da, Sc + Dc - 2*min(Sc*Da, Dc*Sa) ] (And notice Sa*Da == min(
Sa*Da, Da*Sa).) | |
1229 XFERMODE(Difference) { | |
1230 auto m = Sk4px::Wide(Sk16h::Min(s.mulWiden(d.alphas()), d.mulWiden(s.alphas(
)))) | |
1231 .div255RoundNarrow(); | |
1232 // There's no chance of underflow, and if we subtract m before adding s+d, n
o overflow. | |
1233 return (s - m) + (d - m.zeroAlphas()); | |
1234 } | |
1235 // [ Sa + Da - Sa*Da, Sc + Dc - 2*Sc*Dc ] | |
1236 XFERMODE(Exclusion) { | |
1237 auto p = s.fastMulDiv255Round(d); | |
1238 // There's no chance of underflow, and if we subtract p before adding src+ds
t, no overflow. | |
1239 return (s - p) + (d - p.zeroAlphas()); | |
1240 } | |
1241 | |
1242 #undef XFERMODE | |
1243 | |
1244 // A reasonable fallback mode for doing AA is to simply apply the transfermode f
irst, | |
1245 // then linearly interpolate the AA. | |
1246 template <typename Mode> | |
1247 static Sk4px xfer_aa(const Sk4px& s, const Sk4px& d, const Sk16b& aa) { | |
1248 Sk4px noAA = Mode::Xfer(s, d); | |
1249 return Sk4px::Wide(noAA.mulWiden(aa) + d.mulWiden(Sk4px(aa).inv())) | |
1250 .div255RoundNarrow(); | |
1251 } | |
1252 | |
1253 // For some transfermodes we specialize AA, either for correctness or performanc
e. | |
1254 #ifndef SK_NO_SPECIALIZED_AA_XFERMODES | |
1255 #define XFERMODE_AA(Name) \ | |
1256 template <> Sk4px xfer_aa<Name>(const Sk4px& s, const Sk4px& d, const Sk
16b& aa) | |
1257 | |
1258 // Plus' clamp needs to happen after AA. skia:3852 | |
1259 XFERMODE_AA(Plus) { // [ clamp(D + AA*S) ] | |
1260 // We implement this as D + Min(S*AA, (1-D)) to fit the arguments to Min
in 16 bits. | |
1261 return d + | |
1262 Sk4px::Wide(Sk16h::Min(s.mulWiden(aa), d.inv().mul255Widen())).div25
5RoundNarrow(); | |
1263 } | |
1264 | |
1265 #undef XFERMODE_AA | |
1266 #endif | |
1267 | |
1268 template <typename ProcType> | |
1269 class SkT4pxXfermode : public SkProcCoeffXfermode { | |
1270 public: | |
1271 static SkXfermode* Create(const ProcCoeff& rec) { | |
1272 return SkNEW_ARGS(SkT4pxXfermode, (rec)); | |
1273 } | |
1274 | |
1275 void xfer32(SkPMColor dst[], const SkPMColor src[], int n, const SkAlpha aa[
]) const override { | |
1276 if (NULL == aa) { | |
1277 Sk4px::MapDstSrc(n, dst, src, [&](const Sk4px& dst4, const Sk4px& sr
c4) { | |
1278 return ProcType::Xfer(src4, dst4); | |
1279 }); | |
1280 } else { | |
1281 Sk4px::MapDstSrcAlpha(n, dst, src, aa, | |
1282 [&](const Sk4px& dst4, const Sk4px& src4, const Sk16b& alpha
) { | |
1283 return xfer_aa<ProcType>(src4, dst4, alpha); | |
1284 }); | |
1285 } | |
1286 } | |
1287 | |
1288 private: | |
1289 SkT4pxXfermode(const ProcCoeff& rec) : SkProcCoeffXfermode(rec, ProcType::kM
ode) {} | |
1290 | |
1291 typedef SkProcCoeffXfermode INHERITED; | |
1292 }; | |
1293 | |
1294 /////////////////////////////////////////////////////////////////////////////// | 1176 /////////////////////////////////////////////////////////////////////////////// |
1295 | 1177 |
1296 class SkDstOutXfermode : public SkProcCoeffXfermode { | 1178 class SkDstOutXfermode : public SkProcCoeffXfermode { |
1297 public: | 1179 public: |
1298 static SkDstOutXfermode* Create(const ProcCoeff& rec) { | 1180 static SkDstOutXfermode* Create(const ProcCoeff& rec) { |
1299 return SkNEW_ARGS(SkDstOutXfermode, (rec)); | 1181 return SkNEW_ARGS(SkDstOutXfermode, (rec)); |
1300 } | 1182 } |
1301 | 1183 |
1302 void xfer32(SkPMColor*, const SkPMColor*, int, const SkAlpha*) const overrid
e; | 1184 void xfer32(SkPMColor*, const SkPMColor*, int, const SkAlpha*) const overrid
e; |
1303 | 1185 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1344 namespace { | 1226 namespace { |
1345 SkXfermode* create_mode(int iMode) { | 1227 SkXfermode* create_mode(int iMode) { |
1346 SkXfermode::Mode mode = (SkXfermode::Mode)iMode; | 1228 SkXfermode::Mode mode = (SkXfermode::Mode)iMode; |
1347 | 1229 |
1348 ProcCoeff rec = gProcCoeffs[mode]; | 1230 ProcCoeff rec = gProcCoeffs[mode]; |
1349 SkXfermodeProc pp = SkPlatformXfermodeProcFactory(mode); | 1231 SkXfermodeProc pp = SkPlatformXfermodeProcFactory(mode); |
1350 if (pp != NULL) { | 1232 if (pp != NULL) { |
1351 rec.fProc = pp; | 1233 rec.fProc = pp; |
1352 } | 1234 } |
1353 | 1235 |
1354 #if defined(SK_USE_4PX_XFERMODES) | 1236 if (auto xfermode = SkCreate4pxXfermode(rec, mode)) { |
1355 switch (mode) { | 1237 return xfermode; |
1356 case SkXfermode::kClear_Mode: return SkT4pxXfermode<Clear>::Create(
rec); | |
1357 case SkXfermode::kSrc_Mode: return SkT4pxXfermode<Src>::Create(re
c); | |
1358 case SkXfermode::kDst_Mode: return SkT4pxXfermode<Dst>::Create(re
c); | |
1359 case SkXfermode::kSrcOver_Mode: return SkT4pxXfermode<SrcOver>::Creat
e(rec); | |
1360 case SkXfermode::kDstOver_Mode: return SkT4pxXfermode<DstOver>::Creat
e(rec); | |
1361 case SkXfermode::kSrcIn_Mode: return SkT4pxXfermode<SrcIn>::Create(
rec); | |
1362 case SkXfermode::kDstIn_Mode: return SkT4pxXfermode<DstIn>::Create(
rec); | |
1363 case SkXfermode::kSrcOut_Mode: return SkT4pxXfermode<SrcOut>::Create
(rec); | |
1364 case SkXfermode::kDstOut_Mode: return SkT4pxXfermode<DstOut>::Create
(rec); | |
1365 case SkXfermode::kSrcATop_Mode: return SkT4pxXfermode<SrcATop>::Creat
e(rec); | |
1366 case SkXfermode::kDstATop_Mode: return SkT4pxXfermode<DstATop>::Creat
e(rec); | |
1367 case SkXfermode::kXor_Mode: return SkT4pxXfermode<Xor>::Create(re
c); | |
1368 case SkXfermode::kPlus_Mode: return SkT4pxXfermode<Plus>::Create(r
ec); | |
1369 case SkXfermode::kModulate_Mode: return SkT4pxXfermode<Modulate>::Crea
te(rec); | |
1370 case SkXfermode::kScreen_Mode: return SkT4pxXfermode<Screen>::Create
(rec); | |
1371 case SkXfermode::kMultiply_Mode: return SkT4pxXfermode<Multiply>::Crea
te(rec); | |
1372 case SkXfermode::kDifference_Mode: return SkT4pxXfermode<Difference>::Cr
eate(rec); | |
1373 case SkXfermode::kExclusion_Mode: return SkT4pxXfermode<Exclusion>::Cre
ate(rec); | |
1374 default: break; | |
1375 } | 1238 } |
1376 #endif | |
1377 | 1239 |
1378 SkXfermode* xfer = NULL; | 1240 SkXfermode* xfer = NULL; |
1379 | 1241 |
1380 // check if we have a platform optim for that | 1242 // check if we have a platform optim for that |
1381 SkProcCoeffXfermode* xfm = SkPlatformXfermodeFactory(rec, mode); | 1243 SkProcCoeffXfermode* xfm = SkPlatformXfermodeFactory(rec, mode); |
1382 if (xfm != NULL) { | 1244 if (xfm != NULL) { |
1383 xfer = xfm; | 1245 xfer = xfm; |
1384 } else { | 1246 } else { |
1385 // All modes can in theory be represented by the ProcCoeff rec, since | 1247 // All modes can in theory be represented by the ProcCoeff rec, since |
1386 // it contains function ptrs. However, a few modes are both simple and | 1248 // it contains function ptrs. However, a few modes are both simple and |
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1668 } else { | 1530 } else { |
1669 proc16 = rec.fProc16_General; | 1531 proc16 = rec.fProc16_General; |
1670 } | 1532 } |
1671 } | 1533 } |
1672 return proc16; | 1534 return proc16; |
1673 } | 1535 } |
1674 | 1536 |
1675 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkXfermode) | 1537 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkXfermode) |
1676 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkProcCoeffXfermode) | 1538 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkProcCoeffXfermode) |
1677 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 1539 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
OLD | NEW |