OLD | NEW |
(Empty) | |
| 1 /* |
| 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 |
| 14 class ImageScaleAlignedGM : public skiagm::GM { |
| 15 protected: |
| 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]; |
| 23 set.fImages.push_back().reset(MakeImage(vectors[i], SK_ColorGREEN)); |
| 24 set.fScales.push_back() = 1; |
| 25 set.fImages.push_back().reset(MakeImage(vectors[i], SK_ColorRED)); |
| 26 set.fScales.push_back() = kStretchFactor; |
| 27 set.fImages.push_back().reset(MakeImage(vectors[i], SK_ColorGREEN)); |
| 28 set.fScales.push_back() = 1; |
| 29 } |
| 30 } |
| 31 |
| 32 SkString onShortName() override { |
| 33 return SkString("image_scale_aligned"); |
| 34 } |
| 35 |
| 36 SkISize onISize() override { |
| 37 return SkISize::Make(550, 300); |
| 38 } |
| 39 |
| 40 void onDraw(SkCanvas* canvas) override { |
| 41 struct { |
| 42 SkPoint offset; |
| 43 SkScalar scale; |
| 44 } cfgs[] = { |
| 45 { { 10, 10 }, 1 }, |
| 46 { { 140.5f, 10 }, 1 }, |
| 47 { { 10, 80.5f }, 1 }, |
| 48 { { 140.5f, 80.5f }, 1 }, |
| 49 |
| 50 { { 280.49f, 10.49f }, 1 }, |
| 51 { { 410.51f, 10.49f }, 1 }, |
| 52 { { 280.49f, 80.51f }, 1 }, |
| 53 { { 410.51f, 80.51f }, 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 }; |
| 65 |
| 66 for (size_t i = 0; i < SK_ARRAY_COUNT(cfgs); ++i) { |
| 67 SkAutoCanvasRestore acr(canvas, true); |
| 68 canvas->translate(cfgs[i].offset.x(), cfgs[i].offset.y()); |
| 69 canvas->scale(cfgs[i].scale, cfgs[i].scale); |
| 70 drawSets(canvas); |
| 71 } |
| 72 } |
| 73 |
| 74 private: |
| 75 struct ImageSet { |
| 76 SkSTArray<3, SkAutoTUnref<SkImage>, true> fImages; |
| 77 SkSTArray<3, SkScalar> fScales; |
| 78 SkVector fVector; |
| 79 }; |
| 80 |
| 81 static SkImage* MakeImage(const SkVector& vec, SkColor color) { |
| 82 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), |
| 84 start.y() + vec.y() * (kSegLen - 1))
; |
| 85 |
| 86 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(kSegLen, k
SegLen)); |
| 87 surface->getCanvas()->clear(SK_ColorTRANSPARENT); |
| 88 |
| 89 SkPaint paint; |
| 90 paint.setAntiAlias(true); |
| 91 paint.setColor(SK_ColorBLACK); |
| 92 surface->getCanvas()->drawLine(start.x(), start.y(), end.x(), end.y(), p
aint); |
| 93 surface->getCanvas()->drawPoint(start.x(), start.y(), color); |
| 94 surface->getCanvas()->drawPoint(end.x(), end.y(), color); |
| 95 |
| 96 return surface->newImageSnapshot(); |
| 97 } |
| 98 |
| 99 void drawSets(SkCanvas* canvas) const { |
| 100 SkAutoCanvasRestore acr(canvas, true); |
| 101 |
| 102 const SkFilterQuality filters[] = { |
| 103 kNone_SkFilterQuality, |
| 104 kLow_SkFilterQuality, |
| 105 kMedium_SkFilterQuality, |
| 106 kHigh_SkFilterQuality |
| 107 }; |
| 108 const bool AAs[] = { false, true }; |
| 109 |
| 110 SkPaint paint; |
| 111 for (int i = 0; i < fSets.count(); ++i) { |
| 112 auto& set = fSets[i]; |
| 113 SkPoint lastPt; |
| 114 for (size_t j = 0; j < SK_ARRAY_COUNT(AAs); ++j) { |
| 115 paint.setAntiAlias(AAs[j]); |
| 116 for (size_t k = 0; k < SK_ARRAY_COUNT(filters); ++k) { |
| 117 paint.setFilterQuality(filters[k]); |
| 118 lastPt = drawSet(canvas, set, paint); |
| 119 canvas->translate((kSegLen / 2) * set.fVector.y(), |
| 120 (kSegLen / 2) * set.fVector.x()); |
| 121 } |
| 122 } |
| 123 canvas->translate(lastPt.x() + kSegLen, |
| 124 - SkIntToScalar(kSegLen) / 2 * SK_ARRAY_COUNT(filters) * SK_ARRA
Y_COUNT(AAs)); |
| 125 } |
| 126 } |
| 127 |
| 128 SkPoint drawSet(SkCanvas* canvas, const ImageSet& set, const SkPaint& paint)
const { |
| 129 SkASSERT(set.fImages.count() == set.fScales.count()); |
| 130 |
| 131 SkPoint pt = SkPoint::Make(0, 0); |
| 132 for (int i = 0; i < set.fImages.count(); ++i) { |
| 133 auto& img = set.fImages[i]; |
| 134 const SkRect dst = |
| 135 SkRect::MakeXYWH(pt.x(), pt.y(), |
| 136 img->width() * (1 + (set.fScales[i] - 1) * set.fVector.x()), |
| 137 img->height() * (1 + (set.fScales[i] - 1) * set.fVector.y())
); |
| 138 |
| 139 canvas->drawImageRect(img, dst, &paint); |
| 140 pt.offset(dst.width() * set.fVector.x(), dst.height() * set.fVector.
y()); |
| 141 } |
| 142 |
| 143 return pt; |
| 144 } |
| 145 |
| 146 static const unsigned kSegLen = 9; |
| 147 static const unsigned kStretchFactor = 2; |
| 148 SkSTArray<2, ImageSet> fSets; |
| 149 |
| 150 typedef GM INHERITED; |
| 151 }; |
| 152 |
| 153 DEF_GM(return new ImageScaleAlignedGM();) |
OLD | NEW |