blob: b9bc2e69f6facd833acf44071fe1057aaf3e2c2e [file] [log] [blame]
Mike Reed787a16d2017-05-15 09:29:18 -04001/*
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 "SkArenaAlloc.h"
9#include "SkAutoBlitterChoose.h"
Mike Reed176f19c2017-05-24 13:53:36 -040010#include "SkComposeShader.h"
Mike Reed787a16d2017-05-15 09:29:18 -040011#include "SkDraw.h"
12#include "SkNx.h"
Brian Osmand25b7c12018-09-21 16:01:59 -040013#include "SkPM4f.h"
Mike Reed787a16d2017-05-15 09:29:18 -040014#include "SkRasterClip.h"
15#include "SkScan.h"
Florin Malita4aed1382017-05-25 10:38:07 -040016#include "SkShaderBase.h"
Mike Reed787a16d2017-05-15 09:29:18 -040017#include "SkString.h"
18#include "SkVertState.h"
19
Mike Reed02640952017-05-19 15:32:13 -040020#include "SkArenaAlloc.h"
21#include "SkCoreBlitters.h"
22#include "SkColorSpaceXform.h"
Mike Reed02640952017-05-19 15:32:13 -040023
Mike Reed787a16d2017-05-15 09:29:18 -040024struct Matrix43 {
25 float fMat[12]; // column major
26
27 Sk4f map(float x, float y) const {
28 return Sk4f::Load(&fMat[0]) * x + Sk4f::Load(&fMat[4]) * y + Sk4f::Load(&fMat[8]);
29 }
30
31 void setConcat(const Matrix43& a, const SkMatrix& b) {
32 fMat[ 0] = a.dot(0, b.getScaleX(), b.getSkewY());
33 fMat[ 1] = a.dot(1, b.getScaleX(), b.getSkewY());
34 fMat[ 2] = a.dot(2, b.getScaleX(), b.getSkewY());
35 fMat[ 3] = a.dot(3, b.getScaleX(), b.getSkewY());
36
37 fMat[ 4] = a.dot(0, b.getSkewX(), b.getScaleY());
38 fMat[ 5] = a.dot(1, b.getSkewX(), b.getScaleY());
39 fMat[ 6] = a.dot(2, b.getSkewX(), b.getScaleY());
40 fMat[ 7] = a.dot(3, b.getSkewX(), b.getScaleY());
41
42 fMat[ 8] = a.dot(0, b.getTranslateX(), b.getTranslateY()) + a.fMat[ 8];
43 fMat[ 9] = a.dot(1, b.getTranslateX(), b.getTranslateY()) + a.fMat[ 9];
44 fMat[10] = a.dot(2, b.getTranslateX(), b.getTranslateY()) + a.fMat[10];
45 fMat[11] = a.dot(3, b.getTranslateX(), b.getTranslateY()) + a.fMat[11];
46 }
47
48private:
49 float dot(int index, float x, float y) const {
50 return fMat[index + 0] * x + fMat[index + 4] * y;
51 }
52};
53
54static SkScan::HairRCProc ChooseHairProc(bool doAntiAlias) {
55 return doAntiAlias ? SkScan::AntiHairLine : SkScan::HairLine;
56}
57
Mike Reedefb25fd2018-02-05 12:43:18 -050058static bool SK_WARN_UNUSED_RESULT
59texture_to_matrix(const VertState& state, const SkPoint verts[], const SkPoint texs[],
60 SkMatrix* matrix) {
Mike Reed787a16d2017-05-15 09:29:18 -040061 SkPoint src[3], dst[3];
62
63 src[0] = texs[state.f0];
64 src[1] = texs[state.f1];
65 src[2] = texs[state.f2];
66 dst[0] = verts[state.f0];
67 dst[1] = verts[state.f1];
68 dst[2] = verts[state.f2];
69 return matrix->setPolyToPoly(src, dst, 3);
70}
71
Florin Malita4aed1382017-05-25 10:38:07 -040072class SkTriColorShader : public SkShaderBase {
Mike Reed787a16d2017-05-15 09:29:18 -040073public:
Mike Reed176f19c2017-05-24 13:53:36 -040074 SkTriColorShader(bool isOpaque) : fIsOpaque(isOpaque) {}
Mike Reed787a16d2017-05-15 09:29:18 -040075
Mike Reed176f19c2017-05-24 13:53:36 -040076 Matrix43* getMatrix43() { return &fM43; }
Mike Reed787a16d2017-05-15 09:29:18 -040077
Mike Reed176f19c2017-05-24 13:53:36 -040078 bool isOpaque() const override { return fIsOpaque; }
Mike Reed787a16d2017-05-15 09:29:18 -040079
Mike Reed787a16d2017-05-15 09:29:18 -040080 // For serialization. This will never be called.
Ben Wagner7ca9a742017-08-17 14:05:04 -040081 Factory getFactory() const override { SK_ABORT("not reached"); return nullptr; }
Mike Reed787a16d2017-05-15 09:29:18 -040082
Mike Reed787a16d2017-05-15 09:29:18 -040083protected:
84 Context* onMakeContext(const ContextRec& rec, SkArenaAlloc* alloc) const override {
Mike Reed176f19c2017-05-24 13:53:36 -040085 return nullptr;
86 }
Mike Reed1d8c42e2017-08-29 14:58:19 -040087 bool onAppendStages(const StageRec& rec) const override {
Mike Kleine8de0242018-03-10 12:37:11 -050088 rec.fPipeline->append(SkRasterPipeline::seed_shader);
Mike Reed1d8c42e2017-08-29 14:58:19 -040089 rec.fPipeline->append(SkRasterPipeline::matrix_4x3, &fM43);
Mike Reed176f19c2017-05-24 13:53:36 -040090 return true;
Mike Reed787a16d2017-05-15 09:29:18 -040091 }
92
93private:
Mike Reed176f19c2017-05-24 13:53:36 -040094 Matrix43 fM43;
95 const bool fIsOpaque;
Mike Reed787a16d2017-05-15 09:29:18 -040096
Florin Malita4aed1382017-05-25 10:38:07 -040097 typedef SkShaderBase INHERITED;
Mike Reed787a16d2017-05-15 09:29:18 -040098};
99
Mike Reedefb25fd2018-02-05 12:43:18 -0500100static bool SK_WARN_UNUSED_RESULT
Brian Osmand25b7c12018-09-21 16:01:59 -0400101update_tricolor_matrix(const SkMatrix& ctmInv, const SkPoint pts[], const SkPMColor4f colors[],
Mike Reedefb25fd2018-02-05 12:43:18 -0500102 int index0, int index1, int index2, Matrix43* result) {
Mike Reed02640952017-05-19 15:32:13 -0400103 SkMatrix m, im;
104 m.reset();
105 m.set(0, pts[index1].fX - pts[index0].fX);
106 m.set(1, pts[index2].fX - pts[index0].fX);
107 m.set(2, pts[index0].fX);
108 m.set(3, pts[index1].fY - pts[index0].fY);
109 m.set(4, pts[index2].fY - pts[index0].fY);
110 m.set(5, pts[index0].fY);
111 if (!m.invert(&im)) {
112 return false;
113 }
114
115 SkMatrix dstToUnit;
116 dstToUnit.setConcat(im, ctmInv);
117
Brian Osman81cbd032018-09-21 11:09:15 -0400118 Sk4f c0 = Sk4f::Load(colors[index0].vec()),
119 c1 = Sk4f::Load(colors[index1].vec()),
120 c2 = Sk4f::Load(colors[index2].vec());
Mike Reed02640952017-05-19 15:32:13 -0400121
122 Matrix43 colorm;
123 (c1 - c0).store(&colorm.fMat[0]);
124 (c2 - c0).store(&colorm.fMat[4]);
125 c0.store(&colorm.fMat[8]);
126 result->setConcat(colorm, dstToUnit);
127 return true;
128}
129
Mike Reed176f19c2017-05-24 13:53:36 -0400130// Convert the SkColors into float colors. The conversion depends on some conditions:
131// - If the pixmap has a dst colorspace, we have to be "color-correct".
132// Do we map into dst-colorspace before or after we interpolate?
133// - We have to decide when to apply per-color alpha (before or after we interpolate)
134//
135// For now, we will take a simple approach, but recognize this is just a start:
136// - convert colors into dst colorspace before interpolation (matches gradients)
137// - apply per-color alpha before interpolation (matches old version of vertices)
138//
Brian Osmand25b7c12018-09-21 16:01:59 -0400139static SkPMColor4f* convert_colors(const SkColor src[], int count, SkColorSpace* deviceCS,
Brian Osman81cbd032018-09-21 11:09:15 -0400140 SkArenaAlloc* alloc) {
Brian Osmand25b7c12018-09-21 16:01:59 -0400141 SkPMColor4f* dst = alloc->makeArray<SkPMColor4f>(count);
Mike Reed02640952017-05-19 15:32:13 -0400142 if (!deviceCS) {
143 for (int i = 0; i < count; ++i) {
Brian Osman81cbd032018-09-21 11:09:15 -0400144 dst[i] = SkColor4f::FromColor(src[i]).premul();
Mike Reed02640952017-05-19 15:32:13 -0400145 }
146 } else {
Mike Reed02640952017-05-19 15:32:13 -0400147 auto srcCS = SkColorSpace::MakeSRGB();
Mike Kleine08059d2018-07-11 17:31:02 -0400148 SkColorSpaceXform::Apply(deviceCS , SkColorSpaceXform::kRGBA_F32_ColorFormat, dst,
Mike Reededf8a762017-05-22 13:41:36 -0400149 srcCS.get(), SkColorSpaceXform::kBGRA_8888_ColorFormat, src,
150 count, SkColorSpaceXform::kPremul_AlphaOp);
Mike Reed02640952017-05-19 15:32:13 -0400151 }
152 return dst;
153}
154
155static bool compute_is_opaque(const SkColor colors[], int count) {
156 uint32_t c = ~0;
157 for (int i = 0; i < count; ++i) {
158 c &= colors[i];
159 }
160 return SkColorGetA(c) == 0xFF;
161}
162
Ruiqi Maof5101492018-06-29 14:32:21 -0400163void SkDraw::drawVertices(SkVertices::VertexMode vmode, int vertexCount,
Mike Reed787a16d2017-05-15 09:29:18 -0400164 const SkPoint vertices[], const SkPoint textures[],
Ruiqi Maof5101492018-06-29 14:32:21 -0400165 const SkColor colors[], const SkVertices::BoneIndices boneIndices[],
166 const SkVertices::BoneWeights boneWeights[], SkBlendMode bmode,
Mike Reed787a16d2017-05-15 09:29:18 -0400167 const uint16_t indices[], int indexCount,
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400168 const SkPaint& paint, const SkVertices::Bone bones[],
169 int boneCount) const {
Ruiqi Maof5101492018-06-29 14:32:21 -0400170 SkASSERT(0 == vertexCount || vertices);
Mike Reed787a16d2017-05-15 09:29:18 -0400171
172 // abort early if there is nothing to draw
Ruiqi Maof5101492018-06-29 14:32:21 -0400173 if (vertexCount < 3 || (indices && indexCount < 3) || fRC->isEmpty()) {
Mike Reed787a16d2017-05-15 09:29:18 -0400174 return;
175 }
Mike Reed02640952017-05-19 15:32:13 -0400176 SkMatrix ctmInv;
177 if (!fMatrix->invert(&ctmInv)) {
178 return;
179 }
Mike Reed787a16d2017-05-15 09:29:18 -0400180
Mike Reed176f19c2017-05-24 13:53:36 -0400181 // make textures and shader mutually consistent
182 SkShader* shader = paint.getShader();
183 if (!(shader && textures)) {
184 shader = nullptr;
185 textures = nullptr;
186 }
187
Mike Reed0271c5b2017-06-02 14:52:22 -0400188 // We can simplify things for certain blendmodes. This is for speed, and SkComposeShader
189 // itself insists we don't pass kSrc or kDst to it.
190 //
191 if (colors && textures) {
192 switch (bmode) {
193 case SkBlendMode::kSrc:
194 colors = nullptr;
195 break;
196 case SkBlendMode::kDst:
197 textures = nullptr;
198 break;
199 default: break;
200 }
201 }
202
203 // we don't use the shader if there are no textures
204 if (!textures) {
205 shader = nullptr;
206 }
207
Ruiqi Maof5101492018-06-29 14:32:21 -0400208 constexpr size_t kDefVertexCount = 16;
Ruiqi Maof5101492018-06-29 14:32:21 -0400209 constexpr size_t kOuterSize = sizeof(SkTriColorShader) +
Mike Reed176f19c2017-05-24 13:53:36 -0400210 sizeof(SkComposeShader) +
Brian Osman81cbd032018-09-21 11:09:15 -0400211 (2 * sizeof(SkPoint) + sizeof(SkColor4f)) * kDefVertexCount;
Ruiqi Maof5101492018-06-29 14:32:21 -0400212 SkSTArenaAlloc<kOuterSize> outerAlloc;
Mike Reed176f19c2017-05-24 13:53:36 -0400213
Ruiqi Maof5101492018-06-29 14:32:21 -0400214 // deform vertices using the skeleton if it is passed in
215 if (bones && boneCount) {
216 // allocate space for the deformed vertices
217 SkPoint* deformed = outerAlloc.makeArray<SkPoint>(vertexCount);
218
Ruiqi Maof5101492018-06-29 14:32:21 -0400219 // deform the vertices
220 if (boneIndices && boneWeights) {
221 for (int i = 0; i < vertexCount; i ++) {
222 const SkVertices::BoneIndices& indices = boneIndices[i];
223 const SkVertices::BoneWeights& weights = boneWeights[i];
224
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400225 // apply the world transform
226 SkPoint worldPoint = bones[0].mapPoint(vertices[i]);
227
Ruiqi Maof5101492018-06-29 14:32:21 -0400228 // apply bone deformations
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400229 deformed[i] = SkPoint::Make(0.0f, 0.0f);
Ruiqi Maof5101492018-06-29 14:32:21 -0400230 for (uint32_t j = 0; j < 4; j ++) {
231 // get the attachment data
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400232 uint32_t index = indices[j];
233 float weight = weights[j];
Ruiqi Maof5101492018-06-29 14:32:21 -0400234
235 // skip the bone if there is no weight
236 if (weight == 0.0f) {
237 continue;
238 }
239 SkASSERT(index != 0);
240
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400241 // deformed += M * v * w
242 deformed[i] += bones[index].mapPoint(worldPoint) * weight;
Ruiqi Maof5101492018-06-29 14:32:21 -0400243 }
Ruiqi Maof5101492018-06-29 14:32:21 -0400244 }
245 } else {
246 // no bones, so only apply world transform
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400247 SkMatrix worldTransform = SkMatrix::I();
248 worldTransform.setAffine(bones[0].values);
Ruiqi Maof5101492018-06-29 14:32:21 -0400249 worldTransform.mapPoints(deformed, vertices, vertexCount);
250 }
251
252 // change the vertices to point to deformed
253 vertices = deformed;
254 }
255
256 SkPoint* devVerts = outerAlloc.makeArray<SkPoint>(vertexCount);
257 fMatrix->mapPoints(devVerts, vertices, vertexCount);
Mike Reed787a16d2017-05-15 09:29:18 -0400258
Mike Reed81afc042018-05-02 13:15:06 -0400259 {
260 SkRect bounds;
261 // this also sets bounds to empty if we see a non-finite value
Ruiqi Maof5101492018-06-29 14:32:21 -0400262 bounds.set(devVerts, vertexCount);
Mike Reed81afc042018-05-02 13:15:06 -0400263 if (bounds.isEmpty()) {
264 return;
265 }
266 }
267
Ruiqi Maof5101492018-06-29 14:32:21 -0400268 VertState state(vertexCount, indices, indexCount);
Mike Reed787a16d2017-05-15 09:29:18 -0400269 VertState::Proc vertProc = state.chooseProc(vmode);
270
Mike Reed176f19c2017-05-24 13:53:36 -0400271 if (colors || textures) {
Brian Osmand25b7c12018-09-21 16:01:59 -0400272 SkPMColor4f* dstColors = nullptr;
Mike Reed176f19c2017-05-24 13:53:36 -0400273 Matrix43* matrix43 = nullptr;
274
275 if (colors) {
Ruiqi Maof5101492018-06-29 14:32:21 -0400276 dstColors = convert_colors(colors, vertexCount, fDst.colorSpace(), &outerAlloc);
Mike Reed176f19c2017-05-24 13:53:36 -0400277
278 SkTriColorShader* triShader = outerAlloc.make<SkTriColorShader>(
Ruiqi Maof5101492018-06-29 14:32:21 -0400279 compute_is_opaque(colors,
280 vertexCount));
Mike Reed176f19c2017-05-24 13:53:36 -0400281 matrix43 = triShader->getMatrix43();
282 if (shader) {
283 shader = outerAlloc.make<SkComposeShader>(sk_ref_sp(triShader), sk_ref_sp(shader),
Mike Reed01b2b832017-06-09 10:51:52 -0400284 bmode, 1);
Mike Reed176f19c2017-05-24 13:53:36 -0400285 } else {
286 shader = triShader;
287 }
288 }
289
290 SkPaint p(paint);
291 p.setShader(sk_ref_sp(shader));
Mike Reed787a16d2017-05-15 09:29:18 -0400292
Mike Reede28bbcf2017-05-26 09:52:05 -0400293 if (!textures) { // only tricolor shader
294 SkASSERT(matrix43);
295 auto blitter = SkCreateRasterPipelineBlitter(fDst, p, *fMatrix, &outerAlloc);
296 while (vertProc(&state)) {
297 if (!update_tricolor_matrix(ctmInv, vertices, dstColors,
298 state.f0, state.f1, state.f2,
299 matrix43)) {
300 continue;
301 }
Mike Reed787a16d2017-05-15 09:29:18 -0400302
Mike Reede28bbcf2017-05-26 09:52:05 -0400303 SkPoint tmp[] = {
304 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
305 };
306 SkScan::FillTriangle(tmp, *fRC, blitter);
Mike Reed787a16d2017-05-15 09:29:18 -0400307 }
Mike Reede28bbcf2017-05-26 09:52:05 -0400308 } else {
309 while (vertProc(&state)) {
310 SkSTArenaAlloc<2048> innerAlloc;
Mike Reed176f19c2017-05-24 13:53:36 -0400311
Mike Reede28bbcf2017-05-26 09:52:05 -0400312 const SkMatrix* ctm = fMatrix;
313 SkMatrix tmpCtm;
314 if (textures) {
315 SkMatrix localM;
Mike Reedefb25fd2018-02-05 12:43:18 -0500316 if (!texture_to_matrix(state, vertices, textures, &localM)) {
317 continue;
318 }
Mike Reede28bbcf2017-05-26 09:52:05 -0400319 tmpCtm = SkMatrix::Concat(*fMatrix, localM);
320 ctm = &tmpCtm;
321 }
322
323 if (matrix43 && !update_tricolor_matrix(ctmInv, vertices, dstColors,
324 state.f0, state.f1, state.f2,
325 matrix43)) {
326 continue;
327 }
328
329 SkPoint tmp[] = {
330 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
331 };
332 auto blitter = SkCreateRasterPipelineBlitter(fDst, p, *ctm, &innerAlloc);
333 SkScan::FillTriangle(tmp, *fRC, blitter);
Mike Reed787a16d2017-05-15 09:29:18 -0400334 }
Mike Reed787a16d2017-05-15 09:29:18 -0400335 }
336 } else {
337 // no colors[] and no texture, stroke hairlines with paint's color.
Mike Reed176f19c2017-05-24 13:53:36 -0400338 SkPaint p;
339 p.setStyle(SkPaint::kStroke_Style);
Mike Reed910ca0f2018-04-25 13:04:05 -0400340 SkAutoBlitterChoose blitter(*this, nullptr, p);
Mike Reed176f19c2017-05-24 13:53:36 -0400341 // Abort early if we failed to create a shader context.
342 if (blitter->isNullBlitter()) {
343 return;
344 }
Mike Reed787a16d2017-05-15 09:29:18 -0400345 SkScan::HairRCProc hairProc = ChooseHairProc(paint.isAntiAlias());
346 const SkRasterClip& clip = *fRC;
347 while (vertProc(&state)) {
348 SkPoint array[] = {
349 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2], devVerts[state.f0]
350 };
351 hairProc(array, 4, clip, blitter.get());
352 }
353 }
354}