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