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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkImage.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/private/SkTArray.h"
|
2015-12-11 19:03:27 +00:00
|
|
|
|
|
|
|
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);
|
2017-05-15 17:35:35 +00:00
|
|
|
surface->getCanvas()->drawLine(start, end, 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);
|
|
|
|
|
2021-01-23 17:23:23 +00:00
|
|
|
const SkSamplingOptions samplings[] = {
|
|
|
|
SkSamplingOptions(SkFilterMode::kNearest),
|
|
|
|
SkSamplingOptions(SkFilterMode::kLinear),
|
|
|
|
SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear),
|
2021-02-05 17:55:38 +00:00
|
|
|
SkSamplingOptions(SkCubicResampler::Mitchell()),
|
2015-12-11 19:03:27 +00:00
|
|
|
};
|
|
|
|
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]);
|
2021-01-23 17:23:23 +00:00
|
|
|
for (size_t k = 0; k < SK_ARRAY_COUNT(samplings); ++k) {
|
|
|
|
lastPt = drawSet(canvas, set, samplings[k], 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,
|
2021-01-23 17:23:23 +00:00
|
|
|
- SkIntToScalar(kSegLen + 4) * SK_ARRAY_COUNT(samplings) * SK_ARRAY_COUNT(AAs));
|
2015-12-11 19:03:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-23 17:23:23 +00:00
|
|
|
SkPoint drawSet(SkCanvas* canvas, const ImageSet& set, const SkSamplingOptions& sampling,
|
|
|
|
const SkPaint& paint) const {
|
2015-12-11 19:03:27 +00:00
|
|
|
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()));
|
|
|
|
|
2021-01-23 17:23:23 +00:00
|
|
|
canvas->drawImageRect(img.get(), dst, sampling, &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;
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2015-12-11 19:03:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DEF_GM(return new ImageScaleAlignedGM();)
|