blob: 7a874b202b07d29e8647a142e41897dc0e413bab [file] [log] [blame]
robertphillipsf5ac9722015-04-14 08:19:01 -07001/*
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
10namespace skiagm {
11
12// This GM exercises HighQuality anisotropic filtering.
13class AnisotropicGM : public GM {
14public:
15 AnisotropicGM() : fFilterQuality(kHigh_SkFilterQuality) {
Mike Kleind46dce32018-08-16 10:17:03 -040016 this->setBGColor(0xFFCCCCCC);
robertphillipsf5ac9722015-04-14 08:19:01 -070017 }
18
19protected:
20
21 SkString onShortName() override { return SkString("anisotropic_hq"); }
22
23 SkISize onISize() override {
24 return SkISize::Make(2*kImageSize + 3*kSpacer,
25 kNumVertImages*kImageSize + (kNumVertImages+1)*kSpacer);
26 }
27
28 // Create an image consisting of lines radiating from its center
29 void onOnceBeforeDraw() override {
mtkleindbfd7ab2016-09-01 11:24:54 -070030 constexpr int kNumLines = 100;
31 constexpr SkScalar kAngleStep = 360.0f / kNumLines;
32 constexpr int kInnerOffset = 10;
robertphillipsf5ac9722015-04-14 08:19:01 -070033
34 fBM.allocN32Pixels(kImageSize, kImageSize, true);
35
36 SkCanvas canvas(fBM);
37
38 canvas.clear(SK_ColorWHITE);
39
40 SkPaint p;
41 p.setAntiAlias(true);
42
43 SkScalar angle = 0.0f, sin, cos;
44
45 canvas.translate(kImageSize/2.0f, kImageSize/2.0f);
46 for (int i = 0; i < kNumLines; ++i, angle += kAngleStep) {
47 sin = SkScalarSinCos(angle, &cos);
48 canvas.drawLine(cos * kInnerOffset, sin * kInnerOffset,
49 cos * kImageSize/2, sin * kImageSize/2, p);
50 }
51 }
52
53 void draw(SkCanvas* canvas, int x, int y, int xSize, int ySize) {
54 SkRect r = SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
55 SkIntToScalar(xSize), SkIntToScalar(ySize));
56 SkPaint p;
57 p.setFilterQuality(fFilterQuality);
58 canvas->drawBitmapRect(fBM, r, &p);
59 }
60
61 void onDraw(SkCanvas* canvas) override {
62 SkScalar gScales[] = { 0.9f, 0.8f, 0.75f, 0.6f, 0.5f, 0.4f, 0.25f, 0.2f, 0.1f };
halcanary9d524f22016-03-29 09:03:52 -070063
robertphillipsf5ac9722015-04-14 08:19:01 -070064 SkASSERT(kNumVertImages-1 == (int)SK_ARRAY_COUNT(gScales)/2);
65
66 // Minimize vertically
67 for (int i = 0; i < (int)SK_ARRAY_COUNT(gScales); ++i) {
68 int height = SkScalarFloorToInt(fBM.height() * gScales[i]);
69
70 int yOff;
71 if (i <= (int)SK_ARRAY_COUNT(gScales)/2) {
72 yOff = kSpacer + i * (fBM.height() + kSpacer);
73 } else {
74 // Position the more highly squashed images with their less squashed counterparts
75 yOff = (SK_ARRAY_COUNT(gScales) - i) * (fBM.height() + kSpacer) - height;
76 }
77
78 this->draw(canvas, kSpacer, yOff, fBM.width(), height);
79 }
80
81 // Minimize horizontally
82 for (int i = 0; i < (int)SK_ARRAY_COUNT(gScales); ++i) {
83 int width = SkScalarFloorToInt(fBM.width() * gScales[i]);
84
85 int xOff, yOff;
86 if (i <= (int)SK_ARRAY_COUNT(gScales)/2) {
87 xOff = fBM.width() + 2*kSpacer;
88 yOff = kSpacer + i * (fBM.height() + kSpacer);
89 } else {
90 // Position the more highly squashed images with their less squashed counterparts
91 xOff = fBM.width() + 2*kSpacer + fBM.width() - width;
92 yOff = kSpacer + (SK_ARRAY_COUNT(gScales) - i - 1) * (fBM.height() + kSpacer);
93 }
94
95 this->draw(canvas, xOff, yOff, width, fBM.height());
96 }
97 }
98
99private:
mtkleindbfd7ab2016-09-01 11:24:54 -0700100 static constexpr int kImageSize = 256;
101 static constexpr int kSpacer = 10;
102 static constexpr int kNumVertImages = 5;
robertphillipsf5ac9722015-04-14 08:19:01 -0700103
104 SkBitmap fBM;
105 SkFilterQuality fFilterQuality;
106
107 typedef GM INHERITED;
108};
109
110//////////////////////////////////////////////////////////////////////////////
111
halcanary385fe4d2015-08-26 13:07:48 -0700112DEF_GM(return new AnisotropicGM;)
robertphillipsf5ac9722015-04-14 08:19:01 -0700113}