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 "GrFragmentProcessor.h"
14 #include "GrInvariantOutput.h"
15 #include "SkColorFilter.h"
16 #include "SkGr.h"
17 #include "Test.h"
18
filterColor(const GrColor & color,uint32_t flags)19 static GrColor filterColor(const GrColor& color, uint32_t flags) {
20 uint32_t mask = 0;
21 if (flags & kR_GrColorComponentFlag) {
22 mask = 0xFF << GrColor_SHIFT_R;
23 }
24 if (flags & kG_GrColorComponentFlag) {
25 mask |= 0xFF << GrColor_SHIFT_G;
26 }
27 if (flags & kB_GrColorComponentFlag) {
28 mask |= 0xFF << GrColor_SHIFT_B;
29 }
30 if (flags & kA_GrColorComponentFlag) {
31 mask |= 0xFF << GrColor_SHIFT_A;
32 }
33 return color & mask;
34 }
35
test_getConstantColorComponents(skiatest::Reporter * reporter,GrContext * grContext)36 static void test_getConstantColorComponents(skiatest::Reporter* reporter, GrContext* grContext) {
37 struct GetConstantComponentTestCase {
38 // "Shape drawn with"
39 uint32_t inputComponents; // "rgb of", "red of", "alpha of", ...
40 GrColor inputColor; // "[color]"
41
42 SkColor filterColor; // "with filter color [color]"
43 SkXfermode::Mode filterMode; // "in mode [mode]"
44
45 // "produces"
46 uint32_t outputComponents; // "rgb of", "red of", "alpha of", ...
47 GrColor outputColor; // "[color]"
48 };
49
50 // Shorthands.
51 enum {
52 kR = kR_GrColorComponentFlag,
53 kG = kG_GrColorComponentFlag,
54 kB = kB_GrColorComponentFlag,
55 kA = kA_GrColorComponentFlag,
56 kRGB = kRGB_GrColorComponentFlags,
57 kRGBA = kRGBA_GrColorComponentFlags
58 };
59
60 // Note: below, SkColors are non-premultiplied, where as GrColors are premultiplied.
61
62 const SkColor c1 = SkColorSetARGB(200, 200, 200, 200);
63 const SkColor c2 = SkColorSetARGB(60, 60, 60, 60);
64 const GrColor gr_c1 = SkColor2GrColor(c1);
65 const GrColor gr_c2 = SkColor2GrColor(c2);
66
67 const GrColor gr_black = GrColorPackA4(0);
68 const GrColor gr_white = GrColorPackA4(255);
69 const GrColor gr_whiteTrans = GrColorPackA4(128);
70
71 GetConstantComponentTestCase filterTests[] = {
72 // A color filtered with Clear produces black.
73 { kRGBA, gr_white, SK_ColorBLACK, SkXfermode::kClear_Mode, kRGBA, gr_black },
74 { kRGBA, gr_c1, SK_ColorWHITE, SkXfermode::kClear_Mode, kRGBA, gr_black },
75 { kR, gr_white, c1, SkXfermode::kClear_Mode, kRGBA, gr_black },
76
77 // A color filtered with a color in mode Src, produces the filter color.
78 { kRGBA, gr_c2, c1, SkXfermode::kSrc_Mode, kRGBA, gr_c1 },
79 { kA, gr_c1, c1, SkXfermode::kSrc_Mode, kRGBA, gr_c1 },
80
81 // A color filtered with SrcOver produces a color.
82 { kRGBA, gr_whiteTrans, SkColorSetARGB(128, 200, 200, 200), SkXfermode::kSrcOver_Mode, kRGBA, GrColorPackRGBA(164, 164, 164, 192)},
83 // An unknown color with known alpha filtered with SrcOver produces an unknown color with known alpha.
84 { kA , gr_whiteTrans, SkColorSetARGB(128, 200, 200, 200), SkXfermode::kSrcOver_Mode, kA , GrColorPackRGBA(0, 0, 0, 192)},
85 // A color with unknown alpha filtered with SrcOver produces a color with unknown alpha.
86 { kRGB , gr_whiteTrans, SkColorSetARGB(128, 200, 200, 200), SkXfermode::kSrcOver_Mode, kRGB, GrColorPackRGBA(164, 164, 164, 0)},
87
88 // A color filtered with DstOver produces a color.
89 { kRGBA, gr_whiteTrans, SkColorSetARGB(128, 200, 200, 200), SkXfermode::kDstOver_Mode, kRGBA, GrColorPackRGBA(178, 178, 178, 192)},
90 // An unknown color with known alpha filtered with DstOver produces an unknown color with known alpha.
91 { kA , gr_whiteTrans, SkColorSetARGB(128, 200, 200, 200), SkXfermode::kDstOver_Mode, kA , GrColorPackRGBA(0, 0, 0, 192)},
92 // A color with unknown alpha filtered with DstOver produces an unknown color.
93 { kRGB , gr_whiteTrans, SkColorSetARGB(128, 200, 200, 200), SkXfermode::kDstOver_Mode, 0 , gr_black},
94
95 // An unknown color with known alpha and red component filtered with Multiply produces an unknown color with known red and alpha.
96 { kR|kA , gr_whiteTrans, SkColorSetARGB(128, 200, 200, 200), SkXfermode::kModulate_Mode, kR|kA, GrColorPackRGBA(50, 0, 0, 64) }
97 };
98
99 for (size_t i = 0; i < SK_ARRAY_COUNT(filterTests); ++i) {
100 const GetConstantComponentTestCase& test = filterTests[i];
101 SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(test.filterColor, test.filterMode));
102 SkTDArray<GrFragmentProcessor*> array;
103 bool hasFrag = cf->asFragmentProcessors(grContext, &array);
104 REPORTER_ASSERT(reporter, hasFrag);
105 REPORTER_ASSERT(reporter, 1 == array.count());
106 GrInvariantOutput inout(test.inputColor,
107 static_cast<GrColorComponentFlags>(test.inputComponents),
108 false);
109 array[0]->computeInvariantOutput(&inout);
110
111 REPORTER_ASSERT(reporter, filterColor(inout.color(), inout.validFlags()) == test.outputColor);
112 REPORTER_ASSERT(reporter, test.outputComponents == inout.validFlags());
113 array[0]->unref();
114 }
115 }
116
DEF_GPUTEST(GpuColorFilter,reporter,factory)117 DEF_GPUTEST(GpuColorFilter, reporter, factory) {
118 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
119 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
120
121 GrContext* grContext = factory->get(glType);
122 if (NULL == grContext) {
123 continue;
124 }
125
126 test_getConstantColorComponents(reporter, grContext);
127 }
128 }
129
130 #endif
131