skia2/bench/CmapBench.cpp
Mike Reed 358fcad1b8 start to hide textparams on SkPaint
Bug: skia:
Change-Id: Id73c983cc71d39fe587d355e690261627fa63aee
Reviewed-on: https://skia-review.googlesource.com/c/172643
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Mike Reed <reed@google.com>
2018-11-23 20:54:16 +00:00

94 lines
2.8 KiB
C++

/*
* Copyright 2013 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "Benchmark.h"
#include "SkCanvas.h"
#include "SkFont.h"
#include "SkTypeface.h"
enum {
NGLYPHS = 100
};
typedef void (*TypefaceProc)(int loops, const SkFont&, const void* text, size_t len,
int glyphCount);
static void containsText_proc(int loops, const SkFont& font, const void* text, size_t len,
int glyphCount) {
for (int i = 0; i < loops; ++i) {
font.containsText(text, len, kUTF8_SkTextEncoding);
}
}
static void textToGlyphs_proc(int loops, const SkFont& font, const void* text, size_t len,
int glyphCount) {
uint16_t glyphs[NGLYPHS];
SkASSERT(glyphCount <= NGLYPHS);
for (int i = 0; i < loops; ++i) {
font.textToGlyphs(text, len, kUTF8_SkTextEncoding, glyphs, NGLYPHS);
}
}
static void charsToGlyphs_proc(int loops, const SkFont& font, const void* text,
size_t len, int glyphCount) {
uint16_t glyphs[NGLYPHS];
SkASSERT(glyphCount <= NGLYPHS);
SkTypeface* face = font.getTypeface();
for (int i = 0; i < loops; ++i) {
face->charsToGlyphs(text, SkTypeface::kUTF8_Encoding, glyphs, glyphCount);
}
}
static void charsToGlyphsNull_proc(int loops, const SkFont& font, const void* text,
size_t len, int glyphCount) {
SkTypeface* face = font.getTypeface();
for (int i = 0; i < loops; ++i) {
face->charsToGlyphs(text, SkTypeface::kUTF8_Encoding, nullptr, glyphCount);
}
}
class CMAPBench : public Benchmark {
TypefaceProc fProc;
SkString fName;
char fText[NGLYPHS];
SkFont fFont;
public:
CMAPBench(TypefaceProc proc, const char name[]) {
fProc = proc;
fName.printf("cmap_%s", name);
for (int i = 0; i < NGLYPHS; ++i) {
// we're jamming values into utf8, so we must keep it legal utf8
fText[i] = 'A' + (i & 31);
}
fFont.setTypeface(SkTypeface::MakeDefault());
}
protected:
const char* onGetName() override {
return fName.c_str();
}
void onDraw(int loops, SkCanvas* canvas) override {
fProc(loops, fFont, fText, sizeof(fText), NGLYPHS);
}
private:
typedef Benchmark INHERITED;
};
//////////////////////////////////////////////////////////////////////////////
DEF_BENCH( return new CMAPBench(containsText_proc, "paint_containsText"); )
DEF_BENCH( return new CMAPBench(textToGlyphs_proc, "paint_textToGlyphs"); )
DEF_BENCH( return new CMAPBench(charsToGlyphs_proc, "face_charsToGlyphs"); )
DEF_BENCH( return new CMAPBench(charsToGlyphsNull_proc, "face_charsToGlyphs_null"); )