OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
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 "gm.h" | 8 #include "gm.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
| 10 #include "SkColorFilter.h" |
10 #include "SkGradientShader.h" | 11 #include "SkGradientShader.h" |
11 #include "SkRandom.h" | 12 #include "SkRandom.h" |
12 | 13 |
13 static SkShader* make_shader(SkScalar w, SkScalar h) { | 14 static SkShader* make_shader1(SkScalar w, SkScalar h) { |
14 const SkColor colors[] = { | 15 const SkColor colors[] = { |
15 SK_ColorRED, SK_ColorCYAN, SK_ColorGREEN, SK_ColorWHITE, | 16 SK_ColorRED, SK_ColorCYAN, SK_ColorGREEN, SK_ColorWHITE, |
16 SK_ColorMAGENTA, SK_ColorBLUE, SK_ColorYELLOW, | 17 SK_ColorMAGENTA, SK_ColorBLUE, SK_ColorYELLOW, |
17 }; | 18 }; |
18 const SkPoint pts[] = { { w/4, 0 }, { 3*w/4, h } }; | 19 const SkPoint pts[] = { { w/4, 0 }, { 3*w/4, h } }; |
19 | 20 |
20 return SkGradientShader::CreateLinear(pts, colors, nullptr, | 21 return SkGradientShader::CreateLinear(pts, colors, nullptr, |
21 SK_ARRAY_COUNT(colors), | 22 SK_ARRAY_COUNT(colors), |
22 SkShader::kMirror_TileMode); | 23 SkShader::kMirror_TileMode); |
23 } | 24 } |
24 | 25 |
| 26 static SkShader* make_shader2() { |
| 27 return SkShader::CreateColorShader(SK_ColorBLUE); |
| 28 } |
| 29 |
| 30 static SkColorFilter* make_color_filter() { |
| 31 return SkColorFilter::CreateModeFilter(0xFFAABBCC, SkXfermode::kDarken_Mode)
; |
| 32 } |
| 33 |
25 class VerticesGM : public skiagm::GM { | 34 class VerticesGM : public skiagm::GM { |
26 SkPoint fPts[9]; | 35 SkPoint fPts[9]; |
27 SkPoint fTexs[9]; | 36 SkPoint fTexs[9]; |
28 SkColor fColors[9]; | 37 SkColor fColors[9]; |
29 SkShader* fShader; | 38 SkAutoTUnref<SkShader> fShader1; |
30 unsigned fAlpha; | 39 SkAutoTUnref<SkShader> fShader2; |
| 40 SkAutoTUnref<SkColorFilter> fColorFilter; |
31 | 41 |
32 public: | 42 public: |
33 VerticesGM(unsigned alpha) : fShader(nullptr), fAlpha(alpha) { | 43 VerticesGM() {} |
34 } | |
35 | |
36 virtual ~VerticesGM() { | |
37 SkSafeUnref(fShader); | |
38 } | |
39 | 44 |
40 protected: | 45 protected: |
41 | 46 |
42 void onOnceBeforeDraw() override { | 47 void onOnceBeforeDraw() override { |
43 const SkScalar X = 150; | 48 const SkScalar X = 30; |
44 const SkScalar Y = 150; | 49 const SkScalar Y = 30; |
45 | 50 |
46 fPts[0].set(0, 0); fPts[1].set(X/2, 10); fPts[2].set(X, 0); | 51 fPts[0].set(0, 0); fPts[1].set(X/2, 3); fPts[2].set(X, 0); |
47 fPts[3].set(10, Y/2); fPts[4].set(X/2, Y/2); fPts[5].set(X-10, Y/2); | 52 fPts[3].set(3, Y/2); fPts[4].set(X/2, Y/2); fPts[5].set(X-3, Y/2); |
48 fPts[6].set(0, Y); fPts[7].set(X/2, Y-10); fPts[8].set(X, Y); | 53 fPts[6].set(0, Y); fPts[7].set(X/2, Y-3); fPts[8].set(X, Y); |
49 | 54 |
50 const SkScalar w = 200; | 55 const SkScalar w = 40; |
51 const SkScalar h = 200; | 56 const SkScalar h = 40; |
52 | 57 |
53 fTexs[0].set(0, 0); fTexs[1].set(w/2, 0); fTexs[2].set(w, 0); | 58 fTexs[0].set(0, 0); fTexs[1].set(w/2, 0); fTexs[2].set(w, 0); |
54 fTexs[3].set(0, h/2); fTexs[4].set(w/2, h/2); fTexs[5].set(w, h/2); | 59 fTexs[3].set(0, h/2); fTexs[4].set(w/2, h/2); fTexs[5].set(w, h/2); |
55 fTexs[6].set(0, h); fTexs[7].set(w/2, h); fTexs[8].set(w, h); | 60 fTexs[6].set(0, h); fTexs[7].set(w/2, h); fTexs[8].set(w, h); |
56 | 61 |
57 fShader = make_shader(w, h); | 62 fShader1.reset(make_shader1(w, h)); |
| 63 fShader2.reset(make_shader2()); |
| 64 fColorFilter.reset(make_color_filter()); |
58 | 65 |
59 SkRandom rand; | 66 SkRandom rand; |
60 for (size_t i = 0; i < SK_ARRAY_COUNT(fColors); ++i) { | 67 for (size_t i = 0; i < SK_ARRAY_COUNT(fColors); ++i) { |
61 fColors[i] = rand.nextU() | 0xFF000000; | 68 fColors[i] = rand.nextU() | 0xFF000000; |
62 } | 69 } |
63 } | 70 } |
64 | 71 |
65 SkString onShortName() override { | 72 SkString onShortName() override { |
66 SkString name("vertices"); | 73 SkString name("vertices"); |
67 if (0xFF != fAlpha) { | |
68 name.appendf("_%02X", fAlpha); | |
69 } | |
70 return name; | 74 return name; |
71 } | 75 } |
72 | 76 |
73 SkISize onISize() override { | 77 SkISize onISize() override { |
74 return SkISize::Make(600, 600); | 78 return SkISize::Make(600, 600); |
75 } | 79 } |
76 | 80 |
77 void onDraw(SkCanvas* canvas) override { | 81 void onDraw(SkCanvas* canvas) override { |
78 // start with the center of a 3x3 grid | 82 // start with the center of a 3x3 grid |
79 static const uint16_t fan[] = { | 83 static const uint16_t fan[] = { |
80 4, | 84 4, |
81 0, 1, 2, 5, 8, 7, 6, 3, 0 | 85 0, 1, 2, 5, 8, 7, 6, 3, 0 |
82 }; | 86 }; |
83 | 87 |
84 const struct { | 88 const struct { |
85 const SkColor* fColors; | 89 const SkColor* fColors; |
86 const SkPoint* fTexs; | 90 const SkPoint* fTexs; |
| 91 SkShader* fShader; |
| 92 SkColorFilter* fColorFilter; |
| 93 uint8_t fAlpha; |
87 } rec[] = { | 94 } rec[] = { |
88 { fColors, nullptr }, | 95 { fColors, nullptr, fShader1, nullptr , 0xFF }, |
89 { nullptr, fTexs }, | 96 { nullptr, fTexs , fShader1, nullptr , 0xFF }, |
90 { fColors, fTexs }, | 97 { fColors, fTexs , fShader1, nullptr , 0xFF }, |
| 98 { fColors, nullptr, fShader2, nullptr , 0xFF }, |
| 99 { nullptr, fTexs , fShader2, nullptr , 0xFF }, |
| 100 { fColors, fTexs , fShader2, nullptr , 0xFF }, |
| 101 { fColors, nullptr, fShader1, fColorFilter, 0xFF }, |
| 102 { nullptr, fTexs , fShader1, fColorFilter, 0xFF }, |
| 103 { fColors, fTexs , fShader1, fColorFilter, 0xFF }, |
| 104 { fColors, nullptr, fShader2, fColorFilter, 0xFF }, |
| 105 { nullptr, fTexs , fShader2, fColorFilter, 0xFF }, |
| 106 { fColors, fTexs , fShader2, fColorFilter, 0xFF }, |
| 107 { fColors, nullptr, fShader1, nullptr , 0x80 }, |
| 108 { nullptr, fTexs , fShader1, nullptr , 0x80 }, |
| 109 { fColors, fTexs , fShader1, nullptr , 0x80 }, |
| 110 { fColors, nullptr, fShader2, nullptr , 0x80 }, |
| 111 { nullptr, fTexs , fShader2, nullptr , 0x80 }, |
| 112 { fColors, fTexs , fShader2, nullptr , 0x80 }, |
| 113 { fColors, nullptr, fShader1, fColorFilter, 0x80 }, |
| 114 { nullptr, fTexs , fShader1, fColorFilter, 0x80 }, |
| 115 { fColors, fTexs , fShader1, fColorFilter, 0x80 }, |
| 116 { fColors, nullptr, fShader2, fColorFilter, 0x80 }, |
| 117 { nullptr, fTexs , fShader2, fColorFilter, 0x80 }, |
| 118 { fColors, fTexs , fShader2, fColorFilter, 0x80 }, |
91 }; | 119 }; |
92 | 120 |
93 const SkXfermode::Mode modes[] = { | 121 const SkXfermode::Mode modes[] = { |
| 122 SkXfermode::kClear_Mode, |
94 SkXfermode::kSrc_Mode, | 123 SkXfermode::kSrc_Mode, |
95 SkXfermode::kDst_Mode, | 124 SkXfermode::kDst_Mode, |
| 125 SkXfermode::kSrcOver_Mode, |
| 126 SkXfermode::kDstOver_Mode, |
| 127 SkXfermode::kSrcIn_Mode, |
| 128 SkXfermode::kDstIn_Mode, |
| 129 SkXfermode::kSrcOut_Mode, |
| 130 SkXfermode::kDstOut_Mode, |
| 131 SkXfermode::kSrcATop_Mode, |
| 132 SkXfermode::kDstATop_Mode, |
| 133 SkXfermode::kXor_Mode, |
| 134 SkXfermode::kPlus_Mode, |
96 SkXfermode::kModulate_Mode, | 135 SkXfermode::kModulate_Mode, |
| 136 SkXfermode::kScreen_Mode, |
| 137 SkXfermode::kOverlay_Mode, |
| 138 SkXfermode::kDarken_Mode, |
| 139 SkXfermode::kLighten_Mode, |
| 140 SkXfermode::kColorDodge_Mode, |
| 141 SkXfermode::kColorBurn_Mode, |
| 142 SkXfermode::kHardLight_Mode, |
| 143 SkXfermode::kSoftLight_Mode, |
| 144 SkXfermode::kDifference_Mode, |
| 145 SkXfermode::kExclusion_Mode, |
| 146 SkXfermode::kMultiply_Mode, |
| 147 SkXfermode::kHue_Mode, |
| 148 SkXfermode::kSaturation_Mode, |
| 149 SkXfermode::kColor_Mode, |
| 150 SkXfermode::kLuminosity_Mode, |
97 }; | 151 }; |
98 | 152 |
99 SkPaint paint; | 153 SkPaint paint; |
100 paint.setShader(fShader); | |
101 paint.setAlpha(fAlpha); | |
102 | 154 |
103 canvas->translate(20, 20); | 155 canvas->translate(4, 4); |
| 156 int x = 0; |
104 for (size_t j = 0; j < SK_ARRAY_COUNT(modes); ++j) { | 157 for (size_t j = 0; j < SK_ARRAY_COUNT(modes); ++j) { |
105 SkXfermode* xfer = SkXfermode::Create(modes[j]); | 158 SkXfermode* xfer = SkXfermode::Create(modes[j]); |
106 canvas->save(); | 159 canvas->save(); |
107 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) { | 160 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) { |
| 161 paint.setShader(rec[i].fShader); |
| 162 paint.setColorFilter(rec[i].fColorFilter); |
| 163 paint.setAlpha(rec[i].fAlpha); |
| 164 //if (2 == x) |
108 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, | 165 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, |
109 SK_ARRAY_COUNT(fPts), fPts, | 166 SK_ARRAY_COUNT(fPts), fPts, |
110 rec[i].fTexs, rec[i].fColors, | 167 rec[i].fTexs, rec[i].fColors, |
111 xfer, fan, SK_ARRAY_COUNT(fan), paint); | 168 xfer, fan, SK_ARRAY_COUNT(fan), paint); |
112 canvas->translate(200, 0); | 169 canvas->translate(40, 0); |
| 170 ++x; |
113 } | 171 } |
114 canvas->restore(); | 172 canvas->restore(); |
115 canvas->translate(0, 200); | 173 canvas->translate(0, 40); |
116 xfer->unref(); | 174 SkSafeUnref(xfer); |
117 } | 175 } |
118 } | 176 } |
119 | 177 |
120 private: | 178 private: |
121 typedef skiagm::GM INHERITED; | 179 typedef skiagm::GM INHERITED; |
122 }; | 180 }; |
123 | 181 |
124 ////////////////////////////////////////////////////////////////////////////////
///// | 182 ////////////////////////////////////////////////////////////////////////////////
///// |
125 | 183 |
126 DEF_GM(return new VerticesGM(0xFF);) | 184 DEF_GM(return new VerticesGM();) |
127 DEF_GM(return new VerticesGM(0x80);) | |
OLD | NEW |