OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SampleCode.h" | 8 #include "SampleCode.h" |
9 #include "SkAnimTimer.h" | 9 #include "SkAnimTimer.h" |
10 #include "SkView.h" | 10 #include "SkView.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 matrix.setRSXform(xform[i]); | 32 matrix.setRSXform(xform[i]); |
33 | 33 |
34 canvas->save(); | 34 canvas->save(); |
35 canvas->concat(matrix); | 35 canvas->concat(matrix); |
36 canvas->drawImageRect(atlas, tex[i], tex[i].makeOffset(-tex[i].x(), -tex
[i].y()), paint, | 36 canvas->drawImageRect(atlas, tex[i], tex[i].makeOffset(-tex[i].x(), -tex
[i].y()), paint, |
37 SkCanvas::kFast_SrcRectConstraint); | 37 SkCanvas::kFast_SrcRectConstraint); |
38 canvas->restore(); | 38 canvas->restore(); |
39 } | 39 } |
40 } | 40 } |
41 | 41 |
42 static SkImage* make_atlas(int atlasSize, int cellSize) { | 42 static sk_sp<SkImage> make_atlas(int atlasSize, int cellSize) { |
43 SkImageInfo info = SkImageInfo::MakeN32Premul(atlasSize, atlasSize); | 43 SkImageInfo info = SkImageInfo::MakeN32Premul(atlasSize, atlasSize); |
44 SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info)); | 44 SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info)); |
45 SkCanvas* canvas = surface->getCanvas(); | 45 SkCanvas* canvas = surface->getCanvas(); |
46 | 46 |
47 SkPaint paint; | 47 SkPaint paint; |
48 paint.setAntiAlias(true); | 48 paint.setAntiAlias(true); |
49 SkRandom rand; | 49 SkRandom rand; |
50 | 50 |
51 const SkScalar half = cellSize * SK_ScalarHalf; | 51 const SkScalar half = cellSize * SK_ScalarHalf; |
52 const char* s = "01234567890!@#$%^&*=+<>?abcdefghijklmnopqrstuvwxyzABCDEFGHI
JKLMNOPQRSTUVWXYZ"; | 52 const char* s = "01234567890!@#$%^&*=+<>?abcdefghijklmnopqrstuvwxyzABCDEFGHI
JKLMNOPQRSTUVWXYZ"; |
53 paint.setTextSize(28); | 53 paint.setTextSize(28); |
54 paint.setTextAlign(SkPaint::kCenter_Align); | 54 paint.setTextAlign(SkPaint::kCenter_Align); |
55 int i = 0; | 55 int i = 0; |
56 for (int y = 0; y < atlasSize; y += cellSize) { | 56 for (int y = 0; y < atlasSize; y += cellSize) { |
57 for (int x = 0; x < atlasSize; x += cellSize) { | 57 for (int x = 0; x < atlasSize; x += cellSize) { |
58 paint.setColor(rand.nextU()); | 58 paint.setColor(rand.nextU()); |
59 paint.setAlpha(0xFF); | 59 paint.setAlpha(0xFF); |
60 int index = i % strlen(s); | 60 int index = i % strlen(s); |
61 canvas->drawText(&s[index], 1, x + half, y + half + half/2, paint); | 61 canvas->drawText(&s[index], 1, x + half, y + half + half/2, paint); |
62 i += 1; | 62 i += 1; |
63 } | 63 } |
64 } | 64 } |
65 return surface->newImageSnapshot(); | 65 return surface->makeImageSnapshot(); |
66 } | 66 } |
67 | 67 |
68 class DrawAtlasDrawable : public SkDrawable { | 68 class DrawAtlasDrawable : public SkDrawable { |
69 enum { | 69 enum { |
70 kMaxScale = 2, | 70 kMaxScale = 2, |
71 kCellSize = 32, | 71 kCellSize = 32, |
72 kAtlasSize = 512, | 72 kAtlasSize = 512, |
73 }; | 73 }; |
74 | 74 |
75 struct Rec { | 75 struct Rec { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 SkScalarHalf(kCellSize), SkScalarH
alf(kCellSize)); | 124 SkScalarHalf(kCellSize), SkScalarH
alf(kCellSize)); |
125 } | 125 } |
126 }; | 126 }; |
127 | 127 |
128 DrawAtlasProc fProc; | 128 DrawAtlasProc fProc; |
129 | 129 |
130 enum { | 130 enum { |
131 N = 256, | 131 N = 256, |
132 }; | 132 }; |
133 | 133 |
134 SkAutoTUnref<SkImage> fAtlas; | 134 sk_sp<SkImage> fAtlas; |
135 Rec fRec[N]; | 135 Rec fRec[N]; |
136 SkRect fTex[N]; | 136 SkRect fTex[N]; |
137 SkRect fBounds; | 137 SkRect fBounds; |
138 bool fUseColors; | 138 bool fUseColors; |
139 | 139 |
140 public: | 140 public: |
141 DrawAtlasDrawable(DrawAtlasProc proc, const SkRect& r) | 141 DrawAtlasDrawable(DrawAtlasProc proc, const SkRect& r) |
142 : fProc(proc), fBounds(r), fUseColors(false) | 142 : fProc(proc), fBounds(r), fUseColors(false) |
143 { | 143 { |
144 SkRandom rand; | 144 SkRandom rand; |
145 fAtlas.reset(make_atlas(kAtlasSize, kCellSize)); | 145 fAtlas = make_atlas(kAtlasSize, kCellSize); |
146 const SkScalar kMaxSpeed = 5; | 146 const SkScalar kMaxSpeed = 5; |
147 const SkScalar cell = SkIntToScalar(kCellSize); | 147 const SkScalar cell = SkIntToScalar(kCellSize); |
148 int i = 0; | 148 int i = 0; |
149 for (int y = 0; y < kAtlasSize; y += kCellSize) { | 149 for (int y = 0; y < kAtlasSize; y += kCellSize) { |
150 for (int x = 0; x < kAtlasSize; x += kCellSize) { | 150 for (int x = 0; x < kAtlasSize; x += kCellSize) { |
151 const SkScalar sx = SkIntToScalar(x); | 151 const SkScalar sx = SkIntToScalar(x); |
152 const SkScalar sy = SkIntToScalar(y); | 152 const SkScalar sy = SkIntToScalar(y); |
153 fTex[i].setXYWH(sx, sy, cell, cell); | 153 fTex[i].setXYWH(sx, sy, cell, cell); |
154 | 154 |
155 fRec[i].fCenter.set(sx + cell/2, sy + 3*cell/4); | 155 fRec[i].fCenter.set(sx + cell/2, sy + 3*cell/4); |
(...skipping 24 matching lines...) Expand all Loading... |
180 xform[i] = fRec[i].asRSXform(); | 180 xform[i] = fRec[i].asRSXform(); |
181 if (fUseColors) { | 181 if (fUseColors) { |
182 colors[i] = SkColorSetARGB((int)(fRec[i].fAlpha * 0xFF), 0xFF, 0
xFF, 0xFF); | 182 colors[i] = SkColorSetARGB((int)(fRec[i].fAlpha * 0xFF), 0xFF, 0
xFF, 0xFF); |
183 } | 183 } |
184 } | 184 } |
185 SkPaint paint; | 185 SkPaint paint; |
186 paint.setFilterQuality(kLow_SkFilterQuality); | 186 paint.setFilterQuality(kLow_SkFilterQuality); |
187 | 187 |
188 const SkRect cull = this->getBounds(); | 188 const SkRect cull = this->getBounds(); |
189 const SkColor* colorsPtr = fUseColors ? colors : nullptr; | 189 const SkColor* colorsPtr = fUseColors ? colors : nullptr; |
190 fProc(canvas, fAtlas, xform, fTex, colorsPtr, N, &cull, &paint); | 190 fProc(canvas, fAtlas.get(), xform, fTex, colorsPtr, N, &cull, &paint); |
191 } | 191 } |
192 | 192 |
193 SkRect onGetBounds() override { | 193 SkRect onGetBounds() override { |
194 const SkScalar border = kMaxScale * kCellSize; | 194 const SkScalar border = kMaxScale * kCellSize; |
195 SkRect r = fBounds; | 195 SkRect r = fBounds; |
196 r.outset(border, border); | 196 r.outset(border, border); |
197 return r; | 197 return r; |
198 } | 198 } |
199 | 199 |
200 private: | 200 private: |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 #endif | 245 #endif |
246 | 246 |
247 private: | 247 private: |
248 typedef SampleView INHERITED; | 248 typedef SampleView INHERITED; |
249 }; | 249 }; |
250 | 250 |
251 ////////////////////////////////////////////////////////////////////////////// | 251 ////////////////////////////////////////////////////////////////////////////// |
252 | 252 |
253 DEF_SAMPLE( return new DrawAtlasView("DrawAtlas", draw_atlas); ) | 253 DEF_SAMPLE( return new DrawAtlasView("DrawAtlas", draw_atlas); ) |
254 DEF_SAMPLE( return new DrawAtlasView("DrawAtlasSim", draw_atlas_sim); ) | 254 DEF_SAMPLE( return new DrawAtlasView("DrawAtlasSim", draw_atlas_sim); ) |
OLD | NEW |