blob: 38fe9f43021113d8176443b4f9c4cff74e2b0a7c [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"
reed71c3c762015-06-24 10:29:17 -070011#include "SkRSXform.h"
12#include "SkSurface.h"
reed@google.com12fa9ba2013-01-16 18:54:15 +000013
14static void make_bm(SkBitmap* bm) {
reed@google.comeb9a46c2014-01-25 16:46:20 +000015 bm->allocN32Pixels(100, 100);
reed@google.com12fa9ba2013-01-16 18:54:15 +000016 bm->eraseColor(SK_ColorBLUE);
17
18 SkCanvas canvas(*bm);
19 SkPaint paint;
20 paint.setAntiAlias(true);
21 paint.setColor(SK_ColorRED);
22 canvas.drawCircle(50, 50, 50, paint);
23}
24
reedda420b92015-12-16 08:38:15 -080025static void draw_1_bitmap(SkCanvas* canvas, const SkBitmap& bm, bool doClip,
robertphillips6e7025a2016-04-04 04:31:25 -070026 int dx, int dy, sk_sp<SkImageFilter> filter) {
reed@google.com12fa9ba2013-01-16 18:54:15 +000027 SkAutoCanvasRestore acr(canvas, true);
28 SkPaint paint;
29
skia.committer@gmail.com4d28d982013-01-17 07:06:06 +000030 SkRect clipR = SkRect::MakeXYWH(SkIntToScalar(dx),
robertphillips@google.com914a2f22013-01-16 19:57:02 +000031 SkIntToScalar(dy),
reed@google.com1c711ca2013-01-16 19:24:15 +000032 SkIntToScalar(bm.width()),
33 SkIntToScalar(bm.height()));
reed@google.com12fa9ba2013-01-16 18:54:15 +000034
robertphillips6e7025a2016-04-04 04:31:25 -070035 paint.setImageFilter(std::move(filter));
reed@google.com12fa9ba2013-01-16 18:54:15 +000036 clipR.inset(5, 5);
37
reed@google.com1c711ca2013-01-16 19:24:15 +000038 canvas->translate(SkIntToScalar(bm.width() + 20), 0);
reed@google.com12fa9ba2013-01-16 18:54:15 +000039
40 if (doClip) {
41 canvas->save();
42 canvas->clipRect(clipR);
43 }
reed@google.com1c711ca2013-01-16 19:24:15 +000044 canvas->drawBitmap(bm, SkIntToScalar(dx), SkIntToScalar(dy), &paint);
reed@google.com12fa9ba2013-01-16 18:54:15 +000045 if (doClip) {
46 canvas->restore();
47 }
48}
49
50/**
51 * Compare output of drawSprite and drawBitmap (esp. clipping and imagefilters)
52 */
53class SpriteBitmapGM : public skiagm::GM {
54public:
55 SpriteBitmapGM() {}
56
57protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000058
mtklein36352bf2015-03-25 18:17:31 -070059 SkString onShortName() override {
reed@google.com12fa9ba2013-01-16 18:54:15 +000060 return SkString("spritebitmap");
61 }
62
mtklein36352bf2015-03-25 18:17:31 -070063 SkISize onISize() override {
reed@google.com12fa9ba2013-01-16 18:54:15 +000064 return SkISize::Make(640, 480);
65 }
66
mtklein36352bf2015-03-25 18:17:31 -070067 void onDraw(SkCanvas* canvas) override {
reed@google.com12fa9ba2013-01-16 18:54:15 +000068 SkBitmap bm;
69 make_bm(&bm);
70
71 int dx = 10;
72 int dy = 10;
73
74 SkScalar sigma = 8;
robertphillips6e7025a2016-04-04 04:31:25 -070075 sk_sp<SkImageFilter> filter(SkBlurImageFilter::Make(sigma, sigma, nullptr));
reed@google.com12fa9ba2013-01-16 18:54:15 +000076
robertphillips6e7025a2016-04-04 04:31:25 -070077 draw_1_bitmap(canvas, bm, false, dx, dy, nullptr);
reed@google.com12fa9ba2013-01-16 18:54:15 +000078 dy += bm.height() + 20;
reedda420b92015-12-16 08:38:15 -080079 draw_1_bitmap(canvas, bm, false, dx, dy, filter);
reed@google.com12fa9ba2013-01-16 18:54:15 +000080 dy += bm.height() + 20;
robertphillips6e7025a2016-04-04 04:31:25 -070081 draw_1_bitmap(canvas, bm, true, dx, dy, nullptr);
reed@google.com12fa9ba2013-01-16 18:54:15 +000082 dy += bm.height() + 20;
reedda420b92015-12-16 08:38:15 -080083 draw_1_bitmap(canvas, bm, true, dx, dy, filter);
reed@google.com12fa9ba2013-01-16 18:54:15 +000084 }
85
86private:
87 typedef GM INHERITED;
88};
reed@google.com12fa9ba2013-01-16 18:54:15 +000089DEF_GM( return new SpriteBitmapGM; )