blob: 0d09dc669ba9285bb82f34c895829e4e8f925ee8 [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;
30
31public:
32 VerticesGM() : fShader(NULL) {
33 }
34
35 virtual ~VerticesGM() {
36 SkSafeUnref(fShader);
37 }
38
39protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000040 virtual uint32_t onGetFlags() const SK_OVERRIDE {
41 return kSkipTiled_Flag;
42 }
43
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000044 virtual void onOnceBeforeDraw() SK_OVERRIDE {
reed@google.com85e143c2013-12-30 15:51:25 +000045 const SkScalar X = 150;
46 const SkScalar Y = 150;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000047
reed@google.com85e143c2013-12-30 15:51:25 +000048 fPts[0].set(0, 0); fPts[1].set(X/2, 10); fPts[2].set(X, 0);
49 fPts[3].set(10, Y/2); fPts[4].set(X/2, Y/2); fPts[5].set(X-10, Y/2);
50 fPts[6].set(0, Y); fPts[7].set(X/2, Y-10); fPts[8].set(X, Y);
51
52 const SkScalar w = 200;
53 const SkScalar h = 200;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000054
55 fTexs[0].set(0, 0); fTexs[1].set(w/2, 0); fTexs[2].set(w, 0);
56 fTexs[3].set(0, h/2); fTexs[4].set(w/2, h/2); fTexs[5].set(w, h/2);
57 fTexs[6].set(0, h); fTexs[7].set(w/2, h); fTexs[8].set(w, h);
58
59 fShader = make_shader(w, h);
60
61 SkRandom rand;
62 for (size_t i = 0; i < SK_ARRAY_COUNT(fColors); ++i) {
reed@google.com85e143c2013-12-30 15:51:25 +000063 fColors[i] = rand.nextU() | 0xFF000000;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000064 }
65 }
66
67 virtual SkString onShortName() SK_OVERRIDE {
68 return SkString("vertices");
69 }
70
71 virtual SkISize onISize() SK_OVERRIDE {
reed@google.com85e143c2013-12-30 15:51:25 +000072 return SkISize::Make(600, 600);
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000073 }
74
75 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
reed@google.com85e143c2013-12-30 15:51:25 +000076 // start with the center of a 3x3 grid
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000077 static const uint16_t fan[] = {
78 4,
79 0, 1, 2, 5, 8, 7, 6, 3, 0
80 };
81
82 const struct {
83 const SkColor* fColors;
84 const SkPoint* fTexs;
85 } rec[] = {
86 { fColors, NULL },
87 { NULL, fTexs },
88 { fColors, fTexs },
89 };
90
91 const SkXfermode::Mode modes[] = {
92 SkXfermode::kSrc_Mode,
93 SkXfermode::kDst_Mode,
94 SkXfermode::kModulate_Mode,
95 };
skia.committer@gmail.com4c912862013-12-30 07:01:37 +000096
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000097 SkPaint paint;
98 paint.setShader(fShader);
99
100 canvas->translate(20, 20);
101 for (size_t j = 0; j < SK_ARRAY_COUNT(modes); ++j) {
102 SkXfermode* xfer = SkXfermode::Create(modes[j]);
103 canvas->save();
104 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
105 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode,
106 SK_ARRAY_COUNT(fPts), fPts,
107 rec[i].fTexs, rec[i].fColors,
108 xfer, fan, SK_ARRAY_COUNT(fan), paint);
reed@google.com85e143c2013-12-30 15:51:25 +0000109 canvas->translate(200, 0);
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000110 }
111 canvas->restore();
reed@google.com85e143c2013-12-30 15:51:25 +0000112 canvas->translate(0, 200);
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000113 xfer->unref();
114 }
115 }
116
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000117private:
118 typedef skiagm::GM INHERITED;
119};
120
121DEF_GM( return SkNEW(VerticesGM); )