commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 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 "gm.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkGraphics.h" |
| 11 | #include "SkTypeface.h" |
| 12 | |
skia.committer@gmail.com | f29c380 | 2013-10-10 07:01:40 +0000 | [diff] [blame] | 13 | // GM to stress the GPU font cache |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 14 | |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 15 | static SkScalar draw_string(SkCanvas* canvas, const SkString& text, SkScalar x, |
| 16 | SkScalar y, const SkPaint& paint) { |
| 17 | canvas->drawText(text.c_str(), text.size(), x, y, paint); |
| 18 | return x + paint.measureText(text.c_str(), text.size()); |
| 19 | } |
| 20 | |
| 21 | class FontCacheGM : public skiagm::GM { |
| 22 | public: |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 23 | FontCacheGM() {} |
skia.committer@gmail.com | f29c380 | 2013-10-10 07:01:40 +0000 | [diff] [blame] | 24 | |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 25 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 26 | SkString onShortName() override { |
jvanverth@google.com | 73d13d7 | 2013-10-09 18:53:35 +0000 | [diff] [blame] | 27 | return SkString("fontcache"); |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 28 | } |
| 29 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 30 | SkISize onISize() override { |
jvanverth | 3f54175 | 2014-09-03 08:44:59 -0700 | [diff] [blame] | 31 | return SkISize::Make(1280, 640); |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 32 | } |
| 33 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 34 | void onOnceBeforeDraw() override { |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame^] | 35 | fTypefaces[0] = sk_tool_utils::create_portable_typeface("serif", |
| 36 | SkFontStyle::FromOldStyle(SkTypeface::kItalic)); |
| 37 | fTypefaces[1] = sk_tool_utils::create_portable_typeface("sans-serif", |
| 38 | SkFontStyle::FromOldStyle(SkTypeface::kItalic)); |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 39 | } |
skia.committer@gmail.com | f29c380 | 2013-10-10 07:01:40 +0000 | [diff] [blame] | 40 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 41 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 42 | SkPaint paint; |
| 43 | paint.setAntiAlias(true); |
| 44 | paint.setLCDRenderText(true); |
| 45 | paint.setSubpixelText(true); |
jvanverth | ce07afb | 2015-02-19 08:28:02 -0800 | [diff] [blame] | 46 | paint.setTypeface(fTypefaces[0]); |
| 47 | paint.setTextSize(192); |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 48 | |
herb | c1e97b3 | 2015-03-05 11:51:11 -0800 | [diff] [blame] | 49 | // Make sure the nul character does not cause problems. |
| 50 | paint.measureText("\0", 1); |
| 51 | |
jvanverth | ce07afb | 2015-02-19 08:28:02 -0800 | [diff] [blame] | 52 | SkScalar x = 20; |
| 53 | SkScalar y = 128; |
| 54 | SkString text("ABCDEFGHIJ"); |
| 55 | draw_string(canvas, text, x, y, paint); |
| 56 | y += 100; |
| 57 | SkString text2("KLMNOPQRS"); |
| 58 | draw_string(canvas, text2, x, y, paint); |
| 59 | y += 100; |
| 60 | SkString text3("TUVWXYZ012"); |
| 61 | draw_string(canvas, text3, x, y, paint); |
| 62 | y += 100; |
| 63 | paint.setTypeface(fTypefaces[1]); |
| 64 | draw_string(canvas, text, x, y, paint); |
| 65 | y += 100; |
| 66 | draw_string(canvas, text2, x, y, paint); |
| 67 | y += 100; |
| 68 | draw_string(canvas, text3, x, y, paint); |
| 69 | y += 100; |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 70 | } |
| 71 | |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 72 | private: |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 73 | sk_sp<SkTypeface> fTypefaces[2]; |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 74 | typedef GM INHERITED; |
| 75 | }; |
| 76 | |
| 77 | |
| 78 | ////////////////////////////////////////////////////////////////////////////// |
| 79 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 80 | DEF_GM(return new FontCacheGM;) |