fmalita | cad54b5 | 2015-12-11 11:03:27 -0800 | [diff] [blame] | 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 { |
fmalita | aed4d32 | 2015-12-15 06:48:48 -0800 | [diff] [blame] | 37 | return SkISize::Make(580, 780); |
fmalita | cad54b5 | 2015-12-11 11:03:27 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void onDraw(SkCanvas* canvas) override { |
| 41 | struct { |
| 42 | SkPoint offset; |
fmalita | aed4d32 | 2015-12-15 06:48:48 -0800 | [diff] [blame] | 43 | SkVector scale; |
fmalita | cad54b5 | 2015-12-11 11:03:27 -0800 | [diff] [blame] | 44 | } cfgs[] = { |
fmalita | aed4d32 | 2015-12-15 06:48:48 -0800 | [diff] [blame] | 45 | {{ 10, 10 }, { 1, 1 }}, |
| 46 | {{ 300.5f, 10 }, { 1, 1 }}, |
| 47 | {{ 10, 200.5f }, { 1, 1 }}, |
| 48 | {{ 300.5f, 200.5f }, { 1, 1 }}, |
fmalita | cad54b5 | 2015-12-11 11:03:27 -0800 | [diff] [blame] | 49 | |
fmalita | aed4d32 | 2015-12-15 06:48:48 -0800 | [diff] [blame] | 50 | {{ 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 }}, |
fmalita | cad54b5 | 2015-12-11 11:03:27 -0800 | [diff] [blame] | 54 | }; |
| 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()); |
fmalita | aed4d32 | 2015-12-15 06:48:48 -0800 | [diff] [blame] | 59 | canvas->scale(cfgs[i].scale.x(), cfgs[i].scale.y()); |
fmalita | cad54b5 | 2015-12-11 11:03:27 -0800 | [diff] [blame] | 60 | drawSets(canvas); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | struct ImageSet { |
| 66 | SkSTArray<3, SkAutoTUnref<SkImage>, true> fImages; |
| 67 | SkSTArray<3, SkScalar> fScales; |
| 68 | SkVector fVector; |
| 69 | }; |
| 70 | |
| 71 | static SkImage* MakeImage(const SkVector& vec, SkColor color) { |
| 72 | 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 | |
| 76 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(kSegLen, kSegLen)); |
| 77 | surface->getCanvas()->clear(SK_ColorTRANSPARENT); |
| 78 | |
| 79 | SkPaint paint; |
| 80 | paint.setAntiAlias(true); |
fmalita | aed4d32 | 2015-12-15 06:48:48 -0800 | [diff] [blame] | 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 | |
fmalita | cad54b5 | 2015-12-11 11:03:27 -0800 | [diff] [blame] | 86 | 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 | |
| 91 | return surface->newImageSnapshot(); |
| 92 | } |
| 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); |
fmalita | aed4d32 | 2015-12-15 06:48:48 -0800 | [diff] [blame] | 114 | canvas->translate((kSegLen + 4) * set.fVector.y(), |
| 115 | (kSegLen + 4) * set.fVector.x()); |
fmalita | cad54b5 | 2015-12-11 11:03:27 -0800 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | canvas->translate(lastPt.x() + kSegLen, |
fmalita | aed4d32 | 2015-12-15 06:48:48 -0800 | [diff] [blame] | 119 | - SkIntToScalar(kSegLen + 4) * SK_ARRAY_COUNT(filters) * SK_ARRAY_COUNT(AAs)); |
fmalita | cad54b5 | 2015-12-11 11:03:27 -0800 | [diff] [blame] | 120 | } |
| 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 | |
| 134 | canvas->drawImageRect(img, dst, &paint); |
| 135 | pt.offset(dst.width() * set.fVector.x(), dst.height() * set.fVector.y()); |
| 136 | } |
| 137 | |
| 138 | return pt; |
| 139 | } |
| 140 | |
fmalita | aed4d32 | 2015-12-15 06:48:48 -0800 | [diff] [blame] | 141 | static const unsigned kSegLen = 15; |
| 142 | static const unsigned kStretchFactor = 4; |
fmalita | cad54b5 | 2015-12-11 11:03:27 -0800 | [diff] [blame] | 143 | SkSTArray<2, ImageSet> fSets; |
| 144 | |
| 145 | typedef GM INHERITED; |
| 146 | }; |
| 147 | |
| 148 | DEF_GM(return new ImageScaleAlignedGM();) |