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