imagesource2.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/SkCanvas.h"
  9. #include "include/core/SkColor.h"
  10. #include "include/core/SkFilterQuality.h"
  11. #include "include/core/SkImage.h"
  12. #include "include/core/SkImageFilter.h"
  13. #include "include/core/SkPaint.h"
  14. #include "include/core/SkRect.h"
  15. #include "include/core/SkRefCnt.h"
  16. #include "include/core/SkScalar.h"
  17. #include "include/core/SkSize.h"
  18. #include "include/core/SkString.h"
  19. #include "include/core/SkSurface.h"
  20. #include "include/core/SkTypes.h"
  21. #include "include/effects/SkImageSource.h"
  22. namespace skiagm {
  23. // This GM reproduces the issue in crbug.com/472795. The SkImageSource image
  24. // is shifted for high quality mode between cpu and gpu.
  25. class ImageSourceGM : public GM {
  26. public:
  27. ImageSourceGM(const char* suffix, SkFilterQuality filter) : fSuffix(suffix), fFilter(filter) {
  28. this->setBGColor(0xFFFFFFFF);
  29. }
  30. protected:
  31. SkString onShortName() override {
  32. SkString name("imagesrc2_");
  33. name.append(fSuffix);
  34. return name;
  35. }
  36. SkISize onISize() override { return SkISize::Make(256, 256); }
  37. // Create an image with high frequency vertical stripes
  38. void onOnceBeforeDraw() override {
  39. constexpr SkPMColor gColors[] = {
  40. SK_ColorRED, SK_ColorGRAY,
  41. SK_ColorGREEN, SK_ColorGRAY,
  42. SK_ColorBLUE, SK_ColorGRAY,
  43. SK_ColorCYAN, SK_ColorGRAY,
  44. SK_ColorMAGENTA, SK_ColorGRAY,
  45. SK_ColorYELLOW, SK_ColorGRAY,
  46. SK_ColorWHITE, SK_ColorGRAY,
  47. };
  48. auto surface(SkSurface::MakeRasterN32Premul(kImageSize, kImageSize));
  49. SkCanvas* canvas = surface->getCanvas();
  50. int curColor = 0;
  51. for (int x = 0; x < kImageSize; x += 3) {
  52. SkRect r = SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(0),
  53. SkIntToScalar(3), SkIntToScalar(kImageSize));
  54. SkPaint p;
  55. p.setColor(gColors[curColor]);
  56. canvas->drawRect(r, p);
  57. curColor = (curColor+1) % SK_ARRAY_COUNT(gColors);
  58. }
  59. fImage = surface->makeImageSnapshot();
  60. }
  61. void onDraw(SkCanvas* canvas) override {
  62. const SkRect srcRect = SkRect::MakeLTRB(0, 0,
  63. SkIntToScalar(kImageSize),
  64. SkIntToScalar(kImageSize));
  65. const SkRect dstRect = SkRect::MakeLTRB(0.75f, 0.75f, 225.75f, 225.75f);
  66. SkPaint p;
  67. p.setImageFilter(SkImageSource::Make(fImage, srcRect, dstRect, fFilter));
  68. canvas->saveLayer(nullptr, &p);
  69. canvas->restore();
  70. }
  71. private:
  72. static constexpr int kImageSize = 503;
  73. SkString fSuffix;
  74. SkFilterQuality fFilter;
  75. sk_sp<SkImage> fImage;
  76. typedef GM INHERITED;
  77. };
  78. //////////////////////////////////////////////////////////////////////////////
  79. DEF_GM(return new ImageSourceGM("none", kNone_SkFilterQuality);)
  80. DEF_GM(return new ImageSourceGM("low", kLow_SkFilterQuality);)
  81. DEF_GM(return new ImageSourceGM("med", kMedium_SkFilterQuality);)
  82. DEF_GM(return new ImageSourceGM("high", kHigh_SkFilterQuality);)
  83. }