blob: a3c0672ef4638c727b8462cfba5b2b01351f0700 [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
8#include "SkBenchmark.h"
9#include "SkCanvas.h"
10#include "SkFontHost.h"
11#include "SkPaint.h"
12#include "SkString.h"
13#include "SkTemplates.h"
14
reed@google.com664621a2013-06-11 19:24:08 +000015#include "gUniqueGlyphIDs.h"
reed@google.comc2652302013-06-12 15:50:26 +000016#define gUniqueGlyphIDs_Sentinel 0xFFFF
17
18static int count_glyphs(const uint16_t start[]) {
19 const uint16_t* curr = start;
20 while (*curr != gUniqueGlyphIDs_Sentinel) {
21 curr += 1;
22 }
robertphillips@google.com8b169312013-10-15 17:47:36 +000023 return static_cast<int>(curr - start);
reed@google.comc2652302013-06-12 15:50:26 +000024}
reed@google.com8af03712013-06-11 19:18:44 +000025
26class FontCacheBench : public SkBenchmark {
reed@google.comc2652302013-06-12 15:50:26 +000027public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000028 FontCacheBench() {}
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +000029
reed@google.com8af03712013-06-11 19:18:44 +000030protected:
31 virtual const char* onGetName() SK_OVERRIDE {
32 return "fontcache";
33 }
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +000034
commit-bot@chromium.org33614712013-12-03 18:17:16 +000035 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
reed@google.com8af03712013-06-11 19:18:44 +000036 SkPaint paint;
37 this->setupPaint(&paint);
38 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +000039
reed@google.com2fef6d22013-06-11 20:25:53 +000040 const uint16_t* array = gUniqueGlyphIDs;
reed@google.comc2652302013-06-12 15:50:26 +000041 while (*array != gUniqueGlyphIDs_Sentinel) {
robertphillips@google.com8b169312013-10-15 17:47:36 +000042 int count = count_glyphs(array);
commit-bot@chromium.org33614712013-12-03 18:17:16 +000043 for (int i = 0; i < loops; ++i) {
reed@google.comc2652302013-06-12 15:50:26 +000044 paint.measureText(array, count * sizeof(uint16_t));
reed@google.com2fef6d22013-06-11 20:25:53 +000045 }
reed@google.comc2652302013-06-12 15:50:26 +000046 array += count + 1; // skip the sentinel
reed@google.com8af03712013-06-11 19:18:44 +000047 }
48 }
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +000049
reed@google.comc2652302013-06-12 15:50:26 +000050private:
51 typedef SkBenchmark INHERITED;
52};
reed@google.com8af03712013-06-11 19:18:44 +000053
reed@google.comc2652302013-06-12 15:50:26 +000054///////////////////////////////////////////////////////////////////////////////
55
56static uint32_t rotr(uint32_t value, unsigned bits) {
57 return (value >> bits) | (value << (32 - bits));
58}
59
60typedef uint32_t (*HasherProc)(uint32_t);
61
62static uint32_t hasher0(uint32_t value) {
63 value = value ^ (value >> 16);
64 return value ^ (value >> 8);
65}
66
67static uint32_t hasher2(uint32_t h) {
68 h ^= h >> 16;
69 h *= 0x85ebca6b;
70 h ^= h >> 13;
71 h *= 0xc2b2ae35;
72 h ^= h >> 16;
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +000073
reed@google.comc2652302013-06-12 15:50:26 +000074 h ^= (h >> 8);
75 return h;
76}
77
78static const struct {
79 const char* fName;
80 HasherProc fHasher;
81} gRec[] = {
82 { "hasher0", hasher0 },
83 { "hasher2", hasher2 },
84};
85
86#define kMaxHashBits 12
87#define kMaxHashCount (1 << kMaxHashBits)
88
89static int count_collisions(const uint16_t array[], int count, HasherProc proc,
90 unsigned hashMask) {
91 char table[kMaxHashCount];
92 sk_bzero(table, sizeof(table));
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +000093
reed@google.comc2652302013-06-12 15:50:26 +000094 int collisions = 0;
95 for (int i = 0; i < count; ++i) {
96 int index = proc(array[i]) & hashMask;
97 collisions += table[index];
98 table[index] = 1;
99 }
100 return collisions;
101}
102
103static void dump_array(const uint16_t array[], int count) {
104 for (int i = 0; i < count; ++i) {
105 SkDebugf(" %d,", array[i]);
106 }
107 SkDebugf("\n");
108}
109
110class FontCacheEfficiency : public SkBenchmark {
111public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000112 FontCacheEfficiency() {
reed@google.comc2652302013-06-12 15:50:26 +0000113 if (false) dump_array(NULL, 0);
114 if (false) rotr(0, 0);
115 }
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +0000116
reed@google.comc2652302013-06-12 15:50:26 +0000117protected:
118 virtual const char* onGetName() SK_OVERRIDE {
119 return "fontefficiency";
120 }
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +0000121
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000122 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
reed@google.comc2652302013-06-12 15:50:26 +0000123 static bool gDone;
124 if (gDone) {
125 return;
126 }
127 gDone = true;
128
129 for (int hashBits = 6; hashBits <= 12; hashBits += 1) {
130 int hashMask = ((1 << hashBits) - 1);
131 for (int limit = 32; limit <= 1024; limit <<= 1) {
132 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
133 int collisions = 0;
134 int glyphs = 0;
135 const uint16_t* array = gUniqueGlyphIDs;
136 while (*array != gUniqueGlyphIDs_Sentinel) {
137 int count = SkMin32(count_glyphs(array), limit);
138 collisions += count_collisions(array, count, gRec[i].fHasher, hashMask);
139 glyphs += count;
140 array += count + 1; // skip the sentinel
141 }
142 SkDebugf("hashBits [%d] limit [%d] collisions [%d / %d = %1.2g%%] using %s\n", hashBits, limit, collisions, glyphs,
143 collisions * 100.0 / glyphs, gRec[i].fName);
144 }
145 }
146 }
147 }
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +0000148
reed@google.com8af03712013-06-11 19:18:44 +0000149private:
150 typedef SkBenchmark INHERITED;
151};
152
153///////////////////////////////////////////////////////////////////////////////
154
mtklein@google.com410e6e82013-09-13 19:52:27 +0000155DEF_BENCH( return new FontCacheBench(); )
reed@google.comc2652302013-06-12 15:50:26 +0000156
157// undefine this to run the efficiency test
mtklein@google.com410e6e82013-09-13 19:52:27 +0000158//DEF_BENCH( return new FontCacheEfficiency(); )