blob: 3bff0637d456ee06c9ddcd8275878ea24110d76a [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 "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
tfarinaf168b862014-06-19 12:32:29 -070026class FontCacheBench : public Benchmark {
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:
mtklein72c9faa2015-01-09 10:06:39 -080031 const char* onGetName() SK_OVERRIDE {
reed@google.com8af03712013-06-11 19:18:44 +000032 return "fontcache";
33 }
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +000034
mtklein72c9faa2015-01-09 10:06:39 -080035 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:
tfarinaf168b862014-06-19 12:32:29 -070051 typedef Benchmark INHERITED;
reed@google.comc2652302013-06-12 15:50:26 +000052};
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
reed@google.comc2652302013-06-12 15:50:26 +000067static const struct {
68 const char* fName;
69 HasherProc fHasher;
70} gRec[] = {
71 { "hasher0", hasher0 },
mtklein67a32712014-07-10 06:03:46 -070072 { "hasher2", SkChecksum::Mix },
reed@google.comc2652302013-06-12 15:50:26 +000073};
74
75#define kMaxHashBits 12
76#define kMaxHashCount (1 << kMaxHashBits)
77
78static int count_collisions(const uint16_t array[], int count, HasherProc proc,
79 unsigned hashMask) {
80 char table[kMaxHashCount];
81 sk_bzero(table, sizeof(table));
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +000082
reed@google.comc2652302013-06-12 15:50:26 +000083 int collisions = 0;
84 for (int i = 0; i < count; ++i) {
85 int index = proc(array[i]) & hashMask;
86 collisions += table[index];
87 table[index] = 1;
88 }
89 return collisions;
90}
91
92static void dump_array(const uint16_t array[], int count) {
93 for (int i = 0; i < count; ++i) {
94 SkDebugf(" %d,", array[i]);
95 }
96 SkDebugf("\n");
97}
98
tfarinaf168b862014-06-19 12:32:29 -070099class FontCacheEfficiency : public Benchmark {
reed@google.comc2652302013-06-12 15:50:26 +0000100public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000101 FontCacheEfficiency() {
reed@google.comc2652302013-06-12 15:50:26 +0000102 if (false) dump_array(NULL, 0);
103 if (false) rotr(0, 0);
104 }
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +0000105
reed@google.comc2652302013-06-12 15:50:26 +0000106protected:
mtklein72c9faa2015-01-09 10:06:39 -0800107 const char* onGetName() SK_OVERRIDE {
reed@google.comc2652302013-06-12 15:50:26 +0000108 return "fontefficiency";
109 }
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +0000110
mtklein72c9faa2015-01-09 10:06:39 -0800111 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
reed@google.comc2652302013-06-12 15:50:26 +0000112 static bool gDone;
113 if (gDone) {
114 return;
115 }
116 gDone = true;
117
118 for (int hashBits = 6; hashBits <= 12; hashBits += 1) {
119 int hashMask = ((1 << hashBits) - 1);
120 for (int limit = 32; limit <= 1024; limit <<= 1) {
121 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
122 int collisions = 0;
123 int glyphs = 0;
124 const uint16_t* array = gUniqueGlyphIDs;
125 while (*array != gUniqueGlyphIDs_Sentinel) {
126 int count = SkMin32(count_glyphs(array), limit);
127 collisions += count_collisions(array, count, gRec[i].fHasher, hashMask);
128 glyphs += count;
129 array += count + 1; // skip the sentinel
130 }
131 SkDebugf("hashBits [%d] limit [%d] collisions [%d / %d = %1.2g%%] using %s\n", hashBits, limit, collisions, glyphs,
132 collisions * 100.0 / glyphs, gRec[i].fName);
133 }
134 }
135 }
136 }
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +0000137
reed@google.com8af03712013-06-11 19:18:44 +0000138private:
tfarinaf168b862014-06-19 12:32:29 -0700139 typedef Benchmark INHERITED;
reed@google.com8af03712013-06-11 19:18:44 +0000140};
141
142///////////////////////////////////////////////////////////////////////////////
143
mtklein@google.com410e6e82013-09-13 19:52:27 +0000144DEF_BENCH( return new FontCacheBench(); )
reed@google.comc2652302013-06-12 15:50:26 +0000145
146// undefine this to run the efficiency test
mtklein@google.com410e6e82013-09-13 19:52:27 +0000147//DEF_BENCH( return new FontCacheEfficiency(); )