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