senorblanco@chromium.org | b2ee33c | 2012-08-22 16:24:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 "SkBlendImageFilter.h" |
| 10 | #include "SkBitmapSource.h" |
| 11 | |
| 12 | namespace skiagm { |
| 13 | |
| 14 | class ImageBlendGM : public GM { |
| 15 | public: |
| 16 | ImageBlendGM() : fInitialized(false) { |
| 17 | this->setBGColor(0xFF000000); |
| 18 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 19 | |
senorblanco@chromium.org | b2ee33c | 2012-08-22 16:24:44 +0000 | [diff] [blame] | 20 | protected: |
| 21 | virtual SkString onShortName() { |
| 22 | return SkString("blend"); |
| 23 | } |
| 24 | |
| 25 | void make_bitmap() { |
| 26 | fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 80, 80); |
| 27 | fBitmap.allocPixels(); |
| 28 | SkDevice device(fBitmap); |
| 29 | SkCanvas canvas(&device); |
| 30 | canvas.clear(0x00000000); |
| 31 | SkPaint paint; |
| 32 | paint.setAntiAlias(true); |
| 33 | paint.setColor(0xD000D000); |
| 34 | paint.setTextSize(SkIntToScalar(96)); |
| 35 | const char* str = "e"; |
| 36 | canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(65), paint); |
| 37 | } |
| 38 | |
| 39 | void make_checkerboard() { |
| 40 | fCheckerboard.setConfig(SkBitmap::kARGB_8888_Config, 80, 80); |
| 41 | fCheckerboard.allocPixels(); |
| 42 | SkDevice device(fCheckerboard); |
| 43 | SkCanvas canvas(&device); |
| 44 | canvas.clear(0x00000000); |
| 45 | SkPaint darkPaint; |
| 46 | darkPaint.setColor(0xFF404040); |
| 47 | SkPaint lightPaint; |
| 48 | lightPaint.setColor(0xFFA0A0A0); |
| 49 | for (int y = 0; y < 80; y += 16) { |
| 50 | for (int x = 0; x < 80; x += 16) { |
| 51 | canvas.save(); |
robertphillips@google.com | 94acc70 | 2012-09-06 18:43:21 +0000 | [diff] [blame] | 52 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); |
senorblanco@chromium.org | b2ee33c | 2012-08-22 16:24:44 +0000 | [diff] [blame] | 53 | canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint); |
| 54 | canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint); |
| 55 | canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint); |
| 56 | canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint); |
| 57 | canvas.restore(); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | virtual SkISize onISize() { |
| 63 | return make_isize(500, 100); |
| 64 | } |
| 65 | |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 66 | void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x) { |
| 67 | canvas->save(); |
| 68 | canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), 0, |
| 69 | SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height()))); |
| 70 | canvas->drawBitmap(fBitmap, SkIntToScalar(x), 0, &paint); |
| 71 | canvas->restore(); |
| 72 | } |
| 73 | |
senorblanco@chromium.org | b2ee33c | 2012-08-22 16:24:44 +0000 | [diff] [blame] | 74 | virtual void onDraw(SkCanvas* canvas) { |
| 75 | if (!fInitialized) { |
| 76 | make_bitmap(); |
| 77 | make_checkerboard(); |
| 78 | fInitialized = true; |
| 79 | } |
| 80 | canvas->clear(0x00000000); |
| 81 | SkPaint paint; |
| 82 | SkAutoTUnref<SkImageFilter> background(SkNEW_ARGS(SkBitmapSource, (fCheckerboard))); |
| 83 | paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter, (SkBlendImageFilter::kNormal_Mode, background)))->unref(); |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 84 | drawClippedBitmap(canvas, paint, 0); |
senorblanco@chromium.org | b2ee33c | 2012-08-22 16:24:44 +0000 | [diff] [blame] | 85 | paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter, (SkBlendImageFilter::kMultiply_Mode, background)))->unref(); |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 86 | drawClippedBitmap(canvas, paint, 100); |
senorblanco@chromium.org | b2ee33c | 2012-08-22 16:24:44 +0000 | [diff] [blame] | 87 | paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter, (SkBlendImageFilter::kScreen_Mode, background)))->unref(); |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 88 | drawClippedBitmap(canvas, paint, 200); |
senorblanco@chromium.org | b2ee33c | 2012-08-22 16:24:44 +0000 | [diff] [blame] | 89 | paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter, (SkBlendImageFilter::kDarken_Mode, background)))->unref(); |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 90 | drawClippedBitmap(canvas, paint, 300); |
senorblanco@chromium.org | b2ee33c | 2012-08-22 16:24:44 +0000 | [diff] [blame] | 91 | paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter, (SkBlendImageFilter::kLighten_Mode, background)))->unref(); |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 92 | drawClippedBitmap(canvas, paint, 400); |
senorblanco@chromium.org | b2ee33c | 2012-08-22 16:24:44 +0000 | [diff] [blame] | 93 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 94 | |
senorblanco@chromium.org | b2ee33c | 2012-08-22 16:24:44 +0000 | [diff] [blame] | 95 | private: |
| 96 | typedef GM INHERITED; |
| 97 | SkBitmap fBitmap, fCheckerboard; |
| 98 | bool fInitialized; |
| 99 | }; |
| 100 | |
| 101 | ////////////////////////////////////////////////////////////////////////////// |
| 102 | |
| 103 | static GM* MyFactory(void*) { return new ImageBlendGM; } |
| 104 | static GMRegistry reg(MyFactory); |
| 105 | |
| 106 | } |