Debian Codegraph @

src/gpu/ganesh/ops/AtlasTextOp.h

raw
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 skgpu_ganesh_AtlasTextOp_DEFINED
9 #define skgpu_ganesh_AtlasTextOp_DEFINED
10
11 #include "include/core/SkColor.h"
12 #include "include/core/SkMatrix.h"
13 #include "include/core/SkRect.h"
14 #include "include/core/SkRefCnt.h"
15 #include "include/core/SkString.h"
16 #include "include/private/base/SkAssert.h"
17 #include "include/private/base/SkPoint_impl.h"
18 #include "include/private/gpu/ganesh/GrTypesPriv.h"
19 #include "src/core/SkColorData.h"
20 #include "src/gpu/AtlasTypes.h"
21 #include "src/gpu/ganesh/GrAppliedClip.h"
22 #include "src/gpu/ganesh/GrBuffer.h"
23 #include "src/gpu/ganesh/GrCaps.h"
24 #include "src/gpu/ganesh/GrColorInfo.h"
25 #include "src/gpu/ganesh/GrColorSpaceXform.h"
26 #include "src/gpu/ganesh/GrProcessorSet.h"
27 #include "src/gpu/ganesh/effects/GrDistanceFieldGeoProc.h"
28 #include "src/gpu/ganesh/ops/GrMeshDrawOp.h"
29 #include "src/gpu/ganesh/ops/GrOp.h"
30
31 #include <cstddef>
32 #include <cstdint>
33 #include <tuple>
34 #include <utility>
35
36 class GrClip;
37 class GrDstProxyView;
38 class GrGeometryProcessor;
39 class GrMeshDrawTarget;
40 class GrOpFlushState;
41 class GrPaint;
42 class GrProgramInfo;
43 class GrRecordingContext;
44 class GrSurfaceProxy;
45 class GrSurfaceProxyView;
46 class SkArenaAlloc;
47 class SkPaint;
48 enum class GrXferBarrierFlags;
49 struct GrShaderCaps;
50
51 namespace skgpu { namespace ganesh { class SurfaceDrawContext; } }
52 namespace sktext { namespace gpu { class AtlasSubRun; } }
53
54 namespace skgpu::ganesh {
55
56 class AtlasTextOp final : public GrMeshDrawOp {
57 public:
58 DEFINE_OP_CLASS_ID
59
60 static std::tuple<const GrClip*, GrOp::Owner> Make(SurfaceDrawContext*,
61 const sktext::gpu::AtlasSubRun*,
62 const GrClip*,
63 const SkMatrix& viewMatrix,
64 SkPoint drawOrigin,
65 const SkPaint&,
66 sk_sp<SkRefCnt>&& subRunStorage);
67
68 ~AtlasTextOp() override {
69 for (const Geometry* g = fHead; g != nullptr;) {
70 const Geometry* next = g->fNext;
71 g->~Geometry();
72 g = next;
73 }
74 }
75
76 void* operator new(size_t s);
77 void operator delete(void* b) noexcept;
78 static void ClearCache();
79
80 struct Geometry {
81 Geometry(const sktext::gpu::AtlasSubRun& subRun,
82 const SkMatrix& drawMatrix,
83 SkPoint drawOrigin,
84 SkIRect clipRect,
85 sk_sp<SkRefCnt>&& supportData,
86 const SkPMColor4f& color)
87 : fSubRun{subRun}
88 , fSupportDataKeepAlive{std::move(supportData)}
89 , fDrawMatrix{drawMatrix}
90 , fDrawOrigin{drawOrigin}
91 , fClipRect{clipRect}
92 , fColor{color} {
93 SkASSERT(fSupportDataKeepAlive != nullptr);
94 }
95
96 static Geometry* Make(const sktext::gpu::AtlasSubRun& subRun,
97 const SkMatrix& drawMatrix,
98 SkPoint drawOrigin,
99 SkIRect clipRect,
100 sk_sp<SkRefCnt>&& supportData,
101 const SkPMColor4f& color,
102 SkArenaAlloc* alloc);
103
104 void fillVertexData(void* dst, int offset, int count) const;
105
106 const sktext::gpu::AtlasSubRun& fSubRun;
107
108 // Keep the TextBlob or Slug alive until the op is deleted.
109 sk_sp<SkRefCnt> fSupportDataKeepAlive;
110
111 const SkMatrix fDrawMatrix;
112 const SkPoint fDrawOrigin;
113
114 // fClipRect is only used in the DirectMaskSubRun case to do geometric clipping.
115 // TransformedMaskSubRun, and SDFTSubRun don't use this field, and expect an empty rect.
116 const SkIRect fClipRect;
117
118 // Color is updated after processor analysis if it was determined the shader resolves to
119 // a constant color that we then evaluate on the CPU.
120 // TODO: This can be made const once processor analysis is separated from op creation.
121 SkPMColor4f fColor;
122 Geometry* fNext{nullptr};
123 };
124
125 const char* name() const override { return "AtlasTextOp"; }
126
127 void visitProxies(const GrVisitProxyFunc&) const override;
128
129 FixedFunctionFlags fixedFunctionFlags() const override;
130
131 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, GrClampType) override;
132
133 enum class MaskType : uint32_t {
134 kGrayscaleCoverage,
135 kLCDCoverage,
136 kColorBitmap,
137 #if !defined(SK_DISABLE_SDF_TEXT)
138 kAliasedDistanceField,
139 kGrayscaleDistanceField,
140 kLCDDistanceField,
141
142 kLast = kLCDDistanceField
143 #else
144 kLast = kColorBitmap
145 #endif
146 };
147 inline static constexpr int kMaskTypeCount = static_cast<int>(MaskType::kLast) + 1;
148
149 private:
150 friend class GrOp; // for ctor
151
152 struct FlushInfo {
153 sk_sp<const GrBuffer> fVertexBuffer;
154 sk_sp<const GrBuffer> fIndexBuffer;
155 GrGeometryProcessor* fGeometryProcessor;
156 const GrSurfaceProxy** fPrimProcProxies;
157 int fGlyphsToFlush = 0;
158 int fVertexOffset = 0;
159 int fNumDraws = 0;
160 };
161
162 // DirectMask and TransformedMask constructor
163 AtlasTextOp(MaskType maskType,
164 bool needsTransform,
165 int glyphCount,
166 SkRect deviceRect,
167 Geometry* geo,
168 const GrColorInfo& dstColorInfo,
169 GrPaint&& paint);
170
171 // SDF constructor
172 AtlasTextOp(MaskType maskType,
173 bool needsTransform,
174 int glyphCount,
175 SkRect deviceRect,
176 SkColor luminanceColor,
177 bool useGammaCorrectDistanceTable,
178 uint32_t DFGPFlags,
179 Geometry* geo,
180 GrPaint&& paint);
181
182 GrProgramInfo* programInfo() override {
183 // TODO [PI]: implement
184 return nullptr;
185 }
186
187 void addGeometry(Geometry* geometry) {
188 *fTail = geometry;
189 // The geometry may have many entries. Find the end.
190 do {
191 fTail = &(*fTail)->fNext;
192 } while (*fTail != nullptr);
193 }
194
195 void onCreateProgramInfo(const GrCaps*,
196 SkArenaAlloc*,
197 const GrSurfaceProxyView& writeView,
198 bool usesMSAASurface,
199 GrAppliedClip&&,
200 const GrDstProxyView&,
201 GrXferBarrierFlags renderPassXferBarriers,
202 GrLoadOp colorLoadOp) override {
203 // We cannot surface the AtlasTextOp's programInfo at record time. As currently
204 // implemented, the GP is modified at flush time based on the number of pages in the
205 // atlas.
206 }
207
208 void onPrePrepareDraws(GrRecordingContext*,
209 const GrSurfaceProxyView& writeView,
210 GrAppliedClip*,
211 const GrDstProxyView&,
212 GrXferBarrierFlags renderPassXferBarriers,
213 GrLoadOp colorLoadOp) override {
214 // TODO [PI]: implement
215 }
216
217 void onPrepareDraws(GrMeshDrawTarget*) override;
218 void onExecute(GrOpFlushState*, const SkRect& chainBounds) override;
219
220 #if defined(GPU_TEST_UTILS)
221 SkString onDumpInfo() const override;
222 #endif
223
224 skgpu::MaskFormat maskFormat() const {
225 switch (this->maskType()) {
226 case MaskType::kLCDCoverage:
227 return skgpu::MaskFormat::kA565;
228 case MaskType::kColorBitmap:
229 return skgpu::MaskFormat::kARGB;
230 case MaskType::kGrayscaleCoverage:
231 #if !defined(SK_DISABLE_SDF_TEXT)
232 case MaskType::kAliasedDistanceField:
233 case MaskType::kGrayscaleDistanceField:
234 case MaskType::kLCDDistanceField:
235 #endif
236 return skgpu::MaskFormat::kA8;
237 }
238 // SkUNREACHABLE;
239 return skgpu::MaskFormat::kA8;
240 }
241
242 #if !defined(SK_DISABLE_SDF_TEXT)
243 bool usesDistanceFields() const {
244 return MaskType::kAliasedDistanceField == this->maskType() ||
245 MaskType::kGrayscaleDistanceField == this->maskType() ||
246 MaskType::kLCDDistanceField == this->maskType();
247 }
248
249 bool isLCD() const {
250 return MaskType::kLCDCoverage == this->maskType() ||
251 MaskType::kLCDDistanceField == this->maskType();
252 }
253 #else
254 bool isLCD() const {
255 return MaskType::kLCDCoverage == this->maskType();
256 }
257 #endif
258
259 inline void createDrawForGeneratedGlyphs(
260 GrMeshDrawTarget* target, FlushInfo* flushInfo) const;
261
262 MaskType maskType() const { return static_cast<MaskType>(fMaskType); }
263
264 CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override;
265
266 #if !defined(SK_DISABLE_SDF_TEXT)
267 GrGeometryProcessor* setupDfProcessor(SkArenaAlloc*,
268 const GrShaderCaps&,
269 const SkMatrix& localMatrix,
270 const GrSurfaceProxyView* views,
271 unsigned int numActiveViews) const;
272 #endif
273
274 GrProcessorSet fProcessors;
275 int fNumGlyphs; // Sum of glyphs in each geometry's subrun
276
277 // All combinable atlas ops have equal bit field values
278 uint32_t fDFGPFlags : 10; // Distance field properties
279 uint32_t fMaskType : 3; // MaskType
280 uint32_t fUsesLocalCoords : 1; // Filled in post processor analysis
281 uint32_t fNeedsGlyphTransform : 1;
282 uint32_t fHasPerspective : 1; // True if perspective affects draw
283 uint32_t fUseGammaCorrectDistanceTable : 1;
284 static_assert(kMaskTypeCount <= 8, "MaskType does not fit in 3 bits");
285 #if !defined(SK_DISABLE_SDF_TEXT)
286 static_assert(kInvalid_DistanceFieldEffectFlag <= (1 << 9), "DFGP Flags do not fit in 10 bits");
287 #endif
288
289 // Only needed for color emoji
290 sk_sp<GrColorSpaceXform> fColorSpaceXform;
291
292 // Only used for distance fields; per-channel luminance for LCD, or gamma-corrected luminance
293 // for single-channel distance fields.
294 const SkColor fLuminanceColor{0};
295
296 Geometry* fHead{nullptr};
297 Geometry** fTail{&fHead};
298
299 using INHERITED = GrMeshDrawOp;
300 };
301
302 } // namespace skgpu::ganesh
303
304 #endif // skgpu_ganesh_AtlasTextOp_DEFINED