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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkFont.h" |
| 10 | #include "include/core/SkRSXform.h" |
| 11 | #include "include/core/SkSurface.h" |
| 12 | #include "samplecode/Sample.h" |
| 13 | #include "tools/Resources.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "tools/timer/Timer.h" |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 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 | } |
| 84 | |
| 85 | ~DrawShipView() override {} |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 86 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 87 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 88 | SkString name() override { return SkString(fName); } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 89 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 90 | void onDrawContent(SkCanvas* canvas) override { |
| 91 | const float kCosDiff = 0.99984769515f; |
| 92 | const float kSinDiff = 0.01745240643f; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 93 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 94 | if (!fAtlas) { |
| 95 | return; |
| 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 | SkPaint paint; |
| 99 | paint.setFilterQuality(kLow_SkFilterQuality); |
| 100 | paint.setColor(SK_ColorWHITE); |
Mike Reed | 89126e4 | 2019-01-03 12:59:14 -0500 | [diff] [blame] | 101 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 102 | SkScalar anchorX = fAtlas->width()*0.5f; |
| 103 | SkScalar anchorY = fAtlas->height()*0.5f; |
| 104 | for (int i = 0; i < kGrid*kGrid+1; ++i) { |
| 105 | SkScalar c = fXform[i].fSCos; |
| 106 | SkScalar s = fXform[i].fSSin; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 107 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 108 | SkScalar dx = c*anchorX - s*anchorY; |
| 109 | SkScalar dy = s*anchorX + c*anchorY; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 110 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 111 | fXform[i].fSCos = kCosDiff*c - kSinDiff*s; |
| 112 | fXform[i].fSSin = kSinDiff*c + kCosDiff*s; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 113 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 114 | dx -= fXform[i].fSCos*anchorX - fXform[i].fSSin*anchorY; |
| 115 | dy -= fXform[i].fSSin*anchorX + fXform[i].fSCos*anchorY; |
| 116 | fXform[i].fTx += dx; |
| 117 | fXform[i].fTy += dy; |
| 118 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 119 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 120 | fProc(canvas, fAtlas.get(), fXform, fTex, nullptr, kGrid*kGrid+1, nullptr, &paint); |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 123 | bool onAnimate(double nanos) override { |
Ben Wagner | acf98df | 2019-07-12 12:51:44 -0400 | [diff] [blame] | 124 | //TODO: use nanos |
| 125 | //SkScalar angle = SkDoubleToScalar(fmod(1e-9 * nanos * 360 / 24, 360)); |
| 126 | //fAnimatingDrawable->setSweep(angle); |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 127 | return true; |
| 128 | } |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 129 | |
| 130 | private: |
jvanverth | 9011bcf | 2015-10-07 10:43:05 -0700 | [diff] [blame] | 131 | const char* fName; |
| 132 | DrawAtlasProc fProc; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 133 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 134 | sk_sp<SkImage> fAtlas; |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 135 | SkRSXform fXform[kGrid*kGrid+1]; |
| 136 | SkRect fTex[kGrid*kGrid+1]; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 137 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 138 | using INHERITED = Sample; |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | ////////////////////////////////////////////////////////////////////////////// |
| 142 | |
jvanverth | 9011bcf | 2015-10-07 10:43:05 -0700 | [diff] [blame] | 143 | DEF_SAMPLE( return new DrawShipView("DrawShip", draw_atlas); ) |
| 144 | DEF_SAMPLE( return new DrawShipView("DrawShipSim", draw_atlas_sim); ) |