Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
spritebitmap.cpp
Go to the documentation of this file.
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"
15#include "include/core/SkRect.h"
19#include "include/core/SkSize.h"
22#include "tools/ToolUtils.h"
23
24#include <utility>
25
26static void make_bm(SkBitmap* bm) {
27 bm->allocN32Pixels(100, 100);
29
30 SkCanvas canvas(*bm);
32 paint.setAntiAlias(true);
33 paint.setColor(SK_ColorRED);
34 canvas.drawCircle(50, 50, 50, paint);
35}
36
37static void draw_1_bitmap(SkCanvas* canvas, const SkBitmap& bm, bool doClip,
38 int dx, int dy, sk_sp<SkImageFilter> filter) {
39 SkAutoCanvasRestore acr(canvas, true);
41
43 SkIntToScalar(dy),
44 SkIntToScalar(bm.width()),
45 SkIntToScalar(bm.height()));
46
47 paint.setImageFilter(std::move(filter));
48 clipR.inset(5, 5);
49
50 canvas->translate(SkIntToScalar(bm.width() + 20), 0);
51
52 if (doClip) {
53 canvas->save();
54 canvas->clipRect(clipR);
55 }
56 canvas->drawImage(bm.asImage(), SkIntToScalar(dx), SkIntToScalar(dy),
58 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:
69
70protected:
71 SkString getName() const override { return SkString("spritebitmap"); }
72
73 SkISize getISize() override { return SkISize::Make(640, 480); }
74
75 void onDraw(SkCanvas* canvas) override {
76 SkBitmap bm;
77 make_bm(&bm);
78
79 int dx = 10;
80 int dy = 10;
81
82 SkScalar sigma = 8;
83 sk_sp<SkImageFilter> filter(SkImageFilters::Blur(sigma, sigma, nullptr));
84
85 draw_1_bitmap(canvas, bm, false, dx, dy, nullptr);
86 dy += bm.height() + 20;
87 draw_1_bitmap(canvas, bm, false, dx, dy, filter);
88 dy += bm.height() + 20;
89 draw_1_bitmap(canvas, bm, true, dx, dy, nullptr);
90 dy += bm.height() + 20;
91 draw_1_bitmap(canvas, bm, true, dx, dy, filter);
92 }
93
94private:
95 using INHERITED = GM;
96};
97DEF_GM( return new SpriteBitmapGM; )
98
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) {
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);
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}
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
#define SkIntToScalar(x)
Definition SkScalar.h:57
static sk_sp< SkImage > make_bm()
sk_sp< SkImage > asImage() const
Definition SkBitmap.cpp:645
int width() const
Definition SkBitmap.h:149
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition SkBitmap.cpp:232
int height() const
Definition SkBitmap.h:158
void eraseColor(SkColor4f) const
Definition SkBitmap.cpp:442
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
int save()
Definition SkCanvas.cpp:451
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition SkCanvas.h:1528
void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint &paint)
static sk_sp< SkImageFilter > Blur(SkScalar sigmaX, SkScalar sigmaY, SkTileMode tileMode, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition SkImage.cpp:179
static SkMatrix Translate(SkScalar dx, SkScalar dy)
Definition SkMatrix.h:91
SkString getName() const override
void onDraw(SkCanvas *canvas) override
SkISize getISize() override
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition gm.cpp:81
const Paint & paint
sk_sp< SkImage > image
Definition examples.cpp:29
float SkScalar
Definition extension.cpp:12
#define DEF_SIMPLE_GM_BG(NAME, CANVAS, W, H, BGCOLOR)
Definition gm.h:52
#define DEF_GM(CODE)
Definition gm.h:40
sk_sp< SkImage > create_checkerboard_image(int w, int h, SkColor c1, SkColor c2, int checkSize)
static void draw_1_bitmap(SkCanvas *canvas, const SkBitmap &bm, bool doClip, int dx, int dy, sk_sp< SkImageFilter > filter)
Point offset
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
void inset(float dx, float dy)
Definition SkRect.h:1060
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659