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 
8 #include "Sample.h"
9 #include "Resources.h"
10 #include "SkAnimTimer.h"
11 #include "SkCanvas.h"
12 #include "SkFont.h"
13 #include "SkRSXform.h"
14 #include "SkSurface.h"
15 #include "Timer.h"
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 
23 typedef void (*DrawAtlasProc)(SkCanvas*, SkImage*, const SkRSXform[], const SkRect[],
24 const SkColor[], int, const SkRect*, const SkPaint*);
25 
draw_atlas(SkCanvas * canvas,SkImage * atlas,const SkRSXform xform[],const SkRect tex[],const SkColor colors[],int count,const SkRect * cull,const SkPaint * paint)26 static 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, SkBlendMode::kModulate, cull, paint);
30 }
31 
draw_atlas_sim(SkCanvas * canvas,SkImage * atlas,const SkRSXform xform[],const SkRect tex[],const SkColor colors[],int count,const SkRect * cull,const SkPaint * paint)32 static 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]);
38 
39         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 
48 class DrawShipView : public Sample {
49 public:
DrawShipView(const char name[],DrawAtlasProc proc)50     DrawShipView(const char name[], DrawAtlasProc proc) : fName(name), fProc(proc) {
51         fAtlas = GetResourceAsImage("images/ship.png");
52         if (!fAtlas) {
53             SkDebugf("\nCould not decode file ship.png. Falling back to penguin mode.\n");
54             fAtlas = GetResourceAsImage("images/baby_tux.png");
55             if (!fAtlas) {
56                 SkDebugf("\nCould not decode file baby_tux.png. Did you forget"
57                          " to set the resourcePath?\n");
58                 return;
59             }
60         }
61 
62         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;
69 
70                 fTex[currIndex] = SkRect::MakeLTRB(0.0f, 0.0f,
71                                                    SkIntToScalar(fAtlas->width()),
72                                                    SkIntToScalar(fAtlas->height()));
73                 fXform[currIndex] = SkRSXform::MakeFromRadians(0.1f, SK_ScalarPI*0.5f,
74                                                                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()));
81         fXform[currIndex] = SkRSXform::MakeFromRadians(0.5f, SK_ScalarPI*0.5f,
82                                                        kWidth*0.5f, kHeight*0.5f, anchorX, anchorY);
83 
84         fCurrentTime = 0;
85         fTimer.start();
86     }
87 
~DrawShipView()88     ~DrawShipView() override {}
89 
90 protected:
onQuery(Sample::Event * evt)91     bool onQuery(Sample::Event* evt) override {
92         if (Sample::TitleQ(*evt)) {
93             Sample::TitleR(evt, fName);
94             return true;
95         }
96         return this->INHERITED::onQuery(evt);
97     }
98 
onDrawContent(SkCanvas * canvas)99     void onDrawContent(SkCanvas* canvas) override {
100         const float kCosDiff = 0.99984769515f;
101         const float kSinDiff = 0.01745240643f;
102 
103         if (!fAtlas) {
104             return;
105         }
106 
107         SkPaint paint;
108         paint.setFilterQuality(kLow_SkFilterQuality);
109         paint.setColor(SK_ColorWHITE);
110 
111         SkFont font;
112         font.setSize(15.0f);
113 
114         fTimer.end();
115 
116         fTimes[fCurrentTime] = (float)(fTimer.fWall);
117         fCurrentTime = (fCurrentTime + 1) & 0x1f;
118 
119         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);
129 
130         fTimer.start();
131 
132         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;
137 
138             SkScalar dx = c*anchorX - s*anchorY;
139             SkScalar dy = s*anchorX + c*anchorY;
140 
141             fXform[i].fSCos = kCosDiff*c - kSinDiff*s;
142             fXform[i].fSSin = kSinDiff*c + kCosDiff*s;
143 
144             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         }
149 
150         fProc(canvas, fAtlas.get(), fXform, fTex, nullptr, kGrid*kGrid+1, nullptr, &paint);
151         paint.setColor(SK_ColorBLACK);
152         canvas->drawRect(SkRect::MakeXYWH(0, 0, 200, 24), paint);
153         paint.setColor(SK_ColorWHITE);
154         canvas->drawString(outString, 5, 15, font, paint);
155     }
156 
157 #if 0
158     // TODO: switch over to use this for our animation
159     bool onAnimate(const SkAnimTimer& timer) override {
160         SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360));
161         fAnimatingDrawable->setSweep(angle);
162         return true;
163     }
164 #endif
165 
166 private:
167     const char*         fName;
168     DrawAtlasProc       fProc;
169 
170     sk_sp<SkImage> fAtlas;
171     SkRSXform   fXform[kGrid*kGrid+1];
172     SkRect      fTex[kGrid*kGrid+1];
173     WallTimer   fTimer;
174     float       fTimes[32];
175     int         fCurrentTime;
176 
177 
178     typedef Sample INHERITED;
179 };
180 
181 //////////////////////////////////////////////////////////////////////////////
182 
183 DEF_SAMPLE( return new DrawShipView("DrawShip", draw_atlas); )
184 DEF_SAMPLE( return new DrawShipView("DrawShipSim", draw_atlas_sim); )
185