OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkXfermode.h" | 8 #include "SkXfermode.h" |
9 #include "SkXfermode_proccoeff.h" | 9 #include "SkXfermode_proccoeff.h" |
10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
11 #include "SkMathPriv.h" | 11 #include "SkMathPriv.h" |
12 #include "SkOncePtr.h" | 12 #include "SkOnce.h" |
13 #include "SkOpts.h" | 13 #include "SkOpts.h" |
14 #include "SkReadBuffer.h" | 14 #include "SkReadBuffer.h" |
15 #include "SkString.h" | 15 #include "SkString.h" |
16 #include "SkWriteBuffer.h" | 16 #include "SkWriteBuffer.h" |
17 #include "SkPM4f.h" | 17 #include "SkPM4f.h" |
18 | 18 |
19 #define SkAlphaMulAlpha(a, b) SkMulDiv255Round(a, b) | 19 #define SkAlphaMulAlpha(a, b) SkMulDiv255Round(a, b) |
20 | 20 |
21 static inline unsigned saturated_add(unsigned a, unsigned b) { | 21 static inline unsigned saturated_add(unsigned a, unsigned b) { |
22 SkASSERT(a <= 255); | 22 SkASSERT(a <= 255); |
(...skipping 1273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1296 str->append(" dst: "); | 1296 str->append(" dst: "); |
1297 if (CANNOT_USE_COEFF == fDstCoeff) { | 1297 if (CANNOT_USE_COEFF == fDstCoeff) { |
1298 str->append("can't use"); | 1298 str->append("can't use"); |
1299 } else { | 1299 } else { |
1300 str->append(gCoeffStrings[fDstCoeff]); | 1300 str->append(gCoeffStrings[fDstCoeff]); |
1301 } | 1301 } |
1302 } | 1302 } |
1303 #endif | 1303 #endif |
1304 | 1304 |
1305 | 1305 |
1306 SK_DECLARE_STATIC_ONCE_PTR(SkXfermode, cached[SkXfermode::kLastMode + 1]); | |
1307 | |
1308 sk_sp<SkXfermode> SkXfermode::Make(Mode mode) { | 1306 sk_sp<SkXfermode> SkXfermode::Make(Mode mode) { |
1309 SkASSERT(SK_ARRAY_COUNT(gProcCoeffs) == kModeCount); | |
1310 | |
1311 if ((unsigned)mode >= kModeCount) { | 1307 if ((unsigned)mode >= kModeCount) { |
1312 // report error | 1308 // report error |
1313 return nullptr; | 1309 return nullptr; |
1314 } | 1310 } |
1315 | 1311 |
1316 // Skia's "default" mode is srcover. nullptr in SkPaint is interpreted as sr
cover | 1312 // Skia's "default" mode is srcover. nullptr in SkPaint is interpreted as sr
cover |
1317 // so we can just return nullptr from the factory. | 1313 // so we can just return nullptr from the factory. |
1318 if (kSrcOver_Mode == mode) { | 1314 if (kSrcOver_Mode == mode) { |
1319 return nullptr; | 1315 return nullptr; |
1320 } | 1316 } |
1321 | 1317 |
1322 return sk_ref_sp(cached[mode].get([=]{ | 1318 SkASSERT(SK_ARRAY_COUNT(gProcCoeffs) == kModeCount); |
| 1319 |
| 1320 static SkOnce once[SkXfermode::kLastMode+1]; |
| 1321 static SkXfermode* cached[SkXfermode::kLastMode+1]; |
| 1322 |
| 1323 once[mode]([mode] { |
1323 ProcCoeff rec = gProcCoeffs[mode]; | 1324 ProcCoeff rec = gProcCoeffs[mode]; |
1324 if (auto xfermode = SkOpts::create_xfermode(rec, mode)) { | 1325 if (auto xfermode = SkOpts::create_xfermode(rec, mode)) { |
1325 return xfermode; | 1326 cached[mode] = xfermode; |
| 1327 } else { |
| 1328 cached[mode] = new SkProcCoeffXfermode(rec, mode); |
1326 } | 1329 } |
1327 return (SkXfermode*) new SkProcCoeffXfermode(rec, mode); | 1330 }); |
1328 })); | 1331 return sk_ref_sp(cached[mode]); |
1329 } | 1332 } |
1330 | 1333 |
1331 SkXfermodeProc SkXfermode::GetProc(Mode mode) { | 1334 SkXfermodeProc SkXfermode::GetProc(Mode mode) { |
1332 SkXfermodeProc proc = nullptr; | 1335 SkXfermodeProc proc = nullptr; |
1333 if ((unsigned)mode < kModeCount) { | 1336 if ((unsigned)mode < kModeCount) { |
1334 proc = gProcCoeffs[mode].fProc; | 1337 proc = gProcCoeffs[mode].fProc; |
1335 } | 1338 } |
1336 return proc; | 1339 return proc; |
1337 } | 1340 } |
1338 | 1341 |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1410 if (!xfer) { | 1413 if (!xfer) { |
1411 return SkXfermode::kOpaque_SrcColorOpacity == opacityType; | 1414 return SkXfermode::kOpaque_SrcColorOpacity == opacityType; |
1412 } | 1415 } |
1413 | 1416 |
1414 return xfer->isOpaque(opacityType); | 1417 return xfer->isOpaque(opacityType); |
1415 } | 1418 } |
1416 | 1419 |
1417 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkXfermode) | 1420 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkXfermode) |
1418 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkProcCoeffXfermode) | 1421 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkProcCoeffXfermode) |
1419 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 1422 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
OLD | NEW |