blob: 8d76843e818b2611551dc7270d1abe5af2d0e4f9 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBitmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#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"
Michael Ludwig898bbfa2019-08-02 15:21:23 -040019#include "include/effects/SkImageFilters.h"
reed@google.com12fa9ba2013-01-16 18:54:15 +000020
Ben Wagner7fde8e12019-05-01 17:28:53 -040021#include <utility>
22
reed@google.com12fa9ba2013-01-16 18:54:15 +000023static void make_bm(SkBitmap* bm) {
reed@google.comeb9a46c2014-01-25 16:46:20 +000024 bm->allocN32Pixels(100, 100);
reed@google.com12fa9ba2013-01-16 18:54:15 +000025 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
reedda420b92015-12-16 08:38:15 -080034static void draw_1_bitmap(SkCanvas* canvas, const SkBitmap& bm, bool doClip,
robertphillips6e7025a2016-04-04 04:31:25 -070035 int dx, int dy, sk_sp<SkImageFilter> filter) {
reed@google.com12fa9ba2013-01-16 18:54:15 +000036 SkAutoCanvasRestore acr(canvas, true);
37 SkPaint paint;
38
skia.committer@gmail.com4d28d982013-01-17 07:06:06 +000039 SkRect clipR = SkRect::MakeXYWH(SkIntToScalar(dx),
robertphillips@google.com914a2f22013-01-16 19:57:02 +000040 SkIntToScalar(dy),
reed@google.com1c711ca2013-01-16 19:24:15 +000041 SkIntToScalar(bm.width()),
42 SkIntToScalar(bm.height()));
reed@google.com12fa9ba2013-01-16 18:54:15 +000043
robertphillips6e7025a2016-04-04 04:31:25 -070044 paint.setImageFilter(std::move(filter));
reed@google.com12fa9ba2013-01-16 18:54:15 +000045 clipR.inset(5, 5);
46
reed@google.com1c711ca2013-01-16 19:24:15 +000047 canvas->translate(SkIntToScalar(bm.width() + 20), 0);
reed@google.com12fa9ba2013-01-16 18:54:15 +000048
49 if (doClip) {
50 canvas->save();
51 canvas->clipRect(clipR);
52 }
reed@google.com1c711ca2013-01-16 19:24:15 +000053 canvas->drawBitmap(bm, SkIntToScalar(dx), SkIntToScalar(dy), &paint);
reed@google.com12fa9ba2013-01-16 18:54:15 +000054 if (doClip) {
55 canvas->restore();
56 }
57}
58
59/**
60 * Compare output of drawSprite and drawBitmap (esp. clipping and imagefilters)
61 */
62class SpriteBitmapGM : public skiagm::GM {
63public:
64 SpriteBitmapGM() {}
65
66protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000067
mtklein36352bf2015-03-25 18:17:31 -070068 SkString onShortName() override {
reed@google.com12fa9ba2013-01-16 18:54:15 +000069 return SkString("spritebitmap");
70 }
71
mtklein36352bf2015-03-25 18:17:31 -070072 SkISize onISize() override {
reed@google.com12fa9ba2013-01-16 18:54:15 +000073 return SkISize::Make(640, 480);
74 }
75
mtklein36352bf2015-03-25 18:17:31 -070076 void onDraw(SkCanvas* canvas) override {
reed@google.com12fa9ba2013-01-16 18:54:15 +000077 SkBitmap bm;
78 make_bm(&bm);
79
80 int dx = 10;
81 int dy = 10;
82
83 SkScalar sigma = 8;
Michael Ludwig898bbfa2019-08-02 15:21:23 -040084 sk_sp<SkImageFilter> filter(SkImageFilters::Blur(sigma, sigma, nullptr));
reed@google.com12fa9ba2013-01-16 18:54:15 +000085
robertphillips6e7025a2016-04-04 04:31:25 -070086 draw_1_bitmap(canvas, bm, false, dx, dy, nullptr);
reed@google.com12fa9ba2013-01-16 18:54:15 +000087 dy += bm.height() + 20;
reedda420b92015-12-16 08:38:15 -080088 draw_1_bitmap(canvas, bm, false, dx, dy, filter);
reed@google.com12fa9ba2013-01-16 18:54:15 +000089 dy += bm.height() + 20;
robertphillips6e7025a2016-04-04 04:31:25 -070090 draw_1_bitmap(canvas, bm, true, dx, dy, nullptr);
reed@google.com12fa9ba2013-01-16 18:54:15 +000091 dy += bm.height() + 20;
reedda420b92015-12-16 08:38:15 -080092 draw_1_bitmap(canvas, bm, true, dx, dy, filter);
reed@google.com12fa9ba2013-01-16 18:54:15 +000093 }
94
95private:
96 typedef GM INHERITED;
97};
reed@google.com12fa9ba2013-01-16 18:54:15 +000098DEF_GM( return new SpriteBitmapGM; )