blob: 8775b58220dfc09253d3659cddf3cea7bf5a6352 [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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000010#include "SkCanvas.h"
11#include "SkGraphics.h"
12#include "SkTypeface.h"
13
skia.committer@gmail.comf29c3802013-10-10 07:01:40 +000014// GM to stress the GPU font cache
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000015
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000016static 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
22class FontCacheGM : public skiagm::GM {
23public:
bungeman13b9c952016-05-12 10:09:30 -070024 FontCacheGM() {}
skia.committer@gmail.comf29c3802013-10-10 07:01:40 +000025
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000026protected:
mtklein36352bf2015-03-25 18:17:31 -070027 SkString onShortName() override {
jvanverth@google.com73d13d72013-10-09 18:53:35 +000028 return SkString("fontcache");
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000029 }
30
mtklein36352bf2015-03-25 18:17:31 -070031 SkISize onISize() override {
jvanverth3f541752014-09-03 08:44:59 -070032 return SkISize::Make(1280, 640);
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000033 }
34
mtklein36352bf2015-03-25 18:17:31 -070035 void onOnceBeforeDraw() override {
mbocee6a9912016-05-31 11:42:36 -070036 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.org338a49f2013-10-09 18:12:23 +000040 }
skia.committer@gmail.comf29c3802013-10-10 07:01:40 +000041
mtklein36352bf2015-03-25 18:17:31 -070042 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000043 SkPaint paint;
44 paint.setAntiAlias(true);
45 paint.setLCDRenderText(true);
46 paint.setSubpixelText(true);
jvanverthce07afb2015-02-19 08:28:02 -080047 paint.setTypeface(fTypefaces[0]);
48 paint.setTextSize(192);
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000049
herbc1e97b32015-03-05 11:51:11 -080050 // Make sure the nul character does not cause problems.
51 paint.measureText("\0", 1);
52
jvanverthce07afb2015-02-19 08:28:02 -080053 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.org338a49f2013-10-09 18:12:23 +000071 }
72
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000073private:
bungeman13b9c952016-05-12 10:09:30 -070074 sk_sp<SkTypeface> fTypefaces[2];
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000075 typedef GM INHERITED;
76};
77
78
79//////////////////////////////////////////////////////////////////////////////
80
halcanary385fe4d2015-08-26 13:07:48 -070081DEF_GM(return new FontCacheGM;)