OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2015 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 "sk_tool_utils.h" | |
9 #include "SkCanvas.h" | |
10 #include "SkPaint.h" | |
11 #include "SkPoint.h" | |
12 #include "SkTextBlob.h" | |
13 #include "SkFontMgr.h" | |
14 #include "SkGraphics.h" | |
15 #include "SkSurface.h" | |
16 #include "SkTypeface.h" | |
17 | |
18 #ifdef SK_BUILD_FOR_WIN | |
19 #include "SkTypeface_win.h" | |
20 #endif | |
21 | |
22 #include "Test.h" | |
23 | |
24 #if SK_SUPPORT_GPU | |
25 #include "GrContextFactory.h" | |
26 | |
27 struct TextBlobWrapper { | |
28 explicit TextBlobWrapper(const SkTextBlob* blob) : fBlob(SkRef(blob)) {} | |
29 TextBlobWrapper(const TextBlobWrapper& blob) : fBlob(SkRef(blob.fBlob.get()) ) {} | |
30 | |
31 SkAutoTUnref<const SkTextBlob> fBlob; | |
32 }; | |
33 | |
34 static void draw(SkCanvas* canvas, int redraw, const SkTArray<TextBlobWrapper>& blobs) { | |
35 for (int r = 0; r < redraw; r++) { | |
36 for (int i = 0; i < blobs.count(); i++) { | |
37 SkPaint paint; | |
38 canvas->drawTextBlob(blobs[i].fBlob.get(), 0, 0, paint); | |
39 } | |
40 } | |
41 } | |
42 | |
43 // limit this just so we don't take too long to draw | |
44 #define MAX_TOTAL_TEXT 4096 | |
45 #define MAX_CHAR 256 | |
46 #define MAX_FAMILIES 5 | |
47 DEF_TEST(TextBlobCache, reporter) { | |
48 SkAutoTDelete<GrContextFactory> grFactory(SkNEW(GrContextFactory)); | |
49 | |
50 // setup surface | |
51 uint32_t flags = 0; | |
52 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType); | |
53 | |
54 GrContext* ctx = grFactory->get(GrContextFactory::kNative_GLContextType); | |
55 SkImageInfo info = SkImageInfo::Make(1024, 768, kN32_SkColorType, kPremul_Sk AlphaType); | |
56 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(ctx, SkSurface::k No_Budgeted, info, | |
57 0, &props)); | |
58 REPORTER_ASSERT(reporter, surface); | |
59 if (!surface) { | |
60 return; | |
61 } | |
62 | |
63 SkCanvas* canvas = surface->getCanvas(); | |
64 | |
65 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); | |
66 | |
67 int count = SkMin32(fm->countFamilies(), MAX_FAMILIES); | |
68 | |
69 // make a ton of text | |
70 uint16_t text[MAX_TOTAL_TEXT]; | |
71 for (int i = 0; i < MAX_TOTAL_TEXT; i++) { | |
72 text[i] = i % MAX_CHAR; | |
73 } | |
74 | |
75 // generate textblobs | |
76 SkTArray<TextBlobWrapper> blobs; | |
77 for (int i = 0; i < count; i++) { | |
78 SkPaint paint; | |
79 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); | |
80 paint.setTextSize(256); // draw big glyphs to really stress the atlas | |
81 | |
82 SkString familyName; | |
83 fm->getFamilyName(i, &familyName); | |
84 SkAutoTUnref<SkFontStyleSet> set(fm->createStyleSet(i)); | |
85 for (int j = 0; j < set->count(); ++j) { | |
86 SkString sname; | |
bungeman-skia
2015/07/17 19:07:57
Doesn't look like you're using anything in this bl
| |
87 SkFontStyle fs; | |
88 set->getStyle(j, &fs, &sname); | |
89 sname.appendf(" [%d %d %d]", fs.weight(), fs.width(), fs.isItalic()) ; | |
90 //SkDebugf("%s\n", sname.c_str()); | |
91 | |
92 SkSafeUnref(paint.setTypeface(set->createTypeface(j))); | |
93 | |
94 SkTextBlobBuilder builder; | |
95 for (int aa = 0; aa < 2; aa++) { | |
96 for (int subpixel = 0; subpixel < 2; subpixel++) { | |
97 for (int lcd = 0; lcd < 2; lcd++) { | |
98 paint.setAntiAlias(SkToBool(aa)); | |
99 paint.setSubpixelText(SkToBool(subpixel)); | |
100 paint.setLCDRenderText(SkToBool(lcd)); | |
101 const SkTextBlobBuilder::RunBuffer& run = builder.allocR un(paint, | |
102 MAX_TOTAL_TEXT, | |
103 0, 0, | |
104 NULL); | |
105 memcpy(run.glyphs, text, MAX_TOTAL_TEXT * sizeof(uint16_ t)); | |
106 } | |
107 } | |
108 } | |
109 SkNEW_APPEND_TO_TARRAY(&blobs, TextBlobWrapper, (builder.build())); | |
110 } | |
111 } | |
112 | |
113 // test redraw | |
114 draw(canvas, 2, blobs); | |
115 | |
116 // test draw after free | |
117 ctx->freeGpuResources(); | |
118 draw(canvas, 1, blobs); | |
119 | |
120 // test draw after abandon | |
121 ctx->abandonContext(); | |
122 draw(canvas, 1, blobs); | |
123 } | |
124 #endif | |
OLD | NEW |