• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #ifndef SkRemoteGlyphCacheImpl_DEFINED
9 #define SkRemoteGlyphCacheImpl_DEFINED
10 
11 #include "SkArenaAlloc.h"
12 #include "SkDescriptor.h"
13 #include "SkGlyphRun.h"
14 #include "SkGlyphRunPainter.h"
15 #include "SkRemoteGlyphCache.h"
16 
17 class SkStrikeServer::SkGlyphCacheState : public SkStrikeInterface {
18 public:
19     // N.B. SkGlyphCacheState is not valid until ensureScalerContext is called.
20     SkGlyphCacheState(const SkDescriptor& keyDescriptor,
21                       const SkDescriptor& deviceDescriptor,
22                       std::unique_ptr<SkScalerContext> context,
23                       SkDiscardableHandleId discardableHandleId);
24     ~SkGlyphCacheState() override;
25 
26     void addGlyph(SkPackedGlyphID, bool pathOnly);
27     void writePendingGlyphs(Serializer* serializer);
discardableHandleId()28     SkDiscardableHandleId discardableHandleId() const { return fDiscardableHandleId; }
getDeviceDescriptor()29     const SkDescriptor& getDeviceDescriptor() {
30         return *fDeviceDescriptor.getDesc();
31     }
32 
isSubpixel()33     bool isSubpixel() const { return fIsSubpixel; }
axisAlignmentForHText()34     SkAxisAlignment axisAlignmentForHText() const { return fAxisAlignmentForHText; }
35 
getKeyDescriptor()36     const SkDescriptor& getKeyDescriptor() {
37         return *fKeyDescriptor.getDesc();
38     }
39 
40     const SkGlyph& findGlyph(SkPackedGlyphID);
41 
42     void setFontAndEffects(const SkFont& font, SkScalerContextEffects effects);
43 
44     SkVector rounding() const override;
45 
46     const SkGlyph& getGlyphMetrics(SkGlyphID glyphID, SkPoint position) override;
47 
48     bool hasImage(const SkGlyph& glyph) override;
49 
50     bool hasPath(const SkGlyph& glyph) override;
51 
52 private:
hasPendingGlyphs()53     bool hasPendingGlyphs() const {
54         return !fPendingGlyphImages.empty() || !fPendingGlyphPaths.empty();
55     }
56     void writeGlyphPath(const SkPackedGlyphID& glyphID, Serializer* serializer) const;
57 
58     void ensureScalerContext();
59     void resetScalerContext();
60 
61     // The set of glyphs cached on the remote client.
62     SkTHashSet<SkPackedGlyphID> fCachedGlyphImages;
63     SkTHashSet<SkPackedGlyphID> fCachedGlyphPaths;
64 
65     // The set of glyphs which has not yet been serialized and sent to the
66     // remote client.
67     std::vector<SkPackedGlyphID> fPendingGlyphImages;
68     std::vector<SkPackedGlyphID> fPendingGlyphPaths;
69 
70     // The device descriptor is used to create the scaler context. The glyphs to have the
71     // correct device rendering. The key descriptor is used for communication. The GPU side will
72     // create descriptors with out the device filtering, thus matching the key descriptor.
73     const SkAutoDescriptor fKeyDescriptor;
74     const SkAutoDescriptor fDeviceDescriptor;
75 
76     const SkDiscardableHandleId fDiscardableHandleId;
77 
78     // Values saved from the initial context.
79     const bool fIsSubpixel;
80     const SkAxisAlignment fAxisAlignmentForHText;
81 
82     // The context built using fDeviceDescriptor
83     std::unique_ptr<SkScalerContext> fContext;
84 
85     // These fields are set everytime getOrCreateCache. This allows the code to maintain the
86     // fContext as lazy as possible.
87     const SkFont* fFont{nullptr};
88     SkScalerContextEffects fEffects;
89 
90     class GlyphMapHashTraits {
91     public:
GetKey(const SkGlyph * glyph)92         static SkPackedGlyphID GetKey(const SkGlyph* glyph) {
93             return glyph->getPackedID();
94         }
Hash(SkPackedGlyphID glyphId)95         static uint32_t Hash(SkPackedGlyphID glyphId) {
96             return glyphId.hash();
97         }
98     };
99 
100     // FallbackTextHelper cases require glyph metrics when analyzing a glyph run, in which case
101     // we cache them here.
102     SkTHashTable<SkGlyph*, SkPackedGlyphID, GlyphMapHashTraits> fGlyphMap;
103 
104     SkArenaAlloc fAlloc{256};
105 };
106 
107 class SkTextBlobCacheDiffCanvas::TrackLayerDevice : public SkNoPixelsDevice {
108 public:
109     TrackLayerDevice(const SkIRect& bounds, const SkSurfaceProps& props, SkStrikeServer* server,
110                      const SkTextBlobCacheDiffCanvas::Settings& settings);
111 
112     SkBaseDevice* onCreateDevice(const CreateInfo& cinfo, const SkPaint*) override;
113 
114 protected:
115     void drawGlyphRunList(const SkGlyphRunList& glyphRunList) override;
116 
117 private:
118     void processGlyphRun(
119             const SkPoint& origin, const SkGlyphRun& glyphRun, const SkPaint& runPaint);
120 
121     void processGlyphRunForMask(
122             const SkGlyphRun& glyphRun, const SkMatrix& runMatrix,
123             SkPoint origin, const SkPaint& paint);
124 
125     void processGlyphRunForPaths(
126             const SkGlyphRun& glyphRun, const SkMatrix& runMatrix,
127             SkPoint origin, const SkPaint& paint);
128 
129 #if SK_SUPPORT_GPU
130     bool maybeProcessGlyphRunForDFT(
131             const SkGlyphRun& glyphRun, const SkMatrix& runMatrix,
132             SkPoint origin, const SkPaint& paint);
133 #endif
134 
135     SkStrikeServer* const fStrikeServer;
136     const SkTextBlobCacheDiffCanvas::Settings fSettings;
137     SkGlyphRunListPainter fPainter;
138 };
139 
140 #endif // SkRemoteGlyphCacheImpl_DEFINED
141