Debian Codegraph @

src/gpu/ganesh/text/GaneshVertexFiller.cpp

raw
1 /*
2 * Copyright 2025 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 #include "src/text/gpu/VertexFiller.h"
9
10 #include "include/core/SkMatrix.h"
11 #include "include/core/SkPoint3.h"
12 #include "include/core/SkRect.h"
13 #include "include/core/SkScalar.h"
14 #include "include/private/base/SkAssert.h"
15 #include "include/private/base/SkPoint_impl.h"
16 #include "include/private/base/SkSpan_impl.h"
17 #include "src/base/SkZip.h"
18 #include "src/core/SkColorData.h"
19 #include "src/gpu/AtlasTypes.h"
20 #include "src/gpu/ganesh/GrColor.h"
21 #include "src/text/gpu/Glyph.h"
22
23 #include <cstddef>
24 #include <cstdint>
25 #include <tuple>
26
27 using MaskFormat = skgpu::MaskFormat;
28
29 namespace sktext::gpu {
30
31 struct AtlasPt {
32 uint16_t u;
33 uint16_t v;
34 };
35
36 // Normal text mask, SDFT, or color.
37 struct Mask2DVertex {
38 SkPoint devicePos;
39 GrColor color;
40 AtlasPt atlasPos;
41 };
42
43 struct ARGB2DVertex {
44 ARGB2DVertex(SkPoint d, GrColor, AtlasPt a) : devicePos{d}, atlasPos{a} {}
45
46 SkPoint devicePos;
47 AtlasPt atlasPos;
48 };
49
50 // Perspective SDFT or SDFT forced to 3D or perspective color.
51 struct Mask3DVertex {
52 SkPoint3 devicePos;
53 GrColor color;
54 AtlasPt atlasPos;
55 };
56
57 struct ARGB3DVertex {
58 ARGB3DVertex(SkPoint3 d, GrColor, AtlasPt a) : devicePos{d}, atlasPos{a} {}
59
60 SkPoint3 devicePos;
61 AtlasPt atlasPos;
62 };
63
64 size_t VertexFiller::vertexStride(const SkMatrix &matrix) const {
65 if (fMaskType != skgpu::MaskFormat::kARGB) {
66 // For formats MaskFormat::kA565 and MaskFormat::kA8 where A8 include SDF.
67 return matrix.hasPerspective() ? sizeof(Mask3DVertex) : sizeof(Mask2DVertex);
68 } else {
69 // For format MaskFormat::kARGB
70 return matrix.hasPerspective() ? sizeof(ARGB3DVertex) : sizeof(ARGB2DVertex);
71 }
72 }
73
74 // The 99% case. Direct Mask, No clip, No RGB.
75 void fillDirectNoClipping(SkZip<Mask2DVertex[4], const Glyph*, const SkPoint> quadData,
76 GrColor color,
77 SkPoint originOffset) {
78 for (auto[quad, glyph, leftTop] : quadData) {
79 auto[al, at, ar, ab] = glyph->fAtlasLocator.getUVs();
80 SkScalar dl = leftTop.x() + originOffset.x(),
81 dt = leftTop.y() + originOffset.y(),
82 dr = dl + (ar - al),
83 db = dt + (ab - at);
84
85 quad[0] = {{dl, dt}, color, {al, at}}; // L,T
86 quad[1] = {{dl, db}, color, {al, ab}}; // L,B
87 quad[2] = {{dr, dt}, color, {ar, at}}; // R,T
88 quad[3] = {{dr, db}, color, {ar, ab}}; // R,B
89 }
90 }
91
92 template <typename Rect>
93 static auto LTBR(const Rect& r) {
94 return std::make_tuple(r.left(), r.top(), r.right(), r.bottom());
95 }
96
97 // Handle any combination of BW or color and clip or no clip.
98 template<typename Quad, typename VertexData>
99 static void fillDirectClipped(SkZip<Quad, const Glyph*, const VertexData> quadData,
100 GrColor color,
101 SkPoint originOffset,
102 SkIRect* clip = nullptr) {
103 for (auto[quad, glyph, leftTop] : quadData) {
104 auto[al, at, ar, ab] = glyph->fAtlasLocator.getUVs();
105 uint16_t w = ar - al,
106 h = ab - at;
107 SkScalar l = leftTop.x() + originOffset.x(),
108 t = leftTop.y() + originOffset.y();
109 if (clip == nullptr) {
110 auto[dl, dt, dr, db] = SkRect::MakeLTRB(l, t, l + w, t + h);
111 quad[0] = {{dl, dt}, color, {al, at}}; // L,T
112 quad[1] = {{dl, db}, color, {al, ab}}; // L,B
113 quad[2] = {{dr, dt}, color, {ar, at}}; // R,T
114 quad[3] = {{dr, db}, color, {ar, ab}}; // R,B
115 } else {
116 SkIRect devIRect = SkIRect::MakeLTRB(l, t, l + w, t + h);
117 SkScalar dl, dt, dr, db;
118 if (!clip->containsNoEmptyCheck(devIRect)) {
119 if (SkIRect clipped; clipped.intersect(devIRect, *clip)) {
120 al += clipped.left() - devIRect.left();
121 at += clipped.top() - devIRect.top();
122 ar += clipped.right() - devIRect.right();
123 ab += clipped.bottom() - devIRect.bottom();
124 std::tie(dl, dt, dr, db) = LTBR(clipped);
125 } else {
126 // TODO: omit generating any vertex data for fully clipped glyphs ?
127 std::tie(dl, dt, dr, db) = std::make_tuple(0, 0, 0, 0);
128 std::tie(al, at, ar, ab) = std::make_tuple(0, 0, 0, 0);
129 }
130 } else {
131 std::tie(dl, dt, dr, db) = LTBR(devIRect);
132 }
133 quad[0] = {{dl, dt}, color, {al, at}}; // L,T
134 quad[1] = {{dl, db}, color, {al, ab}}; // L,B
135 quad[2] = {{dr, dt}, color, {ar, at}}; // R,T
136 quad[3] = {{dr, db}, color, {ar, ab}}; // R,B
137 }
138 }
139 }
140
141 template<typename Quad, typename VertexData>
142 static void fill2D(SkZip<Quad, const Glyph*, const VertexData> quadData,
143 GrColor color,
144 const SkMatrix& viewDifference) {
145 for (auto [quad, glyph, leftTop] : quadData) {
146 auto [l, t] = leftTop;
147 auto [r, b] = leftTop + glyph->fAtlasLocator.widthHeight();
148 SkPoint lt = viewDifference.mapPoint({l, t}),
149 lb = viewDifference.mapPoint({l, b}),
150 rt = viewDifference.mapPoint({r, t}),
151 rb = viewDifference.mapPoint({r, b});
152 auto [al, at, ar, ab] = glyph->fAtlasLocator.getUVs();
153 quad[0] = {lt, color, {al, at}}; // L,T
154 quad[1] = {lb, color, {al, ab}}; // L,B
155 quad[2] = {rt, color, {ar, at}}; // R,T
156 quad[3] = {rb, color, {ar, ab}}; // R,B
157 }
158 }
159
160 template<typename Quad, typename VertexData>
161 static void fill3D(SkZip<Quad, const Glyph*, const VertexData> quadData,
162 GrColor color,
163 const SkMatrix& viewDifference) {
164 auto mapXYZ = [&](SkScalar x, SkScalar y) {
165 return viewDifference.mapPointToHomogeneous({x, y});
166 };
167 for (auto [quad, glyph, leftTop] : quadData) {
168 auto [l, t] = leftTop;
169 auto [r, b] = leftTop + glyph->fAtlasLocator.widthHeight();
170 SkPoint3 lt = mapXYZ(l, t),
171 lb = mapXYZ(l, b),
172 rt = mapXYZ(r, t),
173 rb = mapXYZ(r, b);
174 auto [al, at, ar, ab] = glyph->fAtlasLocator.getUVs();
175 quad[0] = {lt, color, {al, at}}; // L,T
176 quad[1] = {lb, color, {al, ab}}; // L,B
177 quad[2] = {rt, color, {ar, at}}; // R,T
178 quad[3] = {rb, color, {ar, ab}}; // R,B
179 }
180 }
181
182 void VertexFiller::fillVertexData(int offset, int count,
183 SkSpan<const Glyph*> glyphs,
184 const SkPMColor4f& pmColor,
185 const SkMatrix& positionMatrix,
186 SkIRect clip,
187 void* vertexBuffer) const {
188 GrColor color = pmColor.toBytes_RGBA();
189 auto quadData = [&](auto dst) {
190 return SkMakeZip(dst,
191 glyphs.subspan(offset, count),
192 fLeftTop.subspan(offset, count));
193 };
194
195 // Handle direct mask drawing specifically.
196 if (fCanDrawDirect) {
197 auto [noTransformNeeded, originOffset] = CanUseDirect(fCreationMatrix, positionMatrix);
198
199 if (noTransformNeeded) {
200 if (clip.isEmpty()) {
201 if (fMaskType != MaskFormat::kARGB) {
202 using Quad = Mask2DVertex[4];
203 SkASSERT(sizeof(Mask2DVertex) == this->vertexStride(SkMatrix::I()));
204 fillDirectNoClipping(quadData((Quad*)vertexBuffer), color, originOffset);
205 } else {
206 using Quad = ARGB2DVertex[4];
207 SkASSERT(sizeof(ARGB2DVertex) == this->vertexStride(SkMatrix::I()));
208 fillDirectClipped(quadData((Quad*)vertexBuffer), color, originOffset);
209 }
210 } else {
211 if (fMaskType != MaskFormat::kARGB) {
212 using Quad = Mask2DVertex[4];
213 SkASSERT(sizeof(Mask2DVertex) == this->vertexStride(SkMatrix::I()));
214 fillDirectClipped(quadData((Quad*)vertexBuffer), color, originOffset, &clip);
215 } else {
216 using Quad = ARGB2DVertex[4];
217 SkASSERT(sizeof(ARGB2DVertex) == this->vertexStride(SkMatrix::I()));
218 fillDirectClipped(quadData((Quad*)vertexBuffer), color, originOffset, &clip);
219 }
220 }
221 return;
222 }
223 }
224
225 // Handle the general transformed case.
226 SkMatrix viewDifference = this->viewDifference(positionMatrix);
227 if (!positionMatrix.hasPerspective()) {
228 if (fMaskType == MaskFormat::kARGB) {
229 using Quad = ARGB2DVertex[4];
230 SkASSERT(sizeof(ARGB2DVertex) == this->vertexStride(positionMatrix));
231 fill2D(quadData((Quad*)vertexBuffer), color, viewDifference);
232 } else {
233 using Quad = Mask2DVertex[4];
234 SkASSERT(sizeof(Mask2DVertex) == this->vertexStride(positionMatrix));
235 fill2D(quadData((Quad*)vertexBuffer), color, viewDifference);
236 }
237 } else {
238 if (fMaskType == MaskFormat::kARGB) {
239 using Quad = ARGB3DVertex[4];
240 SkASSERT(sizeof(ARGB3DVertex) == this->vertexStride(positionMatrix));
241 fill3D(quadData((Quad*)vertexBuffer), color, viewDifference);
242 } else {
243 using Quad = Mask3DVertex[4];
244 SkASSERT(sizeof(Mask3DVertex) == this->vertexStride(positionMatrix));
245 fill3D(quadData((Quad*)vertexBuffer), color, viewDifference);
246 }
247 }
248 }
249
250 } // namespace sktext::gpu