Home | History | Annotate | Download | only in tests
      1 
      2 /*
      3  * Copyright 2013 Google Inc.
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 
      9 #if SK_SUPPORT_GPU
     10 
     11 #include "GrContext.h"
     12 #include "GrContextFactory.h"
     13 #include "GrEffect.h"
     14 #include "SkColorFilter.h"
     15 #include "Test.h"
     16 #include "SkGr.h"
     17 
     18 namespace {
     19 
     20 static GrColor filterColor(const GrColor& color, uint32_t flags)  {
     21     uint32_t mask = 0;
     22     if (flags & kR_GrColorComponentFlag) {
     23         mask = 0xFF << GrColor_SHIFT_R;
     24     }
     25     if (flags & kG_GrColorComponentFlag) {
     26         mask |= 0xFF << GrColor_SHIFT_G;
     27     }
     28     if (flags & kB_GrColorComponentFlag) {
     29         mask |= 0xFF << GrColor_SHIFT_B;
     30     }
     31     if (flags & kA_GrColorComponentFlag) {
     32         mask |= 0xFF << GrColor_SHIFT_A;
     33     }
     34     return color & mask;
     35 }
     36 
     37 static void test_getConstantColorComponents(skiatest::Reporter* reporter, GrContext* grContext) {
     38     struct GetConstantComponentTestCase {
     39         // "Shape drawn with"
     40         uint32_t inputComponents; // "rgb of", "red of", "alpha of", ...
     41         GrColor inputColor;       // "[color]"
     42 
     43         SkColor filterColor;      // "with filter color [color]"
     44         SkXfermode::Mode filterMode; // "in mode [mode]"
     45 
     46         // "produces"
     47         uint32_t outputComponents; // "rgb of", "red of", "alpha of", ...
     48         GrColor outputColor;       // "[color]"
     49     };
     50 
     51     // Shorthands.
     52     enum {
     53         kR = kR_GrColorComponentFlag,
     54         kG = kG_GrColorComponentFlag,
     55         kB = kB_GrColorComponentFlag,
     56         kA = kA_GrColorComponentFlag,
     57         kRGB = kRGB_GrColorComponentFlags,
     58         kRGBA = kRGBA_GrColorComponentFlags
     59     };
     60 
     61     // Note: below, SkColors are non-premultiplied, where as GrColors are premultiplied.
     62 
     63     const SkColor c1 = SkColorSetARGB(200, 200, 200, 200);
     64     const SkColor c2 = SkColorSetARGB(60, 60, 60, 60);
     65     const GrColor gr_c1 = SkColor2GrColor(c1);
     66     const GrColor gr_c2 = SkColor2GrColor(c2);
     67 
     68     const GrColor gr_black = GrColorPackRGBA(0, 0, 0, 0);
     69     const GrColor gr_white = GrColorPackRGBA(255, 255, 255, 255);
     70     const GrColor gr_whiteTrans = GrColorPackRGBA(128, 128, 128, 128);
     71 
     72     GetConstantComponentTestCase filterTests[] = {
     73         // A color filtered with Clear produces black.
     74         { kRGBA, gr_white, SK_ColorBLACK, SkXfermode::kClear_Mode, kRGBA, gr_black },
     75         { kRGBA, gr_c1,    SK_ColorWHITE, SkXfermode::kClear_Mode, kRGBA, gr_black },
     76         { kR,    gr_white, c1,            SkXfermode::kClear_Mode, kRGBA, gr_black },
     77 
     78         // A color filtered with a color in mode Src, produces the filter color.
     79         { kRGBA, gr_c2, c1, SkXfermode::kSrc_Mode, kRGBA, gr_c1 },
     80         { kA,    gr_c1, c1, SkXfermode::kSrc_Mode, kRGBA, gr_c1 },
     81 
     82         // A color filtered with SrcOver produces a color.
     83         { kRGBA, gr_whiteTrans, SkColorSetARGB(128, 200, 200, 200), SkXfermode::kSrcOver_Mode, kRGBA, GrColorPackRGBA(164, 164, 164, 192)},
     84         // An unknown color with known alpha filtered with SrcOver produces an unknown color with known alpha.
     85         { kA   , gr_whiteTrans, SkColorSetARGB(128, 200, 200, 200), SkXfermode::kSrcOver_Mode, kA   , GrColorPackRGBA(0, 0, 0, 192)},
     86         // A color with unknown alpha filtered with SrcOver produces a color with unknown alpha.
     87         { kRGB , gr_whiteTrans, SkColorSetARGB(128, 200, 200, 200), SkXfermode::kSrcOver_Mode, kRGB, GrColorPackRGBA(164, 164, 164, 0)},
     88 
     89         // A color filtered with DstOver produces a color.
     90         { kRGBA, gr_whiteTrans, SkColorSetARGB(128, 200, 200, 200), SkXfermode::kDstOver_Mode, kRGBA, GrColorPackRGBA(178, 178, 178, 192)},
     91         // An unknown color with known alpha filtered with DstOver produces an unknown color with known alpha.
     92         { kA   , gr_whiteTrans, SkColorSetARGB(128, 200, 200, 200), SkXfermode::kDstOver_Mode, kA   , GrColorPackRGBA(0, 0, 0, 192)},
     93         // A color with unknown alpha filtered with DstOver produces an unknown color.
     94         { kRGB , gr_whiteTrans, SkColorSetARGB(128, 200, 200, 200), SkXfermode::kDstOver_Mode, 0    , gr_black},
     95 
     96         // An unknown color with known alpha and red component filtered with Multiply produces an unknown color with known red and alpha.
     97         { kR|kA , gr_whiteTrans, SkColorSetARGB(128, 200, 200, 200), SkXfermode::kModulate_Mode, kR|kA, GrColorPackRGBA(50, 0, 0, 64) }
     98     };
     99 
    100     for (size_t i = 0; i < SK_ARRAY_COUNT(filterTests); ++i) {
    101         const GetConstantComponentTestCase& test = filterTests[i];
    102         SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(test.filterColor, test.filterMode));
    103         SkAutoTUnref<GrEffectRef> grEffect(cf->asNewEffect(grContext));
    104         GrColor color = test.inputColor;
    105         uint32_t components = test.inputComponents;
    106         grEffect->get()->getConstantColorComponents(&color, &components);
    107 
    108         REPORTER_ASSERT(reporter, filterColor(color, components) == test.outputColor);
    109         REPORTER_ASSERT(reporter, test.outputComponents == components);
    110     }
    111 }
    112 
    113 static void TestGpuColorFilter(skiatest::Reporter* reporter, GrContextFactory* factory) {
    114     for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
    115         GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
    116 
    117         GrContext* grContext = factory->get(glType);
    118         if (NULL == grContext) {
    119             continue;
    120         }
    121 
    122         test_getConstantColorComponents(reporter, grContext);
    123     }
    124 }
    125 
    126 }
    127 
    128 #include "TestClassDef.h"
    129 DEFINE_GPUTESTCLASS("GpuColorFilter", TestGpuColorFilterClass, TestGpuColorFilter)
    130 
    131 #endif
    132