1 /*
2 * Copyright 2020 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 "include/core/SkAlphaType.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkColorType.h"
13 #include "include/core/SkFont.h"
14 #include "include/core/SkFontStyle.h"
15 #include "include/core/SkFontTypes.h"
16 #include "include/core/SkImageInfo.h"
17 #include "include/core/SkMatrix.h"
18 #include "include/core/SkPaint.h"
19 #include "include/core/SkPoint.h"
20 #include "include/core/SkRefCnt.h"
21 #include "include/core/SkScalar.h"
22 #include "include/core/SkSurface.h"
23 #include "include/core/SkSurfaceProps.h"
24 #include "include/core/SkTextBlob.h"
25 #include "include/core/SkTypeface.h"
26 #include "include/core/SkTypes.h"
27 #include "include/gpu/GpuTypes.h"
28 #include "include/gpu/ganesh/GrDirectContext.h"
29 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
30 #include "src/core/SkDevice.h"
31 #include "src/core/SkScalerContext.h"
32 #include "src/text/GlyphRun.h"
33 #include "src/text/gpu/SubRunAllocator.h"
34 #include "src/text/gpu/SubRunControl.h"
35 #include "src/text/gpu/TextBlob.h"
36 #include "tests/CtsEnforcement.h"
37 #include "tests/Test.h"
38 #include "tools/ToolUtils.h"
39 #include "tools/fonts/FontToolUtils.h"
40
41 #include <cmath>
42 #include <cstddef>
43 #include <cstdint>
44 #include <limits>
45 #include <memory>
46 #include <tuple>
47 #include <utility>
48
49 class GrRecordingContext;
50 struct GrContextOptions;
51
52 using BagOfBytes = sktext::gpu::BagOfBytes;
53 using SubRunAllocator = sktext::gpu::SubRunAllocator;
54
rasterize_blob(SkTextBlob * blob,const SkPaint & paint,GrRecordingContext * rContext,const SkMatrix & matrix)55 SkBitmap rasterize_blob(SkTextBlob* blob,
56 const SkPaint& paint,
57 GrRecordingContext* rContext,
58 const SkMatrix& matrix) {
59 const SkImageInfo info =
60 SkImageInfo::Make(500, 500, kN32_SkColorType, kPremul_SkAlphaType);
61 auto surface = SkSurfaces::RenderTarget(rContext, skgpu::Budgeted::kNo, info);
62 auto canvas = surface->getCanvas();
63 canvas->drawColor(SK_ColorWHITE);
64 canvas->concat(matrix);
65 canvas->drawTextBlob(blob, 10, 250, paint);
66 SkBitmap bitmap;
67 bitmap.allocN32Pixels(500, 500);
68 surface->readPixels(bitmap, 0, 0);
69 return bitmap;
70 }
71
check_for_black(const SkBitmap & bm)72 bool check_for_black(const SkBitmap& bm) {
73 for (int y = 0; y < bm.height(); y++) {
74 for (int x = 0; x < bm.width(); x++) {
75 if (bm.getColor(x, y) == SK_ColorBLACK) {
76 return true;
77 }
78 }
79 }
80 return false;
81 }
82
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(GrTextBlobScaleAnimation,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)83 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(GrTextBlobScaleAnimation,
84 reporter,
85 ctxInfo,
86 CtsEnforcement::kApiLevel_T) {
87 auto tf = ToolUtils::CreatePortableTypeface("Mono", SkFontStyle());
88 SkFont font{tf};
89 font.setHinting(SkFontHinting::kNormal);
90 font.setSize(12);
91 font.setEdging(SkFont::Edging::kAntiAlias);
92 font.setSubpixel(true);
93
94 SkTextBlobBuilder builder;
95 const auto& runBuffer = builder.allocRunPosH(font, 30, 0, nullptr);
96
97 for (int i = 0; i < 30; i++) {
98 runBuffer.glyphs[i] = static_cast<SkGlyphID>(i);
99 runBuffer.pos[i] = SkIntToScalar(i);
100 }
101 auto blob = builder.make();
102
103 auto dContext = ctxInfo.directContext();
104 bool anyBlack = false;
105 for (int n = -13; n < 5; n++) {
106 SkMatrix m = SkMatrix::Scale(std::exp2(n), std::exp2(n));
107 auto bm = rasterize_blob(blob.get(), SkPaint(), dContext, m);
108 anyBlack |= check_for_black(bm);
109 }
110 REPORTER_ASSERT(reporter, anyBlack);
111 }
112
113 // Test extreme positions for all combinations of positions, origins, and translation matrices.
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(GrTextBlobMoveAround,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)114 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(GrTextBlobMoveAround,
115 reporter,
116 ctxInfo,
117 CtsEnforcement::kApiLevel_T) {
118 auto tf = ToolUtils::CreatePortableTypeface("Mono", SkFontStyle());
119 SkFont font{tf};
120 font.setHinting(SkFontHinting::kNormal);
121 font.setSize(12);
122 font.setEdging(SkFont::Edging::kAntiAlias);
123 font.setSubpixel(true);
124
125 auto makeBlob = [&](SkPoint delta) {
126 SkTextBlobBuilder builder;
127 const auto& runBuffer = builder.allocRunPos(font, 30, nullptr);
128
129 for (int i = 0; i < 30; i++) {
130 runBuffer.glyphs[i] = static_cast<SkGlyphID>(i);
131 runBuffer.points()[i] = SkPoint::Make(SkIntToScalar(i*10) + delta.x(), 50 + delta.y());
132 }
133 return builder.make();
134 };
135
136 auto dContext = ctxInfo.directContext();
137 auto rasterizeBlob = [&](SkTextBlob* blob, SkPoint origin, const SkMatrix& matrix) {
138 SkPaint paint;
139 const SkImageInfo info =
140 SkImageInfo::Make(350, 80, kN32_SkColorType, kPremul_SkAlphaType);
141 auto surface = SkSurfaces::RenderTarget(dContext, skgpu::Budgeted::kNo, info);
142 auto canvas = surface->getCanvas();
143 canvas->drawColor(SK_ColorWHITE);
144 canvas->concat(matrix);
145 canvas->drawTextBlob(blob, 10 + origin.x(), 40 + origin.y(), paint);
146 SkBitmap bitmap;
147 bitmap.allocN32Pixels(350, 80);
148 surface->readPixels(bitmap, 0, 0);
149 return bitmap;
150 };
151
152 SkBitmap benchMark;
153 {
154 auto blob = makeBlob({0, 0});
155 benchMark = rasterizeBlob(blob.get(), {0,0}, SkMatrix::I());
156 }
157
158 auto checkBitmap = [&](const SkBitmap& bitmap) {
159 REPORTER_ASSERT(reporter, benchMark.width() == bitmap.width());
160 REPORTER_ASSERT(reporter, benchMark.width() == bitmap.width());
161
162 for (int y = 0; y < benchMark.height(); y++) {
163 for (int x = 0; x < benchMark.width(); x++) {
164 if (benchMark.getColor(x, y) != bitmap.getColor(x, y)) {
165 return false;
166 }
167 }
168 }
169 return true;
170 };
171
172 SkScalar interestingNumbers[] = {-10'000'000, -1'000'000, -1, 0, +1, +1'000'000, +10'000'000};
173 for (auto originX : interestingNumbers) {
174 for (auto originY : interestingNumbers) {
175 for (auto translateX : interestingNumbers) {
176 for (auto translateY : interestingNumbers) {
177 // Make sure everything adds to zero.
178 SkScalar deltaPosX = -(originX + translateX);
179 SkScalar deltaPosY = -(originY + translateY);
180 auto blob = makeBlob({deltaPosX, deltaPosY});
181 SkMatrix t = SkMatrix::Translate(translateX, translateY);
182 auto bitmap = rasterizeBlob(blob.get(), {originX, originY}, t);
183 REPORTER_ASSERT(reporter, checkBitmap(bitmap));
184 }
185 }
186 }
187 }
188 }
189
DEF_TEST(BagOfBytesBasic,r)190 DEF_TEST(BagOfBytesBasic, r) {
191 const int k4K = 1 << 12;
192 {
193 // GrBagOfBytes::MinimumSizeWithOverhead(-1); // This should fail
194 BagOfBytes::PlatformMinimumSizeWithOverhead(0, 16);
195 BagOfBytes::PlatformMinimumSizeWithOverhead(
196 std::numeric_limits<int>::max() - k4K - 1, 16);
197 // GrBagOfBytes::MinimumSizeWithOverhead(std::numeric_limits<int>::max() - k4K); // Fail
198 REPORTER_ASSERT(r, BagOfBytes::MinimumSizeWithOverhead(0, 1, 16, 16) == 31);
199 REPORTER_ASSERT(r, BagOfBytes::MinimumSizeWithOverhead(1, 1, 16, 16) == 32);
200 REPORTER_ASSERT(r, BagOfBytes::MinimumSizeWithOverhead(63, 1, 16, 16) == 94);
201 REPORTER_ASSERT(r, BagOfBytes::MinimumSizeWithOverhead(0, 8, 16, 16) == 24);
202 REPORTER_ASSERT(r, BagOfBytes::MinimumSizeWithOverhead(1, 8, 16, 16) == 32);
203 REPORTER_ASSERT(r, BagOfBytes::MinimumSizeWithOverhead(63, 8, 16, 16) == 88);
204 REPORTER_ASSERT(r, BagOfBytes::MinimumSizeWithOverhead(0, 16, 16, 16) == 16);
205 REPORTER_ASSERT(r, BagOfBytes::MinimumSizeWithOverhead(1, 16, 16, 16) == 32);
206 REPORTER_ASSERT(r, BagOfBytes::MinimumSizeWithOverhead(63, 16, 16, 16) == 80);
207
208 REPORTER_ASSERT(r, BagOfBytes::MinimumSizeWithOverhead(0, 1, 8, 16) == 23);
209 REPORTER_ASSERT(r, BagOfBytes::MinimumSizeWithOverhead(1, 1, 8, 16) == 24);
210 REPORTER_ASSERT(r, BagOfBytes::MinimumSizeWithOverhead(63, 1, 8, 16) == 86);
211 REPORTER_ASSERT(r, BagOfBytes::MinimumSizeWithOverhead(0, 8, 8, 16) == 16);
212 REPORTER_ASSERT(r, BagOfBytes::MinimumSizeWithOverhead(1, 8, 8, 16) == 24);
213 REPORTER_ASSERT(r, BagOfBytes::MinimumSizeWithOverhead(63, 8, 8, 16) == 80);
214 }
215
216 {
217 BagOfBytes bob;
218 // bob.alignedBytes(0, 1); // This should fail
219 // bob.alignedBytes(1, 0); // This should fail
220 // bob.alignedBytes(1, 3); // This should fail
221
222 struct Big {
223 char stuff[std::numeric_limits<int>::max()];
224 };
225 // bob.alignedBytes(sizeof(Big), 1); // this should fail
226 // bob.allocateBytesFor<Big>(); // this should not compile
227 // The following should run, but should not be regularly tested.
228 // bob.allocateBytesFor<int>((std::numeric_limits<int>::max() - (1<<12)) / sizeof(int) - 1);
229 // The following should fail
230 // bob.allocateBytesFor<int>((std::numeric_limits<int>::max() - (1<<12)) / sizeof(int));
231 bob.alignedBytes(1, 1); // To avoid unused variable problems.
232 }
233
234 // Force multiple block allocation
235 {
236 BagOfBytes bob;
237 const int k64K = 1 << 16;
238 // By default allocation block sizes start at 1K and go up with fib. This should allocate
239 // 10 individual blocks.
240 for (int i = 0; i < 10; i++) {
241 bob.alignedBytes(k64K, 1);
242 }
243 }
244 }
245
DEF_TEST(SubRunAllocator,r)246 DEF_TEST(SubRunAllocator, r) {
247 static int created = 0;
248 static int destroyed = 0;
249 struct Foo {
250 Foo() : fI{-2}, fX{-3} { created++; }
251 Foo(int i, float x) : fI{i}, fX{x} { created++; }
252 ~Foo() { destroyed++; }
253 int fI;
254 float fX;
255 };
256
257 struct alignas(8) OddAlignment {
258 char buf[10];
259 };
260
261 auto exercise = [&](SubRunAllocator* alloc) {
262 created = 0;
263 destroyed = 0;
264 {
265 int* p = alloc->makePOD<int>(3);
266 REPORTER_ASSERT(r, *p == 3);
267 int* q = alloc->makePOD<int>(7);
268 REPORTER_ASSERT(r, *q == 7);
269
270 REPORTER_ASSERT(r, *alloc->makePOD<int>(3) == 3);
271 auto foo = alloc->makeUnique<Foo>(3, 4.0f);
272 REPORTER_ASSERT(r, foo->fI == 3);
273 REPORTER_ASSERT(r, foo->fX == 4.0f);
274 REPORTER_ASSERT(r, created == 1);
275 REPORTER_ASSERT(r, destroyed == 0);
276
277 alloc->makePODArray<int>(10);
278
279 auto fooArray = alloc->makeUniqueArray<Foo>(10);
280 REPORTER_ASSERT(r, fooArray[3].fI == -2);
281 REPORTER_ASSERT(r, fooArray[4].fX == -3.0f);
282 REPORTER_ASSERT(r, created == 11);
283 REPORTER_ASSERT(r, destroyed == 0);
284 alloc->makePOD<OddAlignment>();
285 }
286
287 REPORTER_ASSERT(r, created == 11);
288 REPORTER_ASSERT(r, destroyed == 11);
289 };
290
291 // Exercise default arena
292 {
293 SubRunAllocator arena{0};
294 exercise(&arena);
295 }
296
297 // Exercise on stack arena
298 {
299 sktext::gpu::STSubRunAllocator<64, 16> arena;
300 exercise(&arena);
301 }
302
303 // Exercise arena with a heap allocated starting block
304 {
305 std::unique_ptr<char[]> block{new char[1024]};
306 SubRunAllocator arena{block.get(), 1024, 0};
307 exercise(&arena);
308 }
309
310 // Exercise the singly-link list of unique_ptrs use case
311 {
312 created = 0;
313 destroyed = 0;
314 SubRunAllocator arena;
315
316 struct Node {
317 Node(std::unique_ptr<Node, SubRunAllocator::Destroyer> next)
318 : fNext{std::move(next)} { created++; }
319 ~Node() { destroyed++; }
320 std::unique_ptr<Node, SubRunAllocator::Destroyer> fNext;
321 };
322
323 std::unique_ptr<Node, SubRunAllocator::Destroyer> current = nullptr;
324 for (int i = 0; i < 128; i++) {
325 current = arena.makeUnique<Node>(std::move(current));
326 }
327 REPORTER_ASSERT(r, created == 128);
328 REPORTER_ASSERT(r, destroyed == 0);
329 }
330 REPORTER_ASSERT(r, created == 128);
331 REPORTER_ASSERT(r, destroyed == 128);
332
333 // Exercise the array ctor w/ a mapping function
334 {
335 struct I {
336 I(int v) : i{v} {}
337 ~I() {}
338 int i;
339 };
340 sktext::gpu::STSubRunAllocator<64, 16> arena;
341 auto a = arena.makeUniqueArray<I>(8, [](size_t i) { return i; });
342 for (size_t i = 0; i < 8; i++) {
343 REPORTER_ASSERT(r, a[i].i == (int)i);
344 }
345 }
346
347 {
348 SubRunAllocator arena(4096);
349 void* ptr = arena.alignedBytes(4081, 8);
350 REPORTER_ASSERT(r, ((intptr_t)ptr & 7) == 0);
351 }
352 }
353
354 using TextBlob = sktext::gpu::TextBlob;
355
DEF_TEST(KeyEqualityOnPerspective,r)356 DEF_TEST(KeyEqualityOnPerspective, r) {
357 SkTextBlobBuilder builder;
358 SkFont font(ToolUtils::DefaultTypeface(), 16);
359 auto runBuffer = builder.allocRun(font, 1, 0.0f, 0.0f);
360 runBuffer.glyphs[0] = 3;
361 auto blob = builder.make();
362 sktext::GlyphRunBuilder grBuilder;
363 auto glyphRunList = grBuilder.blobToGlyphRunList(*blob, {100, 100});
364 SkPaint paint;
365
366 // Build the strike device.
367 SkSurfaceProps props;
368 #if !defined(SK_DISABLE_SDF_TEXT)
369 sktext::gpu::SubRunControl control(false, false, false, 1, 100);
370 #else
371 sktext::gpu::SubRunControl control{};
372 #endif
373 SkStrikeDeviceInfo strikeDevice{props, SkScalerContextFlags::kBoostContrast, &control};
374 SkMatrix matrix1;
375 matrix1.setAll(1, 0, 0, 0, 1, 0, 1, 1, 1);
376 SkMatrix matrix2;
377 matrix2.setAll(1, 0, 0, 0, 1, 0, 2, 2, 1);
378 auto key1 = std::get<1>(
379 TextBlob::Key::Make(glyphRunList, paint, matrix1, strikeDevice));
380 auto key2 = std::get<1>(
381 TextBlob::Key::Make(glyphRunList, paint, matrix1, strikeDevice));
382 auto key3 = std::get<1>(
383 TextBlob::Key::Make(glyphRunList, paint, matrix2, strikeDevice));
384 REPORTER_ASSERT(r, key1 == key2);
385 REPORTER_ASSERT(r, key1 == key3);
386 }
387