OLD | NEW |
---|---|
(Empty) | |
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 "SkCanvas.h" | |
10 #include "SkGraphics.h" | |
11 #include "SkTypeface.h" | |
12 | |
13 // GM to stress the GPU font cache | |
14 | |
15 const char* gFamilyNames[] = { | |
16 "sans-serif", "serif", "monospace" | |
17 }; | |
18 | |
19 const SkTypeface::Style gStyles[] = { | |
20 SkTypeface::kNormal, SkTypeface::kItalic | |
21 }; | |
22 | |
robertphillips
2013/10/09 15:23:49
Is "20" missing on purpose?
| |
23 const int gTextSizes[] = { | |
24 10, 12, 14, 16, 18, 22, 24, 26, 28, 30 | |
25 }; | |
26 | |
robertphillips
2013/10/09 15:23:49
DrawString?
| |
27 static SkScalar drawString(SkCanvas* canvas, const SkString& text, SkScalar x, | |
28 SkScalar y, const SkPaint& paint) { | |
29 canvas->drawText(text.c_str(), text.size(), x, y, paint); | |
30 return x + paint.measureText(text.c_str(), text.size()); | |
31 } | |
32 | |
33 class FontCacheGM : public skiagm::GM { | |
34 public: | |
35 FontCacheGM() { | |
robertphillips
2013/10/09 15:23:49
Is this init expensive? Should it be moved to onOn
| |
36 fTypefaceCount = 0; | |
robertphillips
2013/10/09 15:23:49
Where does 16*1024*1024 come from?
| |
37 SkGraphics::SetFontCacheLimit(16 * 1024 * 1024); | |
robertphillips
2013/10/09 15:23:49
should "unsigned long" just be size_t?
| |
38 for (unsigned long i = 0; i < SK_ARRAY_COUNT(gFamilyNames); ++i) { | |
39 for (unsigned long j = 0; j < SK_ARRAY_COUNT(gStyles); ++j) { | |
40 fTypefaces[fTypefaceCount++] = SkTypeface::CreateFromName(gFamil yNames[i], | |
41 gStyle s[j]); | |
42 } | |
43 } | |
44 fName.set("fontcache"); | |
45 } | |
46 | |
robertphillips
2013/10/09 15:23:49
virtual?
| |
47 ~FontCacheGM() { | |
48 for (size_t i = 0; i < fTypefaceCount; ++i) { | |
49 SkSafeUnref(fTypefaces[i]); | |
50 } | |
51 } | |
52 | |
53 protected: | |
robertphillips
2013/10/09 15:23:49
override
| |
54 virtual SkString onShortName() { | |
55 return fName; | |
56 } | |
57 | |
robertphillips
2013/10/09 15:23:49
override
| |
58 virtual SkISize onISize() { | |
59 return SkISize::Make(640, 320); | |
60 } | |
61 | |
62 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { | |
63 SkScalar y = 32; | |
64 SkPaint paint; | |
65 paint.setAntiAlias(true); | |
66 paint.setLCDRenderText(true); | |
67 paint.setSubpixelText(true); | |
68 | |
69 SkString text("Ham"); | |
70 | |
71 // draw some initial text to partially fill the GPU cache | |
72 for (size_t i = 0; i < 2; ++i) { | |
73 paint.setTypeface(fTypefaces[i]); | |
74 SkScalar x = 20; | |
75 | |
76 for (size_t j = 0; j < SK_ARRAY_COUNT(gTextSizes); ++j) { | |
77 paint.setTextSize(gTextSizes[j]); | |
78 x = drawString(canvas, text, x, y, paint) + 20; | |
79 } | |
80 y += 32; | |
81 } | |
82 | |
83 // force a flush | |
84 canvas->flush(); | |
85 | |
86 // draw again, and more to overflow the cache | |
87 for (size_t i = 0; i < fTypefaceCount; ++i) { | |
88 paint.setTypeface(fTypefaces[i]); | |
89 SkScalar x = 20; | |
90 | |
91 for (size_t j = 0; j < SK_ARRAY_COUNT(gTextSizes); ++j) { | |
92 paint.setTextSize(gTextSizes[j]); | |
93 x = drawString(canvas, text, x, y, paint) + 20; | |
94 } | |
95 y += 32; | |
96 } | |
97 | |
98 } | |
99 | |
100 virtual uint32_t onGetFlags() const SK_OVERRIDE { | |
101 // this GM is meant only for the GPU | |
102 return kGPUOnly_Flag; | |
103 } | |
104 | |
105 private: | |
106 SkTypeface* fTypefaces[SK_ARRAY_COUNT(gFamilyNames)*SK_ARRAY_COUNT(gStyles)] ; | |
robertphillips
2013/10/09 15:23:49
Technically I don't think fTypefaceCount needs to
| |
107 size_t fTypefaceCount; | |
108 SkString fName; | |
109 typedef GM INHERITED; | |
110 }; | |
111 | |
112 | |
113 ////////////////////////////////////////////////////////////////////////////// | |
114 | |
115 DEF_GM( return SkNEW(FontCacheGM); ) | |
116 | |
OLD | NEW |