2015-12-11 19:03:27 +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 "SkImage.h"
|
|
|
|
#include "SkSurface.h"
|
|
|
|
#include "SkTArray.h"
|
|
|
|
|
|
|
|
class ImageScaleAlignedGM : public skiagm::GM {
|
|
|
|
protected:
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
const SkVector vectors[] = { { 1, 0 }, { 0, 1 } };
|
|
|
|
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) {
|
|
|
|
auto& set = fSets.push_back();
|
|
|
|
|
|
|
|
set.fVector = vectors[i];
|
2016-03-17 17:51:11 +00:00
|
|
|
set.fImages.push_back() = MakeImage(vectors[i], SK_ColorGREEN);
|
2015-12-11 19:03:27 +00:00
|
|
|
set.fScales.push_back() = 1;
|
2016-03-17 17:51:11 +00:00
|
|
|
set.fImages.push_back() = MakeImage(vectors[i], SK_ColorRED);
|
2015-12-11 19:03:27 +00:00
|
|
|
set.fScales.push_back() = kStretchFactor;
|
2016-03-17 17:51:11 +00:00
|
|
|
set.fImages.push_back() = MakeImage(vectors[i], SK_ColorGREEN);
|
2015-12-11 19:03:27 +00:00
|
|
|
set.fScales.push_back() = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("image_scale_aligned");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
2015-12-15 14:48:48 +00:00
|
|
|
return SkISize::Make(580, 780);
|
2015-12-11 19:03:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
struct {
|
|
|
|
SkPoint offset;
|
2015-12-15 14:48:48 +00:00
|
|
|
SkVector scale;
|
2015-12-11 19:03:27 +00:00
|
|
|
} cfgs[] = {
|
2015-12-15 14:48:48 +00:00
|
|
|
{{ 10, 10 }, { 1, 1 }},
|
|
|
|
{{ 300.5f, 10 }, { 1, 1 }},
|
|
|
|
{{ 10, 200.5f }, { 1, 1 }},
|
|
|
|
{{ 300.5f, 200.5f }, { 1, 1 }},
|
|
|
|
|
|
|
|
{{ 10.5f, 400.5f }, { 1, 1 }},
|
|
|
|
{{ 550.5f, 400.5f }, { -1, 1 }},
|
|
|
|
{{ 10.5f, 750.5f }, { 1, -1 }},
|
|
|
|
{{ 550.5f, 750.5f }, { -1, -1 }},
|
2015-12-11 19:03:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(cfgs); ++i) {
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
canvas->translate(cfgs[i].offset.x(), cfgs[i].offset.y());
|
2015-12-15 14:48:48 +00:00
|
|
|
canvas->scale(cfgs[i].scale.x(), cfgs[i].scale.y());
|
2015-12-11 19:03:27 +00:00
|
|
|
drawSets(canvas);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct ImageSet {
|
2016-03-17 17:51:11 +00:00
|
|
|
SkSTArray<3, sk_sp<SkImage>, true> fImages;
|
|
|
|
SkSTArray<3, SkScalar> fScales;
|
|
|
|
SkVector fVector;
|
2015-12-11 19:03:27 +00:00
|
|
|
};
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
static sk_sp<SkImage> MakeImage(const SkVector& vec, SkColor color) {
|
2015-12-11 19:03:27 +00:00
|
|
|
const SkPoint start = SkPoint::Make(vec.y() * kSegLen / 2, vec.x() * kSegLen / 2);
|
|
|
|
const SkPoint end = SkPoint::Make(start.x() + vec.x() * (kSegLen - 1),
|
|
|
|
start.y() + vec.y() * (kSegLen - 1));
|
|
|
|
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface(SkSurface::MakeRasterN32Premul(kSegLen, kSegLen));
|
2015-12-11 19:03:27 +00:00
|
|
|
surface->getCanvas()->clear(SK_ColorTRANSPARENT);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2015-12-15 14:48:48 +00:00
|
|
|
const SkRect border = SkRect::MakeIWH(kSegLen, kSegLen).makeInset(.5f, .5f);
|
|
|
|
paint.setColor(SK_ColorBLUE);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
surface->getCanvas()->drawRect(border, paint);
|
|
|
|
|
2015-12-11 19:03:27 +00:00
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
surface->getCanvas()->drawLine(start.x(), start.y(), end.x(), end.y(), paint);
|
2017-02-22 18:21:42 +00:00
|
|
|
|
|
|
|
paint.reset();
|
|
|
|
paint.setColor(color);
|
|
|
|
const SkPoint pts[] = { start, end };
|
|
|
|
surface->getCanvas()->drawPoints(SkCanvas::kPoints_PointMode, 2, pts, paint);
|
2015-12-11 19:03:27 +00:00
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
return surface->makeImageSnapshot();
|
2015-12-11 19:03:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void drawSets(SkCanvas* canvas) const {
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
|
|
|
|
const SkFilterQuality filters[] = {
|
|
|
|
kNone_SkFilterQuality,
|
|
|
|
kLow_SkFilterQuality,
|
|
|
|
kMedium_SkFilterQuality,
|
|
|
|
kHigh_SkFilterQuality
|
|
|
|
};
|
|
|
|
const bool AAs[] = { false, true };
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
for (int i = 0; i < fSets.count(); ++i) {
|
|
|
|
auto& set = fSets[i];
|
|
|
|
SkPoint lastPt;
|
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(AAs); ++j) {
|
|
|
|
paint.setAntiAlias(AAs[j]);
|
|
|
|
for (size_t k = 0; k < SK_ARRAY_COUNT(filters); ++k) {
|
|
|
|
paint.setFilterQuality(filters[k]);
|
|
|
|
lastPt = drawSet(canvas, set, paint);
|
2015-12-15 14:48:48 +00:00
|
|
|
canvas->translate((kSegLen + 4) * set.fVector.y(),
|
|
|
|
(kSegLen + 4) * set.fVector.x());
|
2015-12-11 19:03:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
canvas->translate(lastPt.x() + kSegLen,
|
2015-12-15 14:48:48 +00:00
|
|
|
- SkIntToScalar(kSegLen + 4) * SK_ARRAY_COUNT(filters) * SK_ARRAY_COUNT(AAs));
|
2015-12-11 19:03:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPoint drawSet(SkCanvas* canvas, const ImageSet& set, const SkPaint& paint) const {
|
|
|
|
SkASSERT(set.fImages.count() == set.fScales.count());
|
|
|
|
|
|
|
|
SkPoint pt = SkPoint::Make(0, 0);
|
|
|
|
for (int i = 0; i < set.fImages.count(); ++i) {
|
|
|
|
auto& img = set.fImages[i];
|
|
|
|
const SkRect dst =
|
|
|
|
SkRect::MakeXYWH(pt.x(), pt.y(),
|
|
|
|
img->width() * (1 + (set.fScales[i] - 1) * set.fVector.x()),
|
|
|
|
img->height() * (1 + (set.fScales[i] - 1) * set.fVector.y()));
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
canvas->drawImageRect(img.get(), dst, &paint);
|
2015-12-11 19:03:27 +00:00
|
|
|
pt.offset(dst.width() * set.fVector.x(), dst.height() * set.fVector.y());
|
|
|
|
}
|
|
|
|
|
|
|
|
return pt;
|
|
|
|
}
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
static constexpr unsigned kSegLen = 15;
|
|
|
|
static constexpr unsigned kStretchFactor = 4;
|
2015-12-11 19:03:27 +00:00
|
|
|
SkSTArray<2, ImageSet> fSets;
|
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
DEF_GM(return new ImageScaleAlignedGM();)
|