Debian Codegraph @

src/core/SkDraw_vertices.cpp

raw
1 /*
2 * Copyright 2017 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 #include "include/core/SkAlphaType.h"
9 #include "include/core/SkBlendMode.h"
10 #include "include/core/SkBlender.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkColorSpace.h"
13 #include "include/core/SkColorType.h"
14 #include "include/core/SkImageInfo.h"
15 #include "include/core/SkMatrix.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkPixmap.h"
18 #include "include/core/SkPoint.h"
19 #include "include/core/SkPoint3.h"
20 #include "include/core/SkRect.h"
21 #include "include/core/SkRefCnt.h"
22 #include "include/core/SkScalar.h"
23 #include "include/core/SkShader.h"
24 #include "include/core/SkSurfaceProps.h"
25 #include "include/core/SkTypes.h"
26 #include "include/core/SkVertices.h"
27 #include "include/private/base/SkFloatingPoint.h"
28 #include "include/private/base/SkTo.h"
29 #include "src/base/SkArenaAlloc.h"
30 #include "src/core/SkBlenderBase.h"
31 #include "src/core/SkColorData.h"
32 #include "src/core/SkConvertPixels.h"
33 #include "src/core/SkCoreBlitters.h"
34 #include "src/core/SkDraw.h"
35 #include "src/core/SkRasterClip.h"
36 #include "src/core/SkScan.h"
37 #include "src/core/SkSurfacePriv.h"
38 #include "src/core/SkVertState.h"
39 #include "src/core/SkVerticesPriv.h"
40 #include "src/shaders/SkShaderBase.h"
41 #include "src/shaders/SkTransformShader.h"
42 #include "src/shaders/SkTriColorShader.h"
43
44 #include <cstddef>
45 #include <cstdint>
46 #include <optional>
47 #include <utility>
48
49 class SkBlitter;
50
51 std::optional<SkMatrix> texture_to_matrix(const VertState& state, const SkPoint verts[],
52 const SkPoint texs[]) {
53 SkPoint src[3], dst[3];
54
55 src[0] = verts[state.f0];
56 src[1] = verts[state.f1];
57 src[2] = verts[state.f2];
58 dst[0] = texs[state.f0];
59 dst[1] = texs[state.f1];
60 dst[2] = texs[state.f2];
61 return SkMatrix::PolyToPoly(src, dst);
62 }
63
64 // Convert the SkColors into float colors. The conversion depends on some conditions:
65 // - If the pixmap has a dst colorspace, we have to be "color-correct".
66 // Do we map into dst-colorspace before or after we interpolate?
67 // - We have to decide when to apply per-color alpha (before or after we interpolate)
68 //
69 // For now, we will take a simple approach, but recognize this is just a start:
70 // - convert colors into dst colorspace before interpolation (matches gradients)
71 // - apply per-color alpha before interpolation (matches old version of vertices)
72 //
73 static SkPMColor4f* convert_colors(const SkColor src[],
74 int count,
75 SkColorSpace* deviceCS,
76 SkArenaAlloc* alloc,
77 bool skipColorXform) {
78 SkPMColor4f* dst = alloc->makeArray<SkPMColor4f>(count);
79
80 // Passing `nullptr` for the destination CS effectively disables color conversion.
81 auto dstCS = skipColorXform ? nullptr : sk_ref_sp(deviceCS);
82 SkImageInfo srcInfo = SkImageInfo::Make(
83 count, 1, kBGRA_8888_SkColorType, kUnpremul_SkAlphaType, SkColorSpace::MakeSRGB());
84 SkImageInfo dstInfo =
85 SkImageInfo::Make(count, 1, kRGBA_F32_SkColorType, kPremul_SkAlphaType, dstCS);
86 SkAssertResult(SkConvertPixels(dstInfo, dst, 0, srcInfo, src, 0));
87 return dst;
88 }
89
90 static bool compute_is_opaque(const SkColor colors[], int count) {
91 uint32_t c = ~0;
92 for (int i = 0; i < count; ++i) {
93 c &= colors[i];
94 }
95 return SkColorGetA(c) == 0xFF;
96 }
97
98 static void fill_triangle_2(const VertState& state, SkBlitter* blitter, const SkRasterClip& rc,
99 const SkPoint dev2[]) {
100 SkPoint tmp[] = {
101 dev2[state.f0], dev2[state.f1], dev2[state.f2]
102 };
103 SkScan::FillTriangle(tmp, rc, blitter);
104 }
105
106 static constexpr int kMaxClippedTrianglePointCount = 4;
107 static void fill_triangle_3(const VertState& state, SkBlitter* blitter, const SkRasterClip& rc,
108 const SkPoint3 dev3[]) {
109 // Compute the crossing point (across zero) for the two values, expressed as a
110 // normalized 0...1 value. If curr is 0, returns 0. If next is 0, returns 1.
111 auto computeT = [](float curr, float next) {
112 // Check that 0 is between next and curr.
113 SkASSERT((next <= 0 && 0 < curr) || (curr <= 0 && 0 < next));
114 float t = curr / (curr - next);
115 SkASSERT(0 <= t && t <= 1);
116 return t;
117 };
118
119 auto lerp = [](SkPoint3 curr, SkPoint3 next, float t) {
120 return curr + t * (next - curr);
121 };
122
123 constexpr float tol = 0.05f;
124 // tol is the nudge away from zero, to keep the numerics nice.
125 // Think of it as our near-clipping-plane (or w-plane).
126 auto clip = [&](SkPoint3 curr, SkPoint3 next) {
127 // Return the point between curr and next where the fZ value crosses tol.
128 // To be (really) perspective correct, we should be computing based on 1/Z, not Z.
129 // For now, this is close enough (and faster).
130 return lerp(curr, next, computeT(curr.fZ - tol, next.fZ - tol));
131 };
132
133 // Clip a triangle (based on its homogeneous W values), and return the projected polygon.
134 // Since we only clip against one "edge"/plane, the max number of points in the clipped
135 // polygon is 4.
136 auto clipTriangle = [&](SkPoint dst[], const int idx[3], const SkPoint3 pts[]) -> int {
137 SkPoint3 outPoints[kMaxClippedTrianglePointCount];
138 SkPoint3* outP = outPoints;
139
140 for (int i = 0; i < 3; ++i) {
141 int curr = idx[i];
142 int next = idx[(i + 1) % 3];
143 if (pts[curr].fZ > tol) {
144 *outP++ = pts[curr];
145 if (pts[next].fZ <= tol) { // curr is IN, next is OUT
146 *outP++ = clip(pts[curr], pts[next]);
147 }
148 } else {
149 if (pts[next].fZ > tol) { // curr is OUT, next is IN
150 *outP++ = clip(pts[curr], pts[next]);
151 }
152 }
153 }
154
155 const int count = SkTo<int>(outP - outPoints);
156 SkASSERT(count == 0 || count == 3 || count == 4);
157 for (int i = 0; i < count; ++i) {
158 float scale = sk_ieee_float_divide(1.0f, outPoints[i].fZ);
159 dst[i].set(outPoints[i].fX * scale, outPoints[i].fY * scale);
160 }
161 return count;
162 };
163
164 SkPoint tmp[kMaxClippedTrianglePointCount];
165 int idx[] = { state.f0, state.f1, state.f2 };
166 if (int n = clipTriangle(tmp, idx, dev3)) {
167 // TODO: SkScan::FillConvexPoly(tmp, n, ...);
168 SkASSERT(n == 3 || n == 4);
169 SkScan::FillTriangle(tmp, rc, blitter);
170 if (n == 4) {
171 tmp[1] = tmp[2];
172 tmp[2] = tmp[3];
173 SkScan::FillTriangle(tmp, rc, blitter);
174 }
175 }
176 }
177
178 static void fill_triangle(const VertState& state, SkBlitter* blitter, const SkRasterClip& rc,
179 const SkPoint dev2[], const SkPoint3 dev3[]) {
180 if (dev3) {
181 fill_triangle_3(state, blitter, rc, dev3);
182 } else {
183 fill_triangle_2(state, blitter, rc, dev2);
184 }
185 }
186
187 namespace skcpu {
188 void Draw::drawFixedVertices(const SkVertices* vertices,
189 sk_sp<SkBlender> blender,
190 const SkPaint& paint,
191 const SkMatrix& ctmInverse,
192 const SkPoint* dev2,
193 const SkPoint3* dev3,
194 SkArenaAlloc* outerAlloc,
195 bool skipColorXform) const {
196 SkVerticesPriv info(vertices->priv());
197
198 const int vertexCount = info.vertexCount();
199 const int indexCount = info.indexCount();
200 const SkPoint* positions = info.positions();
201 const SkPoint* texCoords = info.texCoords();
202 const uint16_t* indices = info.indices();
203 const SkColor* colors = info.colors();
204
205 SkShader* paintShader = paint.getShader();
206
207 if (paintShader) {
208 if (!texCoords) {
209 texCoords = positions;
210 }
211 } else {
212 texCoords = nullptr;
213 }
214
215 bool blenderIsDst = false;
216 // We can simplify things for certain blend modes. This is for speed, and SkShader_Blend
217 // itself insists we don't pass kSrc or kDst to it.
218 if (std::optional<SkBlendMode> bm = as_BB(blender)->asBlendMode(); bm.has_value() && colors) {
219 switch (*bm) {
220 case SkBlendMode::kSrc:
221 colors = nullptr;
222 break;
223 case SkBlendMode::kDst:
224 blenderIsDst = true;
225 texCoords = nullptr;
226 paintShader = nullptr;
227 break;
228 default: break;
229 }
230 }
231
232 // There is a paintShader iff there is texCoords.
233 SkASSERT((texCoords != nullptr) == (paintShader != nullptr));
234
235 // Explicit texture coords can't contain perspective - only the CTM can.
236 const bool usePerspective = fCTM->hasPerspective();
237
238 SkTriColorShader* triColorShader = nullptr;
239 SkPMColor4f* dstColors = nullptr;
240 if (colors) {
241 dstColors =
242 convert_colors(colors, vertexCount, fDst.colorSpace(), outerAlloc, skipColorXform);
243 triColorShader = outerAlloc->make<SkTriColorShader>(compute_is_opaque(colors, vertexCount),
244 usePerspective);
245 }
246
247 // Combines per-vertex colors with 'shader' using 'blender'.
248 auto applyShaderColorBlend = [&](SkShader* shader) -> sk_sp<SkShader> {
249 if (!colors) {
250 return sk_ref_sp(shader);
251 }
252 if (blenderIsDst) {
253 return sk_ref_sp(triColorShader);
254 }
255 sk_sp<SkShader> shaderWithWhichToBlend;
256 if (!shader) {
257 // When there is no shader then the blender applies to the vertex colors and opaque
258 // paint color.
259 shaderWithWhichToBlend = SkShaders::Color(paint.getColor4f().makeOpaque(), nullptr);
260 } else {
261 shaderWithWhichToBlend = sk_ref_sp(shader);
262 }
263 return SkShaders::Blend(blender,
264 sk_ref_sp(triColorShader),
265 std::move(shaderWithWhichToBlend));
266 };
267
268 // If there are separate texture coords then we need to insert a transform shader to update
269 // a matrix derived from each triangle's coords. In that case we will fold the CTM into
270 // each update and use an identity matrix.
271 SkTransformShader* transformShader = nullptr;
272 const SkMatrix* ctm = fCTM;
273 if (texCoords && texCoords != positions) {
274 paintShader = transformShader = outerAlloc->make<SkTransformShader>(*as_SB(paintShader),
275 usePerspective);
276 ctm = &SkMatrix::I();
277 }
278 sk_sp<SkShader> blenderShader = applyShaderColorBlend(paintShader);
279
280 SkPaint finalPaint{paint};
281 finalPaint.setShader(std::move(blenderShader));
282
283 VertState state(vertexCount, indices, indexCount);
284 VertState::Proc vertProc = state.chooseProc(info.mode());
285 SkSurfaceProps props = SkSurfacePropsCopyOrDefault(fProps);
286
287 auto blitter = SkCreateRasterPipelineBlitter(fDst,
288 finalPaint,
289 *ctm,
290 outerAlloc,
291 fRC->clipShader(),
292 props,
293 SkRect::MakeEmpty());
294 if (!blitter) {
295 return;
296 }
297 while (vertProc(&state)) {
298 if (triColorShader && !triColorShader->update(ctmInverse, positions, dstColors,
299 state.f0, state.f1, state.f2)) {
300 continue;
301 }
302
303 std::optional<SkMatrix> localM;
304 if (!transformShader || ((localM = texture_to_matrix(state, positions, texCoords)) &&
305 transformShader->update(SkMatrix::Concat(*localM, ctmInverse)))) {
306 fill_triangle(state, blitter, *fRC, dev2, dev3);
307 }
308 }
309 }
310
311 void Draw::drawVertices(const SkVertices* vertices,
312 sk_sp<SkBlender> blender,
313 const SkPaint& paint,
314 bool skipColorXform) const {
315 SkVerticesPriv info(vertices->priv());
316 const size_t vertexCount = info.vertexCount();
317 const size_t indexCount = info.indexCount();
318
319 // abort early if there is nothing to draw
320 if (vertexCount < 3 || (indexCount > 0 && indexCount < 3) || fRC->isEmpty()) {
321 return;
322 }
323 auto ctmInv = fCTM->invert();
324 if (!ctmInv) {
325 return;
326 }
327
328 constexpr size_t kDefVertexCount = 16;
329 constexpr size_t kOuterSize = sizeof(SkTriColorShader) +
330 (2 * sizeof(SkPoint) + sizeof(SkColor4f)) * kDefVertexCount;
331 SkSTArenaAlloc<kOuterSize> outerAlloc;
332
333 SkPoint* dev2 = nullptr;
334 SkPoint3* dev3 = nullptr;
335
336 if (fCTM->hasPerspective()) {
337 dev3 = outerAlloc.makeArray<SkPoint3>(vertexCount);
338 fCTM->mapPointsToHomogeneous({dev3, vertexCount}, {info.positions(), vertexCount});
339 // similar to the bounds check for 2d points (below)
340 if (!SkIsFinite((const SkScalar*)dev3, vertexCount * 3)) {
341 return;
342 }
343 } else {
344 dev2 = outerAlloc.makeArray<SkPoint>(vertexCount);
345 fCTM->mapPoints({dev2, vertexCount}, {info.positions(), vertexCount});
346
347 if (SkRect::BoundsOrEmpty({dev2, vertexCount}).isEmpty()) {
348 return;
349 }
350 }
351
352 this->drawFixedVertices(
353 vertices, std::move(blender), paint, *ctmInv, dev2, dev3, &outerAlloc, skipColorXform);
354 }
355 } // namespace skcpu