OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "gm.h" | 8 #include "gm.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkGraphics.h" | 10 #include "SkGraphics.h" |
11 #include "SkTypeface.h" | 11 #include "SkTypeface.h" |
12 | 12 |
13 // GM to stress the GPU font cache | 13 // GM to stress the GPU font cache |
14 | 14 |
15 const char* gFamilyNames[] = { | |
16 "sans-serif", "serif" | |
17 }; | |
18 | |
19 const SkTypeface::Style gStyles[] = { | |
20 SkTypeface::kNormal, SkTypeface::kItalic, SkTypeface::kBold | |
21 }; | |
22 | |
23 const SkScalar gTextSizes[] = { | |
24 192, 194, 196, 198, 200, 202, 204, 206 | |
25 }; | |
26 | |
27 #define TYPEFACE_COUNT (SK_ARRAY_COUNT(gFamilyNames)*SK_ARRAY_COUNT(gStyles)) | |
28 | |
29 static SkScalar draw_string(SkCanvas* canvas, const SkString& text, SkScalar x, | 15 static SkScalar draw_string(SkCanvas* canvas, const SkString& text, SkScalar x, |
30 SkScalar y, const SkPaint& paint) { | 16 SkScalar y, const SkPaint& paint) { |
31 canvas->drawText(text.c_str(), text.size(), x, y, paint); | 17 canvas->drawText(text.c_str(), text.size(), x, y, paint); |
32 return x + paint.measureText(text.c_str(), text.size()); | 18 return x + paint.measureText(text.c_str(), text.size()); |
33 } | 19 } |
34 | 20 |
35 class FontCacheGM : public skiagm::GM { | 21 class FontCacheGM : public skiagm::GM { |
36 public: | 22 public: |
37 FontCacheGM() { | 23 FontCacheGM() { |
38 for (size_t i = 0; i < TYPEFACE_COUNT; ++i) { | 24 fTypefaces[0] = NULL; |
39 fTypefaces[i] = NULL; | 25 fTypefaces[1] = NULL; |
40 } | |
41 } | 26 } |
42 | 27 |
43 virtual ~FontCacheGM() { | 28 virtual ~FontCacheGM() { |
44 for (size_t i = 0; i < TYPEFACE_COUNT; ++i) { | 29 SkSafeUnref(fTypefaces[0]); |
45 SkSafeUnref(fTypefaces[i]); | 30 SkSafeUnref(fTypefaces[1]); |
46 } | |
47 } | 31 } |
48 | 32 |
49 protected: | 33 protected: |
50 SkString onShortName() SK_OVERRIDE { | 34 SkString onShortName() SK_OVERRIDE { |
51 return SkString("fontcache"); | 35 return SkString("fontcache"); |
52 } | 36 } |
53 | 37 |
54 SkISize onISize() SK_OVERRIDE { | 38 SkISize onISize() SK_OVERRIDE { |
55 return SkISize::Make(1280, 640); | 39 return SkISize::Make(1280, 640); |
56 } | 40 } |
57 | 41 |
58 void onOnceBeforeDraw() SK_OVERRIDE { | 42 void onOnceBeforeDraw() SK_OVERRIDE { |
59 int typefaceCount = 0; | 43 fTypefaces[0] = sk_tool_utils::create_portable_typeface("serif", SkTypef
ace::kItalic); |
60 for (size_t i = 0; i < SK_ARRAY_COUNT(gFamilyNames); ++i) { | 44 fTypefaces[1] = sk_tool_utils::create_portable_typeface("sans-serif", Sk
Typeface::kItalic); |
61 for (size_t j = 0; j < SK_ARRAY_COUNT(gStyles); ++j) { | |
62 fTypefaces[typefaceCount++] = sk_tool_utils::create_portable_typ
eface(gFamilyNames[i], | |
63 g
Styles[j]); | |
64 } | |
65 } | |
66 } | 45 } |
67 | 46 |
68 void onDraw(SkCanvas* canvas) SK_OVERRIDE { | 47 void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
69 SkScalar y = 32; | |
70 SkPaint paint; | 48 SkPaint paint; |
71 paint.setAntiAlias(true); | 49 paint.setAntiAlias(true); |
72 paint.setLCDRenderText(true); | 50 paint.setLCDRenderText(true); |
73 paint.setSubpixelText(true); | 51 paint.setSubpixelText(true); |
| 52 paint.setTypeface(fTypefaces[0]); |
| 53 paint.setTextSize(192); |
74 | 54 |
75 SkString text("H"); | 55 SkScalar x = 20; |
76 | 56 SkScalar y = 128; |
77 // draw enough to overflow the cache | 57 SkString text("ABCDEFGHIJ"); |
78 for (size_t i = 0; i < TYPEFACE_COUNT; ++i) { | 58 draw_string(canvas, text, x, y, paint); |
79 paint.setTypeface(fTypefaces[i]); | 59 y += 100; |
80 SkScalar x = 20; | 60 SkString text2("KLMNOPQRS"); |
81 | 61 draw_string(canvas, text2, x, y, paint); |
82 for (size_t j = 0; j < SK_ARRAY_COUNT(gTextSizes); ++j) { | 62 y += 100; |
83 paint.setTextSize(gTextSizes[j]); | 63 SkString text3("TUVWXYZ012"); |
84 x = draw_string(canvas, text, x, y, paint) + 10; | 64 draw_string(canvas, text3, x, y, paint); |
85 } | 65 y += 100; |
86 y += 128; | 66 paint.setTypeface(fTypefaces[1]); |
87 } | 67 draw_string(canvas, text, x, y, paint); |
88 | 68 y += 100; |
| 69 draw_string(canvas, text2, x, y, paint); |
| 70 y += 100; |
| 71 draw_string(canvas, text3, x, y, paint); |
| 72 y += 100; |
89 } | 73 } |
90 | 74 |
91 private: | 75 private: |
92 SkTypeface* fTypefaces[TYPEFACE_COUNT]; | 76 SkTypeface* fTypefaces[2]; |
93 typedef GM INHERITED; | 77 typedef GM INHERITED; |
94 }; | 78 }; |
95 | 79 |
96 | 80 |
97 ////////////////////////////////////////////////////////////////////////////// | 81 ////////////////////////////////////////////////////////////////////////////// |
98 | 82 |
99 DEF_GM( return SkNEW(FontCacheGM); ) | 83 DEF_GM( return SkNEW(FontCacheGM); ) |
OLD | NEW |