blob: 697bde64f42739456eaff520cea0d38c42ad67d5 [file] [log] [blame]
reed@google.com8af03712013-06-11 19:18:44 +00001/*
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
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
reed@google.com8af03712013-06-11 19:18:44 +00009#include "SkCanvas.h"
mtklein67a32712014-07-10 06:03:46 -070010#include "SkChecksum.h"
reed@google.com8af03712013-06-11 19:18:44 +000011#include "SkFontHost.h"
12#include "SkPaint.h"
13#include "SkString.h"
14#include "SkTemplates.h"
15
reed@google.com664621a2013-06-11 19:24:08 +000016#include "gUniqueGlyphIDs.h"
reed@google.comc2652302013-06-12 15:50:26 +000017#define gUniqueGlyphIDs_Sentinel 0xFFFF
18
19static int count_glyphs(const uint16_t start[]) {
20 const uint16_t* curr = start;
21 while (*curr != gUniqueGlyphIDs_Sentinel) {
22 curr += 1;
23 }
robertphillips@google.com8b169312013-10-15 17:47:36 +000024 return static_cast<int>(curr - start);
reed@google.comc2652302013-06-12 15:50:26 +000025}
reed@google.com8af03712013-06-11 19:18:44 +000026
tfarinaf168b862014-06-19 12:32:29 -070027class FontCacheBench : public Benchmark {
reed@google.comc2652302013-06-12 15:50:26 +000028public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000029 FontCacheBench() {}
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +000030
reed@google.com8af03712013-06-11 19:18:44 +000031protected:
32 virtual const char* onGetName() SK_OVERRIDE {
33 return "fontcache";
34 }
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +000035
commit-bot@chromium.org33614712013-12-03 18:17:16 +000036 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
reed@google.com8af03712013-06-11 19:18:44 +000037 SkPaint paint;
38 this->setupPaint(&paint);
39 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +000040
reed@google.com2fef6d22013-06-11 20:25:53 +000041 const uint16_t* array = gUniqueGlyphIDs;
reed@google.comc2652302013-06-12 15:50:26 +000042 while (*array != gUniqueGlyphIDs_Sentinel) {
robertphillips@google.com8b169312013-10-15 17:47:36 +000043 int count = count_glyphs(array);
commit-bot@chromium.org33614712013-12-03 18:17:16 +000044 for (int i = 0; i < loops; ++i) {
reed@google.comc2652302013-06-12 15:50:26 +000045 paint.measureText(array, count * sizeof(uint16_t));
reed@google.com2fef6d22013-06-11 20:25:53 +000046 }
reed@google.comc2652302013-06-12 15:50:26 +000047 array += count + 1; // skip the sentinel
reed@google.com8af03712013-06-11 19:18:44 +000048 }
49 }
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +000050
reed@google.comc2652302013-06-12 15:50:26 +000051private:
tfarinaf168b862014-06-19 12:32:29 -070052 typedef Benchmark INHERITED;
reed@google.comc2652302013-06-12 15:50:26 +000053};
reed@google.com8af03712013-06-11 19:18:44 +000054
reed@google.comc2652302013-06-12 15:50:26 +000055///////////////////////////////////////////////////////////////////////////////
56
57static uint32_t rotr(uint32_t value, unsigned bits) {
58 return (value >> bits) | (value << (32 - bits));
59}
60
61typedef uint32_t (*HasherProc)(uint32_t);
62
63static uint32_t hasher0(uint32_t value) {
64 value = value ^ (value >> 16);
65 return value ^ (value >> 8);
66}
67
reed@google.comc2652302013-06-12 15:50:26 +000068static const struct {
69 const char* fName;
70 HasherProc fHasher;
71} gRec[] = {
72 { "hasher0", hasher0 },
mtklein67a32712014-07-10 06:03:46 -070073 { "hasher2", SkChecksum::Mix },
reed@google.comc2652302013-06-12 15:50:26 +000074};
75
76#define kMaxHashBits 12
77#define kMaxHashCount (1 << kMaxHashBits)
78
79static int count_collisions(const uint16_t array[], int count, HasherProc proc,
80 unsigned hashMask) {
81 char table[kMaxHashCount];
82 sk_bzero(table, sizeof(table));
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +000083
reed@google.comc2652302013-06-12 15:50:26 +000084 int collisions = 0;
85 for (int i = 0; i < count; ++i) {
86 int index = proc(array[i]) & hashMask;
87 collisions += table[index];
88 table[index] = 1;
89 }
90 return collisions;
91}
92
93static void dump_array(const uint16_t array[], int count) {
94 for (int i = 0; i < count; ++i) {
95 SkDebugf(" %d,", array[i]);
96 }
97 SkDebugf("\n");
98}
99
tfarinaf168b862014-06-19 12:32:29 -0700100class FontCacheEfficiency : public Benchmark {
reed@google.comc2652302013-06-12 15:50:26 +0000101public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000102 FontCacheEfficiency() {
reed@google.comc2652302013-06-12 15:50:26 +0000103 if (false) dump_array(NULL, 0);
104 if (false) rotr(0, 0);
105 }
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +0000106
reed@google.comc2652302013-06-12 15:50:26 +0000107protected:
108 virtual const char* onGetName() SK_OVERRIDE {
109 return "fontefficiency";
110 }
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +0000111
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000112 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
reed@google.comc2652302013-06-12 15:50:26 +0000113 static bool gDone;
114 if (gDone) {
115 return;
116 }
117 gDone = true;
118
119 for (int hashBits = 6; hashBits <= 12; hashBits += 1) {
120 int hashMask = ((1 << hashBits) - 1);
121 for (int limit = 32; limit <= 1024; limit <<= 1) {
122 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
123 int collisions = 0;
124 int glyphs = 0;
125 const uint16_t* array = gUniqueGlyphIDs;
126 while (*array != gUniqueGlyphIDs_Sentinel) {
127 int count = SkMin32(count_glyphs(array), limit);
128 collisions += count_collisions(array, count, gRec[i].fHasher, hashMask);
129 glyphs += count;
130 array += count + 1; // skip the sentinel
131 }
132 SkDebugf("hashBits [%d] limit [%d] collisions [%d / %d = %1.2g%%] using %s\n", hashBits, limit, collisions, glyphs,
133 collisions * 100.0 / glyphs, gRec[i].fName);
134 }
135 }
136 }
137 }
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +0000138
reed@google.com8af03712013-06-11 19:18:44 +0000139private:
tfarinaf168b862014-06-19 12:32:29 -0700140 typedef Benchmark INHERITED;
reed@google.com8af03712013-06-11 19:18:44 +0000141};
142
143///////////////////////////////////////////////////////////////////////////////
144
mtklein@google.com410e6e82013-09-13 19:52:27 +0000145DEF_BENCH( return new FontCacheBench(); )
reed@google.comc2652302013-06-12 15:50:26 +0000146
147// undefine this to run the efficiency test
mtklein@google.com410e6e82013-09-13 19:52:27 +0000148//DEF_BENCH( return new FontCacheEfficiency(); )