2013-06-11 19:18:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
#include "Benchmark.h"
|
2013-06-11 19:18:44 +00:00
|
|
|
#include "SkCanvas.h"
|
2014-07-10 13:03:46 +00:00
|
|
|
#include "SkChecksum.h"
|
2013-06-11 19:18:44 +00:00
|
|
|
#include "SkPaint.h"
|
|
|
|
#include "SkString.h"
|
|
|
|
#include "SkTemplates.h"
|
|
|
|
|
2013-06-11 19:24:08 +00:00
|
|
|
#include "gUniqueGlyphIDs.h"
|
2013-06-12 15:50:26 +00:00
|
|
|
#define gUniqueGlyphIDs_Sentinel 0xFFFF
|
2013-06-11 19:18:44 +00:00
|
|
|
|
2013-06-12 15:50:26 +00:00
|
|
|
static int count_glyphs(const uint16_t start[]) {
|
|
|
|
const uint16_t* curr = start;
|
|
|
|
while (*curr != gUniqueGlyphIDs_Sentinel) {
|
|
|
|
curr += 1;
|
2013-06-11 19:18:44 +00:00
|
|
|
}
|
2013-10-15 17:47:36 +00:00
|
|
|
return static_cast<int>(curr - start);
|
2013-06-12 15:50:26 +00:00
|
|
|
}
|
2013-06-11 19:18:44 +00:00
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
class FontCacheBench : public Benchmark {
|
2013-06-12 15:50:26 +00:00
|
|
|
public:
|
2013-09-13 19:52:27 +00:00
|
|
|
FontCacheBench() {}
|
2013-06-13 07:00:51 +00:00
|
|
|
|
2013-06-11 19:18:44 +00:00
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
const char* onGetName() override {
|
2013-06-11 19:18:44 +00:00
|
|
|
return "fontcache";
|
|
|
|
}
|
2013-06-13 07:00:51 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(const int loops, SkCanvas* canvas) override {
|
2013-06-11 19:18:44 +00:00
|
|
|
SkPaint paint;
|
|
|
|
this->setupPaint(&paint);
|
|
|
|
paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
|
2013-06-13 07:00:51 +00:00
|
|
|
|
2013-06-11 20:25:53 +00:00
|
|
|
const uint16_t* array = gUniqueGlyphIDs;
|
2013-06-12 15:50:26 +00:00
|
|
|
while (*array != gUniqueGlyphIDs_Sentinel) {
|
2013-10-15 17:47:36 +00:00
|
|
|
int count = count_glyphs(array);
|
2013-12-03 18:17:16 +00:00
|
|
|
for (int i = 0; i < loops; ++i) {
|
2013-06-12 15:50:26 +00:00
|
|
|
paint.measureText(array, count * sizeof(uint16_t));
|
2013-06-11 19:18:44 +00:00
|
|
|
}
|
2013-06-12 15:50:26 +00:00
|
|
|
array += count + 1; // skip the sentinel
|
2013-06-11 19:18:44 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-13 07:00:51 +00:00
|
|
|
|
2013-06-12 15:50:26 +00:00
|
|
|
private:
|
2014-06-19 19:32:29 +00:00
|
|
|
typedef Benchmark INHERITED;
|
2013-06-12 15:50:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static uint32_t rotr(uint32_t value, unsigned bits) {
|
|
|
|
return (value >> bits) | (value << (32 - bits));
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef uint32_t (*HasherProc)(uint32_t);
|
|
|
|
|
|
|
|
static uint32_t hasher0(uint32_t value) {
|
|
|
|
value = value ^ (value >> 16);
|
|
|
|
return value ^ (value >> 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
const char* fName;
|
|
|
|
HasherProc fHasher;
|
|
|
|
} gRec[] = {
|
|
|
|
{ "hasher0", hasher0 },
|
2014-07-10 13:03:46 +00:00
|
|
|
{ "hasher2", SkChecksum::Mix },
|
2013-06-12 15:50:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define kMaxHashBits 12
|
|
|
|
#define kMaxHashCount (1 << kMaxHashBits)
|
|
|
|
|
|
|
|
static int count_collisions(const uint16_t array[], int count, HasherProc proc,
|
|
|
|
unsigned hashMask) {
|
|
|
|
char table[kMaxHashCount];
|
|
|
|
sk_bzero(table, sizeof(table));
|
2013-06-13 07:00:51 +00:00
|
|
|
|
2013-06-12 15:50:26 +00:00
|
|
|
int collisions = 0;
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
int index = proc(array[i]) & hashMask;
|
|
|
|
collisions += table[index];
|
|
|
|
table[index] = 1;
|
|
|
|
}
|
|
|
|
return collisions;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dump_array(const uint16_t array[], int count) {
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
SkDebugf(" %d,", array[i]);
|
|
|
|
}
|
|
|
|
SkDebugf("\n");
|
|
|
|
}
|
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
class FontCacheEfficiency : public Benchmark {
|
2013-06-12 15:50:26 +00:00
|
|
|
public:
|
2013-09-13 19:52:27 +00:00
|
|
|
FontCacheEfficiency() {
|
2015-08-27 14:41:13 +00:00
|
|
|
if (false) dump_array(nullptr, 0);
|
2013-06-12 15:50:26 +00:00
|
|
|
if (false) rotr(0, 0);
|
|
|
|
}
|
2013-06-13 07:00:51 +00:00
|
|
|
|
2013-06-12 15:50:26 +00:00
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
const char* onGetName() override {
|
2013-06-12 15:50:26 +00:00
|
|
|
return "fontefficiency";
|
|
|
|
}
|
2013-06-13 07:00:51 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(const int loops, SkCanvas* canvas) override {
|
2013-06-12 15:50:26 +00:00
|
|
|
static bool gDone;
|
|
|
|
if (gDone) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gDone = true;
|
2013-06-11 19:18:44 +00:00
|
|
|
|
2013-06-12 15:50:26 +00:00
|
|
|
for (int hashBits = 6; hashBits <= 12; hashBits += 1) {
|
|
|
|
int hashMask = ((1 << hashBits) - 1);
|
|
|
|
for (int limit = 32; limit <= 1024; limit <<= 1) {
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
|
|
|
|
int collisions = 0;
|
|
|
|
int glyphs = 0;
|
|
|
|
const uint16_t* array = gUniqueGlyphIDs;
|
|
|
|
while (*array != gUniqueGlyphIDs_Sentinel) {
|
|
|
|
int count = SkMin32(count_glyphs(array), limit);
|
|
|
|
collisions += count_collisions(array, count, gRec[i].fHasher, hashMask);
|
|
|
|
glyphs += count;
|
|
|
|
array += count + 1; // skip the sentinel
|
|
|
|
}
|
|
|
|
SkDebugf("hashBits [%d] limit [%d] collisions [%d / %d = %1.2g%%] using %s\n", hashBits, limit, collisions, glyphs,
|
|
|
|
collisions * 100.0 / glyphs, gRec[i].fName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-13 07:00:51 +00:00
|
|
|
|
2013-06-11 19:18:44 +00:00
|
|
|
private:
|
2014-06-19 19:32:29 +00:00
|
|
|
typedef Benchmark INHERITED;
|
2013-06-11 19:18:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-09-13 19:52:27 +00:00
|
|
|
DEF_BENCH( return new FontCacheBench(); )
|
2013-06-12 15:50:26 +00:00
|
|
|
|
|
|
|
// undefine this to run the efficiency test
|
2013-09-13 19:52:27 +00:00
|
|
|
//DEF_BENCH( return new FontCacheEfficiency(); )
|