MergeBench.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/core/SkFont.h"
  10. #include "include/core/SkSurface.h"
  11. #include "include/effects/SkImageSource.h"
  12. #include "include/effects/SkMergeImageFilter.h"
  13. #define FILTER_WIDTH_SMALL SkIntToScalar(32)
  14. #define FILTER_HEIGHT_SMALL SkIntToScalar(32)
  15. #define FILTER_WIDTH_LARGE SkIntToScalar(256)
  16. #define FILTER_HEIGHT_LARGE SkIntToScalar(256)
  17. static sk_sp<SkImage> make_bitmap() {
  18. sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(80, 80));
  19. surface->getCanvas()->clear(0x00000000);
  20. SkPaint paint;
  21. paint.setColor(0xFF884422);
  22. SkFont font;
  23. font.setSize(SkIntToScalar(96));
  24. surface->getCanvas()->drawSimpleText("g", 1, SkTextEncoding::kUTF8, 15, 55, font, paint);
  25. return surface->makeImageSnapshot();
  26. }
  27. static sk_sp<SkImage> make_checkerboard() {
  28. sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(80, 80));
  29. SkCanvas* canvas = surface->getCanvas();
  30. canvas->clear(0x00000000);
  31. SkPaint darkPaint;
  32. darkPaint.setColor(0xFF804020);
  33. SkPaint lightPaint;
  34. lightPaint.setColor(0xFF244484);
  35. for (int y = 0; y < 80; y += 16) {
  36. for (int x = 0; x < 80; x += 16) {
  37. canvas->save();
  38. canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
  39. canvas->drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
  40. canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
  41. canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
  42. canvas->drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
  43. canvas->restore();
  44. }
  45. }
  46. return surface->makeImageSnapshot();
  47. }
  48. class MergeBench : public Benchmark {
  49. public:
  50. MergeBench(bool small) : fIsSmall(small), fInitialized(false) { }
  51. protected:
  52. const char* onGetName() override {
  53. return fIsSmall ? "merge_small" : "merge_large";
  54. }
  55. void onDelayedSetup() override {
  56. if (!fInitialized) {
  57. fImage = make_bitmap();
  58. fCheckerboard = make_checkerboard();
  59. fInitialized = true;
  60. }
  61. }
  62. void onDraw(int loops, SkCanvas* canvas) override {
  63. SkRect r = fIsSmall ? SkRect::MakeWH(FILTER_WIDTH_SMALL, FILTER_HEIGHT_SMALL) :
  64. SkRect::MakeWH(FILTER_WIDTH_LARGE, FILTER_HEIGHT_LARGE);
  65. SkPaint paint;
  66. paint.setImageFilter(this->mergeBitmaps());
  67. for (int i = 0; i < loops; i++) {
  68. canvas->drawRect(r, paint);
  69. }
  70. }
  71. private:
  72. sk_sp<SkImageFilter> mergeBitmaps() {
  73. return SkMergeImageFilter::Make(SkImageSource::Make(fCheckerboard),
  74. SkImageSource::Make(fImage));
  75. }
  76. bool fIsSmall;
  77. bool fInitialized;
  78. sk_sp<SkImage> fImage, fCheckerboard;
  79. typedef Benchmark INHERITED;
  80. };
  81. ///////////////////////////////////////////////////////////////////////////////
  82. DEF_BENCH( return new MergeBench(true); )
  83. DEF_BENCH( return new MergeBench(false); )