• 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 <string>
11 
12 #include "include/core/SkBitmap.h"
13 #include "include/core/SkCanvas.h"
14 #include "include/core/SkFontMgr.h"
15 #include "include/core/SkGraphics.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkPoint.h"
18 #include "include/core/SkSurface.h"
19 #include "include/core/SkTextBlob.h"
20 #include "include/core/SkTypeface.h"
21 #include "include/gpu/GrDirectContext.h"
22 #include "src/core/SkGlyphRun.h"
23 #include "src/gpu/GrDirectContextPriv.h"
24 #include "tools/fonts/RandomScalerContext.h"
25 
26 #ifdef SK_BUILD_FOR_WIN
27     #include "include/ports/SkTypeface_win.h"
28 #endif
29 
30 #include "tests/Test.h"
31 
32 #include "src/gpu/GrDirectContextPriv.h"
33 #include "src/gpu/text/GrAtlasManager.h"
34 #include "src/gpu/text/GrTextBlobCache.h"
35 
draw(SkCanvas * canvas,int redraw,const SkTArray<sk_sp<SkTextBlob>> & blobs)36 static void draw(SkCanvas* canvas, int redraw, const SkTArray<sk_sp<SkTextBlob>>& blobs) {
37     int yOffset = 0;
38     for (int r = 0; r < redraw; r++) {
39         for (int i = 0; i < blobs.count(); i++) {
40             const auto& blob = blobs[i];
41             const SkRect& bounds = blob->bounds();
42             yOffset += SkScalarCeilToInt(bounds.height());
43             SkPaint paint;
44             canvas->drawTextBlob(blob, 0, SkIntToScalar(yOffset), paint);
45         }
46     }
47 }
48 
49 static const int kWidth = 1024;
50 static const int kHeight = 768;
51 
setup_always_evict_atlas(GrDirectContext * dContext)52 static void setup_always_evict_atlas(GrDirectContext* dContext) {
53     dContext->priv().getAtlasManager()->setAtlasDimensionsToMinimum_ForTesting();
54 }
55 
56 class GrTextBlobTestingPeer {
57 public:
SetBudget(GrTextBlobCache * cache,size_t budget)58     static void SetBudget(GrTextBlobCache* cache, size_t budget) {
59         SkAutoSpinlock lock{cache->fSpinLock};
60         cache->fSizeBudget = budget;
61         cache->internalCheckPurge();
62     }
63 };
64 
65 // This test hammers the GPU textblobcache and font atlas
text_blob_cache_inner(skiatest::Reporter * reporter,GrDirectContext * dContext,int maxTotalText,int maxGlyphID,int maxFamilies,bool normal,bool stressTest)66 static void text_blob_cache_inner(skiatest::Reporter* reporter, GrDirectContext* dContext,
67                                   int maxTotalText, int maxGlyphID, int maxFamilies, bool normal,
68                                   bool stressTest) {
69     // setup surface
70     uint32_t flags = 0;
71     SkSurfaceProps props(flags, kRGB_H_SkPixelGeometry);
72 
73     // configure our context for maximum stressing of cache and atlas
74     if (stressTest) {
75         setup_always_evict_atlas(dContext);
76         GrTextBlobTestingPeer::SetBudget(dContext->priv().getTextBlobCache(), 0);
77     }
78 
79     SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, kRGBA_8888_SkColorType,
80                                          kPremul_SkAlphaType);
81     auto surface(SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info, 0, &props));
82     REPORTER_ASSERT(reporter, surface);
83     if (!surface) {
84         return;
85     }
86 
87     SkCanvas* canvas = surface->getCanvas();
88 
89     sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
90 
91     int count = std::min(fm->countFamilies(), maxFamilies);
92 
93     // make a ton of text
94     SkAutoTArray<uint16_t> text(maxTotalText);
95     for (int i = 0; i < maxTotalText; i++) {
96         text[i] = i % maxGlyphID;
97     }
98 
99     // generate textblobs
100     SkTArray<sk_sp<SkTextBlob>> blobs;
101     for (int i = 0; i < count; i++) {
102         SkFont font;
103         font.setSize(48); // draw big glyphs to really stress the atlas
104 
105         SkString familyName;
106         fm->getFamilyName(i, &familyName);
107         sk_sp<SkFontStyleSet> set(fm->createStyleSet(i));
108         for (int j = 0; j < set->count(); ++j) {
109             SkFontStyle fs;
110             set->getStyle(j, &fs, nullptr);
111 
112             // We use a typeface which randomy returns unexpected mask formats to fuzz
113             sk_sp<SkTypeface> orig(set->createTypeface(j));
114             if (normal) {
115                 font.setTypeface(orig);
116             } else {
117                 font.setTypeface(sk_make_sp<SkRandomTypeface>(orig, SkPaint(), true));
118             }
119 
120             SkTextBlobBuilder builder;
121             for (int aa = 0; aa < 2; aa++) {
122                 for (int subpixel = 0; subpixel < 2; subpixel++) {
123                     for (int lcd = 0; lcd < 2; lcd++) {
124                         font.setEdging(SkFont::Edging::kAlias);
125                         if (aa) {
126                             font.setEdging(SkFont::Edging::kAntiAlias);
127                             if (lcd) {
128                                 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
129                             }
130                         }
131                         font.setSubpixel(SkToBool(subpixel));
132                         if (!SkToBool(lcd)) {
133                             font.setSize(160);
134                         }
135                         const SkTextBlobBuilder::RunBuffer& run = builder.allocRun(font,
136                                                                                    maxTotalText,
137                                                                                    0, 0,
138                                                                                    nullptr);
139                         memcpy(run.glyphs, text.get(), maxTotalText * sizeof(uint16_t));
140                     }
141                 }
142             }
143             blobs.emplace_back(builder.make());
144         }
145     }
146 
147     // create surface where LCD is impossible
148     info = SkImageInfo::Make(kWidth, kHeight, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
149     SkSurfaceProps propsNoLCD(0, kUnknown_SkPixelGeometry);
150     auto surfaceNoLCD(canvas->makeSurface(info, &propsNoLCD));
151     REPORTER_ASSERT(reporter, surface);
152     if (!surface) {
153         return;
154     }
155 
156     SkCanvas* canvasNoLCD = surfaceNoLCD->getCanvas();
157 
158     // test redraw
159     draw(canvas, 2, blobs);
160     draw(canvasNoLCD, 2, blobs);
161 
162     // test draw after free
163     dContext->freeGpuResources();
164     draw(canvas, 1, blobs);
165 
166     dContext->freeGpuResources();
167     draw(canvasNoLCD, 1, blobs);
168 
169     // test draw after abandon
170     dContext->abandonContext();
171     draw(canvas, 1, blobs);
172 }
173 
DEF_GPUTEST_FOR_MOCK_CONTEXT(TextBlobCache,reporter,ctxInfo)174 DEF_GPUTEST_FOR_MOCK_CONTEXT(TextBlobCache, reporter, ctxInfo) {
175     text_blob_cache_inner(reporter, ctxInfo.directContext(), 1024, 256, 30, true, false);
176 }
177 
DEF_GPUTEST_FOR_MOCK_CONTEXT(TextBlobStressCache,reporter,ctxInfo)178 DEF_GPUTEST_FOR_MOCK_CONTEXT(TextBlobStressCache, reporter, ctxInfo) {
179     text_blob_cache_inner(reporter, ctxInfo.directContext(), 256, 256, 10, true, true);
180 }
181 
DEF_GPUTEST_FOR_MOCK_CONTEXT(TextBlobAbnormal,reporter,ctxInfo)182 DEF_GPUTEST_FOR_MOCK_CONTEXT(TextBlobAbnormal, reporter, ctxInfo) {
183     text_blob_cache_inner(reporter, ctxInfo.directContext(), 256, 256, 10, false, false);
184 }
185 
DEF_GPUTEST_FOR_MOCK_CONTEXT(TextBlobStressAbnormal,reporter,ctxInfo)186 DEF_GPUTEST_FOR_MOCK_CONTEXT(TextBlobStressAbnormal, reporter, ctxInfo) {
187     text_blob_cache_inner(reporter, ctxInfo.directContext(), 256, 256, 10, false, true);
188 }
189 
190 static const int kScreenDim = 160;
191 
draw_blob(SkTextBlob * blob,SkSurface * surface,SkPoint offset)192 static SkBitmap draw_blob(SkTextBlob* blob, SkSurface* surface, SkPoint offset) {
193 
194     SkPaint paint;
195 
196     SkCanvas* canvas = surface->getCanvas();
197     canvas->save();
198     canvas->drawColor(SK_ColorWHITE, SkBlendMode::kSrc);
199     canvas->translate(offset.fX, offset.fY);
200     canvas->drawTextBlob(blob, 0, 0, paint);
201     SkBitmap bitmap;
202     bitmap.allocN32Pixels(kScreenDim, kScreenDim);
203     surface->readPixels(bitmap, 0, 0);
204     canvas->restore();
205     return bitmap;
206 }
207 
compare_bitmaps(const SkBitmap & expected,const SkBitmap & actual)208 static bool compare_bitmaps(const SkBitmap& expected, const SkBitmap& actual) {
209     SkASSERT(expected.width() == actual.width());
210     SkASSERT(expected.height() == actual.height());
211     for (int i = 0; i < expected.width(); ++i) {
212         for (int j = 0; j < expected.height(); ++j) {
213             SkColor expectedColor = expected.getColor(i, j);
214             SkColor actualColor = actual.getColor(i, j);
215             if (expectedColor != actualColor) {
216                 return false;
217             }
218         }
219     }
220     return true;
221 }
222 
make_blob()223 static sk_sp<SkTextBlob> make_blob() {
224     auto tf = SkTypeface::MakeFromName("Roboto2-Regular", SkFontStyle());
225     SkFont font;
226     font.setTypeface(tf);
227     font.setSubpixel(false);
228     font.setEdging(SkFont::Edging::kAlias);
229     font.setSize(24);
230 
231     static char text[] = "HekpqB";
232     static const int maxGlyphLen = sizeof(text) * 4;
233     SkGlyphID glyphs[maxGlyphLen];
234     int glyphCount =
235             font.textToGlyphs(text, sizeof(text), SkTextEncoding::kUTF8, glyphs, maxGlyphLen);
236 
237     SkTextBlobBuilder builder;
238     const auto& runBuffer = builder.allocRun(font, glyphCount, 0, 0);
239     for (int i = 0; i < glyphCount; i++) {
240         runBuffer.glyphs[i] = glyphs[i];
241     }
242     return builder.make();
243 }
244 
245 // Turned off to pass on android and ios devices, which were running out of memory..
246 #if 0
247 static sk_sp<SkTextBlob> make_large_blob() {
248     auto tf = SkTypeface::MakeFromName("Roboto2-Regular", SkFontStyle());
249     SkFont font;
250     font.setTypeface(tf);
251     font.setSubpixel(false);
252     font.setEdging(SkFont::Edging::kAlias);
253     font.setSize(24);
254 
255     const int mallocSize = 0x3c3c3bd; // x86 size
256     std::unique_ptr<char[]> text{new char[mallocSize + 1]};
257     if (text == nullptr) {
258         return nullptr;
259     }
260     for (int i = 0; i < mallocSize; i++) {
261         text[i] = 'x';
262     }
263     text[mallocSize] = 0;
264 
265     static const int maxGlyphLen = mallocSize;
266     std::unique_ptr<SkGlyphID[]> glyphs{new SkGlyphID[maxGlyphLen]};
267     int glyphCount =
268             font.textToGlyphs(
269                     text.get(), mallocSize, SkTextEncoding::kUTF8, glyphs.get(), maxGlyphLen);
270     SkTextBlobBuilder builder;
271     const auto& runBuffer = builder.allocRun(font, glyphCount, 0, 0);
272     for (int i = 0; i < glyphCount; i++) {
273         runBuffer.glyphs[i] = glyphs[i];
274     }
275     return builder.make();
276 }
277 
278 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TextBlobIntegerOverflowTest, reporter, ctxInfo) {
279     auto dContext = ctxInfo.directContext();
280     const SkImageInfo info =
281             SkImageInfo::Make(kScreenDim, kScreenDim, kN32_SkColorType, kPremul_SkAlphaType);
282     auto surface = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info);
283 
284     auto blob = make_large_blob();
285     int y = 40;
286     SkBitmap base = draw_blob(blob.get(), surface.get(), {40, y + 0.0f});
287 }
288 #endif
289 
290 static const bool kDumpPngs = true;
291 // dump pngs needs a "good" and a "bad" directory to put the results in. This allows the use of the
292 // skdiff tool to visualize the differences.
293 
write_png(const std::string & filename,const SkBitmap & bitmap)294 void write_png(const std::string& filename, const SkBitmap& bitmap) {
295     auto data = SkEncodeBitmap(bitmap, SkEncodedImageFormat::kPNG, 0);
296     SkFILEWStream w{filename.c_str()};
297     w.write(data->data(), data->size());
298     w.fsync();
299 }
300 
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TextBlobJaggedGlyph,reporter,ctxInfo)301 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TextBlobJaggedGlyph, reporter, ctxInfo) {
302     auto direct = ctxInfo.directContext();
303     const SkImageInfo info =
304             SkImageInfo::Make(kScreenDim, kScreenDim, kN32_SkColorType, kPremul_SkAlphaType);
305     auto surface = SkSurface::MakeRenderTarget(direct, SkBudgeted::kNo, info);
306 
307     auto blob = make_blob();
308 
309     for (int y = 40; y < kScreenDim - 40; y++) {
310         SkBitmap base = draw_blob(blob.get(), surface.get(), {40, y + 0.0f});
311         SkBitmap half = draw_blob(blob.get(), surface.get(), {40, y + 0.5f});
312         SkBitmap unit = draw_blob(blob.get(), surface.get(), {40, y + 1.0f});
313         bool isOk = compare_bitmaps(base, half) || compare_bitmaps(unit, half);
314         REPORTER_ASSERT(reporter, isOk);
315         if (!isOk) {
316             if (kDumpPngs) {
317                 {
318                     std::string filename = "bad/half-y" + std::to_string(y) + ".png";
319                     write_png(filename, half);
320                 }
321                 {
322                     std::string filename = "good/half-y" + std::to_string(y) + ".png";
323                     write_png(filename, base);
324                 }
325             }
326             break;
327         }
328     }
329 
330     // Testing the x direction across all platforms does not workout, because letter spacing can
331     // change based on non-integer advance widths, but this has been useful for diagnosing problems.
332 #if 0
333     blob = make_blob();
334     for (int x = 40; x < kScreenDim - 40; x++) {
335         SkBitmap base = draw_blob(blob.get(), surface.get(), {x + 0.0f, 40});
336         SkBitmap half = draw_blob(blob.get(), surface.get(), {x + 0.5f, 40});
337         SkBitmap unit = draw_blob(blob.get(), surface.get(), {x + 1.0f, 40});
338         bool isOk = compare_bitmaps(base, half) || compare_bitmaps(unit, half);
339         REPORTER_ASSERT(reporter, isOk);
340         if (!isOk) {
341             if (kDumpPngs) {
342                 {
343                     std::string filename = "bad/half-x" + std::to_string(x) + ".png";
344                     write_png(filename, half);
345                 }
346                 {
347                     std::string filename = "good/half-x" + std::to_string(x) + ".png";
348                     write_png(filename, base);
349                 }
350             }
351             break;
352         }
353     }
354 #endif
355 }
356 
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TextBlobSmoothScroll,reporter,ctxInfo)357 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TextBlobSmoothScroll, reporter, ctxInfo) {
358     auto direct = ctxInfo.directContext();
359     const SkImageInfo info =
360             SkImageInfo::Make(kScreenDim, kScreenDim, kN32_SkColorType, kPremul_SkAlphaType);
361     auto surface = SkSurface::MakeRenderTarget(direct, SkBudgeted::kNo, info);
362 
363     auto movingBlob = make_blob();
364 
365     for (SkScalar y = 40; y < 50; y += 1.0/8.0) {
366         auto expectedBlob = make_blob();
367         auto expectedBitMap = draw_blob(expectedBlob.get(), surface.get(), {40, y});
368         auto movingBitmap = draw_blob(movingBlob.get(), surface.get(), {40, y});
369         bool isOk = compare_bitmaps(expectedBitMap, movingBitmap);
370         REPORTER_ASSERT(reporter, isOk);
371         if (!isOk) {
372             if (kDumpPngs) {
373                 {
374                     std::string filename = "bad/scroll-y" + std::to_string(y) + ".png";
375                     write_png(filename, movingBitmap);
376                 }
377                 {
378                     std::string filename = "good/scroll-y" + std::to_string(y) + ".png";
379                     write_png(filename, expectedBitMap);
380                 }
381             }
382             break;
383         }
384     }
385 }
386