Home | History | Annotate | Download | only in bench
      1 /*
      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 
      8 #include "Benchmark.h"
      9 #include "SkCanvas.h"
     10 #include "SkFont.h"
     11 #include "SkTypeface.h"
     12 
     13 enum {
     14     NGLYPHS = 100
     15 };
     16 
     17 typedef void (*TypefaceProc)(int loops, const SkFont&, const void* text, size_t len,
     18                              int glyphCount);
     19 
     20 static void textToGlyphs_proc(int loops, const SkFont& font, const void* text, size_t len,
     21                               int glyphCount) {
     22     uint16_t glyphs[NGLYPHS];
     23     SkASSERT(glyphCount <= NGLYPHS);
     24 
     25     for (int i = 0; i < loops; ++i) {
     26         font.textToGlyphs(text, len, kUTF8_SkTextEncoding, glyphs, NGLYPHS);
     27     }
     28 }
     29 
     30 static void charsToGlyphs_proc(int loops, const SkFont& font, const void* text,
     31                                size_t len, int glyphCount) {
     32     uint16_t glyphs[NGLYPHS];
     33     SkASSERT(glyphCount <= NGLYPHS);
     34 
     35     SkTypeface* face = font.getTypefaceOrDefault();
     36     for (int i = 0; i < loops; ++i) {
     37         face->charsToGlyphs(text, SkTypeface::kUTF8_Encoding, glyphs, glyphCount);
     38     }
     39 }
     40 
     41 static void charsToGlyphsNull_proc(int loops, const SkFont& font, const void* text,
     42                                    size_t len, int glyphCount) {
     43     SkTypeface* face = font.getTypefaceOrDefault();
     44     for (int i = 0; i < loops; ++i) {
     45         face->charsToGlyphs(text, SkTypeface::kUTF8_Encoding, nullptr, glyphCount);
     46     }
     47 }
     48 
     49 class CMAPBench : public Benchmark {
     50     TypefaceProc fProc;
     51     SkString     fName;
     52     char         fText[NGLYPHS];
     53     SkFont       fFont;
     54 
     55 public:
     56     CMAPBench(TypefaceProc proc, const char name[]) {
     57         fProc = proc;
     58         fName.printf("cmap_%s", name);
     59 
     60         for (int i = 0; i < NGLYPHS; ++i) {
     61             // we're jamming values into utf8, so we must keep it legal utf8
     62             fText[i] = 'A' + (i & 31);
     63         }
     64         fFont.setTypeface(SkTypeface::MakeDefault());
     65     }
     66 
     67 protected:
     68     const char* onGetName() override {
     69         return fName.c_str();
     70     }
     71 
     72     void onDraw(int loops, SkCanvas* canvas) override {
     73         fProc(loops, fFont, fText, sizeof(fText), NGLYPHS);
     74     }
     75 
     76 private:
     77 
     78     typedef Benchmark INHERITED;
     79 };
     80 
     81 //////////////////////////////////////////////////////////////////////////////
     82 
     83 DEF_BENCH( return new CMAPBench(textToGlyphs_proc, "paint_textToGlyphs"); )
     84 DEF_BENCH( return new CMAPBench(charsToGlyphs_proc, "face_charsToGlyphs"); )
     85 DEF_BENCH( return new CMAPBench(charsToGlyphsNull_proc, "face_charsToGlyphs_null"); )
     86