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