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