clippedbitmapshaders.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright 2013 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/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/SkTypes.h"
  21. namespace {
  22. // This GM draws a 3x3 grid (with the center element excluded) of rectangles
  23. // filled with a bitmap shader. The bitmap shader is transformed so that the
  24. // pattern cell is at the center (excluded) region.
  25. //
  26. // In Repeat and Mirror mode, this tests that the bitmap shader still draws
  27. // even though the pattern cell is outside the clip.
  28. //
  29. // In Clamp mode, this tests that the clamp is handled properly. For PDF,
  30. // (and possibly other exported formats) this also "tests" that the image itself
  31. // is not stored (well, you'll need to open it up with an external tool to
  32. // verify that).
  33. static SkBitmap create_bitmap() {
  34. SkBitmap bmp;
  35. bmp.allocN32Pixels(2, 2);
  36. uint32_t* pixels = reinterpret_cast<uint32_t*>(bmp.getPixels());
  37. pixels[0] = SkPreMultiplyColor(SK_ColorRED);
  38. pixels[1] = SkPreMultiplyColor(SK_ColorGREEN);
  39. pixels[2] = SkPreMultiplyColor(SK_ColorBLACK);
  40. pixels[3] = SkPreMultiplyColor(SK_ColorBLUE);
  41. return bmp;
  42. }
  43. constexpr SkScalar RECT_SIZE = 64;
  44. constexpr SkScalar SLIDE_SIZE = 300;
  45. class ClippedBitmapShadersGM : public skiagm::GM {
  46. public:
  47. ClippedBitmapShadersGM(SkTileMode mode, bool hq=false)
  48. : fMode(mode), fHQ(hq) {
  49. }
  50. protected:
  51. SkTileMode fMode;
  52. bool fHQ;
  53. SkString onShortName() override {
  54. SkString descriptor;
  55. switch (fMode) {
  56. case SkTileMode::kRepeat:
  57. descriptor = "tile";
  58. break;
  59. case SkTileMode::kMirror:
  60. descriptor = "mirror";
  61. break;
  62. case SkTileMode::kClamp:
  63. descriptor = "clamp";
  64. break;
  65. case SkTileMode::kDecal:
  66. descriptor = "decal";
  67. break;
  68. }
  69. descriptor.prepend("clipped-bitmap-shaders-");
  70. if (fHQ) {
  71. descriptor.append("-hq");
  72. }
  73. return descriptor;
  74. }
  75. SkISize onISize() override { return {300, 300}; }
  76. void onDraw(SkCanvas* canvas) override {
  77. SkBitmap bmp = create_bitmap();
  78. SkMatrix s;
  79. s.reset();
  80. s.setScale(8, 8);
  81. s.postTranslate(SLIDE_SIZE / 2, SLIDE_SIZE / 2);
  82. SkPaint paint;
  83. paint.setShader(bmp.makeShader(fMode, fMode, &s));
  84. if (fHQ) {
  85. paint.setFilterQuality(kHigh_SkFilterQuality);
  86. }
  87. SkScalar margin = (SLIDE_SIZE / 3 - RECT_SIZE) / 2;
  88. for (int i = 0; i < 3; i++) {
  89. SkScalar yOrigin = SLIDE_SIZE / 3 * i + margin;
  90. for (int j = 0; j < 3; j++) {
  91. SkScalar xOrigin = SLIDE_SIZE / 3 * j + margin;
  92. if (i == 1 && j == 1) {
  93. continue; // skip center element
  94. }
  95. SkRect rect = SkRect::MakeXYWH(xOrigin, yOrigin,
  96. RECT_SIZE, RECT_SIZE);
  97. canvas->save();
  98. canvas->clipRect(rect);
  99. canvas->drawRect(rect, paint);
  100. canvas->restore();
  101. }
  102. }
  103. }
  104. private:
  105. typedef GM INHERITED;
  106. };
  107. } // namespace
  108. DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kRepeat); )
  109. DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kMirror); )
  110. DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kClamp); )
  111. DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kRepeat, true); )
  112. DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kMirror, true); )
  113. DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kClamp, true); )