blob: d55d71315e3e935962d7bf0f152175e3e7fe20da [file] [log] [blame]
reed@google.com12fa9ba2013-01-16 18:54:15 +00001/*
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
12static 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
24static 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
29 SkRect clipR = SkRect::MakeXYWH(dx, dy, bm.width(), bm.height());
30
31 paint.setImageFilter(filter);
32 clipR.inset(5, 5);
33
34 if (doClip) {
35 canvas->save();
36 canvas->clipRect(clipR);
37 }
38 canvas->drawSprite(bm, dx, dy, &paint);
39 if (doClip) {
40 canvas->restore();
41 }
42
43 canvas->translate(bm.width() + 20, 0);
44
45 if (doClip) {
46 canvas->save();
47 canvas->clipRect(clipR);
48 }
49 canvas->drawBitmap(bm, dx, dy, &paint);
50 if (doClip) {
51 canvas->restore();
52 }
53}
54
55/**
56 * Compare output of drawSprite and drawBitmap (esp. clipping and imagefilters)
57 */
58class SpriteBitmapGM : public skiagm::GM {
59public:
60 SpriteBitmapGM() {}
61
62protected:
63 virtual SkString onShortName() {
64 return SkString("spritebitmap");
65 }
66
67 virtual SkISize onISize() {
68 return SkISize::Make(640, 480);
69 }
70
71 virtual void onDraw(SkCanvas* canvas) {
72 SkBitmap bm;
73 make_bm(&bm);
74
75 int dx = 10;
76 int dy = 10;
77
78 SkScalar sigma = 8;
79 SkAutoTUnref<SkImageFilter> filter(new SkBlurImageFilter(sigma, sigma));
80
81 draw_2_bitmaps(canvas, bm, false, dx, dy);
82 dy += bm.height() + 20;
83 draw_2_bitmaps(canvas, bm, false, dx, dy, filter);
84 dy += bm.height() + 20;
85 draw_2_bitmaps(canvas, bm, true, dx, dy);
86 dy += bm.height() + 20;
87 draw_2_bitmaps(canvas, bm, true, dx, dy, filter);
88 }
89
90private:
91 typedef GM INHERITED;
92};
93
94//////////////////////////////////////////////////////////////////////////////
95
96DEF_GM( return new SpriteBitmapGM; )