OLD | NEW |
(Empty) | |
| 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!@#$%^&*=+<>?abcdefghijklmnopqrstuvwxyzABCDEFGHI
JKLMNOPQRSTUVWXYZ"; |
| 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()) { |
| 71 SkASSERT(fVelocity.fY > 0); |
| 72 fVelocity.fY = -fVelocity.fY; |
| 73 } else if (fCenter.fY < bounds.top()) { |
| 74 SkASSERT(fVelocity.fY < 0); |
| 75 fVelocity.fY = -fVelocity.fY; |
| 76 } |
| 77 |
| 78 fScale += fDScale; |
| 79 if (fScale > 2 || fScale < SK_Scalar1/2) { |
| 80 fDScale = -fDScale; |
| 81 } |
| 82 |
| 83 fRadian += fDRadian; |
| 84 fRadian = SkScalarMod(fRadian, 2 * SK_ScalarPI); |
| 85 |
| 86 fAlpha += fDAlpha; |
| 87 if (fAlpha > 1) { |
| 88 fAlpha = 1; |
| 89 fDAlpha = -fDAlpha; |
| 90 } else if (fAlpha < 0) { |
| 91 fAlpha = 0; |
| 92 fDAlpha = -fDAlpha; |
| 93 } |
| 94 } |
| 95 |
| 96 SkRSXform asRSXform() const { |
| 97 SkMatrix m; |
| 98 m.setTranslate(-8, -8); |
| 99 m.postScale(fScale, fScale); |
| 100 m.postRotate(SkRadiansToDegrees(fRadian)); |
| 101 m.postTranslate(fCenter.fX, fCenter.fY); |
| 102 |
| 103 SkRSXform x; |
| 104 x.fSCos = m.getScaleX(); |
| 105 x.fSSin = m.getSkewY(); |
| 106 x.fTx = m.getTranslateX(); |
| 107 x.fTy = m.getTranslateY(); |
| 108 return x; |
| 109 } |
| 110 }; |
| 111 |
| 112 enum { |
| 113 N = 256, |
| 114 }; |
| 115 |
| 116 SkAutoTUnref<SkImage> fAtlas; |
| 117 Rec fRec[N]; |
| 118 SkRect fTex[N]; |
| 119 SkRect fBounds; |
| 120 bool fUseColors; |
| 121 |
| 122 public: |
| 123 DrawAtlasDrawable(const SkRect& r) : fBounds(r), fUseColors(false) { |
| 124 SkRandom rand; |
| 125 fAtlas.reset(make_atlas(kAtlasSize, kCellSize)); |
| 126 const SkScalar kMaxSpeed = 5; |
| 127 const SkScalar cell = SkIntToScalar(kCellSize); |
| 128 int i = 0; |
| 129 for (int y = 0; y < kAtlasSize; y += kCellSize) { |
| 130 for (int x = 0; x < kAtlasSize; x += kCellSize) { |
| 131 const SkScalar sx = SkIntToScalar(x); |
| 132 const SkScalar sy = SkIntToScalar(y); |
| 133 fTex[i].setXYWH(sx, sy, cell, cell); |
| 134 |
| 135 fRec[i].fCenter.set(sx + cell/2, sy + 3*cell/4); |
| 136 fRec[i].fVelocity.fX = rand.nextSScalar1() * kMaxSpeed; |
| 137 fRec[i].fVelocity.fY = rand.nextSScalar1() * kMaxSpeed; |
| 138 fRec[i].fScale = 1; |
| 139 fRec[i].fDScale = rand.nextSScalar1() / 4; |
| 140 fRec[i].fRadian = 0; |
| 141 fRec[i].fDRadian = rand.nextSScalar1() / 8; |
| 142 fRec[i].fAlpha = rand.nextUScalar1(); |
| 143 fRec[i].fDAlpha = rand.nextSScalar1() / 10; |
| 144 i += 1; |
| 145 } |
| 146 } |
| 147 } |
| 148 |
| 149 void toggleUseColors() { |
| 150 fUseColors = !fUseColors; |
| 151 } |
| 152 |
| 153 protected: |
| 154 void onDraw(SkCanvas* canvas) override { |
| 155 SkRSXform xform[N]; |
| 156 SkColor colors[N]; |
| 157 |
| 158 for (int i = 0; i < N; ++i) { |
| 159 fRec[i].advance(fBounds); |
| 160 xform[i] = fRec[i].asRSXform(); |
| 161 if (fUseColors) { |
| 162 colors[i] = SkColorSetARGB((int)(fRec[i].fAlpha * 0xFF), 0xFF, 0
xFF, 0xFF); |
| 163 } |
| 164 } |
| 165 SkPaint paint; |
| 166 paint.setFilterQuality(kLow_SkFilterQuality); |
| 167 |
| 168 const SkRect cull = this->getBounds(); |
| 169 const SkColor* colorsPtr = fUseColors ? colors : NULL; |
| 170 canvas->drawAtlas(fAtlas, xform, fTex, colorsPtr, N, SkXfermode::kModula
te_Mode, |
| 171 &cull, &paint); |
| 172 } |
| 173 |
| 174 SkRect onGetBounds() override { |
| 175 const SkScalar border = kMaxScale * kCellSize; |
| 176 SkRect r = fBounds; |
| 177 r.outset(border, border); |
| 178 return r; |
| 179 } |
| 180 |
| 181 private: |
| 182 typedef SkDrawable INHERITED; |
| 183 }; |
| 184 |
| 185 class DrawAtlasView : public SampleView { |
| 186 DrawAtlasDrawable* fDrawable; |
| 187 |
| 188 public: |
| 189 DrawAtlasView() { |
| 190 fDrawable = new DrawAtlasDrawable(SkRect::MakeWH(640, 480)); |
| 191 } |
| 192 |
| 193 ~DrawAtlasView() override { |
| 194 fDrawable->unref(); |
| 195 } |
| 196 |
| 197 protected: |
| 198 bool onQuery(SkEvent* evt) override { |
| 199 if (SampleCode::TitleQ(*evt)) { |
| 200 SampleCode::TitleR(evt, "DrawAtlas"); |
| 201 return true; |
| 202 } |
| 203 SkUnichar uni; |
| 204 if (SampleCode::CharQ(*evt, &uni)) { |
| 205 switch (uni) { |
| 206 case 'C': fDrawable->toggleUseColors(); this->inval(NULL); retur
n true; |
| 207 default: break; |
| 208 } |
| 209 } |
| 210 return this->INHERITED::onQuery(evt); |
| 211 } |
| 212 |
| 213 void onDrawContent(SkCanvas* canvas) override { |
| 214 canvas->drawDrawable(fDrawable); |
| 215 this->inval(NULL); |
| 216 } |
| 217 |
| 218 #if 0 |
| 219 // TODO: switch over to use this for our animation |
| 220 bool onAnimate(const SkAnimTimer& timer) override { |
| 221 SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360)); |
| 222 fAnimatingDrawable->setSweep(angle); |
| 223 return true; |
| 224 } |
| 225 #endif |
| 226 |
| 227 private: |
| 228 typedef SampleView INHERITED; |
| 229 }; |
| 230 |
| 231 ////////////////////////////////////////////////////////////////////////////// |
| 232 |
| 233 static SkView* MyFactory() { return new DrawAtlasView; } |
| 234 static SkViewRegister reg(MyFactory); |
OLD | NEW |