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: AnisotropicGM()15 AnisotropicGM() : fFilterQuality(kHigh_SkFilterQuality) { 16 this->setBGColor(0xFFCCCCCC); 17 } 18 19 protected: 20 onShortName()21 SkString onShortName() override { return SkString("anisotropic_hq"); } 22 onISize()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 onOnceBeforeDraw()29 void onOnceBeforeDraw() override { 30 constexpr int kNumLines = 100; 31 constexpr SkScalar kAngleStep = 360.0f / kNumLines; 32 constexpr int kInnerOffset = 10; 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 draw(SkCanvas * canvas,int x,int y,int xSize,int ySize)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 onDraw(SkCanvas * canvas)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 }; 63 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: 100 static constexpr int kImageSize = 256; 101 static constexpr int kSpacer = 10; 102 static constexpr int kNumVertImages = 5; 103 104 SkBitmap fBM; 105 SkFilterQuality fFilterQuality; 106 107 typedef GM INHERITED; 108 }; 109 110 ////////////////////////////////////////////////////////////////////////////// 111 112 DEF_GM(return new AnisotropicGM;) 113 } 114