2015-06-24 17:29:17 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkDrawable.h"
|
2022-04-28 15:04:30 +00:00
|
|
|
#include "include/core/SkFont.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/core/SkRSXform.h"
|
|
|
|
#include "include/core/SkSurface.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "include/utils/SkTextUtils.h"
|
|
|
|
#include "samplecode/Sample.h"
|
2020-12-19 20:14:06 +00:00
|
|
|
#include "src/core/SkPaintPriv.h"
|
2015-06-24 17:29:17 +00:00
|
|
|
|
2015-08-06 12:14:11 +00:00
|
|
|
typedef void (*DrawAtlasProc)(SkCanvas*, SkImage*, const SkRSXform[], const SkRect[],
|
2021-01-22 20:26:41 +00:00
|
|
|
const SkColor[], int, const SkRect*, const SkSamplingOptions&,
|
|
|
|
const SkPaint*);
|
2015-08-06 12:14:11 +00:00
|
|
|
|
|
|
|
static void draw_atlas(SkCanvas* canvas, SkImage* atlas, const SkRSXform xform[],
|
|
|
|
const SkRect tex[], const SkColor colors[], int count, const SkRect* cull,
|
2021-01-22 20:26:41 +00:00
|
|
|
const SkSamplingOptions& sampling, const SkPaint* paint) {
|
|
|
|
canvas->drawAtlas(atlas, xform, tex, colors, count, SkBlendMode::kModulate,
|
|
|
|
sampling, cull, paint);
|
2015-08-06 12:14:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_atlas_sim(SkCanvas* canvas, SkImage* atlas, const SkRSXform xform[],
|
|
|
|
const SkRect tex[], const SkColor colors[], int count, const SkRect* cull,
|
2021-01-22 20:26:41 +00:00
|
|
|
const SkSamplingOptions& sampling, const SkPaint* paint) {
|
2015-08-06 12:14:11 +00:00
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
SkMatrix matrix;
|
|
|
|
matrix.setRSXform(xform[i]);
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-08-06 12:14:11 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->concat(matrix);
|
2021-01-22 20:26:41 +00:00
|
|
|
canvas->drawImageRect(atlas, tex[i], tex[i].makeOffset(-tex[i].x(), -tex[i].y()),
|
|
|
|
sampling, paint, SkCanvas::kFast_SrcRectConstraint);
|
2015-08-06 12:14:11 +00:00
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
static sk_sp<SkImage> make_atlas(int atlasSize, int cellSize) {
|
2015-06-24 17:29:17 +00:00
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(atlasSize, atlasSize);
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface(SkSurface::MakeRaster(info));
|
2015-06-24 17:29:17 +00:00
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
SkRandom rand;
|
|
|
|
|
|
|
|
const SkScalar half = cellSize * SK_ScalarHalf;
|
|
|
|
const char* s = "01234567890!@#$%^&*=+<>?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
2019-01-08 19:00:08 +00:00
|
|
|
SkFont font(nullptr, 28);
|
|
|
|
|
2015-06-24 17:29:17 +00:00
|
|
|
int i = 0;
|
|
|
|
for (int y = 0; y < atlasSize; y += cellSize) {
|
|
|
|
for (int x = 0; x < atlasSize; x += cellSize) {
|
|
|
|
paint.setColor(rand.nextU());
|
|
|
|
paint.setAlpha(0xFF);
|
|
|
|
int index = i % strlen(s);
|
2019-05-07 19:38:46 +00:00
|
|
|
SkTextUtils::Draw(canvas, &s[index], 1, SkTextEncoding::kUTF8,
|
2019-01-08 19:00:08 +00:00
|
|
|
x + half, y + half + half/2, font, paint,
|
2019-01-03 20:45:53 +00:00
|
|
|
SkTextUtils::kCenter_Align);
|
2015-06-24 17:29:17 +00:00
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
2016-03-17 17:51:11 +00:00
|
|
|
return surface->makeImageSnapshot();
|
2015-06-24 17:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class DrawAtlasDrawable : public SkDrawable {
|
|
|
|
enum {
|
|
|
|
kMaxScale = 2,
|
|
|
|
kCellSize = 32,
|
|
|
|
kAtlasSize = 512,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Rec {
|
|
|
|
SkPoint fCenter;
|
|
|
|
SkVector fVelocity;
|
|
|
|
SkScalar fScale;
|
|
|
|
SkScalar fDScale;
|
|
|
|
SkScalar fRadian;
|
|
|
|
SkScalar fDRadian;
|
|
|
|
SkScalar fAlpha;
|
|
|
|
SkScalar fDAlpha;
|
|
|
|
|
|
|
|
void advance(const SkRect& bounds) {
|
|
|
|
fCenter += fVelocity;
|
|
|
|
if (fCenter.fX > bounds.right()) {
|
|
|
|
SkASSERT(fVelocity.fX > 0);
|
|
|
|
fVelocity.fX = -fVelocity.fX;
|
|
|
|
} else if (fCenter.fX < bounds.left()) {
|
|
|
|
SkASSERT(fVelocity.fX < 0);
|
|
|
|
fVelocity.fX = -fVelocity.fX;
|
|
|
|
}
|
|
|
|
if (fCenter.fY > bounds.bottom()) {
|
2015-06-25 23:25:25 +00:00
|
|
|
if (fVelocity.fY > 0) {
|
|
|
|
fVelocity.fY = -fVelocity.fY;
|
|
|
|
}
|
2015-06-24 17:29:17 +00:00
|
|
|
} else if (fCenter.fY < bounds.top()) {
|
2015-06-25 23:25:25 +00:00
|
|
|
if (fVelocity.fY < 0) {
|
|
|
|
fVelocity.fY = -fVelocity.fY;
|
|
|
|
}
|
2015-06-24 17:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fScale += fDScale;
|
|
|
|
if (fScale > 2 || fScale < SK_Scalar1/2) {
|
|
|
|
fDScale = -fDScale;
|
|
|
|
}
|
|
|
|
|
|
|
|
fRadian += fDRadian;
|
|
|
|
fRadian = SkScalarMod(fRadian, 2 * SK_ScalarPI);
|
|
|
|
|
|
|
|
fAlpha += fDAlpha;
|
|
|
|
if (fAlpha > 1) {
|
|
|
|
fAlpha = 1;
|
|
|
|
fDAlpha = -fDAlpha;
|
|
|
|
} else if (fAlpha < 0) {
|
|
|
|
fAlpha = 0;
|
|
|
|
fDAlpha = -fDAlpha;
|
|
|
|
}
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-06-24 17:29:17 +00:00
|
|
|
SkRSXform asRSXform() const {
|
2015-07-30 12:46:05 +00:00
|
|
|
return SkRSXform::MakeFromRadians(fScale, fRadian, fCenter.x(), fCenter.y(),
|
|
|
|
SkScalarHalf(kCellSize), SkScalarHalf(kCellSize));
|
2015-06-24 17:29:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-06 12:14:11 +00:00
|
|
|
DrawAtlasProc fProc;
|
|
|
|
|
2015-06-24 17:29:17 +00:00
|
|
|
enum {
|
|
|
|
N = 256,
|
|
|
|
};
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> fAtlas;
|
2015-06-24 17:29:17 +00:00
|
|
|
Rec fRec[N];
|
|
|
|
SkRect fTex[N];
|
|
|
|
SkRect fBounds;
|
|
|
|
bool fUseColors;
|
|
|
|
|
|
|
|
public:
|
2015-08-06 12:14:11 +00:00
|
|
|
DrawAtlasDrawable(DrawAtlasProc proc, const SkRect& r)
|
|
|
|
: fProc(proc), fBounds(r), fUseColors(false)
|
|
|
|
{
|
2015-06-24 17:29:17 +00:00
|
|
|
SkRandom rand;
|
2016-03-17 17:51:11 +00:00
|
|
|
fAtlas = make_atlas(kAtlasSize, kCellSize);
|
2015-06-24 17:29:17 +00:00
|
|
|
const SkScalar kMaxSpeed = 5;
|
|
|
|
const SkScalar cell = SkIntToScalar(kCellSize);
|
|
|
|
int i = 0;
|
|
|
|
for (int y = 0; y < kAtlasSize; y += kCellSize) {
|
|
|
|
for (int x = 0; x < kAtlasSize; x += kCellSize) {
|
|
|
|
const SkScalar sx = SkIntToScalar(x);
|
|
|
|
const SkScalar sy = SkIntToScalar(y);
|
|
|
|
fTex[i].setXYWH(sx, sy, cell, cell);
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-06-24 17:29:17 +00:00
|
|
|
fRec[i].fCenter.set(sx + cell/2, sy + 3*cell/4);
|
|
|
|
fRec[i].fVelocity.fX = rand.nextSScalar1() * kMaxSpeed;
|
|
|
|
fRec[i].fVelocity.fY = rand.nextSScalar1() * kMaxSpeed;
|
|
|
|
fRec[i].fScale = 1;
|
2015-07-30 12:46:05 +00:00
|
|
|
fRec[i].fDScale = rand.nextSScalar1() / 16;
|
2015-06-24 17:29:17 +00:00
|
|
|
fRec[i].fRadian = 0;
|
|
|
|
fRec[i].fDRadian = rand.nextSScalar1() / 8;
|
|
|
|
fRec[i].fAlpha = rand.nextUScalar1();
|
|
|
|
fRec[i].fDAlpha = rand.nextSScalar1() / 10;
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void toggleUseColors() {
|
|
|
|
fUseColors = !fUseColors;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
SkRSXform xform[N];
|
|
|
|
SkColor colors[N];
|
|
|
|
|
|
|
|
for (int i = 0; i < N; ++i) {
|
|
|
|
fRec[i].advance(fBounds);
|
|
|
|
xform[i] = fRec[i].asRSXform();
|
|
|
|
if (fUseColors) {
|
|
|
|
colors[i] = SkColorSetARGB((int)(fRec[i].fAlpha * 0xFF), 0xFF, 0xFF, 0xFF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SkPaint paint;
|
2021-01-22 20:26:41 +00:00
|
|
|
SkSamplingOptions sampling(SkFilterMode::kLinear);
|
2015-06-24 17:29:17 +00:00
|
|
|
|
|
|
|
const SkRect cull = this->getBounds();
|
2015-08-27 14:41:13 +00:00
|
|
|
const SkColor* colorsPtr = fUseColors ? colors : nullptr;
|
2021-01-22 20:26:41 +00:00
|
|
|
fProc(canvas, fAtlas.get(), xform, fTex, colorsPtr, N, &cull, sampling, &paint);
|
2015-06-24 17:29:17 +00:00
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-06-24 17:29:17 +00:00
|
|
|
SkRect onGetBounds() override {
|
|
|
|
const SkScalar border = kMaxScale * kCellSize;
|
|
|
|
SkRect r = fBounds;
|
|
|
|
r.outset(border, border);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = SkDrawable;
|
2015-06-24 17:29:17 +00:00
|
|
|
};
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class DrawAtlasView : public Sample {
|
2018-06-07 17:11:37 +00:00
|
|
|
const char* fName;
|
|
|
|
DrawAtlasProc fProc;
|
|
|
|
sk_sp<DrawAtlasDrawable> fDrawable;
|
2015-06-24 17:29:17 +00:00
|
|
|
|
|
|
|
public:
|
2018-06-07 17:11:37 +00:00
|
|
|
DrawAtlasView(const char name[], DrawAtlasProc proc) : fName(name), fProc(proc) { }
|
2015-06-24 17:29:17 +00:00
|
|
|
|
|
|
|
protected:
|
2019-07-03 14:55:44 +00:00
|
|
|
SkString name() override { return SkString(fName); }
|
|
|
|
|
2019-07-03 19:53:04 +00:00
|
|
|
bool onChar(SkUnichar uni) override {
|
2015-06-24 17:29:17 +00:00
|
|
|
switch (uni) {
|
2017-11-22 21:36:07 +00:00
|
|
|
case 'C': fDrawable->toggleUseColors(); return true;
|
2015-06-24 17:29:17 +00:00
|
|
|
default: break;
|
|
|
|
}
|
2019-07-03 19:53:04 +00:00
|
|
|
return false;
|
2015-06-24 17:29:17 +00:00
|
|
|
}
|
|
|
|
|
2018-06-07 17:11:37 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
fDrawable = sk_make_sp<DrawAtlasDrawable>(fProc, SkRect::MakeWH(640, 480));
|
|
|
|
}
|
|
|
|
|
2015-06-24 17:29:17 +00:00
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2018-06-07 17:11:37 +00:00
|
|
|
canvas->drawDrawable(fDrawable.get());
|
2015-06-24 17:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 20:32:53 +00:00
|
|
|
bool onAnimate(double /*nanos*/) override { return true; }
|
2015-06-24 17:29:17 +00:00
|
|
|
#if 0
|
|
|
|
// TODO: switch over to use this for our animation
|
2019-07-11 20:32:53 +00:00
|
|
|
bool onAnimate(double nanos) override {
|
|
|
|
SkScalar angle = SkDoubleToScalar(fmod(1e-9 * nanos * 360 / 24, 360));
|
2015-06-24 17:29:17 +00:00
|
|
|
fAnimatingDrawable->setSweep(angle);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = Sample;
|
2015-06-24 17:29:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-08-06 12:14:11 +00:00
|
|
|
DEF_SAMPLE( return new DrawAtlasView("DrawAtlas", draw_atlas); )
|
|
|
|
DEF_SAMPLE( return new DrawAtlasView("DrawAtlasSim", draw_atlas_sim); )
|