filterbug.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright 2016 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/SkColorPriv.h"
  12. #include "include/core/SkFilterQuality.h"
  13. #include "include/core/SkImage.h"
  14. #include "include/core/SkImageInfo.h"
  15. #include "include/core/SkMatrix.h"
  16. #include "include/core/SkPaint.h"
  17. #include "include/core/SkRect.h"
  18. #include "include/core/SkRefCnt.h"
  19. #include "include/core/SkShader.h"
  20. #include "include/core/SkSize.h"
  21. #include "include/core/SkString.h"
  22. #include "include/core/SkTileMode.h"
  23. static const sk_sp<SkImage> make_image(int firstBlackRow, int lastBlackRow) {
  24. static const int kWidth = 25;
  25. static const int kHeight = 27;
  26. SkBitmap bm;
  27. bm.allocN32Pixels(kWidth, kHeight);
  28. bm.eraseColor(SK_ColorWHITE);
  29. for (int y = firstBlackRow; y < lastBlackRow; ++y) {
  30. for (int x = 0; x < kWidth; ++x) {
  31. *bm.getAddr32(x, y) = SkPackARGB32(0xFF, 0x0, 0x0, 0x0);
  32. }
  33. }
  34. bm.setAlphaType(SkAlphaType::kOpaque_SkAlphaType);
  35. bm.setImmutable();
  36. return SkImage::MakeFromBitmap(bm);
  37. }
  38. // GM to reproduce crbug.com/673261.
  39. class FilterBugGM : public skiagm::GM {
  40. public:
  41. FilterBugGM() { this->setBGColor(SK_ColorRED); }
  42. protected:
  43. SkString onShortName() override { return SkString("filterbug"); }
  44. SkISize onISize() override { return SkISize::Make(150, 150); }
  45. void onOnceBeforeDraw() override {
  46. // The top texture has 5 black rows on top and then 22 white rows on the bottom
  47. fTop = make_image(0, 5);
  48. // The bottom texture has 5 black rows on the bottom and then 22 white rows on the top
  49. fBot = make_image(22, 27);
  50. }
  51. void onDraw(SkCanvas* canvas) override {
  52. static const SkFilterQuality kFilterQuality = SkFilterQuality::kHigh_SkFilterQuality;
  53. static const bool kDoAA = true;
  54. {
  55. SkRect r1 = SkRect::MakeXYWH(50.0f, 0.0f, 50.0f, 50.0f);
  56. SkPaint p1;
  57. p1.setAntiAlias(kDoAA);
  58. p1.setFilterQuality(kFilterQuality);
  59. SkMatrix localMat;
  60. localMat.setScaleTranslate(2.0f, 2.0f, 50.0f, 0.0f);
  61. p1.setShader(fTop->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &localMat));
  62. canvas->drawRect(r1, p1);
  63. }
  64. {
  65. SkRect r2 = SkRect::MakeXYWH(50.0f, 50.0f, 50.0f, 36.0f);
  66. SkPaint p2;
  67. p2.setColor(SK_ColorWHITE);
  68. p2.setAntiAlias(kDoAA);
  69. p2.setFilterQuality(kFilterQuality);
  70. canvas->drawRect(r2, p2);
  71. }
  72. {
  73. SkRect r3 = SkRect::MakeXYWH(50.0f, 86.0f, 50.0f, 50.0f);
  74. SkPaint p3;
  75. p3.setAntiAlias(kDoAA);
  76. p3.setFilterQuality(kFilterQuality);
  77. SkMatrix localMat;
  78. localMat.setScaleTranslate(2.0f, 2.0f, 50.0f, 86.0f);
  79. p3.setShader(fBot->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &localMat));
  80. canvas->drawRect(r3, p3);
  81. }
  82. }
  83. private:
  84. sk_sp<SkImage> fTop;
  85. sk_sp<SkImage> fBot;
  86. typedef skiagm::GM INHERITED;
  87. };
  88. //////////////////////////////////////////////////////////////////////////////
  89. DEF_GM(return new FilterBugGM;)