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