commit-bot@chromium.org | e324cc6 | 2013-08-21 23:10:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 "SkBitmap.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkColor.h" |
| 12 | #include "SkShader.h" |
| 13 | |
| 14 | namespace skiagm { |
| 15 | |
| 16 | // This GM draws a 3x3 grid (with the center element excluded) of rectangles |
| 17 | // filled with a bitmap shader. The bitmap shader is transformed so that the |
| 18 | // pattern cell is at the center (excluded) region. |
| 19 | // |
| 20 | // In Repeat and Mirror mode, this tests that the bitmap shader still draws |
| 21 | // even though the pattern cell is outside the clip. |
| 22 | // |
| 23 | // In Clamp mode, this tests that the clamp is handled properly. For PDF, |
| 24 | // (and possibly other exported formats) this also "tests" that the image itself |
| 25 | // is not stored (well, you'll need to open it up with an external tool to |
| 26 | // verify that). |
| 27 | |
| 28 | static SkBitmap create_bitmap() { |
| 29 | SkBitmap bmp; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 30 | bmp.allocN32Pixels(2, 2); |
commit-bot@chromium.org | e324cc6 | 2013-08-21 23:10:45 +0000 | [diff] [blame] | 31 | uint32_t* pixels = reinterpret_cast<uint32_t*>(bmp.getPixels()); |
| 32 | pixels[0] = SkPreMultiplyColor(SK_ColorRED); |
| 33 | pixels[1] = SkPreMultiplyColor(SK_ColorGREEN); |
| 34 | pixels[2] = SkPreMultiplyColor(SK_ColorBLACK); |
| 35 | pixels[3] = SkPreMultiplyColor(SK_ColorBLUE); |
commit-bot@chromium.org | e324cc6 | 2013-08-21 23:10:45 +0000 | [diff] [blame] | 36 | |
| 37 | return bmp; |
| 38 | } |
| 39 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 40 | constexpr SkScalar RECT_SIZE = 64; |
| 41 | constexpr SkScalar SLIDE_SIZE = 300; |
commit-bot@chromium.org | e324cc6 | 2013-08-21 23:10:45 +0000 | [diff] [blame] | 42 | |
| 43 | class ClippedBitmapShadersGM : public GM { |
| 44 | public: |
commit-bot@chromium.org | 759befb | 2013-10-23 16:18:50 +0000 | [diff] [blame] | 45 | ClippedBitmapShadersGM(SkShader::TileMode mode, bool hq=false) |
| 46 | : fMode(mode), fHQ(hq) { |
commit-bot@chromium.org | e324cc6 | 2013-08-21 23:10:45 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | protected: |
| 50 | SkShader::TileMode fMode; |
commit-bot@chromium.org | 759befb | 2013-10-23 16:18:50 +0000 | [diff] [blame] | 51 | bool fHQ; |
commit-bot@chromium.org | e324cc6 | 2013-08-21 23:10:45 +0000 | [diff] [blame] | 52 | |
| 53 | virtual SkString onShortName() { |
| 54 | SkString descriptor; |
| 55 | switch (fMode) { |
| 56 | case SkShader::kRepeat_TileMode: |
| 57 | descriptor = "tile"; |
| 58 | break; |
| 59 | case SkShader::kMirror_TileMode: |
| 60 | descriptor = "mirror"; |
| 61 | break; |
| 62 | case SkShader::kClamp_TileMode: |
| 63 | descriptor = "clamp"; |
| 64 | break; |
| 65 | default: |
| 66 | SkASSERT(false); |
| 67 | } |
| 68 | descriptor.prepend("clipped-bitmap-shaders-"); |
commit-bot@chromium.org | 759befb | 2013-10-23 16:18:50 +0000 | [diff] [blame] | 69 | if (fHQ) { |
| 70 | descriptor.append("-hq"); |
| 71 | } |
commit-bot@chromium.org | e324cc6 | 2013-08-21 23:10:45 +0000 | [diff] [blame] | 72 | return descriptor; |
| 73 | } |
| 74 | |
| 75 | virtual SkISize onISize() { |
| 76 | return SkISize::Make(300, 300); |
| 77 | } |
skia.committer@gmail.com | 583b18a | 2013-10-24 07:01:59 +0000 | [diff] [blame] | 78 | |
commit-bot@chromium.org | e324cc6 | 2013-08-21 23:10:45 +0000 | [diff] [blame] | 79 | virtual void onDraw(SkCanvas* canvas) { |
| 80 | SkBitmap bmp = create_bitmap(); |
commit-bot@chromium.org | e324cc6 | 2013-08-21 23:10:45 +0000 | [diff] [blame] | 81 | SkMatrix s; |
| 82 | s.reset(); |
| 83 | s.setScale(8, 8); |
| 84 | s.postTranslate(SLIDE_SIZE / 2, SLIDE_SIZE / 2); |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 85 | SkPaint paint; |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 86 | paint.setShader(SkShader::MakeBitmapShader(bmp, fMode, fMode, &s)); |
skia.committer@gmail.com | 583b18a | 2013-10-24 07:01:59 +0000 | [diff] [blame] | 87 | |
commit-bot@chromium.org | 759befb | 2013-10-23 16:18:50 +0000 | [diff] [blame] | 88 | if (fHQ) { |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 89 | paint.setFilterQuality(kHigh_SkFilterQuality); |
commit-bot@chromium.org | 759befb | 2013-10-23 16:18:50 +0000 | [diff] [blame] | 90 | } |
commit-bot@chromium.org | e324cc6 | 2013-08-21 23:10:45 +0000 | [diff] [blame] | 91 | |
| 92 | SkScalar margin = (SLIDE_SIZE / 3 - RECT_SIZE) / 2; |
| 93 | for (int i = 0; i < 3; i++) { |
| 94 | SkScalar yOrigin = SLIDE_SIZE / 3 * i + margin; |
| 95 | for (int j = 0; j < 3; j++) { |
| 96 | SkScalar xOrigin = SLIDE_SIZE / 3 * j + margin; |
| 97 | if (i == 1 && j == 1) { |
| 98 | continue; // skip center element |
| 99 | } |
| 100 | SkRect rect = SkRect::MakeXYWH(xOrigin, yOrigin, |
| 101 | RECT_SIZE, RECT_SIZE); |
| 102 | canvas->save(); |
| 103 | canvas->clipRect(rect); |
| 104 | canvas->drawRect(rect, paint); |
| 105 | canvas->restore(); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | private: |
| 111 | typedef GM INHERITED; |
| 112 | }; |
| 113 | |
| 114 | ////////////////////////////////////////////////////////////////////////////// |
| 115 | |
| 116 | DEF_GM( return new ClippedBitmapShadersGM(SkShader::kRepeat_TileMode); ) |
| 117 | DEF_GM( return new ClippedBitmapShadersGM(SkShader::kMirror_TileMode); ) |
| 118 | DEF_GM( return new ClippedBitmapShadersGM(SkShader::kClamp_TileMode); ) |
commit-bot@chromium.org | 759befb | 2013-10-23 16:18:50 +0000 | [diff] [blame] | 119 | |
| 120 | DEF_GM( return new ClippedBitmapShadersGM(SkShader::kRepeat_TileMode, true); ) |
| 121 | DEF_GM( return new ClippedBitmapShadersGM(SkShader::kMirror_TileMode, true); ) |
| 122 | DEF_GM( return new ClippedBitmapShadersGM(SkShader::kClamp_TileMode, true); ) |
| 123 | |
| 124 | |
commit-bot@chromium.org | e324cc6 | 2013-08-21 23:10:45 +0000 | [diff] [blame] | 125 | } |