From 0f27b5ee2b23190d6bdb4cd95aab24b544db087d Mon Sep 17 00:00:00 2001 From: Herb Derby Date: Mon, 4 Mar 2019 11:20:28 -0500 Subject: [PATCH] Introduce SkStrikeCacheInterface Introduce SkStrikeCacheInterface in order to move from a template a based interface on SkGlyphRunListPainter to a class based interface. Change-Id: Ib15e437420c00f4e11242ac1a4d8a87ee2af9ee1 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/197101 Reviewed-by: Ben Wagner Commit-Queue: Herb Derby --- gn/core.gni | 5 +- src/core/SkGlyphRunPainter.cpp | 7 +-- src/core/SkGlyphRunPainter.h | 39 ---------------- src/core/SkRemoteGlyphCache.cpp | 30 +++++++----- src/core/SkRemoteGlyphCache.h | 15 ++++-- src/core/SkScalerContext.h | 13 +----- src/core/SkStrike.h | 1 + src/core/SkStrikeCache.h | 6 +-- src/core/SkStrikeInterface.h | 81 +++++++++++++++++++++++++++++++++ 9 files changed, 121 insertions(+), 76 deletions(-) create mode 100644 src/core/SkStrikeInterface.h diff --git a/gn/core.gni b/gn/core.gni index 469e6fa282..c03fd3e09e 100644 --- a/gn/core.gni +++ b/gn/core.gni @@ -133,8 +133,6 @@ skia_core_sources = [ "$_src/core/SkGlobalInitialization_core.cpp", "$_src/core/SkGlyph.h", "$_src/core/SkGlyph.cpp", - "$_src/core/SkStrike.cpp", - "$_src/core/SkStrike.h", "$_src/core/SkGlyphRun.cpp", "$_src/core/SkGlyphRun.h", "$_src/core/SkGlyphRunPainter.cpp", @@ -264,8 +262,11 @@ skia_core_sources = [ "$_src/core/SkSpriteBlitter.h", "$_src/core/SkStream.cpp", "$_src/core/SkStreamPriv.h", + "$_src/core/SkStrike.cpp", + "$_src/core/SkStrike.h", "$_src/core/SkStrikeCache.cpp", "$_src/core/SkStrikeCache.h", + "$_src/core/SkStrikeInterface.h", "$_src/core/SkString.cpp", "$_src/core/SkStringUtils.cpp", "$_src/core/SkStroke.h", diff --git a/src/core/SkGlyphRunPainter.cpp b/src/core/SkGlyphRunPainter.cpp index 0081f13f02..74c4a51fb2 100644 --- a/src/core/SkGlyphRunPainter.cpp +++ b/src/core/SkGlyphRunPainter.cpp @@ -29,6 +29,7 @@ #include "SkPathEffect.h" #include "SkRasterClip.h" #include "SkRemoteGlyphCacheImpl.h" +#include "SkStrikeInterface.h" #include "SkStrike.h" #include "SkStrikeCache.h" #include "SkTDArray.h" @@ -1074,7 +1075,7 @@ void SkTextBlobCacheDiffCanvas::TrackLayerDevice::processGlyphRunForMask( auto creator = [this] (const SkDescriptor& desc, SkScalerContextEffects effects, const SkTypeface& typeface) { - return SkScopedStrike{fStrikeServer->getOrCreateCache(desc, typeface, effects)}; + return fStrikeServer->findOrCreateScopedStrike(desc, effects, typeface); }; auto processMasks = [] ( @@ -1099,7 +1100,7 @@ void SkTextBlobCacheDiffCanvas::TrackLayerDevice::processGlyphRunForPaths( auto creator = [this] (const SkDescriptor& desc, SkScalerContextEffects effects, const SkTypeface& typeface) { - return SkScopedStrike{fStrikeServer->getOrCreateCache(desc, typeface, effects)}; + return fStrikeServer->findOrCreateScopedStrike(desc, effects, typeface); }; // This processor is empty because all changes to the cache are tracked through @@ -1160,7 +1161,7 @@ bool SkTextBlobCacheDiffCanvas::TrackLayerDevice::maybeProcessGlyphRunForDFT( auto creator = [this] (const SkDescriptor& desc, SkScalerContextEffects effects, const SkTypeface& typeface) { - return SkScopedStrike{fStrikeServer->getOrCreateCache(desc, typeface, effects)}; + return fStrikeServer->findOrCreateScopedStrike(desc, effects, typeface); }; fPainter.drawGlyphRunAsSDFWithARGBFallback( diff --git a/src/core/SkGlyphRunPainter.h b/src/core/SkGlyphRunPainter.h index 3af38c7de0..85bfbf615f 100644 --- a/src/core/SkGlyphRunPainter.h +++ b/src/core/SkGlyphRunPainter.h @@ -20,45 +20,6 @@ class GrColorSpaceInfo; class GrRenderTargetContext; #endif -class SkStrikeSpec { -public: - SkStrikeSpec(const SkDescriptor& desc, - const SkTypeface& typeface, - const SkScalerContextEffects& effects) - : fDesc{desc} - , fTypeface{typeface} - , fEffects{effects} {} - - - const SkDescriptor& desc() const { return fDesc; } - const SkTypeface& typeface() const { return fTypeface; } - SkScalerContextEffects effects() const {return fEffects; } - -private: - const SkDescriptor& fDesc; - const SkTypeface& fTypeface; - const SkScalerContextEffects fEffects; -}; - -class SkStrikeInterface { -public: - virtual ~SkStrikeInterface() = default; - virtual SkVector rounding() const = 0; - virtual const SkDescriptor& getDescriptor() const = 0; - virtual SkStrikeSpec strikeSpec() const = 0; - virtual const SkGlyph& getGlyphMetrics(SkGlyphID glyphID, SkPoint position) = 0; - virtual bool decideCouldDrawFromPath(const SkGlyph& glyph) = 0; - virtual void onAboutToExitScope() = 0; - - struct Deleter { - void operator()(SkStrikeInterface* ptr) const { - ptr->onAboutToExitScope(); - } - }; -}; - -using SkScopedStrike = std::unique_ptr; - class SkStrikeCommon { public: static SkVector PixelRounding(bool isSubpixel, SkAxisAlignment axisAlignment); diff --git a/src/core/SkRemoteGlyphCache.cpp b/src/core/SkRemoteGlyphCache.cpp index f62ae0c96b..a75c47b634 100644 --- a/src/core/SkRemoteGlyphCache.cpp +++ b/src/core/SkRemoteGlyphCache.cpp @@ -315,6 +315,24 @@ SkStrikeServer::SkGlyphCacheState* SkStrikeServer::getOrCreateCache( } +SkScopedStrike SkStrikeServer::findOrCreateScopedStrike(const SkDescriptor& desc, + const SkScalerContextEffects& effects, + const SkTypeface& typeface) { + return SkScopedStrike{this->getOrCreateCache(desc, typeface, effects)}; +} + +void SkStrikeServer::checkForDeletedEntries() { + auto it = fRemoteGlyphStateMap.begin(); + while (fRemoteGlyphStateMap.size() > fMaxEntriesInDescriptorMap && + it != fRemoteGlyphStateMap.end()) { + if (fDiscardableHandleManager->isHandleDeleted(it->second->discardableHandleId())) { + it = fRemoteGlyphStateMap.erase(it); + } else { + ++it; + } + } +} + SkStrikeServer::SkGlyphCacheState* SkStrikeServer::getOrCreateCache( const SkDescriptor& desc, const SkTypeface& typeface, SkScalerContextEffects effects) { @@ -381,18 +399,6 @@ SkStrikeServer::SkGlyphCacheState* SkStrikeServer::getOrCreateCache( return cacheStatePtr; } -void SkStrikeServer::checkForDeletedEntries() { - auto it = fRemoteGlyphStateMap.begin(); - while (fRemoteGlyphStateMap.size() > fMaxEntriesInDescriptorMap && - it != fRemoteGlyphStateMap.end()) { - if (fDiscardableHandleManager->isHandleDeleted(it->second->discardableHandleId())) { - it = fRemoteGlyphStateMap.erase(it); - } else { - ++it; - } - } -} - // -- SkGlyphCacheState ---------------------------------------------------------------------------- SkStrikeServer::SkGlyphCacheState::SkGlyphCacheState( const SkDescriptor& descriptor, diff --git a/src/core/SkRemoteGlyphCache.h b/src/core/SkRemoteGlyphCache.h index eb231cd869..7b0dd187c2 100644 --- a/src/core/SkRemoteGlyphCache.h +++ b/src/core/SkRemoteGlyphCache.h @@ -22,6 +22,7 @@ #include "SkNoDrawCanvas.h" #include "SkRefCnt.h" #include "SkSerialProcs.h" +#include "SkStrikeInterface.h" #include "SkTypeface.h" class Serializer; @@ -86,7 +87,7 @@ private: using SkDiscardableHandleId = uint32_t; // This class is not thread-safe. -class SK_API SkStrikeServer { +class SK_API SkStrikeServer final : public SkStrikeCacheInterface { public: // An interface used by the server to create handles for pinning SkStrike // entries on the remote client. @@ -113,7 +114,7 @@ public: }; explicit SkStrikeServer(DiscardableHandleManager* discardableHandleManager); - ~SkStrikeServer(); + ~SkStrikeServer() override; // Serializes the typeface to be remoted using this server. sk_sp serializeTypeface(SkTypeface*); @@ -133,9 +134,9 @@ public: SkScalerContextFlags flags, SkScalerContextEffects* effects); - SkGlyphCacheState* getOrCreateCache(const SkDescriptor& desc, - const SkTypeface& typeface, - SkScalerContextEffects effects); + SkScopedStrike findOrCreateScopedStrike(const SkDescriptor& desc, + const SkScalerContextEffects& effects, + const SkTypeface& typeface) override; void setMaxEntriesInDescriptorMapForTesting(size_t count) { fMaxEntriesInDescriptorMap = count; @@ -147,6 +148,10 @@ private: void checkForDeletedEntries(); + SkGlyphCacheState* getOrCreateCache(const SkDescriptor& desc, + const SkTypeface& typeface, + SkScalerContextEffects effects); + SkDescriptorMap> fRemoteGlyphStateMap; DiscardableHandleManager* const fDiscardableHandleManager; SkTHashSet fCachedTypefaces; diff --git a/src/core/SkScalerContext.h b/src/core/SkScalerContext.h index 55a736c889..8338b50b52 100644 --- a/src/core/SkScalerContext.h +++ b/src/core/SkScalerContext.h @@ -19,6 +19,7 @@ #include "SkMaskGamma.h" #include "SkMatrix.h" #include "SkPaint.h" +#include "SkStrikeInterface.h" #include "SkSurfacePriv.h" #include "SkTypeface.h" #include "SkWriteBuffer.h" @@ -37,18 +38,6 @@ enum SkScalerContextFlags : uint32_t { kFakeGammaAndBoostContrast = kFakeGamma | kBoostContrast, }; -struct SkScalerContextEffects { - SkScalerContextEffects() : fPathEffect(nullptr), fMaskFilter(nullptr) {} - SkScalerContextEffects(SkPathEffect* pe, SkMaskFilter* mf) - : fPathEffect(pe), fMaskFilter(mf) {} - explicit SkScalerContextEffects(const SkPaint& paint) - : fPathEffect(paint.getPathEffect()) - , fMaskFilter(paint.getMaskFilter()) {} - - SkPathEffect* fPathEffect; - SkMaskFilter* fMaskFilter; -}; - enum SkAxisAlignment : uint32_t { kNone_SkAxisAlignment, kX_SkAxisAlignment, diff --git a/src/core/SkStrike.h b/src/core/SkStrike.h index eec5c24289..9136872702 100644 --- a/src/core/SkStrike.h +++ b/src/core/SkStrike.h @@ -16,6 +16,7 @@ #include "SkPaint.h" #include "SkTHash.h" #include "SkScalerContext.h" +#include "SkStrikeInterface.h" #include "SkTemplates.h" #include diff --git a/src/core/SkStrikeCache.h b/src/core/SkStrikeCache.h index 374fe93a53..8aeb63d247 100644 --- a/src/core/SkStrikeCache.h +++ b/src/core/SkStrikeCache.h @@ -39,12 +39,12 @@ public: virtual bool canDelete() = 0; }; -class SkStrikeCache { +class SkStrikeCache final : public SkStrikeCacheInterface { class Node; public: SkStrikeCache() = default; - ~SkStrikeCache(); + ~SkStrikeCache() override; class ExclusiveStrikePtr { public: @@ -116,7 +116,7 @@ public: SkScopedStrike findOrCreateScopedStrike(const SkDescriptor& desc, const SkScalerContextEffects& effects, - const SkTypeface& typeface); + const SkTypeface& typeface) override; static ExclusiveStrikePtr FindOrCreateStrikeExclusive( const SkFont& font, diff --git a/src/core/SkStrikeInterface.h b/src/core/SkStrikeInterface.h new file mode 100644 index 0000000000..edee4fb712 --- /dev/null +++ b/src/core/SkStrikeInterface.h @@ -0,0 +1,81 @@ +/* + * Copyright 2019 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SkStrikeInterface_DEFINED +#define SkStrikeInterface_DEFINED + +#include + +#include "SkPoint.h" +#include "SkTypes.h" + +class SkDescriptor; +class SkGlyph; +class SkMaskFilter; +class SkPathEffect; +class SkTypeface; + +// TODO: rename SkScalerContextEffects -> SkStrikeEffects +struct SkScalerContextEffects { + SkScalerContextEffects() : fPathEffect(nullptr), fMaskFilter(nullptr) {} + SkScalerContextEffects(SkPathEffect* pe, SkMaskFilter* mf) + : fPathEffect(pe), fMaskFilter(mf) {} + explicit SkScalerContextEffects(const SkPaint& paint) + : fPathEffect(paint.getPathEffect()) + , fMaskFilter(paint.getMaskFilter()) {} + + SkPathEffect* fPathEffect; + SkMaskFilter* fMaskFilter; +}; + +class SkStrikeSpec { +public: + SkStrikeSpec(const SkDescriptor& desc, + const SkTypeface& typeface, + const SkScalerContextEffects& effects) + : fDesc{desc} + , fTypeface{typeface} + , fEffects{effects} {} + + const SkDescriptor& desc() const { return fDesc; } + const SkTypeface& typeface() const { return fTypeface; } + SkScalerContextEffects effects() const {return fEffects; } + +private: + const SkDescriptor& fDesc; + const SkTypeface& fTypeface; + const SkScalerContextEffects fEffects; +}; + +class SkStrikeInterface { +public: + virtual ~SkStrikeInterface() = default; + virtual SkVector rounding() const = 0; + virtual const SkDescriptor& getDescriptor() const = 0; + virtual SkStrikeSpec strikeSpec() const = 0; + virtual const SkGlyph& getGlyphMetrics(SkGlyphID glyphID, SkPoint position) = 0; + virtual bool decideCouldDrawFromPath(const SkGlyph& glyph) = 0; + virtual void onAboutToExitScope() = 0; + + struct Deleter { + void operator()(SkStrikeInterface* ptr) const { + ptr->onAboutToExitScope(); + } + }; +}; + +using SkScopedStrike = std::unique_ptr; + +class SkStrikeCacheInterface { +public: + virtual ~SkStrikeCacheInterface() = default; + virtual SkScopedStrike findOrCreateScopedStrike(const SkDescriptor& desc, + const SkScalerContextEffects& effects, + const SkTypeface& typeface) = 0; +}; + +#endif //SkStrikeInterface_DEFINED