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/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
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"
19 #include "include/effects/SkImageFilters.h"
20
21 #include <utility>
22
make_bm(SkBitmap * bm)23 static void make_bm(SkBitmap* bm) {
24 bm->allocN32Pixels(100, 100);
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
draw_1_bitmap(SkCanvas * canvas,const SkBitmap & bm,bool doClip,int dx,int dy,sk_sp<SkImageFilter> filter)34 static void draw_1_bitmap(SkCanvas* canvas, const SkBitmap& bm, bool doClip,
35 int dx, int dy, sk_sp<SkImageFilter> filter) {
36 SkAutoCanvasRestore acr(canvas, true);
37 SkPaint paint;
38
39 SkRect clipR = SkRect::MakeXYWH(SkIntToScalar(dx),
40 SkIntToScalar(dy),
41 SkIntToScalar(bm.width()),
42 SkIntToScalar(bm.height()));
43
44 paint.setImageFilter(std::move(filter));
45 clipR.inset(5, 5);
46
47 canvas->translate(SkIntToScalar(bm.width() + 20), 0);
48
49 if (doClip) {
50 canvas->save();
51 canvas->clipRect(clipR);
52 }
53 canvas->drawBitmap(bm, SkIntToScalar(dx), SkIntToScalar(dy), &paint);
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:
SpriteBitmapGM()64 SpriteBitmapGM() {}
65
66 protected:
67
onShortName()68 SkString onShortName() override {
69 return SkString("spritebitmap");
70 }
71
onISize()72 SkISize onISize() override {
73 return SkISize::Make(640, 480);
74 }
75
onDraw(SkCanvas * canvas)76 void onDraw(SkCanvas* canvas) override {
77 SkBitmap bm;
78 make_bm(&bm);
79
80 int dx = 10;
81 int dy = 10;
82
83 SkScalar sigma = 8;
84 sk_sp<SkImageFilter> filter(SkImageFilters::Blur(sigma, sigma, nullptr));
85
86 draw_1_bitmap(canvas, bm, false, dx, dy, nullptr);
87 dy += bm.height() + 20;
88 draw_1_bitmap(canvas, bm, false, dx, dy, filter);
89 dy += bm.height() + 20;
90 draw_1_bitmap(canvas, bm, true, dx, dy, nullptr);
91 dy += bm.height() + 20;
92 draw_1_bitmap(canvas, bm, true, dx, dy, filter);
93 }
94
95 private:
96 typedef GM INHERITED;
97 };
98 DEF_GM( return new SpriteBitmapGM; )
99