anisotropic.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #include "gm/gm.h"
  8. #include "include/core/SkBitmap.h"
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkFilterQuality.h"
  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"
  17. #include "include/core/SkTypes.h"
  18. namespace skiagm {
  19. // This GM exercises HighQuality anisotropic filtering.
  20. class AnisotropicGM : public GM {
  21. public:
  22. AnisotropicGM() : fFilterQuality(kHigh_SkFilterQuality) {
  23. this->setBGColor(0xFFCCCCCC);
  24. }
  25. protected:
  26. SkString onShortName() override { return SkString("anisotropic_hq"); }
  27. SkISize onISize() override {
  28. return SkISize::Make(2*kImageSize + 3*kSpacer,
  29. kNumVertImages*kImageSize + (kNumVertImages+1)*kSpacer);
  30. }
  31. // Create an image consisting of lines radiating from its center
  32. void onOnceBeforeDraw() override {
  33. constexpr int kNumLines = 100;
  34. constexpr SkScalar kAngleStep = 360.0f / kNumLines;
  35. constexpr int kInnerOffset = 10;
  36. fBM.allocN32Pixels(kImageSize, kImageSize, true);
  37. SkCanvas canvas(fBM);
  38. canvas.clear(SK_ColorWHITE);
  39. SkPaint p;
  40. p.setAntiAlias(true);
  41. SkScalar angle = 0.0f, sin, cos;
  42. canvas.translate(kImageSize/2.0f, kImageSize/2.0f);
  43. for (int i = 0; i < kNumLines; ++i, angle += kAngleStep) {
  44. sin = SkScalarSin(angle);
  45. cos = SkScalarCos(angle);
  46. canvas.drawLine(cos * kInnerOffset, sin * kInnerOffset,
  47. cos * kImageSize/2, sin * kImageSize/2, p);
  48. }
  49. }
  50. void draw(SkCanvas* canvas, int x, int y, int xSize, int ySize) {
  51. SkRect r = SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
  52. SkIntToScalar(xSize), SkIntToScalar(ySize));
  53. SkPaint p;
  54. p.setFilterQuality(fFilterQuality);
  55. canvas->drawBitmapRect(fBM, r, &p);
  56. }
  57. void onDraw(SkCanvas* canvas) override {
  58. SkScalar gScales[] = { 0.9f, 0.8f, 0.75f, 0.6f, 0.5f, 0.4f, 0.25f, 0.2f, 0.1f };
  59. SkASSERT(kNumVertImages-1 == (int)SK_ARRAY_COUNT(gScales)/2);
  60. // Minimize vertically
  61. for (int i = 0; i < (int)SK_ARRAY_COUNT(gScales); ++i) {
  62. int height = SkScalarFloorToInt(fBM.height() * gScales[i]);
  63. int yOff;
  64. if (i <= (int)SK_ARRAY_COUNT(gScales)/2) {
  65. yOff = kSpacer + i * (fBM.height() + kSpacer);
  66. } else {
  67. // Position the more highly squashed images with their less squashed counterparts
  68. yOff = (SK_ARRAY_COUNT(gScales) - i) * (fBM.height() + kSpacer) - height;
  69. }
  70. this->draw(canvas, kSpacer, yOff, fBM.width(), height);
  71. }
  72. // Minimize horizontally
  73. for (int i = 0; i < (int)SK_ARRAY_COUNT(gScales); ++i) {
  74. int width = SkScalarFloorToInt(fBM.width() * gScales[i]);
  75. int xOff, yOff;
  76. if (i <= (int)SK_ARRAY_COUNT(gScales)/2) {
  77. xOff = fBM.width() + 2*kSpacer;
  78. yOff = kSpacer + i * (fBM.height() + kSpacer);
  79. } else {
  80. // Position the more highly squashed images with their less squashed counterparts
  81. xOff = fBM.width() + 2*kSpacer + fBM.width() - width;
  82. yOff = kSpacer + (SK_ARRAY_COUNT(gScales) - i - 1) * (fBM.height() + kSpacer);
  83. }
  84. this->draw(canvas, xOff, yOff, width, fBM.height());
  85. }
  86. }
  87. private:
  88. static constexpr int kImageSize = 256;
  89. static constexpr int kSpacer = 10;
  90. static constexpr int kNumVertImages = 5;
  91. SkBitmap fBM;
  92. SkFilterQuality fFilterQuality;
  93. typedef GM INHERITED;
  94. };
  95. //////////////////////////////////////////////////////////////////////////////
  96. DEF_GM(return new AnisotropicGM;)
  97. }