blob: c0d2075db1d3f18685da4528d5f1269af6bf9829 [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:
40 virtual void onOnceBeforeDraw() SK_OVERRIDE {
reed@google.com85e143c2013-12-30 15:51:25 +000041 const SkScalar X = 150;
42 const SkScalar Y = 150;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000043
reed@google.com85e143c2013-12-30 15:51:25 +000044 fPts[0].set(0, 0); fPts[1].set(X/2, 10); fPts[2].set(X, 0);
45 fPts[3].set(10, Y/2); fPts[4].set(X/2, Y/2); fPts[5].set(X-10, Y/2);
46 fPts[6].set(0, Y); fPts[7].set(X/2, Y-10); fPts[8].set(X, Y);
47
48 const SkScalar w = 200;
49 const SkScalar h = 200;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000050
51 fTexs[0].set(0, 0); fTexs[1].set(w/2, 0); fTexs[2].set(w, 0);
52 fTexs[3].set(0, h/2); fTexs[4].set(w/2, h/2); fTexs[5].set(w, h/2);
53 fTexs[6].set(0, h); fTexs[7].set(w/2, h); fTexs[8].set(w, h);
54
55 fShader = make_shader(w, h);
56
57 SkRandom rand;
58 for (size_t i = 0; i < SK_ARRAY_COUNT(fColors); ++i) {
reed@google.com85e143c2013-12-30 15:51:25 +000059 fColors[i] = rand.nextU() | 0xFF000000;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000060 }
61 }
62
63 virtual SkString onShortName() SK_OVERRIDE {
64 return SkString("vertices");
65 }
66
67 virtual SkISize onISize() SK_OVERRIDE {
reed@google.com85e143c2013-12-30 15:51:25 +000068 return SkISize::Make(600, 600);
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000069 }
70
71 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
reed@google.com85e143c2013-12-30 15:51:25 +000072 // start with the center of a 3x3 grid
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000073 static const uint16_t fan[] = {
74 4,
75 0, 1, 2, 5, 8, 7, 6, 3, 0
76 };
77
78 const struct {
79 const SkColor* fColors;
80 const SkPoint* fTexs;
81 } rec[] = {
82 { fColors, NULL },
83 { NULL, fTexs },
84 { fColors, fTexs },
85 };
86
87 const SkXfermode::Mode modes[] = {
88 SkXfermode::kSrc_Mode,
89 SkXfermode::kDst_Mode,
90 SkXfermode::kModulate_Mode,
91 };
skia.committer@gmail.com4c912862013-12-30 07:01:37 +000092
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000093 SkPaint paint;
94 paint.setShader(fShader);
95
96 canvas->translate(20, 20);
97 for (size_t j = 0; j < SK_ARRAY_COUNT(modes); ++j) {
98 SkXfermode* xfer = SkXfermode::Create(modes[j]);
99 canvas->save();
100 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
101 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode,
102 SK_ARRAY_COUNT(fPts), fPts,
103 rec[i].fTexs, rec[i].fColors,
104 xfer, fan, SK_ARRAY_COUNT(fan), paint);
reed@google.com85e143c2013-12-30 15:51:25 +0000105 canvas->translate(200, 0);
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000106 }
107 canvas->restore();
reed@google.com85e143c2013-12-30 15:51:25 +0000108 canvas->translate(0, 200);
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000109 xfer->unref();
110 }
111 }
112
reed@google.com85e143c2013-12-30 15:51:25 +0000113#if 0
mike@reedtribe.org2c326b72013-12-30 04:20:38 +0000114 virtual uint32_t onGetFlags() const {
mike@reedtribe.org5f14dfd2013-12-30 04:27:22 +0000115 return kSkipPipe_Flag | kSkipPicture_Flag;
mike@reedtribe.org2c326b72013-12-30 04:20:38 +0000116 }
reed@google.com85e143c2013-12-30 15:51:25 +0000117#endif
skia.committer@gmail.com4c912862013-12-30 07:01:37 +0000118
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000119private:
120 typedef skiagm::GM INHERITED;
121};
122
123DEF_GM( return SkNEW(VerticesGM); )