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 | } |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 107 | SkShader* s = SkShader::CreateBitmapShader(getBitmap(), fMode, fMode, &m); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 108 | |
reed@google.com | b0b462b | 2012-03-08 22:13:39 +0000 | [diff] [blame] | 109 | paint.setShader(s)->unref(); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 110 | paint.setFilterQuality(fDoFilter ? kLow_SkFilterQuality : kNone_SkFilterQuality); |
reed@google.com | b0b462b | 2012-03-08 22:13:39 +0000 | [diff] [blame] | 111 | |
| 112 | canvas->translate(SkIntToScalar(50), SkIntToScalar(50)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 113 | |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 114 | // SkRect r = SkRect::MakeXYWH(-50, -50, 32, 16); |
reed@google.com | 6005aeb | 2012-03-14 20:15:47 +0000 | [diff] [blame] | 115 | // canvas->drawRect(r, paint); return; |
reed@google.com | 4bc0a9d | 2012-03-07 21:47:41 +0000 | [diff] [blame] | 116 | canvas->drawPaint(paint); |
| 117 | } |
| 118 | |
| 119 | private: |
| 120 | typedef GM INHERITED; |
| 121 | }; |
| 122 | |
| 123 | /////////////////////////////////////////////////////////////////////////////// |
| 124 | |
reed@google.com | b0b462b | 2012-03-08 22:13:39 +0000 | [diff] [blame] | 125 | static skiagm::GM* G000(void*) { return new GiantBitmapGM(SkShader::kClamp_TileMode, false, false); } |
| 126 | static skiagm::GM* G100(void*) { return new GiantBitmapGM(SkShader::kRepeat_TileMode, false, false); } |
| 127 | static skiagm::GM* G200(void*) { return new GiantBitmapGM(SkShader::kMirror_TileMode, false, false); } |
| 128 | static skiagm::GM* G010(void*) { return new GiantBitmapGM(SkShader::kClamp_TileMode, true, false); } |
| 129 | static skiagm::GM* G110(void*) { return new GiantBitmapGM(SkShader::kRepeat_TileMode, true, false); } |
| 130 | static skiagm::GM* G210(void*) { return new GiantBitmapGM(SkShader::kMirror_TileMode, true, false); } |
reed@google.com | 4bc0a9d | 2012-03-07 21:47:41 +0000 | [diff] [blame] | 131 | |
reed@google.com | b0b462b | 2012-03-08 22:13:39 +0000 | [diff] [blame] | 132 | static skiagm::GM* G001(void*) { return new GiantBitmapGM(SkShader::kClamp_TileMode, false, true); } |
| 133 | static skiagm::GM* G101(void*) { return new GiantBitmapGM(SkShader::kRepeat_TileMode, false, true); } |
| 134 | static skiagm::GM* G201(void*) { return new GiantBitmapGM(SkShader::kMirror_TileMode, false, true); } |
| 135 | static skiagm::GM* G011(void*) { return new GiantBitmapGM(SkShader::kClamp_TileMode, true, true); } |
| 136 | static skiagm::GM* G111(void*) { return new GiantBitmapGM(SkShader::kRepeat_TileMode, true, true); } |
| 137 | static skiagm::GM* G211(void*) { return new GiantBitmapGM(SkShader::kMirror_TileMode, true, true); } |
| 138 | |
| 139 | static skiagm::GMRegistry reg000(G000); |
| 140 | static skiagm::GMRegistry reg100(G100); |
| 141 | static skiagm::GMRegistry reg200(G200); |
| 142 | static skiagm::GMRegistry reg010(G010); |
| 143 | static skiagm::GMRegistry reg110(G110); |
| 144 | static skiagm::GMRegistry reg210(G210); |
| 145 | |
| 146 | static skiagm::GMRegistry reg001(G001); |
| 147 | static skiagm::GMRegistry reg101(G101); |
| 148 | static skiagm::GMRegistry reg201(G201); |
| 149 | static skiagm::GMRegistry reg011(G011); |
| 150 | static skiagm::GMRegistry reg111(G111); |
| 151 | static skiagm::GMRegistry reg211(G211); |