blob: b9320d4e82ddbbcc0a8758361cbc7f011e63dedd [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"
Mike Reed02640952017-05-19 15:32:13 -040013#include "SkPM4fPriv.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
101update_tricolor_matrix(const SkMatrix& ctmInv, const SkPoint pts[], const SkPM4f colors[],
102 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
118 Sk4f c0 = colors[index0].to4f(),
119 c1 = colors[index1].to4f(),
120 c2 = colors[index2].to4f();
121
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//
Mike Reed02640952017-05-19 15:32:13 -0400139static SkPM4f* convert_colors(const SkColor src[], int count, SkColorSpace* deviceCS,
140 SkArenaAlloc* alloc) {
141 SkPM4f* dst = alloc->makeArray<SkPM4f>(count);
142 if (!deviceCS) {
143 for (int i = 0; i < count; ++i) {
Robert Phillips4a175012018-07-13 13:18:56 +0000144 SkColor4f color4f;
145 swizzle_rb(Sk4f_fromL32(src[i])).store(&color4f);
146 dst[i] = color4f.premul();
Mike Reed02640952017-05-19 15:32:13 -0400147 }
148 } else {
Mike Reed02640952017-05-19 15:32:13 -0400149 auto srcCS = SkColorSpace::MakeSRGB();
Mike Kleine08059d2018-07-11 17:31:02 -0400150 SkColorSpaceXform::Apply(deviceCS , SkColorSpaceXform::kRGBA_F32_ColorFormat, dst,
Mike Reededf8a762017-05-22 13:41:36 -0400151 srcCS.get(), SkColorSpaceXform::kBGRA_8888_ColorFormat, src,
152 count, SkColorSpaceXform::kPremul_AlphaOp);
Mike Reed02640952017-05-19 15:32:13 -0400153 }
154 return dst;
155}
156
157static bool compute_is_opaque(const SkColor colors[], int count) {
158 uint32_t c = ~0;
159 for (int i = 0; i < count; ++i) {
160 c &= colors[i];
161 }
162 return SkColorGetA(c) == 0xFF;
163}
164
Ruiqi Maof5101492018-06-29 14:32:21 -0400165void SkDraw::drawVertices(SkVertices::VertexMode vmode, int vertexCount,
Mike Reed787a16d2017-05-15 09:29:18 -0400166 const SkPoint vertices[], const SkPoint textures[],
Ruiqi Maof5101492018-06-29 14:32:21 -0400167 const SkColor colors[], const SkVertices::BoneIndices boneIndices[],
168 const SkVertices::BoneWeights boneWeights[], SkBlendMode bmode,
Mike Reed787a16d2017-05-15 09:29:18 -0400169 const uint16_t indices[], int indexCount,
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400170 const SkPaint& paint, const SkVertices::Bone bones[],
171 int boneCount) const {
Ruiqi Maof5101492018-06-29 14:32:21 -0400172 SkASSERT(0 == vertexCount || vertices);
Mike Reed787a16d2017-05-15 09:29:18 -0400173
174 // abort early if there is nothing to draw
Ruiqi Maof5101492018-06-29 14:32:21 -0400175 if (vertexCount < 3 || (indices && indexCount < 3) || fRC->isEmpty()) {
Mike Reed787a16d2017-05-15 09:29:18 -0400176 return;
177 }
Mike Reed02640952017-05-19 15:32:13 -0400178 SkMatrix ctmInv;
179 if (!fMatrix->invert(&ctmInv)) {
180 return;
181 }
Mike Reed787a16d2017-05-15 09:29:18 -0400182
Mike Reed176f19c2017-05-24 13:53:36 -0400183 // make textures and shader mutually consistent
184 SkShader* shader = paint.getShader();
185 if (!(shader && textures)) {
186 shader = nullptr;
187 textures = nullptr;
188 }
189
Mike Reed0271c5b2017-06-02 14:52:22 -0400190 // We can simplify things for certain blendmodes. This is for speed, and SkComposeShader
191 // itself insists we don't pass kSrc or kDst to it.
192 //
193 if (colors && textures) {
194 switch (bmode) {
195 case SkBlendMode::kSrc:
196 colors = nullptr;
197 break;
198 case SkBlendMode::kDst:
199 textures = nullptr;
200 break;
201 default: break;
202 }
203 }
204
205 // we don't use the shader if there are no textures
206 if (!textures) {
207 shader = nullptr;
208 }
209
Ruiqi Maof5101492018-06-29 14:32:21 -0400210 constexpr size_t kDefVertexCount = 16;
Ruiqi Maof5101492018-06-29 14:32:21 -0400211 constexpr size_t kOuterSize = sizeof(SkTriColorShader) +
Mike Reed176f19c2017-05-24 13:53:36 -0400212 sizeof(SkComposeShader) +
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400213 (2 * sizeof(SkPoint) + sizeof(SkPM4f)) * kDefVertexCount;
Ruiqi Maof5101492018-06-29 14:32:21 -0400214 SkSTArenaAlloc<kOuterSize> outerAlloc;
Mike Reed176f19c2017-05-24 13:53:36 -0400215
Ruiqi Maof5101492018-06-29 14:32:21 -0400216 // deform vertices using the skeleton if it is passed in
217 if (bones && boneCount) {
218 // allocate space for the deformed vertices
219 SkPoint* deformed = outerAlloc.makeArray<SkPoint>(vertexCount);
220
Ruiqi Maof5101492018-06-29 14:32:21 -0400221 // deform the vertices
222 if (boneIndices && boneWeights) {
223 for (int i = 0; i < vertexCount; i ++) {
224 const SkVertices::BoneIndices& indices = boneIndices[i];
225 const SkVertices::BoneWeights& weights = boneWeights[i];
226
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400227 // apply the world transform
228 SkPoint worldPoint = bones[0].mapPoint(vertices[i]);
229
Ruiqi Maof5101492018-06-29 14:32:21 -0400230 // apply bone deformations
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400231 deformed[i] = SkPoint::Make(0.0f, 0.0f);
Ruiqi Maof5101492018-06-29 14:32:21 -0400232 for (uint32_t j = 0; j < 4; j ++) {
233 // get the attachment data
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400234 uint32_t index = indices[j];
235 float weight = weights[j];
Ruiqi Maof5101492018-06-29 14:32:21 -0400236
237 // skip the bone if there is no weight
238 if (weight == 0.0f) {
239 continue;
240 }
241 SkASSERT(index != 0);
242
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400243 // deformed += M * v * w
244 deformed[i] += bones[index].mapPoint(worldPoint) * weight;
Ruiqi Maof5101492018-06-29 14:32:21 -0400245 }
Ruiqi Maof5101492018-06-29 14:32:21 -0400246 }
247 } else {
248 // no bones, so only apply world transform
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400249 SkMatrix worldTransform = SkMatrix::I();
250 worldTransform.setAffine(bones[0].values);
Ruiqi Maof5101492018-06-29 14:32:21 -0400251 worldTransform.mapPoints(deformed, vertices, vertexCount);
252 }
253
254 // change the vertices to point to deformed
255 vertices = deformed;
256 }
257
258 SkPoint* devVerts = outerAlloc.makeArray<SkPoint>(vertexCount);
259 fMatrix->mapPoints(devVerts, vertices, vertexCount);
Mike Reed787a16d2017-05-15 09:29:18 -0400260
Mike Reed81afc042018-05-02 13:15:06 -0400261 {
262 SkRect bounds;
263 // this also sets bounds to empty if we see a non-finite value
Ruiqi Maof5101492018-06-29 14:32:21 -0400264 bounds.set(devVerts, vertexCount);
Mike Reed81afc042018-05-02 13:15:06 -0400265 if (bounds.isEmpty()) {
266 return;
267 }
268 }
269
Ruiqi Maof5101492018-06-29 14:32:21 -0400270 VertState state(vertexCount, indices, indexCount);
Mike Reed787a16d2017-05-15 09:29:18 -0400271 VertState::Proc vertProc = state.chooseProc(vmode);
272
Mike Reed176f19c2017-05-24 13:53:36 -0400273 if (colors || textures) {
274 SkPM4f* dstColors = nullptr;
275 Matrix43* matrix43 = nullptr;
276
277 if (colors) {
Ruiqi Maof5101492018-06-29 14:32:21 -0400278 dstColors = convert_colors(colors, vertexCount, fDst.colorSpace(), &outerAlloc);
Mike Reed176f19c2017-05-24 13:53:36 -0400279
280 SkTriColorShader* triShader = outerAlloc.make<SkTriColorShader>(
Ruiqi Maof5101492018-06-29 14:32:21 -0400281 compute_is_opaque(colors,
282 vertexCount));
Mike Reed176f19c2017-05-24 13:53:36 -0400283 matrix43 = triShader->getMatrix43();
284 if (shader) {
285 shader = outerAlloc.make<SkComposeShader>(sk_ref_sp(triShader), sk_ref_sp(shader),
Mike Reed01b2b832017-06-09 10:51:52 -0400286 bmode, 1);
Mike Reed176f19c2017-05-24 13:53:36 -0400287 } else {
288 shader = triShader;
289 }
290 }
291
292 SkPaint p(paint);
293 p.setShader(sk_ref_sp(shader));
Mike Reed787a16d2017-05-15 09:29:18 -0400294
Mike Reede28bbcf2017-05-26 09:52:05 -0400295 if (!textures) { // only tricolor shader
296 SkASSERT(matrix43);
297 auto blitter = SkCreateRasterPipelineBlitter(fDst, p, *fMatrix, &outerAlloc);
298 while (vertProc(&state)) {
299 if (!update_tricolor_matrix(ctmInv, vertices, dstColors,
300 state.f0, state.f1, state.f2,
301 matrix43)) {
302 continue;
303 }
Mike Reed787a16d2017-05-15 09:29:18 -0400304
Mike Reede28bbcf2017-05-26 09:52:05 -0400305 SkPoint tmp[] = {
306 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
307 };
308 SkScan::FillTriangle(tmp, *fRC, blitter);
Mike Reed787a16d2017-05-15 09:29:18 -0400309 }
Mike Reede28bbcf2017-05-26 09:52:05 -0400310 } else {
311 while (vertProc(&state)) {
312 SkSTArenaAlloc<2048> innerAlloc;
Mike Reed176f19c2017-05-24 13:53:36 -0400313
Mike Reede28bbcf2017-05-26 09:52:05 -0400314 const SkMatrix* ctm = fMatrix;
315 SkMatrix tmpCtm;
316 if (textures) {
317 SkMatrix localM;
Mike Reedefb25fd2018-02-05 12:43:18 -0500318 if (!texture_to_matrix(state, vertices, textures, &localM)) {
319 continue;
320 }
Mike Reede28bbcf2017-05-26 09:52:05 -0400321 tmpCtm = SkMatrix::Concat(*fMatrix, localM);
322 ctm = &tmpCtm;
323 }
324
325 if (matrix43 && !update_tricolor_matrix(ctmInv, vertices, dstColors,
326 state.f0, state.f1, state.f2,
327 matrix43)) {
328 continue;
329 }
330
331 SkPoint tmp[] = {
332 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
333 };
334 auto blitter = SkCreateRasterPipelineBlitter(fDst, p, *ctm, &innerAlloc);
335 SkScan::FillTriangle(tmp, *fRC, blitter);
Mike Reed787a16d2017-05-15 09:29:18 -0400336 }
Mike Reed787a16d2017-05-15 09:29:18 -0400337 }
338 } else {
339 // no colors[] and no texture, stroke hairlines with paint's color.
Mike Reed176f19c2017-05-24 13:53:36 -0400340 SkPaint p;
341 p.setStyle(SkPaint::kStroke_Style);
Mike Reed910ca0f2018-04-25 13:04:05 -0400342 SkAutoBlitterChoose blitter(*this, nullptr, p);
Mike Reed176f19c2017-05-24 13:53:36 -0400343 // Abort early if we failed to create a shader context.
344 if (blitter->isNullBlitter()) {
345 return;
346 }
Mike Reed787a16d2017-05-15 09:29:18 -0400347 SkScan::HairRCProc hairProc = ChooseHairProc(paint.isAntiAlias());
348 const SkRasterClip& clip = *fRC;
349 while (vertProc(&state)) {
350 SkPoint array[] = {
351 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2], devVerts[state.f0]
352 };
353 hairProc(array, 4, clip, blitter.get());
354 }
355 }
356}