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() { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 30 | return SkColorFilter::MakeModeFilter(0xFFAABBCC, SkBlendMode::kDarken); |
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 |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 82 | constexpr uint16_t fan[] = { |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 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 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 120 | const SkBlendMode modes[] = { |
| 121 | SkBlendMode::kClear, |
| 122 | SkBlendMode::kSrc, |
| 123 | SkBlendMode::kDst, |
| 124 | SkBlendMode::kSrcOver, |
| 125 | SkBlendMode::kDstOver, |
| 126 | SkBlendMode::kSrcIn, |
| 127 | SkBlendMode::kDstIn, |
| 128 | SkBlendMode::kSrcOut, |
| 129 | SkBlendMode::kDstOut, |
| 130 | SkBlendMode::kSrcATop, |
| 131 | SkBlendMode::kDstATop, |
| 132 | SkBlendMode::kXor, |
| 133 | SkBlendMode::kPlus, |
| 134 | SkBlendMode::kModulate, |
| 135 | SkBlendMode::kScreen, |
| 136 | SkBlendMode::kOverlay, |
| 137 | SkBlendMode::kDarken, |
| 138 | SkBlendMode::kLighten, |
| 139 | SkBlendMode::kColorDodge, |
| 140 | SkBlendMode::kColorBurn, |
| 141 | SkBlendMode::kHardLight, |
| 142 | SkBlendMode::kSoftLight, |
| 143 | SkBlendMode::kDifference, |
| 144 | SkBlendMode::kExclusion, |
| 145 | SkBlendMode::kMultiply, |
| 146 | SkBlendMode::kHue, |
| 147 | SkBlendMode::kSaturation, |
| 148 | SkBlendMode::kColor, |
| 149 | SkBlendMode::kLuminosity, |
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) { |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 157 | canvas->save(); |
| 158 | for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) { |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 159 | paint.setShader(rec[i].fShader); |
| 160 | paint.setColorFilter(rec[i].fColorFilter); |
| 161 | paint.setAlpha(rec[i].fAlpha); |
| 162 | //if (2 == x) |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 163 | canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, |
| 164 | SK_ARRAY_COUNT(fPts), fPts, |
| 165 | rec[i].fTexs, rec[i].fColors, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 166 | modes[j], fan, SK_ARRAY_COUNT(fan), paint); |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 167 | canvas->translate(40, 0); |
| 168 | ++x; |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 169 | } |
| 170 | canvas->restore(); |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 171 | canvas->translate(0, 40); |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 175 | private: |
| 176 | typedef skiagm::GM INHERITED; |
| 177 | }; |
| 178 | |
reed@google.com | 60da8f3 | 2014-05-05 20:41:21 +0000 | [diff] [blame] | 179 | ///////////////////////////////////////////////////////////////////////////////////// |
| 180 | |
bsalomon | 093779c | 2016-03-08 11:53:31 -0800 | [diff] [blame] | 181 | DEF_GM(return new VerticesGM();) |