blob: a1f67410f1eba1f7e9e938c556dfa05e0db5e187 [file] [log] [blame]
fmalitacad54b52015-12-11 11:03:27 -08001/*
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 "gm.h"
9
10#include "SkImage.h"
11#include "SkSurface.h"
12#include "SkTArray.h"
13
14class ImageScaleAlignedGM : public skiagm::GM {
15protected:
16 void onOnceBeforeDraw() override {
17 const SkVector vectors[] = { { 1, 0 }, { 0, 1 } };
18
19 for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) {
20 auto& set = fSets.push_back();
21
22 set.fVector = vectors[i];
reed9ce9d672016-03-17 10:51:11 -070023 set.fImages.push_back() = MakeImage(vectors[i], SK_ColorGREEN);
fmalitacad54b52015-12-11 11:03:27 -080024 set.fScales.push_back() = 1;
reed9ce9d672016-03-17 10:51:11 -070025 set.fImages.push_back() = MakeImage(vectors[i], SK_ColorRED);
fmalitacad54b52015-12-11 11:03:27 -080026 set.fScales.push_back() = kStretchFactor;
reed9ce9d672016-03-17 10:51:11 -070027 set.fImages.push_back() = MakeImage(vectors[i], SK_ColorGREEN);
fmalitacad54b52015-12-11 11:03:27 -080028 set.fScales.push_back() = 1;
29 }
30 }
31
32 SkString onShortName() override {
33 return SkString("image_scale_aligned");
34 }
35
36 SkISize onISize() override {
fmalitaaed4d322015-12-15 06:48:48 -080037 return SkISize::Make(580, 780);
fmalitacad54b52015-12-11 11:03:27 -080038 }
39
40 void onDraw(SkCanvas* canvas) override {
41 struct {
42 SkPoint offset;
fmalitaaed4d322015-12-15 06:48:48 -080043 SkVector scale;
fmalitacad54b52015-12-11 11:03:27 -080044 } cfgs[] = {
fmalitaaed4d322015-12-15 06:48:48 -080045 {{ 10, 10 }, { 1, 1 }},
46 {{ 300.5f, 10 }, { 1, 1 }},
47 {{ 10, 200.5f }, { 1, 1 }},
48 {{ 300.5f, 200.5f }, { 1, 1 }},
fmalitacad54b52015-12-11 11:03:27 -080049
fmalitaaed4d322015-12-15 06:48:48 -080050 {{ 10.5f, 400.5f }, { 1, 1 }},
51 {{ 550.5f, 400.5f }, { -1, 1 }},
52 {{ 10.5f, 750.5f }, { 1, -1 }},
53 {{ 550.5f, 750.5f }, { -1, -1 }},
fmalitacad54b52015-12-11 11:03:27 -080054 };
55
56 for (size_t i = 0; i < SK_ARRAY_COUNT(cfgs); ++i) {
57 SkAutoCanvasRestore acr(canvas, true);
58 canvas->translate(cfgs[i].offset.x(), cfgs[i].offset.y());
fmalitaaed4d322015-12-15 06:48:48 -080059 canvas->scale(cfgs[i].scale.x(), cfgs[i].scale.y());
fmalitacad54b52015-12-11 11:03:27 -080060 drawSets(canvas);
61 }
62 }
63
64private:
65 struct ImageSet {
reed9ce9d672016-03-17 10:51:11 -070066 SkSTArray<3, sk_sp<SkImage>, true> fImages;
67 SkSTArray<3, SkScalar> fScales;
68 SkVector fVector;
fmalitacad54b52015-12-11 11:03:27 -080069 };
70
reed9ce9d672016-03-17 10:51:11 -070071 static sk_sp<SkImage> MakeImage(const SkVector& vec, SkColor color) {
fmalitacad54b52015-12-11 11:03:27 -080072 const SkPoint start = SkPoint::Make(vec.y() * kSegLen / 2, vec.x() * kSegLen / 2);
73 const SkPoint end = SkPoint::Make(start.x() + vec.x() * (kSegLen - 1),
74 start.y() + vec.y() * (kSegLen - 1));
75
reede8f30622016-03-23 18:59:25 -070076 auto surface(SkSurface::MakeRasterN32Premul(kSegLen, kSegLen));
fmalitacad54b52015-12-11 11:03:27 -080077 surface->getCanvas()->clear(SK_ColorTRANSPARENT);
78
79 SkPaint paint;
80 paint.setAntiAlias(true);
fmalitaaed4d322015-12-15 06:48:48 -080081 const SkRect border = SkRect::MakeIWH(kSegLen, kSegLen).makeInset(.5f, .5f);
82 paint.setColor(SK_ColorBLUE);
83 paint.setStyle(SkPaint::kStroke_Style);
84 surface->getCanvas()->drawRect(border, paint);
85
fmalitacad54b52015-12-11 11:03:27 -080086 paint.setColor(SK_ColorBLACK);
Hal Canary23e474c2017-05-15 13:35:35 -040087 surface->getCanvas()->drawLine(start, end, paint);
Mike Reed3661bc92017-02-22 13:21:42 -050088
89 paint.reset();
90 paint.setColor(color);
91 const SkPoint pts[] = { start, end };
92 surface->getCanvas()->drawPoints(SkCanvas::kPoints_PointMode, 2, pts, paint);
fmalitacad54b52015-12-11 11:03:27 -080093
reed9ce9d672016-03-17 10:51:11 -070094 return surface->makeImageSnapshot();
fmalitacad54b52015-12-11 11:03:27 -080095 }
96
97 void drawSets(SkCanvas* canvas) const {
98 SkAutoCanvasRestore acr(canvas, true);
99
100 const SkFilterQuality filters[] = {
101 kNone_SkFilterQuality,
102 kLow_SkFilterQuality,
103 kMedium_SkFilterQuality,
104 kHigh_SkFilterQuality
105 };
106 const bool AAs[] = { false, true };
107
108 SkPaint paint;
109 for (int i = 0; i < fSets.count(); ++i) {
110 auto& set = fSets[i];
111 SkPoint lastPt;
112 for (size_t j = 0; j < SK_ARRAY_COUNT(AAs); ++j) {
113 paint.setAntiAlias(AAs[j]);
114 for (size_t k = 0; k < SK_ARRAY_COUNT(filters); ++k) {
115 paint.setFilterQuality(filters[k]);
116 lastPt = drawSet(canvas, set, paint);
fmalitaaed4d322015-12-15 06:48:48 -0800117 canvas->translate((kSegLen + 4) * set.fVector.y(),
118 (kSegLen + 4) * set.fVector.x());
fmalitacad54b52015-12-11 11:03:27 -0800119 }
120 }
121 canvas->translate(lastPt.x() + kSegLen,
fmalitaaed4d322015-12-15 06:48:48 -0800122 - SkIntToScalar(kSegLen + 4) * SK_ARRAY_COUNT(filters) * SK_ARRAY_COUNT(AAs));
fmalitacad54b52015-12-11 11:03:27 -0800123 }
124 }
125
126 SkPoint drawSet(SkCanvas* canvas, const ImageSet& set, const SkPaint& paint) const {
127 SkASSERT(set.fImages.count() == set.fScales.count());
128
129 SkPoint pt = SkPoint::Make(0, 0);
130 for (int i = 0; i < set.fImages.count(); ++i) {
131 auto& img = set.fImages[i];
132 const SkRect dst =
133 SkRect::MakeXYWH(pt.x(), pt.y(),
134 img->width() * (1 + (set.fScales[i] - 1) * set.fVector.x()),
135 img->height() * (1 + (set.fScales[i] - 1) * set.fVector.y()));
136
reed9ce9d672016-03-17 10:51:11 -0700137 canvas->drawImageRect(img.get(), dst, &paint);
fmalitacad54b52015-12-11 11:03:27 -0800138 pt.offset(dst.width() * set.fVector.x(), dst.height() * set.fVector.y());
139 }
140
141 return pt;
142 }
143
mtkleindbfd7ab2016-09-01 11:24:54 -0700144 static constexpr unsigned kSegLen = 15;
145 static constexpr unsigned kStretchFactor = 4;
fmalitacad54b52015-12-11 11:03:27 -0800146 SkSTArray<2, ImageSet> fSets;
147
148 typedef GM INHERITED;
149};
150
151DEF_GM(return new ImageScaleAlignedGM();)