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