blob: a8c5b768c3fdcff49af6eb158a2a75bde212eed1 [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) {
Cary Clark2a475ea2017-04-28 15:35:12 -040018 canvas->drawString(text, x, y, paint);
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000019 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 {
Ben Wagner71319502017-07-27 10:45:29 -040036 fTypefaces[0] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Italic());
37 fTypefaces[1] = sk_tool_utils::create_portable_typeface("sans-serif",SkFontStyle::Italic());
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000038 }
skia.committer@gmail.comf29c3802013-10-10 07:01:40 +000039
mtklein36352bf2015-03-25 18:17:31 -070040 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000041 SkPaint paint;
42 paint.setAntiAlias(true);
43 paint.setLCDRenderText(true);
44 paint.setSubpixelText(true);
jvanverthce07afb2015-02-19 08:28:02 -080045 paint.setTypeface(fTypefaces[0]);
46 paint.setTextSize(192);
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000047
herbc1e97b32015-03-05 11:51:11 -080048 // Make sure the nul character does not cause problems.
49 paint.measureText("\0", 1);
50
jvanverthce07afb2015-02-19 08:28:02 -080051 SkScalar x = 20;
52 SkScalar y = 128;
53 SkString text("ABCDEFGHIJ");
54 draw_string(canvas, text, x, y, paint);
55 y += 100;
56 SkString text2("KLMNOPQRS");
57 draw_string(canvas, text2, x, y, paint);
58 y += 100;
59 SkString text3("TUVWXYZ012");
60 draw_string(canvas, text3, x, y, paint);
61 y += 100;
62 paint.setTypeface(fTypefaces[1]);
63 draw_string(canvas, text, x, y, paint);
64 y += 100;
65 draw_string(canvas, text2, x, y, paint);
66 y += 100;
67 draw_string(canvas, text3, x, y, paint);
68 y += 100;
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000069 }
70
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000071private:
bungeman13b9c952016-05-12 10:09:30 -070072 sk_sp<SkTypeface> fTypefaces[2];
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000073 typedef GM INHERITED;
74};
75
76
77//////////////////////////////////////////////////////////////////////////////
78
halcanary385fe4d2015-08-26 13:07:48 -070079DEF_GM(return new FontCacheGM;)