robertphillips | f5ac972 | 2015-04-14 08:19:01 -0700 | [diff] [blame] | 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 | |
| 10 | namespace skiagm { |
| 11 | |
| 12 | // This GM exercises HighQuality anisotropic filtering. |
| 13 | class AnisotropicGM : public GM { |
| 14 | public: |
| 15 | AnisotropicGM() : fFilterQuality(kHigh_SkFilterQuality) { |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 16 | this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); |
robertphillips | f5ac972 | 2015-04-14 08:19:01 -0700 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | protected: |
| 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 { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 30 | constexpr int kNumLines = 100; |
| 31 | constexpr SkScalar kAngleStep = 360.0f / kNumLines; |
| 32 | constexpr int kInnerOffset = 10; |
robertphillips | f5ac972 | 2015-04-14 08:19:01 -0700 | [diff] [blame] | 33 | |
| 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 }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 63 | |
robertphillips | f5ac972 | 2015-04-14 08:19:01 -0700 | [diff] [blame] | 64 | 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 | |
| 99 | private: |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 100 | static constexpr int kImageSize = 256; |
| 101 | static constexpr int kSpacer = 10; |
| 102 | static constexpr int kNumVertImages = 5; |
robertphillips | f5ac972 | 2015-04-14 08:19:01 -0700 | [diff] [blame] | 103 | |
| 104 | SkBitmap fBM; |
| 105 | SkFilterQuality fFilterQuality; |
| 106 | |
| 107 | typedef GM INHERITED; |
| 108 | }; |
| 109 | |
| 110 | ////////////////////////////////////////////////////////////////////////////// |
| 111 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 112 | DEF_GM(return new AnisotropicGM;) |
robertphillips | f5ac972 | 2015-04-14 08:19:01 -0700 | [diff] [blame] | 113 | } |