blob: a4871fa174480c8288827e63d50dc2147051efdf [file] [log] [blame]
jvanverth2f5bb3a2015-10-05 11:05:07 -07001/*
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 Wagnerb2c4ea62018-08-08 11:36:17 -04008#include "Sample.h"
jvanverth2f5bb3a2015-10-05 11:05:07 -07009#include "Resources.h"
10#include "SkAnimTimer.h"
jvanverth2f5bb3a2015-10-05 11:05:07 -070011#include "SkCanvas.h"
12#include "SkRSXform.h"
13#include "SkSurface.h"
14#include "Timer.h"
15
16#include <stdio.h>
17
18static const int kGrid = 100;
19static const int kWidth = 960;
20static const int kHeight = 640;
21
jvanverth9011bcf2015-10-07 10:43:05 -070022typedef void (*DrawAtlasProc)(SkCanvas*, SkImage*, const SkRSXform[], const SkRect[],
23const SkColor[], int, const SkRect*, const SkPaint*);
jvanverth2f5bb3a2015-10-05 11:05:07 -070024
jvanverth9011bcf2015-10-07 10:43:05 -070025static 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 Reed7d954ad2016-10-28 15:42:34 -040028 canvas->drawAtlas(atlas, xform, tex, colors, count, SkBlendMode::kModulate, cull, paint);
jvanverth9011bcf2015-10-07 10:43:05 -070029}
30
31static 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]);
halcanary9d524f22016-03-29 09:03:52 -070037
jvanverth9011bcf2015-10-07 10:43:05 -070038 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 Wagnerb2c4ea62018-08-08 11:36:17 -040047class DrawShipView : public Sample {
jvanverth2f5bb3a2015-10-05 11:05:07 -070048public:
jvanverth9011bcf2015-10-07 10:43:05 -070049 DrawShipView(const char name[], DrawAtlasProc proc) : fName(name), fProc(proc) {
Hal Canaryc465d132017-12-08 10:21:31 -050050 fAtlas = GetResourceAsImage("images/ship.png");
jvanverth2f5bb3a2015-10-05 11:05:07 -070051 if (!fAtlas) {
52 SkDebugf("\nCould not decode file ship.png. Falling back to penguin mode.\n");
Hal Canaryc465d132017-12-08 10:21:31 -050053 fAtlas = GetResourceAsImage("images/baby_tux.png");
jvanverth2f5bb3a2015-10-05 11:05:07 -070054 if (!fAtlas) {
55 SkDebugf("\nCould not decode file baby_tux.png. Did you forget"
56 " to set the resourcePath?\n");
57 return;
58 }
59 }
halcanary9d524f22016-03-29 09:03:52 -070060
jvanverth2f5bb3a2015-10-05 11:05:07 -070061 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;
halcanary9d524f22016-03-29 09:03:52 -070068
jvanverth2f5bb3a2015-10-05 11:05:07 -070069 fTex[currIndex] = SkRect::MakeLTRB(0.0f, 0.0f,
70 SkIntToScalar(fAtlas->width()),
71 SkIntToScalar(fAtlas->height()));
jvanverth629162d2015-11-08 08:07:24 -080072 fXform[currIndex] = SkRSXform::MakeFromRadians(0.1f, SK_ScalarPI*0.5f,
jvanverth2f5bb3a2015-10-05 11:05:07 -070073 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()));
jvanverth629162d2015-11-08 08:07:24 -080080 fXform[currIndex] = SkRSXform::MakeFromRadians(0.5f, SK_ScalarPI*0.5f,
jvanverth2f5bb3a2015-10-05 11:05:07 -070081 kWidth*0.5f, kHeight*0.5f, anchorX, anchorY);
halcanary9d524f22016-03-29 09:03:52 -070082
jvanverth2f5bb3a2015-10-05 11:05:07 -070083 fCurrentTime = 0;
84 fTimer.start();
85 }
86
87 ~DrawShipView() override {}
halcanary9d524f22016-03-29 09:03:52 -070088
jvanverth2f5bb3a2015-10-05 11:05:07 -070089protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040090 bool onQuery(Sample::Event* evt) override {
91 if (Sample::TitleQ(*evt)) {
92 Sample::TitleR(evt, fName);
jvanverth2f5bb3a2015-10-05 11:05:07 -070093 return true;
94 }
95 return this->INHERITED::onQuery(evt);
96 }
halcanary9d524f22016-03-29 09:03:52 -070097
jvanverth2f5bb3a2015-10-05 11:05:07 -070098 void onDrawContent(SkCanvas* canvas) override {
99 const float kCosDiff = 0.99984769515f;
100 const float kSinDiff = 0.01745240643f;
halcanary9d524f22016-03-29 09:03:52 -0700101
jvanverth2f5bb3a2015-10-05 11:05:07 -0700102 if (!fAtlas) {
103 return;
104 }
halcanary9d524f22016-03-29 09:03:52 -0700105
jvanverth2f5bb3a2015-10-05 11:05:07 -0700106 SkPaint paint;
107 paint.setFilterQuality(kLow_SkFilterQuality);
108 paint.setColor(SK_ColorWHITE);
109 paint.setTextSize(15.0f);
halcanary9d524f22016-03-29 09:03:52 -0700110
jvanverth2f5bb3a2015-10-05 11:05:07 -0700111 fTimer.end();
halcanary9d524f22016-03-29 09:03:52 -0700112
jvanverth2f5bb3a2015-10-05 11:05:07 -0700113 fTimes[fCurrentTime] = (float)(fTimer.fWall);
114 fCurrentTime = (fCurrentTime + 1) & 0x1f;
halcanary9d524f22016-03-29 09:03:52 -0700115
jvanverth2f5bb3a2015-10-05 11:05:07 -0700116 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);
halcanary9d524f22016-03-29 09:03:52 -0700126
jvanverth2f5bb3a2015-10-05 11:05:07 -0700127 fTimer.start();
halcanary9d524f22016-03-29 09:03:52 -0700128
jvanverth2f5bb3a2015-10-05 11:05:07 -0700129 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;
halcanary9d524f22016-03-29 09:03:52 -0700134
jvanverth2f5bb3a2015-10-05 11:05:07 -0700135 SkScalar dx = c*anchorX - s*anchorY;
136 SkScalar dy = s*anchorX + c*anchorY;
halcanary9d524f22016-03-29 09:03:52 -0700137
jvanverth2f5bb3a2015-10-05 11:05:07 -0700138 fXform[i].fSCos = kCosDiff*c - kSinDiff*s;
139 fXform[i].fSSin = kSinDiff*c + kCosDiff*s;
halcanary9d524f22016-03-29 09:03:52 -0700140
jvanverth2f5bb3a2015-10-05 11:05:07 -0700141 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 }
halcanary9d524f22016-03-29 09:03:52 -0700146
reed9ce9d672016-03-17 10:51:11 -0700147 fProc(canvas, fAtlas.get(), fXform, fTex, nullptr, kGrid*kGrid+1, nullptr, &paint);
jvanverth629162d2015-11-08 08:07:24 -0800148 paint.setColor(SK_ColorBLACK);
149 canvas->drawRect(SkRect::MakeXYWH(0, 0, 200, 24), paint);
150 paint.setColor(SK_ColorWHITE);
Cary Clark2a475ea2017-04-28 15:35:12 -0400151 canvas->drawString(outString, 5, 15, paint);
jvanverth2f5bb3a2015-10-05 11:05:07 -0700152 }
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
163private:
jvanverth9011bcf2015-10-07 10:43:05 -0700164 const char* fName;
165 DrawAtlasProc fProc;
halcanary9d524f22016-03-29 09:03:52 -0700166
reed9ce9d672016-03-17 10:51:11 -0700167 sk_sp<SkImage> fAtlas;
jvanverth2f5bb3a2015-10-05 11:05:07 -0700168 SkRSXform fXform[kGrid*kGrid+1];
169 SkRect fTex[kGrid*kGrid+1];
170 WallTimer fTimer;
171 float fTimes[32];
172 int fCurrentTime;
halcanary9d524f22016-03-29 09:03:52 -0700173
174
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400175 typedef Sample INHERITED;
jvanverth2f5bb3a2015-10-05 11:05:07 -0700176};
177
178//////////////////////////////////////////////////////////////////////////////
179
jvanverth9011bcf2015-10-07 10:43:05 -0700180DEF_SAMPLE( return new DrawShipView("DrawShip", draw_atlas); )
181DEF_SAMPLE( return new DrawShipView("DrawShipSim", draw_atlas_sim); )