reed@google.com | 4bc0a9d | 2012-03-07 21:47:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | #include "SkCanvas.h" |
| 10 | #include "SkColorPriv.h" |
| 11 | #include "SkShader.h" |
| 12 | |
| 13 | /* |
| 14 | * Want to ensure that our bitmap sampler (in bitmap shader) keeps plenty of |
| 15 | * precision when scaling very large images (where the dx might get very small. |
| 16 | */ |
| 17 | |
reed@google.com | 6005aeb | 2012-03-14 20:15:47 +0000 | [diff] [blame] | 18 | #define W 257 |
| 19 | #define H 161 |
reed@google.com | 4bc0a9d | 2012-03-07 21:47:41 +0000 | [diff] [blame] | 20 | |
| 21 | class GiantBitmapGM : public skiagm::GM { |
| 22 | SkBitmap* fBM; |
| 23 | SkShader::TileMode fMode; |
reed@google.com | b0b462b | 2012-03-08 22:13:39 +0000 | [diff] [blame] | 24 | bool fDoFilter; |
| 25 | bool fDoRotate; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 26 | |
reed@google.com | 4bc0a9d | 2012-03-07 21:47:41 +0000 | [diff] [blame] | 27 | const SkBitmap& getBitmap() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 28 | if (nullptr == fBM) { |
reed@google.com | 4bc0a9d | 2012-03-07 21:47:41 +0000 | [diff] [blame] | 29 | fBM = new SkBitmap; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 30 | fBM->allocN32Pixels(W, H); |
reed@google.com | 4bc0a9d | 2012-03-07 21:47:41 +0000 | [diff] [blame] | 31 | fBM->eraseColor(SK_ColorWHITE); |
| 32 | |
| 33 | const SkColor colors[] = { |
| 34 | SK_ColorBLUE, SK_ColorRED, SK_ColorBLACK, SK_ColorGREEN |
| 35 | }; |
| 36 | |
| 37 | SkCanvas canvas(*fBM); |
| 38 | SkPaint paint; |
| 39 | paint.setAntiAlias(true); |
reed@google.com | 6005aeb | 2012-03-14 20:15:47 +0000 | [diff] [blame] | 40 | paint.setStrokeWidth(SkIntToScalar(20)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 41 | |
reed@google.com | 6005aeb | 2012-03-14 20:15:47 +0000 | [diff] [blame] | 42 | #if 0 |
| 43 | for (int y = -H*2; y < H; y += 50) { |
reed@google.com | 4bc0a9d | 2012-03-07 21:47:41 +0000 | [diff] [blame] | 44 | SkScalar yy = SkIntToScalar(y); |
reed@google.com | 6005aeb | 2012-03-14 20:15:47 +0000 | [diff] [blame] | 45 | paint.setColor(colors[y/50 & 0x3]); |
reed@google.com | 4bc0a9d | 2012-03-07 21:47:41 +0000 | [diff] [blame] | 46 | canvas.drawLine(0, yy, SkIntToScalar(W), yy + SkIntToScalar(W), |
| 47 | paint); |
| 48 | } |
reed@google.com | 6005aeb | 2012-03-14 20:15:47 +0000 | [diff] [blame] | 49 | #else |
| 50 | for (int x = -W; x < W; x += 60) { |
| 51 | paint.setColor(colors[x/60 & 0x3]); |
| 52 | |
| 53 | SkScalar xx = SkIntToScalar(x); |
| 54 | canvas.drawLine(xx, 0, xx, SkIntToScalar(H), |
| 55 | paint); |
| 56 | } |
| 57 | #endif |
reed@google.com | 4bc0a9d | 2012-03-07 21:47:41 +0000 | [diff] [blame] | 58 | } |
| 59 | return *fBM; |
| 60 | } |
| 61 | |
| 62 | public: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 63 | GiantBitmapGM(SkShader::TileMode mode, bool doFilter, bool doRotate) : fBM(nullptr) { |
reed@google.com | b0b462b | 2012-03-08 22:13:39 +0000 | [diff] [blame] | 64 | fMode = mode; |
| 65 | fDoFilter = doFilter; |
| 66 | fDoRotate = doRotate; |
| 67 | } |
reed@google.com | 4bc0a9d | 2012-03-07 21:47:41 +0000 | [diff] [blame] | 68 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 69 | virtual ~GiantBitmapGM() { delete fBM; } |
reed@google.com | 4bc0a9d | 2012-03-07 21:47:41 +0000 | [diff] [blame] | 70 | |
| 71 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 72 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 73 | SkString onShortName() override { |
reed@google.com | 4bc0a9d | 2012-03-07 21:47:41 +0000 | [diff] [blame] | 74 | SkString str("giantbitmap_"); |
| 75 | switch (fMode) { |
| 76 | case SkShader::kClamp_TileMode: |
| 77 | str.append("clamp"); |
| 78 | break; |
| 79 | case SkShader::kRepeat_TileMode: |
| 80 | str.append("repeat"); |
| 81 | break; |
| 82 | case SkShader::kMirror_TileMode: |
| 83 | str.append("mirror"); |
| 84 | break; |
| 85 | default: |
| 86 | break; |
| 87 | } |
reed@google.com | b0b462b | 2012-03-08 22:13:39 +0000 | [diff] [blame] | 88 | str.append(fDoFilter ? "_bilerp" : "_point"); |
| 89 | str.append(fDoRotate ? "_rotate" : "_scale"); |
reed@google.com | 4bc0a9d | 2012-03-07 21:47:41 +0000 | [diff] [blame] | 90 | return str; |
| 91 | } |
| 92 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 93 | SkISize onISize() override { return SkISize::Make(640, 480); } |
reed@google.com | 4bc0a9d | 2012-03-07 21:47:41 +0000 | [diff] [blame] | 94 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 95 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | 4bc0a9d | 2012-03-07 21:47:41 +0000 | [diff] [blame] | 96 | SkPaint paint; |
reed@google.com | 4bc0a9d | 2012-03-07 21:47:41 +0000 | [diff] [blame] | 97 | |
reed@google.com | b0b462b | 2012-03-08 22:13:39 +0000 | [diff] [blame] | 98 | SkMatrix m; |
| 99 | if (fDoRotate) { |
| 100 | // m.setRotate(SkIntToScalar(30), 0, 0); |
| 101 | m.setSkew(SK_Scalar1, 0, 0, 0); |
reed@google.com | 6005aeb | 2012-03-14 20:15:47 +0000 | [diff] [blame] | 102 | // m.postScale(2*SK_Scalar1/3, 2*SK_Scalar1/3); |
reed@google.com | b0b462b | 2012-03-08 22:13:39 +0000 | [diff] [blame] | 103 | } else { |
reed@google.com | 6005aeb | 2012-03-14 20:15:47 +0000 | [diff] [blame] | 104 | SkScalar scale = 11*SK_Scalar1/12; |
| 105 | m.setScale(scale, scale); |
reed@google.com | b0b462b | 2012-03-08 22:13:39 +0000 | [diff] [blame] | 106 | } |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 107 | paint.setShader(SkShader::MakeBitmapShader(getBitmap(), fMode, fMode, &m)); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 108 | paint.setFilterQuality(fDoFilter ? kLow_SkFilterQuality : kNone_SkFilterQuality); |
reed@google.com | b0b462b | 2012-03-08 22:13:39 +0000 | [diff] [blame] | 109 | |
| 110 | canvas->translate(SkIntToScalar(50), SkIntToScalar(50)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 111 | |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 112 | // SkRect r = SkRect::MakeXYWH(-50, -50, 32, 16); |
reed@google.com | 6005aeb | 2012-03-14 20:15:47 +0000 | [diff] [blame] | 113 | // canvas->drawRect(r, paint); return; |
reed@google.com | 4bc0a9d | 2012-03-07 21:47:41 +0000 | [diff] [blame] | 114 | canvas->drawPaint(paint); |
| 115 | } |
| 116 | |
| 117 | private: |
| 118 | typedef GM INHERITED; |
| 119 | }; |
| 120 | |
| 121 | /////////////////////////////////////////////////////////////////////////////// |
| 122 | |
scroggo | 96f16e8 | 2015-12-10 13:31:59 -0800 | [diff] [blame] | 123 | DEF_GM( return new GiantBitmapGM(SkShader::kClamp_TileMode, false, false); ) |
| 124 | DEF_GM( return new GiantBitmapGM(SkShader::kRepeat_TileMode, false, false); ) |
| 125 | DEF_GM( return new GiantBitmapGM(SkShader::kMirror_TileMode, false, false); ) |
| 126 | DEF_GM( return new GiantBitmapGM(SkShader::kClamp_TileMode, true, false); ) |
| 127 | DEF_GM( return new GiantBitmapGM(SkShader::kRepeat_TileMode, true, false); ) |
| 128 | DEF_GM( return new GiantBitmapGM(SkShader::kMirror_TileMode, true, false); ) |
reed@google.com | 4bc0a9d | 2012-03-07 21:47:41 +0000 | [diff] [blame] | 129 | |
scroggo | 96f16e8 | 2015-12-10 13:31:59 -0800 | [diff] [blame] | 130 | DEF_GM( return new GiantBitmapGM(SkShader::kClamp_TileMode, false, true); ) |
| 131 | DEF_GM( return new GiantBitmapGM(SkShader::kRepeat_TileMode, false, true); ) |
| 132 | DEF_GM( return new GiantBitmapGM(SkShader::kMirror_TileMode, false, true); ) |
| 133 | DEF_GM( return new GiantBitmapGM(SkShader::kClamp_TileMode, true, true); ) |
| 134 | DEF_GM( return new GiantBitmapGM(SkShader::kRepeat_TileMode, true, true); ) |
| 135 | DEF_GM( return new GiantBitmapGM(SkShader::kMirror_TileMode, true, true); ) |