1 /*
2  * Copyright 2018 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/SkPaint.h"
9 #include "src/core/SkRemoteGlyphCache.h"
10 #include "src/core/SkStrike.h"
11 #include "src/core/SkStrikeCache.h"
12 #include "src/core/SkTraceEvent.h"
13 #include "src/core/SkTypeface_remote.h"
14 
SkScalerContextProxy(sk_sp<SkTypeface> tf,const SkScalerContextEffects & effects,const SkDescriptor * desc,sk_sp<SkStrikeClient::DiscardableHandleManager> manager)15 SkScalerContextProxy::SkScalerContextProxy(sk_sp<SkTypeface> tf,
16                                            const SkScalerContextEffects& effects,
17                                            const SkDescriptor* desc,
18                                            sk_sp<SkStrikeClient::DiscardableHandleManager> manager)
19         : SkScalerContext{std::move(tf), effects, desc}
20         , fDiscardableManager{std::move(manager)} {}
21 
initCache(SkStrike * cache,SkStrikeCache * strikeCache)22 void SkScalerContextProxy::initCache(SkStrike* cache, SkStrikeCache* strikeCache) {
23     SkASSERT(fCache == nullptr);
24     SkASSERT(cache != nullptr);
25 
26     fCache = cache;
27     fStrikeCache = strikeCache;
28 }
29 
generateGlyphCount()30 unsigned SkScalerContextProxy::generateGlyphCount()  {
31     SK_ABORT("Should never be called.");
32 }
33 
generateAdvance(SkGlyph * glyph)34 bool SkScalerContextProxy::generateAdvance(SkGlyph* glyph) {
35     return false;
36 }
37 
generateMetrics(SkGlyph * glyph)38 void SkScalerContextProxy::generateMetrics(SkGlyph* glyph) {
39     TRACE_EVENT1("skia", "generateMetrics", "rec", TRACE_STR_COPY(this->getRec().dump().c_str()));
40     if (this->getProxyTypeface()->isLogging()) {
41         SkDebugf("GlyphCacheMiss generateMetrics: %s\n", this->getRec().dump().c_str());
42     }
43 
44     glyph->fMaskFormat = fRec.fMaskFormat;
45 
46     // Since the scaler context is being called, we don't have the needed data. Try to find a
47     // fallback before failing.
48     if (fCache && fCache->belongsToCache(glyph)) {
49         // First check the original cache, in case there is a sub-pixel pos mismatch.
50         if (const SkGlyph* from =
51                     fCache->getCachedGlyphAnySubPix(glyph->getGlyphID(), glyph->getPackedID())) {
52             fCache->mergeGlyphAndImage(glyph->getPackedID(), *from);
53             fDiscardableManager->notifyCacheMiss(
54                     SkStrikeClient::CacheMissType::kGlyphMetricsFallback);
55             return;
56         }
57 
58         // Now check other caches for a desc mismatch.
59         if (fStrikeCache->desperationSearchForImage(fCache->getDescriptor(), glyph, fCache)) {
60             fDiscardableManager->notifyCacheMiss(
61                     SkStrikeClient::CacheMissType::kGlyphMetricsFallback);
62             return;
63         }
64     }
65 
66     glyph->zeroMetrics();
67     fDiscardableManager->notifyCacheMiss(SkStrikeClient::CacheMissType::kGlyphMetrics);
68 }
69 
generateImage(const SkGlyph & glyph)70 void SkScalerContextProxy::generateImage(const SkGlyph& glyph) {
71     TRACE_EVENT1("skia", "generateImage", "rec", TRACE_STR_COPY(this->getRec().dump().c_str()));
72     if (this->getProxyTypeface()->isLogging()) {
73         SkDebugf("GlyphCacheMiss generateImage: %s\n", this->getRec().dump().c_str());
74     }
75 
76     // There is no desperation search here, because if there was an image to be found it was
77     // copied over with the metrics search.
78     fDiscardableManager->notifyCacheMiss(SkStrikeClient::CacheMissType::kGlyphImage);
79 }
80 
generatePath(SkGlyphID glyphID,SkPath * path)81 bool SkScalerContextProxy::generatePath(SkGlyphID glyphID, SkPath* path) {
82     TRACE_EVENT1("skia", "generatePath", "rec", TRACE_STR_COPY(this->getRec().dump().c_str()));
83     if (this->getProxyTypeface()->isLogging()) {
84         SkDebugf("GlyphCacheMiss generatePath: %s\n", this->getRec().dump().c_str());
85     }
86 
87     // Since the scaler context is being called, we don't have the needed data. Try to find a
88     // fallback before failing.
89     auto desc = SkScalerContext::DescriptorGivenRecAndEffects(this->getRec(), this->getEffects());
90     bool foundPath = fStrikeCache && fStrikeCache->desperationSearchForPath(*desc, glyphID, path);
91     fDiscardableManager->notifyCacheMiss(foundPath
92                                                  ? SkStrikeClient::CacheMissType::kGlyphPathFallback
93                                                  : SkStrikeClient::CacheMissType::kGlyphPath);
94     return foundPath;
95 }
96 
generateFontMetrics(SkFontMetrics * metrics)97 void SkScalerContextProxy::generateFontMetrics(SkFontMetrics* metrics) {
98     TRACE_EVENT1(
99             "skia", "generateFontMetrics", "rec", TRACE_STR_COPY(this->getRec().dump().c_str()));
100     if (this->getProxyTypeface()->isLogging()) {
101         SkDebugf("GlyphCacheMiss generateFontMetrics: %s\n", this->getRec().dump().c_str());
102         SkDEBUGCODE(SkStrikeCache::Dump());
103     }
104 
105     // Font metrics aren't really used for render, so just zero out the data and return.
106     fDiscardableManager->notifyCacheMiss(SkStrikeClient::CacheMissType::kFontMetrics);
107     sk_bzero(metrics, sizeof(*metrics));
108 }
109 
getProxyTypeface() const110 SkTypefaceProxy* SkScalerContextProxy::getProxyTypeface() const {
111     return (SkTypefaceProxy*)this->getTypeface();
112 }
113