joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 24 | class TextBlobBench : public Benchmark { |
| 25 | public: |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 26 | TextBlobBench() {} |
kkinnunen | b4a797f | 2015-05-21 06:15:28 -0700 | [diff] [blame] | 27 | |
| 28 | protected: |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 29 | void onDelayedSetup() override { |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 30 | fTypeface = sk_tool_utils::create_portable_typeface("serif", SkFontStyle()); |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 31 | // make textblob |
| 32 | SkPaint paint; |
| 33 | paint.setTypeface(fTypeface); |
| 34 | const char* text = "Hello blob!"; |
| 35 | SkTDArray<uint16_t> glyphs; |
| 36 | size_t len = strlen(text); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 37 | glyphs.append(paint.textToGlyphs(text, len, nullptr)); |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 38 | paint.textToGlyphs(text, len, glyphs.begin()); |
| 39 | |
| 40 | SkTextBlobBuilder builder; |
| 41 | |
| 42 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 43 | const SkTextBlobBuilder::RunBuffer& run = builder.allocRun(paint, glyphs.count(), 10, 10, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 44 | nullptr); |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 45 | memcpy(run.glyphs, glyphs.begin(), glyphs.count() * sizeof(uint16_t)); |
| 46 | |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 47 | fBlob = builder.make(); |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 48 | } |
| 49 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 50 | const char* onGetName() override { |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 51 | return "TextBlobBench"; |
| 52 | } |
| 53 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 54 | void onDraw(int loops, SkCanvas* canvas) override { |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 55 | SkPaint paint; |
| 56 | |
| 57 | // To ensure maximum caching, we just redraw the blob at the same place everytime |
| 58 | for (int i = 0; i < loops; i++) { |
| 59 | canvas->drawTextBlob(fBlob, 0, 0, paint); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | private: |
| 64 | |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 65 | sk_sp<SkTextBlob> fBlob; |
| 66 | SkTDArray<uint16_t> fGlyphs; |
| 67 | sk_sp<SkTypeface> fTypeface; |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 68 | |
| 69 | typedef Benchmark INHERITED; |
| 70 | }; |
| 71 | |
| 72 | DEF_BENCH( return new TextBlobBench(); ) |