GrStrikeCache.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright 2015 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. #ifndef GrStrikeCache_DEFINED
  8. #define GrStrikeCache_DEFINED
  9. #include "src/codec/SkMasks.h"
  10. #include "src/core/SkArenaAlloc.h"
  11. #include "src/core/SkStrike.h"
  12. #include "src/core/SkTDynamicHash.h"
  13. #include "src/gpu/GrDrawOpAtlas.h"
  14. #include "src/gpu/GrGlyph.h"
  15. class GrAtlasManager;
  16. class GrGpu;
  17. class GrStrikeCache;
  18. /**
  19. * The GrTextStrike manages a pool of CPU backing memory for GrGlyphs. This backing memory
  20. * is indexed by a PackedID and SkStrike. The SkStrike is what actually creates the mask.
  21. * The GrTextStrike may outlive the generating SkStrike. However, it retains a copy
  22. * of it's SkDescriptor as a key to access (or regenerate) the SkStrike. GrTextStrikes are
  23. * created by and owned by a GrStrikeCache.
  24. */
  25. class GrTextStrike : public SkNVRefCnt<GrTextStrike> {
  26. public:
  27. GrTextStrike(const SkDescriptor& fontScalerKey);
  28. GrGlyph* getGlyph(const SkGlyph& skGlyph) {
  29. GrGlyph* grGlyph = fCache.find(skGlyph.getPackedID());
  30. if (grGlyph == nullptr) {
  31. grGlyph = fAlloc.make<GrGlyph>(skGlyph);
  32. fCache.add(grGlyph);
  33. }
  34. return grGlyph;
  35. }
  36. // This variant of the above function is called by GrAtlasTextOp. At this point, it is possible
  37. // that the maskformat of the glyph differs from what we expect. In these cases we will just
  38. // draw a clear square.
  39. // skbug:4143 crbug:510931
  40. GrGlyph* getGlyph(SkPackedGlyphID packed, SkStrike* skStrike) {
  41. GrGlyph* grGlyph = fCache.find(packed);
  42. if (grGlyph == nullptr) {
  43. // We could return this to the caller, but in practice it adds code complexity for
  44. // potentially little benefit(ie, if the glyph is not in our font cache, then its not
  45. // in the atlas and we're going to be doing a texture upload anyways).
  46. grGlyph = fAlloc.make<GrGlyph>(*skStrike->glyph(packed));
  47. fCache.add(grGlyph);
  48. }
  49. return grGlyph;
  50. }
  51. // returns true if glyph successfully added to texture atlas, false otherwise. If the glyph's
  52. // mask format has changed, then addGlyphToAtlas will draw a clear box. This will almost never
  53. // happen.
  54. // TODO we can handle some of these cases if we really want to, but the long term solution is to
  55. // get the actual glyph image itself when we get the glyph metrics.
  56. GrDrawOpAtlas::ErrorCode addGlyphToAtlas(GrResourceProvider*, GrDeferredUploadTarget*,
  57. GrStrikeCache*, GrAtlasManager*, GrGlyph*,
  58. SkStrike*, GrMaskFormat expectedMaskFormat,
  59. bool isScaledGlyph);
  60. // testing
  61. int countGlyphs() const { return fCache.count(); }
  62. // remove any references to this plot
  63. void removeID(GrDrawOpAtlas::AtlasID);
  64. // If a TextStrike is abandoned by the cache, then the caller must get a new strike
  65. bool isAbandoned() const { return fIsAbandoned; }
  66. static const SkDescriptor& GetKey(const GrTextStrike& strike) {
  67. return *strike.fFontScalerKey.getDesc();
  68. }
  69. static uint32_t Hash(const SkDescriptor& desc) { return desc.getChecksum(); }
  70. private:
  71. SkTDynamicHash<GrGlyph, SkPackedGlyphID> fCache;
  72. SkAutoDescriptor fFontScalerKey;
  73. SkArenaAlloc fAlloc{512};
  74. int fAtlasedGlyphs{0};
  75. bool fIsAbandoned{false};
  76. friend class GrStrikeCache;
  77. };
  78. /**
  79. * GrStrikeCache manages strikes which are indexed by a SkStrike. These strikes can then be
  80. * used to generate individual Glyph Masks.
  81. */
  82. class GrStrikeCache {
  83. public:
  84. GrStrikeCache(const GrCaps* caps, size_t maxTextureBytes);
  85. ~GrStrikeCache();
  86. void setStrikeToPreserve(GrTextStrike* strike) { fPreserveStrike = strike; }
  87. // The user of the cache may hold a long-lived ref to the returned strike. However, actions by
  88. // another client of the cache may cause the strike to be purged while it is still reffed.
  89. // Therefore, the caller must check GrTextStrike::isAbandoned() if there are other
  90. // interactions with the cache since the strike was received.
  91. sk_sp<GrTextStrike> getStrike(const SkDescriptor& desc) {
  92. sk_sp<GrTextStrike> strike = sk_ref_sp(fCache.find(desc));
  93. if (!strike) {
  94. strike = this->generateStrike(desc);
  95. }
  96. return strike;
  97. }
  98. const SkMasks& getMasks() const { return *f565Masks; }
  99. void freeAll();
  100. static void HandleEviction(GrDrawOpAtlas::AtlasID, void*);
  101. private:
  102. sk_sp<GrTextStrike> generateStrike(const SkDescriptor& desc) {
  103. // 'fCache' get the construction ref
  104. sk_sp<GrTextStrike> strike = sk_ref_sp(new GrTextStrike(desc));
  105. fCache.add(strike.get());
  106. return strike;
  107. }
  108. using StrikeHash = SkTDynamicHash<GrTextStrike, SkDescriptor>;
  109. StrikeHash fCache;
  110. GrTextStrike* fPreserveStrike;
  111. std::unique_ptr<const SkMasks> f565Masks;
  112. };
  113. #endif // GrStrikeCache_DEFINED