2013-07-02 13:56:39 +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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "bench/Benchmark.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "src/utils/SkCharToGlyphCache.h"
|
|
|
|
#include "src/utils/SkUTF.h"
|
2013-07-02 13:56:39 +00:00
|
|
|
|
|
|
|
enum {
|
|
|
|
NGLYPHS = 100
|
|
|
|
};
|
|
|
|
|
2019-04-11 21:10:17 +00:00
|
|
|
namespace {
|
|
|
|
struct Rec {
|
|
|
|
const SkCharToGlyphCache& fCache;
|
|
|
|
int fLoops;
|
|
|
|
const SkFont& fFont;
|
|
|
|
const SkUnichar* fText;
|
|
|
|
int fCount;
|
|
|
|
};
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace
|
2013-07-02 13:56:39 +00:00
|
|
|
|
2019-04-11 21:10:17 +00:00
|
|
|
typedef void (*TypefaceProc)(const Rec& r);
|
|
|
|
|
|
|
|
static void textToGlyphs_proc(const Rec& r) {
|
2013-07-02 13:56:39 +00:00
|
|
|
uint16_t glyphs[NGLYPHS];
|
2019-04-11 21:10:17 +00:00
|
|
|
SkASSERT(r.fCount <= NGLYPHS);
|
2013-07-02 13:56:39 +00:00
|
|
|
|
2019-04-11 21:10:17 +00:00
|
|
|
for (int i = 0; i < r.fLoops; ++i) {
|
2019-05-07 19:38:46 +00:00
|
|
|
r.fFont.textToGlyphs(r.fText, r.fCount*4, SkTextEncoding::kUTF32, glyphs, NGLYPHS);
|
2013-07-02 13:56:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 21:10:17 +00:00
|
|
|
static void charsToGlyphs_proc(const Rec& r) {
|
2013-07-02 13:56:39 +00:00
|
|
|
uint16_t glyphs[NGLYPHS];
|
2019-04-11 21:10:17 +00:00
|
|
|
SkASSERT(r.fCount <= NGLYPHS);
|
|
|
|
|
|
|
|
SkTypeface* face = r.fFont.getTypefaceOrDefault();
|
|
|
|
for (int i = 0; i < r.fLoops; ++i) {
|
2019-04-16 18:37:38 +00:00
|
|
|
face->unicharsToGlyphs(r.fText, r.fCount, glyphs);
|
2019-04-11 21:10:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void addcache_proc(const Rec& r) {
|
2021-08-13 02:27:39 +00:00
|
|
|
for (int loop = 0; loop < r.fLoops; ++loop) {
|
2019-04-11 21:10:17 +00:00
|
|
|
SkCharToGlyphCache cache;
|
|
|
|
for (int i = 0; i < r.fCount; ++i) {
|
|
|
|
cache.addCharAndGlyph(r.fText[i], i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-03 07:00:57 +00:00
|
|
|
|
2019-04-11 21:10:17 +00:00
|
|
|
static void findcache_proc(const Rec& r) {
|
2021-08-13 02:27:39 +00:00
|
|
|
for (int loop = 0; loop < r.fLoops; ++loop) {
|
2019-04-11 21:10:17 +00:00
|
|
|
for (int i = 0; i < r.fCount; ++i) {
|
|
|
|
r.fCache.findGlyphIndex(r.fText[i]);
|
|
|
|
}
|
2013-07-02 13:56:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
class CMAPBench : public Benchmark {
|
2013-07-02 13:56:39 +00:00
|
|
|
TypefaceProc fProc;
|
|
|
|
SkString fName;
|
2019-04-11 21:10:17 +00:00
|
|
|
SkUnichar fText[NGLYPHS];
|
2018-11-23 20:27:51 +00:00
|
|
|
SkFont fFont;
|
2019-04-11 21:10:17 +00:00
|
|
|
SkCharToGlyphCache fCache;
|
2019-04-15 16:07:19 +00:00
|
|
|
int fCount;
|
2013-07-02 13:56:39 +00:00
|
|
|
|
|
|
|
public:
|
2019-04-15 16:07:19 +00:00
|
|
|
CMAPBench(TypefaceProc proc, const char name[], int count) {
|
|
|
|
SkASSERT(count <= NGLYPHS);
|
|
|
|
|
2013-07-02 13:56:39 +00:00
|
|
|
fProc = proc;
|
2019-04-15 16:07:19 +00:00
|
|
|
fName.printf("%s_%d", name, count);
|
|
|
|
fCount = count;
|
2013-07-03 07:00:57 +00:00
|
|
|
|
2019-04-15 16:07:19 +00:00
|
|
|
SkRandom rand;
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
fText[i] = rand.nextU() & 0xFFFF;
|
2019-04-11 21:10:17 +00:00
|
|
|
fCache.addCharAndGlyph(fText[i], i);
|
2013-07-02 13:56:39 +00:00
|
|
|
}
|
2018-11-23 20:27:51 +00:00
|
|
|
fFont.setTypeface(SkTypeface::MakeDefault());
|
2013-07-02 13:56:39 +00:00
|
|
|
}
|
|
|
|
|
2019-04-11 21:10:17 +00:00
|
|
|
bool isSuitableFor(Backend backend) override {
|
|
|
|
return backend == kNonRendering_Backend;
|
|
|
|
}
|
|
|
|
|
2013-07-02 13:56:39 +00:00
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
const char* onGetName() override {
|
2013-07-02 13:56:39 +00:00
|
|
|
return fName.c_str();
|
|
|
|
}
|
|
|
|
|
2015-10-01 16:43:39 +00:00
|
|
|
void onDraw(int loops, SkCanvas* canvas) override {
|
2019-04-15 16:07:19 +00:00
|
|
|
fProc({fCache, loops, fFont, fText, fCount});
|
2013-07-02 13:56:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = Benchmark;
|
2013-07-02 13:56:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-04-15 16:07:19 +00:00
|
|
|
constexpr int SMALL = 10;
|
|
|
|
|
|
|
|
DEF_BENCH( return new CMAPBench(textToGlyphs_proc, "font_charToGlyph", SMALL); )
|
|
|
|
DEF_BENCH( return new CMAPBench(charsToGlyphs_proc, "face_charToGlyph", SMALL); )
|
|
|
|
DEF_BENCH( return new CMAPBench(addcache_proc, "addcache_charToGlyph", SMALL); )
|
|
|
|
DEF_BENCH( return new CMAPBench(findcache_proc, "findcache_charToGlyph", SMALL); )
|
|
|
|
|
|
|
|
constexpr int BIG = 100;
|
|
|
|
|
|
|
|
DEF_BENCH( return new CMAPBench(textToGlyphs_proc, "font_charToGlyph", BIG); )
|
|
|
|
DEF_BENCH( return new CMAPBench(charsToGlyphs_proc, "face_charToGlyph", BIG); )
|
|
|
|
DEF_BENCH( return new CMAPBench(addcache_proc, "addcache_charToGlyph", BIG); )
|
|
|
|
DEF_BENCH( return new CMAPBench(findcache_proc, "findcache_charToGlyph", BIG); )
|