• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkExecutor.h"
9 #include "include/core/SkFont.h"
10 #include "include/core/SkFontStyle.h"
11 #include "include/core/SkMatrix.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkPoint.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkSurfaceProps.h"
18 #include "include/core/SkTypeface.h"
19 #include "include/core/SkTypes.h"
20 #include "include/private/base/SkTo.h"
21 #include "src/base/SkZip.h"
22 #include "src/core/SkEnumerate.h"
23 #include "src/core/SkGlyph.h"
24 #include "src/core/SkGlyphBuffer.h"
25 #include "src/core/SkScalerContext.h"
26 #include "src/core/SkStrike.h"
27 #include "src/core/SkStrikeCache.h"
28 #include "src/core/SkStrikeSpec.h"
29 #include "src/core/SkTaskGroup.h"
30 #include "src/text/StrikeForGPU.h"
31 #include "tests/Test.h"
32 #include "tools/ToolUtils.h"
33 
34 #include <atomic>
35 #include <cstddef>
36 #include <functional>
37 #include <initializer_list>
38 #include <memory>
39 
40 using namespace sktext;
41 using namespace skglyph;
42 
43 class Barrier {
44 public:
Barrier(int threadCount)45     Barrier(int threadCount) : fThreadCount(threadCount) { }
waitForAll()46     void waitForAll() {
47         fThreadCount -= 1;
48         while (fThreadCount > 0) { }
49     }
50 
51 private:
52     std::atomic<int> fThreadCount;
53 };
54 
55 // This should stay in sync with the implementation from SubRunContainer.
prepare_for_mask_drawing(StrikeForGPU * strike,SkDrawableGlyphBuffer * accepted,SkSourceGlyphBuffer * rejected)56 static SkRect prepare_for_mask_drawing(StrikeForGPU* strike,
57                                        SkDrawableGlyphBuffer* accepted,
58                                        SkSourceGlyphBuffer* rejected) {
59     SkGlyphRect boundingRect = skglyph::empty_rect();
60     StrikeMutationMonitor m{strike};
61     for (auto [i, packedID, pos] : SkMakeEnumerate(accepted->input())) {
62         if (SkScalarsAreFinite(pos.x(), pos.y())) {
63             SkGlyphDigest digest = strike->digestFor(kDirectMask, packedID);
64             if (!digest.isEmpty()) {
65                 if (digest.fitsInAtlasDirect()) {
66                     const SkGlyphRect glyphBounds = digest.bounds().offset(pos);
67                     boundingRect = skglyph::rect_union(boundingRect, glyphBounds);
68                     accepted->accept(packedID, glyphBounds.leftTop(), digest.maskFormat());
69                 } else {
70                     rejected->reject(i);
71                 }
72             }
73         }
74     }
75 
76     return boundingRect.rect();
77 }
78 
DEF_TEST(SkStrikeMultiThread,Reporter)79 DEF_TEST(SkStrikeMultiThread, Reporter) {
80     sk_sp<SkTypeface> typeface =
81             ToolUtils::create_portable_typeface("serif", SkFontStyle::Italic());
82     static constexpr int kThreadCount = 4;
83 
84     Barrier barrier{kThreadCount};
85 
86     SkFont font;
87     font.setEdging(SkFont::Edging::kAntiAlias);
88     font.setSubpixel(true);
89     font.setTypeface(typeface);
90 
91     SkGlyphID glyphs['z'];
92     SkPoint pos['z'];
93     for (int c = ' '; c < 'z'; c++) {
94         glyphs[c] = font.unicharToGlyph(c);
95         pos[c] = {30.0f * c + 30, 30.0f};
96     }
97     constexpr size_t glyphCount = 'z' - ' ';
98     auto data = SkMakeZip(glyphs, pos).subspan(SkTo<int>(' '), glyphCount);
99 
100     SkPaint defaultPaint;
101     SkStrikeSpec strikeSpec = SkStrikeSpec::MakeMask(
102             font, defaultPaint, SkSurfaceProps(0, kUnknown_SkPixelGeometry),
103             SkScalerContextFlags::kNone, SkMatrix::I());
104 
105     SkStrikeCache strikeCache;
106 
107     // Make our own executor so the --threads parameter doesn't mess things up.
108     auto executor = SkExecutor::MakeFIFOThreadPool(kThreadCount);
109     for (int tries = 0; tries < 100; tries++) {
110         SkStrike strike{&strikeCache, strikeSpec, strikeSpec.createScalerContext(), nullptr,
111                         nullptr};
112 
113         auto perThread = [&](int threadIndex) {
114             barrier.waitForAll();
115 
116             auto local = data.subspan(threadIndex * 2, data.size() - kThreadCount * 2);
117             for (int i = 0; i < 100; i++) {
118                 SkDrawableGlyphBuffer accepted;
119                 SkSourceGlyphBuffer rejected;
120 
121                 accepted.ensureSize(glyphCount);
122                 rejected.setSource(local);
123 
124                 accepted.startSource(rejected.source());
125                 prepare_for_mask_drawing(&strike, &accepted, &rejected);
126                 rejected.flipRejectsToSource();
127                 accepted.reset();
128             }
129         };
130 
131         SkTaskGroup(*executor).batch(kThreadCount, perThread);
132     }
133 }
134