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