TextBlobCacheTest.cpp 6.0 KB

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