| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkBitmapSource.h" | 8 #include "SkBitmapSource.h" |
| 9 #include "SkDisplacementMapEffect.h" | 9 #include "SkDisplacementMapEffect.h" |
| 10 #include "gm.h" | 10 #include "gm.h" |
| 11 #include "sk_tool_utils.h" | 11 #include "sk_tool_utils.h" |
| 12 | 12 |
| 13 namespace skiagm { | 13 namespace skiagm { |
| 14 | 14 |
| 15 class DisplacementMapGM : public GM { | 15 class DisplacementMapGM : public GM { |
| 16 public: | 16 public: |
| 17 DisplacementMapGM() : fInitialized(false) { | 17 DisplacementMapGM() { |
| 18 this->setBGColor(0xFF000000); | 18 this->setBGColor(0xFF000000); |
| 19 } | 19 } |
| 20 | 20 |
| 21 protected: | 21 protected: |
| 22 virtual SkString onShortName() { | 22 SkString onShortName() override { |
| 23 return SkString("displacement"); | 23 return SkString("displacement"); |
| 24 } | 24 } |
| 25 | 25 |
| 26 void make_bitmaps() { | 26 void onOnceBeforeDraw() override { |
| 27 make_bitmap(); | 27 fBitmap = sk_tool_utils::create_string_bitmap(80, 80, 0xFF884422, 15, 55
, 96, "g"); |
| 28 make_checkerboard(&fCheckerboard, 80, 80); | 28 |
| 29 make_checkerboard(&fSmall, 64, 64); | 29 SkColor c1 = sk_tool_utils::color_to_565(0xFF244484); |
| 30 make_checkerboard(&fLarge, 96, 96); | 30 SkColor c2 = sk_tool_utils::color_to_565(0xFF804020); |
| 31 make_checkerboard(&fLargeW, 96, 64); | 31 |
| 32 make_checkerboard(&fLargeH, 64, 96); | 32 fCheckerboard = sk_tool_utils::create_checkerboard_bitmap(80, 80, c1, c2
, 8); |
| 33 fSmall = sk_tool_utils::create_checkerboard_bitmap(64, 64, c1, c2, 8); |
| 34 fLarge = sk_tool_utils::create_checkerboard_bitmap(96, 96, c1, c2, 8); |
| 35 fLargeW = sk_tool_utils::create_checkerboard_bitmap(96, 64, c1, c2, 8); |
| 36 fLargeH = sk_tool_utils::create_checkerboard_bitmap(64, 96, c1, c2, 8); |
| 33 } | 37 } |
| 34 | 38 |
| 35 void make_bitmap() { | 39 SkISize onISize() override { |
| 36 fBitmap.allocN32Pixels(80, 80); | |
| 37 SkCanvas canvas(fBitmap); | |
| 38 canvas.clear(0x00000000); | |
| 39 SkPaint paint; | |
| 40 paint.setAntiAlias(true); | |
| 41 sk_tool_utils::set_portable_typeface(&paint); | |
| 42 paint.setColor(sk_tool_utils::color_to_565(0xFF884422)); | |
| 43 paint.setTextSize(SkIntToScalar(96)); | |
| 44 const char* str = "g"; | |
| 45 canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(55),
paint); | |
| 46 } | |
| 47 | |
| 48 void make_checkerboard(SkBitmap* bitmap, int w, int h) { | |
| 49 bitmap->allocN32Pixels(w, h); | |
| 50 SkCanvas canvas(*bitmap); | |
| 51 sk_tool_utils::draw_checkerboard(&canvas, sk_tool_utils::color_to_565(0x
FF244484), | |
| 52 sk_tool_utils::color_to_565(0xFF804020), 8); | |
| 53 } | |
| 54 | |
| 55 virtual SkISize onISize() { | |
| 56 return SkISize::Make(500, 500); | 40 return SkISize::Make(500, 500); |
| 57 } | 41 } |
| 58 | 42 |
| 59 void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint)
{ | 43 void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint)
const { |
| 60 canvas->save(); | 44 canvas->save(); |
| 61 canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); | 45 canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 62 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(fBitmap.width()), SkIntToS
calar(fBitmap.height()))); | 46 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(fBitmap.width()), SkIntToS
calar(fBitmap.height()))); |
| 63 canvas->drawBitmap(fBitmap, 0, 0, &paint); | 47 canvas->drawBitmap(fBitmap, 0, 0, &paint); |
| 64 canvas->restore(); | 48 canvas->restore(); |
| 65 } | 49 } |
| 66 | 50 |
| 67 virtual void onDraw(SkCanvas* canvas) { | 51 void onDraw(SkCanvas* canvas) override { |
| 68 if (!fInitialized) { | |
| 69 make_bitmaps(); | |
| 70 fInitialized = true; | |
| 71 } | |
| 72 canvas->clear(SK_ColorBLACK); | 52 canvas->clear(SK_ColorBLACK); |
| 73 SkPaint paint; | 53 SkPaint paint; |
| 74 SkAutoTUnref<SkImageFilter> displ(SkBitmapSource::Create(fCheckerboard))
; | 54 SkAutoTUnref<SkImageFilter> displ(SkBitmapSource::Create(fCheckerboard))
; |
| 75 paint.setImageFilter(SkDisplacementMapEffect::Create( | 55 paint.setImageFilter(SkDisplacementMapEffect::Create( |
| 76 SkDisplacementMapEffect::kR_ChannelSelectorType, | 56 SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 77 SkDisplacementMapEffect::kG_ChannelSelectorType, 0.0f, displ))->unre
f(); | 57 SkDisplacementMapEffect::kG_ChannelSelectorType, 0.0f, displ))->unre
f(); |
| 78 drawClippedBitmap(canvas, 0, 0, paint); | 58 drawClippedBitmap(canvas, 0, 0, paint); |
| 79 paint.setImageFilter(SkDisplacementMapEffect::Create( | 59 paint.setImageFilter(SkDisplacementMapEffect::Create( |
| 80 SkDisplacementMapEffect::kB_ChannelSelectorType, | 60 SkDisplacementMapEffect::kB_ChannelSelectorType, |
| 81 SkDisplacementMapEffect::kA_ChannelSelectorType, 16.0f, displ))->unr
ef(); | 61 SkDisplacementMapEffect::kA_ChannelSelectorType, 16.0f, displ))->unr
ef(); |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 // and color should use the same bitmap, given to SkCanvas::drawBitmap() | 177 // and color should use the same bitmap, given to SkCanvas::drawBitmap() |
| 198 // as an input argument. | 178 // as an input argument. |
| 199 paint.setImageFilter(SkDisplacementMapEffect::Create( | 179 paint.setImageFilter(SkDisplacementMapEffect::Create( |
| 200 SkDisplacementMapEffect::kG_ChannelSelectorType, | 180 SkDisplacementMapEffect::kG_ChannelSelectorType, |
| 201 SkDisplacementMapEffect::kA_ChannelSelectorType, | 181 SkDisplacementMapEffect::kA_ChannelSelectorType, |
| 202 40.0f, nullptr))->unref(); | 182 40.0f, nullptr))->unref(); |
| 203 drawClippedBitmap(canvas, 400, 400, paint); | 183 drawClippedBitmap(canvas, 400, 400, paint); |
| 204 } | 184 } |
| 205 | 185 |
| 206 private: | 186 private: |
| 187 SkBitmap fBitmap, fCheckerboard, fSmall, fLarge, fLargeW, fLargeH; |
| 188 |
| 207 typedef GM INHERITED; | 189 typedef GM INHERITED; |
| 208 SkBitmap fBitmap, fCheckerboard, fSmall, fLarge, fLargeW, fLargeH; | |
| 209 bool fInitialized; | |
| 210 }; | 190 }; |
| 211 | 191 |
| 212 ////////////////////////////////////////////////////////////////////////////// | 192 ////////////////////////////////////////////////////////////////////////////// |
| 213 | 193 |
| 214 static GM* MyFactory(void*) { return new DisplacementMapGM; } | 194 DEF_GM(return new DisplacementMapGM;) |
| 215 static GMRegistry reg(MyFactory); | |
| 216 | |
| 217 } | 195 } |
| OLD | NEW |