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 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 14 | static sk_sp<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 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 21 | return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), |
| 22 | SkShader::kMirror_TileMode); |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 23 | } |
| 24 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 25 | static sk_sp<SkShader> make_shader2() { |
| 26 | return SkShader::MakeColorShader(SK_ColorBLUE); |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 27 | } |
| 28 | |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 29 | static sk_sp<SkColorFilter> make_color_filter() { |
| 30 | return SkColorFilter::MakeModeFilter(0xFFAABBCC, SkXfermode::kDarken_Mode); |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 31 | } |
| 32 | |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 33 | class VerticesGM : public skiagm::GM { |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 34 | SkPoint fPts[9]; |
| 35 | SkPoint fTexs[9]; |
| 36 | SkColor fColors[9]; |
| 37 | sk_sp<SkShader> fShader1; |
| 38 | sk_sp<SkShader> fShader2; |
| 39 | sk_sp<SkColorFilter> fColorFilter; |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 40 | |
| 41 | public: |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 42 | VerticesGM() {} |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 43 | |
| 44 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 45 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 46 | void onOnceBeforeDraw() override { |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 47 | const SkScalar X = 30; |
| 48 | const SkScalar Y = 30; |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 49 | |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 50 | fPts[0].set(0, 0); fPts[1].set(X/2, 3); fPts[2].set(X, 0); |
| 51 | fPts[3].set(3, Y/2); fPts[4].set(X/2, Y/2); fPts[5].set(X-3, Y/2); |
| 52 | 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] | 53 | |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 54 | const SkScalar w = 40; |
| 55 | const SkScalar h = 40; |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 56 | |
| 57 | fTexs[0].set(0, 0); fTexs[1].set(w/2, 0); fTexs[2].set(w, 0); |
| 58 | fTexs[3].set(0, h/2); fTexs[4].set(w/2, h/2); fTexs[5].set(w, h/2); |
| 59 | fTexs[6].set(0, h); fTexs[7].set(w/2, h); fTexs[8].set(w, h); |
| 60 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 61 | fShader1 = make_shader1(w, h); |
| 62 | fShader2 = make_shader2(); |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 63 | fColorFilter = make_color_filter(); |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 64 | |
| 65 | SkRandom rand; |
| 66 | for (size_t i = 0; i < SK_ARRAY_COUNT(fColors); ++i) { |
reed@google.com | 85e143c | 2013-12-30 15:51:25 +0000 | [diff] [blame] | 67 | fColors[i] = rand.nextU() | 0xFF000000; |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 71 | SkString onShortName() override { |
reed@google.com | 60da8f3 | 2014-05-05 20:41:21 +0000 | [diff] [blame] | 72 | SkString name("vertices"); |
reed@google.com | 60da8f3 | 2014-05-05 20:41:21 +0000 | [diff] [blame] | 73 | return name; |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 74 | } |
| 75 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 76 | SkISize onISize() override { |
reed@google.com | 85e143c | 2013-12-30 15:51:25 +0000 | [diff] [blame] | 77 | return SkISize::Make(600, 600); |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 78 | } |
| 79 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 80 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | 85e143c | 2013-12-30 15:51:25 +0000 | [diff] [blame] | 81 | // start with the center of a 3x3 grid |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 82 | static const uint16_t fan[] = { |
| 83 | 4, |
| 84 | 0, 1, 2, 5, 8, 7, 6, 3, 0 |
| 85 | }; |
| 86 | |
| 87 | const struct { |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 88 | const SkColor* fColors; |
| 89 | const SkPoint* fTexs; |
| 90 | const sk_sp<SkShader>& fShader; |
| 91 | const sk_sp<SkColorFilter>& fColorFilter; |
| 92 | uint8_t fAlpha; |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 93 | } rec[] = { |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 94 | { fColors, nullptr, fShader1, nullptr , 0xFF }, |
| 95 | { nullptr, fTexs , fShader1, nullptr , 0xFF }, |
| 96 | { fColors, fTexs , fShader1, nullptr , 0xFF }, |
| 97 | { fColors, nullptr, fShader2, nullptr , 0xFF }, |
| 98 | { nullptr, fTexs , fShader2, nullptr , 0xFF }, |
| 99 | { fColors, fTexs , fShader2, nullptr , 0xFF }, |
| 100 | { fColors, nullptr, fShader1, fColorFilter, 0xFF }, |
| 101 | { nullptr, fTexs , fShader1, fColorFilter, 0xFF }, |
| 102 | { fColors, fTexs , fShader1, fColorFilter, 0xFF }, |
| 103 | { fColors, nullptr, fShader2, fColorFilter, 0xFF }, |
| 104 | { nullptr, fTexs , fShader2, fColorFilter, 0xFF }, |
| 105 | { fColors, fTexs , fShader2, fColorFilter, 0xFF }, |
| 106 | { fColors, nullptr, fShader1, nullptr , 0x80 }, |
| 107 | { nullptr, fTexs , fShader1, nullptr , 0x80 }, |
| 108 | { fColors, fTexs , fShader1, nullptr , 0x80 }, |
| 109 | { fColors, nullptr, fShader2, nullptr , 0x80 }, |
| 110 | { nullptr, fTexs , fShader2, nullptr , 0x80 }, |
| 111 | { fColors, fTexs , fShader2, nullptr , 0x80 }, |
| 112 | { fColors, nullptr, fShader1, fColorFilter, 0x80 }, |
| 113 | { nullptr, fTexs , fShader1, fColorFilter, 0x80 }, |
| 114 | { fColors, fTexs , fShader1, fColorFilter, 0x80 }, |
| 115 | { fColors, nullptr, fShader2, fColorFilter, 0x80 }, |
| 116 | { nullptr, fTexs , fShader2, fColorFilter, 0x80 }, |
| 117 | { fColors, fTexs , fShader2, fColorFilter, 0x80 }, |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | const SkXfermode::Mode modes[] = { |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 121 | SkXfermode::kClear_Mode, |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 122 | SkXfermode::kSrc_Mode, |
| 123 | SkXfermode::kDst_Mode, |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 124 | SkXfermode::kSrcOver_Mode, |
| 125 | SkXfermode::kDstOver_Mode, |
| 126 | SkXfermode::kSrcIn_Mode, |
| 127 | SkXfermode::kDstIn_Mode, |
| 128 | SkXfermode::kSrcOut_Mode, |
| 129 | SkXfermode::kDstOut_Mode, |
| 130 | SkXfermode::kSrcATop_Mode, |
| 131 | SkXfermode::kDstATop_Mode, |
| 132 | SkXfermode::kXor_Mode, |
| 133 | SkXfermode::kPlus_Mode, |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 134 | SkXfermode::kModulate_Mode, |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 135 | SkXfermode::kScreen_Mode, |
| 136 | SkXfermode::kOverlay_Mode, |
| 137 | SkXfermode::kDarken_Mode, |
| 138 | SkXfermode::kLighten_Mode, |
| 139 | SkXfermode::kColorDodge_Mode, |
| 140 | SkXfermode::kColorBurn_Mode, |
| 141 | SkXfermode::kHardLight_Mode, |
| 142 | SkXfermode::kSoftLight_Mode, |
| 143 | SkXfermode::kDifference_Mode, |
| 144 | SkXfermode::kExclusion_Mode, |
| 145 | SkXfermode::kMultiply_Mode, |
| 146 | SkXfermode::kHue_Mode, |
| 147 | SkXfermode::kSaturation_Mode, |
| 148 | SkXfermode::kColor_Mode, |
| 149 | SkXfermode::kLuminosity_Mode, |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 150 | }; |
skia.committer@gmail.com | 4c91286 | 2013-12-30 07:01:37 +0000 | [diff] [blame] | 151 | |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 152 | SkPaint paint; |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 153 | |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 154 | canvas->translate(4, 4); |
| 155 | int x = 0; |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 156 | for (size_t j = 0; j < SK_ARRAY_COUNT(modes); ++j) { |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 157 | auto xfer = SkXfermode::Make(modes[j]); |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 158 | canvas->save(); |
| 159 | for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) { |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 160 | paint.setShader(rec[i].fShader); |
| 161 | paint.setColorFilter(rec[i].fColorFilter); |
| 162 | paint.setAlpha(rec[i].fAlpha); |
| 163 | //if (2 == x) |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 164 | canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, |
| 165 | SK_ARRAY_COUNT(fPts), fPts, |
| 166 | rec[i].fTexs, rec[i].fColors, |
| 167 | xfer, fan, SK_ARRAY_COUNT(fan), paint); |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 168 | canvas->translate(40, 0); |
| 169 | ++x; |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 170 | } |
| 171 | canvas->restore(); |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 172 | canvas->translate(0, 40); |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 176 | private: |
| 177 | typedef skiagm::GM INHERITED; |
| 178 | }; |
| 179 | |
reed@google.com | 60da8f3 | 2014-05-05 20:41:21 +0000 | [diff] [blame] | 180 | ///////////////////////////////////////////////////////////////////////////////////// |
| 181 | |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 182 | DEF_GM(return new VerticesGM();) |