blob: bb42fc182b913ae012b9e91f2c548a4c026e0018 [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
reedfd3d87c2015-08-06 05:14:11 -070018typedef void (*DrawAtlasProc)(SkCanvas*, SkImage*, const SkRSXform[], const SkRect[],
19 const SkColor[], int, const SkRect*, const SkPaint*);
20
21static void draw_atlas(SkCanvas* canvas, SkImage* atlas, const SkRSXform xform[],
22 const SkRect tex[], const SkColor colors[], int count, const SkRect* cull,
23 const SkPaint* paint) {
Mike Reed7d954ad2016-10-28 15:42:34 -040024 canvas->drawAtlas(atlas, xform, tex, colors, count, SkBlendMode::kModulate, cull, paint);
reedfd3d87c2015-08-06 05:14:11 -070025}
26
27static void draw_atlas_sim(SkCanvas* canvas, SkImage* atlas, const SkRSXform xform[],
28 const SkRect tex[], const SkColor colors[], int count, const SkRect* cull,
29 const SkPaint* paint) {
30 for (int i = 0; i < count; ++i) {
31 SkMatrix matrix;
32 matrix.setRSXform(xform[i]);
halcanary9d524f22016-03-29 09:03:52 -070033
reedfd3d87c2015-08-06 05:14:11 -070034 canvas->save();
35 canvas->concat(matrix);
reede47829b2015-08-06 10:02:53 -070036 canvas->drawImageRect(atlas, tex[i], tex[i].makeOffset(-tex[i].x(), -tex[i].y()), paint,
reed2dcc7592015-08-06 06:51:52 -070037 SkCanvas::kFast_SrcRectConstraint);
reedfd3d87c2015-08-06 05:14:11 -070038 canvas->restore();
39 }
40}
41
reed9ce9d672016-03-17 10:51:11 -070042static sk_sp<SkImage> make_atlas(int atlasSize, int cellSize) {
reed71c3c762015-06-24 10:29:17 -070043 SkImageInfo info = SkImageInfo::MakeN32Premul(atlasSize, atlasSize);
reede8f30622016-03-23 18:59:25 -070044 auto surface(SkSurface::MakeRaster(info));
reed71c3c762015-06-24 10:29:17 -070045 SkCanvas* canvas = surface->getCanvas();
46
47 SkPaint paint;
48 paint.setAntiAlias(true);
49 SkRandom rand;
50
51 const SkScalar half = cellSize * SK_ScalarHalf;
52 const char* s = "01234567890!@#$%^&*=+<>?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
53 paint.setTextSize(28);
54 paint.setTextAlign(SkPaint::kCenter_Align);
55 int i = 0;
56 for (int y = 0; y < atlasSize; y += cellSize) {
57 for (int x = 0; x < atlasSize; x += cellSize) {
58 paint.setColor(rand.nextU());
59 paint.setAlpha(0xFF);
60 int index = i % strlen(s);
61 canvas->drawText(&s[index], 1, x + half, y + half + half/2, paint);
62 i += 1;
63 }
64 }
reed9ce9d672016-03-17 10:51:11 -070065 return surface->makeImageSnapshot();
reed71c3c762015-06-24 10:29:17 -070066}
67
68class DrawAtlasDrawable : public SkDrawable {
69 enum {
70 kMaxScale = 2,
71 kCellSize = 32,
72 kAtlasSize = 512,
73 };
74
75 struct Rec {
76 SkPoint fCenter;
77 SkVector fVelocity;
78 SkScalar fScale;
79 SkScalar fDScale;
80 SkScalar fRadian;
81 SkScalar fDRadian;
82 SkScalar fAlpha;
83 SkScalar fDAlpha;
84
85 void advance(const SkRect& bounds) {
86 fCenter += fVelocity;
87 if (fCenter.fX > bounds.right()) {
88 SkASSERT(fVelocity.fX > 0);
89 fVelocity.fX = -fVelocity.fX;
90 } else if (fCenter.fX < bounds.left()) {
91 SkASSERT(fVelocity.fX < 0);
92 fVelocity.fX = -fVelocity.fX;
93 }
94 if (fCenter.fY > bounds.bottom()) {
reedca109532015-06-25 16:25:25 -070095 if (fVelocity.fY > 0) {
96 fVelocity.fY = -fVelocity.fY;
97 }
reed71c3c762015-06-24 10:29:17 -070098 } else if (fCenter.fY < bounds.top()) {
reedca109532015-06-25 16:25:25 -070099 if (fVelocity.fY < 0) {
100 fVelocity.fY = -fVelocity.fY;
101 }
reed71c3c762015-06-24 10:29:17 -0700102 }
103
104 fScale += fDScale;
105 if (fScale > 2 || fScale < SK_Scalar1/2) {
106 fDScale = -fDScale;
107 }
108
109 fRadian += fDRadian;
110 fRadian = SkScalarMod(fRadian, 2 * SK_ScalarPI);
111
112 fAlpha += fDAlpha;
113 if (fAlpha > 1) {
114 fAlpha = 1;
115 fDAlpha = -fDAlpha;
116 } else if (fAlpha < 0) {
117 fAlpha = 0;
118 fDAlpha = -fDAlpha;
119 }
120 }
halcanary9d524f22016-03-29 09:03:52 -0700121
reed71c3c762015-06-24 10:29:17 -0700122 SkRSXform asRSXform() const {
reed6b38eab2015-07-30 05:46:05 -0700123 return SkRSXform::MakeFromRadians(fScale, fRadian, fCenter.x(), fCenter.y(),
124 SkScalarHalf(kCellSize), SkScalarHalf(kCellSize));
reed71c3c762015-06-24 10:29:17 -0700125 }
126 };
127
reedfd3d87c2015-08-06 05:14:11 -0700128 DrawAtlasProc fProc;
129
reed71c3c762015-06-24 10:29:17 -0700130 enum {
131 N = 256,
132 };
133
reed9ce9d672016-03-17 10:51:11 -0700134 sk_sp<SkImage> fAtlas;
reed71c3c762015-06-24 10:29:17 -0700135 Rec fRec[N];
136 SkRect fTex[N];
137 SkRect fBounds;
138 bool fUseColors;
139
140public:
reedfd3d87c2015-08-06 05:14:11 -0700141 DrawAtlasDrawable(DrawAtlasProc proc, const SkRect& r)
142 : fProc(proc), fBounds(r), fUseColors(false)
143 {
reed71c3c762015-06-24 10:29:17 -0700144 SkRandom rand;
reed9ce9d672016-03-17 10:51:11 -0700145 fAtlas = make_atlas(kAtlasSize, kCellSize);
reed71c3c762015-06-24 10:29:17 -0700146 const SkScalar kMaxSpeed = 5;
147 const SkScalar cell = SkIntToScalar(kCellSize);
148 int i = 0;
149 for (int y = 0; y < kAtlasSize; y += kCellSize) {
150 for (int x = 0; x < kAtlasSize; x += kCellSize) {
151 const SkScalar sx = SkIntToScalar(x);
152 const SkScalar sy = SkIntToScalar(y);
153 fTex[i].setXYWH(sx, sy, cell, cell);
halcanary9d524f22016-03-29 09:03:52 -0700154
reed71c3c762015-06-24 10:29:17 -0700155 fRec[i].fCenter.set(sx + cell/2, sy + 3*cell/4);
156 fRec[i].fVelocity.fX = rand.nextSScalar1() * kMaxSpeed;
157 fRec[i].fVelocity.fY = rand.nextSScalar1() * kMaxSpeed;
158 fRec[i].fScale = 1;
reed6b38eab2015-07-30 05:46:05 -0700159 fRec[i].fDScale = rand.nextSScalar1() / 16;
reed71c3c762015-06-24 10:29:17 -0700160 fRec[i].fRadian = 0;
161 fRec[i].fDRadian = rand.nextSScalar1() / 8;
162 fRec[i].fAlpha = rand.nextUScalar1();
163 fRec[i].fDAlpha = rand.nextSScalar1() / 10;
164 i += 1;
165 }
166 }
167 }
168
169 void toggleUseColors() {
170 fUseColors = !fUseColors;
171 }
172
173protected:
174 void onDraw(SkCanvas* canvas) override {
175 SkRSXform xform[N];
176 SkColor colors[N];
177
178 for (int i = 0; i < N; ++i) {
179 fRec[i].advance(fBounds);
180 xform[i] = fRec[i].asRSXform();
181 if (fUseColors) {
182 colors[i] = SkColorSetARGB((int)(fRec[i].fAlpha * 0xFF), 0xFF, 0xFF, 0xFF);
183 }
184 }
185 SkPaint paint;
186 paint.setFilterQuality(kLow_SkFilterQuality);
187
188 const SkRect cull = this->getBounds();
halcanary96fcdcc2015-08-27 07:41:13 -0700189 const SkColor* colorsPtr = fUseColors ? colors : nullptr;
reed9ce9d672016-03-17 10:51:11 -0700190 fProc(canvas, fAtlas.get(), xform, fTex, colorsPtr, N, &cull, &paint);
reed71c3c762015-06-24 10:29:17 -0700191 }
halcanary9d524f22016-03-29 09:03:52 -0700192
reed71c3c762015-06-24 10:29:17 -0700193 SkRect onGetBounds() override {
194 const SkScalar border = kMaxScale * kCellSize;
195 SkRect r = fBounds;
196 r.outset(border, border);
197 return r;
198 }
199
200private:
201 typedef SkDrawable INHERITED;
202};
203
204class DrawAtlasView : public SampleView {
reedfd3d87c2015-08-06 05:14:11 -0700205 const char* fName;
206 DrawAtlasDrawable* fDrawable;
reed71c3c762015-06-24 10:29:17 -0700207
208public:
reedfd3d87c2015-08-06 05:14:11 -0700209 DrawAtlasView(const char name[], DrawAtlasProc proc) : fName(name) {
210 fDrawable = new DrawAtlasDrawable(proc, SkRect::MakeWH(640, 480));
reed71c3c762015-06-24 10:29:17 -0700211 }
212
213 ~DrawAtlasView() override {
214 fDrawable->unref();
215 }
216
217protected:
218 bool onQuery(SkEvent* evt) override {
219 if (SampleCode::TitleQ(*evt)) {
reedfd3d87c2015-08-06 05:14:11 -0700220 SampleCode::TitleR(evt, fName);
reed71c3c762015-06-24 10:29:17 -0700221 return true;
222 }
223 SkUnichar uni;
224 if (SampleCode::CharQ(*evt, &uni)) {
225 switch (uni) {
Brian Osmanede860e2017-11-22 16:36:07 -0500226 case 'C': fDrawable->toggleUseColors(); return true;
reed71c3c762015-06-24 10:29:17 -0700227 default: break;
228 }
229 }
230 return this->INHERITED::onQuery(evt);
231 }
232
233 void onDrawContent(SkCanvas* canvas) override {
234 canvas->drawDrawable(fDrawable);
reed71c3c762015-06-24 10:29:17 -0700235 }
236
jvanverthc7027ab2016-06-16 09:52:35 -0700237 bool onAnimate(const SkAnimTimer&) override {
238 return true;
239 }
reed71c3c762015-06-24 10:29:17 -0700240#if 0
241 // TODO: switch over to use this for our animation
242 bool onAnimate(const SkAnimTimer& timer) override {
243 SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360));
244 fAnimatingDrawable->setSweep(angle);
245 return true;
246 }
247#endif
248
249private:
250 typedef SampleView INHERITED;
251};
252
253//////////////////////////////////////////////////////////////////////////////
254
reedfd3d87c2015-08-06 05:14:11 -0700255DEF_SAMPLE( return new DrawAtlasView("DrawAtlas", draw_atlas); )
256DEF_SAMPLE( return new DrawAtlasView("DrawAtlasSim", draw_atlas_sim); )