• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "tools/ToolUtils.h"
9 
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkFontMgr.h"
12 #include "include/core/SkGraphics.h"
13 #include "include/core/SkPaint.h"
14 #include "include/core/SkPoint.h"
15 #include "include/core/SkSurface.h"
16 #include "include/core/SkTextBlob.h"
17 #include "include/core/SkTypeface.h"
18 #include "src/core/SkGlyphRun.h"
19 #include "tools/fonts/RandomScalerContext.h"
20 
21 #ifdef SK_BUILD_FOR_WIN
22     #include "include/ports/SkTypeface_win.h"
23 #endif
24 
25 #include "tests/Test.h"
26 
27 #include "include/gpu/GrContext.h"
28 #include "src/gpu/GrContextPriv.h"
29 
draw(SkCanvas * canvas,int redraw,const SkTArray<sk_sp<SkTextBlob>> & blobs)30 static void draw(SkCanvas* canvas, int redraw, const SkTArray<sk_sp<SkTextBlob>>& blobs) {
31     int yOffset = 0;
32     for (int r = 0; r < redraw; r++) {
33         for (int i = 0; i < blobs.count(); i++) {
34             const auto& blob = blobs[i];
35             const SkRect& bounds = blob->bounds();
36             yOffset += SkScalarCeilToInt(bounds.height());
37             SkPaint paint;
38             canvas->drawTextBlob(blob, 0, SkIntToScalar(yOffset), paint);
39         }
40     }
41 }
42 
43 static const int kWidth = 1024;
44 static const int kHeight = 768;
45 
setup_always_evict_atlas(GrContext * context)46 static void setup_always_evict_atlas(GrContext* context) {
47     context->priv().getAtlasManager()->setAtlasSizesToMinimum_ForTesting();
48 }
49 
50 // This test hammers the GPU textblobcache and font atlas
text_blob_cache_inner(skiatest::Reporter * reporter,GrContext * context,int maxTotalText,int maxGlyphID,int maxFamilies,bool normal,bool stressTest)51 static void text_blob_cache_inner(skiatest::Reporter* reporter, GrContext* context,
52                                   int maxTotalText, int maxGlyphID, int maxFamilies, bool normal,
53                                   bool stressTest) {
54     // setup surface
55     uint32_t flags = 0;
56     SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
57 
58     // configure our context for maximum stressing of cache and atlas
59     if (stressTest) {
60         setup_always_evict_atlas(context);
61         context->priv().testingOnly_setTextBlobCacheLimit(0);
62     }
63 
64     SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, kRGBA_8888_SkColorType,
65                                          kPremul_SkAlphaType);
66     auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, &props));
67     REPORTER_ASSERT(reporter, surface);
68     if (!surface) {
69         return;
70     }
71 
72     SkCanvas* canvas = surface->getCanvas();
73 
74     sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
75 
76     int count = SkMin32(fm->countFamilies(), maxFamilies);
77 
78     // make a ton of text
79     SkAutoTArray<uint16_t> text(maxTotalText);
80     for (int i = 0; i < maxTotalText; i++) {
81         text[i] = i % maxGlyphID;
82     }
83 
84     // generate textblobs
85     SkTArray<sk_sp<SkTextBlob>> blobs;
86     for (int i = 0; i < count; i++) {
87         SkFont font;
88         font.setSize(48); // draw big glyphs to really stress the atlas
89 
90         SkString familyName;
91         fm->getFamilyName(i, &familyName);
92         sk_sp<SkFontStyleSet> set(fm->createStyleSet(i));
93         for (int j = 0; j < set->count(); ++j) {
94             SkFontStyle fs;
95             set->getStyle(j, &fs, nullptr);
96 
97             // We use a typeface which randomy returns unexpected mask formats to fuzz
98             sk_sp<SkTypeface> orig(set->createTypeface(j));
99             if (normal) {
100                 font.setTypeface(orig);
101             } else {
102                 font.setTypeface(sk_make_sp<SkRandomTypeface>(orig, SkPaint(), true));
103             }
104 
105             SkTextBlobBuilder builder;
106             for (int aa = 0; aa < 2; aa++) {
107                 for (int subpixel = 0; subpixel < 2; subpixel++) {
108                     for (int lcd = 0; lcd < 2; lcd++) {
109                         font.setEdging(SkFont::Edging::kAlias);
110                         if (aa) {
111                             font.setEdging(SkFont::Edging::kAntiAlias);
112                             if (lcd) {
113                                 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
114                             }
115                         }
116                         font.setSubpixel(SkToBool(subpixel));
117                         if (!SkToBool(lcd)) {
118                             font.setSize(160);
119                         }
120                         const SkTextBlobBuilder::RunBuffer& run = builder.allocRun(font,
121                                                                                    maxTotalText,
122                                                                                    0, 0,
123                                                                                    nullptr);
124                         memcpy(run.glyphs, text.get(), maxTotalText * sizeof(uint16_t));
125                     }
126                 }
127             }
128             blobs.emplace_back(builder.make());
129         }
130     }
131 
132     // create surface where LCD is impossible
133     info = SkImageInfo::Make(kWidth, kHeight, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
134     SkSurfaceProps propsNoLCD(0, kUnknown_SkPixelGeometry);
135     auto surfaceNoLCD(canvas->makeSurface(info, &propsNoLCD));
136     REPORTER_ASSERT(reporter, surface);
137     if (!surface) {
138         return;
139     }
140 
141     SkCanvas* canvasNoLCD = surfaceNoLCD->getCanvas();
142 
143     // test redraw
144     draw(canvas, 2, blobs);
145     draw(canvasNoLCD, 2, blobs);
146 
147     // test draw after free
148     context->freeGpuResources();
149     draw(canvas, 1, blobs);
150 
151     context->freeGpuResources();
152     draw(canvasNoLCD, 1, blobs);
153 
154     // test draw after abandon
155     context->abandonContext();
156     draw(canvas, 1, blobs);
157 }
158 
DEF_GPUTEST_FOR_MOCK_CONTEXT(TextBlobCache,reporter,ctxInfo)159 DEF_GPUTEST_FOR_MOCK_CONTEXT(TextBlobCache, reporter, ctxInfo) {
160     text_blob_cache_inner(reporter, ctxInfo.grContext(), 1024, 256, 30, true, false);
161 }
162 
DEF_GPUTEST_FOR_MOCK_CONTEXT(TextBlobStressCache,reporter,ctxInfo)163 DEF_GPUTEST_FOR_MOCK_CONTEXT(TextBlobStressCache, reporter, ctxInfo) {
164     text_blob_cache_inner(reporter, ctxInfo.grContext(), 256, 256, 10, true, true);
165 }
166 
DEF_GPUTEST_FOR_MOCK_CONTEXT(TextBlobAbnormal,reporter,ctxInfo)167 DEF_GPUTEST_FOR_MOCK_CONTEXT(TextBlobAbnormal, reporter, ctxInfo) {
168     text_blob_cache_inner(reporter, ctxInfo.grContext(), 256, 256, 10, false, false);
169 }
170 
DEF_GPUTEST_FOR_MOCK_CONTEXT(TextBlobStressAbnormal,reporter,ctxInfo)171 DEF_GPUTEST_FOR_MOCK_CONTEXT(TextBlobStressAbnormal, reporter, ctxInfo) {
172     text_blob_cache_inner(reporter, ctxInfo.grContext(), 256, 256, 10, false, true);
173 }
174