blob: d0a0150b60de2c08c6818483397668dcfe60279a [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"
Brian Osman01d95662024-02-28 11:54:05 -050013#include "include/core/SkMatrix.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040014#include "include/core/SkPaint.h"
15#include "include/core/SkRect.h"
16#include "include/core/SkRefCnt.h"
Brian Osman01d95662024-02-28 11:54:05 -050017#include "include/core/SkSamplingOptions.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040018#include "include/core/SkScalar.h"
19#include "include/core/SkSize.h"
20#include "include/core/SkString.h"
Michael Ludwig898bbfa2019-08-02 15:21:23 -040021#include "include/effects/SkImageFilters.h"
Brian Osman01d95662024-02-28 11:54:05 -050022#include "tools/ToolUtils.h"
reed@google.com12fa9ba2013-01-16 18:54:15 +000023
Ben Wagner7fde8e12019-05-01 17:28:53 -040024#include <utility>
25
reed@google.com12fa9ba2013-01-16 18:54:15 +000026static void make_bm(SkBitmap* bm) {
reed@google.comeb9a46c2014-01-25 16:46:20 +000027 bm->allocN32Pixels(100, 100);
reed@google.com12fa9ba2013-01-16 18:54:15 +000028 bm->eraseColor(SK_ColorBLUE);
29
30 SkCanvas canvas(*bm);
31 SkPaint paint;
32 paint.setAntiAlias(true);
33 paint.setColor(SK_ColorRED);
34 canvas.drawCircle(50, 50, 50, paint);
35}
36
reedda420b92015-12-16 08:38:15 -080037static void draw_1_bitmap(SkCanvas* canvas, const SkBitmap& bm, bool doClip,
robertphillips6e7025a2016-04-04 04:31:25 -070038 int dx, int dy, sk_sp<SkImageFilter> filter) {
reed@google.com12fa9ba2013-01-16 18:54:15 +000039 SkAutoCanvasRestore acr(canvas, true);
40 SkPaint paint;
41
skia.committer@gmail.com4d28d982013-01-17 07:06:06 +000042 SkRect clipR = SkRect::MakeXYWH(SkIntToScalar(dx),
robertphillips@google.com914a2f22013-01-16 19:57:02 +000043 SkIntToScalar(dy),
reed@google.com1c711ca2013-01-16 19:24:15 +000044 SkIntToScalar(bm.width()),
45 SkIntToScalar(bm.height()));
reed@google.com12fa9ba2013-01-16 18:54:15 +000046
robertphillips6e7025a2016-04-04 04:31:25 -070047 paint.setImageFilter(std::move(filter));
reed@google.com12fa9ba2013-01-16 18:54:15 +000048 clipR.inset(5, 5);
49
reed@google.com1c711ca2013-01-16 19:24:15 +000050 canvas->translate(SkIntToScalar(bm.width() + 20), 0);
reed@google.com12fa9ba2013-01-16 18:54:15 +000051
52 if (doClip) {
53 canvas->save();
54 canvas->clipRect(clipR);
55 }
Mike Reed568f0ae2021-01-24 08:57:23 -050056 canvas->drawImage(bm.asImage(), SkIntToScalar(dx), SkIntToScalar(dy),
57 SkSamplingOptions(), &paint);
reed@google.com12fa9ba2013-01-16 18:54:15 +000058 if (doClip) {
59 canvas->restore();
60 }
61}
62
63/**
64 * Compare output of drawSprite and drawBitmap (esp. clipping and imagefilters)
65 */
66class SpriteBitmapGM : public skiagm::GM {
67public:
68 SpriteBitmapGM() {}
69
70protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000071 SkString getName() const override { return SkString("spritebitmap"); }
reed@google.com12fa9ba2013-01-16 18:54:15 +000072
Leandro Lovisolo8f023882023-08-15 21:13:52 +000073 SkISize getISize() override { return SkISize::Make(640, 480); }
reed@google.com12fa9ba2013-01-16 18:54:15 +000074
mtklein36352bf2015-03-25 18:17:31 -070075 void onDraw(SkCanvas* canvas) override {
reed@google.com12fa9ba2013-01-16 18:54:15 +000076 SkBitmap bm;
77 make_bm(&bm);
78
79 int dx = 10;
80 int dy = 10;
81
82 SkScalar sigma = 8;
Michael Ludwig898bbfa2019-08-02 15:21:23 -040083 sk_sp<SkImageFilter> filter(SkImageFilters::Blur(sigma, sigma, nullptr));
reed@google.com12fa9ba2013-01-16 18:54:15 +000084
robertphillips6e7025a2016-04-04 04:31:25 -070085 draw_1_bitmap(canvas, bm, false, dx, dy, nullptr);
reed@google.com12fa9ba2013-01-16 18:54:15 +000086 dy += bm.height() + 20;
reedda420b92015-12-16 08:38:15 -080087 draw_1_bitmap(canvas, bm, false, dx, dy, filter);
reed@google.com12fa9ba2013-01-16 18:54:15 +000088 dy += bm.height() + 20;
robertphillips6e7025a2016-04-04 04:31:25 -070089 draw_1_bitmap(canvas, bm, true, dx, dy, nullptr);
reed@google.com12fa9ba2013-01-16 18:54:15 +000090 dy += bm.height() + 20;
reedda420b92015-12-16 08:38:15 -080091 draw_1_bitmap(canvas, bm, true, dx, dy, filter);
reed@google.com12fa9ba2013-01-16 18:54:15 +000092 }
93
94private:
John Stiles7571f9e2020-09-02 22:42:33 -040095 using INHERITED = GM;
reed@google.com12fa9ba2013-01-16 18:54:15 +000096};
reed@google.com12fa9ba2013-01-16 18:54:15 +000097DEF_GM( return new SpriteBitmapGM; )
Brian Osman01d95662024-02-28 11:54:05 -050098
99// b/41322892 : The CPU backend tries to detect when an image draw is landing perfectly on pixel
100// centers, so it can use a faster sprite-blitting path. That code just assumes that ANY translation
101// can be pixel-snapped. Image-shaders used to behave like this, but were fixed long-ago.
102// The correct result here is for ALL rectangles to be averaged to grey, rather than a pixel-snapped
103// black and white checkerboard.
104//
105// This GM now tests that linear filtering is preserved for subpixel translation, across several
106// different possible methods of drawing.
107DEF_SIMPLE_GM_BG(drawimagerect_filter, canvas, 180, 60, SK_ColorWHITE) {
108 auto image = ToolUtils::create_checkerboard_image(50, 50, SK_ColorWHITE, SK_ColorBLACK, 1);
109 SkSamplingOptions sampling{SkFilterMode::kLinear};
110
111 canvas->translate(5, 5);
112 canvas->drawImage(image, 0.5f, 0.5f, sampling);
113
114 canvas->translate(60, 0);
115 canvas->drawImageRect(image, {0.5f, 0.5f, 50.5f, 50.5f}, sampling);
116
117 auto shader = image->makeShader(sampling);
118 canvas->translate(60, 0);
119 SkPaint paint;
120 SkMatrix offset = SkMatrix::Translate(0.5f, 0.5f);
121 paint.setShader(image->makeShader(sampling, &offset));
122 canvas->drawRect({0.0f, 0.0f, 50.0f, 50.0f}, paint);
123}