Chromium Code Reviews| 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() { | |
| 19 static int N = 512; | |
| 20 SkImageInfo info = SkImageInfo::MakeN32Premul(N, N); | |
| 21 SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info)); | |
| 22 SkCanvas* canvas = surface->getCanvas(); | |
| 23 | |
| 24 SkPaint paint; | |
| 25 paint.setAntiAlias(true); | |
| 26 SkRandom rand; | |
| 27 | |
| 28 const SkScalar half = 16; | |
| 29 const char* s = "01234567890!@#$%^&*=+<>?abcdefghijklmnopqrstuvwxyzABCDEFGHI JKLMNOPQRSTUVWXYZ"; | |
| 30 paint.setTextSize(28); | |
| 31 paint.setTextAlign(SkPaint::kCenter_Align); | |
| 32 int i = 0; | |
| 33 for (int y = 0; y < N; y += 32) { | |
| 34 for (int x = 0; x < N; x += 32) { | |
| 35 paint.setColor(rand.nextU()); | |
| 36 paint.setAlpha(0xFF); | |
| 37 int index = i % strlen(s); | |
| 38 canvas->drawText(&s[index], 1, x + half, y + half + half/2, paint); | |
| 39 i += 1; | |
| 40 } | |
| 41 } | |
| 42 return surface->newImageSnapshot(); | |
| 43 } | |
| 44 | |
| 45 class DrawAtlasDrawable : public SkDrawable { | |
| 46 enum { | |
| 47 kMaxScale = 2, | |
| 48 kCellSize = 32, | |
| 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 | |
| 121 public: | |
| 122 DrawAtlasDrawable(const SkRect& r) : fBounds(r) { | |
| 123 SkRandom rand; | |
| 124 fAtlas.reset(make_atlas()); | |
| 125 const SkScalar half = 8; | |
|
robertphillips
2015/06/24 15:11:02
speed -> kMaxSpeed ?
reed1
2015/06/24 17:02:42
Done.
| |
| 126 const SkScalar speed = 5; | |
| 127 int i = 0; | |
|
robertphillips
2015/06/24 15:11:02
Have 512 be kAtlasWH (and use in make_atlas too) ?
reed1
2015/06/24 17:02:42
Done.
| |
| 128 for (int y = 0; y < 512; y += 32) { | |
| 129 for (int x = 0; x < 512; x += 32) { | |
| 130 fTex[i].setXYWH(SkIntToScalar(x), SkIntToScalar(y), 32, 32); | |
| 131 | |
| 132 fRec[i].fCenter.set(x/2 + half, y/2 + half); | |
| 133 fRec[i].fVelocity.fX = rand.nextSScalar1() * speed; | |
| 134 fRec[i].fVelocity.fY = rand.nextSScalar1() * speed; | |
| 135 fRec[i].fScale = 1; | |
| 136 fRec[i].fDScale = rand.nextSScalar1() / 4; | |
| 137 fRec[i].fRadian = 0; | |
| 138 fRec[i].fDRadian = rand.nextSScalar1() / 8; | |
| 139 fRec[i].fAlpha = rand.nextUScalar1(); | |
| 140 fRec[i].fDAlpha = rand.nextSScalar1() / 10; | |
| 141 i += 1; | |
| 142 } | |
| 143 } | |
|
robertphillips
2015/06/24 15:11:02
rm this ?
reed1
2015/06/24 17:02:42
Done.
| |
| 144 SkDebugf("total %d\n", i); | |
| 145 } | |
| 146 | |
| 147 void onDraw(SkCanvas* canvas) override { | |
| 148 SkRSXform xform[N]; | |
|
robertphillips
2015/06/24 15:11:02
Why not have a keystroke to enable/disable animati
reed1
2015/06/24 17:02:42
Done.
| |
| 149 // SkColor colors[N]; | |
| 150 | |
| 151 for (int i = 0; i < N; ++i) { | |
| 152 fRec[i].advance(fBounds); | |
| 153 xform[i] = fRec[i].asRSXform(); | |
| 154 // colors[i] = SkColorSetARGB(fRec[i].fAlpha * 0xFF, 0xFF, 0xFF, 0xFF ); | |
| 155 } | |
| 156 SkPaint paint; | |
| 157 // paint.setFilterQuality(kLow_SkFilterQuality); | |
| 158 // paint.setAntiAlias(true); | |
| 159 | |
| 160 const SkRect cull = this->getBounds(); | |
| 161 canvas->drawAtlas(fAtlas, xform, fTex, N, &cull, &paint); | |
| 162 } | |
| 163 | |
| 164 SkRect onGetBounds() override { | |
|
robertphillips
2015/06/24 15:11:02
border ?
reed1
2015/06/24 17:02:42
Done.
| |
| 165 const SkScalar boarder = kMaxScale * kCellSize; | |
| 166 SkRect r = fBounds; | |
| 167 r.outset(boarder, boarder); | |
| 168 return r; | |
| 169 } | |
| 170 | |
| 171 private: | |
| 172 typedef SkDrawable INHERITED; | |
| 173 }; | |
| 174 | |
| 175 class DrawAtlasView : public SampleView { | |
|
robertphillips
2015/06/24 15:11:02
Why not just have this be a member ?
reed1
2015/06/24 17:02:42
canvas->drawDrawable(fDrawable)
this guy may need
| |
| 176 SkDrawable* fDrawable; | |
| 177 | |
| 178 public: | |
| 179 DrawAtlasView() { | |
| 180 fDrawable = new DrawAtlasDrawable(SkRect::MakeWH(640, 480)); | |
| 181 } | |
| 182 | |
|
robertphillips
2015/06/24 15:11:02
rm virtual ?
reed1
2015/06/24 17:02:42
Done.
| |
| 183 virtual ~DrawAtlasView() override { | |
| 184 fDrawable->unref(); | |
| 185 } | |
| 186 | |
| 187 protected: | |
| 188 bool onQuery(SkEvent* evt) override { | |
| 189 if (SampleCode::TitleQ(*evt)) { | |
| 190 SampleCode::TitleR(evt, "DrawAtlas"); | |
| 191 return true; | |
| 192 } | |
| 193 return this->INHERITED::onQuery(evt); | |
| 194 } | |
| 195 | |
| 196 void onDrawContent(SkCanvas* canvas) override { | |
| 197 canvas->drawDrawable(fDrawable); | |
| 198 this->inval(NULL); | |
| 199 } | |
| 200 | |
|
robertphillips
2015/06/24 15:11:02
Do we still need this code ?
reed1
2015/06/24 17:02:42
Leaving it as a reminder/todo
| |
| 201 #if 0 | |
| 202 bool onAnimate(const SkAnimTimer& timer) override { | |
| 203 SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360)); | |
| 204 fAnimatingDrawable->setSweep(angle); | |
| 205 return true; | |
| 206 } | |
| 207 #endif | |
| 208 | |
| 209 private: | |
| 210 typedef SampleView INHERITED; | |
| 211 }; | |
| 212 | |
| 213 ////////////////////////////////////////////////////////////////////////////// | |
| 214 | |
| 215 static SkView* MyFactory() { return new DrawAtlasView; } | |
| 216 static SkViewRegister reg(MyFactory); | |
| OLD | NEW |