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 | |
| 8 | #include "SampleCode.h" |
| 9 | #include "SkAnimTimer.h" |
| 10 | #include "SkView.h" |
| 11 | #include "SkCanvas.h" |
| 12 | #include "SkDrawable.h" |
| 13 | #include "SkPath.h" |
| 14 | #include "SkRandom.h" |
| 15 | #include "SkRSXform.h" |
| 16 | #include "SkSurface.h" |
| 17 | |
| 18 | static SkImage* make_atlas(int atlasSize, int cellSize) { |
| 19 | SkImageInfo info = SkImageInfo::MakeN32Premul(atlasSize, atlasSize); |
| 20 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info)); |
| 21 | SkCanvas* canvas = surface->getCanvas(); |
| 22 | |
| 23 | SkPaint paint; |
| 24 | paint.setAntiAlias(true); |
| 25 | SkRandom rand; |
| 26 | |
| 27 | const SkScalar half = cellSize * SK_ScalarHalf; |
| 28 | const char* s = "01234567890!@#$%^&*=+<>?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 29 | paint.setTextSize(28); |
| 30 | paint.setTextAlign(SkPaint::kCenter_Align); |
| 31 | int i = 0; |
| 32 | for (int y = 0; y < atlasSize; y += cellSize) { |
| 33 | for (int x = 0; x < atlasSize; x += cellSize) { |
| 34 | paint.setColor(rand.nextU()); |
| 35 | paint.setAlpha(0xFF); |
| 36 | int index = i % strlen(s); |
| 37 | canvas->drawText(&s[index], 1, x + half, y + half + half/2, paint); |
| 38 | i += 1; |
| 39 | } |
| 40 | } |
| 41 | return surface->newImageSnapshot(); |
| 42 | } |
| 43 | |
| 44 | class DrawAtlasDrawable : public SkDrawable { |
| 45 | enum { |
| 46 | kMaxScale = 2, |
| 47 | kCellSize = 32, |
| 48 | kAtlasSize = 512, |
| 49 | }; |
| 50 | |
| 51 | struct Rec { |
| 52 | SkPoint fCenter; |
| 53 | SkVector fVelocity; |
| 54 | SkScalar fScale; |
| 55 | SkScalar fDScale; |
| 56 | SkScalar fRadian; |
| 57 | SkScalar fDRadian; |
| 58 | SkScalar fAlpha; |
| 59 | SkScalar fDAlpha; |
| 60 | |
| 61 | void advance(const SkRect& bounds) { |
| 62 | fCenter += fVelocity; |
| 63 | if (fCenter.fX > bounds.right()) { |
| 64 | SkASSERT(fVelocity.fX > 0); |
| 65 | fVelocity.fX = -fVelocity.fX; |
| 66 | } else if (fCenter.fX < bounds.left()) { |
| 67 | SkASSERT(fVelocity.fX < 0); |
| 68 | fVelocity.fX = -fVelocity.fX; |
| 69 | } |
| 70 | if (fCenter.fY > bounds.bottom()) { |
reed | ca10953 | 2015-06-25 16:25:25 -0700 | [diff] [blame] | 71 | if (fVelocity.fY > 0) { |
| 72 | fVelocity.fY = -fVelocity.fY; |
| 73 | } |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 74 | } else if (fCenter.fY < bounds.top()) { |
reed | ca10953 | 2015-06-25 16:25:25 -0700 | [diff] [blame] | 75 | if (fVelocity.fY < 0) { |
| 76 | fVelocity.fY = -fVelocity.fY; |
| 77 | } |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | fScale += fDScale; |
| 81 | if (fScale > 2 || fScale < SK_Scalar1/2) { |
| 82 | fDScale = -fDScale; |
| 83 | } |
| 84 | |
| 85 | fRadian += fDRadian; |
| 86 | fRadian = SkScalarMod(fRadian, 2 * SK_ScalarPI); |
| 87 | |
| 88 | fAlpha += fDAlpha; |
| 89 | if (fAlpha > 1) { |
| 90 | fAlpha = 1; |
| 91 | fDAlpha = -fDAlpha; |
| 92 | } else if (fAlpha < 0) { |
| 93 | fAlpha = 0; |
| 94 | fDAlpha = -fDAlpha; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | SkRSXform asRSXform() const { |
| 99 | SkMatrix m; |
| 100 | m.setTranslate(-8, -8); |
| 101 | m.postScale(fScale, fScale); |
| 102 | m.postRotate(SkRadiansToDegrees(fRadian)); |
| 103 | m.postTranslate(fCenter.fX, fCenter.fY); |
| 104 | |
| 105 | SkRSXform x; |
| 106 | x.fSCos = m.getScaleX(); |
| 107 | x.fSSin = m.getSkewY(); |
| 108 | x.fTx = m.getTranslateX(); |
| 109 | x.fTy = m.getTranslateY(); |
| 110 | return x; |
| 111 | } |
| 112 | }; |
| 113 | |
| 114 | enum { |
| 115 | N = 256, |
| 116 | }; |
| 117 | |
| 118 | SkAutoTUnref<SkImage> fAtlas; |
| 119 | Rec fRec[N]; |
| 120 | SkRect fTex[N]; |
| 121 | SkRect fBounds; |
| 122 | bool fUseColors; |
| 123 | |
| 124 | public: |
| 125 | DrawAtlasDrawable(const SkRect& r) : fBounds(r), fUseColors(false) { |
| 126 | SkRandom rand; |
| 127 | fAtlas.reset(make_atlas(kAtlasSize, kCellSize)); |
| 128 | const SkScalar kMaxSpeed = 5; |
| 129 | const SkScalar cell = SkIntToScalar(kCellSize); |
| 130 | int i = 0; |
| 131 | for (int y = 0; y < kAtlasSize; y += kCellSize) { |
| 132 | for (int x = 0; x < kAtlasSize; x += kCellSize) { |
| 133 | const SkScalar sx = SkIntToScalar(x); |
| 134 | const SkScalar sy = SkIntToScalar(y); |
| 135 | fTex[i].setXYWH(sx, sy, cell, cell); |
| 136 | |
| 137 | fRec[i].fCenter.set(sx + cell/2, sy + 3*cell/4); |
| 138 | fRec[i].fVelocity.fX = rand.nextSScalar1() * kMaxSpeed; |
| 139 | fRec[i].fVelocity.fY = rand.nextSScalar1() * kMaxSpeed; |
| 140 | fRec[i].fScale = 1; |
| 141 | fRec[i].fDScale = rand.nextSScalar1() / 4; |
| 142 | fRec[i].fRadian = 0; |
| 143 | fRec[i].fDRadian = rand.nextSScalar1() / 8; |
| 144 | fRec[i].fAlpha = rand.nextUScalar1(); |
| 145 | fRec[i].fDAlpha = rand.nextSScalar1() / 10; |
| 146 | i += 1; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void toggleUseColors() { |
| 152 | fUseColors = !fUseColors; |
| 153 | } |
| 154 | |
| 155 | protected: |
| 156 | void onDraw(SkCanvas* canvas) override { |
| 157 | SkRSXform xform[N]; |
| 158 | SkColor colors[N]; |
| 159 | |
| 160 | for (int i = 0; i < N; ++i) { |
| 161 | fRec[i].advance(fBounds); |
| 162 | xform[i] = fRec[i].asRSXform(); |
| 163 | if (fUseColors) { |
| 164 | colors[i] = SkColorSetARGB((int)(fRec[i].fAlpha * 0xFF), 0xFF, 0xFF, 0xFF); |
| 165 | } |
| 166 | } |
| 167 | SkPaint paint; |
| 168 | paint.setFilterQuality(kLow_SkFilterQuality); |
| 169 | |
| 170 | const SkRect cull = this->getBounds(); |
| 171 | const SkColor* colorsPtr = fUseColors ? colors : NULL; |
| 172 | canvas->drawAtlas(fAtlas, xform, fTex, colorsPtr, N, SkXfermode::kModulate_Mode, |
| 173 | &cull, &paint); |
| 174 | } |
| 175 | |
| 176 | SkRect onGetBounds() override { |
| 177 | const SkScalar border = kMaxScale * kCellSize; |
| 178 | SkRect r = fBounds; |
| 179 | r.outset(border, border); |
| 180 | return r; |
| 181 | } |
| 182 | |
| 183 | private: |
| 184 | typedef SkDrawable INHERITED; |
| 185 | }; |
| 186 | |
| 187 | class DrawAtlasView : public SampleView { |
| 188 | DrawAtlasDrawable* fDrawable; |
| 189 | |
| 190 | public: |
| 191 | DrawAtlasView() { |
| 192 | fDrawable = new DrawAtlasDrawable(SkRect::MakeWH(640, 480)); |
| 193 | } |
| 194 | |
| 195 | ~DrawAtlasView() override { |
| 196 | fDrawable->unref(); |
| 197 | } |
| 198 | |
| 199 | protected: |
| 200 | bool onQuery(SkEvent* evt) override { |
| 201 | if (SampleCode::TitleQ(*evt)) { |
| 202 | SampleCode::TitleR(evt, "DrawAtlas"); |
| 203 | return true; |
| 204 | } |
| 205 | SkUnichar uni; |
| 206 | if (SampleCode::CharQ(*evt, &uni)) { |
| 207 | switch (uni) { |
| 208 | case 'C': fDrawable->toggleUseColors(); this->inval(NULL); return true; |
| 209 | default: break; |
| 210 | } |
| 211 | } |
| 212 | return this->INHERITED::onQuery(evt); |
| 213 | } |
| 214 | |
| 215 | void onDrawContent(SkCanvas* canvas) override { |
| 216 | canvas->drawDrawable(fDrawable); |
| 217 | this->inval(NULL); |
| 218 | } |
| 219 | |
| 220 | #if 0 |
| 221 | // TODO: switch over to use this for our animation |
| 222 | bool onAnimate(const SkAnimTimer& timer) override { |
| 223 | SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360)); |
| 224 | fAnimatingDrawable->setSweep(angle); |
| 225 | return true; |
| 226 | } |
| 227 | #endif |
| 228 | |
| 229 | private: |
| 230 | typedef SampleView INHERITED; |
| 231 | }; |
| 232 | |
| 233 | ////////////////////////////////////////////////////////////////////////////// |
| 234 | |
| 235 | static SkView* MyFactory() { return new DrawAtlasView; } |
| 236 | static SkViewRegister reg(MyFactory); |