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: |
| 23 | FontCacheGM() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 24 | fTypefaces[0] = nullptr; |
| 25 | fTypefaces[1] = nullptr; |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | virtual ~FontCacheGM() { |
jvanverth | ce07afb | 2015-02-19 08:28:02 -0800 | [diff] [blame] | 29 | SkSafeUnref(fTypefaces[0]); |
| 30 | SkSafeUnref(fTypefaces[1]); |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 31 | } |
skia.committer@gmail.com | f29c380 | 2013-10-10 07:01:40 +0000 | [diff] [blame] | 32 | |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 33 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 34 | SkString onShortName() override { |
jvanverth@google.com | 73d13d7 | 2013-10-09 18:53:35 +0000 | [diff] [blame] | 35 | return SkString("fontcache"); |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 36 | } |
| 37 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 38 | SkISize onISize() override { |
jvanverth | 3f54175 | 2014-09-03 08:44:59 -0700 | [diff] [blame] | 39 | return SkISize::Make(1280, 640); |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 40 | } |
| 41 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 42 | void onOnceBeforeDraw() override { |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 43 | fTypefaces[0] = sk_tool_utils::create_portable_typeface("serif", SkTypeface::kItalic); |
| 44 | fTypefaces[1] = sk_tool_utils::create_portable_typeface("sans-serif", SkTypeface::kItalic); |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 45 | } |
skia.committer@gmail.com | f29c380 | 2013-10-10 07:01:40 +0000 | [diff] [blame] | 46 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 47 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 48 | SkPaint paint; |
| 49 | paint.setAntiAlias(true); |
| 50 | paint.setLCDRenderText(true); |
| 51 | paint.setSubpixelText(true); |
jvanverth | ce07afb | 2015-02-19 08:28:02 -0800 | [diff] [blame] | 52 | paint.setTypeface(fTypefaces[0]); |
| 53 | paint.setTextSize(192); |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 54 | |
herb | c1e97b3 | 2015-03-05 11:51:11 -0800 | [diff] [blame] | 55 | // Make sure the nul character does not cause problems. |
| 56 | paint.measureText("\0", 1); |
| 57 | |
jvanverth | ce07afb | 2015-02-19 08:28:02 -0800 | [diff] [blame] | 58 | SkScalar x = 20; |
| 59 | SkScalar y = 128; |
| 60 | SkString text("ABCDEFGHIJ"); |
| 61 | draw_string(canvas, text, x, y, paint); |
| 62 | y += 100; |
| 63 | SkString text2("KLMNOPQRS"); |
| 64 | draw_string(canvas, text2, x, y, paint); |
| 65 | y += 100; |
| 66 | SkString text3("TUVWXYZ012"); |
| 67 | draw_string(canvas, text3, x, y, paint); |
| 68 | y += 100; |
| 69 | paint.setTypeface(fTypefaces[1]); |
| 70 | draw_string(canvas, text, x, y, paint); |
| 71 | y += 100; |
| 72 | draw_string(canvas, text2, x, y, paint); |
| 73 | y += 100; |
| 74 | draw_string(canvas, text3, x, y, paint); |
| 75 | y += 100; |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 76 | } |
| 77 | |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 78 | private: |
jvanverth | ce07afb | 2015-02-19 08:28:02 -0800 | [diff] [blame] | 79 | SkTypeface* fTypefaces[2]; |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 80 | typedef GM INHERITED; |
| 81 | }; |
| 82 | |
| 83 | |
| 84 | ////////////////////////////////////////////////////////////////////////////// |
| 85 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 86 | DEF_GM(return new FontCacheGM;) |