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 "SkOnce.h" | 12 #include "SkOncePtr.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 |
1306 sk_sp<SkXfermode> SkXfermode::Make(Mode mode) { | 1308 sk_sp<SkXfermode> SkXfermode::Make(Mode mode) { |
| 1309 SkASSERT(SK_ARRAY_COUNT(gProcCoeffs) == kModeCount); |
| 1310 |
1307 if ((unsigned)mode >= kModeCount) { | 1311 if ((unsigned)mode >= kModeCount) { |
1308 // report error | 1312 // report error |
1309 return nullptr; | 1313 return nullptr; |
1310 } | 1314 } |
1311 | 1315 |
1312 // Skia's "default" mode is srcover. nullptr in SkPaint is interpreted as sr
cover | 1316 // Skia's "default" mode is srcover. nullptr in SkPaint is interpreted as sr
cover |
1313 // so we can just return nullptr from the factory. | 1317 // so we can just return nullptr from the factory. |
1314 if (kSrcOver_Mode == mode) { | 1318 if (kSrcOver_Mode == mode) { |
1315 return nullptr; | 1319 return nullptr; |
1316 } | 1320 } |
1317 | 1321 |
1318 SkASSERT(SK_ARRAY_COUNT(gProcCoeffs) == kModeCount); | 1322 return sk_ref_sp(cached[mode].get([=]{ |
1319 | |
1320 static SkOnce once[SkXfermode::kLastMode+1]; | |
1321 static SkXfermode* cached[SkXfermode::kLastMode+1]; | |
1322 | |
1323 once[mode]([mode] { | |
1324 ProcCoeff rec = gProcCoeffs[mode]; | 1323 ProcCoeff rec = gProcCoeffs[mode]; |
1325 if (auto xfermode = SkOpts::create_xfermode(rec, mode)) { | 1324 if (auto xfermode = SkOpts::create_xfermode(rec, mode)) { |
1326 cached[mode] = xfermode; | 1325 return xfermode; |
1327 } else { | |
1328 cached[mode] = new SkProcCoeffXfermode(rec, mode); | |
1329 } | 1326 } |
1330 }); | 1327 return (SkXfermode*) new SkProcCoeffXfermode(rec, mode); |
1331 return sk_ref_sp(cached[mode]); | 1328 })); |
1332 } | 1329 } |
1333 | 1330 |
1334 SkXfermodeProc SkXfermode::GetProc(Mode mode) { | 1331 SkXfermodeProc SkXfermode::GetProc(Mode mode) { |
1335 SkXfermodeProc proc = nullptr; | 1332 SkXfermodeProc proc = nullptr; |
1336 if ((unsigned)mode < kModeCount) { | 1333 if ((unsigned)mode < kModeCount) { |
1337 proc = gProcCoeffs[mode].fProc; | 1334 proc = gProcCoeffs[mode].fProc; |
1338 } | 1335 } |
1339 return proc; | 1336 return proc; |
1340 } | 1337 } |
1341 | 1338 |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1413 if (!xfer) { | 1410 if (!xfer) { |
1414 return SkXfermode::kOpaque_SrcColorOpacity == opacityType; | 1411 return SkXfermode::kOpaque_SrcColorOpacity == opacityType; |
1415 } | 1412 } |
1416 | 1413 |
1417 return xfer->isOpaque(opacityType); | 1414 return xfer->isOpaque(opacityType); |
1418 } | 1415 } |
1419 | 1416 |
1420 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkXfermode) | 1417 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkXfermode) |
1421 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkProcCoeffXfermode) | 1418 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkProcCoeffXfermode) |
1422 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 1419 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
OLD | NEW |