blob: 22b29f47aba2fcfcbe8ff706030621453dfebfac [file] [log] [blame]
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +00001/*
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.comf29c3802013-10-10 07:01:40 +000013// GM to stress the GPU font cache
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000014
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000015static 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
21class FontCacheGM : public skiagm::GM {
22public:
bungeman13b9c952016-05-12 10:09:30 -070023 FontCacheGM() {}
skia.committer@gmail.comf29c3802013-10-10 07:01:40 +000024
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000025protected:
mtklein36352bf2015-03-25 18:17:31 -070026 SkString onShortName() override {
jvanverth@google.com73d13d72013-10-09 18:53:35 +000027 return SkString("fontcache");
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000028 }
29
mtklein36352bf2015-03-25 18:17:31 -070030 SkISize onISize() override {
jvanverth3f541752014-09-03 08:44:59 -070031 return SkISize::Make(1280, 640);
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000032 }
33
mtklein36352bf2015-03-25 18:17:31 -070034 void onOnceBeforeDraw() override {
mbocee6a9912016-05-31 11:42:36 -070035 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.org338a49f2013-10-09 18:12:23 +000039 }
skia.committer@gmail.comf29c3802013-10-10 07:01:40 +000040
mtklein36352bf2015-03-25 18:17:31 -070041 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000042 SkPaint paint;
43 paint.setAntiAlias(true);
44 paint.setLCDRenderText(true);
45 paint.setSubpixelText(true);
jvanverthce07afb2015-02-19 08:28:02 -080046 paint.setTypeface(fTypefaces[0]);
47 paint.setTextSize(192);
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000048
herbc1e97b32015-03-05 11:51:11 -080049 // Make sure the nul character does not cause problems.
50 paint.measureText("\0", 1);
51
jvanverthce07afb2015-02-19 08:28:02 -080052 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.org338a49f2013-10-09 18:12:23 +000070 }
71
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000072private:
bungeman13b9c952016-05-12 10:09:30 -070073 sk_sp<SkTypeface> fTypefaces[2];
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000074 typedef GM INHERITED;
75};
76
77
78//////////////////////////////////////////////////////////////////////////////
79
halcanary385fe4d2015-08-26 13:07:48 -070080DEF_GM(return new FontCacheGM;)