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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkRSXform.h"
|
|
|
|
#include "SkSurface.h"
|
|
|
|
|
|
|
|
class DrawAtlasGM : public skiagm::GM {
|
2016-03-17 17:51:11 +00:00
|
|
|
static sk_sp<SkImage> MakeAtlas(SkCanvas* caller, const SkRect& target) {
|
2015-06-24 17:29:17 +00:00
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface(caller->makeSurface(info));
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == surface) {
|
2016-03-24 01:59:25 +00:00
|
|
|
surface = SkSurface::MakeRaster(info);
|
2015-06-24 17:29:17 +00:00
|
|
|
}
|
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
// draw red everywhere, but we don't expect to see it in the draw, testing the notion
|
|
|
|
// that drawAtlas draws a subset-region of the atlas.
|
|
|
|
canvas->clear(SK_ColorRED);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setXfermodeMode(SkXfermode::kClear_Mode);
|
|
|
|
SkRect r(target);
|
|
|
|
r.inset(-1, -1);
|
|
|
|
// zero out a place (with a 1-pixel border) to land our drawing.
|
|
|
|
canvas->drawRect(r, paint);
|
2015-08-27 14:41:13 +00:00
|
|
|
paint.setXfermode(nullptr);
|
2015-06-24 17:29:17 +00:00
|
|
|
paint.setColor(SK_ColorBLUE);
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
canvas->drawOval(target, paint);
|
2016-03-17 17:51:11 +00:00
|
|
|
return surface->makeImageSnapshot();
|
2015-06-24 17:29:17 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> fAtlas;
|
2015-06-24 17:29:17 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
DrawAtlasGM() {}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-06-24 17:29:17 +00:00
|
|
|
protected:
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-06-24 17:29:17 +00:00
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("draw-atlas");
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-06-24 17:29:17 +00:00
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(640, 480);
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-06-24 17:29:17 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
const SkRect target = { 50, 50, 80, 90 };
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == fAtlas) {
|
2016-03-17 17:51:11 +00:00
|
|
|
fAtlas = MakeAtlas(canvas, target);
|
2015-06-24 17:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const struct {
|
|
|
|
SkScalar fScale;
|
|
|
|
SkScalar fDegrees;
|
|
|
|
SkScalar fTx;
|
|
|
|
SkScalar fTy;
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-06-24 17:29:17 +00:00
|
|
|
void apply(SkRSXform* xform) const {
|
|
|
|
const SkScalar rad = SkDegreesToRadians(fDegrees);
|
|
|
|
xform->fSCos = fScale * SkScalarCos(rad);
|
|
|
|
xform->fSSin = fScale * SkScalarSin(rad);
|
|
|
|
xform->fTx = fTx;
|
|
|
|
xform->fTy = fTy;
|
|
|
|
}
|
|
|
|
} rec[] = {
|
|
|
|
{ 1, 0, 10, 10 }, // just translate
|
|
|
|
{ 2, 0, 110, 10 }, // scale + translate
|
|
|
|
{ 1, 30, 210, 10 }, // rotate + translate
|
|
|
|
{ 2, -30, 310, 30 }, // scale + rotate + translate
|
|
|
|
};
|
|
|
|
|
|
|
|
const int N = SK_ARRAY_COUNT(rec);
|
|
|
|
SkRSXform xform[N];
|
|
|
|
SkRect tex[N];
|
|
|
|
SkColor colors[N];
|
|
|
|
|
|
|
|
for (int i = 0; i < N; ++i) {
|
|
|
|
rec[i].apply(&xform[i]);
|
|
|
|
tex[i] = target;
|
|
|
|
colors[i] = 0x80FF0000 + (i * 40 * 256);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setFilterQuality(kLow_SkFilterQuality);
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
canvas->drawAtlas(fAtlas.get(), xform, tex, N, nullptr, &paint);
|
2015-06-24 17:29:17 +00:00
|
|
|
canvas->translate(0, 100);
|
2016-03-17 17:51:11 +00:00
|
|
|
canvas->drawAtlas(fAtlas.get(), xform, tex, colors, N, SkXfermode::kSrcIn_Mode, nullptr, &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
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
DEF_GM( return new DrawAtlasGM; )
|