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