ColorFilterBench.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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/SkCanvas.h"
  9. #include "include/effects/SkColorFilterImageFilter.h"
  10. // Just need an interesting filter, nothing to special about colormatrix
  11. static sk_sp<SkColorFilter> make_grayscale() {
  12. float matrix[20];
  13. memset(matrix, 0, 20 * sizeof(float));
  14. matrix[0] = matrix[5] = matrix[10] = 0.2126f;
  15. matrix[1] = matrix[6] = matrix[11] = 0.7152f;
  16. matrix[2] = matrix[7] = matrix[12] = 0.0722f;
  17. matrix[18] = 1.0f;
  18. return SkColorFilters::Matrix(matrix);
  19. }
  20. /**
  21. * Different ways to draw the same thing (a red rect)
  22. * All of their timings should be about the same
  23. * (we allow for slight overhead to figure out that we can undo the presence of the filters)
  24. */
  25. class FilteredRectBench : public Benchmark {
  26. public:
  27. enum Type {
  28. kNoFilter_Type,
  29. kColorFilter_Type,
  30. kImageFilter_Type,
  31. };
  32. FilteredRectBench(Type t) : fType(t) {
  33. static const char* suffix[] = { "nofilter", "colorfilter", "imagefilter" };
  34. fName.printf("filteredrect_%s", suffix[t]);
  35. fPaint.setColor(SK_ColorRED);
  36. }
  37. protected:
  38. const char* onGetName() override {
  39. return fName.c_str();
  40. }
  41. void onDelayedSetup() override {
  42. switch (fType) {
  43. case kNoFilter_Type:
  44. break;
  45. case kColorFilter_Type:
  46. fPaint.setColorFilter(make_grayscale());
  47. break;
  48. case kImageFilter_Type:
  49. fPaint.setImageFilter(SkColorFilterImageFilter::Make(make_grayscale(), nullptr));
  50. break;
  51. }
  52. }
  53. void onDraw(int loops, SkCanvas* canvas) override {
  54. const SkRect r = { 0, 0, 256, 256 };
  55. for (int i = 0; i < loops; ++i) {
  56. canvas->drawRect(r, fPaint);
  57. }
  58. }
  59. private:
  60. SkPaint fPaint;
  61. SkString fName;
  62. Type fType;
  63. typedef Benchmark INHERITED;
  64. };
  65. DEF_BENCH( return new FilteredRectBench(FilteredRectBench::kNoFilter_Type); )
  66. DEF_BENCH( return new FilteredRectBench(FilteredRectBench::kColorFilter_Type); )
  67. DEF_BENCH( return new FilteredRectBench(FilteredRectBench::kImageFilter_Type); )