blob: 1eb99d591fd867a1d47b0ce5d5f6011f87f785ef [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -04009#include "include/core/SkBitmap.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
12#include "include/core/SkFilterQuality.h"
13#include "include/core/SkPaint.h"
14#include "include/core/SkRect.h"
15#include "include/core/SkScalar.h"
16#include "include/core/SkSize.h"
17#include "include/core/SkString.h"
18#include "include/core/SkTypes.h"
robertphillipsf5ac9722015-04-14 08:19:01 -070019
20namespace skiagm {
21
22// This GM exercises HighQuality anisotropic filtering.
23class AnisotropicGM : public GM {
24public:
25 AnisotropicGM() : fFilterQuality(kHigh_SkFilterQuality) {
Mike Kleind46dce32018-08-16 10:17:03 -040026 this->setBGColor(0xFFCCCCCC);
robertphillipsf5ac9722015-04-14 08:19:01 -070027 }
28
29protected:
30
31 SkString onShortName() override { return SkString("anisotropic_hq"); }
32
33 SkISize onISize() override {
34 return SkISize::Make(2*kImageSize + 3*kSpacer,
35 kNumVertImages*kImageSize + (kNumVertImages+1)*kSpacer);
36 }
37
38 // Create an image consisting of lines radiating from its center
39 void onOnceBeforeDraw() override {
mtkleindbfd7ab2016-09-01 11:24:54 -070040 constexpr int kNumLines = 100;
41 constexpr SkScalar kAngleStep = 360.0f / kNumLines;
42 constexpr int kInnerOffset = 10;
robertphillipsf5ac9722015-04-14 08:19:01 -070043
44 fBM.allocN32Pixels(kImageSize, kImageSize, true);
45
46 SkCanvas canvas(fBM);
47
48 canvas.clear(SK_ColorWHITE);
49
50 SkPaint p;
51 p.setAntiAlias(true);
52
53 SkScalar angle = 0.0f, sin, cos;
54
55 canvas.translate(kImageSize/2.0f, kImageSize/2.0f);
56 for (int i = 0; i < kNumLines; ++i, angle += kAngleStep) {
Brian Osman4428f2c2019-04-02 10:59:28 -040057 sin = SkScalarSin(angle);
58 cos = SkScalarCos(angle);
robertphillipsf5ac9722015-04-14 08:19:01 -070059 canvas.drawLine(cos * kInnerOffset, sin * kInnerOffset,
60 cos * kImageSize/2, sin * kImageSize/2, p);
61 }
62 }
63
64 void draw(SkCanvas* canvas, int x, int y, int xSize, int ySize) {
65 SkRect r = SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
66 SkIntToScalar(xSize), SkIntToScalar(ySize));
67 SkPaint p;
68 p.setFilterQuality(fFilterQuality);
69 canvas->drawBitmapRect(fBM, r, &p);
70 }
71
72 void onDraw(SkCanvas* canvas) override {
73 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 -070074
robertphillipsf5ac9722015-04-14 08:19:01 -070075 SkASSERT(kNumVertImages-1 == (int)SK_ARRAY_COUNT(gScales)/2);
76
77 // Minimize vertically
78 for (int i = 0; i < (int)SK_ARRAY_COUNT(gScales); ++i) {
79 int height = SkScalarFloorToInt(fBM.height() * gScales[i]);
80
81 int yOff;
82 if (i <= (int)SK_ARRAY_COUNT(gScales)/2) {
83 yOff = kSpacer + i * (fBM.height() + kSpacer);
84 } else {
85 // Position the more highly squashed images with their less squashed counterparts
86 yOff = (SK_ARRAY_COUNT(gScales) - i) * (fBM.height() + kSpacer) - height;
87 }
88
89 this->draw(canvas, kSpacer, yOff, fBM.width(), height);
90 }
91
92 // Minimize horizontally
93 for (int i = 0; i < (int)SK_ARRAY_COUNT(gScales); ++i) {
94 int width = SkScalarFloorToInt(fBM.width() * gScales[i]);
95
96 int xOff, yOff;
97 if (i <= (int)SK_ARRAY_COUNT(gScales)/2) {
98 xOff = fBM.width() + 2*kSpacer;
99 yOff = kSpacer + i * (fBM.height() + kSpacer);
100 } else {
101 // Position the more highly squashed images with their less squashed counterparts
102 xOff = fBM.width() + 2*kSpacer + fBM.width() - width;
103 yOff = kSpacer + (SK_ARRAY_COUNT(gScales) - i - 1) * (fBM.height() + kSpacer);
104 }
105
106 this->draw(canvas, xOff, yOff, width, fBM.height());
107 }
108 }
109
110private:
mtkleindbfd7ab2016-09-01 11:24:54 -0700111 static constexpr int kImageSize = 256;
112 static constexpr int kSpacer = 10;
113 static constexpr int kNumVertImages = 5;
robertphillipsf5ac9722015-04-14 08:19:01 -0700114
115 SkBitmap fBM;
116 SkFilterQuality fFilterQuality;
117
118 typedef GM INHERITED;
119};
120
121//////////////////////////////////////////////////////////////////////////////
122
halcanary385fe4d2015-08-26 13:07:48 -0700123DEF_GM(return new AnisotropicGM;)
robertphillipsf5ac9722015-04-14 08:19:01 -0700124}