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