blob: 54642044a7aa9abf9e5f3a128553601f3e133c26 [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
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04008#include "Sample.h"
reed71c3c762015-06-24 10:29:17 -07009#include "SkAnimTimer.h"
reed71c3c762015-06-24 10:29:17 -070010#include "SkCanvas.h"
11#include "SkDrawable.h"
12#include "SkPath.h"
13#include "SkRandom.h"
14#include "SkRSXform.h"
15#include "SkSurface.h"
Mike Reeda697df92018-10-26 07:28:30 -040016#include "SkTextUtils.h"
reed71c3c762015-06-24 10:29:17 -070017
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;
reed71c3c762015-06-24 10:29:17 -070048 SkRandom rand;
49
50 const SkScalar half = cellSize * SK_ScalarHalf;
51 const char* s = "01234567890!@#$%^&*=+<>?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
Hal Canary4484b8f2019-01-08 14:00:08 -050052 SkFont font(nullptr, 28);
53
reed71c3c762015-06-24 10:29:17 -070054 int i = 0;
55 for (int y = 0; y < atlasSize; y += cellSize) {
56 for (int x = 0; x < atlasSize; x += cellSize) {
57 paint.setColor(rand.nextU());
58 paint.setAlpha(0xFF);
59 int index = i % strlen(s);
Hal Canary4484b8f2019-01-08 14:00:08 -050060 SkTextUtils::Draw(canvas, &s[index], 1, kUTF8_SkTextEncoding,
61 x + half, y + half + half/2, font, paint,
Mike Reedb579f072019-01-03 15:45:53 -050062 SkTextUtils::kCenter_Align);
reed71c3c762015-06-24 10:29:17 -070063 i += 1;
64 }
65 }
reed9ce9d672016-03-17 10:51:11 -070066 return surface->makeImageSnapshot();
reed71c3c762015-06-24 10:29:17 -070067}
68
69class DrawAtlasDrawable : public SkDrawable {
70 enum {
71 kMaxScale = 2,
72 kCellSize = 32,
73 kAtlasSize = 512,
74 };
75
76 struct Rec {
77 SkPoint fCenter;
78 SkVector fVelocity;
79 SkScalar fScale;
80 SkScalar fDScale;
81 SkScalar fRadian;
82 SkScalar fDRadian;
83 SkScalar fAlpha;
84 SkScalar fDAlpha;
85
86 void advance(const SkRect& bounds) {
87 fCenter += fVelocity;
88 if (fCenter.fX > bounds.right()) {
89 SkASSERT(fVelocity.fX > 0);
90 fVelocity.fX = -fVelocity.fX;
91 } else if (fCenter.fX < bounds.left()) {
92 SkASSERT(fVelocity.fX < 0);
93 fVelocity.fX = -fVelocity.fX;
94 }
95 if (fCenter.fY > bounds.bottom()) {
reedca109532015-06-25 16:25:25 -070096 if (fVelocity.fY > 0) {
97 fVelocity.fY = -fVelocity.fY;
98 }
reed71c3c762015-06-24 10:29:17 -070099 } else if (fCenter.fY < bounds.top()) {
reedca109532015-06-25 16:25:25 -0700100 if (fVelocity.fY < 0) {
101 fVelocity.fY = -fVelocity.fY;
102 }
reed71c3c762015-06-24 10:29:17 -0700103 }
104
105 fScale += fDScale;
106 if (fScale > 2 || fScale < SK_Scalar1/2) {
107 fDScale = -fDScale;
108 }
109
110 fRadian += fDRadian;
111 fRadian = SkScalarMod(fRadian, 2 * SK_ScalarPI);
112
113 fAlpha += fDAlpha;
114 if (fAlpha > 1) {
115 fAlpha = 1;
116 fDAlpha = -fDAlpha;
117 } else if (fAlpha < 0) {
118 fAlpha = 0;
119 fDAlpha = -fDAlpha;
120 }
121 }
halcanary9d524f22016-03-29 09:03:52 -0700122
reed71c3c762015-06-24 10:29:17 -0700123 SkRSXform asRSXform() const {
reed6b38eab2015-07-30 05:46:05 -0700124 return SkRSXform::MakeFromRadians(fScale, fRadian, fCenter.x(), fCenter.y(),
125 SkScalarHalf(kCellSize), SkScalarHalf(kCellSize));
reed71c3c762015-06-24 10:29:17 -0700126 }
127 };
128
reedfd3d87c2015-08-06 05:14:11 -0700129 DrawAtlasProc fProc;
130
reed71c3c762015-06-24 10:29:17 -0700131 enum {
132 N = 256,
133 };
134
reed9ce9d672016-03-17 10:51:11 -0700135 sk_sp<SkImage> fAtlas;
reed71c3c762015-06-24 10:29:17 -0700136 Rec fRec[N];
137 SkRect fTex[N];
138 SkRect fBounds;
139 bool fUseColors;
140
141public:
reedfd3d87c2015-08-06 05:14:11 -0700142 DrawAtlasDrawable(DrawAtlasProc proc, const SkRect& r)
143 : fProc(proc), fBounds(r), fUseColors(false)
144 {
reed71c3c762015-06-24 10:29:17 -0700145 SkRandom rand;
reed9ce9d672016-03-17 10:51:11 -0700146 fAtlas = make_atlas(kAtlasSize, kCellSize);
reed71c3c762015-06-24 10:29:17 -0700147 const SkScalar kMaxSpeed = 5;
148 const SkScalar cell = SkIntToScalar(kCellSize);
149 int i = 0;
150 for (int y = 0; y < kAtlasSize; y += kCellSize) {
151 for (int x = 0; x < kAtlasSize; x += kCellSize) {
152 const SkScalar sx = SkIntToScalar(x);
153 const SkScalar sy = SkIntToScalar(y);
154 fTex[i].setXYWH(sx, sy, cell, cell);
halcanary9d524f22016-03-29 09:03:52 -0700155
reed71c3c762015-06-24 10:29:17 -0700156 fRec[i].fCenter.set(sx + cell/2, sy + 3*cell/4);
157 fRec[i].fVelocity.fX = rand.nextSScalar1() * kMaxSpeed;
158 fRec[i].fVelocity.fY = rand.nextSScalar1() * kMaxSpeed;
159 fRec[i].fScale = 1;
reed6b38eab2015-07-30 05:46:05 -0700160 fRec[i].fDScale = rand.nextSScalar1() / 16;
reed71c3c762015-06-24 10:29:17 -0700161 fRec[i].fRadian = 0;
162 fRec[i].fDRadian = rand.nextSScalar1() / 8;
163 fRec[i].fAlpha = rand.nextUScalar1();
164 fRec[i].fDAlpha = rand.nextSScalar1() / 10;
165 i += 1;
166 }
167 }
168 }
169
170 void toggleUseColors() {
171 fUseColors = !fUseColors;
172 }
173
174protected:
175 void onDraw(SkCanvas* canvas) override {
176 SkRSXform xform[N];
177 SkColor colors[N];
178
179 for (int i = 0; i < N; ++i) {
180 fRec[i].advance(fBounds);
181 xform[i] = fRec[i].asRSXform();
182 if (fUseColors) {
183 colors[i] = SkColorSetARGB((int)(fRec[i].fAlpha * 0xFF), 0xFF, 0xFF, 0xFF);
184 }
185 }
186 SkPaint paint;
187 paint.setFilterQuality(kLow_SkFilterQuality);
188
189 const SkRect cull = this->getBounds();
halcanary96fcdcc2015-08-27 07:41:13 -0700190 const SkColor* colorsPtr = fUseColors ? colors : nullptr;
reed9ce9d672016-03-17 10:51:11 -0700191 fProc(canvas, fAtlas.get(), xform, fTex, colorsPtr, N, &cull, &paint);
reed71c3c762015-06-24 10:29:17 -0700192 }
halcanary9d524f22016-03-29 09:03:52 -0700193
reed71c3c762015-06-24 10:29:17 -0700194 SkRect onGetBounds() override {
195 const SkScalar border = kMaxScale * kCellSize;
196 SkRect r = fBounds;
197 r.outset(border, border);
198 return r;
199 }
200
201private:
202 typedef SkDrawable INHERITED;
203};
204
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400205class DrawAtlasView : public Sample {
Ben Wagner4d516112018-06-07 13:11:37 -0400206 const char* fName;
207 DrawAtlasProc fProc;
208 sk_sp<DrawAtlasDrawable> fDrawable;
reed71c3c762015-06-24 10:29:17 -0700209
210public:
Ben Wagner4d516112018-06-07 13:11:37 -0400211 DrawAtlasView(const char name[], DrawAtlasProc proc) : fName(name), fProc(proc) { }
reed71c3c762015-06-24 10:29:17 -0700212
213protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400214 bool onQuery(Sample::Event* evt) override {
215 if (Sample::TitleQ(*evt)) {
216 Sample::TitleR(evt, fName);
reed71c3c762015-06-24 10:29:17 -0700217 return true;
218 }
219 SkUnichar uni;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400220 if (Sample::CharQ(*evt, &uni)) {
reed71c3c762015-06-24 10:29:17 -0700221 switch (uni) {
Brian Osmanede860e2017-11-22 16:36:07 -0500222 case 'C': fDrawable->toggleUseColors(); return true;
reed71c3c762015-06-24 10:29:17 -0700223 default: break;
224 }
225 }
226 return this->INHERITED::onQuery(evt);
227 }
228
Ben Wagner4d516112018-06-07 13:11:37 -0400229 void onOnceBeforeDraw() override {
230 fDrawable = sk_make_sp<DrawAtlasDrawable>(fProc, SkRect::MakeWH(640, 480));
231 }
232
reed71c3c762015-06-24 10:29:17 -0700233 void onDrawContent(SkCanvas* canvas) override {
Ben Wagner4d516112018-06-07 13:11:37 -0400234 canvas->drawDrawable(fDrawable.get());
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:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400250 typedef Sample INHERITED;
reed71c3c762015-06-24 10:29:17 -0700251};
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); )