BlurImageFilterBench.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 "bench/Benchmark.h"
  8. #include "include/core/SkBitmap.h"
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkPaint.h"
  11. #include "include/core/SkShader.h"
  12. #include "include/core/SkString.h"
  13. #include "include/effects/SkBlurImageFilter.h"
  14. #include "include/effects/SkOffsetImageFilter.h"
  15. #include "include/utils/SkRandom.h"
  16. #define FILTER_WIDTH_SMALL 32
  17. #define FILTER_HEIGHT_SMALL 32
  18. #define FILTER_WIDTH_LARGE 256
  19. #define FILTER_HEIGHT_LARGE 256
  20. #define BLUR_SIGMA_MINI 0.5f
  21. #define BLUR_SIGMA_SMALL 1.0f
  22. #define BLUR_SIGMA_LARGE 10.0f
  23. #define BLUR_SIGMA_HUGE 80.0f
  24. // When 'cropped' is set we apply a cropRect to the blurImageFilter. The crop rect is an inset of
  25. // the source's natural dimensions. This is intended to exercise blurring a larger source bitmap
  26. // to a smaller destination bitmap.
  27. // When 'expanded' is set we apply a cropRect to the input of the blurImageFilter (a noOp
  28. // offsetImageFilter). The crop rect in this case is an inset of the source's natural dimensions.
  29. // An additional crop rect is applied to the blurImageFilter that is just the natural dimensions
  30. // of the source (not inset). This is intended to exercise blurring a smaller source bitmap to a
  31. // larger destination.
  32. static SkBitmap make_checkerboard(int width, int height) {
  33. SkBitmap bm;
  34. bm.allocN32Pixels(width, height);
  35. SkCanvas canvas(bm);
  36. canvas.clear(0x00000000);
  37. SkPaint darkPaint;
  38. darkPaint.setColor(0xFF804020);
  39. SkPaint lightPaint;
  40. lightPaint.setColor(0xFF244484);
  41. for (int y = 0; y < height; y += 16) {
  42. for (int x = 0; x < width; x += 16) {
  43. canvas.save();
  44. canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
  45. canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
  46. canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
  47. canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
  48. canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
  49. canvas.restore();
  50. }
  51. }
  52. return bm;
  53. }
  54. class BlurImageFilterBench : public Benchmark {
  55. public:
  56. BlurImageFilterBench(SkScalar sigmaX, SkScalar sigmaY, bool small, bool cropped,
  57. bool expanded)
  58. : fIsSmall(small)
  59. , fIsCropped(cropped)
  60. , fIsExpanded(expanded)
  61. , fInitialized(false)
  62. , fSigmaX(sigmaX)
  63. , fSigmaY(sigmaY) {
  64. fName.printf("blur_image_filter_%s%s%s_%.2f_%.2f",
  65. fIsSmall ? "small" : "large",
  66. fIsCropped ? "_cropped" : "",
  67. fIsExpanded ? "_expanded" : "",
  68. SkScalarToFloat(sigmaX), SkScalarToFloat(sigmaY));
  69. SkASSERT(!fIsExpanded || fIsCropped); // never want expansion w/o cropping
  70. }
  71. protected:
  72. const char* onGetName() override {
  73. return fName.c_str();
  74. }
  75. void onDelayedSetup() override {
  76. if (!fInitialized) {
  77. fCheckerboard = make_checkerboard(fIsSmall ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE,
  78. fIsSmall ? FILTER_HEIGHT_SMALL : FILTER_HEIGHT_LARGE);
  79. fInitialized = true;
  80. }
  81. }
  82. void onDraw(int loops, SkCanvas* canvas) override {
  83. static const SkScalar kX = 0;
  84. static const SkScalar kY = 0;
  85. const SkRect bmpRect = SkRect::MakeXYWH(kX, kY,
  86. SkIntToScalar(fCheckerboard.width()),
  87. SkIntToScalar(fCheckerboard.height()));
  88. const SkImageFilter::CropRect cropRect(bmpRect.makeInset(10.f, 10.f));
  89. const SkImageFilter::CropRect cropRectLarge(bmpRect);
  90. sk_sp<SkImageFilter> input = fIsExpanded
  91. ? SkOffsetImageFilter::Make(0, 0, nullptr, &cropRect)
  92. : nullptr;
  93. const SkImageFilter::CropRect* crop =
  94. fIsExpanded ? &cropRectLarge : fIsCropped ? &cropRect : nullptr;
  95. SkPaint paint;
  96. paint.setImageFilter(SkBlurImageFilter::Make(fSigmaX, fSigmaY, std::move(input), crop));
  97. for (int i = 0; i < loops; i++) {
  98. canvas->drawBitmap(fCheckerboard, kX, kY, &paint);
  99. }
  100. }
  101. private:
  102. SkString fName;
  103. bool fIsSmall;
  104. bool fIsCropped;
  105. bool fIsExpanded;
  106. bool fInitialized;
  107. SkBitmap fCheckerboard;
  108. SkScalar fSigmaX, fSigmaY;
  109. typedef Benchmark INHERITED;
  110. };
  111. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, 0, false, false, false);)
  112. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, 0, false, false, false);)
  113. DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_LARGE, false, false, false);)
  114. DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_SMALL, false, false, false);)
  115. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, true, false, false);)
  116. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, false, false, false);)
  117. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, true, false, false);)
  118. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, false, false, false);)
  119. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, true, false, false);)
  120. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, false, false, false);)
  121. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, true, false, false);)
  122. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, false, false, false);)
  123. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, 0, false, true, false);)
  124. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, 0, false, true, false);)
  125. DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_LARGE, false, true, false);)
  126. DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_SMALL, false, true, false);)
  127. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, true, true, false);)
  128. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, false, true, false);)
  129. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, true, true, false);)
  130. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, false, true, false);)
  131. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, true, true, false);)
  132. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, false, true, false);)
  133. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, true, true, false);)
  134. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, false, true, false);)
  135. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, 0, false, true, true);)
  136. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, 0, false, true, true);)
  137. DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_LARGE, false, true, true);)
  138. DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_SMALL, false, true, true);)
  139. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, true, true, true);)
  140. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, false, true, true);)
  141. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, true, true, true);)
  142. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, false, true, true);)
  143. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, true, true, true);)
  144. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, false, true, true);)
  145. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, true, true, true);)
  146. DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, false, true, true);)