bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
| 8 | #include "Test.h" |
| 9 | |
| 10 | #if SK_SUPPORT_GPU |
| 11 | |
| 12 | #include "GrBlend.h" |
| 13 | #include "SkGr.h" |
| 14 | #include "SkRandom.h" |
| 15 | #include "SkXfermode.h" |
| 16 | |
| 17 | static GrColor make_baseline_color(GrColor src, GrColor dst, const SkXfermode* xm) { |
| 18 | SkPMColor skSrc = GrColorToSkPMColor(src); |
| 19 | SkPMColor skDst = GrColorToSkPMColor(dst); |
| 20 | if (xm) { |
| 21 | xm->xfer32(&skDst, &skSrc, 1, nullptr); |
| 22 | } else { |
| 23 | // null means src-over |
| 24 | skDst = SkPMSrcOver(skSrc, skDst); |
| 25 | } |
| 26 | return SkPMColorToGrColor(skDst); |
| 27 | } |
| 28 | |
| 29 | DEF_TEST(GrGetCoeffBlendKnownComponents, reporter) { |
| 30 | SkRandom random; |
| 31 | for (int i = 0; i < SkXfermode::kLastCoeffMode; ++i) { |
| 32 | SkXfermode::Mode mode = (SkXfermode::Mode)i; |
| 33 | SkAutoTUnref<SkXfermode> xm(SkXfermode::Create(mode)); |
| 34 | SkXfermode::Coeff srcCoeff, dstCoeff; |
| 35 | SkAssertResult(SkXfermode::ModeAsCoeff(mode, &srcCoeff, &dstCoeff)); |
| 36 | for (int j = 0; j < 1000; ++j) { |
| 37 | GrColor src = GrPremulColor(random.nextU()); |
| 38 | GrColor dst = GrPremulColor(random.nextU()); |
| 39 | GrColor outColor; |
| 40 | GrColorComponentFlags outFlags; |
| 41 | GrGetCoeffBlendKnownComponents(SkXfermodeCoeffToGrBlendCoeff(srcCoeff), |
| 42 | SkXfermodeCoeffToGrBlendCoeff(dstCoeff), |
| 43 | src, kRGBA_GrColorComponentFlags, |
| 44 | dst, kRGBA_GrColorComponentFlags, |
| 45 | &outColor, &outFlags); |
| 46 | GrColor baselineColor = make_baseline_color(src, dst, xm); |
| 47 | if (SkAbs32(GrColorUnpackA(baselineColor) - GrColorUnpackA(outColor)) > 1 || |
| 48 | SkAbs32(GrColorUnpackR(baselineColor) - GrColorUnpackR(outColor)) > 1 || |
| 49 | SkAbs32(GrColorUnpackG(baselineColor) - GrColorUnpackG(outColor)) > 1 || |
| 50 | SkAbs32(GrColorUnpackB(baselineColor) - GrColorUnpackB(outColor)) > 1) { |
| 51 | ERRORF(reporter, "Blended color is 0x%08x, expected 0x%08x", outColor, |
| 52 | baselineColor); |
| 53 | } |
| 54 | GrColorIsPMAssert(outColor); |
| 55 | } |
| 56 | } |
| 57 | GrColor outColor; |
| 58 | GrColorComponentFlags outFlags; |
| 59 | GrGetCoeffBlendKnownComponents(kZero_GrBlendCoeff, kZero_GrBlendCoeff, |
| 60 | 0xFFFFFFFF, kNone_GrColorComponentFlags, |
| 61 | 0xFFFFFFFF, kNone_GrColorComponentFlags, |
| 62 | &outColor, &outFlags); |
| 63 | REPORTER_ASSERT(reporter, GrColor_TRANSPARENT_BLACK == outColor && |
| 64 | kRGBA_GrColorComponentFlags == outFlags); |
| 65 | GrGetCoeffBlendKnownComponents( |
| 66 | kOne_GrBlendCoeff, kOne_GrBlendCoeff, |
| 67 | 0x80FF0100, (kG_GrColorComponentFlag | kB_GrColorComponentFlag | kA_GrColorComponentFlag), |
| 68 | 0x7F00FFFF, (kR_GrColorComponentFlag | kG_GrColorComponentFlag | kA_GrColorComponentFlag), |
| 69 | &outColor, &outFlags); |
| 70 | REPORTER_ASSERT(reporter, GrColor_WHITE == outColor && kRGBA_GrColorComponentFlags == outFlags); |
| 71 | |
| 72 | GrGetCoeffBlendKnownComponents( |
| 73 | kOne_GrBlendCoeff, kISA_GrBlendCoeff, |
| 74 | 0x0000000, kRGBA_GrColorComponentFlags, |
| 75 | 0x80010203, kRGBA_GrColorComponentFlags, |
| 76 | &outColor, &outFlags); |
| 77 | REPORTER_ASSERT(reporter, 0x80010203 == outColor && kRGBA_GrColorComponentFlags == outFlags); |
| 78 | |
| 79 | GrGetCoeffBlendKnownComponents(kZero_GrBlendCoeff, kISA_GrBlendCoeff, |
| 80 | 0x0000000, kA_GrColorComponentFlag, |
| 81 | 0x80010203, kRGBA_GrColorComponentFlags, |
| 82 | &outColor, &outFlags); |
| 83 | REPORTER_ASSERT(reporter, 0x80010203 == outColor && kRGBA_GrColorComponentFlags == outFlags); |
| 84 | |
| 85 | GrGetCoeffBlendKnownComponents( |
| 86 | kIDC_GrBlendCoeff, kSC_GrBlendCoeff, |
| 87 | 0x0, kNone_GrColorComponentFlags, |
| 88 | 0x0, kRGBA_GrColorComponentFlags, |
| 89 | &outColor, &outFlags); |
| 90 | REPORTER_ASSERT(reporter, kNone_GrColorComponentFlags == outFlags); |
| 91 | |
| 92 | GrGetCoeffBlendKnownComponents( |
| 93 | kOne_GrBlendCoeff, kISA_GrBlendCoeff, |
| 94 | 0xFF808080, (kG_GrColorComponentFlag | kB_GrColorComponentFlag | kA_GrColorComponentFlag), |
| 95 | 0xFF606060, kRGBA_GrColorComponentFlags, |
| 96 | &outColor, &outFlags); |
| 97 | REPORTER_ASSERT(reporter, |
| 98 | (kG_GrColorComponentFlag | kB_GrColorComponentFlag | kA_GrColorComponentFlag) == outFlags && |
| 99 | (outColor & 0xFFFFFF00) == 0xFF808000); |
| 100 | } |
| 101 | |
| 102 | #endif |