Flutter Engine
The Flutter Engine
ShipSlide.cpp
Go to the documentation of this file.
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
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
20static const int kGrid = 100;
21static const int kWidth = 960;
22static const int kHeight = 640;
23
24typedef void (*DrawAtlasProc)(SkCanvas*, SkImage*, const SkRSXform[], const SkRect[],
25const SkColor[], int, const SkRect*, const SkSamplingOptions&, const SkPaint*);
26
27static 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) {
31 cull, paint);
32}
33
34static 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) {
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()),
45 canvas->restore();
46 }
47}
48
49
50class DrawShipSlide : public Slide {
51public:
52 DrawShipSlide(const char name[], DrawAtlasProc proc) : fProc(proc) {
53 fName = name;
54 }
55
56protected:
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
92 void unload() override {
93 fAtlas = nullptr;
94 }
95
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
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,
127 }
128
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
136private:
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
146DEF_SLIDE( return new DrawShipSlide("DrawShip", draw_atlas); )
147DEF_SLIDE( return new DrawShipSlide("DrawShipSim", draw_atlas_sim); )
int count
Definition: FontMgrTest.cpp:50
static const int kGrid
Definition: ShipSlide.cpp:20
void(* DrawAtlasProc)(SkCanvas *, SkImage *, const SkRSXform[], const SkRect[], const SkColor[], int, const SkRect *, const SkSamplingOptions &, const SkPaint *)
Definition: ShipSlide.cpp:24
static void 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)
Definition: ShipSlide.cpp:27
static const int kHeight
Definition: ShipSlide.cpp:22
static const int kWidth
Definition: ShipSlide.cpp:21
static void 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)
Definition: ShipSlide.cpp:34
@ kModulate
r = s*d
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
#define SkIntToScalar(x)
Definition: SkScalar.h:57
#define SK_ScalarPI
Definition: SkScalar.h:21
#define DEF_SLIDE(code)
Definition: Slide.h:25
void unload() override
Definition: ShipSlide.cpp:92
DrawShipSlide(const char name[], DrawAtlasProc proc)
Definition: ShipSlide.cpp:52
void draw(SkCanvas *canvas) override
Definition: ShipSlide.cpp:96
bool animate(double nanos) override
Definition: ShipSlide.cpp:129
void load(SkScalar, SkScalar) override
Definition: ShipSlide.cpp:57
void restore()
Definition: SkCanvas.cpp:461
@ kFast_SrcRectConstraint
sample outside bounds; faster
Definition: SkCanvas.h:1543
void drawImageRect(const SkImage *, const SkRect &src, const SkRect &dst, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint)
Definition: SkCanvas.cpp:2333
int save()
Definition: SkCanvas.cpp:447
void drawAtlas(const SkImage *atlas, const SkRSXform xform[], const SkRect tex[], const SkColor colors[], int count, SkBlendMode mode, const SkSamplingOptions &sampling, const SkRect *cullRect, const SkPaint *paint)
Definition: SkCanvas.cpp:1810
void concat(const SkMatrix &matrix)
Definition: SkCanvas.cpp:1318
int width() const
Definition: SkImage.h:285
int height() const
Definition: SkImage.h:291
Definition: Slide.h:29
SkString fName
Definition: Slide.h:54
T * get() const
Definition: SkRefCnt.h:303
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
struct MyStruct s
double y
double x
sk_sp< const SkImage > atlas
Definition: SkRecords.h:331
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
PODArray< SkColor > colors
Definition: SkRecords.h:276
SkSamplingOptions sampling
Definition: SkRecords.h:337
skia_private::AutoTArray< sk_sp< SkImageFilter > > filters TypedMatrix matrix TypedMatrix matrix SkScalar dx
Definition: SkRecords.h:208
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition: DecodeUtils.h:25
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
SkScalar fTy
Definition: SkRSXform.h:45
SkScalar fSCos
Definition: SkRSXform.h:42
static SkRSXform MakeFromRadians(SkScalar scale, SkScalar radians, SkScalar tx, SkScalar ty, SkScalar ax, SkScalar ay)
Definition: SkRSXform.h:35
SkScalar fTx
Definition: SkRSXform.h:44
SkScalar fSSin
Definition: SkRSXform.h:43
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition: SkRect.h:646