blob: 1335c310765864c82a4a68b401911ee2440d5c8e [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
8#include "SampleCode.h"
9#include "Resources.h"
10#include "SkAnimTimer.h"
11#include "SkView.h"
12#include "SkCanvas.h"
13#include "SkRSXform.h"
14#include "SkSurface.h"
15#include "Timer.h"
16
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) {
29 canvas->drawAtlas(atlas, xform, tex, colors, count, SkXfermode::kModulate_Mode, cull, paint);
30}
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
48class DrawShipView : public SampleView {
jvanverth2f5bb3a2015-10-05 11:05:07 -070049public:
jvanverth9011bcf2015-10-07 10:43:05 -070050 DrawShipView(const char name[], DrawAtlasProc proc) : fName(name), fProc(proc) {
reed9ce9d672016-03-17 10:51:11 -070051 fAtlas = GetResourceAsImage("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");
reed9ce9d672016-03-17 10:51:11 -070054 fAtlas = GetResourceAsImage("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:
91 // overrides from SkEventSink
92 bool onQuery(SkEvent* evt) override {
93 if (SampleCode::TitleQ(*evt)) {
94 SampleCode::TitleR(evt, fName);
95 return true;
96 }
97 return this->INHERITED::onQuery(evt);
98 }
halcanary9d524f22016-03-29 09:03:52 -070099
jvanverth2f5bb3a2015-10-05 11:05:07 -0700100 void onDrawContent(SkCanvas* canvas) override {
101 const float kCosDiff = 0.99984769515f;
102 const float kSinDiff = 0.01745240643f;
halcanary9d524f22016-03-29 09:03:52 -0700103
jvanverth2f5bb3a2015-10-05 11:05:07 -0700104 if (!fAtlas) {
105 return;
106 }
halcanary9d524f22016-03-29 09:03:52 -0700107
jvanverth2f5bb3a2015-10-05 11:05:07 -0700108 SkPaint paint;
109 paint.setFilterQuality(kLow_SkFilterQuality);
110 paint.setColor(SK_ColorWHITE);
111 paint.setTextSize(15.0f);
halcanary9d524f22016-03-29 09:03:52 -0700112
jvanverth2f5bb3a2015-10-05 11:05:07 -0700113 fTimer.end();
halcanary9d524f22016-03-29 09:03:52 -0700114
jvanverth2f5bb3a2015-10-05 11:05:07 -0700115 fTimes[fCurrentTime] = (float)(fTimer.fWall);
116 fCurrentTime = (fCurrentTime + 1) & 0x1f;
halcanary9d524f22016-03-29 09:03:52 -0700117
jvanverth2f5bb3a2015-10-05 11:05:07 -0700118 float meanTime = 0.0f;
119 for (int i = 0; i < 32; ++i) {
120 meanTime += fTimes[i];
121 }
122 meanTime /= 32.f;
123 SkString outString("fps: ");
124 SkScalar fps = 1000.f/meanTime;
125 outString.appendScalar(fps);
126 outString.append(" ms: ");
127 outString.appendScalar(meanTime);
halcanary9d524f22016-03-29 09:03:52 -0700128
jvanverth2f5bb3a2015-10-05 11:05:07 -0700129 fTimer.start();
halcanary9d524f22016-03-29 09:03:52 -0700130
jvanverth2f5bb3a2015-10-05 11:05:07 -0700131 SkScalar anchorX = fAtlas->width()*0.5f;
132 SkScalar anchorY = fAtlas->height()*0.5f;
133 for (int i = 0; i < kGrid*kGrid+1; ++i) {
134 SkScalar c = fXform[i].fSCos;
135 SkScalar s = fXform[i].fSSin;
halcanary9d524f22016-03-29 09:03:52 -0700136
jvanverth2f5bb3a2015-10-05 11:05:07 -0700137 SkScalar dx = c*anchorX - s*anchorY;
138 SkScalar dy = s*anchorX + c*anchorY;
halcanary9d524f22016-03-29 09:03:52 -0700139
jvanverth2f5bb3a2015-10-05 11:05:07 -0700140 fXform[i].fSCos = kCosDiff*c - kSinDiff*s;
141 fXform[i].fSSin = kSinDiff*c + kCosDiff*s;
halcanary9d524f22016-03-29 09:03:52 -0700142
jvanverth2f5bb3a2015-10-05 11:05:07 -0700143 dx -= fXform[i].fSCos*anchorX - fXform[i].fSSin*anchorY;
144 dy -= fXform[i].fSSin*anchorX + fXform[i].fSCos*anchorY;
145 fXform[i].fTx += dx;
146 fXform[i].fTy += dy;
147 }
halcanary9d524f22016-03-29 09:03:52 -0700148
reed9ce9d672016-03-17 10:51:11 -0700149 fProc(canvas, fAtlas.get(), fXform, fTex, nullptr, kGrid*kGrid+1, nullptr, &paint);
jvanverth629162d2015-11-08 08:07:24 -0800150 paint.setColor(SK_ColorBLACK);
151 canvas->drawRect(SkRect::MakeXYWH(0, 0, 200, 24), paint);
152 paint.setColor(SK_ColorWHITE);
153 canvas->drawText(outString.c_str(), outString.size(), 5, 15, paint);
jvanverth2f5bb3a2015-10-05 11:05:07 -0700154
155 this->inval(nullptr);
156 }
157
158#if 0
159 // TODO: switch over to use this for our animation
160 bool onAnimate(const SkAnimTimer& timer) override {
161 SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360));
162 fAnimatingDrawable->setSweep(angle);
163 return true;
164 }
165#endif
166
167private:
jvanverth9011bcf2015-10-07 10:43:05 -0700168 const char* fName;
169 DrawAtlasProc fProc;
halcanary9d524f22016-03-29 09:03:52 -0700170
reed9ce9d672016-03-17 10:51:11 -0700171 sk_sp<SkImage> fAtlas;
jvanverth2f5bb3a2015-10-05 11:05:07 -0700172 SkRSXform fXform[kGrid*kGrid+1];
173 SkRect fTex[kGrid*kGrid+1];
174 WallTimer fTimer;
175 float fTimes[32];
176 int fCurrentTime;
halcanary9d524f22016-03-29 09:03:52 -0700177
178
jvanverth2f5bb3a2015-10-05 11:05:07 -0700179 typedef SampleView INHERITED;
180};
181
182//////////////////////////////////////////////////////////////////////////////
183
jvanverth9011bcf2015-10-07 10:43:05 -0700184DEF_SAMPLE( return new DrawShipView("DrawShip", draw_atlas); )
185DEF_SAMPLE( return new DrawShipView("DrawShipSim", draw_atlas_sim); )