Debian Codegraph @

src/text/gpu/SubRunContainer.h

raw ◀ ▶
1 Debian patch touch this file
1 /*
2 * Copyright 2022 Google LLC
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_SubRunContainer_DEFINED
9 #define sktext_gpu_SubRunContainer_DEFINED
10
11 #include "include/core/SkMatrix.h"
12 #include "include/core/SkRect.h"
13 #include "include/core/SkRefCnt.h"
14 #include "include/core/SkSpan.h"
15 #include "include/private/base/SkPoint_impl.h"
16 #include "include/private/base/SkTLogic.h"
17 #include "src/core/SkColorData.h" // IWYU pragma: keep
18 #include "src/text/gpu/GlyphVector.h"
19 #include "src/text/gpu/SubRunAllocator.h"
20 #include "src/text/gpu/VertexFiller.h"
21
22 #include <cstddef>
23 #include <functional>
24 #include <iterator>
25 #include <memory>
26 #include <tuple>
27 #include <utility>
28
29 class SkCanvas;
30 class SkPaint;
31 class SkReadBuffer;
32 class SkStrikeClient;
33 class SkWriteBuffer;
34 struct SkStrikeDeviceInfo;
35
36 namespace sktext {
37 class GlyphRunList;
38 class StrikeForGPUCacheInterface;
39 }
40
41 namespace skgpu {
42 enum class MaskFormat : int;
43 }
44
45 namespace sktext::gpu {
46 class Glyph;
47 class StrikeCache;
48
49 using RegenerateAtlasDelegate = std::function<std::tuple<bool, int>(GlyphVector*,
50 int begin,
51 int end,
52 skgpu::MaskFormat,
53 int padding)>;
54
55 struct RendererData {
56 bool isSDF = false;
57 bool isLCD = false;
58 skgpu::MaskFormat maskFormat;
59 };
60
61 class AtlasSubRun;
62 using AtlasDrawDelegate = std::function<void(const sktext::gpu::AtlasSubRun* subRun,
63 SkPoint drawOrigin,
64 const SkPaint& paint,
65 sk_sp<SkRefCnt> subRunStorage,
66 sktext::gpu::RendererData)>;
67
68 // -- SubRun -------------------------------------------------------------------------------------
69 // SubRun defines the most basic functionality of a SubRun; the ability to draw, and the
70 // ability to be in a list.
71 class SubRun;
72 using SubRunOwner = std::unique_ptr<SubRun, SubRunAllocator::Destroyer>;
73 class SubRun {
74 public:
75 virtual ~SubRun();
76
77 virtual void draw(SkCanvas*, SkPoint drawOrigin, const SkPaint&, sk_sp<SkRefCnt> subRunStorage,
78 const AtlasDrawDelegate&) const = 0;
79
80 void flatten(SkWriteBuffer& buffer) const;
81 static SubRunOwner MakeFromBuffer(SkReadBuffer& buffer,
82 sktext::gpu::SubRunAllocator* alloc,
83 const SkStrikeClient* client);
84
85 // Size hint for unflattening this run. If this is accurate, it will help with the allocation
86 // of the slug. If it's off then there may be more allocations needed to unflatten.
87 virtual int unflattenSize() const = 0;
88
89 // Given an already cached subRun, can this subRun handle this combination paint, matrix, and
90 // position.
91 virtual bool canReuse(const SkPaint& paint, const SkMatrix& positionMatrix) const = 0;
92
93 // Return the underlying atlas SubRun if it exists. Otherwise, return nullptr.
94 // * Don't use this API. It is only to support testing.
95 virtual const AtlasSubRun* testingOnly_atlasSubRun() const = 0;
96
97 protected:
98 enum SubRunStreamTag : int;
99 virtual SubRunStreamTag subRunStreamTag() const = 0;
100 virtual void doFlatten(SkWriteBuffer& buffer) const = 0;
101
102 private:
103 friend class SubRunList;
•104 friend class TextBlobTools;
105 SubRunOwner fNext;
106 };
107
108 // -- AtlasSubRun --------------------------------------------------------------------------------
109 // AtlasSubRun is the API that AtlasTextOp uses to generate vertex data for drawing.
110 // There are three different ways AtlasSubRun is specialized.
111 // * DirectMaskSubRun* - this is by far the most common type of SubRun. The mask pixels are
112 // in 1:1 correspondence with the pixels on the device. The destination rectangles in this
113 // SubRun are in device space. This SubRun handles color glyphs.
114 // * TransformedMaskSubRun* - handles glyph where the image in the atlas needs to be
115 // transformed to the screen. It is usually used for large color glyph which can't be
116 // drawn with paths or scaled distance fields, but will be used to draw bitmap glyphs to
117 // the screen, if the matrix does not map 1:1 to the screen. The destination rectangles
118 // are in source space.
119 // * SDFTSubRun* - scaled distance field text handles largish single color glyphs that still
120 // can fit in the atlas; the sizes between direct SubRun, and path SubRun. The destination
121 // rectangles are in source space.
122 class AtlasSubRun : public SubRun {
123 public:
124 AtlasSubRun(VertexFiller&& vertexFiller, GlyphVector&& glyphs)
125 : fVertexFiller{std::move(vertexFiller)}
126 , fGlyphs{std::move(glyphs)} {}
127 ~AtlasSubRun() override = default;
128
•129 static bool IsBigEnough(const SkMatrix& matrix) {
130 return matrix.getMaxScale() >= 1.f;
131 }
132
133 SkSpan<const Glyph*> glyphs() const { return fGlyphs.glyphs(); }
134 int glyphCount() const { return SkCount(fGlyphs.glyphs()); }
135 skgpu::MaskFormat maskFormat() const { return fVertexFiller.grMaskType(); }
136 virtual int glyphSrcPadding() const = 0;
137 unsigned short instanceFlags() const { return (unsigned short)this->maskFormat(); }
138
139 virtual std::tuple<bool, SkRect> deviceRectAndNeedsTransform(
140 const SkMatrix &positionMatrix) const = 0;
141
142 struct GlyphParams {
143 bool isSDF;
144 bool isLCD;
145 bool isAA;
146 };
147 virtual GlyphParams glyphParams() const = 0;
148
149 size_t vertexStride(const SkMatrix& drawMatrix) const {
150 return fVertexFiller.vertexStride(drawMatrix);
151 }
152
153 void fillVertexData(
154 void* vertexDst, int offset, int count,
155 const SkPMColor4f& color,
156 const SkMatrix& drawMatrix,
157 SkPoint drawOrigin,
158 SkIRect clip) const {
159 SkMatrix positionMatrix = drawMatrix;
160 positionMatrix.preTranslate(drawOrigin.x(), drawOrigin.y());
161 fVertexFiller.fillVertexData(offset, count,
162 fGlyphs.glyphs(),
163 color,
164 positionMatrix,
165 clip,
166 vertexDst);
167 }
168
169 // This call is not thread safe. It should only be called from a known single-threaded env.
170 virtual std::tuple<bool, int> regenerateAtlas(
171 int begin, int end, RegenerateAtlasDelegate) const = 0;
172
173 const VertexFiller& vertexFiller() const { return fVertexFiller; }
174
175 virtual void testingOnly_packedGlyphIDToGlyph(StrikeCache* cache, skgpu::MaskFormat) const = 0;
176
177 protected:
178 const VertexFiller fVertexFiller;
179
180 // The regenerateAtlas method mutates fGlyphs. It should be called from onPrepare which must
181 // be single threaded.
182 mutable GlyphVector fGlyphs;
183 };
184
185 // -- SubRunList -----------------------------------------------------------------------------------
186 class SubRunList {
187 public:
188 class Iterator {
189 public:
190 using value_type = SubRun;
191 using difference_type = ptrdiff_t;
192 using pointer = value_type*;
193 using reference = value_type&;
194 using iterator_category = std::input_iterator_tag;
195 Iterator(SubRun* subRun) : fPtr{subRun} { }
196 Iterator& operator++() { fPtr = fPtr->fNext.get(); return *this; }
197 Iterator operator++(int) { Iterator tmp(*this); operator++(); return tmp; }
198 bool operator==(const Iterator& rhs) const { return fPtr == rhs.fPtr; }
199 bool operator!=(const Iterator& rhs) const { return fPtr != rhs.fPtr; }
200 reference operator*() { return *fPtr; }
201
202 private:
203 SubRun* fPtr;
204 };
205
206 void append(SubRunOwner subRun) {
207 SubRunOwner* newTail = &subRun->fNext;
208 *fTail = std::move(subRun);
209 fTail = newTail;
210 }
211 bool isEmpty() const { return fHead == nullptr; }
212 Iterator begin() { return Iterator{ fHead.get()}; }
213 Iterator end() { return Iterator{nullptr}; }
214 Iterator begin() const { return Iterator{ fHead.get()}; }
215 Iterator end() const { return Iterator{nullptr}; }
216 SubRun& front() const {return *fHead; }
217
218 private:
219 SubRunOwner fHead{nullptr};
220 SubRunOwner* fTail{&fHead};
221 };
222
223 // -- SubRunContainer ------------------------------------------------------------------------------
224 class SubRunContainer;
225 using SubRunContainerOwner = std::unique_ptr<SubRunContainer, SubRunAllocator::Destroyer>;
226 class SubRunContainer {
227 public:
228 explicit SubRunContainer(const SkMatrix& initialPositionMatrix);
229 SubRunContainer() = delete;
230 SubRunContainer(const SubRunContainer&) = delete;
231 SubRunContainer& operator=(const SubRunContainer&) = delete;
232
233 // Delete the move operations because the SubRuns contain pointers to fInitialPositionMatrix.
234 SubRunContainer(SubRunContainer&&) = delete;
235 SubRunContainer& operator=(SubRunContainer&&) = delete;
236
237 void flattenAllocSizeHint(SkWriteBuffer& buffer) const;
238 static int AllocSizeHintFromBuffer(SkReadBuffer& buffer);
239
240 void flattenRuns(SkWriteBuffer& buffer) const;
241 static SubRunContainerOwner MakeFromBufferInAlloc(SkReadBuffer& buffer,
242 const SkStrikeClient* client,
243 SubRunAllocator* alloc);
244
245 enum SubRunCreationBehavior {kAddSubRuns, kStrikeCalculationsOnly};
246 // The returned SubRunContainerOwner will never be null. If subRunCreation ==
247 // kStrikeCalculationsOnly, then the returned container will be empty.
248 [[nodiscard]] static SubRunContainerOwner MakeInAlloc(const GlyphRunList& glyphRunList,
249 const SkMatrix& positionMatrix,
250 const SkPaint& runPaint,
251 SkStrikeDeviceInfo strikeDeviceInfo,
252 StrikeForGPUCacheInterface* strikeCache,
253 sktext::gpu::SubRunAllocator* alloc,
254 SubRunCreationBehavior creationBehavior,
255 const char* tag);
256
257 static size_t EstimateAllocSize(const GlyphRunList& glyphRunList);
258
259 void draw(SkCanvas*, SkPoint drawOrigin, const SkPaint&, const SkRefCnt* subRunStorage,
260 const AtlasDrawDelegate&) const;
261
262 const SkMatrix& initialPosition() const { return fInitialPositionMatrix; }
263 bool isEmpty() const { return fSubRuns.isEmpty(); }
264 bool canReuse(const SkPaint& paint, const SkMatrix& positionMatrix) const;
265
266 private:
267 friend class TextBlobTools;
268 const SkMatrix fInitialPositionMatrix;
269 SubRunList fSubRuns;
270 };
271
272 } // namespace sktext::gpu
273
274 #endif // sktext_gpu_SubRunContainer_DEFINED