Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
AtlasSlide.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
10#include "include/core/SkFont.h"
11#include "include/core/SkPath.h"
15#include "src/base/SkRandom.h"
18#include "tools/viewer/Slide.h"
19
20typedef void (*DrawAtlasProc)(SkCanvas*, SkImage*, const SkRSXform[], const SkRect[],
21 const SkColor[], int, const SkRect*, const SkSamplingOptions&,
22 const SkPaint*);
23
24static void draw_atlas(SkCanvas* canvas, SkImage* atlas, const SkRSXform xform[],
25 const SkRect tex[], const SkColor colors[], int count, const SkRect* cull,
26 const SkSamplingOptions& sampling, const SkPaint* paint) {
27 canvas->drawAtlas(atlas, xform, tex, colors, count, SkBlendMode::kModulate,
28 sampling, cull, paint);
29}
30
31static void draw_atlas_sim(SkCanvas* canvas, SkImage* atlas, const SkRSXform xform[],
32 const SkRect tex[], const SkColor colors[], int count, const SkRect* cull,
33 const SkSamplingOptions& sampling, const SkPaint* paint) {
34 for (int i = 0; i < count; ++i) {
35 SkMatrix matrix;
36 matrix.setRSXform(xform[i]);
37
38 canvas->save();
39 canvas->concat(matrix);
40 canvas->drawImageRect(atlas, tex[i], tex[i].makeOffset(-tex[i].x(), -tex[i].y()),
42 canvas->restore();
43 }
44}
45
46static sk_sp<SkImage> make_atlas(int atlasSize, int cellSize) {
47 SkImageInfo info = SkImageInfo::MakeN32Premul(atlasSize, atlasSize);
49 SkCanvas* canvas = surface->getCanvas();
50
52 SkRandom rand;
53
54 const SkScalar half = cellSize * SK_ScalarHalf;
55 const char* s = "01234567890!@#$%^&*=+<>?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
57
58 int i = 0;
59 for (int y = 0; y < atlasSize; y += cellSize) {
60 for (int x = 0; x < atlasSize; x += cellSize) {
61 paint.setColor(rand.nextU());
62 paint.setAlpha(0xFF);
63 int index = i % strlen(s);
64 SkTextUtils::Draw(canvas, &s[index], 1, SkTextEncoding::kUTF8,
65 x + half, y + half + half/2, font, paint,
67 i += 1;
68 }
69 }
70 return surface->makeImageSnapshot();
71}
72
74 enum {
75 kMaxScale = 2,
76 kCellSize = 32,
77 kAtlasSize = 512,
78 };
79
80 struct Rec {
81 SkPoint fCenter;
82 SkVector fVelocity;
83 SkScalar fScale;
84 SkScalar fDScale;
85 SkScalar fRadian;
86 SkScalar fDRadian;
87 SkScalar fAlpha;
88 SkScalar fDAlpha;
89
90 void advance(const SkRect& bounds) {
91 fCenter += fVelocity;
92 if (fCenter.fX > bounds.right()) {
93 SkASSERT(fVelocity.fX > 0);
94 fVelocity.fX = -fVelocity.fX;
95 } else if (fCenter.fX < bounds.left()) {
96 SkASSERT(fVelocity.fX < 0);
97 fVelocity.fX = -fVelocity.fX;
98 }
99 if (fCenter.fY > bounds.bottom()) {
100 if (fVelocity.fY > 0) {
101 fVelocity.fY = -fVelocity.fY;
102 }
103 } else if (fCenter.fY < bounds.top()) {
104 if (fVelocity.fY < 0) {
105 fVelocity.fY = -fVelocity.fY;
106 }
107 }
108
109 fScale += fDScale;
110 if (fScale > 2 || fScale < SK_Scalar1/2) {
111 fDScale = -fDScale;
112 }
113
114 fRadian += fDRadian;
115 fRadian = SkScalarMod(fRadian, 2 * SK_ScalarPI);
116
117 fAlpha += fDAlpha;
118 if (fAlpha > 1) {
119 fAlpha = 1;
120 fDAlpha = -fDAlpha;
121 } else if (fAlpha < 0) {
122 fAlpha = 0;
123 fDAlpha = -fDAlpha;
124 }
125 }
126
127 SkRSXform asRSXform() const {
128 return SkRSXform::MakeFromRadians(fScale, fRadian, fCenter.x(), fCenter.y(),
130 }
131 };
132
133 DrawAtlasProc fProc;
134
135 enum {
136 N = 256,
137 };
138
139 sk_sp<SkImage> fAtlas;
140 Rec fRec[N];
141 SkRect fTex[N];
142 SkRect fBounds;
143 bool fUseColors;
144
145public:
147 : fProc(proc), fBounds(r), fUseColors(false)
148 {
149 SkRandom rand;
150 fAtlas = make_atlas(kAtlasSize, kCellSize);
151 const SkScalar kMaxSpeed = 5;
152 const SkScalar cell = SkIntToScalar(kCellSize);
153 int i = 0;
154 for (int y = 0; y < kAtlasSize; y += kCellSize) {
155 for (int x = 0; x < kAtlasSize; x += kCellSize) {
156 const SkScalar sx = SkIntToScalar(x);
157 const SkScalar sy = SkIntToScalar(y);
158 fTex[i].setXYWH(sx, sy, cell, cell);
159
160 fRec[i].fCenter.set(sx + cell/2, sy + 3*cell/4);
161 fRec[i].fVelocity.fX = rand.nextSScalar1() * kMaxSpeed;
162 fRec[i].fVelocity.fY = rand.nextSScalar1() * kMaxSpeed;
163 fRec[i].fScale = 1;
164 fRec[i].fDScale = rand.nextSScalar1() / 16;
165 fRec[i].fRadian = 0;
166 fRec[i].fDRadian = rand.nextSScalar1() / 8;
167 fRec[i].fAlpha = rand.nextUScalar1();
168 fRec[i].fDAlpha = rand.nextSScalar1() / 10;
169 i += 1;
170 }
171 }
172 }
173
175 fUseColors = !fUseColors;
176 }
177
178protected:
179 void onDraw(SkCanvas* canvas) override {
180 SkRSXform xform[N];
181 SkColor colors[N];
182
183 for (int i = 0; i < N; ++i) {
184 fRec[i].advance(fBounds);
185 xform[i] = fRec[i].asRSXform();
186 if (fUseColors) {
187 colors[i] = SkColorSetARGB((int)(fRec[i].fAlpha * 0xFF), 0xFF, 0xFF, 0xFF);
188 }
189 }
192
193 const SkRect cull = this->getBounds();
194 const SkColor* colorsPtr = fUseColors ? colors : nullptr;
195 fProc(canvas, fAtlas.get(), xform, fTex, colorsPtr, N, &cull, sampling, &paint);
196 }
197
198 SkRect onGetBounds() override {
199 const SkScalar border = kMaxScale * kCellSize;
200 SkRect r = fBounds;
201 r.outset(border, border);
202 return r;
203 }
204};
205
206class DrawAtlasSlide : public Slide {
207 DrawAtlasProc fProc;
208 sk_sp<DrawAtlasDrawable> fDrawable;
209
210public:
211 DrawAtlasSlide(const char name[], DrawAtlasProc proc) : fProc(proc) { fName = name; }
212
213 bool onChar(SkUnichar uni) override {
214 switch (uni) {
215 case 'C': fDrawable->toggleUseColors(); return true;
216 default: break;
217 }
218 return false;
219 }
220
221 void draw(SkCanvas* canvas) override {
222 canvas->drawDrawable(fDrawable.get());
223 }
224
225 bool animate(double /*nanos*/) override { return true; }
226#if 0
227 // TODO: switch over to use this for our animation
228 bool animate(double nanos) override {
229 SkScalar angle = SkDoubleToScalar(fmod(1e-9 * nanos * 360 / 24, 360));
230 fAnimatingDrawable->setSweep(angle);
231 return true;
232 }
233#endif
234
235 void load(SkScalar winWidth, SkScalar winHeight) override {
236 fDrawable = sk_make_sp<DrawAtlasDrawable>(fProc, SkRect::Make(this->getDimensions()));
237 }
238
239 SkISize getDimensions() const override { return {640, 480}; }
240};
241
242//////////////////////////////////////////////////////////////////////////////
243
244DEF_SLIDE( return new DrawAtlasSlide("DrawAtlas", draw_atlas); )
245DEF_SLIDE( return new DrawAtlasSlide("DrawAtlasSim", draw_atlas_sim); )
void(* DrawAtlasProc)(SkCanvas *, SkImage *, const SkRSXform[], const SkRect[], const SkColor[], int, const SkRect *, const SkSamplingOptions &, const SkPaint *)
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)
static sk_sp< SkImage > make_atlas(int atlasSize, int cellSize)
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)
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
int count
IsFiniteProc fProc
const SkRect fBounds
#define SkASSERT(cond)
Definition SkAssert.h:116
@ kModulate
r = s*d
uint32_t SkColor
Definition SkColor.h:37
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition SkColor.h:49
@ kUTF8
uses bytes to represent UTF-8 or ASCII
#define SkScalarMod(x, y)
Definition SkScalar.h:41
#define SK_Scalar1
Definition SkScalar.h:18
#define SkScalarHalf(a)
Definition SkScalar.h:75
#define SK_ScalarHalf
Definition SkScalar.h:19
#define SkDoubleToScalar(x)
Definition SkScalar.h:64
#define SkIntToScalar(x)
Definition SkScalar.h:57
#define SK_ScalarPI
Definition SkScalar.h:21
int32_t SkUnichar
Definition SkTypes.h:175
#define DEF_SLIDE(code)
Definition Slide.h:25
Type::kYUV Type::kRGBA() int(0.7 *637)
#define N
Definition beziers.cpp:19
SkRect onGetBounds() override
void onDraw(SkCanvas *canvas) override
DrawAtlasDrawable(DrawAtlasProc proc, const SkRect &r)
DrawAtlasSlide(const char name[], DrawAtlasProc proc)
bool onChar(SkUnichar uni) override
SkISize getDimensions() const override
void load(SkScalar winWidth, SkScalar winHeight) override
bool animate(double) override
void draw(SkCanvas *canvas) override
void restore()
Definition SkCanvas.cpp:465
void drawDrawable(SkDrawable *drawable, const SkMatrix *matrix=nullptr)
@ 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)
int save()
Definition SkCanvas.cpp:451
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)
void concat(const SkMatrix &matrix)
SkRect getBounds()
uint32_t nextU()
Definition SkRandom.h:42
SkScalar nextUScalar1()
Definition SkRandom.h:101
SkScalar nextSScalar1()
Definition SkRandom.h:113
static void Draw(SkCanvas *, const void *text, size_t size, SkTextEncoding, SkScalar x, SkScalar y, const SkFont &, const SkPaint &, Align=kLeft_Align)
Definition Slide.h:29
SkString fName
Definition Slide.h:54
T * get() const
Definition SkRefCnt.h:303
const Paint & paint
VkSurfaceKHR surface
Definition main.cc:49
float SkScalar
Definition extension.cpp:12
struct MyStruct s
const int kCellSize
const char * name
Definition fuchsia.cc:50
double y
double x
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
sk_sp< SkTypeface > DefaultTypeface()
static SkImageInfo MakeN32Premul(int width, int height)
float fX
x-axis value
float fY
y-axis value
constexpr float y() const
constexpr float x() const
static SkRSXform MakeFromRadians(SkScalar scale, SkScalar radians, SkScalar tx, SkScalar ty, SkScalar ax, SkScalar ay)
Definition SkRSXform.h:35
static SkRect Make(const SkISize &size)
Definition SkRect.h:669
void setXYWH(float x, float y, float width, float height)
Definition SkRect.h:931
void outset(float dx, float dy)
Definition SkRect.h:1077