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" |
Mike Reed | 839eef3 | 2020-12-23 11:18:24 -0500 | [diff] [blame] | 13 | #include "src/core/SkPaintPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "tools/Resources.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "tools/timer/Timer.h" |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 16 | |
| 17 | #include <stdio.h> |
| 18 | |
| 19 | static const int kGrid = 100; |
| 20 | static const int kWidth = 960; |
| 21 | static const int kHeight = 640; |
| 22 | |
jvanverth | 9011bcf | 2015-10-07 10:43:05 -0700 | [diff] [blame] | 23 | typedef void (*DrawAtlasProc)(SkCanvas*, SkImage*, const SkRSXform[], const SkRect[], |
Mike Reed | e02d7f8 | 2021-01-21 22:25:21 -0500 | [diff] [blame] | 24 | const SkColor[], int, const SkRect*, const SkSamplingOptions&, const SkPaint*); |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 25 | |
jvanverth | 9011bcf | 2015-10-07 10:43:05 -0700 | [diff] [blame] | 26 | static void draw_atlas(SkCanvas* canvas, SkImage* atlas, const SkRSXform xform[], |
| 27 | const SkRect tex[], const SkColor colors[], int count, const SkRect* cull, |
Mike Reed | e02d7f8 | 2021-01-21 22:25:21 -0500 | [diff] [blame] | 28 | const SkSamplingOptions& sampling, const SkPaint* paint) { |
| 29 | canvas->drawAtlas(atlas, xform, tex, colors, count, SkBlendMode::kModulate, sampling, |
| 30 | cull, paint); |
jvanverth | 9011bcf | 2015-10-07 10:43:05 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | static void draw_atlas_sim(SkCanvas* canvas, SkImage* atlas, const SkRSXform xform[], |
| 34 | const SkRect tex[], const SkColor colors[], int count, const SkRect* cull, |
Mike Reed | e02d7f8 | 2021-01-21 22:25:21 -0500 | [diff] [blame] | 35 | const SkSamplingOptions& sampling, const SkPaint* paint) { |
jvanverth | 9011bcf | 2015-10-07 10:43:05 -0700 | [diff] [blame] | 36 | for (int i = 0; i < count; ++i) { |
| 37 | SkMatrix matrix; |
| 38 | matrix.setRSXform(xform[i]); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 39 | |
jvanverth | 9011bcf | 2015-10-07 10:43:05 -0700 | [diff] [blame] | 40 | canvas->save(); |
| 41 | canvas->concat(matrix); |
Mike Reed | e02d7f8 | 2021-01-21 22:25:21 -0500 | [diff] [blame] | 42 | canvas->drawImageRect(atlas, tex[i], tex[i].makeOffset(-tex[i].x(), -tex[i].y()), |
| 43 | sampling, paint, SkCanvas::kFast_SrcRectConstraint); |
jvanverth | 9011bcf | 2015-10-07 10:43:05 -0700 | [diff] [blame] | 44 | canvas->restore(); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 49 | class DrawShipView : public Sample { |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 50 | public: |
jvanverth | 9011bcf | 2015-10-07 10:43:05 -0700 | [diff] [blame] | 51 | DrawShipView(const char name[], DrawAtlasProc proc) : fName(name), fProc(proc) { |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 52 | fAtlas = GetResourceAsImage("images/ship.png"); |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 53 | if (!fAtlas) { |
| 54 | 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] | 55 | fAtlas = GetResourceAsImage("images/baby_tux.png"); |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 56 | if (!fAtlas) { |
| 57 | SkDebugf("\nCould not decode file baby_tux.png. Did you forget" |
| 58 | " to set the resourcePath?\n"); |
| 59 | return; |
| 60 | } |
| 61 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 62 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 63 | SkScalar anchorX = fAtlas->width()*0.5f; |
| 64 | SkScalar anchorY = fAtlas->height()*0.5f; |
| 65 | int currIndex = 0; |
| 66 | for (int x = 0; x < kGrid; x++) { |
| 67 | for (int y = 0; y < kGrid; y++) { |
| 68 | float xPos = (x / (kGrid - 1.0f)) * kWidth; |
| 69 | float yPos = (y / (kGrid - 1.0f)) * kWidth; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 70 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 71 | fTex[currIndex] = SkRect::MakeLTRB(0.0f, 0.0f, |
| 72 | SkIntToScalar(fAtlas->width()), |
| 73 | SkIntToScalar(fAtlas->height())); |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 74 | fXform[currIndex] = SkRSXform::MakeFromRadians(0.1f, SK_ScalarPI*0.5f, |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 75 | xPos, yPos, anchorX, anchorY); |
| 76 | currIndex++; |
| 77 | } |
| 78 | } |
| 79 | fTex[currIndex] = SkRect::MakeLTRB(0.0f, 0.0f, |
| 80 | SkIntToScalar(fAtlas->width()), |
| 81 | SkIntToScalar(fAtlas->height())); |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 82 | fXform[currIndex] = SkRSXform::MakeFromRadians(0.5f, SK_ScalarPI*0.5f, |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 83 | kWidth*0.5f, kHeight*0.5f, anchorX, anchorY); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 84 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 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: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 90 | SkString name() override { return SkString(fName); } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 91 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 92 | void onDrawContent(SkCanvas* canvas) override { |
| 93 | const float kCosDiff = 0.99984769515f; |
| 94 | const float kSinDiff = 0.01745240643f; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 95 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 96 | if (!fAtlas) { |
| 97 | return; |
| 98 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 99 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 100 | SkPaint paint; |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 101 | paint.setColor(SK_ColorWHITE); |
Mike Reed | 89126e4 | 2019-01-03 12:59:14 -0500 | [diff] [blame] | 102 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 103 | SkScalar anchorX = fAtlas->width()*0.5f; |
| 104 | SkScalar anchorY = fAtlas->height()*0.5f; |
| 105 | for (int i = 0; i < kGrid*kGrid+1; ++i) { |
| 106 | SkScalar c = fXform[i].fSCos; |
| 107 | SkScalar s = fXform[i].fSSin; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 108 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 109 | SkScalar dx = c*anchorX - s*anchorY; |
| 110 | SkScalar dy = s*anchorX + c*anchorY; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 111 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 112 | fXform[i].fSCos = kCosDiff*c - kSinDiff*s; |
| 113 | fXform[i].fSSin = kSinDiff*c + kCosDiff*s; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 114 | |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 115 | dx -= fXform[i].fSCos*anchorX - fXform[i].fSSin*anchorY; |
| 116 | dy -= fXform[i].fSSin*anchorX + fXform[i].fSCos*anchorY; |
| 117 | fXform[i].fTx += dx; |
| 118 | fXform[i].fTy += dy; |
| 119 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 120 | |
Mike Reed | e02d7f8 | 2021-01-21 22:25:21 -0500 | [diff] [blame] | 121 | fProc(canvas, fAtlas.get(), fXform, fTex, nullptr, kGrid*kGrid+1, nullptr, |
| 122 | SkSamplingOptions(SkFilterMode::kLinear), &paint); |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 125 | bool onAnimate(double nanos) override { |
Ben Wagner | acf98df | 2019-07-12 12:51:44 -0400 | [diff] [blame] | 126 | //TODO: use nanos |
| 127 | //SkScalar angle = SkDoubleToScalar(fmod(1e-9 * nanos * 360 / 24, 360)); |
| 128 | //fAnimatingDrawable->setSweep(angle); |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 129 | return true; |
| 130 | } |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 131 | |
| 132 | private: |
jvanverth | 9011bcf | 2015-10-07 10:43:05 -0700 | [diff] [blame] | 133 | const char* fName; |
| 134 | DrawAtlasProc fProc; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 135 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 136 | sk_sp<SkImage> fAtlas; |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 137 | SkRSXform fXform[kGrid*kGrid+1]; |
| 138 | SkRect fTex[kGrid*kGrid+1]; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 139 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 140 | using INHERITED = Sample; |
jvanverth | 2f5bb3a | 2015-10-05 11:05:07 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | ////////////////////////////////////////////////////////////////////////////// |
| 144 | |
jvanverth | 9011bcf | 2015-10-07 10:43:05 -0700 | [diff] [blame] | 145 | DEF_SAMPLE( return new DrawShipView("DrawShip", draw_atlas); ) |
| 146 | DEF_SAMPLE( return new DrawShipView("DrawShipSim", draw_atlas_sim); ) |