Debian Codegraph @

src/text/gpu/SubRunContainer.h

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
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 SubRunOwner fNext;
105 };
106
107 // -- AtlasSubRun --------------------------------------------------------------------------------
108 // AtlasSubRun is the API that AtlasTextOp uses to generate vertex data for drawing.
109 // There are three different ways AtlasSubRun is specialized.
110 // * DirectMaskSubRun* - this is by far the most common type of SubRun. The mask pixels are
111 // in 1:1 correspondence with the pixels on the device. The destination rectangles in this
112 // SubRun are in device space. This SubRun handles color glyphs.
113 // * TransformedMaskSubRun* - handles glyph where the image in the atlas needs to be
114 // transformed to the screen. It is usually used for large color glyph which can't be
115 // drawn with paths or scaled distance fields, but will be used to draw bitmap glyphs to
116 // the screen, if the matrix does not map 1:1 to the screen. The destination rectangles
117 // are in source space.
118 // * SDFTSubRun* - scaled distance field text handles largish single color glyphs that still
119 // can fit in the atlas; the sizes between direct SubRun, and path SubRun. The destination
120 // rectangles are in source space.
121 class AtlasSubRun : public SubRun {
122 public:
123 AtlasSubRun(VertexFiller&& vertexFiller, GlyphVector&& glyphs)
124 : fVertexFiller{std::move(vertexFiller)}
125 , fGlyphs{std::move(glyphs)} {}
126 ~AtlasSubRun() override = default;
127
128 SkSpan<const Glyph*> glyphs() const { return fGlyphs.glyphs(); }
129 int glyphCount() const { return SkCount(fGlyphs.glyphs()); }
130 skgpu::MaskFormat maskFormat() const { return fVertexFiller.grMaskType(); }
131 virtual int glyphSrcPadding() const = 0;
132 unsigned short instanceFlags() const { return (unsigned short)this->maskFormat(); }
133
134 virtual std::tuple<bool, SkRect> deviceRectAndNeedsTransform(
135 const SkMatrix &positionMatrix) const = 0;
136
137 struct GlyphParams {
138 bool isSDF;
139 bool isLCD;
140 bool isAA;
141 };
142 virtual GlyphParams glyphParams() const = 0;
143
144 size_t vertexStride(const SkMatrix& drawMatrix) const {
145 return fVertexFiller.vertexStride(drawMatrix);
146 }
147
148 void fillVertexData(
149 void* vertexDst, int offset, int count,
150 const SkPMColor4f& color,
151 const SkMatrix& drawMatrix,
152 SkPoint drawOrigin,
153 SkIRect clip) const {
154 SkMatrix positionMatrix = drawMatrix;
155 positionMatrix.preTranslate(drawOrigin.x(), drawOrigin.y());
156 fVertexFiller.fillVertexData(offset, count,
157 fGlyphs.glyphs(),
158 color,
159 positionMatrix,
160 clip,
161 vertexDst);
162 }
163
164 // This call is not thread safe. It should only be called from a known single-threaded env.
165 virtual std::tuple<bool, int> regenerateAtlas(
166 int begin, int end, RegenerateAtlasDelegate) const = 0;
167
168 const VertexFiller& vertexFiller() const { return fVertexFiller; }
169
170 virtual void testingOnly_packedGlyphIDToGlyph(StrikeCache* cache, skgpu::MaskFormat) const = 0;
171
172 protected:
173 const VertexFiller fVertexFiller;
174
175 // The regenerateAtlas method mutates fGlyphs. It should be called from onPrepare which must
176 // be single threaded.
177 mutable GlyphVector fGlyphs;
178 };
179
180 // -- SubRunList -----------------------------------------------------------------------------------
181 class SubRunList {
182 public:
183 class Iterator {
184 public:
185 using value_type = SubRun;
186 using difference_type = ptrdiff_t;
187 using pointer = value_type*;
188 using reference = value_type&;
189 using iterator_category = std::input_iterator_tag;
190 Iterator(SubRun* subRun) : fPtr{subRun} { }
191 Iterator& operator++() { fPtr = fPtr->fNext.get(); return *this; }
192 Iterator operator++(int) { Iterator tmp(*this); operator++(); return tmp; }
193 bool operator==(const Iterator& rhs) const { return fPtr == rhs.fPtr; }
194 bool operator!=(const Iterator& rhs) const { return fPtr != rhs.fPtr; }
195 reference operator*() { return *fPtr; }
196
197 private:
198 SubRun* fPtr;
199 };
200
201 void append(SubRunOwner subRun) {
202 SubRunOwner* newTail = &subRun->fNext;
203 *fTail = std::move(subRun);
204 fTail = newTail;
205 }
206 bool isEmpty() const { return fHead == nullptr; }
207 Iterator begin() { return Iterator{ fHead.get()}; }
208 Iterator end() { return Iterator{nullptr}; }
209 Iterator begin() const { return Iterator{ fHead.get()}; }
210 Iterator end() const { return Iterator{nullptr}; }
211 SubRun& front() const {return *fHead; }
212
213 private:
214 SubRunOwner fHead{nullptr};
215 SubRunOwner* fTail{&fHead};
216 };
217
218 // -- SubRunContainer ------------------------------------------------------------------------------
219 class SubRunContainer;
220 using SubRunContainerOwner = std::unique_ptr<SubRunContainer, SubRunAllocator::Destroyer>;
221 class SubRunContainer {
222 public:
223 explicit SubRunContainer(const SkMatrix& initialPositionMatrix);
224 SubRunContainer() = delete;
225 SubRunContainer(const SubRunContainer&) = delete;
226 SubRunContainer& operator=(const SubRunContainer&) = delete;
227
228 // Delete the move operations because the SubRuns contain pointers to fInitialPositionMatrix.
229 SubRunContainer(SubRunContainer&&) = delete;
230 SubRunContainer& operator=(SubRunContainer&&) = delete;
231
232 void flattenAllocSizeHint(SkWriteBuffer& buffer) const;
233 static int AllocSizeHintFromBuffer(SkReadBuffer& buffer);
234
235 void flattenRuns(SkWriteBuffer& buffer) const;
236 static SubRunContainerOwner MakeFromBufferInAlloc(SkReadBuffer& buffer,
237 const SkStrikeClient* client,
238 SubRunAllocator* alloc);
239
240 enum SubRunCreationBehavior {kAddSubRuns, kStrikeCalculationsOnly};
241 // The returned SubRunContainerOwner will never be null. If subRunCreation ==
242 // kStrikeCalculationsOnly, then the returned container will be empty.
243 [[nodiscard]] static SubRunContainerOwner MakeInAlloc(const GlyphRunList& glyphRunList,
244 const SkMatrix& positionMatrix,
245 const SkPaint& runPaint,
246 SkStrikeDeviceInfo strikeDeviceInfo,
247 StrikeForGPUCacheInterface* strikeCache,
248 sktext::gpu::SubRunAllocator* alloc,
249 SubRunCreationBehavior creationBehavior,
250 const char* tag);
251
252 static size_t EstimateAllocSize(const GlyphRunList& glyphRunList);
253
254 void draw(SkCanvas*, SkPoint drawOrigin, const SkPaint&, const SkRefCnt* subRunStorage,
255 const AtlasDrawDelegate&) const;
256
257 const SkMatrix& initialPosition() const { return fInitialPositionMatrix; }
258 bool isEmpty() const { return fSubRuns.isEmpty(); }
259 bool canReuse(const SkPaint& paint, const SkMatrix& positionMatrix) const;
260
261 private:
262 friend class TextBlobTools;
263 const SkMatrix fInitialPositionMatrix;
264 SubRunList fSubRuns;
265 };
266
267 // Returns the empty span if there is a problem reading the positions.
268 SkSpan<SkPoint> MakePointsFromBuffer(SkReadBuffer&, SubRunAllocator*);
269
270 } // namespace sktext::gpu
271
272 #endif // sktext_gpu_SubRunContainer_DEFINED