CmapBench.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/SkTypeface.h"
  11. #include "include/utils/SkRandom.h"
  12. #include "src/utils/SkCharToGlyphCache.h"
  13. #include "src/utils/SkUTF.h"
  14. enum {
  15. NGLYPHS = 100
  16. };
  17. namespace {
  18. struct Rec {
  19. const SkCharToGlyphCache& fCache;
  20. int fLoops;
  21. const SkFont& fFont;
  22. const SkUnichar* fText;
  23. int fCount;
  24. };
  25. }
  26. typedef void (*TypefaceProc)(const Rec& r);
  27. static void textToGlyphs_proc(const Rec& r) {
  28. uint16_t glyphs[NGLYPHS];
  29. SkASSERT(r.fCount <= NGLYPHS);
  30. for (int i = 0; i < r.fLoops; ++i) {
  31. r.fFont.textToGlyphs(r.fText, r.fCount*4, SkTextEncoding::kUTF32, glyphs, NGLYPHS);
  32. }
  33. }
  34. static void charsToGlyphs_proc(const Rec& r) {
  35. uint16_t glyphs[NGLYPHS];
  36. SkASSERT(r.fCount <= NGLYPHS);
  37. SkTypeface* face = r.fFont.getTypefaceOrDefault();
  38. for (int i = 0; i < r.fLoops; ++i) {
  39. face->unicharsToGlyphs(r.fText, r.fCount, glyphs);
  40. }
  41. }
  42. static void addcache_proc(const Rec& r) {
  43. for (int i = 0; i < r.fLoops; ++i) {
  44. SkCharToGlyphCache cache;
  45. for (int i = 0; i < r.fCount; ++i) {
  46. cache.addCharAndGlyph(r.fText[i], i);
  47. }
  48. }
  49. }
  50. static void findcache_proc(const Rec& r) {
  51. for (int i = 0; i < r.fLoops; ++i) {
  52. for (int i = 0; i < r.fCount; ++i) {
  53. r.fCache.findGlyphIndex(r.fText[i]);
  54. }
  55. }
  56. }
  57. class CMAPBench : public Benchmark {
  58. TypefaceProc fProc;
  59. SkString fName;
  60. SkUnichar fText[NGLYPHS];
  61. SkFont fFont;
  62. SkCharToGlyphCache fCache;
  63. int fCount;
  64. public:
  65. CMAPBench(TypefaceProc proc, const char name[], int count) {
  66. SkASSERT(count <= NGLYPHS);
  67. fProc = proc;
  68. fName.printf("%s_%d", name, count);
  69. fCount = count;
  70. SkRandom rand;
  71. for (int i = 0; i < count; ++i) {
  72. fText[i] = rand.nextU() & 0xFFFF;
  73. fCache.addCharAndGlyph(fText[i], i);
  74. }
  75. fFont.setTypeface(SkTypeface::MakeDefault());
  76. }
  77. bool isSuitableFor(Backend backend) override {
  78. return backend == kNonRendering_Backend;
  79. }
  80. protected:
  81. const char* onGetName() override {
  82. return fName.c_str();
  83. }
  84. void onDraw(int loops, SkCanvas* canvas) override {
  85. fProc({fCache, loops, fFont, fText, fCount});
  86. }
  87. private:
  88. typedef Benchmark INHERITED;
  89. };
  90. //////////////////////////////////////////////////////////////////////////////
  91. constexpr int SMALL = 10;
  92. DEF_BENCH( return new CMAPBench(textToGlyphs_proc, "font_charToGlyph", SMALL); )
  93. DEF_BENCH( return new CMAPBench(charsToGlyphs_proc, "face_charToGlyph", SMALL); )
  94. DEF_BENCH( return new CMAPBench(addcache_proc, "addcache_charToGlyph", SMALL); )
  95. DEF_BENCH( return new CMAPBench(findcache_proc, "findcache_charToGlyph", SMALL); )
  96. constexpr int BIG = 100;
  97. DEF_BENCH( return new CMAPBench(textToGlyphs_proc, "font_charToGlyph", BIG); )
  98. DEF_BENCH( return new CMAPBench(charsToGlyphs_proc, "face_charToGlyph", BIG); )
  99. DEF_BENCH( return new CMAPBench(addcache_proc, "addcache_charToGlyph", BIG); )
  100. DEF_BENCH( return new CMAPBench(findcache_proc, "findcache_charToGlyph", BIG); )