blob: 9498d320716795ee93d8907e1191e7eedd69bc52 [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);
87 surface->getCanvas()->drawLine(start.x(), start.y(), end.x(), end.y(), paint);
88 surface->getCanvas()->drawPoint(start.x(), start.y(), color);
89 surface->getCanvas()->drawPoint(end.x(), end.y(), color);
90
reed9ce9d672016-03-17 10:51:11 -070091 return surface->makeImageSnapshot();
fmalitacad54b52015-12-11 11:03:27 -080092 }
93
94 void drawSets(SkCanvas* canvas) const {
95 SkAutoCanvasRestore acr(canvas, true);
96
97 const SkFilterQuality filters[] = {
98 kNone_SkFilterQuality,
99 kLow_SkFilterQuality,
100 kMedium_SkFilterQuality,
101 kHigh_SkFilterQuality
102 };
103 const bool AAs[] = { false, true };
104
105 SkPaint paint;
106 for (int i = 0; i < fSets.count(); ++i) {
107 auto& set = fSets[i];
108 SkPoint lastPt;
109 for (size_t j = 0; j < SK_ARRAY_COUNT(AAs); ++j) {
110 paint.setAntiAlias(AAs[j]);
111 for (size_t k = 0; k < SK_ARRAY_COUNT(filters); ++k) {
112 paint.setFilterQuality(filters[k]);
113 lastPt = drawSet(canvas, set, paint);
fmalitaaed4d322015-12-15 06:48:48 -0800114 canvas->translate((kSegLen + 4) * set.fVector.y(),
115 (kSegLen + 4) * set.fVector.x());
fmalitacad54b52015-12-11 11:03:27 -0800116 }
117 }
118 canvas->translate(lastPt.x() + kSegLen,
fmalitaaed4d322015-12-15 06:48:48 -0800119 - SkIntToScalar(kSegLen + 4) * SK_ARRAY_COUNT(filters) * SK_ARRAY_COUNT(AAs));
fmalitacad54b52015-12-11 11:03:27 -0800120 }
121 }
122
123 SkPoint drawSet(SkCanvas* canvas, const ImageSet& set, const SkPaint& paint) const {
124 SkASSERT(set.fImages.count() == set.fScales.count());
125
126 SkPoint pt = SkPoint::Make(0, 0);
127 for (int i = 0; i < set.fImages.count(); ++i) {
128 auto& img = set.fImages[i];
129 const SkRect dst =
130 SkRect::MakeXYWH(pt.x(), pt.y(),
131 img->width() * (1 + (set.fScales[i] - 1) * set.fVector.x()),
132 img->height() * (1 + (set.fScales[i] - 1) * set.fVector.y()));
133
reed9ce9d672016-03-17 10:51:11 -0700134 canvas->drawImageRect(img.get(), dst, &paint);
fmalitacad54b52015-12-11 11:03:27 -0800135 pt.offset(dst.width() * set.fVector.x(), dst.height() * set.fVector.y());
136 }
137
138 return pt;
139 }
140
mtkleindbfd7ab2016-09-01 11:24:54 -0700141 static constexpr unsigned kSegLen = 15;
142 static constexpr unsigned kStretchFactor = 4;
fmalitacad54b52015-12-11 11:03:27 -0800143 SkSTArray<2, ImageSet> fSets;
144
145 typedef GM INHERITED;
146};
147
148DEF_GM(return new ImageScaleAlignedGM();)