• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #ifndef sktext_gpu_TextBlobRedrawCoordinator_DEFINED
9 #define sktext_gpu_TextBlobRedrawCoordinator_DEFINED
10 
11 #include "include/core/SkRefCnt.h"
12 #include "include/private/SkSpinlock.h"
13 #include "include/private/base/SkTArray.h"
14 #include "src/core/SkMessageBus.h"
15 #include "src/core/SkTHash.h"
16 #include "src/core/SkTextBlobPriv.h"
17 #include "src/text/gpu/TextBlob.h"
18 
19 #include <functional>
20 
21 class GrTextBlobTestingPeer;
22 
23 namespace sktext::gpu {
24 
25 // TextBlobRedrawCoordinator reuses data from previous drawing operations using multiple criteria
26 // to pick the best data for the draw. In addition, it provides a central service for managing
27 // resource usage through a messageBus.
28 // The draw data is stored in a three-tiered system. The first tier is keyed by the SkTextBlob's
29 // uniqueID. The second tier uses the sktext::gpu::TextBlob's key to get a general match for the
30 // draw. The last tier queries each sub run using canReuse to determine if each sub run can handle
31 // the drawing parameters.
32 class TextBlobRedrawCoordinator {
33 public:
34     TextBlobRedrawCoordinator(uint32_t messageBusID);
35 #if defined(SK_GANESH)
36     void drawGlyphRunList(SkCanvas* canvas,
37                           const GrClip* clip,
38                           const SkMatrixProvider& viewMatrix,
39                           const GlyphRunList& glyphRunList,
40                           const SkPaint& paint,
41                           SkStrikeDeviceInfo strikeDeviceInfo,
42                           skgpu::v1::SurfaceDrawContext* sdc);
43 #endif
44 #if defined(SK_GRAPHITE)
45     void drawGlyphRunList(SkCanvas* canvas,
46                           const SkMatrix& viewMatrix,
47                           const GlyphRunList& glyphRunList,
48                           const SkPaint& paint,
49                           SkStrikeDeviceInfo strikeDeviceInfo,
50                           skgpu::graphite::Device* device);
51 #endif
52 
53     void freeAll() SK_EXCLUDES(fSpinLock);
54 
55     struct PurgeBlobMessage {
PurgeBlobMessagePurgeBlobMessage56         PurgeBlobMessage(uint32_t blobID, uint32_t contextUniqueID)
57                 : fBlobID(blobID), fContextID(contextUniqueID) {}
58 
59         uint32_t fBlobID;
60         uint32_t fContextID;
61     };
62 
63     static void PostPurgeBlobMessage(uint32_t blobID, uint32_t cacheID);
64 
65     void purgeStaleBlobs() SK_EXCLUDES(fSpinLock);
66 
67     size_t usedBytes() const SK_EXCLUDES(fSpinLock);
68 
69     bool isOverBudget() const SK_EXCLUDES(fSpinLock);
70 
71 private:
72     friend class ::GrTextBlobTestingPeer;
73     using TextBlobList = SkTInternalLList<TextBlob>;
74 
75     struct BlobIDCacheEntry {
76         BlobIDCacheEntry();
77         explicit BlobIDCacheEntry(uint32_t id);
78 
79         static uint32_t GetKey(const BlobIDCacheEntry& entry);
80 
81         void addBlob(sk_sp<TextBlob> blob);
82 
83         void removeBlob(TextBlob* blob);
84 
85         sk_sp<TextBlob> find(const TextBlob::Key& key) const;
86 
87         int findBlobIndex(const TextBlob::Key& key) const;
88 
89         uint32_t fID;
90         // Current clients don't generate multiple GrAtlasTextBlobs per SkTextBlob, so an array w/
91         // linear search is acceptable.  If usage changes, we should re-evaluate this structure.
92         SkSTArray<1, sk_sp<TextBlob>> fBlobs;
93     };
94 
95     sk_sp<TextBlob> findOrCreateBlob(const SkMatrixProvider& viewMatrix,
96                                      const GlyphRunList& glyphRunList,
97                                      const SkPaint& paint,
98                                      SkStrikeDeviceInfo strikeDeviceInfo);
99 
100     // If not already in the cache, then add it else, return the text blob from the cache.
101     sk_sp<TextBlob> addOrReturnExisting(
102             const GlyphRunList& glyphRunList,
103             sk_sp<TextBlob> blob) SK_EXCLUDES(fSpinLock);
104 
105     sk_sp<TextBlob> find(const TextBlob::Key& key) SK_EXCLUDES(fSpinLock);
106 
107     void remove(TextBlob* blob) SK_EXCLUDES(fSpinLock);
108 
109     void internalPurgeStaleBlobs() SK_REQUIRES(fSpinLock);
110 
111     sk_sp<TextBlob>
112             internalAdd(sk_sp<TextBlob> blob) SK_REQUIRES(fSpinLock);
113     void internalRemove(TextBlob* blob) SK_REQUIRES(fSpinLock);
114 
115     void internalCheckPurge(TextBlob* blob = nullptr) SK_REQUIRES(fSpinLock);
116 
117     static const int kDefaultBudget = 1 << 22;
118 
119     mutable SkSpinlock fSpinLock;
120     TextBlobList fBlobList SK_GUARDED_BY(fSpinLock);
121     SkTHashMap<uint32_t, BlobIDCacheEntry> fBlobIDCache SK_GUARDED_BY(fSpinLock);
122     size_t fSizeBudget SK_GUARDED_BY(fSpinLock);
SK_GUARDED_BY(fSpinLock)123     size_t fCurrentSize SK_GUARDED_BY(fSpinLock) {0};
124 
125     // In practice 'messageBusID' is always the unique ID of the owning GrContext
126     const uint32_t fMessageBusID;
127     SkMessageBus<PurgeBlobMessage, uint32_t>::Inbox fPurgeBlobInbox SK_GUARDED_BY(fSpinLock);
128 };
129 
130 }  // namespace sktext::gpu
131 
132 #endif  // sktext_gpu_TextBlobRedrawCoordinator_DEFINED
133