2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 #include "SkTypeface.h"
17 static SkTypeface::Encoding paint2Encoding(const SkPaint& paint) {
18 SkPaint::TextEncoding enc = paint.getTextEncoding();
19 SkASSERT(SkPaint::kGlyphID_TextEncoding != enc);
20 return (SkTypeface::Encoding)enc;
23 typedef void (*TypefaceProc)(int loops, const SkPaint&, const void* text, size_t len,
26 static void containsText_proc(int loops, const SkPaint& paint, const void* text, size_t len,
28 for (int i = 0; i < loops; ++i) {
29 paint.containsText(text, len);
33 static void textToGlyphs_proc(int loops, const SkPaint& paint, const void* text, size_t len,
35 uint16_t glyphs[NGLYPHS];
36 SkASSERT(glyphCount <= NGLYPHS);
38 for (int i = 0; i < loops; ++i) {
39 paint.textToGlyphs(text, len, glyphs);
43 static void charsToGlyphs_proc(int loops, const SkPaint& paint, const void* text,
44 size_t len, int glyphCount) {
45 SkTypeface::Encoding encoding = paint2Encoding(paint);
46 uint16_t glyphs[NGLYPHS];
47 SkASSERT(glyphCount <= NGLYPHS);
49 SkTypeface* face = paint.getTypeface();
50 for (int i = 0; i < loops; ++i) {
51 face->charsToGlyphs(text, encoding, glyphs, glyphCount);
55 static void charsToGlyphsNull_proc(int loops, const SkPaint& paint, const void* text,
56 size_t len, int glyphCount) {
57 SkTypeface::Encoding encoding = paint2Encoding(paint);
59 SkTypeface* face = paint.getTypeface();
60 for (int i = 0; i < loops; ++i) {
61 face->charsToGlyphs(text, encoding, NULL, glyphCount);
65 class CMAPBench : public Benchmark {
72 CMAPBench(TypefaceProc proc, const char name[]) {
74 fName.printf("cmap_%s", name);
76 for (int i = 0; i < NGLYPHS; ++i) {
77 // we're jamming values into utf8, so we must keep it legal utf8
78 fText[i] = 'A' + (i & 31);
80 fPaint.setTypeface(SkTypeface::RefDefault())->unref();
84 const char* onGetName() SK_OVERRIDE {
88 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
89 fProc(loops, fPaint, fText, sizeof(fText), NGLYPHS);
94 typedef Benchmark INHERITED;
97 //////////////////////////////////////////////////////////////////////////////
99 DEF_BENCH( return new CMAPBench(containsText_proc, "paint_containsText"); )
100 DEF_BENCH( return new CMAPBench(textToGlyphs_proc, "paint_textToGlyphs"); )
101 DEF_BENCH( return new CMAPBench(charsToGlyphs_proc, "face_charsToGlyphs"); )
102 DEF_BENCH( return new CMAPBench(charsToGlyphsNull_proc, "face_charsToGlyphs_null"); )