jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 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 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 8 | #include "Sample.h" |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 9 | #include "Resources.h" |
| 10 | #include "SkAnimTimer.h" |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 11 | #include "SkCanvas.h" |
| 12 | #include "SkRSXform.h" |
| 13 | #include "SkSurface.h" |
| 14 | #include "Timer.h" |
| 15 | |
| 16 | #include <stdio.h> |
| 17 | |
| 18 | static const int kGrid = 100; |
| 19 | static const int kWidth = 960; |
| 20 | static const int kHeight = 640; |
| 21 | |
jvanverth | 9011bcf | 2015-10-07 10:43:05 -0700 | [diff] [blame] | 22 | typedef void (*DrawAtlasProc)(SkCanvas*, SkImage*, const SkRSXform[], const SkRect[], |
| 23 | const SkColor[], int, const SkRect*, const SkPaint*); |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 24 | |
jvanverth | 9011bcf | 2015-10-07 10:43:05 -0700 | [diff] [blame] | 25 | static void draw_atlas(SkCanvas* canvas, SkImage* atlas, const SkRSXform xform[], |
| 26 | const SkRect tex[], const SkColor colors[], int count, const SkRect* cull, |
| 27 | const SkPaint* paint) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 28 | canvas->drawAtlas(atlas, xform, tex, colors, count, SkBlendMode::kModulate, cull, paint); |
jvanverth | 9011bcf | 2015-10-07 10:43:05 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | static void draw_atlas_sim(SkCanvas* canvas, SkImage* atlas, const SkRSXform xform[], |
| 32 | const SkRect tex[], const SkColor colors[], int count, const SkRect* cull, |
| 33 | const SkPaint* paint) { |
| 34 | for (int i = 0; i < count; ++i) { |
| 35 | SkMatrix matrix; |
| 36 | matrix.setRSXform(xform[i]); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 37 | |
jvanverth | 9011bcf | 2015-10-07 10:43:05 -0700 | [diff] [blame] | 38 | canvas->save(); |
| 39 | canvas->concat(matrix); |
| 40 | canvas->drawImageRect(atlas, tex[i], tex[i].makeOffset(-tex[i].x(), -tex[i].y()), paint, |
| 41 | SkCanvas::kFast_SrcRectConstraint); |
| 42 | canvas->restore(); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 47 | class DrawShipView : public Sample { |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 48 | public: |
jvanverth | 9011bcf | 2015-10-07 10:43:05 -0700 | [diff] [blame] | 49 | DrawShipView(const char name[], DrawAtlasProc proc) : fName(name), fProc(proc) { |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 50 | fAtlas = GetResourceAsImage("images/ship.png"); |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 51 | if (!fAtlas) { |
| 52 | SkDebugf("\nCould not decode file ship.png. Falling back to penguin mode.\n"); |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 53 | fAtlas = GetResourceAsImage("images/baby_tux.png"); |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 54 | if (!fAtlas) { |
| 55 | SkDebugf("\nCould not decode file baby_tux.png. Did you forget" |
| 56 | " to set the resourcePath?\n"); |
| 57 | return; |
| 58 | } |
| 59 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 60 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 61 | SkScalar anchorX = fAtlas->width()*0.5f; |
| 62 | SkScalar anchorY = fAtlas->height()*0.5f; |
| 63 | int currIndex = 0; |
| 64 | for (int x = 0; x < kGrid; x++) { |
| 65 | for (int y = 0; y < kGrid; y++) { |
| 66 | float xPos = (x / (kGrid - 1.0f)) * kWidth; |
| 67 | float yPos = (y / (kGrid - 1.0f)) * kWidth; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 68 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 69 | fTex[currIndex] = SkRect::MakeLTRB(0.0f, 0.0f, |
| 70 | SkIntToScalar(fAtlas->width()), |
| 71 | SkIntToScalar(fAtlas->height())); |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 72 | fXform[currIndex] = SkRSXform::MakeFromRadians(0.1f, SK_ScalarPI*0.5f, |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 73 | xPos, yPos, anchorX, anchorY); |
| 74 | currIndex++; |
| 75 | } |
| 76 | } |
| 77 | fTex[currIndex] = SkRect::MakeLTRB(0.0f, 0.0f, |
| 78 | SkIntToScalar(fAtlas->width()), |
| 79 | SkIntToScalar(fAtlas->height())); |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 80 | fXform[currIndex] = SkRSXform::MakeFromRadians(0.5f, SK_ScalarPI*0.5f, |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 81 | kWidth*0.5f, kHeight*0.5f, anchorX, anchorY); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 82 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 83 | fCurrentTime = 0; |
| 84 | fTimer.start(); |
| 85 | } |
| 86 | |
| 87 | ~DrawShipView() override {} |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 88 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 89 | protected: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 90 | bool onQuery(Sample::Event* evt) override { |
| 91 | if (Sample::TitleQ(*evt)) { |
| 92 | Sample::TitleR(evt, fName); |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 93 | return true; |
| 94 | } |
| 95 | return this->INHERITED::onQuery(evt); |
| 96 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 97 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 98 | void onDrawContent(SkCanvas* canvas) override { |
| 99 | const float kCosDiff = 0.99984769515f; |
| 100 | const float kSinDiff = 0.01745240643f; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 101 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 102 | if (!fAtlas) { |
| 103 | return; |
| 104 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 105 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 106 | SkPaint paint; |
| 107 | paint.setFilterQuality(kLow_SkFilterQuality); |
| 108 | paint.setColor(SK_ColorWHITE); |
| 109 | paint.setTextSize(15.0f); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 110 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 111 | fTimer.end(); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 112 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 113 | fTimes[fCurrentTime] = (float)(fTimer.fWall); |
| 114 | fCurrentTime = (fCurrentTime + 1) & 0x1f; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 115 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 116 | float meanTime = 0.0f; |
| 117 | for (int i = 0; i < 32; ++i) { |
| 118 | meanTime += fTimes[i]; |
| 119 | } |
| 120 | meanTime /= 32.f; |
| 121 | SkString outString("fps: "); |
| 122 | SkScalar fps = 1000.f/meanTime; |
| 123 | outString.appendScalar(fps); |
| 124 | outString.append(" ms: "); |
| 125 | outString.appendScalar(meanTime); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 126 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 127 | fTimer.start(); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 128 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 129 | SkScalar anchorX = fAtlas->width()*0.5f; |
| 130 | SkScalar anchorY = fAtlas->height()*0.5f; |
| 131 | for (int i = 0; i < kGrid*kGrid+1; ++i) { |
| 132 | SkScalar c = fXform[i].fSCos; |
| 133 | SkScalar s = fXform[i].fSSin; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 134 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 135 | SkScalar dx = c*anchorX - s*anchorY; |
| 136 | SkScalar dy = s*anchorX + c*anchorY; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 137 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 138 | fXform[i].fSCos = kCosDiff*c - kSinDiff*s; |
| 139 | fXform[i].fSSin = kSinDiff*c + kCosDiff*s; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 140 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 141 | dx -= fXform[i].fSCos*anchorX - fXform[i].fSSin*anchorY; |
| 142 | dy -= fXform[i].fSSin*anchorX + fXform[i].fSCos*anchorY; |
| 143 | fXform[i].fTx += dx; |
| 144 | fXform[i].fTy += dy; |
| 145 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 146 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 147 | fProc(canvas, fAtlas.get(), fXform, fTex, nullptr, kGrid*kGrid+1, nullptr, &paint); |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 148 | paint.setColor(SK_ColorBLACK); |
| 149 | canvas->drawRect(SkRect::MakeXYWH(0, 0, 200, 24), paint); |
| 150 | paint.setColor(SK_ColorWHITE); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 151 | canvas->drawString(outString, 5, 15, paint); |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | #if 0 |
| 155 | // TODO: switch over to use this for our animation |
| 156 | bool onAnimate(const SkAnimTimer& timer) override { |
| 157 | SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360)); |
| 158 | fAnimatingDrawable->setSweep(angle); |
| 159 | return true; |
| 160 | } |
| 161 | #endif |
| 162 | |
| 163 | private: |
jvanverth | 9011bcf | 2015-10-07 10:43:05 -0700 | [diff] [blame] | 164 | const char* fName; |
| 165 | DrawAtlasProc fProc; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 166 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 167 | sk_sp<SkImage> fAtlas; |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 168 | SkRSXform fXform[kGrid*kGrid+1]; |
| 169 | SkRect fTex[kGrid*kGrid+1]; |
| 170 | WallTimer fTimer; |
| 171 | float fTimes[32]; |
| 172 | int fCurrentTime; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 173 | |
| 174 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 175 | typedef Sample INHERITED; |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 176 | }; |
| 177 | |
| 178 | ////////////////////////////////////////////////////////////////////////////// |
| 179 | |
jvanverth | 9011bcf | 2015-10-07 10:43:05 -0700 | [diff] [blame] | 180 | DEF_SAMPLE( return new DrawShipView("DrawShip", draw_atlas); ) |
| 181 | DEF_SAMPLE( return new DrawShipView("DrawShipSim", draw_atlas_sim); ) |