commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +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 | #include "gm.h" |
| 8 | |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkPaint.h" |
| 11 | #include "SkShader.h" |
| 12 | |
| 13 | namespace skiagm { |
| 14 | |
| 15 | static void draw_bm(SkBitmap* bm) { |
skia.committer@gmail.com | 56710d1 | 2013-12-07 07:02:21 +0000 | [diff] [blame] | 16 | SkPaint bluePaint; |
| 17 | bluePaint.setColor(SK_ColorBLUE); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 18 | |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 19 | bm->allocN32Pixels(20, 20); |
skia.committer@gmail.com | 56710d1 | 2013-12-07 07:02:21 +0000 | [diff] [blame] | 20 | bm->eraseColor(SK_ColorRED); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 21 | |
skia.committer@gmail.com | 56710d1 | 2013-12-07 07:02:21 +0000 | [diff] [blame] | 22 | SkCanvas canvas(*bm); |
| 23 | canvas.drawCircle(10, 10, 5, bluePaint); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | static void draw_mask(SkBitmap* bm) { |
skia.committer@gmail.com | 56710d1 | 2013-12-07 07:02:21 +0000 | [diff] [blame] | 27 | SkPaint circlePaint; |
| 28 | circlePaint.setColor(SK_ColorBLACK); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 29 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 30 | bm->allocPixels(SkImageInfo::MakeA8(20, 20)); |
skia.committer@gmail.com | 56710d1 | 2013-12-07 07:02:21 +0000 | [diff] [blame] | 31 | bm->eraseColor(SK_ColorTRANSPARENT); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 32 | |
skia.committer@gmail.com | 56710d1 | 2013-12-07 07:02:21 +0000 | [diff] [blame] | 33 | SkCanvas canvas(*bm); |
| 34 | canvas.drawCircle(10, 10, 10, circlePaint); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | class BitmapShaderGM : public GM { |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 38 | |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 39 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 40 | void onOnceBeforeDraw() override { |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 41 | this->setBGColor(sk_tool_utils::color_to_565(SK_ColorGRAY)); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 42 | draw_bm(&fBitmap); |
| 43 | draw_mask(&fMask); |
| 44 | } |
| 45 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 46 | SkString onShortName() override { |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 47 | return SkString("bitmapshaders"); |
| 48 | } |
| 49 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 50 | SkISize onISize() override { |
commit-bot@chromium.org | 08f6d86 | 2014-03-24 02:49:48 +0000 | [diff] [blame] | 51 | return SkISize::Make(150, 100); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 52 | } |
| 53 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 54 | void onDraw(SkCanvas* canvas) override { |
skia.committer@gmail.com | 56710d1 | 2013-12-07 07:02:21 +0000 | [diff] [blame] | 55 | SkPaint paint; |
commit-bot@chromium.org | e5957f6 | 2014-03-18 16:28:12 +0000 | [diff] [blame] | 56 | |
commit-bot@chromium.org | 08f6d86 | 2014-03-24 02:49:48 +0000 | [diff] [blame] | 57 | for (int i = 0; i < 2; i++) { |
| 58 | SkMatrix s; |
| 59 | s.reset(); |
| 60 | if (1 == i) { |
commit-bot@chromium.org | a723fee | 2014-03-24 14:54:04 +0000 | [diff] [blame] | 61 | s.setScale(1.5f, 1.5f); |
commit-bot@chromium.org | 08f6d86 | 2014-03-24 02:49:48 +0000 | [diff] [blame] | 62 | s.postTranslate(2, 2); |
| 63 | } |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 64 | |
commit-bot@chromium.org | 08f6d86 | 2014-03-24 02:49:48 +0000 | [diff] [blame] | 65 | canvas->save(); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 66 | paint.setShader(SkShader::MakeBitmapShader(fBitmap, SkShader::kClamp_TileMode, |
| 67 | SkShader::kClamp_TileMode, &s)); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 68 | |
commit-bot@chromium.org | 08f6d86 | 2014-03-24 02:49:48 +0000 | [diff] [blame] | 69 | // draw the shader with a bitmap mask |
| 70 | canvas->drawBitmap(fMask, 0, 0, &paint); |
| 71 | canvas->drawBitmap(fMask, 30, 0, &paint); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 72 | |
commit-bot@chromium.org | 08f6d86 | 2014-03-24 02:49:48 +0000 | [diff] [blame] | 73 | canvas->translate(0, 25); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 74 | |
commit-bot@chromium.org | 08f6d86 | 2014-03-24 02:49:48 +0000 | [diff] [blame] | 75 | canvas->drawCircle(10, 10, 10, paint); |
| 76 | canvas->drawCircle(40, 10, 10, paint); // no blue circle expected |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 77 | |
commit-bot@chromium.org | 08f6d86 | 2014-03-24 02:49:48 +0000 | [diff] [blame] | 78 | canvas->translate(0, 25); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 79 | |
commit-bot@chromium.org | 08f6d86 | 2014-03-24 02:49:48 +0000 | [diff] [blame] | 80 | // clear the shader, colorized by a solid color with a bitmap mask |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 81 | paint.setShader(nullptr); |
commit-bot@chromium.org | 08f6d86 | 2014-03-24 02:49:48 +0000 | [diff] [blame] | 82 | paint.setColor(SK_ColorGREEN); |
| 83 | canvas->drawBitmap(fMask, 0, 0, &paint); |
| 84 | canvas->drawBitmap(fMask, 30, 0, &paint); |
| 85 | |
| 86 | canvas->translate(0, 25); |
| 87 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 88 | paint.setShader(SkShader::MakeBitmapShader(fMask, SkShader::kRepeat_TileMode, |
| 89 | SkShader::kRepeat_TileMode, &s)); |
commit-bot@chromium.org | 08f6d86 | 2014-03-24 02:49:48 +0000 | [diff] [blame] | 90 | paint.setColor(SK_ColorRED); |
| 91 | |
| 92 | // draw the mask using the shader and a color |
| 93 | canvas->drawRect(SkRect::MakeXYWH(0, 0, 20, 20), paint); |
| 94 | canvas->drawRect(SkRect::MakeXYWH(30, 0, 20, 20), paint); |
| 95 | canvas->restore(); |
| 96 | canvas->translate(60, 0); |
| 97 | } |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | private: |
| 101 | SkBitmap fBitmap; |
| 102 | SkBitmap fMask; |
| 103 | |
| 104 | typedef GM INHERITED; |
| 105 | }; |
| 106 | |
| 107 | ////////////////////////////////////////////////////////////////////////////// |
| 108 | |
| 109 | static GM* MyFactory(void*) { return new BitmapShaderGM; } |
| 110 | static GMRegistry reg(MyFactory); |
| 111 | |
| 112 | } |