reed@google.com | 12fa9ba | 2013-01-16 18:54:15 +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 "SkCanvas.h" |
| 10 | #include "SkBlurImageFilter.h" |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 11 | #include "SkRSXform.h" |
| 12 | #include "SkSurface.h" |
reed@google.com | 12fa9ba | 2013-01-16 18:54:15 +0000 | [diff] [blame] | 13 | |
| 14 | static void make_bm(SkBitmap* bm) { |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 15 | bm->allocN32Pixels(100, 100); |
reed@google.com | 12fa9ba | 2013-01-16 18:54:15 +0000 | [diff] [blame] | 16 | bm->eraseColor(SK_ColorBLUE); |
| 17 | |
| 18 | SkCanvas canvas(*bm); |
| 19 | SkPaint paint; |
| 20 | paint.setAntiAlias(true); |
| 21 | paint.setColor(SK_ColorRED); |
| 22 | canvas.drawCircle(50, 50, 50, paint); |
| 23 | } |
| 24 | |
reed | da420b9 | 2015-12-16 08:38:15 -0800 | [diff] [blame] | 25 | static void draw_1_bitmap(SkCanvas* canvas, const SkBitmap& bm, bool doClip, |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 26 | int dx, int dy, sk_sp<SkImageFilter> filter) { |
reed@google.com | 12fa9ba | 2013-01-16 18:54:15 +0000 | [diff] [blame] | 27 | SkAutoCanvasRestore acr(canvas, true); |
| 28 | SkPaint paint; |
| 29 | |
skia.committer@gmail.com | 4d28d98 | 2013-01-17 07:06:06 +0000 | [diff] [blame] | 30 | SkRect clipR = SkRect::MakeXYWH(SkIntToScalar(dx), |
robertphillips@google.com | 914a2f2 | 2013-01-16 19:57:02 +0000 | [diff] [blame] | 31 | SkIntToScalar(dy), |
reed@google.com | 1c711ca | 2013-01-16 19:24:15 +0000 | [diff] [blame] | 32 | SkIntToScalar(bm.width()), |
| 33 | SkIntToScalar(bm.height())); |
reed@google.com | 12fa9ba | 2013-01-16 18:54:15 +0000 | [diff] [blame] | 34 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 35 | paint.setImageFilter(std::move(filter)); |
reed@google.com | 12fa9ba | 2013-01-16 18:54:15 +0000 | [diff] [blame] | 36 | clipR.inset(5, 5); |
| 37 | |
reed@google.com | 1c711ca | 2013-01-16 19:24:15 +0000 | [diff] [blame] | 38 | canvas->translate(SkIntToScalar(bm.width() + 20), 0); |
reed@google.com | 12fa9ba | 2013-01-16 18:54:15 +0000 | [diff] [blame] | 39 | |
| 40 | if (doClip) { |
| 41 | canvas->save(); |
| 42 | canvas->clipRect(clipR); |
| 43 | } |
reed@google.com | 1c711ca | 2013-01-16 19:24:15 +0000 | [diff] [blame] | 44 | canvas->drawBitmap(bm, SkIntToScalar(dx), SkIntToScalar(dy), &paint); |
reed@google.com | 12fa9ba | 2013-01-16 18:54:15 +0000 | [diff] [blame] | 45 | if (doClip) { |
| 46 | canvas->restore(); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Compare output of drawSprite and drawBitmap (esp. clipping and imagefilters) |
| 52 | */ |
| 53 | class SpriteBitmapGM : public skiagm::GM { |
| 54 | public: |
| 55 | SpriteBitmapGM() {} |
| 56 | |
| 57 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 58 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 59 | SkString onShortName() override { |
reed@google.com | 12fa9ba | 2013-01-16 18:54:15 +0000 | [diff] [blame] | 60 | return SkString("spritebitmap"); |
| 61 | } |
| 62 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 63 | SkISize onISize() override { |
reed@google.com | 12fa9ba | 2013-01-16 18:54:15 +0000 | [diff] [blame] | 64 | return SkISize::Make(640, 480); |
| 65 | } |
| 66 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 67 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | 12fa9ba | 2013-01-16 18:54:15 +0000 | [diff] [blame] | 68 | SkBitmap bm; |
| 69 | make_bm(&bm); |
| 70 | |
| 71 | int dx = 10; |
| 72 | int dy = 10; |
| 73 | |
| 74 | SkScalar sigma = 8; |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 75 | sk_sp<SkImageFilter> filter(SkBlurImageFilter::Make(sigma, sigma, nullptr)); |
reed@google.com | 12fa9ba | 2013-01-16 18:54:15 +0000 | [diff] [blame] | 76 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 77 | draw_1_bitmap(canvas, bm, false, dx, dy, nullptr); |
reed@google.com | 12fa9ba | 2013-01-16 18:54:15 +0000 | [diff] [blame] | 78 | dy += bm.height() + 20; |
reed | da420b9 | 2015-12-16 08:38:15 -0800 | [diff] [blame] | 79 | draw_1_bitmap(canvas, bm, false, dx, dy, filter); |
reed@google.com | 12fa9ba | 2013-01-16 18:54:15 +0000 | [diff] [blame] | 80 | dy += bm.height() + 20; |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 81 | draw_1_bitmap(canvas, bm, true, dx, dy, nullptr); |
reed@google.com | 12fa9ba | 2013-01-16 18:54:15 +0000 | [diff] [blame] | 82 | dy += bm.height() + 20; |
reed | da420b9 | 2015-12-16 08:38:15 -0800 | [diff] [blame] | 83 | draw_1_bitmap(canvas, bm, true, dx, dy, filter); |
reed@google.com | 12fa9ba | 2013-01-16 18:54:15 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | private: |
| 87 | typedef GM INHERITED; |
| 88 | }; |
reed@google.com | 12fa9ba | 2013-01-16 18:54:15 +0000 | [diff] [blame] | 89 | DEF_GM( return new SpriteBitmapGM; ) |