blob: 5f1141e1e9053271353df11a8c07811bfb59f51d [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:
23 FontCacheGM() {
jvanverthce07afb2015-02-19 08:28:02 -080024 fTypefaces[0] = NULL;
25 fTypefaces[1] = NULL;
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000026 }
27
28 virtual ~FontCacheGM() {
jvanverthce07afb2015-02-19 08:28:02 -080029 SkSafeUnref(fTypefaces[0]);
30 SkSafeUnref(fTypefaces[1]);
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000031 }
skia.committer@gmail.comf29c3802013-10-10 07:01:40 +000032
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000033protected:
mtklein36352bf2015-03-25 18:17:31 -070034 SkString onShortName() override {
jvanverth@google.com73d13d72013-10-09 18:53:35 +000035 return SkString("fontcache");
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000036 }
37
mtklein36352bf2015-03-25 18:17:31 -070038 SkISize onISize() override {
jvanverth3f541752014-09-03 08:44:59 -070039 return SkISize::Make(1280, 640);
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000040 }
41
mtklein36352bf2015-03-25 18:17:31 -070042 void onOnceBeforeDraw() override {
jvanverthce07afb2015-02-19 08:28:02 -080043 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.org338a49f2013-10-09 18:12:23 +000045 }
skia.committer@gmail.comf29c3802013-10-10 07:01:40 +000046
mtklein36352bf2015-03-25 18:17:31 -070047 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000048 SkPaint paint;
49 paint.setAntiAlias(true);
50 paint.setLCDRenderText(true);
51 paint.setSubpixelText(true);
jvanverthce07afb2015-02-19 08:28:02 -080052 paint.setTypeface(fTypefaces[0]);
53 paint.setTextSize(192);
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000054
herbc1e97b32015-03-05 11:51:11 -080055 // Make sure the nul character does not cause problems.
56 paint.measureText("\0", 1);
57
jvanverthce07afb2015-02-19 08:28:02 -080058 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.org338a49f2013-10-09 18:12:23 +000076 }
77
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000078private:
jvanverthce07afb2015-02-19 08:28:02 -080079 SkTypeface* fTypefaces[2];
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000080 typedef GM INHERITED;
81};
82
83
84//////////////////////////////////////////////////////////////////////////////
85
86DEF_GM( return SkNEW(FontCacheGM); )