aboutsummaryrefslogtreecommitdiffhomepage
path: root/gm/bmpfilterqualityrepeat.cpp
blob: 1ddc72635c4d82e83681d0c25089b0cdc32e7a5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "gm.h"

#include "SkShader.h"

// Inspired by svg/as-border-image/svg-as-border-image.html. Draws a four-color checker board bitmap
// such that it is stretched and repeat tiled with different filter qualities. It is testing whether
// the bmp filter respects the repeat mode at the tile seams.
class BmpFilterQualityRepeat : public skiagm::GM {
public:
    BmpFilterQualityRepeat() { this->setBGColor(sk_tool_utils::color_to_565(0xFFCCBBAA)); }

protected:

    void onOnceBeforeDraw() override {
        fBmp.allocN32Pixels(40, 40, true);
        SkCanvas canvas(fBmp);
        SkBitmap colorBmp;
        colorBmp.allocN32Pixels(20, 20, true);
        colorBmp.eraseColor(0xFFFF0000);
        canvas.drawBitmap(colorBmp, 0, 0);
        colorBmp.eraseColor(sk_tool_utils::color_to_565(0xFF008200));
        canvas.drawBitmap(colorBmp, 20, 0);
        colorBmp.eraseColor(sk_tool_utils::color_to_565(0xFFFF9000));
        canvas.drawBitmap(colorBmp, 0, 20);
        colorBmp.eraseColor(sk_tool_utils::color_to_565(0xFF2000FF));
        canvas.drawBitmap(colorBmp, 20, 20);
    }

    SkString onShortName() override { return SkString("bmp_filter_quality_repeat"); }

    SkISize onISize() override { return SkISize::Make(1000, 400); }

    void onDraw(SkCanvas* canvas) override {
        this->drawAll(canvas, 2.5f);
        canvas->translate(0, 250);
        canvas->scale(0.5, 0.5);
        this->drawAll(canvas, 1);
    }

private:
    void drawAll(SkCanvas* canvas, SkScalar scaleX) const {
        constexpr struct {
            SkFilterQuality fQuality;
            const char* fName;
        } kQualities[] = {
            {kNone_SkFilterQuality, "none"},
            {kLow_SkFilterQuality, "low"},
            {kMedium_SkFilterQuality, "medium"},
            {kHigh_SkFilterQuality, "high"},
        };

        SkRect rect = SkRect::MakeLTRB(20, 60, 220, 210);
        SkMatrix lm = SkMatrix::I();
        lm.setScaleX(scaleX);
        lm.setTranslateX(423);
        lm.setTranslateY(330);

        SkPaint textPaint;
        sk_tool_utils::set_portable_typeface(&textPaint);
        textPaint.setAntiAlias(true);

        SkPaint bmpPaint(textPaint);

        SkAutoCanvasRestore acr(canvas, true);

        for (size_t q = 0; q < SK_ARRAY_COUNT(kQualities); ++q) {
            constexpr SkShader::TileMode kTM = SkShader::kRepeat_TileMode;
            bmpPaint.setShader(SkShader::MakeBitmapShader(fBmp, kTM, kTM, &lm));
            bmpPaint.setFilterQuality(kQualities[q].fQuality);
            canvas->drawRect(rect, bmpPaint);
            canvas->drawText(kQualities[q].fName, strlen(kQualities[q].fName), 20, 40, textPaint);
            canvas->translate(250, 0);
        }

    }

    SkBitmap    fBmp;

    typedef skiagm::GM INHERITED;
};

//////////////////////////////////////////////////////////////////////////////

DEF_GM(return new BmpFilterQualityRepeat;)