SkRemoteGlyphCacheImpl.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #ifndef SkRemoteGlyphCacheImpl_DEFINED
  8. #define SkRemoteGlyphCacheImpl_DEFINED
  9. #include "src/core/SkArenaAlloc.h"
  10. #include "src/core/SkDescriptor.h"
  11. #include "src/core/SkGlyphRun.h"
  12. #include "src/core/SkGlyphRunPainter.h"
  13. #include "src/core/SkRemoteGlyphCache.h"
  14. class SkStrikeServer::SkGlyphCacheState : public SkStrikeInterface {
  15. public:
  16. // N.B. SkGlyphCacheState is not valid until ensureScalerContext is called.
  17. SkGlyphCacheState(const SkDescriptor& descriptor,
  18. std::unique_ptr<SkScalerContext> context,
  19. SkDiscardableHandleId discardableHandleId);
  20. ~SkGlyphCacheState() override;
  21. void addGlyph(SkPackedGlyphID, bool pathOnly);
  22. void writePendingGlyphs(Serializer* serializer);
  23. SkDiscardableHandleId discardableHandleId() const { return fDiscardableHandleId; }
  24. bool isSubpixel() const { return fIsSubpixel; }
  25. const SkDescriptor& getDescriptor() const override {
  26. return *fDescriptor.getDesc();
  27. }
  28. void setTypefaceAndEffects(const SkTypeface* typeface, SkScalerContextEffects effects);
  29. SkVector rounding() const override;
  30. SkIPoint subpixelMask() const override {
  31. return SkIPoint::Make((!fIsSubpixel || fAxisAlignment == kY_SkAxisAlignment) ? 0 : ~0u,
  32. (!fIsSubpixel || fAxisAlignment == kX_SkAxisAlignment) ? 0 : ~0u);
  33. }
  34. SkSpan<const SkGlyphPos>
  35. prepareForDrawing(const SkPackedGlyphID packedGlyphIDs[], const SkPoint positions[], size_t n,
  36. int maxDimension, PreparationDetail detail, SkGlyphPos results[]) override;
  37. void onAboutToExitScope() override {}
  38. private:
  39. bool hasPendingGlyphs() const {
  40. return !fPendingGlyphImages.empty() || !fPendingGlyphPaths.empty();
  41. }
  42. void writeGlyphPath(const SkPackedGlyphID& glyphID, Serializer* serializer) const;
  43. void ensureScalerContext();
  44. void resetScalerContext();
  45. // The set of glyphs cached on the remote client.
  46. SkTHashSet<SkPackedGlyphID> fCachedGlyphImages;
  47. SkTHashSet<SkPackedGlyphID> fCachedGlyphPaths;
  48. // The set of glyphs which has not yet been serialized and sent to the
  49. // remote client.
  50. std::vector<SkPackedGlyphID> fPendingGlyphImages;
  51. std::vector<SkPackedGlyphID> fPendingGlyphPaths;
  52. const SkAutoDescriptor fDescriptor;
  53. const SkDiscardableHandleId fDiscardableHandleId;
  54. // Values saved from the initial context.
  55. const bool fIsSubpixel;
  56. const SkAxisAlignment fAxisAlignment;
  57. // The context built using fDescriptor
  58. std::unique_ptr<SkScalerContext> fContext;
  59. // These fields are set every time getOrCreateCache. This allows the code to maintain the
  60. // fContext as lazy as possible.
  61. const SkTypeface* fTypeface{nullptr};
  62. SkScalerContextEffects fEffects;
  63. class GlyphMapHashTraits {
  64. public:
  65. static SkPackedGlyphID GetKey(const SkGlyph* glyph) {
  66. return glyph->getPackedID();
  67. }
  68. static uint32_t Hash(SkPackedGlyphID glyphId) {
  69. return glyphId.hash();
  70. }
  71. };
  72. // FallbackTextHelper cases require glyph metrics when analyzing a glyph run, in which case
  73. // we cache them here.
  74. SkTHashTable<SkGlyph*, SkPackedGlyphID, GlyphMapHashTraits> fGlyphMap;
  75. SkArenaAlloc fAlloc{256};
  76. };
  77. class SkTextBlobCacheDiffCanvas::TrackLayerDevice : public SkNoPixelsDevice {
  78. public:
  79. TrackLayerDevice(const SkIRect& bounds, const SkSurfaceProps& props, SkStrikeServer* server,
  80. sk_sp<SkColorSpace> colorSpace,
  81. const SkTextBlobCacheDiffCanvas::Settings& settings);
  82. SkBaseDevice* onCreateDevice(const CreateInfo& cinfo, const SkPaint*) override;
  83. protected:
  84. void drawGlyphRunList(const SkGlyphRunList& glyphRunList) override;
  85. private:
  86. SkStrikeServer* const fStrikeServer;
  87. const SkTextBlobCacheDiffCanvas::Settings fSettings;
  88. SkGlyphRunListPainter fPainter;
  89. };
  90. #endif // SkRemoteGlyphCacheImpl_DEFINED