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" |
| 10 | #include "SkGradientShader.h" |
mike@reedtribe.org | 2c326b7 | 2013-12-30 04:20:38 +0000 | [diff] [blame] | 11 | #include "SkRandom.h" |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 12 | |
reed@google.com | 85e143c | 2013-12-30 15:51:25 +0000 | [diff] [blame] | 13 | static SkShader* make_shader(SkScalar w, SkScalar h) { |
| 14 | const SkColor colors[] = { |
| 15 | SK_ColorRED, SK_ColorCYAN, SK_ColorGREEN, SK_ColorWHITE, |
| 16 | SK_ColorMAGENTA, SK_ColorBLUE, SK_ColorYELLOW, |
| 17 | }; |
| 18 | const SkPoint pts[] = { { w/4, 0 }, { 3*w/4, h } }; |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 19 | |
reed@google.com | 85e143c | 2013-12-30 15:51:25 +0000 | [diff] [blame] | 20 | return SkGradientShader::CreateLinear(pts, colors, NULL, |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 21 | SK_ARRAY_COUNT(colors), |
| 22 | SkShader::kMirror_TileMode); |
| 23 | } |
| 24 | |
| 25 | class VerticesGM : public skiagm::GM { |
| 26 | SkPoint fPts[9]; |
| 27 | SkPoint fTexs[9]; |
| 28 | SkColor fColors[9]; |
| 29 | SkShader* fShader; |
reed@google.com | 60da8f3 | 2014-05-05 20:41:21 +0000 | [diff] [blame] | 30 | unsigned fAlpha; |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 31 | |
| 32 | public: |
reed@google.com | 60da8f3 | 2014-05-05 20:41:21 +0000 | [diff] [blame] | 33 | VerticesGM(unsigned alpha) : fShader(NULL), fAlpha(alpha) { |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | virtual ~VerticesGM() { |
| 37 | SkSafeUnref(fShader); |
| 38 | } |
| 39 | |
| 40 | protected: |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 41 | uint32_t onGetFlags() const SK_OVERRIDE { |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 42 | return kSkipTiled_Flag; |
| 43 | } |
| 44 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 45 | void onOnceBeforeDraw() SK_OVERRIDE { |
reed@google.com | 85e143c | 2013-12-30 15:51:25 +0000 | [diff] [blame] | 46 | const SkScalar X = 150; |
| 47 | const SkScalar Y = 150; |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 48 | |
reed@google.com | 85e143c | 2013-12-30 15:51:25 +0000 | [diff] [blame] | 49 | fPts[0].set(0, 0); fPts[1].set(X/2, 10); fPts[2].set(X, 0); |
| 50 | fPts[3].set(10, Y/2); fPts[4].set(X/2, Y/2); fPts[5].set(X-10, Y/2); |
| 51 | fPts[6].set(0, Y); fPts[7].set(X/2, Y-10); fPts[8].set(X, Y); |
| 52 | |
| 53 | const SkScalar w = 200; |
| 54 | const SkScalar h = 200; |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 55 | |
| 56 | fTexs[0].set(0, 0); fTexs[1].set(w/2, 0); fTexs[2].set(w, 0); |
| 57 | fTexs[3].set(0, h/2); fTexs[4].set(w/2, h/2); fTexs[5].set(w, h/2); |
| 58 | fTexs[6].set(0, h); fTexs[7].set(w/2, h); fTexs[8].set(w, h); |
| 59 | |
| 60 | fShader = make_shader(w, h); |
| 61 | |
| 62 | SkRandom rand; |
| 63 | for (size_t i = 0; i < SK_ARRAY_COUNT(fColors); ++i) { |
reed@google.com | 85e143c | 2013-12-30 15:51:25 +0000 | [diff] [blame] | 64 | fColors[i] = rand.nextU() | 0xFF000000; |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 68 | SkString onShortName() SK_OVERRIDE { |
reed@google.com | 60da8f3 | 2014-05-05 20:41:21 +0000 | [diff] [blame] | 69 | SkString name("vertices"); |
| 70 | if (0xFF != fAlpha) { |
| 71 | name.appendf("_%02X", fAlpha); |
| 72 | } |
| 73 | return name; |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 74 | } |
| 75 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 76 | SkISize onISize() SK_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 | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 80 | void onDraw(SkCanvas* canvas) SK_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 { |
| 88 | const SkColor* fColors; |
| 89 | const SkPoint* fTexs; |
| 90 | } rec[] = { |
| 91 | { fColors, NULL }, |
| 92 | { NULL, fTexs }, |
| 93 | { fColors, fTexs }, |
| 94 | }; |
| 95 | |
| 96 | const SkXfermode::Mode modes[] = { |
| 97 | SkXfermode::kSrc_Mode, |
| 98 | SkXfermode::kDst_Mode, |
| 99 | SkXfermode::kModulate_Mode, |
| 100 | }; |
skia.committer@gmail.com | 4c91286 | 2013-12-30 07:01:37 +0000 | [diff] [blame] | 101 | |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 102 | SkPaint paint; |
| 103 | paint.setShader(fShader); |
reed@google.com | 60da8f3 | 2014-05-05 20:41:21 +0000 | [diff] [blame] | 104 | paint.setAlpha(fAlpha); |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 105 | |
| 106 | canvas->translate(20, 20); |
| 107 | for (size_t j = 0; j < SK_ARRAY_COUNT(modes); ++j) { |
| 108 | SkXfermode* xfer = SkXfermode::Create(modes[j]); |
| 109 | canvas->save(); |
| 110 | for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) { |
| 111 | canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, |
| 112 | SK_ARRAY_COUNT(fPts), fPts, |
| 113 | rec[i].fTexs, rec[i].fColors, |
| 114 | xfer, fan, SK_ARRAY_COUNT(fan), paint); |
reed@google.com | 85e143c | 2013-12-30 15:51:25 +0000 | [diff] [blame] | 115 | canvas->translate(200, 0); |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 116 | } |
| 117 | canvas->restore(); |
reed@google.com | 85e143c | 2013-12-30 15:51:25 +0000 | [diff] [blame] | 118 | canvas->translate(0, 200); |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 119 | xfer->unref(); |
| 120 | } |
| 121 | } |
| 122 | |
mike@reedtribe.org | 0c87ea8 | 2013-12-30 04:07:34 +0000 | [diff] [blame] | 123 | private: |
| 124 | typedef skiagm::GM INHERITED; |
| 125 | }; |
| 126 | |
reed@google.com | 60da8f3 | 2014-05-05 20:41:21 +0000 | [diff] [blame] | 127 | ///////////////////////////////////////////////////////////////////////////////////// |
| 128 | |
| 129 | DEF_GM( return SkNEW_ARGS(VerticesGM, (0xFF)); ) |
| 130 | DEF_GM( return SkNEW_ARGS(VerticesGM, (0x80)); ) |