• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 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 #include "include/core/SkBitmap.h"
8 #include "include/core/SkCanvas.h"
9 #include "include/core/SkColorFilter.h"
10 #include "include/core/SkColorPriv.h"
11 #include "include/core/SkGraphics.h"
12 #include "include/core/SkPath.h"
13 #include "include/core/SkRegion.h"
14 #include "include/core/SkShader.h"
15 #include "include/core/SkTime.h"
16 #include "include/core/SkTypeface.h"
17 #include "include/core/SkVertices.h"
18 #include "include/effects/SkGradientShader.h"
19 #include "include/utils/SkRandom.h"
20 #include "samplecode/Sample.h"
21 #include "src/utils/SkUTF.h"
22 
23 #include "include/core/SkStream.h"
24 #include "src/core/SkOSFile.h"
25 
make_shader0(SkIPoint * size)26 static sk_sp<SkShader> make_shader0(SkIPoint* size) {
27     SkBitmap    bm;
28     size->set(2, 2);
29     SkPMColor color0 = SkPreMultiplyARGB(0x80, 0x80, 0xff, 0x80);
30     SkPMColor color1 = SkPreMultiplyARGB(0x40, 0xff, 0x00, 0xff);
31     bm.allocN32Pixels(size->fX, size->fY);
32     bm.eraseColor(color0);
33     uint32_t* pixels = (uint32_t*) bm.getPixels();
34     pixels[0] = pixels[2] = color0;
35     pixels[1] = pixels[3] = color1;
36 
37     return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat);
38 }
39 
make_shader1(const SkIPoint & size)40 static sk_sp<SkShader> make_shader1(const SkIPoint& size) {
41     SkPoint pts[] = { { 0, 0 },
42                       { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } };
43     SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED };
44     return SkGradientShader::MakeLinear(pts, colors, nullptr,
45                     SK_ARRAY_COUNT(colors), SkTileMode::kMirror);
46 }
47 
48 class VerticesView : public Sample {
49     sk_sp<SkShader> fShader0;
50     sk_sp<SkShader> fShader1;
51 
52 public:
VerticesView()53     VerticesView() {
54         SkIPoint    size;
55 
56         fShader0 = make_shader0(&size);
57         fShader1 = make_shader1(size);
58 
59         make_strip(&fRecs[0], size.fX, size.fY);
60         make_fan(&fRecs[1], size.fX, size.fY);
61         make_tris(&fRecs[2]);
62 
63         fScale = SK_Scalar1;
64 
65         this->setBGColor(SK_ColorGRAY);
66     }
67 
68 protected:
name()69     SkString name() override { return SkString("Vertices"); }
70 
71     SkScalar fScale;
72 
onDrawContent(SkCanvas * canvas)73     void onDrawContent(SkCanvas* canvas) override {
74         SkPaint paint;
75         paint.setDither(true);
76         paint.setFilterQuality(kLow_SkFilterQuality);
77 
78         for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) {
79             auto verts = SkVertices::MakeCopy(fRecs[i].fMode, fRecs[i].fCount,
80                                               fRecs[i].fVerts, fRecs[i].fTexs,
81                                               nullptr);
82             canvas->save();
83 
84             paint.setShader(nullptr);
85             canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
86 
87             canvas->translate(SkIntToScalar(250), 0);
88 
89             paint.setShader(fShader0);
90             canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
91 
92             canvas->translate(SkIntToScalar(250), 0);
93 
94             paint.setShader(fShader1);
95             canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
96             canvas->restore();
97 
98             canvas->translate(0, SkIntToScalar(250));
99         }
100     }
101 
onFindClickHandler(SkScalar x,SkScalar y,ModifierKey)102     Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, ModifierKey) override {
103         return new Click();
104     }
105 
onClick(Click * click)106     bool onClick(Click* click) override {
107     //    fCurrX = click->fICurr.fX;
108     //    fCurrY = click->fICurr.fY;
109         return true;
110     }
111 
112 private:
113     struct Rec {
114         SkVertices::VertexMode  fMode;
115         int                     fCount;
116         SkPoint*                fVerts;
117         SkPoint*                fTexs;
118 
RecVerticesView::Rec119         Rec() : fCount(0), fVerts(nullptr), fTexs(nullptr) {}
~RecVerticesView::Rec120         ~Rec() { delete[] fVerts; delete[] fTexs; }
121     };
122 
make_tris(Rec * rec)123     void make_tris(Rec* rec) {
124         int n = 10;
125         SkRandom    rand;
126 
127         rec->fMode = SkVertices::kTriangles_VertexMode;
128         rec->fCount = n * 3;
129         rec->fVerts = new SkPoint[rec->fCount];
130 
131         for (int i = 0; i < n; i++) {
132             SkPoint* v = &rec->fVerts[i*3];
133             for (int j = 0; j < 3; j++) {
134                 v[j].set(rand.nextUScalar1() * 250, rand.nextUScalar1() * 250);
135             }
136         }
137     }
138 
make_fan(Rec * rec,int texWidth,int texHeight)139     void make_fan(Rec* rec, int texWidth, int texHeight) {
140         const SkScalar tx = SkIntToScalar(texWidth);
141         const SkScalar ty = SkIntToScalar(texHeight);
142         const int n = 24;
143 
144         rec->fMode = SkVertices::kTriangleFan_VertexMode;
145         rec->fCount = n + 2;
146         rec->fVerts = new SkPoint[rec->fCount];
147         rec->fTexs  = new SkPoint[rec->fCount];
148 
149         SkPoint* v = rec->fVerts;
150         SkPoint* t = rec->fTexs;
151 
152         v[0].set(0, 0);
153         t[0].set(0, 0);
154         for (int i = 0; i < n; i++) {
155             SkScalar r   = SK_ScalarPI * 2 * i / n,
156                      sin = SkScalarSin(r),
157                      cos = SkScalarCos(r);
158             v[i+1].set(cos, sin);
159             t[i+1].set(i*tx/n, ty);
160         }
161         v[n+1] = v[1];
162         t[n+1].set(tx, ty);
163 
164         SkMatrix m;
165         m.setScale(SkIntToScalar(100), SkIntToScalar(100));
166         m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
167         m.mapPoints(v, rec->fCount);
168     }
169 
make_strip(Rec * rec,int texWidth,int texHeight)170     void make_strip(Rec* rec, int texWidth, int texHeight) {
171         const SkScalar tx = SkIntToScalar(texWidth);
172         const SkScalar ty = SkIntToScalar(texHeight);
173         const int n = 24;
174 
175         rec->fMode = SkVertices::kTriangleStrip_VertexMode;
176         rec->fCount = 2 * (n + 1);
177         rec->fVerts = new SkPoint[rec->fCount];
178         rec->fTexs  = new SkPoint[rec->fCount];
179 
180         SkPoint* v = rec->fVerts;
181         SkPoint* t = rec->fTexs;
182 
183         for (int i = 0; i < n; i++) {
184             SkScalar r   = SK_ScalarPI * 2 * i / n,
185                      sin = SkScalarSin(r),
186                      cos = SkScalarCos(r);
187             v[i*2 + 0].set(cos/2, sin/2);
188             v[i*2 + 1].set(cos, sin);
189 
190             t[i*2 + 0].set(tx * i / n, ty);
191             t[i*2 + 1].set(tx * i / n, 0);
192         }
193         v[2*n + 0] = v[0];
194         v[2*n + 1] = v[1];
195 
196         t[2*n + 0].set(tx, ty);
197         t[2*n + 1].set(tx, 0);
198 
199         SkMatrix m;
200         m.setScale(SkIntToScalar(100), SkIntToScalar(100));
201         m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
202         m.mapPoints(v, rec->fCount);
203     }
204 
205     Rec fRecs[3];
206 
207     typedef Sample INHERITED;
208 };
209 
210 //////////////////////////////////////////////////////////////////////////////
211 
212 DEF_SAMPLE( return new VerticesView(); )
213