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" |
Mike Reed | a697df9 | 2018-10-26 07:28:30 -0400 | [diff] [blame] | 16 | #include "SkTextUtils.h" |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 17 | |
reed | fd3d87c | 2015-08-06 05:14:11 -0700 | [diff] [blame] | 18 | typedef void (*DrawAtlasProc)(SkCanvas*, SkImage*, const SkRSXform[], const SkRect[], |
| 19 | const SkColor[], int, const SkRect*, const SkPaint*); |
| 20 | |
| 21 | static 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 Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 24 | canvas->drawAtlas(atlas, xform, tex, colors, count, SkBlendMode::kModulate, cull, paint); |
reed | fd3d87c | 2015-08-06 05:14:11 -0700 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | static 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]); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 33 | |
reed | fd3d87c | 2015-08-06 05:14:11 -0700 | [diff] [blame] | 34 | canvas->save(); |
| 35 | canvas->concat(matrix); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 36 | 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] | 37 | SkCanvas::kFast_SrcRectConstraint); |
reed | fd3d87c | 2015-08-06 05:14:11 -0700 | [diff] [blame] | 38 | canvas->restore(); |
| 39 | } |
| 40 | } |
| 41 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 42 | static sk_sp<SkImage> make_atlas(int atlasSize, int cellSize) { |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 43 | SkImageInfo info = SkImageInfo::MakeN32Premul(atlasSize, atlasSize); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 44 | auto surface(SkSurface::MakeRaster(info)); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 45 | SkCanvas* canvas = surface->getCanvas(); |
| 46 | |
| 47 | SkPaint paint; |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 48 | SkRandom rand; |
| 49 | |
| 50 | const SkScalar half = cellSize * SK_ScalarHalf; |
| 51 | const char* s = "01234567890!@#$%^&*=+<>?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
Hal Canary | 4484b8f | 2019-01-08 14:00:08 -0500 | [diff] [blame] | 52 | SkFont font(nullptr, 28); |
| 53 | |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 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); |
Hal Canary | 4484b8f | 2019-01-08 14:00:08 -0500 | [diff] [blame] | 60 | SkTextUtils::Draw(canvas, &s[index], 1, kUTF8_SkTextEncoding, |
| 61 | x + half, y + half + half/2, font, paint, |
Mike Reed | b579f07 | 2019-01-03 15:45:53 -0500 | [diff] [blame] | 62 | SkTextUtils::kCenter_Align); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 63 | i += 1; |
| 64 | } |
| 65 | } |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 66 | return surface->makeImageSnapshot(); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | class 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()) { |
reed | ca10953 | 2015-06-25 16:25:25 -0700 | [diff] [blame] | 96 | if (fVelocity.fY > 0) { |
| 97 | fVelocity.fY = -fVelocity.fY; |
| 98 | } |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 99 | } else if (fCenter.fY < bounds.top()) { |
reed | ca10953 | 2015-06-25 16:25:25 -0700 | [diff] [blame] | 100 | if (fVelocity.fY < 0) { |
| 101 | fVelocity.fY = -fVelocity.fY; |
| 102 | } |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 103 | } |
| 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 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 122 | |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 123 | SkRSXform asRSXform() const { |
reed | 6b38eab | 2015-07-30 05:46:05 -0700 | [diff] [blame] | 124 | return SkRSXform::MakeFromRadians(fScale, fRadian, fCenter.x(), fCenter.y(), |
| 125 | SkScalarHalf(kCellSize), SkScalarHalf(kCellSize)); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 126 | } |
| 127 | }; |
| 128 | |
reed | fd3d87c | 2015-08-06 05:14:11 -0700 | [diff] [blame] | 129 | DrawAtlasProc fProc; |
| 130 | |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 131 | enum { |
| 132 | N = 256, |
| 133 | }; |
| 134 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 135 | sk_sp<SkImage> fAtlas; |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 136 | Rec fRec[N]; |
| 137 | SkRect fTex[N]; |
| 138 | SkRect fBounds; |
| 139 | bool fUseColors; |
| 140 | |
| 141 | public: |
reed | fd3d87c | 2015-08-06 05:14:11 -0700 | [diff] [blame] | 142 | DrawAtlasDrawable(DrawAtlasProc proc, const SkRect& r) |
| 143 | : fProc(proc), fBounds(r), fUseColors(false) |
| 144 | { |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 145 | SkRandom rand; |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 146 | fAtlas = make_atlas(kAtlasSize, kCellSize); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 147 | 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); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 155 | |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 156 | 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; |
reed | 6b38eab | 2015-07-30 05:46:05 -0700 | [diff] [blame] | 160 | fRec[i].fDScale = rand.nextSScalar1() / 16; |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 161 | 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 | |
| 174 | protected: |
| 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(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 190 | const SkColor* colorsPtr = fUseColors ? colors : nullptr; |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 191 | fProc(canvas, fAtlas.get(), xform, fTex, colorsPtr, N, &cull, &paint); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 192 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 193 | |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 194 | SkRect onGetBounds() override { |
| 195 | const SkScalar border = kMaxScale * kCellSize; |
| 196 | SkRect r = fBounds; |
| 197 | r.outset(border, border); |
| 198 | return r; |
| 199 | } |
| 200 | |
| 201 | private: |
| 202 | typedef SkDrawable INHERITED; |
| 203 | }; |
| 204 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 205 | class DrawAtlasView : public Sample { |
Ben Wagner | 4d51611 | 2018-06-07 13:11:37 -0400 | [diff] [blame] | 206 | const char* fName; |
| 207 | DrawAtlasProc fProc; |
| 208 | sk_sp<DrawAtlasDrawable> fDrawable; |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 209 | |
| 210 | public: |
Ben Wagner | 4d51611 | 2018-06-07 13:11:37 -0400 | [diff] [blame] | 211 | DrawAtlasView(const char name[], DrawAtlasProc proc) : fName(name), fProc(proc) { } |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 212 | |
| 213 | protected: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 214 | bool onQuery(Sample::Event* evt) override { |
| 215 | if (Sample::TitleQ(*evt)) { |
| 216 | Sample::TitleR(evt, fName); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 217 | return true; |
| 218 | } |
| 219 | SkUnichar uni; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 220 | if (Sample::CharQ(*evt, &uni)) { |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 221 | switch (uni) { |
Brian Osman | ede860e | 2017-11-22 16:36:07 -0500 | [diff] [blame] | 222 | case 'C': fDrawable->toggleUseColors(); return true; |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 223 | default: break; |
| 224 | } |
| 225 | } |
| 226 | return this->INHERITED::onQuery(evt); |
| 227 | } |
| 228 | |
Ben Wagner | 4d51611 | 2018-06-07 13:11:37 -0400 | [diff] [blame] | 229 | void onOnceBeforeDraw() override { |
| 230 | fDrawable = sk_make_sp<DrawAtlasDrawable>(fProc, SkRect::MakeWH(640, 480)); |
| 231 | } |
| 232 | |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 233 | void onDrawContent(SkCanvas* canvas) override { |
Ben Wagner | 4d51611 | 2018-06-07 13:11:37 -0400 | [diff] [blame] | 234 | canvas->drawDrawable(fDrawable.get()); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 235 | } |
| 236 | |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 237 | bool onAnimate(const SkAnimTimer&) override { |
| 238 | return true; |
| 239 | } |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 240 | #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 | |
| 249 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 250 | typedef Sample INHERITED; |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | ////////////////////////////////////////////////////////////////////////////// |
| 254 | |
reed | fd3d87c | 2015-08-06 05:14:11 -0700 | [diff] [blame] | 255 | DEF_SAMPLE( return new DrawAtlasView("DrawAtlas", draw_atlas); ) |
| 256 | DEF_SAMPLE( return new DrawAtlasView("DrawAtlasSim", draw_atlas_sim); ) |