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 | |
skia.committer@gmail.com | 56710d1 | 2013-12-07 07:02:21 +0000 | [diff] [blame] | 20 | bm->setConfig(SkBitmap::kARGB_8888_Config, 20, 20); |
| 21 | bm->allocPixels(); |
| 22 | bm->eraseColor(SK_ColorRED); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 23 | |
skia.committer@gmail.com | 56710d1 | 2013-12-07 07:02:21 +0000 | [diff] [blame] | 24 | SkCanvas canvas(*bm); |
| 25 | canvas.drawCircle(10, 10, 5, bluePaint); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | static void draw_mask(SkBitmap* bm) { |
skia.committer@gmail.com | 56710d1 | 2013-12-07 07:02:21 +0000 | [diff] [blame] | 29 | SkPaint circlePaint; |
| 30 | circlePaint.setColor(SK_ColorBLACK); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 31 | |
skia.committer@gmail.com | 56710d1 | 2013-12-07 07:02:21 +0000 | [diff] [blame] | 32 | bm->setConfig(SkBitmap::kA8_Config, 20, 20); |
| 33 | bm->allocPixels(); |
| 34 | bm->eraseColor(SK_ColorTRANSPARENT); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 35 | |
skia.committer@gmail.com | 56710d1 | 2013-12-07 07:02:21 +0000 | [diff] [blame] | 36 | SkCanvas canvas(*bm); |
| 37 | canvas.drawCircle(10, 10, 10, circlePaint); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | class BitmapShaderGM : public GM { |
| 41 | public: |
| 42 | |
| 43 | BitmapShaderGM() { |
| 44 | this->setBGColor(SK_ColorGRAY); |
| 45 | draw_bm(&fBitmap); |
| 46 | draw_mask(&fMask); |
| 47 | } |
| 48 | |
| 49 | protected: |
| 50 | virtual SkString onShortName() { |
| 51 | return SkString("bitmapshaders"); |
| 52 | } |
| 53 | |
| 54 | virtual SkISize onISize() { |
| 55 | return make_isize(75, 100); |
| 56 | } |
| 57 | |
| 58 | virtual void onDraw(SkCanvas* canvas) { |
skia.committer@gmail.com | 56710d1 | 2013-12-07 07:02:21 +0000 | [diff] [blame] | 59 | SkShader* shader = SkShader::CreateBitmapShader(fBitmap, |
| 60 | SkShader::kClamp_TileMode, |
| 61 | SkShader::kClamp_TileMode); |
| 62 | SkPaint paint; |
| 63 | paint.setShader(shader); |
| 64 | // release the shader ref as the paint now holds a reference |
| 65 | shader->unref(); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 66 | |
skia.committer@gmail.com | 56710d1 | 2013-12-07 07:02:21 +0000 | [diff] [blame] | 67 | // draw the shader with a bitmap mask |
| 68 | canvas->drawBitmap(fMask, 0, 0, &paint); |
| 69 | canvas->drawBitmap(fMask, 30, 0, &paint); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 70 | |
skia.committer@gmail.com | 56710d1 | 2013-12-07 07:02:21 +0000 | [diff] [blame] | 71 | canvas->translate(0, 25); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 72 | |
skia.committer@gmail.com | 56710d1 | 2013-12-07 07:02:21 +0000 | [diff] [blame] | 73 | // draw the shader with standard geometry |
| 74 | canvas->drawCircle(10, 10, 10, paint); |
| 75 | canvas->drawCircle(40, 10, 10, paint); // no blue circle expected |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 76 | |
skia.committer@gmail.com | 56710d1 | 2013-12-07 07:02:21 +0000 | [diff] [blame] | 77 | canvas->translate(0, 25); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 78 | |
skia.committer@gmail.com | 56710d1 | 2013-12-07 07:02:21 +0000 | [diff] [blame] | 79 | shader = SkShader::CreateBitmapShader(fMask, |
| 80 | SkShader::kRepeat_TileMode, |
| 81 | SkShader::kRepeat_TileMode); |
| 82 | paint.setShader(shader); |
| 83 | paint.setColor(SK_ColorRED); |
| 84 | shader->unref(); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 85 | |
skia.committer@gmail.com | 56710d1 | 2013-12-07 07:02:21 +0000 | [diff] [blame] | 86 | // draw the mask using the shader and a color |
| 87 | canvas->drawRect(SkRect::MakeXYWH(0, 0, 20, 20), paint); |
| 88 | canvas->drawRect(SkRect::MakeXYWH(30, 0, 20, 20), paint); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | private: |
| 92 | SkBitmap fBitmap; |
| 93 | SkBitmap fMask; |
| 94 | |
| 95 | typedef GM INHERITED; |
| 96 | }; |
| 97 | |
| 98 | ////////////////////////////////////////////////////////////////////////////// |
| 99 | |
| 100 | static GM* MyFactory(void*) { return new BitmapShaderGM; } |
| 101 | static GMRegistry reg(MyFactory); |
| 102 | |
| 103 | } |