blob: 8083708a9b38378b16fd8d095bf127ac97c839aa [file] [log] [blame]
Mike Kleina8a51ce2018-01-09 12:34:11 -05001/*
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
Mike Kleina8a51ce2018-01-09 12:34:11 -05008#include "SkTypeface_remote.h"
Khushal8523b6b2018-06-12 11:26:17 -07009#include "SkGlyphCache.h"
Mike Kleinfc6cf922018-05-23 00:16:47 +000010#include "SkPaint.h"
Khushald4160832018-05-23 18:16:00 -070011#include "SkRemoteGlyphCache.h"
12#include "SkStrikeCache.h"
13#include "SkTraceEvent.h"
Khushal38a08432018-05-02 10:29:37 -070014
15SkScalerContextProxy::SkScalerContextProxy(sk_sp<SkTypeface> tf,
16 const SkScalerContextEffects& effects,
17 const SkDescriptor* desc,
Khushald4160832018-05-23 18:16:00 -070018 sk_sp<SkStrikeClient::DiscardableHandleManager> manager)
Herb Derbyfeb9fa92018-06-01 16:47:21 -040019 : SkScalerContext{std::move(tf), effects, desc}
20 , fDiscardableManager{std::move(manager)} {}
Herb Derby97be88f2018-03-21 16:23:49 -040021
Herb Derby5c0c7982018-06-21 15:15:50 -040022void SkScalerContextProxy::initCache(SkGlyphCache* cache, SkStrikeCache* strikeCache) {
Khushal8523b6b2018-06-12 11:26:17 -070023 SkASSERT(fCache == nullptr);
24 SkASSERT(cache != nullptr);
25
26 fCache = cache;
Herb Derby5c0c7982018-06-21 15:15:50 -040027 fStrikeCache = strikeCache;
Khushal8523b6b2018-06-12 11:26:17 -070028}
29
Herb Derby97be88f2018-03-21 16:23:49 -040030unsigned SkScalerContextProxy::generateGlyphCount() {
31 SK_ABORT("Should never be called.");
32 return 0;
33}
34
35uint16_t SkScalerContextProxy::generateCharToGlyph(SkUnichar) {
36 SK_ABORT("Should never be called.");
37 return 0;
38}
39
Ben Wagnere5416452018-08-09 14:03:42 -040040bool SkScalerContextProxy::generateAdvance(SkGlyph* glyph) {
41 return false;
42}
Mike Kleina8a51ce2018-01-09 12:34:11 -050043
44void SkScalerContextProxy::generateMetrics(SkGlyph* glyph) {
Khushald4160832018-05-23 18:16:00 -070045 TRACE_EVENT1("skia", "generateMetrics", "rec", TRACE_STR_COPY(this->getRec().dump().c_str()));
Herb Derbyfeb9fa92018-06-01 16:47:21 -040046 if (this->getProxyTypeface()->isLogging()) {
47 SkDebugf("GlyphCacheMiss generateMetrics: %s\n", this->getRec().dump().c_str());
48 }
Khushald4160832018-05-23 18:16:00 -070049
Ben Wagnere5416452018-08-09 14:03:42 -040050 glyph->fMaskFormat = fRec.fMaskFormat;
51
Khushal8523b6b2018-06-12 11:26:17 -070052 // Since the scaler context is being called, we don't have the needed data. Try to find a
53 // fallback before failing.
54 if (fCache && fCache->belongsToCache(glyph)) {
55 // First check the original cache, in case there is a sub-pixel pos mismatch.
56 if (const auto* fallback =
57 fCache->getCachedGlyphAnySubPix(glyph->getGlyphID(), glyph->getPackedID())) {
58 fCache->initializeGlyphFromFallback(glyph, *fallback);
59 fDiscardableManager->notifyCacheMiss(
60 SkStrikeClient::CacheMissType::kGlyphMetricsFallback);
61 return;
62 }
Herb Derby65b7bfc2018-06-05 13:32:12 -040063
Khushal8523b6b2018-06-12 11:26:17 -070064 // Now check other caches for a desc mismatch.
Herb Derby5c0c7982018-06-21 15:15:50 -040065 if (fStrikeCache->desperationSearchForImage(fCache->getDescriptor(), glyph, fCache)) {
Khushal8523b6b2018-06-12 11:26:17 -070066 fDiscardableManager->notifyCacheMiss(
67 SkStrikeClient::CacheMissType::kGlyphMetricsFallback);
68 return;
69 }
Herb Derby65b7bfc2018-06-05 13:32:12 -040070 }
Khushal8523b6b2018-06-12 11:26:17 -070071
72 glyph->zeroMetrics();
Khushalfa8ff092018-06-06 17:46:38 -070073 fDiscardableManager->notifyCacheMiss(SkStrikeClient::CacheMissType::kGlyphMetrics);
Mike Kleina8a51ce2018-01-09 12:34:11 -050074}
75
76void SkScalerContextProxy::generateImage(const SkGlyph& glyph) {
Khushald4160832018-05-23 18:16:00 -070077 TRACE_EVENT1("skia", "generateImage", "rec", TRACE_STR_COPY(this->getRec().dump().c_str()));
Herb Derbyfeb9fa92018-06-01 16:47:21 -040078 if (this->getProxyTypeface()->isLogging()) {
79 SkDebugf("GlyphCacheMiss generateImage: %s\n", this->getRec().dump().c_str());
80 }
Khushald4160832018-05-23 18:16:00 -070081
Herb Derby65b7bfc2018-06-05 13:32:12 -040082 // There is no desperation search here, because if there was an image to be found it was
83 // copied over with the metrics search.
Khushalfa8ff092018-06-06 17:46:38 -070084 fDiscardableManager->notifyCacheMiss(SkStrikeClient::CacheMissType::kGlyphImage);
Mike Kleina8a51ce2018-01-09 12:34:11 -050085}
86
Ben Wagner5ddb3082018-03-29 11:18:06 -040087bool SkScalerContextProxy::generatePath(SkGlyphID glyphID, SkPath* path) {
Khushald4160832018-05-23 18:16:00 -070088 TRACE_EVENT1("skia", "generatePath", "rec", TRACE_STR_COPY(this->getRec().dump().c_str()));
Herb Derbyfeb9fa92018-06-01 16:47:21 -040089 if (this->getProxyTypeface()->isLogging()) {
90 SkDebugf("GlyphCacheMiss generatePath: %s\n", this->getRec().dump().c_str());
91 }
Khushal8523b6b2018-06-12 11:26:17 -070092
93 // Since the scaler context is being called, we don't have the needed data. Try to find a
94 // fallback before failing.
Herb Derby65b7bfc2018-06-05 13:32:12 -040095 auto desc = SkScalerContext::DescriptorGivenRecAndEffects(this->getRec(), this->getEffects());
Khushal9dd1e822018-10-09 13:12:42 -070096 bool foundPath = fStrikeCache && fStrikeCache->desperationSearchForPath(*desc, glyphID, path);
Khushal8523b6b2018-06-12 11:26:17 -070097 fDiscardableManager->notifyCacheMiss(foundPath
98 ? SkStrikeClient::CacheMissType::kGlyphPathFallback
99 : SkStrikeClient::CacheMissType::kGlyphPath);
Herb Derby65b7bfc2018-06-05 13:32:12 -0400100 return foundPath;
Mike Kleina8a51ce2018-01-09 12:34:11 -0500101}
102
103void SkScalerContextProxy::generateFontMetrics(SkPaint::FontMetrics* metrics) {
Khushald4160832018-05-23 18:16:00 -0700104 TRACE_EVENT1(
105 "skia", "generateFontMetrics", "rec", TRACE_STR_COPY(this->getRec().dump().c_str()));
Herb Derbyfeb9fa92018-06-01 16:47:21 -0400106 if (this->getProxyTypeface()->isLogging()) {
107 SkDebugf("GlyphCacheMiss generateFontMetrics: %s\n", this->getRec().dump().c_str());
108 SkDEBUGCODE(SkStrikeCache::Dump());
109 }
Mike Kleina8a51ce2018-01-09 12:34:11 -0500110
Herb Derby65b7bfc2018-06-05 13:32:12 -0400111 // Font metrics aren't really used for render, so just zero out the data and return.
Khushalfa8ff092018-06-06 17:46:38 -0700112 fDiscardableManager->notifyCacheMiss(SkStrikeClient::CacheMissType::kFontMetrics);
Khushald4160832018-05-23 18:16:00 -0700113 sk_bzero(metrics, sizeof(*metrics));
Mike Kleina8a51ce2018-01-09 12:34:11 -0500114}
Herb Derbyfeb9fa92018-06-01 16:47:21 -0400115
116SkTypefaceProxy* SkScalerContextProxy::getProxyTypeface() const {
117 return (SkTypefaceProxy*)this->getTypeface();
Khushalfa8ff092018-06-06 17:46:38 -0700118}