OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "gm.h" | 8 #include "gm.h" |
9 | 9 |
10 #include "SkImage.h" | 10 #include "SkImage.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 set.fImages.push_back().reset(MakeImage(vectors[i], SK_ColorGREEN)); | 27 set.fImages.push_back().reset(MakeImage(vectors[i], SK_ColorGREEN)); |
28 set.fScales.push_back() = 1; | 28 set.fScales.push_back() = 1; |
29 } | 29 } |
30 } | 30 } |
31 | 31 |
32 SkString onShortName() override { | 32 SkString onShortName() override { |
33 return SkString("image_scale_aligned"); | 33 return SkString("image_scale_aligned"); |
34 } | 34 } |
35 | 35 |
36 SkISize onISize() override { | 36 SkISize onISize() override { |
37 return SkISize::Make(550, 300); | 37 return SkISize::Make(580, 780); |
38 } | 38 } |
39 | 39 |
40 void onDraw(SkCanvas* canvas) override { | 40 void onDraw(SkCanvas* canvas) override { |
41 struct { | 41 struct { |
42 SkPoint offset; | 42 SkPoint offset; |
43 SkScalar scale; | 43 SkVector scale; |
44 } cfgs[] = { | 44 } cfgs[] = { |
45 { { 10, 10 }, 1 }, | 45 {{ 10, 10 }, { 1, 1 }}, |
46 { { 140.5f, 10 }, 1 }, | 46 {{ 300.5f, 10 }, { 1, 1 }}, |
47 { { 10, 80.5f }, 1 }, | 47 {{ 10, 200.5f }, { 1, 1 }}, |
48 { { 140.5f, 80.5f }, 1 }, | 48 {{ 300.5f, 200.5f }, { 1, 1 }}, |
49 | 49 |
50 { { 280.49f, 10.49f }, 1 }, | 50 {{ 10.5f, 400.5f }, { 1, 1 }}, |
51 { { 410.51f, 10.49f }, 1 }, | 51 {{ 550.5f, 400.5f }, { -1, 1 }}, |
52 { { 280.49f, 80.51f }, 1 }, | 52 {{ 10.5f, 750.5f }, { 1, -1 }}, |
53 { { 410.51f, 80.51f }, 1 }, | 53 {{ 550.5f, 750.5f }, { -1, -1 }}, |
54 | |
55 { { 10, 170 }, 1.1f }, | |
56 { { 140.5f, 170 }, 1.1f }, | |
57 { { 10, 240.5f }, 1.1f }, | |
58 { { 140.5f, 240.5f }, 1.1f }, | |
59 | |
60 { { 280.49f, 170.49f }, 1.1f }, | |
61 { { 410.51f, 170.49f }, 1.1f }, | |
62 { { 280.49f, 240.51f }, 1.1f }, | |
63 { { 410.51f, 240.51f }, 1.1f }, | |
64 }; | 54 }; |
65 | 55 |
66 for (size_t i = 0; i < SK_ARRAY_COUNT(cfgs); ++i) { | 56 for (size_t i = 0; i < SK_ARRAY_COUNT(cfgs); ++i) { |
67 SkAutoCanvasRestore acr(canvas, true); | 57 SkAutoCanvasRestore acr(canvas, true); |
68 canvas->translate(cfgs[i].offset.x(), cfgs[i].offset.y()); | 58 canvas->translate(cfgs[i].offset.x(), cfgs[i].offset.y()); |
69 canvas->scale(cfgs[i].scale, cfgs[i].scale); | 59 canvas->scale(cfgs[i].scale.x(), cfgs[i].scale.y()); |
70 drawSets(canvas); | 60 drawSets(canvas); |
71 } | 61 } |
72 } | 62 } |
73 | 63 |
74 private: | 64 private: |
75 struct ImageSet { | 65 struct ImageSet { |
76 SkSTArray<3, SkAutoTUnref<SkImage>, true> fImages; | 66 SkSTArray<3, SkAutoTUnref<SkImage>, true> fImages; |
77 SkSTArray<3, SkScalar> fScales; | 67 SkSTArray<3, SkScalar> fScales; |
78 SkVector fVector; | 68 SkVector fVector; |
79 }; | 69 }; |
80 | 70 |
81 static SkImage* MakeImage(const SkVector& vec, SkColor color) { | 71 static SkImage* MakeImage(const SkVector& vec, SkColor color) { |
82 const SkPoint start = SkPoint::Make(vec.y() * kSegLen / 2, vec.x() * kSe
gLen / 2); | 72 const SkPoint start = SkPoint::Make(vec.y() * kSegLen / 2, vec.x() * kSe
gLen / 2); |
83 const SkPoint end = SkPoint::Make(start.x() + vec.x() * (kSegLen - 1), | 73 const SkPoint end = SkPoint::Make(start.x() + vec.x() * (kSegLen - 1), |
84 start.y() + vec.y() * (kSegLen - 1))
; | 74 start.y() + vec.y() * (kSegLen - 1))
; |
85 | 75 |
86 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(kSegLen, k
SegLen)); | 76 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(kSegLen, k
SegLen)); |
87 surface->getCanvas()->clear(SK_ColorTRANSPARENT); | 77 surface->getCanvas()->clear(SK_ColorTRANSPARENT); |
88 | 78 |
89 SkPaint paint; | 79 SkPaint paint; |
90 paint.setAntiAlias(true); | 80 paint.setAntiAlias(true); |
| 81 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 |
91 paint.setColor(SK_ColorBLACK); | 86 paint.setColor(SK_ColorBLACK); |
92 surface->getCanvas()->drawLine(start.x(), start.y(), end.x(), end.y(), p
aint); | 87 surface->getCanvas()->drawLine(start.x(), start.y(), end.x(), end.y(), p
aint); |
93 surface->getCanvas()->drawPoint(start.x(), start.y(), color); | 88 surface->getCanvas()->drawPoint(start.x(), start.y(), color); |
94 surface->getCanvas()->drawPoint(end.x(), end.y(), color); | 89 surface->getCanvas()->drawPoint(end.x(), end.y(), color); |
95 | 90 |
96 return surface->newImageSnapshot(); | 91 return surface->newImageSnapshot(); |
97 } | 92 } |
98 | 93 |
99 void drawSets(SkCanvas* canvas) const { | 94 void drawSets(SkCanvas* canvas) const { |
100 SkAutoCanvasRestore acr(canvas, true); | 95 SkAutoCanvasRestore acr(canvas, true); |
101 | 96 |
102 const SkFilterQuality filters[] = { | 97 const SkFilterQuality filters[] = { |
103 kNone_SkFilterQuality, | 98 kNone_SkFilterQuality, |
104 kLow_SkFilterQuality, | 99 kLow_SkFilterQuality, |
105 kMedium_SkFilterQuality, | 100 kMedium_SkFilterQuality, |
106 kHigh_SkFilterQuality | 101 kHigh_SkFilterQuality |
107 }; | 102 }; |
108 const bool AAs[] = { false, true }; | 103 const bool AAs[] = { false, true }; |
109 | 104 |
110 SkPaint paint; | 105 SkPaint paint; |
111 for (int i = 0; i < fSets.count(); ++i) { | 106 for (int i = 0; i < fSets.count(); ++i) { |
112 auto& set = fSets[i]; | 107 auto& set = fSets[i]; |
113 SkPoint lastPt; | 108 SkPoint lastPt; |
114 for (size_t j = 0; j < SK_ARRAY_COUNT(AAs); ++j) { | 109 for (size_t j = 0; j < SK_ARRAY_COUNT(AAs); ++j) { |
115 paint.setAntiAlias(AAs[j]); | 110 paint.setAntiAlias(AAs[j]); |
116 for (size_t k = 0; k < SK_ARRAY_COUNT(filters); ++k) { | 111 for (size_t k = 0; k < SK_ARRAY_COUNT(filters); ++k) { |
117 paint.setFilterQuality(filters[k]); | 112 paint.setFilterQuality(filters[k]); |
118 lastPt = drawSet(canvas, set, paint); | 113 lastPt = drawSet(canvas, set, paint); |
119 canvas->translate((kSegLen / 2) * set.fVector.y(), | 114 canvas->translate((kSegLen + 4) * set.fVector.y(), |
120 (kSegLen / 2) * set.fVector.x()); | 115 (kSegLen + 4) * set.fVector.x()); |
121 } | 116 } |
122 } | 117 } |
123 canvas->translate(lastPt.x() + kSegLen, | 118 canvas->translate(lastPt.x() + kSegLen, |
124 - SkIntToScalar(kSegLen) / 2 * SK_ARRAY_COUNT(filters) * SK_ARRA
Y_COUNT(AAs)); | 119 - SkIntToScalar(kSegLen + 4) * SK_ARRAY_COUNT(filters) * SK_ARRA
Y_COUNT(AAs)); |
125 } | 120 } |
126 } | 121 } |
127 | 122 |
128 SkPoint drawSet(SkCanvas* canvas, const ImageSet& set, const SkPaint& paint)
const { | 123 SkPoint drawSet(SkCanvas* canvas, const ImageSet& set, const SkPaint& paint)
const { |
129 SkASSERT(set.fImages.count() == set.fScales.count()); | 124 SkASSERT(set.fImages.count() == set.fScales.count()); |
130 | 125 |
131 SkPoint pt = SkPoint::Make(0, 0); | 126 SkPoint pt = SkPoint::Make(0, 0); |
132 for (int i = 0; i < set.fImages.count(); ++i) { | 127 for (int i = 0; i < set.fImages.count(); ++i) { |
133 auto& img = set.fImages[i]; | 128 auto& img = set.fImages[i]; |
134 const SkRect dst = | 129 const SkRect dst = |
135 SkRect::MakeXYWH(pt.x(), pt.y(), | 130 SkRect::MakeXYWH(pt.x(), pt.y(), |
136 img->width() * (1 + (set.fScales[i] - 1) * set.fVector.x()), | 131 img->width() * (1 + (set.fScales[i] - 1) * set.fVector.x()), |
137 img->height() * (1 + (set.fScales[i] - 1) * set.fVector.y())
); | 132 img->height() * (1 + (set.fScales[i] - 1) * set.fVector.y())
); |
138 | 133 |
139 canvas->drawImageRect(img, dst, &paint); | 134 canvas->drawImageRect(img, dst, &paint); |
140 pt.offset(dst.width() * set.fVector.x(), dst.height() * set.fVector.
y()); | 135 pt.offset(dst.width() * set.fVector.x(), dst.height() * set.fVector.
y()); |
141 } | 136 } |
142 | 137 |
143 return pt; | 138 return pt; |
144 } | 139 } |
145 | 140 |
146 static const unsigned kSegLen = 9; | 141 static const unsigned kSegLen = 15; |
147 static const unsigned kStretchFactor = 2; | 142 static const unsigned kStretchFactor = 4; |
148 SkSTArray<2, ImageSet> fSets; | 143 SkSTArray<2, ImageSet> fSets; |
149 | 144 |
150 typedef GM INHERITED; | 145 typedef GM INHERITED; |
151 }; | 146 }; |
152 | 147 |
153 DEF_GM(return new ImageScaleAlignedGM();) | 148 DEF_GM(return new ImageScaleAlignedGM();) |
OLD | NEW |