blob: 978d9e69c514d2e7de0a38f0f037729435a82584 [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
reed1a9b9642016-03-13 14:13:58 -070014static sk_sp<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
reed1a9b9642016-03-13 14:13:58 -070021 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
22 SkShader::kMirror_TileMode);
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000023}
24
reed1a9b9642016-03-13 14:13:58 -070025static sk_sp<SkShader> make_shader2() {
26 return SkShader::MakeColorShader(SK_ColorBLUE);
bsalomon093779c2016-03-08 11:53:31 -080027}
28
reedd053ce92016-03-22 10:17:23 -070029static sk_sp<SkColorFilter> make_color_filter() {
30 return SkColorFilter::MakeModeFilter(0xFFAABBCC, SkXfermode::kDarken_Mode);
bsalomon093779c2016-03-08 11:53:31 -080031}
32
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000033class VerticesGM : public skiagm::GM {
reedd053ce92016-03-22 10:17:23 -070034 SkPoint fPts[9];
35 SkPoint fTexs[9];
36 SkColor fColors[9];
37 sk_sp<SkShader> fShader1;
38 sk_sp<SkShader> fShader2;
39 sk_sp<SkColorFilter> fColorFilter;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000040
41public:
bsalomon093779c2016-03-08 11:53:31 -080042 VerticesGM() {}
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000043
44protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000045
mtklein36352bf2015-03-25 18:17:31 -070046 void onOnceBeforeDraw() override {
bsalomon093779c2016-03-08 11:53:31 -080047 const SkScalar X = 30;
48 const SkScalar Y = 30;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000049
bsalomon093779c2016-03-08 11:53:31 -080050 fPts[0].set(0, 0); fPts[1].set(X/2, 3); fPts[2].set(X, 0);
51 fPts[3].set(3, Y/2); fPts[4].set(X/2, Y/2); fPts[5].set(X-3, Y/2);
52 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 +000053
bsalomon093779c2016-03-08 11:53:31 -080054 const SkScalar w = 40;
55 const SkScalar h = 40;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000056
57 fTexs[0].set(0, 0); fTexs[1].set(w/2, 0); fTexs[2].set(w, 0);
58 fTexs[3].set(0, h/2); fTexs[4].set(w/2, h/2); fTexs[5].set(w, h/2);
59 fTexs[6].set(0, h); fTexs[7].set(w/2, h); fTexs[8].set(w, h);
60
reed1a9b9642016-03-13 14:13:58 -070061 fShader1 = make_shader1(w, h);
62 fShader2 = make_shader2();
reedd053ce92016-03-22 10:17:23 -070063 fColorFilter = make_color_filter();
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000064
65 SkRandom rand;
66 for (size_t i = 0; i < SK_ARRAY_COUNT(fColors); ++i) {
reed@google.com85e143c2013-12-30 15:51:25 +000067 fColors[i] = rand.nextU() | 0xFF000000;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000068 }
69 }
70
mtklein36352bf2015-03-25 18:17:31 -070071 SkString onShortName() override {
reed@google.com60da8f32014-05-05 20:41:21 +000072 SkString name("vertices");
reed@google.com60da8f32014-05-05 20:41:21 +000073 return name;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000074 }
75
mtklein36352bf2015-03-25 18:17:31 -070076 SkISize onISize() override {
reed@google.com85e143c2013-12-30 15:51:25 +000077 return SkISize::Make(600, 600);
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000078 }
79
mtklein36352bf2015-03-25 18:17:31 -070080 void onDraw(SkCanvas* canvas) override {
reed@google.com85e143c2013-12-30 15:51:25 +000081 // start with the center of a 3x3 grid
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000082 static const uint16_t fan[] = {
83 4,
84 0, 1, 2, 5, 8, 7, 6, 3, 0
85 };
86
87 const struct {
reedd053ce92016-03-22 10:17:23 -070088 const SkColor* fColors;
89 const SkPoint* fTexs;
90 const sk_sp<SkShader>& fShader;
91 const sk_sp<SkColorFilter>& fColorFilter;
92 uint8_t fAlpha;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000093 } rec[] = {
bsalomon093779c2016-03-08 11:53:31 -080094 { fColors, nullptr, fShader1, nullptr , 0xFF },
95 { nullptr, fTexs , fShader1, nullptr , 0xFF },
96 { fColors, fTexs , fShader1, nullptr , 0xFF },
97 { fColors, nullptr, fShader2, nullptr , 0xFF },
98 { nullptr, fTexs , fShader2, nullptr , 0xFF },
99 { fColors, fTexs , fShader2, nullptr , 0xFF },
100 { fColors, nullptr, fShader1, fColorFilter, 0xFF },
101 { nullptr, fTexs , fShader1, fColorFilter, 0xFF },
102 { fColors, fTexs , fShader1, fColorFilter, 0xFF },
103 { fColors, nullptr, fShader2, fColorFilter, 0xFF },
104 { nullptr, fTexs , fShader2, fColorFilter, 0xFF },
105 { fColors, fTexs , fShader2, fColorFilter, 0xFF },
106 { fColors, nullptr, fShader1, nullptr , 0x80 },
107 { nullptr, fTexs , fShader1, nullptr , 0x80 },
108 { fColors, fTexs , fShader1, nullptr , 0x80 },
109 { fColors, nullptr, fShader2, nullptr , 0x80 },
110 { nullptr, fTexs , fShader2, nullptr , 0x80 },
111 { fColors, fTexs , fShader2, nullptr , 0x80 },
112 { fColors, nullptr, fShader1, fColorFilter, 0x80 },
113 { nullptr, fTexs , fShader1, fColorFilter, 0x80 },
114 { fColors, fTexs , fShader1, fColorFilter, 0x80 },
115 { fColors, nullptr, fShader2, fColorFilter, 0x80 },
116 { nullptr, fTexs , fShader2, fColorFilter, 0x80 },
117 { fColors, fTexs , fShader2, fColorFilter, 0x80 },
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000118 };
119
120 const SkXfermode::Mode modes[] = {
bsalomon093779c2016-03-08 11:53:31 -0800121 SkXfermode::kClear_Mode,
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000122 SkXfermode::kSrc_Mode,
123 SkXfermode::kDst_Mode,
bsalomon093779c2016-03-08 11:53:31 -0800124 SkXfermode::kSrcOver_Mode,
125 SkXfermode::kDstOver_Mode,
126 SkXfermode::kSrcIn_Mode,
127 SkXfermode::kDstIn_Mode,
128 SkXfermode::kSrcOut_Mode,
129 SkXfermode::kDstOut_Mode,
130 SkXfermode::kSrcATop_Mode,
131 SkXfermode::kDstATop_Mode,
132 SkXfermode::kXor_Mode,
133 SkXfermode::kPlus_Mode,
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000134 SkXfermode::kModulate_Mode,
bsalomon093779c2016-03-08 11:53:31 -0800135 SkXfermode::kScreen_Mode,
136 SkXfermode::kOverlay_Mode,
137 SkXfermode::kDarken_Mode,
138 SkXfermode::kLighten_Mode,
139 SkXfermode::kColorDodge_Mode,
140 SkXfermode::kColorBurn_Mode,
141 SkXfermode::kHardLight_Mode,
142 SkXfermode::kSoftLight_Mode,
143 SkXfermode::kDifference_Mode,
144 SkXfermode::kExclusion_Mode,
145 SkXfermode::kMultiply_Mode,
146 SkXfermode::kHue_Mode,
147 SkXfermode::kSaturation_Mode,
148 SkXfermode::kColor_Mode,
149 SkXfermode::kLuminosity_Mode,
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000150 };
skia.committer@gmail.com4c912862013-12-30 07:01:37 +0000151
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000152 SkPaint paint;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000153
bsalomon093779c2016-03-08 11:53:31 -0800154 canvas->translate(4, 4);
155 int x = 0;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000156 for (size_t j = 0; j < SK_ARRAY_COUNT(modes); ++j) {
reedcfb6bdf2016-03-29 11:32:50 -0700157 auto xfer = SkXfermode::Make(modes[j]);
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000158 canvas->save();
159 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
bsalomon093779c2016-03-08 11:53:31 -0800160 paint.setShader(rec[i].fShader);
161 paint.setColorFilter(rec[i].fColorFilter);
162 paint.setAlpha(rec[i].fAlpha);
163 //if (2 == x)
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000164 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode,
165 SK_ARRAY_COUNT(fPts), fPts,
166 rec[i].fTexs, rec[i].fColors,
167 xfer, fan, SK_ARRAY_COUNT(fan), paint);
bsalomon093779c2016-03-08 11:53:31 -0800168 canvas->translate(40, 0);
169 ++x;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000170 }
171 canvas->restore();
bsalomon093779c2016-03-08 11:53:31 -0800172 canvas->translate(0, 40);
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000173 }
174 }
175
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000176private:
177 typedef skiagm::GM INHERITED;
178};
179
reed@google.com60da8f32014-05-05 20:41:21 +0000180/////////////////////////////////////////////////////////////////////////////////////
181
bsalomon093779c2016-03-08 11:53:31 -0800182DEF_GM(return new VerticesGM();)