Flutter Engine
The Flutter Engine
SkStrikeSpec.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2019 The Android Open Source Project
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
9
10#include "include/core/SkFont.h"
16#include "src/base/SkTLazy.h"
17#include "src/core/SkFontPriv.h"
18#include "src/core/SkGlyph.h"
19#include "src/core/SkStrike.h"
22
23#include <utility>
24
26 : fAutoDescriptor{descriptor}
27 , fTypeface{std::move(typeface)} {}
28
32
34 const SkSurfaceProps& surfaceProps,
35 SkScalerContextFlags scalerContextFlags,
36 const SkMatrix& deviceMatrix) {
37
38 return SkStrikeSpec(font, paint, surfaceProps, scalerContextFlags, deviceMatrix);
39}
40
42 const SkPaint& paint,
43 const SkSurfaceProps& surfaceProps,
44 SkScalerContextFlags scalerContextFlags,
45 const SkMatrix& deviceMatrix) {
46 SkFont sourceFont{font};
47 sourceFont.setSubpixel(false);
48 return SkStrikeSpec(sourceFont, paint, surfaceProps, scalerContextFlags, deviceMatrix);
49}
50
51std::tuple<SkStrikeSpec, SkScalar> SkStrikeSpec::MakePath(
52 const SkFont& font, const SkPaint& paint,
53 const SkSurfaceProps& surfaceProps,
54 SkScalerContextFlags scalerContextFlags) {
55
56 // setup our std runPaint, in hopes of getting hits in the cache
57 SkPaint pathPaint{paint};
58 SkFont pathFont{font};
59
60 // The sub-pixel position will always happen when transforming to the screen.
61 pathFont.setSubpixel(false);
62
63 // The factor to get from the size stored in the strike to the size needed for
64 // the source.
65 SkScalar strikeToSourceScale = pathFont.setupForAsPaths(&pathPaint);
66
67 return {SkStrikeSpec(pathFont, pathPaint, surfaceProps, scalerContextFlags, SkMatrix::I()),
68 strikeToSourceScale};
69}
70
71std::tuple<SkStrikeSpec, SkScalar> SkStrikeSpec::MakeCanonicalized(
72 const SkFont& font, const SkPaint* paint) {
73 SkPaint canonicalizedPaint;
74 if (paint != nullptr) {
75 canonicalizedPaint = *paint;
76 }
77
78 const SkFont* canonicalizedFont = &font;
79 SkTLazy<SkFont> pathFont;
80 SkScalar strikeToSourceScale = 1;
81 if (ShouldDrawAsPath(canonicalizedPaint, font, SkMatrix::I())) {
82 canonicalizedFont = pathFont.set(font);
83 strikeToSourceScale = pathFont->setupForAsPaths(nullptr);
84 canonicalizedPaint.reset();
85 }
86
87 return {SkStrikeSpec(*canonicalizedFont, canonicalizedPaint, SkSurfaceProps(),
89 strikeToSourceScale};
90}
91
93 SkPaint setupPaint;
94 if (paint != nullptr) {
95 setupPaint = *paint;
96 }
97
98 return SkStrikeSpec(font, setupPaint, SkSurfaceProps(),
100}
101
103 const SkPaint& paint, const SkFont& font, const SkMatrix& viewMatrix) {
104
105 // hairline glyphs are fast enough, so we don't need to cache them
106 if (SkPaint::kStroke_Style == paint.getStyle() && 0 == paint.getStrokeWidth()) {
107 return true;
108 }
109
110 // we don't cache perspective
111 if (viewMatrix.hasPerspective()) {
112 return true;
113 }
114
116 textMatrix.postConcat(viewMatrix);
117
118 // we have a self-imposed maximum, just to limit memory-usage
119 constexpr SkScalar memoryLimit = 256;
120 constexpr SkScalar maxSizeSquared = memoryLimit * memoryLimit;
121
122 auto distance = [&textMatrix](int XIndex, int YIndex) {
123 return textMatrix[XIndex] * textMatrix[XIndex] + textMatrix[YIndex] * textMatrix[YIndex];
124 };
125
126 return distance(SkMatrix::kMScaleX, SkMatrix::kMSkewY ) > maxSizeSquared
127 || distance(SkMatrix::kMSkewX, SkMatrix::kMScaleY) > maxSizeSquared;
128}
129
131 return fAutoDescriptor.getDesc()->dumpRec();
132}
133
135 SkFont font;
136 font.setHinting(SkFontHinting::kNone);
137 font.setEdging(SkFont::Edging::kAlias);
138 font.setTypeface(sk_ref_sp(&typeface));
139 int unitsPerEm = typeface.getUnitsPerEm();
140 if (unitsPerEm <= 0) {
141 unitsPerEm = 1024;
142 }
143 if (size) {
144 *size = unitsPerEm;
145 }
146 font.setSize((SkScalar)unitsPerEm);
147
148 return SkStrikeSpec(font,
149 SkPaint(),
152 SkMatrix::I());
153}
154
156 const SkSurfaceProps& surfaceProps,
157 SkScalerContextFlags scalerContextFlags,
158 const SkMatrix& deviceMatrix) {
160
162 font, paint, surfaceProps, scalerContextFlags, deviceMatrix,
163 &fAutoDescriptor, &effects);
164
165 fMaskFilter = sk_ref_sp(effects.fMaskFilter);
166 fPathEffect = sk_ref_sp(effects.fPathEffect);
167 fTypeface = font.refTypeface();
168}
169
172 return cache->findOrCreateScopedStrike(*this);
173}
174
177}
178
180 return cache->findOrCreateStrike(*this);
181}
182
184 : fStrike{spec.findOrCreateStrike()} { }
185
187
189 fGlyphs.reset(glyphIDs.size());
190 return fStrike->metrics(glyphIDs, fGlyphs.get());
191}
192
194 return this->glyphs(SkSpan<const SkGlyphID>{&glyphID, 1})[0];
195}
196
198 : fStrike{spec.findOrCreateStrike()} { }
199
201 : fStrike{std::move(strike)} { }
202
204
206 fGlyphs.reset(glyphIDs.size());
207 return fStrike->preparePaths(glyphIDs, fGlyphs.get());
208}
209
211 return this->glyphs(SkSpan<const SkGlyphID>{&glyphID, 1})[0];
212}
213
215 const SkScalar* bounds, SkScalar scale, SkScalar xPos,
216 const SkGlyph* glyph, SkScalar* array, int* count) {
217 // TODO(herb): remove this abominable const_cast. Do the intercepts really need to be on the
218 // glyph?
219 fStrike->findIntercepts(bounds, scale, xPos, const_cast<SkGlyph*>(glyph), array, count);
220}
221
223 : fStrike{spec.findOrCreateStrike()} { }
224
226 : fStrike{std::move(strike)} { }
227
229
231 fGlyphs.reset(glyphIDs.size());
232 return fStrike->prepareDrawables(glyphIDs, fGlyphs.get());
233}
234
236 return this->glyphs(SkSpan<const SkGlyphID>{&glyphID, 1})[0];
237}
238
240 : fStrike{spec.findOrCreateStrike()} { }
241
243 : fStrike{std::move(strike)} { }
244
246
248 fGlyphs.reset(glyphIDs.size());
249 return fStrike->prepareImages(glyphIDs, fGlyphs.get());
250}
251
253 return this->glyphs(SkSpan<const SkPackedGlyphID>{&packedID, 1})[0];
254}
255
257 return fStrike->getDescriptor();
258}
int count
Definition: FontMgrTest.cpp:50
@ kNone
glyph outlines unchanged
sk_sp< T > sk_ref_sp(T *obj)
Definition: SkRefCnt.h:381
SkScalerContextFlags
uint16_t SkGlyphID
Definition: SkTypes.h:179
SkDescriptor * getDesc() const
Definition: SkDescriptor.h:103
SkSpan< const SkGlyph * > glyphs(SkSpan< const SkGlyphID > glyphIDs)
SkBulkGlyphMetricsAndDrawables(const SkStrikeSpec &spec)
const SkGlyph * glyph(SkGlyphID glyphID)
const SkDescriptor & descriptor() const
SkSpan< const SkGlyph * > glyphs(SkSpan< const SkPackedGlyphID > packedIDs)
SkBulkGlyphMetricsAndImages(const SkStrikeSpec &spec)
const SkGlyph * glyph(SkPackedGlyphID packedID)
SkBulkGlyphMetricsAndPaths(const SkStrikeSpec &spec)
SkSpan< const SkGlyph * > glyphs(SkSpan< const SkGlyphID > glyphIDs)
void findIntercepts(const SkScalar bounds[2], SkScalar scale, SkScalar xPos, const SkGlyph *glyph, SkScalar *array, int *count)
const SkGlyph * glyph(SkGlyphID glyphID)
SkBulkGlyphMetrics(const SkStrikeSpec &spec)
SkSpan< const SkGlyph * > glyphs(SkSpan< const SkGlyphID > glyphIDs)
const SkGlyph * glyph(SkGlyphID glyphID)
SkString dumpRec() const
static SkMatrix MakeTextMatrix(SkScalar size, SkScalar scaleX, SkScalar skewX)
Definition: SkFontPriv.h:41
Definition: SkFont.h:35
@ kAlias
no transparent pixels on glyph edges
static constexpr int kMScaleX
horizontal scale factor
Definition: SkMatrix.h:353
SkMatrix & postConcat(const SkMatrix &other)
Definition: SkMatrix.cpp:683
static const SkMatrix & I()
Definition: SkMatrix.cpp:1544
bool hasPerspective() const
Definition: SkMatrix.h:312
static constexpr int kMSkewY
vertical skew factor
Definition: SkMatrix.h:356
static constexpr int kMScaleY
vertical scale factor
Definition: SkMatrix.h:357
static constexpr int kMSkewX
horizontal skew factor
Definition: SkMatrix.h:354
void reset()
Definition: SkPaint.cpp:103
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
static SkDescriptor * CreateDescriptorAndEffectsUsingPaint(const SkFont &, const SkPaint &, const SkSurfaceProps &, SkScalerContextFlags scalerContextFlags, const SkMatrix &deviceMatrix, SkAutoDescriptor *ad, SkScalerContextEffects *effects)
constexpr size_t size() const
Definition: SkSpan_impl.h:95
sk_sp< SkStrike > findOrCreateStrike(const SkStrikeSpec &strikeSpec) SK_EXCLUDES(fLock)
static SkStrikeCache * GlobalStrikeCache()
sk_sp< sktext::StrikeForGPU > findOrCreateScopedStrike(sktext::StrikeForGPUCacheInterface *cache) const
static SkStrikeSpec MakeTransformMask(const SkFont &font, const SkPaint &paint, const SkSurfaceProps &surfaceProps, SkScalerContextFlags scalerContextFlags, const SkMatrix &deviceMatrix)
static SkStrikeSpec MakeMask(const SkFont &font, const SkPaint &paint, const SkSurfaceProps &surfaceProps, SkScalerContextFlags scalerContextFlags, const SkMatrix &deviceMatrix)
SkString dump() const
static SkStrikeSpec MakeWithNoDevice(const SkFont &font, const SkPaint *paint=nullptr)
const SkTypeface & typeface() const
Definition: SkStrikeSpec.h:97
static bool ShouldDrawAsPath(const SkPaint &paint, const SkFont &font, const SkMatrix &matrix)
sk_sp< SkStrike > findOrCreateStrike() const
static std::tuple< SkStrikeSpec, SkScalar > MakeCanonicalized(const SkFont &font, const SkPaint *paint=nullptr)
static SkStrikeSpec MakePDFVector(const SkTypeface &typeface, int *size)
static std::tuple< SkStrikeSpec, SkScalar > MakePath(const SkFont &font, const SkPaint &paint, const SkSurfaceProps &surfaceProps, SkScalerContextFlags scalerContextFlags)
SkStrikeSpec(const SkDescriptor &descriptor, sk_sp< SkTypeface > typeface)
const SkDescriptor & getDescriptor() const override
Definition: SkStrike.h:103
SkSpan< const SkGlyph * > metrics(SkSpan< const SkGlyphID > glyphIDs, const SkGlyph *results[]) SK_EXCLUDES(fStrikeLock)
Definition: SkStrike.cpp:218
SkSpan< const SkGlyph * > preparePaths(SkSpan< const SkGlyphID > glyphIDs, const SkGlyph *results[]) SK_EXCLUDES(fStrikeLock)
Definition: SkStrike.cpp:224
void findIntercepts(const SkScalar bounds[2], SkScalar scale, SkScalar xPos, SkGlyph *, SkScalar *array, int *count) SK_EXCLUDES(fStrikeLock)
Definition: SkStrike.cpp:212
SkSpan< const SkGlyph * > prepareDrawables(SkSpan< const SkGlyphID > glyphIDs, const SkGlyph *results[]) SK_EXCLUDES(fStrikeLock)
Definition: SkStrike.cpp:243
SkSpan< const SkGlyph * > prepareImages(SkSpan< const SkPackedGlyphID > glyphIDs, const SkGlyph *results[]) SK_EXCLUDES(fStrikeLock)
Definition: SkStrike.cpp:230
T * set(const T &src)
Definition: SkTLazy.h:56
int getUnitsPerEm() const
Definition: SkTypeface.cpp:436
void reset(int count)
Definition: SkTemplates.h:195
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
Optional< SkRect > bounds
Definition: SkRecords.h:189
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace Enable an endless trace buffer The default is a ring buffer This is useful when very old events need to viewed For during application launch Memory usage will continue to grow indefinitely however Start app with an specific route defined on the framework flutter assets Path to the Flutter assets directory enable service port Allow the VM service to fallback to automatic port selection if binding to a specified port fails trace Trace early application lifecycle Automatically switches to an endless trace buffer trace skia Filters out all Skia trace event categories except those that are specified in this comma separated list dump skp on shader Automatically dump the skp that triggers new shader compilations This is useful for writing custom ShaderWarmUp to reduce jank By this is not enabled to reduce the overhead purge persistent cache
Definition: switches.h:191
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
font
Font Metadata and Metrics.
Definition: ref_ptr.h:256
const Scalar scale
SkPathEffect * fPathEffect
SkMaskFilter * fMaskFilter