TextBlobBench.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright 2015 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/SkPaint.h"
  11. #include "include/core/SkStream.h"
  12. #include "include/core/SkString.h"
  13. #include "include/core/SkTextBlob.h"
  14. #include "include/core/SkTypeface.h"
  15. #include "include/private/SkTemplates.h"
  16. #include "include/utils/SkRandom.h"
  17. #include "tools/Resources.h"
  18. #include "tools/ToolUtils.h"
  19. /*
  20. * A trivial test which benchmarks the performance of a textblob with a single run.
  21. */
  22. class SkTextBlobBench : public Benchmark {
  23. public:
  24. SkTextBlobBench() {}
  25. void onDelayedSetup() override {
  26. fFont.setTypeface(ToolUtils::create_portable_typeface("serif", SkFontStyle()));
  27. fFont.setSubpixel(true);
  28. // This text seems representative in both length and letter frequency.
  29. const char* text = "Keep your sentences short, but not overly so.";
  30. fGlyphs.setCount(fFont.countText(text, strlen(text), SkTextEncoding::kUTF8));
  31. fXPos.setCount(fGlyphs.count());
  32. fFont.textToGlyphs(text, strlen(text), SkTextEncoding::kUTF8, fGlyphs.begin(), fGlyphs.count());
  33. fFont.getXPos(&fGlyphs[0], fGlyphs.count(), fXPos.begin());
  34. }
  35. sk_sp<SkTextBlob> makeBlob() {
  36. const SkTextBlobBuilder::RunBuffer& run =
  37. fBuilder.allocRunPosH(fFont, fGlyphs.count(), 10, nullptr);
  38. memcpy(run.glyphs, &fGlyphs[0], fGlyphs.count() * sizeof(uint16_t));
  39. memcpy(run.pos, &fXPos[0], fXPos.count() * sizeof(SkScalar));
  40. return fBuilder.make();
  41. }
  42. private:
  43. SkTextBlobBuilder fBuilder;
  44. SkFont fFont;
  45. SkTDArray<uint16_t> fGlyphs;
  46. SkTDArray<SkScalar> fXPos;
  47. typedef Benchmark INHERITED;
  48. };
  49. class TextBlobCachedBench : public SkTextBlobBench {
  50. const char* onGetName() override {
  51. return "TextBlobCachedBench";
  52. }
  53. void onDraw(int loops, SkCanvas* canvas) override {
  54. SkPaint paint;
  55. auto blob = this->makeBlob();
  56. auto bigLoops = loops * 100;
  57. for (int i = 0; i < bigLoops; i++) {
  58. // To ensure maximum caching, we just redraw the blob at the same place everytime
  59. canvas->drawTextBlob(blob, 0, 0, paint);
  60. }
  61. }
  62. };
  63. DEF_BENCH( return new TextBlobCachedBench(); )
  64. class TextBlobFirstTimeBench : public SkTextBlobBench {
  65. const char* onGetName() override {
  66. return "TextBlobFirstTimeBench";
  67. }
  68. void onDraw(int loops, SkCanvas* canvas) override {
  69. SkPaint paint;
  70. auto bigLoops = loops * 100;
  71. for (int i = 0; i < bigLoops; i++) {
  72. canvas->drawTextBlob(this->makeBlob(), 0, 0, paint);
  73. }
  74. }
  75. };
  76. DEF_BENCH( return new TextBlobFirstTimeBench(); )
  77. class TextBlobMakeBench : public SkTextBlobBench {
  78. const char* onGetName() override {
  79. return "TextBlobMakeBench";
  80. }
  81. bool isSuitableFor(Backend backend) override {
  82. return backend == kNonRendering_Backend;
  83. }
  84. void onDraw(int loops, SkCanvas*) override {
  85. for (int i = 0; i < loops; i++) {
  86. for (int inner = 0; inner < 1000; ++inner) {
  87. this->makeBlob();
  88. }
  89. }
  90. }
  91. };
  92. DEF_BENCH( return new TextBlobMakeBench(); )