MatrixConvolutionBench.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 "bench/Benchmark.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkPaint.h"
  10. #include "include/core/SkString.h"
  11. #include "include/effects/SkMatrixConvolutionImageFilter.h"
  12. #include "include/utils/SkRandom.h"
  13. static const char* name(SkMatrixConvolutionImageFilter::TileMode mode) {
  14. switch (mode) {
  15. case SkMatrixConvolutionImageFilter::kClamp_TileMode: return "clamp";
  16. case SkMatrixConvolutionImageFilter::kRepeat_TileMode: return "repeat";
  17. case SkMatrixConvolutionImageFilter::kClampToBlack_TileMode: return "clampToBlack";
  18. }
  19. return "oops";
  20. }
  21. class MatrixConvolutionBench : public Benchmark {
  22. public:
  23. MatrixConvolutionBench(SkMatrixConvolutionImageFilter::TileMode tileMode, bool convolveAlpha)
  24. : fName(SkStringPrintf("matrixconvolution_%s%s",
  25. name(tileMode),
  26. convolveAlpha ? "" : "_noConvolveAlpha")) {
  27. SkISize kernelSize = SkISize::Make(3, 3);
  28. SkScalar kernel[9] = {
  29. SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
  30. SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1),
  31. SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
  32. };
  33. SkScalar gain = 0.3f, bias = SkIntToScalar(100);
  34. SkIPoint kernelOffset = SkIPoint::Make(1, 1);
  35. fFilter = SkMatrixConvolutionImageFilter::Make(kernelSize, kernel, gain, bias,
  36. kernelOffset, tileMode, convolveAlpha,
  37. nullptr);
  38. }
  39. protected:
  40. virtual const char* onGetName() {
  41. return fName.c_str();
  42. }
  43. virtual void onDraw(int loops, SkCanvas* canvas) {
  44. SkPaint paint;
  45. this->setupPaint(&paint);
  46. paint.setAntiAlias(true);
  47. SkRandom rand;
  48. for (int i = 0; i < loops; i++) {
  49. SkRect r = SkRect::MakeWH(rand.nextUScalar1() * 400,
  50. rand.nextUScalar1() * 400);
  51. paint.setImageFilter(fFilter);
  52. canvas->drawOval(r, paint);
  53. }
  54. }
  55. private:
  56. sk_sp<SkImageFilter> fFilter;
  57. SkString fName;
  58. typedef Benchmark INHERITED;
  59. };
  60. DEF_BENCH( return new MatrixConvolutionBench(SkMatrixConvolutionImageFilter::kClamp_TileMode, true); )
  61. DEF_BENCH( return new MatrixConvolutionBench(SkMatrixConvolutionImageFilter::kRepeat_TileMode, true); )
  62. DEF_BENCH( return new MatrixConvolutionBench(SkMatrixConvolutionImageFilter::kClampToBlack_TileMode, true); )
  63. DEF_BENCH( return new MatrixConvolutionBench(SkMatrixConvolutionImageFilter::kClampToBlack_TileMode, false); )