| 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 "Resources.h" | 9 #include "Resources.h" |
| 10 #include "SkAnimTimer.h" | 10 #include "SkAnimTimer.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 const SkPaint* paint) { | 28 const SkPaint* paint) { |
| 29 canvas->drawAtlas(atlas, xform, tex, colors, count, SkXfermode::kModulate_Mo
de, cull, paint); | 29 canvas->drawAtlas(atlas, xform, tex, colors, count, SkXfermode::kModulate_Mo
de, cull, paint); |
| 30 } | 30 } |
| 31 | 31 |
| 32 static void draw_atlas_sim(SkCanvas* canvas, SkImage* atlas, const SkRSXform xfo
rm[], | 32 static void draw_atlas_sim(SkCanvas* canvas, SkImage* atlas, const SkRSXform xfo
rm[], |
| 33 const SkRect tex[], const SkColor colors[], int count
, const SkRect* cull, | 33 const SkRect tex[], const SkColor colors[], int count
, const SkRect* cull, |
| 34 const SkPaint* paint) { | 34 const SkPaint* paint) { |
| 35 for (int i = 0; i < count; ++i) { | 35 for (int i = 0; i < count; ++i) { |
| 36 SkMatrix matrix; | 36 SkMatrix matrix; |
| 37 matrix.setRSXform(xform[i]); | 37 matrix.setRSXform(xform[i]); |
| 38 | 38 |
| 39 canvas->save(); | 39 canvas->save(); |
| 40 canvas->concat(matrix); | 40 canvas->concat(matrix); |
| 41 canvas->drawImageRect(atlas, tex[i], tex[i].makeOffset(-tex[i].x(), -tex
[i].y()), paint, | 41 canvas->drawImageRect(atlas, tex[i], tex[i].makeOffset(-tex[i].x(), -tex
[i].y()), paint, |
| 42 SkCanvas::kFast_SrcRectConstraint); | 42 SkCanvas::kFast_SrcRectConstraint); |
| 43 canvas->restore(); | 43 canvas->restore(); |
| 44 } | 44 } |
| 45 } | 45 } |
| 46 | 46 |
| 47 | 47 |
| 48 class DrawShipView : public SampleView { | 48 class DrawShipView : public SampleView { |
| 49 public: | 49 public: |
| 50 DrawShipView(const char name[], DrawAtlasProc proc) : fName(name), fProc(pro
c) { | 50 DrawShipView(const char name[], DrawAtlasProc proc) : fName(name), fProc(pro
c) { |
| 51 fAtlas = GetResourceAsImage("ship.png"); | 51 fAtlas = GetResourceAsImage("ship.png"); |
| 52 if (!fAtlas) { | 52 if (!fAtlas) { |
| 53 SkDebugf("\nCould not decode file ship.png. Falling back to penguin
mode.\n"); | 53 SkDebugf("\nCould not decode file ship.png. Falling back to penguin
mode.\n"); |
| 54 fAtlas = GetResourceAsImage("baby_tux.png"); | 54 fAtlas = GetResourceAsImage("baby_tux.png"); |
| 55 if (!fAtlas) { | 55 if (!fAtlas) { |
| 56 SkDebugf("\nCould not decode file baby_tux.png. Did you forget" | 56 SkDebugf("\nCould not decode file baby_tux.png. Did you forget" |
| 57 " to set the resourcePath?\n"); | 57 " to set the resourcePath?\n"); |
| 58 return; | 58 return; |
| 59 } | 59 } |
| 60 } | 60 } |
| 61 | 61 |
| 62 SkScalar anchorX = fAtlas->width()*0.5f; | 62 SkScalar anchorX = fAtlas->width()*0.5f; |
| 63 SkScalar anchorY = fAtlas->height()*0.5f; | 63 SkScalar anchorY = fAtlas->height()*0.5f; |
| 64 int currIndex = 0; | 64 int currIndex = 0; |
| 65 for (int x = 0; x < kGrid; x++) { | 65 for (int x = 0; x < kGrid; x++) { |
| 66 for (int y = 0; y < kGrid; y++) { | 66 for (int y = 0; y < kGrid; y++) { |
| 67 float xPos = (x / (kGrid - 1.0f)) * kWidth; | 67 float xPos = (x / (kGrid - 1.0f)) * kWidth; |
| 68 float yPos = (y / (kGrid - 1.0f)) * kWidth; | 68 float yPos = (y / (kGrid - 1.0f)) * kWidth; |
| 69 | 69 |
| 70 fTex[currIndex] = SkRect::MakeLTRB(0.0f, 0.0f, | 70 fTex[currIndex] = SkRect::MakeLTRB(0.0f, 0.0f, |
| 71 SkIntToScalar(fAtlas->width()
), | 71 SkIntToScalar(fAtlas->width()
), |
| 72 SkIntToScalar(fAtlas->height(
))); | 72 SkIntToScalar(fAtlas->height(
))); |
| 73 fXform[currIndex] = SkRSXform::MakeFromRadians(0.1f, SK_ScalarPI
*0.5f, | 73 fXform[currIndex] = SkRSXform::MakeFromRadians(0.1f, SK_ScalarPI
*0.5f, |
| 74 xPos, yPos, ancho
rX, anchorY); | 74 xPos, yPos, ancho
rX, anchorY); |
| 75 currIndex++; | 75 currIndex++; |
| 76 } | 76 } |
| 77 } | 77 } |
| 78 fTex[currIndex] = SkRect::MakeLTRB(0.0f, 0.0f, | 78 fTex[currIndex] = SkRect::MakeLTRB(0.0f, 0.0f, |
| 79 SkIntToScalar(fAtlas->width()), | 79 SkIntToScalar(fAtlas->width()), |
| 80 SkIntToScalar(fAtlas->height())); | 80 SkIntToScalar(fAtlas->height())); |
| 81 fXform[currIndex] = SkRSXform::MakeFromRadians(0.5f, SK_ScalarPI*0.5f, | 81 fXform[currIndex] = SkRSXform::MakeFromRadians(0.5f, SK_ScalarPI*0.5f, |
| 82 kWidth*0.5f, kHeight*0.5f
, anchorX, anchorY); | 82 kWidth*0.5f, kHeight*0.5f
, anchorX, anchorY); |
| 83 | 83 |
| 84 fCurrentTime = 0; | 84 fCurrentTime = 0; |
| 85 fTimer.start(); | 85 fTimer.start(); |
| 86 } | 86 } |
| 87 | 87 |
| 88 ~DrawShipView() override {} | 88 ~DrawShipView() override {} |
| 89 | 89 |
| 90 protected: | 90 protected: |
| 91 // overrides from SkEventSink | 91 // overrides from SkEventSink |
| 92 bool onQuery(SkEvent* evt) override { | 92 bool onQuery(SkEvent* evt) override { |
| 93 if (SampleCode::TitleQ(*evt)) { | 93 if (SampleCode::TitleQ(*evt)) { |
| 94 SampleCode::TitleR(evt, fName); | 94 SampleCode::TitleR(evt, fName); |
| 95 return true; | 95 return true; |
| 96 } | 96 } |
| 97 return this->INHERITED::onQuery(evt); | 97 return this->INHERITED::onQuery(evt); |
| 98 } | 98 } |
| 99 | 99 |
| 100 void onDrawContent(SkCanvas* canvas) override { | 100 void onDrawContent(SkCanvas* canvas) override { |
| 101 const float kCosDiff = 0.99984769515f; | 101 const float kCosDiff = 0.99984769515f; |
| 102 const float kSinDiff = 0.01745240643f; | 102 const float kSinDiff = 0.01745240643f; |
| 103 | 103 |
| 104 if (!fAtlas) { | 104 if (!fAtlas) { |
| 105 return; | 105 return; |
| 106 } | 106 } |
| 107 | 107 |
| 108 SkPaint paint; | 108 SkPaint paint; |
| 109 paint.setFilterQuality(kLow_SkFilterQuality); | 109 paint.setFilterQuality(kLow_SkFilterQuality); |
| 110 paint.setColor(SK_ColorWHITE); | 110 paint.setColor(SK_ColorWHITE); |
| 111 paint.setTextSize(15.0f); | 111 paint.setTextSize(15.0f); |
| 112 | 112 |
| 113 fTimer.end(); | 113 fTimer.end(); |
| 114 | 114 |
| 115 fTimes[fCurrentTime] = (float)(fTimer.fWall); | 115 fTimes[fCurrentTime] = (float)(fTimer.fWall); |
| 116 fCurrentTime = (fCurrentTime + 1) & 0x1f; | 116 fCurrentTime = (fCurrentTime + 1) & 0x1f; |
| 117 | 117 |
| 118 float meanTime = 0.0f; | 118 float meanTime = 0.0f; |
| 119 for (int i = 0; i < 32; ++i) { | 119 for (int i = 0; i < 32; ++i) { |
| 120 meanTime += fTimes[i]; | 120 meanTime += fTimes[i]; |
| 121 } | 121 } |
| 122 meanTime /= 32.f; | 122 meanTime /= 32.f; |
| 123 SkString outString("fps: "); | 123 SkString outString("fps: "); |
| 124 SkScalar fps = 1000.f/meanTime; | 124 SkScalar fps = 1000.f/meanTime; |
| 125 outString.appendScalar(fps); | 125 outString.appendScalar(fps); |
| 126 outString.append(" ms: "); | 126 outString.append(" ms: "); |
| 127 outString.appendScalar(meanTime); | 127 outString.appendScalar(meanTime); |
| 128 | 128 |
| 129 fTimer.start(); | 129 fTimer.start(); |
| 130 | 130 |
| 131 SkScalar anchorX = fAtlas->width()*0.5f; | 131 SkScalar anchorX = fAtlas->width()*0.5f; |
| 132 SkScalar anchorY = fAtlas->height()*0.5f; | 132 SkScalar anchorY = fAtlas->height()*0.5f; |
| 133 for (int i = 0; i < kGrid*kGrid+1; ++i) { | 133 for (int i = 0; i < kGrid*kGrid+1; ++i) { |
| 134 SkScalar c = fXform[i].fSCos; | 134 SkScalar c = fXform[i].fSCos; |
| 135 SkScalar s = fXform[i].fSSin; | 135 SkScalar s = fXform[i].fSSin; |
| 136 | 136 |
| 137 SkScalar dx = c*anchorX - s*anchorY; | 137 SkScalar dx = c*anchorX - s*anchorY; |
| 138 SkScalar dy = s*anchorX + c*anchorY; | 138 SkScalar dy = s*anchorX + c*anchorY; |
| 139 | 139 |
| 140 fXform[i].fSCos = kCosDiff*c - kSinDiff*s; | 140 fXform[i].fSCos = kCosDiff*c - kSinDiff*s; |
| 141 fXform[i].fSSin = kSinDiff*c + kCosDiff*s; | 141 fXform[i].fSSin = kSinDiff*c + kCosDiff*s; |
| 142 | 142 |
| 143 dx -= fXform[i].fSCos*anchorX - fXform[i].fSSin*anchorY; | 143 dx -= fXform[i].fSCos*anchorX - fXform[i].fSSin*anchorY; |
| 144 dy -= fXform[i].fSSin*anchorX + fXform[i].fSCos*anchorY; | 144 dy -= fXform[i].fSSin*anchorX + fXform[i].fSCos*anchorY; |
| 145 fXform[i].fTx += dx; | 145 fXform[i].fTx += dx; |
| 146 fXform[i].fTy += dy; | 146 fXform[i].fTy += dy; |
| 147 } | 147 } |
| 148 | 148 |
| 149 fProc(canvas, fAtlas.get(), fXform, fTex, nullptr, kGrid*kGrid+1, nullpt
r, &paint); | 149 fProc(canvas, fAtlas.get(), fXform, fTex, nullptr, kGrid*kGrid+1, nullpt
r, &paint); |
| 150 paint.setColor(SK_ColorBLACK); | 150 paint.setColor(SK_ColorBLACK); |
| 151 canvas->drawRect(SkRect::MakeXYWH(0, 0, 200, 24), paint); | 151 canvas->drawRect(SkRect::MakeXYWH(0, 0, 200, 24), paint); |
| 152 paint.setColor(SK_ColorWHITE); | 152 paint.setColor(SK_ColorWHITE); |
| 153 canvas->drawText(outString.c_str(), outString.size(), 5, 15, paint); | 153 canvas->drawText(outString.c_str(), outString.size(), 5, 15, paint); |
| 154 | 154 |
| 155 this->inval(nullptr); | 155 this->inval(nullptr); |
| 156 } | 156 } |
| 157 | 157 |
| 158 #if 0 | 158 #if 0 |
| 159 // TODO: switch over to use this for our animation | 159 // TODO: switch over to use this for our animation |
| 160 bool onAnimate(const SkAnimTimer& timer) override { | 160 bool onAnimate(const SkAnimTimer& timer) override { |
| 161 SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360)); | 161 SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360)); |
| 162 fAnimatingDrawable->setSweep(angle); | 162 fAnimatingDrawable->setSweep(angle); |
| 163 return true; | 163 return true; |
| 164 } | 164 } |
| 165 #endif | 165 #endif |
| 166 | 166 |
| 167 private: | 167 private: |
| 168 const char* fName; | 168 const char* fName; |
| 169 DrawAtlasProc fProc; | 169 DrawAtlasProc fProc; |
| 170 | 170 |
| 171 sk_sp<SkImage> fAtlas; | 171 sk_sp<SkImage> fAtlas; |
| 172 SkRSXform fXform[kGrid*kGrid+1]; | 172 SkRSXform fXform[kGrid*kGrid+1]; |
| 173 SkRect fTex[kGrid*kGrid+1]; | 173 SkRect fTex[kGrid*kGrid+1]; |
| 174 WallTimer fTimer; | 174 WallTimer fTimer; |
| 175 float fTimes[32]; | 175 float fTimes[32]; |
| 176 int fCurrentTime; | 176 int fCurrentTime; |
| 177 | 177 |
| 178 | 178 |
| 179 typedef SampleView INHERITED; | 179 typedef SampleView INHERITED; |
| 180 }; | 180 }; |
| 181 | 181 |
| 182 ////////////////////////////////////////////////////////////////////////////// | 182 ////////////////////////////////////////////////////////////////////////////// |
| 183 | 183 |
| 184 DEF_SAMPLE( return new DrawShipView("DrawShip", draw_atlas); ) | 184 DEF_SAMPLE( return new DrawShipView("DrawShip", draw_atlas); ) |
| 185 DEF_SAMPLE( return new DrawShipView("DrawShipSim", draw_atlas_sim); ) | 185 DEF_SAMPLE( return new DrawShipView("DrawShipSim", draw_atlas_sim); ) |
| OLD | NEW |