blob: a350e7b4c40f3ee3e32ebfbdfa9d08add7edf2e2 [file] [log] [blame]
joshualitteef5b3e2015-04-03 08:07:26 -07001/*
2 * Copyright 2015 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 "Resources.h"
10#include "SkCanvas.h"
11#include "SkPaint.h"
12#include "SkRandom.h"
13#include "SkStream.h"
14#include "SkString.h"
15#include "SkTemplates.h"
16#include "SkTextBlob.h"
17#include "SkTypeface.h"
18
19#include "sk_tool_utils.h"
20
21/*
22 * A trivial test which benchmarks the performance of a textblob with a single run.
23 */
24class TextBlobBench : public Benchmark {
25public:
26 TextBlobBench()
halcanary96fcdcc2015-08-27 07:41:13 -070027 : fTypeface(nullptr) {
kkinnunenb4a797f2015-05-21 06:15:28 -070028 }
29
30protected:
joshualitt8a6697a2015-09-30 12:11:07 -070031 void onDelayedSetup() override {
caryclark1818acb2015-07-24 12:09:25 -070032 fTypeface.reset(sk_tool_utils::create_portable_typeface("serif", SkTypeface::kNormal));
joshualitteef5b3e2015-04-03 08:07:26 -070033 // make textblob
34 SkPaint paint;
35 paint.setTypeface(fTypeface);
36 const char* text = "Hello blob!";
37 SkTDArray<uint16_t> glyphs;
38 size_t len = strlen(text);
halcanary96fcdcc2015-08-27 07:41:13 -070039 glyphs.append(paint.textToGlyphs(text, len, nullptr));
joshualitteef5b3e2015-04-03 08:07:26 -070040 paint.textToGlyphs(text, len, glyphs.begin());
41
42 SkTextBlobBuilder builder;
43
44 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
45 const SkTextBlobBuilder::RunBuffer& run = builder.allocRun(paint, glyphs.count(), 10, 10,
halcanary96fcdcc2015-08-27 07:41:13 -070046 nullptr);
joshualitteef5b3e2015-04-03 08:07:26 -070047 memcpy(run.glyphs, glyphs.begin(), glyphs.count() * sizeof(uint16_t));
48
49 fBlob.reset(builder.build());
50 }
51
mtkleinf0599002015-07-13 06:18:39 -070052 const char* onGetName() override {
joshualitteef5b3e2015-04-03 08:07:26 -070053 return "TextBlobBench";
54 }
55
mtkleina1ebeb22015-10-01 09:43:39 -070056 void onDraw(int loops, SkCanvas* canvas) override {
joshualitteef5b3e2015-04-03 08:07:26 -070057 SkPaint paint;
58
59 // To ensure maximum caching, we just redraw the blob at the same place everytime
60 for (int i = 0; i < loops; i++) {
61 canvas->drawTextBlob(fBlob, 0, 0, paint);
62 }
63 }
64
65private:
66
67 SkAutoTUnref<const SkTextBlob> fBlob;
68 SkTDArray<uint16_t> fGlyphs;
69 SkAutoTUnref<SkTypeface> fTypeface;
70
71 typedef Benchmark INHERITED;
72};
73
74DEF_BENCH( return new TextBlobBench(); )