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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | d1701ba | 2019-04-30 13:44:26 -0400 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkColor.h" |
Mike Reed | 6dbeac5 | 2021-01-13 13:14:23 -0500 | [diff] [blame] | 11 | #include "include/core/SkImage.h" |
Ben Wagner | d1701ba | 2019-04-30 13:44:26 -0400 | [diff] [blame] | 12 | #include "include/core/SkPaint.h" |
| 13 | #include "include/core/SkRect.h" |
| 14 | #include "include/core/SkScalar.h" |
| 15 | #include "include/core/SkSize.h" |
| 16 | #include "include/core/SkString.h" |
Mike Reed | 18aeb57 | 2021-01-19 17:58:25 -0500 | [diff] [blame] | 17 | #include "include/core/SkSurface.h" |
robertphillips | f5ac972 | 2015-04-14 08:19:01 -0700 | [diff] [blame] | 18 | |
| 19 | namespace skiagm { |
| 20 | |
| 21 | // This GM exercises HighQuality anisotropic filtering. |
| 22 | class AnisotropicGM : public GM { |
| 23 | public: |
Mike Reed | f3ac2af | 2021-02-05 12:55:38 -0500 | [diff] [blame] | 24 | AnisotropicGM() : fSampling(SkCubicResampler::Mitchell()) { |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 25 | this->setBGColor(0xFFCCCCCC); |
robertphillips | f5ac972 | 2015-04-14 08:19:01 -0700 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | protected: |
| 29 | |
| 30 | SkString onShortName() override { return SkString("anisotropic_hq"); } |
| 31 | |
| 32 | SkISize onISize() override { |
| 33 | return SkISize::Make(2*kImageSize + 3*kSpacer, |
| 34 | kNumVertImages*kImageSize + (kNumVertImages+1)*kSpacer); |
| 35 | } |
| 36 | |
| 37 | // Create an image consisting of lines radiating from its center |
| 38 | void onOnceBeforeDraw() override { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 39 | constexpr int kNumLines = 100; |
| 40 | constexpr SkScalar kAngleStep = 360.0f / kNumLines; |
| 41 | constexpr int kInnerOffset = 10; |
robertphillips | f5ac972 | 2015-04-14 08:19:01 -0700 | [diff] [blame] | 42 | |
Mike Reed | 18aeb57 | 2021-01-19 17:58:25 -0500 | [diff] [blame] | 43 | auto info = SkImageInfo::MakeN32(kImageSize, kImageSize, kOpaque_SkAlphaType); |
| 44 | auto surf = SkSurface::MakeRaster(info); |
| 45 | auto canvas = surf->getCanvas(); |
robertphillips | f5ac972 | 2015-04-14 08:19:01 -0700 | [diff] [blame] | 46 | |
Mike Reed | 18aeb57 | 2021-01-19 17:58:25 -0500 | [diff] [blame] | 47 | canvas->clear(SK_ColorWHITE); |
robertphillips | f5ac972 | 2015-04-14 08:19:01 -0700 | [diff] [blame] | 48 | |
| 49 | SkPaint p; |
| 50 | p.setAntiAlias(true); |
| 51 | |
| 52 | SkScalar angle = 0.0f, sin, cos; |
| 53 | |
Mike Reed | 18aeb57 | 2021-01-19 17:58:25 -0500 | [diff] [blame] | 54 | canvas->translate(kImageSize/2.0f, kImageSize/2.0f); |
robertphillips | f5ac972 | 2015-04-14 08:19:01 -0700 | [diff] [blame] | 55 | for (int i = 0; i < kNumLines; ++i, angle += kAngleStep) { |
Brian Osman | 4428f2c | 2019-04-02 10:59:28 -0400 | [diff] [blame] | 56 | sin = SkScalarSin(angle); |
| 57 | cos = SkScalarCos(angle); |
Mike Reed | 18aeb57 | 2021-01-19 17:58:25 -0500 | [diff] [blame] | 58 | canvas->drawLine(cos * kInnerOffset, sin * kInnerOffset, |
| 59 | cos * kImageSize/2, sin * kImageSize/2, p); |
robertphillips | f5ac972 | 2015-04-14 08:19:01 -0700 | [diff] [blame] | 60 | } |
Mike Reed | 18aeb57 | 2021-01-19 17:58:25 -0500 | [diff] [blame] | 61 | fImage = surf->makeImageSnapshot(); |
robertphillips | f5ac972 | 2015-04-14 08:19:01 -0700 | [diff] [blame] | 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)); |
Mike Reed | 34c56a5 | 2021-01-22 15:26:41 -0500 | [diff] [blame] | 67 | canvas->drawImageRect(fImage, r, fSampling); |
robertphillips | f5ac972 | 2015-04-14 08:19:01 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void onDraw(SkCanvas* canvas) override { |
| 71 | 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] | 72 | |
robertphillips | f5ac972 | 2015-04-14 08:19:01 -0700 | [diff] [blame] | 73 | SkASSERT(kNumVertImages-1 == (int)SK_ARRAY_COUNT(gScales)/2); |
| 74 | |
| 75 | // Minimize vertically |
| 76 | for (int i = 0; i < (int)SK_ARRAY_COUNT(gScales); ++i) { |
Mike Reed | 18aeb57 | 2021-01-19 17:58:25 -0500 | [diff] [blame] | 77 | int height = SkScalarFloorToInt(fImage->height() * gScales[i]); |
robertphillips | f5ac972 | 2015-04-14 08:19:01 -0700 | [diff] [blame] | 78 | |
| 79 | int yOff; |
| 80 | if (i <= (int)SK_ARRAY_COUNT(gScales)/2) { |
Mike Reed | 18aeb57 | 2021-01-19 17:58:25 -0500 | [diff] [blame] | 81 | yOff = kSpacer + i * (fImage->height() + kSpacer); |
robertphillips | f5ac972 | 2015-04-14 08:19:01 -0700 | [diff] [blame] | 82 | } else { |
| 83 | // Position the more highly squashed images with their less squashed counterparts |
Mike Reed | 18aeb57 | 2021-01-19 17:58:25 -0500 | [diff] [blame] | 84 | yOff = (SK_ARRAY_COUNT(gScales) - i) * (fImage->height() + kSpacer) - height; |
robertphillips | f5ac972 | 2015-04-14 08:19:01 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Mike Reed | 18aeb57 | 2021-01-19 17:58:25 -0500 | [diff] [blame] | 87 | this->draw(canvas, kSpacer, yOff, fImage->width(), height); |
robertphillips | f5ac972 | 2015-04-14 08:19:01 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | // Minimize horizontally |
| 91 | for (int i = 0; i < (int)SK_ARRAY_COUNT(gScales); ++i) { |
Mike Reed | 18aeb57 | 2021-01-19 17:58:25 -0500 | [diff] [blame] | 92 | int width = SkScalarFloorToInt(fImage->width() * gScales[i]); |
robertphillips | f5ac972 | 2015-04-14 08:19:01 -0700 | [diff] [blame] | 93 | |
| 94 | int xOff, yOff; |
| 95 | if (i <= (int)SK_ARRAY_COUNT(gScales)/2) { |
Mike Reed | 18aeb57 | 2021-01-19 17:58:25 -0500 | [diff] [blame] | 96 | xOff = fImage->width() + 2*kSpacer; |
| 97 | yOff = kSpacer + i * (fImage->height() + kSpacer); |
robertphillips | f5ac972 | 2015-04-14 08:19:01 -0700 | [diff] [blame] | 98 | } else { |
| 99 | // Position the more highly squashed images with their less squashed counterparts |
Mike Reed | 18aeb57 | 2021-01-19 17:58:25 -0500 | [diff] [blame] | 100 | xOff = fImage->width() + 2*kSpacer + fImage->width() - width; |
| 101 | yOff = kSpacer + (SK_ARRAY_COUNT(gScales) - i - 1) * (fImage->height() + kSpacer); |
robertphillips | f5ac972 | 2015-04-14 08:19:01 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Mike Reed | 18aeb57 | 2021-01-19 17:58:25 -0500 | [diff] [blame] | 104 | this->draw(canvas, xOff, yOff, width, fImage->height()); |
robertphillips | f5ac972 | 2015-04-14 08:19:01 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | private: |
Brian Salomon | 9fa47cc | 2021-10-08 18:48:26 -0400 | [diff] [blame] | 109 | inline static constexpr int kImageSize = 256; |
| 110 | inline static constexpr int kSpacer = 10; |
| 111 | inline static constexpr int kNumVertImages = 5; |
robertphillips | f5ac972 | 2015-04-14 08:19:01 -0700 | [diff] [blame] | 112 | |
Mike Reed | 18aeb57 | 2021-01-19 17:58:25 -0500 | [diff] [blame] | 113 | sk_sp<SkImage> fImage; |
| 114 | SkSamplingOptions fSampling; |
robertphillips | f5ac972 | 2015-04-14 08:19:01 -0700 | [diff] [blame] | 115 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 116 | using INHERITED = GM; |
robertphillips | f5ac972 | 2015-04-14 08:19:01 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | ////////////////////////////////////////////////////////////////////////////// |
| 120 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 121 | DEF_GM(return new AnisotropicGM;) |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 122 | } // namespace skiagm |