Chromium Code Reviews| 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" |
| 11 #include "SkCanvas.h" | 11 #include "SkCanvas.h" |
| 12 #include "SkDrawable.h" | 12 #include "SkDrawable.h" |
| 13 #include "SkPath.h" | 13 #include "SkPath.h" |
| 14 #include "SkRandom.h" | 14 #include "SkRandom.h" |
| 15 #include "SkRSXform.h" | 15 #include "SkRSXform.h" |
| 16 #include "SkSurface.h" | 16 #include "SkSurface.h" |
| 17 | 17 |
| 18 typedef void (*DrawAtlasProc)(SkCanvas*, SkImage*, const SkRSXform[], const SkRe ct[], | |
| 19 const SkColor[], int, const SkRect*, const SkPaint *); | |
| 20 | |
| 21 static void draw_atlas(SkCanvas* canvas, SkImage* atlas, const SkRSXform xform[] , | |
| 22 const SkRect tex[], const SkColor colors[], int count, co nst SkRect* cull, | |
| 23 const SkPaint* paint) { | |
| 24 canvas->drawAtlas(atlas, xform, tex, colors, count, SkXfermode::kModulate_Mo de, cull, paint); | |
| 25 } | |
| 26 | |
| 27 static void draw_atlas_sim(SkCanvas* canvas, SkImage* atlas, const SkRSXform xfo rm[], | |
|
robertphillips
2015/08/06 12:11:55
\n ?
| |
| 28 const SkRect tex[], const SkColor colors[], int count , const SkRect* cull, | |
| 29 const SkPaint* paint) { | |
| 30 for (int i = 0; i < count; ++i) { | |
| 31 SkMatrix matrix; | |
| 32 matrix.setRSXform(xform[i]); | |
| 33 | |
| 34 canvas->save(); | |
| 35 canvas->concat(matrix); | |
| 36 canvas->drawImageRect(atlas, &tex[i], tex[i].makeOffset(-tex[i].x(), -te x[i].y()), paint); | |
| 37 canvas->restore(); | |
| 38 } | |
| 39 } | |
| 40 | |
| 18 static SkImage* make_atlas(int atlasSize, int cellSize) { | 41 static SkImage* make_atlas(int atlasSize, int cellSize) { |
| 19 SkImageInfo info = SkImageInfo::MakeN32Premul(atlasSize, atlasSize); | 42 SkImageInfo info = SkImageInfo::MakeN32Premul(atlasSize, atlasSize); |
| 20 SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info)); | 43 SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info)); |
| 21 SkCanvas* canvas = surface->getCanvas(); | 44 SkCanvas* canvas = surface->getCanvas(); |
| 22 | 45 |
| 23 SkPaint paint; | 46 SkPaint paint; |
| 24 paint.setAntiAlias(true); | 47 paint.setAntiAlias(true); |
| 25 SkRandom rand; | 48 SkRandom rand; |
| 26 | 49 |
| 27 const SkScalar half = cellSize * SK_ScalarHalf; | 50 const SkScalar half = cellSize * SK_ScalarHalf; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 94 fDAlpha = -fDAlpha; | 117 fDAlpha = -fDAlpha; |
| 95 } | 118 } |
| 96 } | 119 } |
| 97 | 120 |
| 98 SkRSXform asRSXform() const { | 121 SkRSXform asRSXform() const { |
| 99 return SkRSXform::MakeFromRadians(fScale, fRadian, fCenter.x(), fCen ter.y(), | 122 return SkRSXform::MakeFromRadians(fScale, fRadian, fCenter.x(), fCen ter.y(), |
| 100 SkScalarHalf(kCellSize), SkScalarH alf(kCellSize)); | 123 SkScalarHalf(kCellSize), SkScalarH alf(kCellSize)); |
| 101 } | 124 } |
| 102 }; | 125 }; |
| 103 | 126 |
| 127 DrawAtlasProc fProc; | |
| 128 | |
| 104 enum { | 129 enum { |
| 105 N = 256, | 130 N = 256, |
| 106 }; | 131 }; |
| 107 | 132 |
| 108 SkAutoTUnref<SkImage> fAtlas; | 133 SkAutoTUnref<SkImage> fAtlas; |
| 109 Rec fRec[N]; | 134 Rec fRec[N]; |
| 110 SkRect fTex[N]; | 135 SkRect fTex[N]; |
| 111 SkRect fBounds; | 136 SkRect fBounds; |
| 112 bool fUseColors; | 137 bool fUseColors; |
| 113 | 138 |
| 114 public: | 139 public: |
| 115 DrawAtlasDrawable(const SkRect& r) : fBounds(r), fUseColors(false) { | 140 DrawAtlasDrawable(DrawAtlasProc proc, const SkRect& r) |
| 141 : fProc(proc), fBounds(r), fUseColors(false) | |
| 142 { | |
| 116 SkRandom rand; | 143 SkRandom rand; |
| 117 fAtlas.reset(make_atlas(kAtlasSize, kCellSize)); | 144 fAtlas.reset(make_atlas(kAtlasSize, kCellSize)); |
| 118 const SkScalar kMaxSpeed = 5; | 145 const SkScalar kMaxSpeed = 5; |
| 119 const SkScalar cell = SkIntToScalar(kCellSize); | 146 const SkScalar cell = SkIntToScalar(kCellSize); |
| 120 int i = 0; | 147 int i = 0; |
| 121 for (int y = 0; y < kAtlasSize; y += kCellSize) { | 148 for (int y = 0; y < kAtlasSize; y += kCellSize) { |
| 122 for (int x = 0; x < kAtlasSize; x += kCellSize) { | 149 for (int x = 0; x < kAtlasSize; x += kCellSize) { |
| 123 const SkScalar sx = SkIntToScalar(x); | 150 const SkScalar sx = SkIntToScalar(x); |
| 124 const SkScalar sy = SkIntToScalar(y); | 151 const SkScalar sy = SkIntToScalar(y); |
| 125 fTex[i].setXYWH(sx, sy, cell, cell); | 152 fTex[i].setXYWH(sx, sy, cell, cell); |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 152 xform[i] = fRec[i].asRSXform(); | 179 xform[i] = fRec[i].asRSXform(); |
| 153 if (fUseColors) { | 180 if (fUseColors) { |
| 154 colors[i] = SkColorSetARGB((int)(fRec[i].fAlpha * 0xFF), 0xFF, 0 xFF, 0xFF); | 181 colors[i] = SkColorSetARGB((int)(fRec[i].fAlpha * 0xFF), 0xFF, 0 xFF, 0xFF); |
| 155 } | 182 } |
| 156 } | 183 } |
| 157 SkPaint paint; | 184 SkPaint paint; |
| 158 paint.setFilterQuality(kLow_SkFilterQuality); | 185 paint.setFilterQuality(kLow_SkFilterQuality); |
| 159 | 186 |
| 160 const SkRect cull = this->getBounds(); | 187 const SkRect cull = this->getBounds(); |
| 161 const SkColor* colorsPtr = fUseColors ? colors : NULL; | 188 const SkColor* colorsPtr = fUseColors ? colors : NULL; |
| 162 canvas->drawAtlas(fAtlas, xform, fTex, colorsPtr, N, SkXfermode::kModula te_Mode, | 189 fProc(canvas, fAtlas, xform, fTex, colorsPtr, N, &cull, &paint); |
| 163 &cull, &paint); | |
| 164 } | 190 } |
| 165 | 191 |
| 166 SkRect onGetBounds() override { | 192 SkRect onGetBounds() override { |
| 167 const SkScalar border = kMaxScale * kCellSize; | 193 const SkScalar border = kMaxScale * kCellSize; |
| 168 SkRect r = fBounds; | 194 SkRect r = fBounds; |
| 169 r.outset(border, border); | 195 r.outset(border, border); |
| 170 return r; | 196 return r; |
| 171 } | 197 } |
| 172 | 198 |
| 173 private: | 199 private: |
| 174 typedef SkDrawable INHERITED; | 200 typedef SkDrawable INHERITED; |
| 175 }; | 201 }; |
| 176 | 202 |
| 177 class DrawAtlasView : public SampleView { | 203 class DrawAtlasView : public SampleView { |
| 178 DrawAtlasDrawable* fDrawable; | 204 const char* fName; |
| 205 DrawAtlasDrawable* fDrawable; | |
| 179 | 206 |
| 180 public: | 207 public: |
| 181 DrawAtlasView() { | 208 DrawAtlasView(const char name[], DrawAtlasProc proc) : fName(name) { |
| 182 fDrawable = new DrawAtlasDrawable(SkRect::MakeWH(640, 480)); | 209 fDrawable = new DrawAtlasDrawable(proc, SkRect::MakeWH(640, 480)); |
| 183 } | 210 } |
| 184 | 211 |
| 185 ~DrawAtlasView() override { | 212 ~DrawAtlasView() override { |
| 186 fDrawable->unref(); | 213 fDrawable->unref(); |
| 187 } | 214 } |
| 188 | 215 |
| 189 protected: | 216 protected: |
| 190 bool onQuery(SkEvent* evt) override { | 217 bool onQuery(SkEvent* evt) override { |
| 191 if (SampleCode::TitleQ(*evt)) { | 218 if (SampleCode::TitleQ(*evt)) { |
| 192 SampleCode::TitleR(evt, "DrawAtlas"); | 219 SampleCode::TitleR(evt, fName); |
| 193 return true; | 220 return true; |
| 194 } | 221 } |
| 195 SkUnichar uni; | 222 SkUnichar uni; |
| 196 if (SampleCode::CharQ(*evt, &uni)) { | 223 if (SampleCode::CharQ(*evt, &uni)) { |
| 197 switch (uni) { | 224 switch (uni) { |
| 198 case 'C': fDrawable->toggleUseColors(); this->inval(NULL); retur n true; | 225 case 'C': fDrawable->toggleUseColors(); this->inval(NULL); retur n true; |
| 199 default: break; | 226 default: break; |
| 200 } | 227 } |
| 201 } | 228 } |
| 202 return this->INHERITED::onQuery(evt); | 229 return this->INHERITED::onQuery(evt); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 215 return true; | 242 return true; |
| 216 } | 243 } |
| 217 #endif | 244 #endif |
| 218 | 245 |
| 219 private: | 246 private: |
| 220 typedef SampleView INHERITED; | 247 typedef SampleView INHERITED; |
| 221 }; | 248 }; |
| 222 | 249 |
| 223 ////////////////////////////////////////////////////////////////////////////// | 250 ////////////////////////////////////////////////////////////////////////////// |
| 224 | 251 |
| 225 static SkView* MyFactory() { return new DrawAtlasView; } | 252 DEF_SAMPLE( return new DrawAtlasView("DrawAtlas", draw_atlas); ) |
| 226 static SkViewRegister reg(MyFactory); | 253 DEF_SAMPLE( return new DrawAtlasView("DrawAtlasSim", draw_atlas_sim); ) |
| OLD | NEW |