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