samplerstress.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright 2012 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/SkBlurTypes.h"
  10. #include "include/core/SkCanvas.h"
  11. #include "include/core/SkColor.h"
  12. #include "include/core/SkFont.h"
  13. #include "include/core/SkMaskFilter.h"
  14. #include "include/core/SkPaint.h"
  15. #include "include/core/SkPath.h"
  16. #include "include/core/SkRect.h"
  17. #include "include/core/SkRefCnt.h"
  18. #include "include/core/SkScalar.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. #include "include/core/SkTypeface.h"
  24. #include "tools/ToolUtils.h"
  25. namespace skiagm {
  26. /**
  27. * Stress test the GPU samplers by rendering a textured glyph with a mask and
  28. * an AA clip
  29. */
  30. class SamplerStressGM : public GM {
  31. public:
  32. SamplerStressGM()
  33. : fTextureCreated(false)
  34. , fMaskFilter(nullptr) {
  35. }
  36. protected:
  37. SkString onShortName() override {
  38. return SkString("gpusamplerstress");
  39. }
  40. SkISize onISize() override {
  41. return SkISize::Make(640, 480);
  42. }
  43. /**
  44. * Create a red & green stripes on black texture
  45. */
  46. void createTexture() {
  47. if (fTextureCreated) {
  48. return;
  49. }
  50. constexpr int xSize = 16;
  51. constexpr int ySize = 16;
  52. fTexture.allocN32Pixels(xSize, ySize);
  53. SkPMColor* addr = fTexture.getAddr32(0, 0);
  54. for (int y = 0; y < ySize; ++y) {
  55. for (int x = 0; x < xSize; ++x) {
  56. addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorBLACK);
  57. if ((y % 5) == 0) {
  58. addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorRED);
  59. }
  60. if ((x % 7) == 0) {
  61. addr[y*xSize+x] = SkPreMultiplyColor(SK_ColorGREEN);
  62. }
  63. }
  64. }
  65. fTextureCreated = true;
  66. }
  67. void createShader() {
  68. if (fShader) {
  69. return;
  70. }
  71. createTexture();
  72. fShader = fTexture.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat);
  73. }
  74. void createMaskFilter() {
  75. if (fMaskFilter) {
  76. return;
  77. }
  78. const SkScalar sigma = 1;
  79. fMaskFilter = SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma);
  80. }
  81. void onDraw(SkCanvas* canvas) override {
  82. createShader();
  83. createMaskFilter();
  84. canvas->save();
  85. // draw a letter "M" with a green & red striped texture and a
  86. // stipple mask with a round rect soft clip
  87. SkPaint paint;
  88. paint.setAntiAlias(true);
  89. paint.setShader(fShader);
  90. paint.setMaskFilter(fMaskFilter);
  91. SkFont font(ToolUtils::create_portable_typeface(), 72);
  92. SkRect temp;
  93. temp.set(SkIntToScalar(115),
  94. SkIntToScalar(75),
  95. SkIntToScalar(144),
  96. SkIntToScalar(110));
  97. SkPath path;
  98. path.addRoundRect(temp, SkIntToScalar(5), SkIntToScalar(5));
  99. canvas->clipPath(path, true); // AA is on
  100. canvas->drawString("M", 100.0f, 100.0f, font, paint);
  101. canvas->restore();
  102. // Now draw stroked versions of the "M" and the round rect so we can
  103. // see what is going on
  104. SkPaint paint2;
  105. paint2.setColor(SK_ColorBLACK);
  106. paint2.setAntiAlias(true);
  107. paint2.setStyle(SkPaint::kStroke_Style);
  108. paint2.setStrokeWidth(1);
  109. canvas->drawString("M", 100.0f, 100.0f, font, paint2);
  110. paint2.setColor(SK_ColorGRAY);
  111. canvas->drawPath(path, paint2);
  112. }
  113. private:
  114. SkBitmap fTexture;
  115. bool fTextureCreated;
  116. sk_sp<SkShader> fShader;
  117. sk_sp<SkMaskFilter> fMaskFilter;
  118. typedef GM INHERITED;
  119. };
  120. //////////////////////////////////////////////////////////////////////////////
  121. DEF_GM( return new SamplerStressGM; )
  122. }