SkTypeface_remote.cpp 4.6 KB

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