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->drawImage(bm.asImage(), SkIntToScalar(dx), SkIntToScalar(dy),
54 SkSamplingOptions(), &paint);
55 if (doClip) {
56 canvas->restore();
57 }
58 }
59
60 /**
61 * Compare output of drawSprite and drawBitmap (esp. clipping and imagefilters)
62 */
63 class SpriteBitmapGM : public skiagm::GM {
64 public:
SpriteBitmapGM()65 SpriteBitmapGM() {}
66
67 protected:
68
onShortName()69 SkString onShortName() override {
70 return SkString("spritebitmap");
71 }
72
onISize()73 SkISize onISize() override {
74 return SkISize::Make(640, 480);
75 }
76
onDraw(SkCanvas * canvas)77 void onDraw(SkCanvas* canvas) override {
78 SkBitmap bm;
79 make_bm(&bm);
80
81 int dx = 10;
82 int dy = 10;
83
84 SkScalar sigma = 8;
85 sk_sp<SkImageFilter> filter(SkImageFilters::Blur(sigma, sigma, nullptr));
86
87 draw_1_bitmap(canvas, bm, false, dx, dy, nullptr);
88 dy += bm.height() + 20;
89 draw_1_bitmap(canvas, bm, false, dx, dy, filter);
90 dy += bm.height() + 20;
91 draw_1_bitmap(canvas, bm, true, dx, dy, nullptr);
92 dy += bm.height() + 20;
93 draw_1_bitmap(canvas, bm, true, dx, dy, filter);
94 }
95
96 private:
97 using INHERITED = GM;
98 };
99 DEF_GM( return new SpriteBitmapGM; )
100