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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkDrawable.h" |
| 10 | #include "include/core/SkPath.h" |
| 11 | #include "include/core/SkRSXform.h" |
| 12 | #include "include/core/SkSurface.h" |
| 13 | #include "include/utils/SkRandom.h" |
| 14 | #include "include/utils/SkTextUtils.h" |
| 15 | #include "samplecode/Sample.h" |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 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; |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 47 | SkRandom rand; |
| 48 | |
| 49 | const SkScalar half = cellSize * SK_ScalarHalf; |
| 50 | const char* s = "01234567890!@#$%^&*=+<>?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
Hal Canary | 4484b8f | 2019-01-08 14:00:08 -0500 | [diff] [blame] | 51 | SkFont font(nullptr, 28); |
| 52 | |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 53 | int i = 0; |
| 54 | for (int y = 0; y < atlasSize; y += cellSize) { |
| 55 | for (int x = 0; x < atlasSize; x += cellSize) { |
| 56 | paint.setColor(rand.nextU()); |
| 57 | paint.setAlpha(0xFF); |
| 58 | int index = i % strlen(s); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 59 | SkTextUtils::Draw(canvas, &s[index], 1, SkTextEncoding::kUTF8, |
Hal Canary | 4484b8f | 2019-01-08 14:00:08 -0500 | [diff] [blame] | 60 | x + half, y + half + half/2, font, paint, |
Mike Reed | b579f07 | 2019-01-03 15:45:53 -0500 | [diff] [blame] | 61 | SkTextUtils::kCenter_Align); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 62 | i += 1; |
| 63 | } |
| 64 | } |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 65 | return surface->makeImageSnapshot(); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | class 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()) { |
reed | ca10953 | 2015-06-25 16:25:25 -0700 | [diff] [blame] | 95 | if (fVelocity.fY > 0) { |
| 96 | fVelocity.fY = -fVelocity.fY; |
| 97 | } |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 98 | } else if (fCenter.fY < bounds.top()) { |
reed | ca10953 | 2015-06-25 16:25:25 -0700 | [diff] [blame] | 99 | if (fVelocity.fY < 0) { |
| 100 | fVelocity.fY = -fVelocity.fY; |
| 101 | } |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 102 | } |
| 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 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 121 | |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 122 | SkRSXform asRSXform() const { |
reed | 6b38eab | 2015-07-30 05:46:05 -0700 | [diff] [blame] | 123 | return SkRSXform::MakeFromRadians(fScale, fRadian, fCenter.x(), fCenter.y(), |
| 124 | SkScalarHalf(kCellSize), SkScalarHalf(kCellSize)); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 125 | } |
| 126 | }; |
| 127 | |
reed | fd3d87c | 2015-08-06 05:14:11 -0700 | [diff] [blame] | 128 | DrawAtlasProc fProc; |
| 129 | |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 130 | enum { |
| 131 | N = 256, |
| 132 | }; |
| 133 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 134 | sk_sp<SkImage> fAtlas; |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 135 | Rec fRec[N]; |
| 136 | SkRect fTex[N]; |
| 137 | SkRect fBounds; |
| 138 | bool fUseColors; |
| 139 | |
| 140 | public: |
reed | fd3d87c | 2015-08-06 05:14:11 -0700 | [diff] [blame] | 141 | DrawAtlasDrawable(DrawAtlasProc proc, const SkRect& r) |
| 142 | : fProc(proc), fBounds(r), fUseColors(false) |
| 143 | { |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 144 | SkRandom rand; |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 145 | fAtlas = make_atlas(kAtlasSize, kCellSize); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 146 | 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); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 154 | |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 155 | 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; |
reed | 6b38eab | 2015-07-30 05:46:05 -0700 | [diff] [blame] | 159 | fRec[i].fDScale = rand.nextSScalar1() / 16; |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 160 | 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 | |
| 173 | protected: |
| 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(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 189 | const SkColor* colorsPtr = fUseColors ? colors : nullptr; |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 190 | fProc(canvas, fAtlas.get(), xform, fTex, colorsPtr, N, &cull, &paint); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 191 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 192 | |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 193 | SkRect onGetBounds() override { |
| 194 | const SkScalar border = kMaxScale * kCellSize; |
| 195 | SkRect r = fBounds; |
| 196 | r.outset(border, border); |
| 197 | return r; |
| 198 | } |
| 199 | |
| 200 | private: |
| 201 | typedef SkDrawable INHERITED; |
| 202 | }; |
| 203 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 204 | class DrawAtlasView : public Sample { |
Ben Wagner | 4d51611 | 2018-06-07 13:11:37 -0400 | [diff] [blame] | 205 | const char* fName; |
| 206 | DrawAtlasProc fProc; |
| 207 | sk_sp<DrawAtlasDrawable> fDrawable; |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 208 | |
| 209 | public: |
Ben Wagner | 4d51611 | 2018-06-07 13:11:37 -0400 | [diff] [blame] | 210 | DrawAtlasView(const char name[], DrawAtlasProc proc) : fName(name), fProc(proc) { } |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 211 | |
| 212 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 213 | SkString name() override { return SkString(fName); } |
| 214 | |
Hal Canary | 6cc65e1 | 2019-07-03 15:53:04 -0400 | [diff] [blame] | 215 | bool onChar(SkUnichar uni) override { |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 216 | switch (uni) { |
Brian Osman | ede860e | 2017-11-22 16:36:07 -0500 | [diff] [blame] | 217 | case 'C': fDrawable->toggleUseColors(); return true; |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 218 | default: break; |
| 219 | } |
Hal Canary | 6cc65e1 | 2019-07-03 15:53:04 -0400 | [diff] [blame] | 220 | return false; |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Ben Wagner | 4d51611 | 2018-06-07 13:11:37 -0400 | [diff] [blame] | 223 | void onOnceBeforeDraw() override { |
| 224 | fDrawable = sk_make_sp<DrawAtlasDrawable>(fProc, SkRect::MakeWH(640, 480)); |
| 225 | } |
| 226 | |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 227 | void onDrawContent(SkCanvas* canvas) override { |
Ben Wagner | 4d51611 | 2018-06-07 13:11:37 -0400 | [diff] [blame] | 228 | canvas->drawDrawable(fDrawable.get()); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 231 | bool onAnimate(double /*nanos*/) override { return true; } |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 232 | #if 0 |
| 233 | // TODO: switch over to use this for our animation |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 234 | bool onAnimate(double nanos) override { |
| 235 | SkScalar angle = SkDoubleToScalar(fmod(1e-9 * nanos * 360 / 24, 360)); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 236 | fAnimatingDrawable->setSweep(angle); |
| 237 | return true; |
| 238 | } |
| 239 | #endif |
| 240 | |
| 241 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 242 | typedef Sample INHERITED; |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 243 | }; |
| 244 | |
| 245 | ////////////////////////////////////////////////////////////////////////////// |
| 246 | |
reed | fd3d87c | 2015-08-06 05:14:11 -0700 | [diff] [blame] | 247 | DEF_SAMPLE( return new DrawAtlasView("DrawAtlas", draw_atlas); ) |
| 248 | DEF_SAMPLE( return new DrawAtlasView("DrawAtlasSim", draw_atlas_sim); ) |