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 "include/core/SkCanvas.h"
9 #include "include/core/SkFont.h"
10 #include "include/core/SkRSXform.h"
11 #include "include/core/SkSurface.h"
12 #include "src/core/SkPaintPriv.h"
13 #include "tools/DecodeUtils.h"
14 #include "tools/Resources.h"
15 #include "tools/timer/Timer.h"
16 #include "tools/viewer/Slide.h"
17
18 #include <stdio.h>
19
20 static const int kGrid = 100;
21 static const int kWidth = 960;
22 static const int kHeight = 640;
23
24 typedef void (*DrawAtlasProc)(SkCanvas*, SkImage*, const SkRSXform[], const SkRect[],
25 const SkColor[], int, const SkRect*, const SkSamplingOptions&, const SkPaint*);
26
draw_atlas(SkCanvas * canvas,SkImage * atlas,const SkRSXform xform[],const SkRect tex[],const SkColor colors[],int count,const SkRect * cull,const SkSamplingOptions & sampling,const SkPaint * paint)27 static void draw_atlas(SkCanvas* canvas, SkImage* atlas, const SkRSXform xform[],
28 const SkRect tex[], const SkColor colors[], int count, const SkRect* cull,
29 const SkSamplingOptions& sampling, const SkPaint* paint) {
30 canvas->drawAtlas(atlas, xform, tex, colors, count, SkBlendMode::kModulate, sampling,
31 cull, paint);
32 }
33
draw_atlas_sim(SkCanvas * canvas,SkImage * atlas,const SkRSXform xform[],const SkRect tex[],const SkColor colors[],int count,const SkRect * cull,const SkSamplingOptions & sampling,const SkPaint * paint)34 static void draw_atlas_sim(SkCanvas* canvas, SkImage* atlas, const SkRSXform xform[],
35 const SkRect tex[], const SkColor colors[], int count, const SkRect* cull,
36 const SkSamplingOptions& sampling, const SkPaint* paint) {
37 for (int i = 0; i < count; ++i) {
38 SkMatrix matrix;
39 matrix.setRSXform(xform[i]);
40
41 canvas->save();
42 canvas->concat(matrix);
43 canvas->drawImageRect(atlas, tex[i], tex[i].makeOffset(-tex[i].x(), -tex[i].y()),
44 sampling, paint, SkCanvas::kFast_SrcRectConstraint);
45 canvas->restore();
46 }
47 }
48
49
50 class DrawShipSlide : public Slide {
51 public:
DrawShipSlide(const char name[],DrawAtlasProc proc)52 DrawShipSlide(const char name[], DrawAtlasProc proc) : fProc(proc) {
53 fName = name;
54 }
55
56 protected:
load(SkScalar,SkScalar)57 void load(SkScalar, SkScalar) override {
58 fAtlas = ToolUtils::GetResourceAsImage("images/ship.png");
59 if (!fAtlas) {
60 SkDebugf("\nCould not decode file ship.png. Falling back to penguin mode.\n");
61 fAtlas = ToolUtils::GetResourceAsImage("images/baby_tux.png");
62 if (!fAtlas) {
63 SkDebugf("\nCould not decode file baby_tux.png. Did you forget"
64 " to set the resourcePath?\n");
65 return;
66 }
67 }
68
69 SkScalar anchorX = fAtlas->width()*0.5f;
70 SkScalar anchorY = fAtlas->height()*0.5f;
71 int currIndex = 0;
72 for (int x = 0; x < kGrid; x++) {
73 for (int y = 0; y < kGrid; y++) {
74 float xPos = (x / (kGrid - 1.0f)) * kWidth;
75 float yPos = (y / (kGrid - 1.0f)) * kWidth;
76
77 fTex[currIndex] = SkRect::MakeLTRB(0.0f, 0.0f,
78 SkIntToScalar(fAtlas->width()),
79 SkIntToScalar(fAtlas->height()));
80 fXform[currIndex] = SkRSXform::MakeFromRadians(0.1f, SK_ScalarPI*0.5f,
81 xPos, yPos, anchorX, anchorY);
82 currIndex++;
83 }
84 }
85 fTex[currIndex] = SkRect::MakeLTRB(0.0f, 0.0f,
86 SkIntToScalar(fAtlas->width()),
87 SkIntToScalar(fAtlas->height()));
88 fXform[currIndex] = SkRSXform::MakeFromRadians(0.5f, SK_ScalarPI*0.5f,
89 kWidth*0.5f, kHeight*0.5f, anchorX, anchorY);
90 }
91
unload()92 void unload() override {
93 fAtlas = nullptr;
94 }
95
draw(SkCanvas * canvas)96 void draw(SkCanvas* canvas) override {
97 const float kCosDiff = 0.99984769515f;
98 const float kSinDiff = 0.01745240643f;
99
100 if (!fAtlas) {
101 return;
102 }
103
104 SkPaint paint;
105 paint.setColor(SK_ColorWHITE);
106
107 SkScalar anchorX = fAtlas->width()*0.5f;
108 SkScalar anchorY = fAtlas->height()*0.5f;
109 for (int i = 0; i < kGrid*kGrid+1; ++i) {
110 SkScalar c = fXform[i].fSCos;
111 SkScalar s = fXform[i].fSSin;
112
113 SkScalar dx = c*anchorX - s*anchorY;
114 SkScalar dy = s*anchorX + c*anchorY;
115
116 fXform[i].fSCos = kCosDiff*c - kSinDiff*s;
117 fXform[i].fSSin = kSinDiff*c + kCosDiff*s;
118
119 dx -= fXform[i].fSCos*anchorX - fXform[i].fSSin*anchorY;
120 dy -= fXform[i].fSSin*anchorX + fXform[i].fSCos*anchorY;
121 fXform[i].fTx += dx;
122 fXform[i].fTy += dy;
123 }
124
125 fProc(canvas, fAtlas.get(), fXform, fTex, nullptr, kGrid*kGrid+1, nullptr,
126 SkSamplingOptions(SkFilterMode::kLinear), &paint);
127 }
128
animate(double nanos)129 bool animate(double nanos) override {
130 //TODO: use nanos
131 //SkScalar angle = SkDoubleToScalar(fmod(1e-9 * nanos * 360 / 24, 360));
132 //fAnimatingDrawable->setSweep(angle);
133 return true;
134 }
135
136 private:
137 DrawAtlasProc fProc;
138
139 sk_sp<SkImage> fAtlas;
140 SkRSXform fXform[kGrid*kGrid+1];
141 SkRect fTex[kGrid*kGrid+1];
142 };
143
144 //////////////////////////////////////////////////////////////////////////////
145
146 DEF_SLIDE( return new DrawShipSlide("DrawShip", draw_atlas); )
147 DEF_SLIDE( return new DrawShipSlide("DrawShipSim", draw_atlas_sim); )
148