blob: 079d679fb850690a5cb6bb04bcac3e6cf00074f4 [file] [log] [blame]
reed71c3c762015-06-24 10:29:17 -07001/*
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 "SampleCode.h"
9#include "SkAnimTimer.h"
10#include "SkView.h"
11#include "SkCanvas.h"
12#include "SkDrawable.h"
13#include "SkPath.h"
14#include "SkRandom.h"
15#include "SkRSXform.h"
16#include "SkSurface.h"
17
18static SkImage* make_atlas(int atlasSize, int cellSize) {
19 SkImageInfo info = SkImageInfo::MakeN32Premul(atlasSize, atlasSize);
20 SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info));
21 SkCanvas* canvas = surface->getCanvas();
22
23 SkPaint paint;
24 paint.setAntiAlias(true);
25 SkRandom rand;
26
27 const SkScalar half = cellSize * SK_ScalarHalf;
28 const char* s = "01234567890!@#$%^&*=+<>?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
29 paint.setTextSize(28);
30 paint.setTextAlign(SkPaint::kCenter_Align);
31 int i = 0;
32 for (int y = 0; y < atlasSize; y += cellSize) {
33 for (int x = 0; x < atlasSize; x += cellSize) {
34 paint.setColor(rand.nextU());
35 paint.setAlpha(0xFF);
36 int index = i % strlen(s);
37 canvas->drawText(&s[index], 1, x + half, y + half + half/2, paint);
38 i += 1;
39 }
40 }
41 return surface->newImageSnapshot();
42}
43
44class DrawAtlasDrawable : public SkDrawable {
45 enum {
46 kMaxScale = 2,
47 kCellSize = 32,
48 kAtlasSize = 512,
49 };
50
51 struct Rec {
52 SkPoint fCenter;
53 SkVector fVelocity;
54 SkScalar fScale;
55 SkScalar fDScale;
56 SkScalar fRadian;
57 SkScalar fDRadian;
58 SkScalar fAlpha;
59 SkScalar fDAlpha;
60
61 void advance(const SkRect& bounds) {
62 fCenter += fVelocity;
63 if (fCenter.fX > bounds.right()) {
64 SkASSERT(fVelocity.fX > 0);
65 fVelocity.fX = -fVelocity.fX;
66 } else if (fCenter.fX < bounds.left()) {
67 SkASSERT(fVelocity.fX < 0);
68 fVelocity.fX = -fVelocity.fX;
69 }
70 if (fCenter.fY > bounds.bottom()) {
reedca109532015-06-25 16:25:25 -070071 if (fVelocity.fY > 0) {
72 fVelocity.fY = -fVelocity.fY;
73 }
reed71c3c762015-06-24 10:29:17 -070074 } else if (fCenter.fY < bounds.top()) {
reedca109532015-06-25 16:25:25 -070075 if (fVelocity.fY < 0) {
76 fVelocity.fY = -fVelocity.fY;
77 }
reed71c3c762015-06-24 10:29:17 -070078 }
79
80 fScale += fDScale;
81 if (fScale > 2 || fScale < SK_Scalar1/2) {
82 fDScale = -fDScale;
83 }
84
85 fRadian += fDRadian;
86 fRadian = SkScalarMod(fRadian, 2 * SK_ScalarPI);
87
88 fAlpha += fDAlpha;
89 if (fAlpha > 1) {
90 fAlpha = 1;
91 fDAlpha = -fDAlpha;
92 } else if (fAlpha < 0) {
93 fAlpha = 0;
94 fDAlpha = -fDAlpha;
95 }
96 }
97
98 SkRSXform asRSXform() const {
reed6b38eab2015-07-30 05:46:05 -070099 return SkRSXform::MakeFromRadians(fScale, fRadian, fCenter.x(), fCenter.y(),
100 SkScalarHalf(kCellSize), SkScalarHalf(kCellSize));
reed71c3c762015-06-24 10:29:17 -0700101 }
102 };
103
104 enum {
105 N = 256,
106 };
107
108 SkAutoTUnref<SkImage> fAtlas;
109 Rec fRec[N];
110 SkRect fTex[N];
111 SkRect fBounds;
112 bool fUseColors;
113
114public:
115 DrawAtlasDrawable(const SkRect& r) : fBounds(r), fUseColors(false) {
116 SkRandom rand;
117 fAtlas.reset(make_atlas(kAtlasSize, kCellSize));
118 const SkScalar kMaxSpeed = 5;
119 const SkScalar cell = SkIntToScalar(kCellSize);
120 int i = 0;
121 for (int y = 0; y < kAtlasSize; y += kCellSize) {
122 for (int x = 0; x < kAtlasSize; x += kCellSize) {
123 const SkScalar sx = SkIntToScalar(x);
124 const SkScalar sy = SkIntToScalar(y);
125 fTex[i].setXYWH(sx, sy, cell, cell);
126
127 fRec[i].fCenter.set(sx + cell/2, sy + 3*cell/4);
128 fRec[i].fVelocity.fX = rand.nextSScalar1() * kMaxSpeed;
129 fRec[i].fVelocity.fY = rand.nextSScalar1() * kMaxSpeed;
130 fRec[i].fScale = 1;
reed6b38eab2015-07-30 05:46:05 -0700131 fRec[i].fDScale = rand.nextSScalar1() / 16;
reed71c3c762015-06-24 10:29:17 -0700132 fRec[i].fRadian = 0;
133 fRec[i].fDRadian = rand.nextSScalar1() / 8;
134 fRec[i].fAlpha = rand.nextUScalar1();
135 fRec[i].fDAlpha = rand.nextSScalar1() / 10;
136 i += 1;
137 }
138 }
139 }
140
141 void toggleUseColors() {
142 fUseColors = !fUseColors;
143 }
144
145protected:
146 void onDraw(SkCanvas* canvas) override {
147 SkRSXform xform[N];
148 SkColor colors[N];
149
150 for (int i = 0; i < N; ++i) {
151 fRec[i].advance(fBounds);
152 xform[i] = fRec[i].asRSXform();
153 if (fUseColors) {
154 colors[i] = SkColorSetARGB((int)(fRec[i].fAlpha * 0xFF), 0xFF, 0xFF, 0xFF);
155 }
156 }
157 SkPaint paint;
158 paint.setFilterQuality(kLow_SkFilterQuality);
159
160 const SkRect cull = this->getBounds();
161 const SkColor* colorsPtr = fUseColors ? colors : NULL;
162 canvas->drawAtlas(fAtlas, xform, fTex, colorsPtr, N, SkXfermode::kModulate_Mode,
163 &cull, &paint);
164 }
165
166 SkRect onGetBounds() override {
167 const SkScalar border = kMaxScale * kCellSize;
168 SkRect r = fBounds;
169 r.outset(border, border);
170 return r;
171 }
172
173private:
174 typedef SkDrawable INHERITED;
175};
176
177class DrawAtlasView : public SampleView {
178 DrawAtlasDrawable* fDrawable;
179
180public:
181 DrawAtlasView() {
182 fDrawable = new DrawAtlasDrawable(SkRect::MakeWH(640, 480));
183 }
184
185 ~DrawAtlasView() override {
186 fDrawable->unref();
187 }
188
189protected:
190 bool onQuery(SkEvent* evt) override {
191 if (SampleCode::TitleQ(*evt)) {
192 SampleCode::TitleR(evt, "DrawAtlas");
193 return true;
194 }
195 SkUnichar uni;
196 if (SampleCode::CharQ(*evt, &uni)) {
197 switch (uni) {
198 case 'C': fDrawable->toggleUseColors(); this->inval(NULL); return true;
199 default: break;
200 }
201 }
202 return this->INHERITED::onQuery(evt);
203 }
204
205 void onDrawContent(SkCanvas* canvas) override {
206 canvas->drawDrawable(fDrawable);
207 this->inval(NULL);
208 }
209
210#if 0
211 // TODO: switch over to use this for our animation
212 bool onAnimate(const SkAnimTimer& timer) override {
213 SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360));
214 fAnimatingDrawable->setSweep(angle);
215 return true;
216 }
217#endif
218
219private:
220 typedef SampleView INHERITED;
221};
222
223//////////////////////////////////////////////////////////////////////////////
224
225static SkView* MyFactory() { return new DrawAtlasView; }
226static SkViewRegister reg(MyFactory);