blob: 9b96e67e6a8d7ec7de336d5771660ce0587bb675 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#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"
14#include "tools/timer/AnimTimer.h"
15#include "tools/timer/Timer.h"
jvanverth2f5bb3a2015-10-05 11:05:07 -070016
17#include <stdio.h>
18
19static const int kGrid = 100;
20static const int kWidth = 960;
21static const int kHeight = 640;
22
jvanverth9011bcf2015-10-07 10:43:05 -070023typedef void (*DrawAtlasProc)(SkCanvas*, SkImage*, const SkRSXform[], const SkRect[],
24const SkColor[], int, const SkRect*, const SkPaint*);
jvanverth2f5bb3a2015-10-05 11:05:07 -070025
jvanverth9011bcf2015-10-07 10:43:05 -070026static void draw_atlas(SkCanvas* canvas, SkImage* atlas, const SkRSXform xform[],
27 const SkRect tex[], const SkColor colors[], int count, const SkRect* cull,
28 const SkPaint* paint) {
Mike Reed7d954ad2016-10-28 15:42:34 -040029 canvas->drawAtlas(atlas, xform, tex, colors, count, SkBlendMode::kModulate, cull, paint);
jvanverth9011bcf2015-10-07 10:43:05 -070030}
31
32static void draw_atlas_sim(SkCanvas* canvas, SkImage* atlas, const SkRSXform xform[],
33 const SkRect tex[], const SkColor colors[], int count, const SkRect* cull,
34 const SkPaint* paint) {
35 for (int i = 0; i < count; ++i) {
36 SkMatrix matrix;
37 matrix.setRSXform(xform[i]);
halcanary9d524f22016-03-29 09:03:52 -070038
jvanverth9011bcf2015-10-07 10:43:05 -070039 canvas->save();
40 canvas->concat(matrix);
41 canvas->drawImageRect(atlas, tex[i], tex[i].makeOffset(-tex[i].x(), -tex[i].y()), paint,
42 SkCanvas::kFast_SrcRectConstraint);
43 canvas->restore();
44 }
45}
46
47
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040048class DrawShipView : public Sample {
jvanverth2f5bb3a2015-10-05 11:05:07 -070049public:
jvanverth9011bcf2015-10-07 10:43:05 -070050 DrawShipView(const char name[], DrawAtlasProc proc) : fName(name), fProc(proc) {
Hal Canaryc465d132017-12-08 10:21:31 -050051 fAtlas = GetResourceAsImage("images/ship.png");
jvanverth2f5bb3a2015-10-05 11:05:07 -070052 if (!fAtlas) {
53 SkDebugf("\nCould not decode file ship.png. Falling back to penguin mode.\n");
Hal Canaryc465d132017-12-08 10:21:31 -050054 fAtlas = GetResourceAsImage("images/baby_tux.png");
jvanverth2f5bb3a2015-10-05 11:05:07 -070055 if (!fAtlas) {
56 SkDebugf("\nCould not decode file baby_tux.png. Did you forget"
57 " to set the resourcePath?\n");
58 return;
59 }
60 }
halcanary9d524f22016-03-29 09:03:52 -070061
jvanverth2f5bb3a2015-10-05 11:05:07 -070062 SkScalar anchorX = fAtlas->width()*0.5f;
63 SkScalar anchorY = fAtlas->height()*0.5f;
64 int currIndex = 0;
65 for (int x = 0; x < kGrid; x++) {
66 for (int y = 0; y < kGrid; y++) {
67 float xPos = (x / (kGrid - 1.0f)) * kWidth;
68 float yPos = (y / (kGrid - 1.0f)) * kWidth;
halcanary9d524f22016-03-29 09:03:52 -070069
jvanverth2f5bb3a2015-10-05 11:05:07 -070070 fTex[currIndex] = SkRect::MakeLTRB(0.0f, 0.0f,
71 SkIntToScalar(fAtlas->width()),
72 SkIntToScalar(fAtlas->height()));
jvanverth629162d2015-11-08 08:07:24 -080073 fXform[currIndex] = SkRSXform::MakeFromRadians(0.1f, SK_ScalarPI*0.5f,
jvanverth2f5bb3a2015-10-05 11:05:07 -070074 xPos, yPos, anchorX, anchorY);
75 currIndex++;
76 }
77 }
78 fTex[currIndex] = SkRect::MakeLTRB(0.0f, 0.0f,
79 SkIntToScalar(fAtlas->width()),
80 SkIntToScalar(fAtlas->height()));
jvanverth629162d2015-11-08 08:07:24 -080081 fXform[currIndex] = SkRSXform::MakeFromRadians(0.5f, SK_ScalarPI*0.5f,
jvanverth2f5bb3a2015-10-05 11:05:07 -070082 kWidth*0.5f, kHeight*0.5f, anchorX, anchorY);
halcanary9d524f22016-03-29 09:03:52 -070083
jvanverth2f5bb3a2015-10-05 11:05:07 -070084 fCurrentTime = 0;
85 fTimer.start();
86 }
87
88 ~DrawShipView() override {}
halcanary9d524f22016-03-29 09:03:52 -070089
jvanverth2f5bb3a2015-10-05 11:05:07 -070090protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040091 bool onQuery(Sample::Event* evt) override {
92 if (Sample::TitleQ(*evt)) {
93 Sample::TitleR(evt, fName);
jvanverth2f5bb3a2015-10-05 11:05:07 -070094 return true;
95 }
96 return this->INHERITED::onQuery(evt);
97 }
halcanary9d524f22016-03-29 09:03:52 -070098
jvanverth2f5bb3a2015-10-05 11:05:07 -070099 void onDrawContent(SkCanvas* canvas) override {
100 const float kCosDiff = 0.99984769515f;
101 const float kSinDiff = 0.01745240643f;
halcanary9d524f22016-03-29 09:03:52 -0700102
jvanverth2f5bb3a2015-10-05 11:05:07 -0700103 if (!fAtlas) {
104 return;
105 }
halcanary9d524f22016-03-29 09:03:52 -0700106
jvanverth2f5bb3a2015-10-05 11:05:07 -0700107 SkPaint paint;
108 paint.setFilterQuality(kLow_SkFilterQuality);
109 paint.setColor(SK_ColorWHITE);
Mike Reed89126e42019-01-03 12:59:14 -0500110
111 SkFont font;
112 font.setSize(15.0f);
halcanary9d524f22016-03-29 09:03:52 -0700113
jvanverth2f5bb3a2015-10-05 11:05:07 -0700114 fTimer.end();
halcanary9d524f22016-03-29 09:03:52 -0700115
jvanverth2f5bb3a2015-10-05 11:05:07 -0700116 fTimes[fCurrentTime] = (float)(fTimer.fWall);
117 fCurrentTime = (fCurrentTime + 1) & 0x1f;
halcanary9d524f22016-03-29 09:03:52 -0700118
jvanverth2f5bb3a2015-10-05 11:05:07 -0700119 float meanTime = 0.0f;
120 for (int i = 0; i < 32; ++i) {
121 meanTime += fTimes[i];
122 }
123 meanTime /= 32.f;
124 SkString outString("fps: ");
125 SkScalar fps = 1000.f/meanTime;
126 outString.appendScalar(fps);
127 outString.append(" ms: ");
128 outString.appendScalar(meanTime);
halcanary9d524f22016-03-29 09:03:52 -0700129
jvanverth2f5bb3a2015-10-05 11:05:07 -0700130 fTimer.start();
halcanary9d524f22016-03-29 09:03:52 -0700131
jvanverth2f5bb3a2015-10-05 11:05:07 -0700132 SkScalar anchorX = fAtlas->width()*0.5f;
133 SkScalar anchorY = fAtlas->height()*0.5f;
134 for (int i = 0; i < kGrid*kGrid+1; ++i) {
135 SkScalar c = fXform[i].fSCos;
136 SkScalar s = fXform[i].fSSin;
halcanary9d524f22016-03-29 09:03:52 -0700137
jvanverth2f5bb3a2015-10-05 11:05:07 -0700138 SkScalar dx = c*anchorX - s*anchorY;
139 SkScalar dy = s*anchorX + c*anchorY;
halcanary9d524f22016-03-29 09:03:52 -0700140
jvanverth2f5bb3a2015-10-05 11:05:07 -0700141 fXform[i].fSCos = kCosDiff*c - kSinDiff*s;
142 fXform[i].fSSin = kSinDiff*c + kCosDiff*s;
halcanary9d524f22016-03-29 09:03:52 -0700143
jvanverth2f5bb3a2015-10-05 11:05:07 -0700144 dx -= fXform[i].fSCos*anchorX - fXform[i].fSSin*anchorY;
145 dy -= fXform[i].fSSin*anchorX + fXform[i].fSCos*anchorY;
146 fXform[i].fTx += dx;
147 fXform[i].fTy += dy;
148 }
halcanary9d524f22016-03-29 09:03:52 -0700149
reed9ce9d672016-03-17 10:51:11 -0700150 fProc(canvas, fAtlas.get(), fXform, fTex, nullptr, kGrid*kGrid+1, nullptr, &paint);
jvanverth629162d2015-11-08 08:07:24 -0800151 paint.setColor(SK_ColorBLACK);
152 canvas->drawRect(SkRect::MakeXYWH(0, 0, 200, 24), paint);
153 paint.setColor(SK_ColorWHITE);
Hal Canary89a644b2019-01-07 09:36:09 -0500154 canvas->drawString(outString, 5, 15, font, paint);
jvanverth2f5bb3a2015-10-05 11:05:07 -0700155 }
156
157#if 0
158 // TODO: switch over to use this for our animation
Mike Kleincd5104e2019-03-20 11:55:08 -0500159 bool onAnimate(const AnimTimer& timer) override {
jvanverth2f5bb3a2015-10-05 11:05:07 -0700160 SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360));
161 fAnimatingDrawable->setSweep(angle);
162 return true;
163 }
164#endif
165
166private:
jvanverth9011bcf2015-10-07 10:43:05 -0700167 const char* fName;
168 DrawAtlasProc fProc;
halcanary9d524f22016-03-29 09:03:52 -0700169
reed9ce9d672016-03-17 10:51:11 -0700170 sk_sp<SkImage> fAtlas;
jvanverth2f5bb3a2015-10-05 11:05:07 -0700171 SkRSXform fXform[kGrid*kGrid+1];
172 SkRect fTex[kGrid*kGrid+1];
173 WallTimer fTimer;
174 float fTimes[32];
175 int fCurrentTime;
halcanary9d524f22016-03-29 09:03:52 -0700176
177
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400178 typedef Sample INHERITED;
jvanverth2f5bb3a2015-10-05 11:05:07 -0700179};
180
181//////////////////////////////////////////////////////////////////////////////
182
jvanverth9011bcf2015-10-07 10:43:05 -0700183DEF_SAMPLE( return new DrawShipView("DrawShip", draw_atlas); )
184DEF_SAMPLE( return new DrawShipView("DrawShipSim", draw_atlas_sim); )