bmpfilterqualityrepeat.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/SkFilterQuality.h"
  11. #include "include/core/SkFont.h"
  12. #include "include/core/SkMatrix.h"
  13. #include "include/core/SkPaint.h"
  14. #include "include/core/SkRect.h"
  15. #include "include/core/SkScalar.h"
  16. #include "include/core/SkShader.h"
  17. #include "include/core/SkSize.h"
  18. #include "include/core/SkString.h"
  19. #include "include/core/SkTileMode.h"
  20. #include "include/core/SkTypeface.h"
  21. #include "include/core/SkTypes.h"
  22. #include "tools/ToolUtils.h"
  23. // Inspired by svg/as-border-image/svg-as-border-image.html. Draws a four-color checker board bitmap
  24. // such that it is stretched and repeat tiled with different filter qualities. It is testing whether
  25. // the bmp filter respects the repeat mode at the tile seams.
  26. class BmpFilterQualityRepeat : public skiagm::GM {
  27. public:
  28. BmpFilterQualityRepeat() { this->setBGColor(ToolUtils::color_to_565(0xFFCCBBAA)); }
  29. protected:
  30. void onOnceBeforeDraw() override {
  31. fBmp.allocN32Pixels(40, 40, true);
  32. SkCanvas canvas(fBmp);
  33. SkBitmap colorBmp;
  34. colorBmp.allocN32Pixels(20, 20, true);
  35. colorBmp.eraseColor(0xFFFF0000);
  36. canvas.drawBitmap(colorBmp, 0, 0);
  37. colorBmp.eraseColor(ToolUtils::color_to_565(0xFF008200));
  38. canvas.drawBitmap(colorBmp, 20, 0);
  39. colorBmp.eraseColor(ToolUtils::color_to_565(0xFFFF9000));
  40. canvas.drawBitmap(colorBmp, 0, 20);
  41. colorBmp.eraseColor(ToolUtils::color_to_565(0xFF2000FF));
  42. canvas.drawBitmap(colorBmp, 20, 20);
  43. }
  44. SkString onShortName() override { return SkString("bmp_filter_quality_repeat"); }
  45. SkISize onISize() override { return SkISize::Make(1000, 400); }
  46. void onDraw(SkCanvas* canvas) override {
  47. this->drawAll(canvas, 2.5f);
  48. canvas->translate(0, 250);
  49. canvas->scale(0.5, 0.5);
  50. this->drawAll(canvas, 1);
  51. }
  52. private:
  53. void drawAll(SkCanvas* canvas, SkScalar scaleX) const {
  54. constexpr struct {
  55. SkFilterQuality fQuality;
  56. const char* fName;
  57. } kQualities[] = {
  58. {kNone_SkFilterQuality, "none"},
  59. {kLow_SkFilterQuality, "low"},
  60. {kMedium_SkFilterQuality, "medium"},
  61. {kHigh_SkFilterQuality, "high"},
  62. };
  63. SkRect rect = SkRect::MakeLTRB(20, 60, 220, 210);
  64. SkMatrix lm = SkMatrix::I();
  65. lm.setScaleX(scaleX);
  66. lm.setTranslateX(423);
  67. lm.setTranslateY(330);
  68. SkPaint textPaint;
  69. textPaint.setAntiAlias(true);
  70. SkPaint bmpPaint(textPaint);
  71. SkFont font(ToolUtils::create_portable_typeface());
  72. SkAutoCanvasRestore acr(canvas, true);
  73. for (size_t q = 0; q < SK_ARRAY_COUNT(kQualities); ++q) {
  74. constexpr SkTileMode kTM = SkTileMode::kRepeat;
  75. bmpPaint.setShader(fBmp.makeShader(kTM, kTM, &lm));
  76. bmpPaint.setFilterQuality(kQualities[q].fQuality);
  77. canvas->drawRect(rect, bmpPaint);
  78. canvas->drawString(kQualities[q].fName, 20, 40, font, textPaint);
  79. canvas->translate(250, 0);
  80. }
  81. }
  82. SkBitmap fBmp;
  83. typedef skiagm::GM INHERITED;
  84. };
  85. //////////////////////////////////////////////////////////////////////////////
  86. DEF_GM(return new BmpFilterQualityRepeat;)