blob: 6d5a88f3183d616a7ab92f75958c959f1baffc16 [file] [log] [blame]
joshualitteef5b3e2015-04-03 08:07:26 -07001
2/*
3 * Copyright 2015 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "Benchmark.h"
10#include "Resources.h"
11#include "SkCanvas.h"
12#include "SkPaint.h"
13#include "SkRandom.h"
14#include "SkStream.h"
15#include "SkString.h"
16#include "SkTemplates.h"
17#include "SkTextBlob.h"
18#include "SkTypeface.h"
19
20#include "sk_tool_utils.h"
21
22/*
23 * A trivial test which benchmarks the performance of a textblob with a single run.
24 */
25class TextBlobBench : public Benchmark {
26public:
27 TextBlobBench()
halcanary96fcdcc2015-08-27 07:41:13 -070028 : fTypeface(nullptr) {
kkinnunenb4a797f2015-05-21 06:15:28 -070029 }
30
31protected:
joshualitt8a6697a2015-09-30 12:11:07 -070032 void onDelayedSetup() override {
caryclark1818acb2015-07-24 12:09:25 -070033 fTypeface.reset(sk_tool_utils::create_portable_typeface("serif", SkTypeface::kNormal));
joshualitteef5b3e2015-04-03 08:07:26 -070034 // make textblob
35 SkPaint paint;
36 paint.setTypeface(fTypeface);
37 const char* text = "Hello blob!";
38 SkTDArray<uint16_t> glyphs;
39 size_t len = strlen(text);
halcanary96fcdcc2015-08-27 07:41:13 -070040 glyphs.append(paint.textToGlyphs(text, len, nullptr));
joshualitteef5b3e2015-04-03 08:07:26 -070041 paint.textToGlyphs(text, len, glyphs.begin());
42
43 SkTextBlobBuilder builder;
44
45 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
46 const SkTextBlobBuilder::RunBuffer& run = builder.allocRun(paint, glyphs.count(), 10, 10,
halcanary96fcdcc2015-08-27 07:41:13 -070047 nullptr);
joshualitteef5b3e2015-04-03 08:07:26 -070048 memcpy(run.glyphs, glyphs.begin(), glyphs.count() * sizeof(uint16_t));
49
50 fBlob.reset(builder.build());
51 }
52
mtkleinf0599002015-07-13 06:18:39 -070053 const char* onGetName() override {
joshualitteef5b3e2015-04-03 08:07:26 -070054 return "TextBlobBench";
55 }
56
mtkleina1ebeb22015-10-01 09:43:39 -070057 void onDraw(int loops, SkCanvas* canvas) override {
joshualitteef5b3e2015-04-03 08:07:26 -070058 SkPaint paint;
59
60 // To ensure maximum caching, we just redraw the blob at the same place everytime
61 for (int i = 0; i < loops; i++) {
62 canvas->drawTextBlob(fBlob, 0, 0, paint);
63 }
64 }
65
66private:
67
68 SkAutoTUnref<const SkTextBlob> fBlob;
69 SkTDArray<uint16_t> fGlyphs;
70 SkAutoTUnref<SkTypeface> fTypeface;
71
72 typedef Benchmark INHERITED;
73};
74
75DEF_BENCH( return new TextBlobBench(); )