MagnifierBench.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/effects/SkMagnifierImageFilter.h"
  11. #include "include/utils/SkRandom.h"
  12. #define FILTER_WIDTH_SMALL 32
  13. #define FILTER_HEIGHT_SMALL 32
  14. #define FILTER_WIDTH_LARGE 256
  15. #define FILTER_HEIGHT_LARGE 256
  16. class MagnifierBench : public Benchmark {
  17. public:
  18. MagnifierBench(bool small) :
  19. fIsSmall(small), fInitialized(false) {
  20. }
  21. protected:
  22. const char* onGetName() override {
  23. return fIsSmall ? "magnifier_small" : "magnifier_large";
  24. }
  25. void onDelayedSetup() override {
  26. if (!fInitialized) {
  27. make_checkerboard();
  28. fInitialized = true;
  29. }
  30. }
  31. void onDraw(int loops, SkCanvas* canvas) override {
  32. const int w = fIsSmall ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
  33. const int h = fIsSmall ? FILTER_HEIGHT_SMALL : FILTER_HEIGHT_LARGE;
  34. SkPaint paint;
  35. paint.setImageFilter(
  36. SkMagnifierImageFilter::Make(
  37. SkRect::MakeXYWH(SkIntToScalar(w / 4),
  38. SkIntToScalar(h / 4),
  39. SkIntToScalar(w / 2),
  40. SkIntToScalar(h / 2)), 100, nullptr));
  41. for (int i = 0; i < loops; i++) {
  42. canvas->drawBitmap(fCheckerboard, 0, 0, &paint);
  43. }
  44. }
  45. private:
  46. void make_checkerboard() {
  47. const int w = fIsSmall ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
  48. const int h = fIsSmall ? FILTER_HEIGHT_SMALL : FILTER_HEIGHT_LARGE;
  49. fCheckerboard.allocN32Pixels(w, h);
  50. SkCanvas canvas(fCheckerboard);
  51. canvas.clear(0x00000000);
  52. SkPaint darkPaint;
  53. darkPaint.setColor(0xFF804020);
  54. SkPaint lightPaint;
  55. lightPaint.setColor(0xFF244484);
  56. for (int y = 0; y < h; y += 16) {
  57. for (int x = 0; x < w; x += 16) {
  58. canvas.save();
  59. canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
  60. canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
  61. canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
  62. canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
  63. canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
  64. canvas.restore();
  65. }
  66. }
  67. }
  68. bool fIsSmall;
  69. bool fInitialized;
  70. SkBitmap fCheckerboard;
  71. typedef Benchmark INHERITED;
  72. };
  73. ///////////////////////////////////////////////////////////////////////////////
  74. DEF_BENCH( return new MagnifierBench(true); )
  75. DEF_BENCH( return new MagnifierBench(false); )