1338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org/*
2338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org * Copyright 2013 Google Inc.
3338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org *
4338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org * Use of this source code is governed by a BSD-style license that can be
5338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org * found in the LICENSE file.
6338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org */
7338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org
8338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org#include "gm.h"
9338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org#include "SkCanvas.h"
10338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org#include "SkGraphics.h"
11338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org#include "SkTypeface.h"
12338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org
13f29c3802899c3e1e285a44140eb32cfa329764dcskia.committer@gmail.com// GM to stress the GPU font cache
14338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org
15338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.orgconst char* gFamilyNames[] = {
16338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org    "sans-serif", "serif", "monospace"
17338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org};
18338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org
19338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.orgconst SkTypeface::Style gStyles[] = {
20338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org    SkTypeface::kNormal, SkTypeface::kItalic
21338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org};
22338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org
23338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.orgconst SkScalar gTextSizes[] = {
24338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org    12, 14, 16, 18, 20, 22, 24, 26, 28, 30
25338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org};
26338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org
27338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org#define TYPEFACE_COUNT (SK_ARRAY_COUNT(gFamilyNames)*SK_ARRAY_COUNT(gStyles))
28338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org
29338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.orgstatic SkScalar draw_string(SkCanvas* canvas, const SkString& text, SkScalar x,
30338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org                           SkScalar y, const SkPaint& paint) {
31338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org    canvas->drawText(text.c_str(), text.size(), x, y, paint);
32338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org    return x + paint.measureText(text.c_str(), text.size());
33338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org}
34338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org
35338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.orgclass FontCacheGM : public skiagm::GM {
36338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.orgpublic:
37338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org    FontCacheGM() {
38338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org        for (size_t i = 0; i < TYPEFACE_COUNT; ++i) {
39338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org            fTypefaces[i] = NULL;
40338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org        }
41338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org    }
42338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org
43338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org    virtual ~FontCacheGM() {
44338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org        for (size_t i = 0; i < TYPEFACE_COUNT; ++i) {
45338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org            SkSafeUnref(fTypefaces[i]);
46338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org        }
47338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org    }
48f29c3802899c3e1e285a44140eb32cfa329764dcskia.committer@gmail.com
49338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.orgprotected:
50338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org    virtual SkString onShortName() SK_OVERRIDE {
5173d13d70dc46849b88b79096772d6f8e32cb5fe2jvanverth@google.com        return SkString("fontcache");
52338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org    }
53338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org
54338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org    virtual SkISize onISize() SK_OVERRIDE {
55338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org        return SkISize::Make(640, 320);
56338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org    }
57338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org
58338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org    virtual void onOnceBeforeDraw() SK_OVERRIDE {
59338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org        int typefaceCount = 0;
60338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org        for (size_t i = 0; i < SK_ARRAY_COUNT(gFamilyNames); ++i) {
61338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org            for (size_t j = 0; j < SK_ARRAY_COUNT(gStyles); ++j) {
62338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org                fTypefaces[typefaceCount++] = SkTypeface::CreateFromName(gFamilyNames[i],
63338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org                                                                          gStyles[j]);
64338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org            }
65338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org        }
66338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org    }
67f29c3802899c3e1e285a44140eb32cfa329764dcskia.committer@gmail.com
68338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
69338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org        SkScalar y = 32;
70338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org        SkPaint paint;
71338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org        paint.setAntiAlias(true);
72338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org        paint.setLCDRenderText(true);
73338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org        paint.setSubpixelText(true);
74338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org
75338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org        SkString text("Ham");
76338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org
77338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org        // draw some initial text to partially fill the GPU cache
78338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org        for (size_t i = 0; i < 2; ++i) {
79338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org            paint.setTypeface(fTypefaces[i]);
80338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org            SkScalar x = 20;
81f29c3802899c3e1e285a44140eb32cfa329764dcskia.committer@gmail.com
82338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org            for (size_t j = 0; j < SK_ARRAY_COUNT(gTextSizes); ++j) {
83338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org                paint.setTextSize(gTextSizes[j]);
84338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org                x = draw_string(canvas, text, x, y, paint) + 19;
85338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org            }
86338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org            y += 32;
87338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org        }
88f29c3802899c3e1e285a44140eb32cfa329764dcskia.committer@gmail.com
89338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org        // force a flush
90338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org        canvas->flush();
91f29c3802899c3e1e285a44140eb32cfa329764dcskia.committer@gmail.com
92338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org        // draw again, and more to overflow the cache
93338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org        for (size_t i = 0; i < TYPEFACE_COUNT; ++i) {
94338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org            paint.setTypeface(fTypefaces[i]);
95338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org            SkScalar x = 20;
96f29c3802899c3e1e285a44140eb32cfa329764dcskia.committer@gmail.com
97338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org            for (size_t j = 0; j < SK_ARRAY_COUNT(gTextSizes); ++j) {
98338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org                paint.setTextSize(gTextSizes[j]);
99338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org                x = draw_string(canvas, text, x, y, paint) + 19;
100338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org            }
101338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org            y += 32;
102338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org        }
103f29c3802899c3e1e285a44140eb32cfa329764dcskia.committer@gmail.com
104338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org    }
105338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org
106338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org    virtual uint32_t onGetFlags() const SK_OVERRIDE {
107338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org        // this GM is meant only for the GPU
108338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org        return kGPUOnly_Flag;
109338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org    }
110338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org
111338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.orgprivate:
112338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org    SkTypeface* fTypefaces[TYPEFACE_COUNT];
113338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org    typedef GM INHERITED;
114338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org};
115338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org
116338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org
117338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org//////////////////////////////////////////////////////////////////////////////
118338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.org
119338a49fc55be8555e171419d079a1b6317060a38commit-bot@chromium.orgDEF_GM( return SkNEW(FontCacheGM); )
120