blob: 31fb3527248b5f38ee4e057a4ab073deffb3a58c [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"
bsalomon093779c2016-03-08 11:53:31 -080010#include "SkColorFilter.h"
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000011#include "SkGradientShader.h"
mike@reedtribe.org2c326b72013-12-30 04:20:38 +000012#include "SkRandom.h"
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000013
bsalomon093779c2016-03-08 11:53:31 -080014static SkShader* make_shader1(SkScalar w, SkScalar h) {
reed@google.com85e143c2013-12-30 15:51:25 +000015 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.org0c87ea82013-12-30 04:07:34 +000020
halcanary96fcdcc2015-08-27 07:41:13 -070021 return SkGradientShader::CreateLinear(pts, colors, nullptr,
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000022 SK_ARRAY_COUNT(colors),
23 SkShader::kMirror_TileMode);
24}
25
bsalomon093779c2016-03-08 11:53:31 -080026static SkShader* make_shader2() {
27 return SkShader::CreateColorShader(SK_ColorBLUE);
28}
29
30static SkColorFilter* make_color_filter() {
31 return SkColorFilter::CreateModeFilter(0xFFAABBCC, SkXfermode::kDarken_Mode);
32}
33
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000034class VerticesGM : public skiagm::GM {
bsalomon093779c2016-03-08 11:53:31 -080035 SkPoint fPts[9];
36 SkPoint fTexs[9];
37 SkColor fColors[9];
38 SkAutoTUnref<SkShader> fShader1;
39 SkAutoTUnref<SkShader> fShader2;
40 SkAutoTUnref<SkColorFilter> fColorFilter;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000041
42public:
bsalomon093779c2016-03-08 11:53:31 -080043 VerticesGM() {}
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000044
45protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000046
mtklein36352bf2015-03-25 18:17:31 -070047 void onOnceBeforeDraw() override {
bsalomon093779c2016-03-08 11:53:31 -080048 const SkScalar X = 30;
49 const SkScalar Y = 30;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000050
bsalomon093779c2016-03-08 11:53:31 -080051 fPts[0].set(0, 0); fPts[1].set(X/2, 3); fPts[2].set(X, 0);
52 fPts[3].set(3, Y/2); fPts[4].set(X/2, Y/2); fPts[5].set(X-3, Y/2);
53 fPts[6].set(0, Y); fPts[7].set(X/2, Y-3); fPts[8].set(X, Y);
reed@google.com85e143c2013-12-30 15:51:25 +000054
bsalomon093779c2016-03-08 11:53:31 -080055 const SkScalar w = 40;
56 const SkScalar h = 40;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000057
58 fTexs[0].set(0, 0); fTexs[1].set(w/2, 0); fTexs[2].set(w, 0);
59 fTexs[3].set(0, h/2); fTexs[4].set(w/2, h/2); fTexs[5].set(w, h/2);
60 fTexs[6].set(0, h); fTexs[7].set(w/2, h); fTexs[8].set(w, h);
61
bsalomon093779c2016-03-08 11:53:31 -080062 fShader1.reset(make_shader1(w, h));
63 fShader2.reset(make_shader2());
64 fColorFilter.reset(make_color_filter());
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000065
66 SkRandom rand;
67 for (size_t i = 0; i < SK_ARRAY_COUNT(fColors); ++i) {
reed@google.com85e143c2013-12-30 15:51:25 +000068 fColors[i] = rand.nextU() | 0xFF000000;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000069 }
70 }
71
mtklein36352bf2015-03-25 18:17:31 -070072 SkString onShortName() override {
reed@google.com60da8f32014-05-05 20:41:21 +000073 SkString name("vertices");
reed@google.com60da8f32014-05-05 20:41:21 +000074 return name;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000075 }
76
mtklein36352bf2015-03-25 18:17:31 -070077 SkISize onISize() override {
reed@google.com85e143c2013-12-30 15:51:25 +000078 return SkISize::Make(600, 600);
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000079 }
80
mtklein36352bf2015-03-25 18:17:31 -070081 void onDraw(SkCanvas* canvas) override {
reed@google.com85e143c2013-12-30 15:51:25 +000082 // start with the center of a 3x3 grid
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000083 static const uint16_t fan[] = {
84 4,
85 0, 1, 2, 5, 8, 7, 6, 3, 0
86 };
87
88 const struct {
89 const SkColor* fColors;
90 const SkPoint* fTexs;
bsalomon093779c2016-03-08 11:53:31 -080091 SkShader* fShader;
92 SkColorFilter* fColorFilter;
93 uint8_t fAlpha;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000094 } rec[] = {
bsalomon093779c2016-03-08 11:53:31 -080095 { fColors, nullptr, fShader1, nullptr , 0xFF },
96 { nullptr, fTexs , fShader1, nullptr , 0xFF },
97 { fColors, fTexs , fShader1, nullptr , 0xFF },
98 { fColors, nullptr, fShader2, nullptr , 0xFF },
99 { nullptr, fTexs , fShader2, nullptr , 0xFF },
100 { fColors, fTexs , fShader2, nullptr , 0xFF },
101 { fColors, nullptr, fShader1, fColorFilter, 0xFF },
102 { nullptr, fTexs , fShader1, fColorFilter, 0xFF },
103 { fColors, fTexs , fShader1, fColorFilter, 0xFF },
104 { fColors, nullptr, fShader2, fColorFilter, 0xFF },
105 { nullptr, fTexs , fShader2, fColorFilter, 0xFF },
106 { fColors, fTexs , fShader2, fColorFilter, 0xFF },
107 { fColors, nullptr, fShader1, nullptr , 0x80 },
108 { nullptr, fTexs , fShader1, nullptr , 0x80 },
109 { fColors, fTexs , fShader1, nullptr , 0x80 },
110 { fColors, nullptr, fShader2, nullptr , 0x80 },
111 { nullptr, fTexs , fShader2, nullptr , 0x80 },
112 { fColors, fTexs , fShader2, nullptr , 0x80 },
113 { fColors, nullptr, fShader1, fColorFilter, 0x80 },
114 { nullptr, fTexs , fShader1, fColorFilter, 0x80 },
115 { fColors, fTexs , fShader1, fColorFilter, 0x80 },
116 { fColors, nullptr, fShader2, fColorFilter, 0x80 },
117 { nullptr, fTexs , fShader2, fColorFilter, 0x80 },
118 { fColors, fTexs , fShader2, fColorFilter, 0x80 },
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000119 };
120
121 const SkXfermode::Mode modes[] = {
bsalomon093779c2016-03-08 11:53:31 -0800122 SkXfermode::kClear_Mode,
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000123 SkXfermode::kSrc_Mode,
124 SkXfermode::kDst_Mode,
bsalomon093779c2016-03-08 11:53:31 -0800125 SkXfermode::kSrcOver_Mode,
126 SkXfermode::kDstOver_Mode,
127 SkXfermode::kSrcIn_Mode,
128 SkXfermode::kDstIn_Mode,
129 SkXfermode::kSrcOut_Mode,
130 SkXfermode::kDstOut_Mode,
131 SkXfermode::kSrcATop_Mode,
132 SkXfermode::kDstATop_Mode,
133 SkXfermode::kXor_Mode,
134 SkXfermode::kPlus_Mode,
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000135 SkXfermode::kModulate_Mode,
bsalomon093779c2016-03-08 11:53:31 -0800136 SkXfermode::kScreen_Mode,
137 SkXfermode::kOverlay_Mode,
138 SkXfermode::kDarken_Mode,
139 SkXfermode::kLighten_Mode,
140 SkXfermode::kColorDodge_Mode,
141 SkXfermode::kColorBurn_Mode,
142 SkXfermode::kHardLight_Mode,
143 SkXfermode::kSoftLight_Mode,
144 SkXfermode::kDifference_Mode,
145 SkXfermode::kExclusion_Mode,
146 SkXfermode::kMultiply_Mode,
147 SkXfermode::kHue_Mode,
148 SkXfermode::kSaturation_Mode,
149 SkXfermode::kColor_Mode,
150 SkXfermode::kLuminosity_Mode,
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000151 };
skia.committer@gmail.com4c912862013-12-30 07:01:37 +0000152
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000153 SkPaint paint;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000154
bsalomon093779c2016-03-08 11:53:31 -0800155 canvas->translate(4, 4);
156 int x = 0;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000157 for (size_t j = 0; j < SK_ARRAY_COUNT(modes); ++j) {
158 SkXfermode* xfer = SkXfermode::Create(modes[j]);
159 canvas->save();
160 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
bsalomon093779c2016-03-08 11:53:31 -0800161 paint.setShader(rec[i].fShader);
162 paint.setColorFilter(rec[i].fColorFilter);
163 paint.setAlpha(rec[i].fAlpha);
164 //if (2 == x)
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000165 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode,
166 SK_ARRAY_COUNT(fPts), fPts,
167 rec[i].fTexs, rec[i].fColors,
168 xfer, fan, SK_ARRAY_COUNT(fan), paint);
bsalomon093779c2016-03-08 11:53:31 -0800169 canvas->translate(40, 0);
170 ++x;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000171 }
172 canvas->restore();
bsalomon093779c2016-03-08 11:53:31 -0800173 canvas->translate(0, 40);
174 SkSafeUnref(xfer);
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000175 }
176 }
177
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000178private:
179 typedef skiagm::GM INHERITED;
180};
181
reed@google.com60da8f32014-05-05 20:41:21 +0000182/////////////////////////////////////////////////////////////////////////////////////
183
bsalomon093779c2016-03-08 11:53:31 -0800184DEF_GM(return new VerticesGM();)