blob: 2f636a2aa4b60468e0425ea66e32d186cd6ed43b [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 }
Mike Reed568f0ae2021-01-24 08:57:23 -050053 canvas->drawImage(bm.asImage(), SkIntToScalar(dx), SkIntToScalar(dy),
54 SkSamplingOptions(), &paint);
reed@google.com12fa9ba2013-01-16 18:54:15 +000055 if (doClip) {
56 canvas->restore();
57 }
58}
59
60/**
61 * Compare output of drawSprite and drawBitmap (esp. clipping and imagefilters)
62 */
63class SpriteBitmapGM : public skiagm::GM {
64public:
65 SpriteBitmapGM() {}
66
67protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000068
mtklein36352bf2015-03-25 18:17:31 -070069 SkString onShortName() override {
reed@google.com12fa9ba2013-01-16 18:54:15 +000070 return SkString("spritebitmap");
71 }
72
mtklein36352bf2015-03-25 18:17:31 -070073 SkISize onISize() override {
reed@google.com12fa9ba2013-01-16 18:54:15 +000074 return SkISize::Make(640, 480);
75 }
76
mtklein36352bf2015-03-25 18:17:31 -070077 void onDraw(SkCanvas* canvas) override {
reed@google.com12fa9ba2013-01-16 18:54:15 +000078 SkBitmap bm;
79 make_bm(&bm);
80
81 int dx = 10;
82 int dy = 10;
83
84 SkScalar sigma = 8;
Michael Ludwig898bbfa2019-08-02 15:21:23 -040085 sk_sp<SkImageFilter> filter(SkImageFilters::Blur(sigma, sigma, nullptr));
reed@google.com12fa9ba2013-01-16 18:54:15 +000086
robertphillips6e7025a2016-04-04 04:31:25 -070087 draw_1_bitmap(canvas, bm, false, dx, dy, nullptr);
reed@google.com12fa9ba2013-01-16 18:54:15 +000088 dy += bm.height() + 20;
reedda420b92015-12-16 08:38:15 -080089 draw_1_bitmap(canvas, bm, false, dx, dy, filter);
reed@google.com12fa9ba2013-01-16 18:54:15 +000090 dy += bm.height() + 20;
robertphillips6e7025a2016-04-04 04:31:25 -070091 draw_1_bitmap(canvas, bm, true, dx, dy, nullptr);
reed@google.com12fa9ba2013-01-16 18:54:15 +000092 dy += bm.height() + 20;
reedda420b92015-12-16 08:38:15 -080093 draw_1_bitmap(canvas, bm, true, dx, dy, filter);
reed@google.com12fa9ba2013-01-16 18:54:15 +000094 }
95
96private:
John Stiles7571f9e2020-09-02 22:42:33 -040097 using INHERITED = GM;
reed@google.com12fa9ba2013-01-16 18:54:15 +000098};
reed@google.com12fa9ba2013-01-16 18:54:15 +000099DEF_GM( return new SpriteBitmapGM; )