1 /*
2  * Copyright 2015 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.h"
9 #include "sk_tool_utils.h"
10 
11 #include "SkShader.h"
12 
13 // Inspired by svg/as-border-image/svg-as-border-image.html. Draws a four-color checker board bitmap
14 // such that it is stretched and repeat tiled with different filter qualities. It is testing whether
15 // the bmp filter respects the repeat mode at the tile seams.
16 class BmpFilterQualityRepeat : public skiagm::GM {
17 public:
BmpFilterQualityRepeat()18     BmpFilterQualityRepeat() { this->setBGColor(sk_tool_utils::color_to_565(0xFFCCBBAA)); }
19 
20 protected:
21 
onOnceBeforeDraw()22     void onOnceBeforeDraw() override {
23         fBmp.allocN32Pixels(40, 40, true);
24         SkCanvas canvas(fBmp);
25         SkBitmap colorBmp;
26         colorBmp.allocN32Pixels(20, 20, true);
27         colorBmp.eraseColor(0xFFFF0000);
28         canvas.drawBitmap(colorBmp, 0, 0);
29         colorBmp.eraseColor(sk_tool_utils::color_to_565(0xFF008200));
30         canvas.drawBitmap(colorBmp, 20, 0);
31         colorBmp.eraseColor(sk_tool_utils::color_to_565(0xFFFF9000));
32         canvas.drawBitmap(colorBmp, 0, 20);
33         colorBmp.eraseColor(sk_tool_utils::color_to_565(0xFF2000FF));
34         canvas.drawBitmap(colorBmp, 20, 20);
35     }
36 
onShortName()37     SkString onShortName() override { return SkString("bmp_filter_quality_repeat"); }
38 
onISize()39     SkISize onISize() override { return SkISize::Make(1000, 400); }
40 
onDraw(SkCanvas * canvas)41     void onDraw(SkCanvas* canvas) override {
42         this->drawAll(canvas, 2.5f);
43         canvas->translate(0, 250);
44         canvas->scale(0.5, 0.5);
45         this->drawAll(canvas, 1);
46     }
47 
48 private:
drawAll(SkCanvas * canvas,SkScalar scaleX) const49     void drawAll(SkCanvas* canvas, SkScalar scaleX) const {
50         constexpr struct {
51             SkFilterQuality fQuality;
52             const char* fName;
53         } kQualities[] = {
54             {kNone_SkFilterQuality, "none"},
55             {kLow_SkFilterQuality, "low"},
56             {kMedium_SkFilterQuality, "medium"},
57             {kHigh_SkFilterQuality, "high"},
58         };
59 
60         SkRect rect = SkRect::MakeLTRB(20, 60, 220, 210);
61         SkMatrix lm = SkMatrix::I();
62         lm.setScaleX(scaleX);
63         lm.setTranslateX(423);
64         lm.setTranslateY(330);
65 
66         SkPaint textPaint;
67         textPaint.setAntiAlias(true);
68 
69         SkPaint bmpPaint(textPaint);
70 
71         SkFont font(sk_tool_utils::create_portable_typeface());
72 
73         SkAutoCanvasRestore acr(canvas, true);
74 
75         for (size_t q = 0; q < SK_ARRAY_COUNT(kQualities); ++q) {
76             constexpr SkShader::TileMode kTM = SkShader::kRepeat_TileMode;
77             bmpPaint.setShader(SkShader::MakeBitmapShader(fBmp, kTM, kTM, &lm));
78             bmpPaint.setFilterQuality(kQualities[q].fQuality);
79             canvas->drawRect(rect, bmpPaint);
80             canvas->drawString(kQualities[q].fName, 20, 40, font, textPaint);
81             canvas->translate(250, 0);
82         }
83 
84     }
85 
86     SkBitmap    fBmp;
87 
88     typedef skiagm::GM INHERITED;
89 };
90 
91 //////////////////////////////////////////////////////////////////////////////
92 
93 DEF_GM(return new BmpFilterQualityRepeat;)
94