blob: a387dd3a9a85ecf1a3de96d7b99314e9401f4a51 [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) {
reed@google.comeb9a46c2014-01-25 16:46:20 +000013 bm->allocN32Pixels(100, 100);
reed@google.com12fa9ba2013-01-16 18:54:15 +000014 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
23static 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.com4d28d982013-01-17 07:06:06 +000028 SkRect clipR = SkRect::MakeXYWH(SkIntToScalar(dx),
robertphillips@google.com914a2f22013-01-16 19:57:02 +000029 SkIntToScalar(dy),
reed@google.com1c711ca2013-01-16 19:24:15 +000030 SkIntToScalar(bm.width()),
31 SkIntToScalar(bm.height()));
reed@google.com12fa9ba2013-01-16 18:54:15 +000032
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.com1c711ca2013-01-16 19:24:15 +000045 canvas->translate(SkIntToScalar(bm.width() + 20), 0);
reed@google.com12fa9ba2013-01-16 18:54:15 +000046
47 if (doClip) {
48 canvas->save();
49 canvas->clipRect(clipR);
50 }
reed@google.com1c711ca2013-01-16 19:24:15 +000051 canvas->drawBitmap(bm, SkIntToScalar(dx), SkIntToScalar(dy), &paint);
reed@google.com12fa9ba2013-01-16 18:54:15 +000052 if (doClip) {
53 canvas->restore();
54 }
55}
56
57/**
58 * Compare output of drawSprite and drawBitmap (esp. clipping and imagefilters)
59 */
60class SpriteBitmapGM : public skiagm::GM {
61public:
62 SpriteBitmapGM() {}
63
64protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000065 virtual uint32_t onGetFlags() const SK_OVERRIDE {
66 return kSkipTiled_Flag;
67 }
68
reed@google.com12fa9ba2013-01-16 18:54:15 +000069 virtual SkString onShortName() {
70 return SkString("spritebitmap");
71 }
72
73 virtual SkISize onISize() {
74 return SkISize::Make(640, 480);
75 }
76
77 virtual void onDraw(SkCanvas* canvas) {
78 SkBitmap bm;
79 make_bm(&bm);
80
81 int dx = 10;
82 int dy = 10;
83
84 SkScalar sigma = 8;
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000085 SkAutoTUnref<SkImageFilter> filter(SkBlurImageFilter::Create(sigma, sigma));
reed@google.com12fa9ba2013-01-16 18:54:15 +000086
87 draw_2_bitmaps(canvas, bm, false, dx, dy);
88 dy += bm.height() + 20;
89 draw_2_bitmaps(canvas, bm, false, dx, dy, filter);
90 dy += bm.height() + 20;
91 draw_2_bitmaps(canvas, bm, true, dx, dy);
92 dy += bm.height() + 20;
93 draw_2_bitmaps(canvas, bm, true, dx, dy, filter);
94 }
95
96private:
97 typedef GM INHERITED;
98};
99
100//////////////////////////////////////////////////////////////////////////////
101
102DEF_GM( return new SpriteBitmapGM; )