1 /*
2 * Copyright 2015 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 "include/core/SkCanvas.h"
9 #include "include/core/SkDrawable.h"
10 #include "include/core/SkPath.h"
11 #include "include/core/SkRSXform.h"
12 #include "include/core/SkSurface.h"
13 #include "include/utils/SkRandom.h"
14 #include "include/utils/SkTextUtils.h"
15 #include "samplecode/Sample.h"
16 #include "src/core/SkPaintPriv.h"
17
18 typedef void (*DrawAtlasProc)(SkCanvas*, SkImage*, const SkRSXform[], const SkRect[],
19 const SkColor[], int, const SkRect*, const SkSamplingOptions&,
20 const SkPaint*);
21
draw_atlas(SkCanvas * canvas,SkImage * atlas,const SkRSXform xform[],const SkRect tex[],const SkColor colors[],int count,const SkRect * cull,const SkSamplingOptions & sampling,const SkPaint * paint)22 static void draw_atlas(SkCanvas* canvas, SkImage* atlas, const SkRSXform xform[],
23 const SkRect tex[], const SkColor colors[], int count, const SkRect* cull,
24 const SkSamplingOptions& sampling, const SkPaint* paint) {
25 canvas->drawAtlas(atlas, xform, tex, colors, count, SkBlendMode::kModulate,
26 sampling, cull, paint);
27 }
28
draw_atlas_sim(SkCanvas * canvas,SkImage * atlas,const SkRSXform xform[],const SkRect tex[],const SkColor colors[],int count,const SkRect * cull,const SkSamplingOptions & sampling,const SkPaint * paint)29 static void draw_atlas_sim(SkCanvas* canvas, SkImage* atlas, const SkRSXform xform[],
30 const SkRect tex[], const SkColor colors[], int count, const SkRect* cull,
31 const SkSamplingOptions& sampling, const SkPaint* paint) {
32 for (int i = 0; i < count; ++i) {
33 SkMatrix matrix;
34 matrix.setRSXform(xform[i]);
35
36 canvas->save();
37 canvas->concat(matrix);
38 canvas->drawImageRect(atlas, tex[i], tex[i].makeOffset(-tex[i].x(), -tex[i].y()),
39 sampling, paint, SkCanvas::kFast_SrcRectConstraint);
40 canvas->restore();
41 }
42 }
43
make_atlas(int atlasSize,int cellSize)44 static sk_sp<SkImage> make_atlas(int atlasSize, int cellSize) {
45 SkImageInfo info = SkImageInfo::MakeN32Premul(atlasSize, atlasSize);
46 auto surface(SkSurface::MakeRaster(info));
47 SkCanvas* canvas = surface->getCanvas();
48
49 SkPaint paint;
50 SkRandom rand;
51
52 const SkScalar half = cellSize * SK_ScalarHalf;
53 const char* s = "01234567890!@#$%^&*=+<>?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
54 SkFont font(nullptr, 28);
55
56 int i = 0;
57 for (int y = 0; y < atlasSize; y += cellSize) {
58 for (int x = 0; x < atlasSize; x += cellSize) {
59 paint.setColor(rand.nextU());
60 paint.setAlpha(0xFF);
61 int index = i % strlen(s);
62 SkTextUtils::Draw(canvas, &s[index], 1, SkTextEncoding::kUTF8,
63 x + half, y + half + half/2, font, paint,
64 SkTextUtils::kCenter_Align);
65 i += 1;
66 }
67 }
68 return surface->makeImageSnapshot();
69 }
70
71 class DrawAtlasDrawable : public SkDrawable {
72 enum {
73 kMaxScale = 2,
74 kCellSize = 32,
75 kAtlasSize = 512,
76 };
77
78 struct Rec {
79 SkPoint fCenter;
80 SkVector fVelocity;
81 SkScalar fScale;
82 SkScalar fDScale;
83 SkScalar fRadian;
84 SkScalar fDRadian;
85 SkScalar fAlpha;
86 SkScalar fDAlpha;
87
advanceDrawAtlasDrawable::Rec88 void advance(const SkRect& bounds) {
89 fCenter += fVelocity;
90 if (fCenter.fX > bounds.right()) {
91 SkASSERT(fVelocity.fX > 0);
92 fVelocity.fX = -fVelocity.fX;
93 } else if (fCenter.fX < bounds.left()) {
94 SkASSERT(fVelocity.fX < 0);
95 fVelocity.fX = -fVelocity.fX;
96 }
97 if (fCenter.fY > bounds.bottom()) {
98 if (fVelocity.fY > 0) {
99 fVelocity.fY = -fVelocity.fY;
100 }
101 } else if (fCenter.fY < bounds.top()) {
102 if (fVelocity.fY < 0) {
103 fVelocity.fY = -fVelocity.fY;
104 }
105 }
106
107 fScale += fDScale;
108 if (fScale > 2 || fScale < SK_Scalar1/2) {
109 fDScale = -fDScale;
110 }
111
112 fRadian += fDRadian;
113 fRadian = SkScalarMod(fRadian, 2 * SK_ScalarPI);
114
115 fAlpha += fDAlpha;
116 if (fAlpha > 1) {
117 fAlpha = 1;
118 fDAlpha = -fDAlpha;
119 } else if (fAlpha < 0) {
120 fAlpha = 0;
121 fDAlpha = -fDAlpha;
122 }
123 }
124
asRSXformDrawAtlasDrawable::Rec125 SkRSXform asRSXform() const {
126 return SkRSXform::MakeFromRadians(fScale, fRadian, fCenter.x(), fCenter.y(),
127 SkScalarHalf(kCellSize), SkScalarHalf(kCellSize));
128 }
129 };
130
131 DrawAtlasProc fProc;
132
133 enum {
134 N = 256,
135 };
136
137 sk_sp<SkImage> fAtlas;
138 Rec fRec[N];
139 SkRect fTex[N];
140 SkRect fBounds;
141 bool fUseColors;
142
143 public:
DrawAtlasDrawable(DrawAtlasProc proc,const SkRect & r)144 DrawAtlasDrawable(DrawAtlasProc proc, const SkRect& r)
145 : fProc(proc), fBounds(r), fUseColors(false)
146 {
147 SkRandom rand;
148 fAtlas = make_atlas(kAtlasSize, kCellSize);
149 const SkScalar kMaxSpeed = 5;
150 const SkScalar cell = SkIntToScalar(kCellSize);
151 int i = 0;
152 for (int y = 0; y < kAtlasSize; y += kCellSize) {
153 for (int x = 0; x < kAtlasSize; x += kCellSize) {
154 const SkScalar sx = SkIntToScalar(x);
155 const SkScalar sy = SkIntToScalar(y);
156 fTex[i].setXYWH(sx, sy, cell, cell);
157
158 fRec[i].fCenter.set(sx + cell/2, sy + 3*cell/4);
159 fRec[i].fVelocity.fX = rand.nextSScalar1() * kMaxSpeed;
160 fRec[i].fVelocity.fY = rand.nextSScalar1() * kMaxSpeed;
161 fRec[i].fScale = 1;
162 fRec[i].fDScale = rand.nextSScalar1() / 16;
163 fRec[i].fRadian = 0;
164 fRec[i].fDRadian = rand.nextSScalar1() / 8;
165 fRec[i].fAlpha = rand.nextUScalar1();
166 fRec[i].fDAlpha = rand.nextSScalar1() / 10;
167 i += 1;
168 }
169 }
170 }
171
toggleUseColors()172 void toggleUseColors() {
173 fUseColors = !fUseColors;
174 }
175
176 protected:
onDraw(SkCanvas * canvas)177 void onDraw(SkCanvas* canvas) override {
178 SkRSXform xform[N];
179 SkColor colors[N];
180
181 for (int i = 0; i < N; ++i) {
182 fRec[i].advance(fBounds);
183 xform[i] = fRec[i].asRSXform();
184 if (fUseColors) {
185 colors[i] = SkColorSetARGB((int)(fRec[i].fAlpha * 0xFF), 0xFF, 0xFF, 0xFF);
186 }
187 }
188 SkPaint paint;
189 SkSamplingOptions sampling(SkFilterMode::kLinear);
190
191 const SkRect cull = this->getBounds();
192 const SkColor* colorsPtr = fUseColors ? colors : nullptr;
193 fProc(canvas, fAtlas.get(), xform, fTex, colorsPtr, N, &cull, sampling, &paint);
194 }
195
onGetBounds()196 SkRect onGetBounds() override {
197 const SkScalar border = kMaxScale * kCellSize;
198 SkRect r = fBounds;
199 r.outset(border, border);
200 return r;
201 }
202
203 private:
204 using INHERITED = SkDrawable;
205 };
206
207 class DrawAtlasView : public Sample {
208 const char* fName;
209 DrawAtlasProc fProc;
210 sk_sp<DrawAtlasDrawable> fDrawable;
211
212 public:
DrawAtlasView(const char name[],DrawAtlasProc proc)213 DrawAtlasView(const char name[], DrawAtlasProc proc) : fName(name), fProc(proc) { }
214
215 protected:
name()216 SkString name() override { return SkString(fName); }
217
onChar(SkUnichar uni)218 bool onChar(SkUnichar uni) override {
219 switch (uni) {
220 case 'C': fDrawable->toggleUseColors(); return true;
221 default: break;
222 }
223 return false;
224 }
225
onOnceBeforeDraw()226 void onOnceBeforeDraw() override {
227 fDrawable = sk_make_sp<DrawAtlasDrawable>(fProc, SkRect::MakeWH(640, 480));
228 }
229
onDrawContent(SkCanvas * canvas)230 void onDrawContent(SkCanvas* canvas) override {
231 canvas->drawDrawable(fDrawable.get());
232 }
233
onAnimate(double)234 bool onAnimate(double /*nanos*/) override { return true; }
235 #if 0
236 // TODO: switch over to use this for our animation
237 bool onAnimate(double nanos) override {
238 SkScalar angle = SkDoubleToScalar(fmod(1e-9 * nanos * 360 / 24, 360));
239 fAnimatingDrawable->setSweep(angle);
240 return true;
241 }
242 #endif
243
244 private:
245 using INHERITED = Sample;
246 };
247
248 //////////////////////////////////////////////////////////////////////////////
249
250 DEF_SAMPLE( return new DrawAtlasView("DrawAtlas", draw_atlas); )
251 DEF_SAMPLE( return new DrawAtlasView("DrawAtlasSim", draw_atlas_sim); )
252