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/SkString.h"
9 #include "include/private/SkNx.h"
10 #include "src/core/SkArenaAlloc.h"
11 #include "src/core/SkAutoBlitterChoose.h"
12 #include "src/core/SkConvertPixels.h"
13 #include "src/core/SkCoreBlitters.h"
14 #include "src/core/SkDraw.h"
15 #include "src/core/SkRasterClip.h"
16 #include "src/core/SkRasterPipeline.h"
17 #include "src/core/SkScan.h"
18 #include "src/core/SkVertState.h"
19 #include "src/shaders/SkComposeShader.h"
20 #include "src/shaders/SkShaderBase.h"
21
22 struct Matrix43 {
23 float fMat[12]; // column major
24
mapMatrix4325 Sk4f map(float x, float y) const {
26 return Sk4f::Load(&fMat[0]) * x + Sk4f::Load(&fMat[4]) * y + Sk4f::Load(&fMat[8]);
27 }
28
setConcatMatrix4329 void setConcat(const Matrix43& a, const SkMatrix& b) {
30 fMat[ 0] = a.dot(0, b.getScaleX(), b.getSkewY());
31 fMat[ 1] = a.dot(1, b.getScaleX(), b.getSkewY());
32 fMat[ 2] = a.dot(2, b.getScaleX(), b.getSkewY());
33 fMat[ 3] = a.dot(3, b.getScaleX(), b.getSkewY());
34
35 fMat[ 4] = a.dot(0, b.getSkewX(), b.getScaleY());
36 fMat[ 5] = a.dot(1, b.getSkewX(), b.getScaleY());
37 fMat[ 6] = a.dot(2, b.getSkewX(), b.getScaleY());
38 fMat[ 7] = a.dot(3, b.getSkewX(), b.getScaleY());
39
40 fMat[ 8] = a.dot(0, b.getTranslateX(), b.getTranslateY()) + a.fMat[ 8];
41 fMat[ 9] = a.dot(1, b.getTranslateX(), b.getTranslateY()) + a.fMat[ 9];
42 fMat[10] = a.dot(2, b.getTranslateX(), b.getTranslateY()) + a.fMat[10];
43 fMat[11] = a.dot(3, b.getTranslateX(), b.getTranslateY()) + a.fMat[11];
44 }
45
46 private:
dotMatrix4347 float dot(int index, float x, float y) const {
48 return fMat[index + 0] * x + fMat[index + 4] * y;
49 }
50 };
51
ChooseHairProc(bool doAntiAlias)52 static SkScan::HairRCProc ChooseHairProc(bool doAntiAlias) {
53 return doAntiAlias ? SkScan::AntiHairLine : SkScan::HairLine;
54 }
55
56 static bool SK_WARN_UNUSED_RESULT
texture_to_matrix(const VertState & state,const SkPoint verts[],const SkPoint texs[],SkMatrix * matrix)57 texture_to_matrix(const VertState& state, const SkPoint verts[], const SkPoint texs[],
58 SkMatrix* matrix) {
59 SkPoint src[3], dst[3];
60
61 src[0] = texs[state.f0];
62 src[1] = texs[state.f1];
63 src[2] = texs[state.f2];
64 dst[0] = verts[state.f0];
65 dst[1] = verts[state.f1];
66 dst[2] = verts[state.f2];
67 return matrix->setPolyToPoly(src, dst, 3);
68 }
69
70 class SkTriColorShader : public SkShaderBase {
71 public:
SkTriColorShader(bool isOpaque)72 SkTriColorShader(bool isOpaque) : fIsOpaque(isOpaque) {}
73
getMatrix43()74 Matrix43* getMatrix43() { return &fM43; }
75
isOpaque() const76 bool isOpaque() const override { return fIsOpaque; }
77
78
79 protected:
80 #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
onMakeContext(const ContextRec & rec,SkArenaAlloc * alloc) const81 Context* onMakeContext(const ContextRec& rec, SkArenaAlloc* alloc) const override {
82 return nullptr;
83 }
84 #endif
onAppendStages(const SkStageRec & rec) const85 bool onAppendStages(const SkStageRec& rec) const override {
86 rec.fPipeline->append(SkRasterPipeline::seed_shader);
87 rec.fPipeline->append(SkRasterPipeline::matrix_4x3, &fM43);
88 return true;
89 }
90
91 private:
92 // For serialization. This will never be called.
getFactory() const93 Factory getFactory() const override { return nullptr; }
getTypeName() const94 const char* getTypeName() const override { return nullptr; }
95
96 Matrix43 fM43;
97 const bool fIsOpaque;
98
99 typedef SkShaderBase INHERITED;
100 };
101
102 static bool SK_WARN_UNUSED_RESULT
update_tricolor_matrix(const SkMatrix & ctmInv,const SkPoint pts[],const SkPMColor4f colors[],int index0,int index1,int index2,Matrix43 * result)103 update_tricolor_matrix(const SkMatrix& ctmInv, const SkPoint pts[], const SkPMColor4f colors[],
104 int index0, int index1, int index2, Matrix43* result) {
105 SkMatrix m, im;
106 m.reset();
107 m.set(0, pts[index1].fX - pts[index0].fX);
108 m.set(1, pts[index2].fX - pts[index0].fX);
109 m.set(2, pts[index0].fX);
110 m.set(3, pts[index1].fY - pts[index0].fY);
111 m.set(4, pts[index2].fY - pts[index0].fY);
112 m.set(5, pts[index0].fY);
113 if (!m.invert(&im)) {
114 return false;
115 }
116
117 SkMatrix dstToUnit;
118 dstToUnit.setConcat(im, ctmInv);
119
120 Sk4f c0 = Sk4f::Load(colors[index0].vec()),
121 c1 = Sk4f::Load(colors[index1].vec()),
122 c2 = Sk4f::Load(colors[index2].vec());
123
124 Matrix43 colorm;
125 (c1 - c0).store(&colorm.fMat[0]);
126 (c2 - c0).store(&colorm.fMat[4]);
127 c0.store(&colorm.fMat[8]);
128 result->setConcat(colorm, dstToUnit);
129 return true;
130 }
131
132 // Convert the SkColors into float colors. The conversion depends on some conditions:
133 // - If the pixmap has a dst colorspace, we have to be "color-correct".
134 // Do we map into dst-colorspace before or after we interpolate?
135 // - We have to decide when to apply per-color alpha (before or after we interpolate)
136 //
137 // For now, we will take a simple approach, but recognize this is just a start:
138 // - convert colors into dst colorspace before interpolation (matches gradients)
139 // - apply per-color alpha before interpolation (matches old version of vertices)
140 //
convert_colors(const SkColor src[],int count,SkColorSpace * deviceCS,SkArenaAlloc * alloc)141 static SkPMColor4f* convert_colors(const SkColor src[], int count, SkColorSpace* deviceCS,
142 SkArenaAlloc* alloc) {
143 SkPMColor4f* dst = alloc->makeArray<SkPMColor4f>(count);
144 SkImageInfo srcInfo = SkImageInfo::Make(count, 1, kBGRA_8888_SkColorType,
145 kUnpremul_SkAlphaType, SkColorSpace::MakeSRGB());
146 SkImageInfo dstInfo = SkImageInfo::Make(count, 1, kRGBA_F32_SkColorType,
147 kPremul_SkAlphaType, sk_ref_sp(deviceCS));
148 SkConvertPixels(dstInfo, dst, 0, srcInfo, src, 0);
149 return dst;
150 }
151
compute_is_opaque(const SkColor colors[],int count)152 static bool compute_is_opaque(const SkColor colors[], int count) {
153 uint32_t c = ~0;
154 for (int i = 0; i < count; ++i) {
155 c &= colors[i];
156 }
157 return SkColorGetA(c) == 0xFF;
158 }
159
drawVertices(SkVertices::VertexMode vmode,int vertexCount,const SkPoint vertices[],const SkPoint textures[],const SkColor colors[],const SkVertices::BoneIndices boneIndices[],const SkVertices::BoneWeights boneWeights[],SkBlendMode bmode,const uint16_t indices[],int indexCount,const SkPaint & paint,const SkVertices::Bone bones[],int boneCount) const160 void SkDraw::drawVertices(SkVertices::VertexMode vmode, int vertexCount,
161 const SkPoint vertices[], const SkPoint textures[],
162 const SkColor colors[], const SkVertices::BoneIndices boneIndices[],
163 const SkVertices::BoneWeights boneWeights[], SkBlendMode bmode,
164 const uint16_t indices[], int indexCount,
165 const SkPaint& paint, const SkVertices::Bone bones[],
166 int boneCount) const {
167 SkASSERT(0 == vertexCount || vertices);
168
169 // abort early if there is nothing to draw
170 if (vertexCount < 3 || (indices && indexCount < 3) || fRC->isEmpty()) {
171 return;
172 }
173 SkMatrix ctmInv;
174 if (!fMatrix->invert(&ctmInv)) {
175 return;
176 }
177
178 // make textures and shader mutually consistent
179 SkShader* shader = paint.getShader();
180 if (!(shader && textures)) {
181 shader = nullptr;
182 textures = nullptr;
183 }
184
185 // We can simplify things for certain blendmodes. This is for speed, and SkComposeShader
186 // itself insists we don't pass kSrc or kDst to it.
187 //
188 if (colors && textures) {
189 switch (bmode) {
190 case SkBlendMode::kSrc:
191 colors = nullptr;
192 break;
193 case SkBlendMode::kDst:
194 textures = nullptr;
195 break;
196 default: break;
197 }
198 }
199
200 // we don't use the shader if there are no textures
201 if (!textures) {
202 shader = nullptr;
203 }
204
205 constexpr size_t kDefVertexCount = 16;
206 constexpr size_t kOuterSize = sizeof(SkTriColorShader) +
207 sizeof(SkShader_Blend) +
208 (2 * sizeof(SkPoint) + sizeof(SkColor4f)) * kDefVertexCount;
209 SkSTArenaAlloc<kOuterSize> outerAlloc;
210
211 // deform vertices using the skeleton if it is passed in
212 if (bones && boneCount) {
213 // allocate space for the deformed vertices
214 SkPoint* deformed = outerAlloc.makeArray<SkPoint>(vertexCount);
215
216 // deform the vertices
217 if (boneIndices && boneWeights) {
218 for (int i = 0; i < vertexCount; i ++) {
219 const SkVertices::BoneIndices& indices = boneIndices[i];
220 const SkVertices::BoneWeights& weights = boneWeights[i];
221
222 // apply the world transform
223 SkPoint worldPoint = bones[0].mapPoint(vertices[i]);
224
225 // apply bone deformations
226 deformed[i] = SkPoint::Make(0.0f, 0.0f);
227 for (uint32_t j = 0; j < 4; j ++) {
228 // get the attachment data
229 uint32_t index = indices[j];
230 float weight = weights[j];
231
232 // skip the bone if there is no weight
233 if (weight == 0.0f) {
234 continue;
235 }
236 SkASSERT(index != 0);
237
238 // deformed += M * v * w
239 deformed[i] += bones[index].mapPoint(worldPoint) * weight;
240 }
241 }
242 } else {
243 // no bones, so only apply world transform
244 SkMatrix worldTransform = SkMatrix::I();
245 worldTransform.setAffine(bones[0].values);
246 worldTransform.mapPoints(deformed, vertices, vertexCount);
247 }
248
249 // change the vertices to point to deformed
250 vertices = deformed;
251 }
252
253 SkPoint* devVerts = outerAlloc.makeArray<SkPoint>(vertexCount);
254 fMatrix->mapPoints(devVerts, vertices, vertexCount);
255
256 {
257 SkRect bounds;
258 // this also sets bounds to empty if we see a non-finite value
259 bounds.set(devVerts, vertexCount);
260 if (bounds.isEmpty()) {
261 return;
262 }
263 }
264
265 VertState state(vertexCount, indices, indexCount);
266 VertState::Proc vertProc = state.chooseProc(vmode);
267
268 if (colors || textures) {
269 SkPMColor4f* dstColors = nullptr;
270 Matrix43* matrix43 = nullptr;
271
272 if (colors) {
273 dstColors = convert_colors(colors, vertexCount, fDst.colorSpace(), &outerAlloc);
274
275 SkTriColorShader* triShader = outerAlloc.make<SkTriColorShader>(
276 compute_is_opaque(colors,
277 vertexCount));
278 matrix43 = triShader->getMatrix43();
279 if (shader) {
280 shader = outerAlloc.make<SkShader_Blend>(bmode,
281 sk_ref_sp(triShader), sk_ref_sp(shader),
282 nullptr);
283 } else {
284 shader = triShader;
285 }
286 }
287
288 SkPaint p(paint);
289 p.setShader(sk_ref_sp(shader));
290
291 if (!textures) { // only tricolor shader
292 SkASSERT(matrix43);
293 auto blitter = SkCreateRasterPipelineBlitter(fDst, p, *fMatrix, &outerAlloc);
294 while (vertProc(&state)) {
295 if (!update_tricolor_matrix(ctmInv, vertices, dstColors,
296 state.f0, state.f1, state.f2,
297 matrix43)) {
298 continue;
299 }
300
301 SkPoint tmp[] = {
302 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
303 };
304 SkScan::FillTriangle(tmp, *fRC, blitter);
305 }
306 } else {
307 while (vertProc(&state)) {
308 SkSTArenaAlloc<2048> innerAlloc;
309
310 const SkMatrix* ctm = fMatrix;
311 SkMatrix tmpCtm;
312 if (textures) {
313 SkMatrix localM;
314 if (!texture_to_matrix(state, vertices, textures, &localM)) {
315 continue;
316 }
317 tmpCtm = SkMatrix::Concat(*fMatrix, localM);
318 ctm = &tmpCtm;
319 }
320
321 if (matrix43 && !update_tricolor_matrix(ctmInv, vertices, dstColors,
322 state.f0, state.f1, state.f2,
323 matrix43)) {
324 continue;
325 }
326
327 SkPoint tmp[] = {
328 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
329 };
330 auto blitter = SkCreateRasterPipelineBlitter(fDst, p, *ctm, &innerAlloc);
331 SkScan::FillTriangle(tmp, *fRC, blitter);
332 }
333 }
334 } else {
335 // no colors[] and no texture, stroke hairlines with paint's color.
336 SkPaint p;
337 p.setStyle(SkPaint::kStroke_Style);
338 SkAutoBlitterChoose blitter(*this, nullptr, p);
339 // Abort early if we failed to create a shader context.
340 if (blitter->isNullBlitter()) {
341 return;
342 }
343 SkScan::HairRCProc hairProc = ChooseHairProc(paint.isAntiAlias());
344 const SkRasterClip& clip = *fRC;
345 while (vertProc(&state)) {
346 SkPoint array[] = {
347 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2], devVerts[state.f0]
348 };
349 hairProc(array, 4, clip, blitter.get());
350 }
351 }
352 }
353