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