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 "sk_tool_utils.h"
10#include "SkCanvas.h"
11#include "SkGraphics.h"
12#include "SkTypeface.h"
13
14// GM to stress the GPU font cache
15
16static 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:
24    FontCacheGM() {}
25
26protected:
27    SkString onShortName() override {
28        return SkString("fontcache");
29    }
30
31    SkISize onISize() override {
32        return SkISize::Make(1280, 640);
33    }
34
35    void onOnceBeforeDraw() override {
36        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));
40    }
41
42    void onDraw(SkCanvas* canvas) override {
43        SkPaint paint;
44        paint.setAntiAlias(true);
45        paint.setLCDRenderText(true);
46        paint.setSubpixelText(true);
47        paint.setTypeface(fTypefaces[0]);
48        paint.setTextSize(192);
49
50        // Make sure the nul character does not cause problems.
51        paint.measureText("\0", 1);
52
53        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;
71    }
72
73private:
74    sk_sp<SkTypeface> fTypefaces[2];
75    typedef GM INHERITED;
76};
77
78
79//////////////////////////////////////////////////////////////////////////////
80
81DEF_GM(return new FontCacheGM;)
82