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; |
| 30 | bmp.setConfig(SkBitmap::kARGB_8888_Config, 2, 2); |
| 31 | bmp.allocPixels(); |
| 32 | bmp.lockPixels(); |
| 33 | uint32_t* pixels = reinterpret_cast<uint32_t*>(bmp.getPixels()); |
| 34 | pixels[0] = SkPreMultiplyColor(SK_ColorRED); |
| 35 | pixels[1] = SkPreMultiplyColor(SK_ColorGREEN); |
| 36 | pixels[2] = SkPreMultiplyColor(SK_ColorBLACK); |
| 37 | pixels[3] = SkPreMultiplyColor(SK_ColorBLUE); |
| 38 | bmp.unlockPixels(); |
| 39 | |
| 40 | return bmp; |
| 41 | } |
| 42 | |
| 43 | static const SkScalar RECT_SIZE = 64; |
| 44 | static const SkScalar SLIDE_SIZE = 300; |
| 45 | |
| 46 | class ClippedBitmapShadersGM : public GM { |
| 47 | public: |
commit-bot@chromium.org | 759befb | 2013-10-23 16:18:50 +0000 | [diff] [blame] | 48 | ClippedBitmapShadersGM(SkShader::TileMode mode, bool hq=false) |
| 49 | : fMode(mode), fHQ(hq) { |
commit-bot@chromium.org | e324cc6 | 2013-08-21 23:10:45 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | protected: |
| 53 | SkShader::TileMode fMode; |
commit-bot@chromium.org | 759befb | 2013-10-23 16:18:50 +0000 | [diff] [blame] | 54 | bool fHQ; |
commit-bot@chromium.org | e324cc6 | 2013-08-21 23:10:45 +0000 | [diff] [blame] | 55 | |
| 56 | virtual SkString onShortName() { |
| 57 | SkString descriptor; |
| 58 | switch (fMode) { |
| 59 | case SkShader::kRepeat_TileMode: |
| 60 | descriptor = "tile"; |
| 61 | break; |
| 62 | case SkShader::kMirror_TileMode: |
| 63 | descriptor = "mirror"; |
| 64 | break; |
| 65 | case SkShader::kClamp_TileMode: |
| 66 | descriptor = "clamp"; |
| 67 | break; |
| 68 | default: |
| 69 | SkASSERT(false); |
| 70 | } |
| 71 | descriptor.prepend("clipped-bitmap-shaders-"); |
commit-bot@chromium.org | 759befb | 2013-10-23 16:18:50 +0000 | [diff] [blame] | 72 | if (fHQ) { |
| 73 | descriptor.append("-hq"); |
| 74 | } |
commit-bot@chromium.org | e324cc6 | 2013-08-21 23:10:45 +0000 | [diff] [blame] | 75 | return descriptor; |
| 76 | } |
| 77 | |
| 78 | virtual SkISize onISize() { |
| 79 | return SkISize::Make(300, 300); |
| 80 | } |
skia.committer@gmail.com | 583b18a | 2013-10-24 07:01:59 +0000 | [diff] [blame] | 81 | |
commit-bot@chromium.org | e324cc6 | 2013-08-21 23:10:45 +0000 | [diff] [blame] | 82 | virtual void onDraw(SkCanvas* canvas) { |
| 83 | SkBitmap bmp = create_bitmap(); |
| 84 | SkShader* shader = SkShader::CreateBitmapShader( |
| 85 | bmp, fMode, fMode); |
| 86 | |
| 87 | SkPaint paint; |
| 88 | SkMatrix s; |
| 89 | s.reset(); |
| 90 | s.setScale(8, 8); |
| 91 | s.postTranslate(SLIDE_SIZE / 2, SLIDE_SIZE / 2); |
| 92 | shader->setLocalMatrix(s); |
| 93 | paint.setShader(shader)->unref(); |
skia.committer@gmail.com | 583b18a | 2013-10-24 07:01:59 +0000 | [diff] [blame] | 94 | |
commit-bot@chromium.org | 759befb | 2013-10-23 16:18:50 +0000 | [diff] [blame] | 95 | if (fHQ) { |
| 96 | paint.setFilterLevel(SkPaint::kHigh_FilterLevel); |
| 97 | } |
commit-bot@chromium.org | e324cc6 | 2013-08-21 23:10:45 +0000 | [diff] [blame] | 98 | |
| 99 | SkScalar margin = (SLIDE_SIZE / 3 - RECT_SIZE) / 2; |
| 100 | for (int i = 0; i < 3; i++) { |
| 101 | SkScalar yOrigin = SLIDE_SIZE / 3 * i + margin; |
| 102 | for (int j = 0; j < 3; j++) { |
| 103 | SkScalar xOrigin = SLIDE_SIZE / 3 * j + margin; |
| 104 | if (i == 1 && j == 1) { |
| 105 | continue; // skip center element |
| 106 | } |
| 107 | SkRect rect = SkRect::MakeXYWH(xOrigin, yOrigin, |
| 108 | RECT_SIZE, RECT_SIZE); |
| 109 | canvas->save(); |
| 110 | canvas->clipRect(rect); |
| 111 | canvas->drawRect(rect, paint); |
| 112 | canvas->restore(); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | private: |
| 118 | typedef GM INHERITED; |
| 119 | }; |
| 120 | |
| 121 | ////////////////////////////////////////////////////////////////////////////// |
| 122 | |
| 123 | DEF_GM( return new ClippedBitmapShadersGM(SkShader::kRepeat_TileMode); ) |
| 124 | DEF_GM( return new ClippedBitmapShadersGM(SkShader::kMirror_TileMode); ) |
| 125 | DEF_GM( return new ClippedBitmapShadersGM(SkShader::kClamp_TileMode); ) |
commit-bot@chromium.org | 759befb | 2013-10-23 16:18:50 +0000 | [diff] [blame] | 126 | |
| 127 | DEF_GM( return new ClippedBitmapShadersGM(SkShader::kRepeat_TileMode, true); ) |
| 128 | DEF_GM( return new ClippedBitmapShadersGM(SkShader::kMirror_TileMode, true); ) |
| 129 | DEF_GM( return new ClippedBitmapShadersGM(SkShader::kClamp_TileMode, true); ) |
| 130 | |
| 131 | |
commit-bot@chromium.org | e324cc6 | 2013-08-21 23:10:45 +0000 | [diff] [blame] | 132 | } |