mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 "gm.h" |
| 9 | #include "SkCanvas.h" |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 10 | #include "SkColorFilter.h" |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 11 | #include "SkGradientShader.h" |
mike@reedtribe.org | 2c326b7 | 2013-12-30 04:20:38 +0000 | [diff] [blame] | 12 | #include "SkRandom.h" |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 13 | |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 14 | static SkShader* make_shader1(SkScalar w, SkScalar h) { |
reed@google.com | 85e143c | 2013-12-30 15:51:25 +0000 | [diff] [blame] | 15 | const SkColor colors[] = { |
| 16 | SK_ColorRED, SK_ColorCYAN, SK_ColorGREEN, SK_ColorWHITE, |
| 17 | SK_ColorMAGENTA, SK_ColorBLUE, SK_ColorYELLOW, |
| 18 | }; |
| 19 | const SkPoint pts[] = { { w/4, 0 }, { 3*w/4, h } }; |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 20 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 21 | return SkGradientShader::CreateLinear(pts, colors, nullptr, |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 22 | SK_ARRAY_COUNT(colors), |
| 23 | SkShader::kMirror_TileMode); |
| 24 | } |
| 25 | |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 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 | |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 34 | class VerticesGM : public skiagm::GM { |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 35 | SkPoint fPts[9]; |
| 36 | SkPoint fTexs[9]; |
| 37 | SkColor fColors[9]; |
| 38 | SkAutoTUnref<SkShader> fShader1; |
| 39 | SkAutoTUnref<SkShader> fShader2; |
| 40 | SkAutoTUnref<SkColorFilter> fColorFilter; |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 41 | |
| 42 | public: |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 43 | VerticesGM() {} |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 44 | |
| 45 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 46 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 47 | void onOnceBeforeDraw() override { |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 48 | const SkScalar X = 30; |
| 49 | const SkScalar Y = 30; |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 50 | |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 51 | fPts[0].set(0, 0); fPts[1].set(X/2, 3); fPts[2].set(X, 0); |
| 52 | fPts[3].set(3, Y/2); fPts[4].set(X/2, Y/2); fPts[5].set(X-3, Y/2); |
| 53 | fPts[6].set(0, Y); fPts[7].set(X/2, Y-3); fPts[8].set(X, Y); |
reed@google.com | 85e143c | 2013-12-30 15:51:25 +0000 | [diff] [blame] | 54 | |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 55 | const SkScalar w = 40; |
| 56 | const SkScalar h = 40; |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 57 | |
| 58 | fTexs[0].set(0, 0); fTexs[1].set(w/2, 0); fTexs[2].set(w, 0); |
| 59 | fTexs[3].set(0, h/2); fTexs[4].set(w/2, h/2); fTexs[5].set(w, h/2); |
| 60 | fTexs[6].set(0, h); fTexs[7].set(w/2, h); fTexs[8].set(w, h); |
| 61 | |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 62 | fShader1.reset(make_shader1(w, h)); |
| 63 | fShader2.reset(make_shader2()); |
| 64 | fColorFilter.reset(make_color_filter()); |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 65 | |
| 66 | SkRandom rand; |
| 67 | for (size_t i = 0; i < SK_ARRAY_COUNT(fColors); ++i) { |
reed@google.com | 85e143c | 2013-12-30 15:51:25 +0000 | [diff] [blame] | 68 | fColors[i] = rand.nextU() | 0xFF000000; |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 72 | SkString onShortName() override { |
reed@google.com | 60da8f3 | 2014-05-05 20:41:21 +0000 | [diff] [blame] | 73 | SkString name("vertices"); |
reed@google.com | 60da8f3 | 2014-05-05 20:41:21 +0000 | [diff] [blame] | 74 | return name; |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 75 | } |
| 76 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 77 | SkISize onISize() override { |
reed@google.com | 85e143c | 2013-12-30 15:51:25 +0000 | [diff] [blame] | 78 | return SkISize::Make(600, 600); |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 79 | } |
| 80 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 81 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | 85e143c | 2013-12-30 15:51:25 +0000 | [diff] [blame] | 82 | // start with the center of a 3x3 grid |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 83 | static const uint16_t fan[] = { |
| 84 | 4, |
| 85 | 0, 1, 2, 5, 8, 7, 6, 3, 0 |
| 86 | }; |
| 87 | |
| 88 | const struct { |
| 89 | const SkColor* fColors; |
| 90 | const SkPoint* fTexs; |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 91 | SkShader* fShader; |
| 92 | SkColorFilter* fColorFilter; |
| 93 | uint8_t fAlpha; |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 94 | } rec[] = { |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 95 | { fColors, nullptr, fShader1, nullptr , 0xFF }, |
| 96 | { nullptr, fTexs , fShader1, nullptr , 0xFF }, |
| 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 }, |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | const SkXfermode::Mode modes[] = { |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 122 | SkXfermode::kClear_Mode, |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 123 | SkXfermode::kSrc_Mode, |
| 124 | SkXfermode::kDst_Mode, |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 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, |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 135 | SkXfermode::kModulate_Mode, |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 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, |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 151 | }; |
skia.committer@gmail.com | 4c91286 | 2013-12-30 07:01:37 +0000 | [diff] [blame] | 152 | |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 153 | SkPaint paint; |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 154 | |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 155 | canvas->translate(4, 4); |
| 156 | int x = 0; |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 157 | for (size_t j = 0; j < SK_ARRAY_COUNT(modes); ++j) { |
| 158 | SkXfermode* xfer = SkXfermode::Create(modes[j]); |
| 159 | canvas->save(); |
| 160 | for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) { |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 161 | paint.setShader(rec[i].fShader); |
| 162 | paint.setColorFilter(rec[i].fColorFilter); |
| 163 | paint.setAlpha(rec[i].fAlpha); |
| 164 | //if (2 == x) |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 165 | canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, |
| 166 | SK_ARRAY_COUNT(fPts), fPts, |
| 167 | rec[i].fTexs, rec[i].fColors, |
| 168 | xfer, fan, SK_ARRAY_COUNT(fan), paint); |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 169 | canvas->translate(40, 0); |
| 170 | ++x; |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 171 | } |
| 172 | canvas->restore(); |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 173 | canvas->translate(0, 40); |
| 174 | SkSafeUnref(xfer); |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 178 | private: |
| 179 | typedef skiagm::GM INHERITED; |
| 180 | }; |
| 181 | |
reed@google.com | 60da8f3 | 2014-05-05 20:41:21 +0000 | [diff] [blame] | 182 | ///////////////////////////////////////////////////////////////////////////////////// |
| 183 | |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 184 | DEF_GM(return new VerticesGM();) |