blob: 2f7a9b99d493dcf13c5a85ad4521f5b8c99ffbaf [file] [log] [blame]
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +00001/*
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.org2c326b72013-12-30 04:20:38 +000011#include "SkRandom.h"
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000012
reed@google.com85e143c2013-12-30 15:51:25 +000013static 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.org0c87ea82013-12-30 04:07:34 +000019
reed@google.com85e143c2013-12-30 15:51:25 +000020 return SkGradientShader::CreateLinear(pts, colors, NULL,
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000021 SK_ARRAY_COUNT(colors),
22 SkShader::kMirror_TileMode);
23}
24
25class VerticesGM : public skiagm::GM {
26 SkPoint fPts[9];
27 SkPoint fTexs[9];
28 SkColor fColors[9];
29 SkShader* fShader;
reed@google.com60da8f32014-05-05 20:41:21 +000030 unsigned fAlpha;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000031
32public:
reed@google.com60da8f32014-05-05 20:41:21 +000033 VerticesGM(unsigned alpha) : fShader(NULL), fAlpha(alpha) {
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000034 }
35
36 virtual ~VerticesGM() {
37 SkSafeUnref(fShader);
38 }
39
40protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000041
mtklein36352bf2015-03-25 18:17:31 -070042 void onOnceBeforeDraw() override {
reed@google.com85e143c2013-12-30 15:51:25 +000043 const SkScalar X = 150;
44 const SkScalar Y = 150;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000045
reed@google.com85e143c2013-12-30 15:51:25 +000046 fPts[0].set(0, 0); fPts[1].set(X/2, 10); 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);
48 fPts[6].set(0, Y); fPts[7].set(X/2, Y-10); fPts[8].set(X, Y);
49
50 const SkScalar w = 200;
51 const SkScalar h = 200;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000052
53 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);
55 fTexs[6].set(0, h); fTexs[7].set(w/2, h); fTexs[8].set(w, h);
56
57 fShader = make_shader(w, h);
58
59 SkRandom rand;
60 for (size_t i = 0; i < SK_ARRAY_COUNT(fColors); ++i) {
reed@google.com85e143c2013-12-30 15:51:25 +000061 fColors[i] = rand.nextU() | 0xFF000000;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000062 }
63 }
64
mtklein36352bf2015-03-25 18:17:31 -070065 SkString onShortName() override {
reed@google.com60da8f32014-05-05 20:41:21 +000066 SkString name("vertices");
67 if (0xFF != fAlpha) {
68 name.appendf("_%02X", fAlpha);
69 }
70 return name;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000071 }
72
mtklein36352bf2015-03-25 18:17:31 -070073 SkISize onISize() override {
reed@google.com85e143c2013-12-30 15:51:25 +000074 return SkISize::Make(600, 600);
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000075 }
76
mtklein36352bf2015-03-25 18:17:31 -070077 void onDraw(SkCanvas* canvas) override {
reed@google.com85e143c2013-12-30 15:51:25 +000078 // start with the center of a 3x3 grid
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000079 static const uint16_t fan[] = {
80 4,
81 0, 1, 2, 5, 8, 7, 6, 3, 0
82 };
83
84 const struct {
85 const SkColor* fColors;
86 const SkPoint* fTexs;
87 } rec[] = {
88 { fColors, NULL },
89 { NULL, fTexs },
90 { fColors, fTexs },
91 };
92
93 const SkXfermode::Mode modes[] = {
94 SkXfermode::kSrc_Mode,
95 SkXfermode::kDst_Mode,
96 SkXfermode::kModulate_Mode,
97 };
skia.committer@gmail.com4c912862013-12-30 07:01:37 +000098
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000099 SkPaint paint;
100 paint.setShader(fShader);
reed@google.com60da8f32014-05-05 20:41:21 +0000101 paint.setAlpha(fAlpha);
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000102
103 canvas->translate(20, 20);
104 for (size_t j = 0; j < SK_ARRAY_COUNT(modes); ++j) {
105 SkXfermode* xfer = SkXfermode::Create(modes[j]);
106 canvas->save();
107 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
108 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode,
109 SK_ARRAY_COUNT(fPts), fPts,
110 rec[i].fTexs, rec[i].fColors,
111 xfer, fan, SK_ARRAY_COUNT(fan), paint);
reed@google.com85e143c2013-12-30 15:51:25 +0000112 canvas->translate(200, 0);
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000113 }
114 canvas->restore();
reed@google.com85e143c2013-12-30 15:51:25 +0000115 canvas->translate(0, 200);
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000116 xfer->unref();
117 }
118 }
119
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000120private:
121 typedef skiagm::GM INHERITED;
122};
123
reed@google.com60da8f32014-05-05 20:41:21 +0000124/////////////////////////////////////////////////////////////////////////////////////
125
126DEF_GM( return SkNEW_ARGS(VerticesGM, (0xFF)); )
127DEF_GM( return SkNEW_ARGS(VerticesGM, (0x80)); )