sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +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 "SkDisplacementMapEffect.h" |
| 10 | #include "SkBitmapSource.h" |
| 11 | |
| 12 | namespace skiagm { |
| 13 | |
| 14 | class DisplacementMapGM : public GM { |
| 15 | public: |
| 16 | DisplacementMapGM() : fInitialized(false) { |
| 17 | this->setBGColor(0xFF000000); |
| 18 | } |
| 19 | |
| 20 | protected: |
| 21 | virtual SkString onShortName() { |
| 22 | return SkString("displacement"); |
| 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(0xFF884422); |
| 34 | paint.setTextSize(SkIntToScalar(96)); |
| 35 | const char* str = "g"; |
| 36 | canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(55), 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(0xFF804020); |
| 47 | SkPaint lightPaint; |
| 48 | lightPaint.setColor(0xFF244484); |
| 49 | for (int y = 0; y < 80; y += 16) { |
| 50 | for (int x = 0; x < 80; x += 16) { |
| 51 | canvas.save(); |
| 52 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 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, 200); |
| 64 | } |
skia.committer@gmail.com | ff21c2e | 2013-01-16 07:05:56 +0000 | [diff] [blame] | 65 | |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 66 | void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) { |
| 67 | canvas->save(); |
| 68 | canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y), |
| 69 | SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height()))); |
| 70 | canvas->drawBitmap(fBitmap, SkIntToScalar(x), SkIntToScalar(y), &paint); |
| 71 | canvas->restore(); |
| 72 | } |
| 73 | |
| 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> displ(SkNEW_ARGS(SkBitmapSource, (fCheckerboard))); |
| 83 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 84 | (SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 85 | SkDisplacementMapEffect::kG_ChannelSelectorType, 0.0f, displ)))->unref(); |
| 86 | drawClippedBitmap(canvas, 0, 0, paint); |
| 87 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 88 | (SkDisplacementMapEffect::kB_ChannelSelectorType, |
sugoi@google.com | 4ecd42e | 2013-03-19 15:07:30 +0000 | [diff] [blame] | 89 | SkDisplacementMapEffect::kA_ChannelSelectorType, 16.0f, displ)))->unref(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 90 | drawClippedBitmap(canvas, 100, 0, paint); |
| 91 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 92 | (SkDisplacementMapEffect::kR_ChannelSelectorType, |
sugoi@google.com | 4ecd42e | 2013-03-19 15:07:30 +0000 | [diff] [blame] | 93 | SkDisplacementMapEffect::kB_ChannelSelectorType, 32.0f, displ)))->unref(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 94 | drawClippedBitmap(canvas, 200, 0, paint); |
| 95 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 96 | (SkDisplacementMapEffect::kG_ChannelSelectorType, |
sugoi@google.com | 4ecd42e | 2013-03-19 15:07:30 +0000 | [diff] [blame] | 97 | SkDisplacementMapEffect::kA_ChannelSelectorType, 48.0f, displ)))->unref(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 98 | drawClippedBitmap(canvas, 300, 0, paint); |
| 99 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 100 | (SkDisplacementMapEffect::kR_ChannelSelectorType, |
sugoi@google.com | 4ecd42e | 2013-03-19 15:07:30 +0000 | [diff] [blame] | 101 | SkDisplacementMapEffect::kA_ChannelSelectorType, 64.0f, displ)))->unref(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 102 | drawClippedBitmap(canvas, 400, 0, paint); |
| 103 | |
| 104 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 105 | (SkDisplacementMapEffect::kR_ChannelSelectorType, |
sugoi@google.com | 4ecd42e | 2013-03-19 15:07:30 +0000 | [diff] [blame] | 106 | SkDisplacementMapEffect::kG_ChannelSelectorType, 40.0f, displ)))->unref(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 107 | drawClippedBitmap(canvas, 0, 100, paint); |
| 108 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 109 | (SkDisplacementMapEffect::kB_ChannelSelectorType, |
sugoi@google.com | 4ecd42e | 2013-03-19 15:07:30 +0000 | [diff] [blame] | 110 | SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ)))->unref(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 111 | drawClippedBitmap(canvas, 100, 100, paint); |
| 112 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 113 | (SkDisplacementMapEffect::kR_ChannelSelectorType, |
sugoi@google.com | 4ecd42e | 2013-03-19 15:07:30 +0000 | [diff] [blame] | 114 | SkDisplacementMapEffect::kB_ChannelSelectorType, 40.0f, displ)))->unref(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 115 | drawClippedBitmap(canvas, 200, 100, paint); |
| 116 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 117 | (SkDisplacementMapEffect::kG_ChannelSelectorType, |
sugoi@google.com | 4ecd42e | 2013-03-19 15:07:30 +0000 | [diff] [blame] | 118 | SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ)))->unref(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 119 | drawClippedBitmap(canvas, 300, 100, paint); |
| 120 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 121 | (SkDisplacementMapEffect::kR_ChannelSelectorType, |
sugoi@google.com | 4ecd42e | 2013-03-19 15:07:30 +0000 | [diff] [blame] | 122 | SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ)))->unref(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 123 | drawClippedBitmap(canvas, 400, 100, paint); |
| 124 | } |
| 125 | |
| 126 | private: |
| 127 | typedef GM INHERITED; |
| 128 | SkBitmap fBitmap, fCheckerboard; |
| 129 | bool fInitialized; |
| 130 | }; |
| 131 | |
| 132 | ////////////////////////////////////////////////////////////////////////////// |
| 133 | |
| 134 | static GM* MyFactory(void*) { return new DisplacementMapGM; } |
| 135 | static GMRegistry reg(MyFactory); |
| 136 | |
| 137 | } |