blob: f918269992e96d8f2e4bb06d7b2418bf9651567e [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) {
144 dst[i] = SkPM4f_from_SkColor(src[i], nullptr);
145 }
146 } else {
Mike Reed02640952017-05-19 15:32:13 -0400147 auto srcCS = SkColorSpace::MakeSRGB();
Brian Osman36703d92017-12-12 14:09:31 -0500148 auto dstCS = deviceCS->makeLinearGamma();
Mike Reededf8a762017-05-22 13:41:36 -0400149 SkColorSpaceXform::Apply(dstCS.get(), SkColorSpaceXform::kRGBA_F32_ColorFormat, dst,
150 srcCS.get(), SkColorSpaceXform::kBGRA_8888_ColorFormat, src,
151 count, SkColorSpaceXform::kPremul_AlphaOp);
Mike Reed02640952017-05-19 15:32:13 -0400152 }
153 return dst;
154}
155
156static bool compute_is_opaque(const SkColor colors[], int count) {
157 uint32_t c = ~0;
158 for (int i = 0; i < count; ++i) {
159 c &= colors[i];
160 }
161 return SkColorGetA(c) == 0xFF;
162}
163
Ruiqi Maof5101492018-06-29 14:32:21 -0400164void SkDraw::drawVertices(SkVertices::VertexMode vmode, int vertexCount,
Mike Reed787a16d2017-05-15 09:29:18 -0400165 const SkPoint vertices[], const SkPoint textures[],
Ruiqi Maof5101492018-06-29 14:32:21 -0400166 const SkColor colors[], const SkVertices::BoneIndices boneIndices[],
167 const SkVertices::BoneWeights boneWeights[], SkBlendMode bmode,
Mike Reed787a16d2017-05-15 09:29:18 -0400168 const uint16_t indices[], int indexCount,
Ruiqi Maof5101492018-06-29 14:32:21 -0400169 const SkPaint& paint, const SkMatrix* bones, int boneCount) const {
170 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;
209 constexpr size_t kDefBoneCount = 8;
210 constexpr size_t kOuterSize = sizeof(SkTriColorShader) +
Mike Reed176f19c2017-05-24 13:53:36 -0400211 sizeof(SkComposeShader) +
Ruiqi Maof5101492018-06-29 14:32:21 -0400212 (2 * sizeof(SkPoint) + sizeof(SkPM4f)) * kDefVertexCount +
213 sizeof(SkMatrix) * kDefBoneCount;
214 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
221 // get the bone matrices
222 SkMatrix* transformedBones = outerAlloc.makeArray<SkMatrix>(boneCount);
223
224 // transform the bone matrices by the world transform
225 transformedBones[0] = bones[0];
226 for (int i = 1; i < boneCount; i ++) {
227 transformedBones[i] = SkMatrix::Concat(bones[i], bones[0]);
228 }
229
230 // deform the vertices
231 if (boneIndices && boneWeights) {
232 for (int i = 0; i < vertexCount; i ++) {
233 const SkVertices::BoneIndices& indices = boneIndices[i];
234 const SkVertices::BoneWeights& weights = boneWeights[i];
235
236 // apply bone deformations
237 SkPoint result = SkPoint::Make(0.0f, 0.0f);
238 SkPoint transformed;
239 for (uint32_t j = 0; j < 4; j ++) {
240 // get the attachment data
241 uint32_t index = indices.indices[j];
242 float weight = weights.weights[j];
243
244 // skip the bone if there is no weight
245 if (weight == 0.0f) {
246 continue;
247 }
248 SkASSERT(index != 0);
249
250 // transformed = M * v
251 transformedBones[index].mapPoints(&transformed, &vertices[i], 1);
252
253 // result += transformed * w
254 result += transformed * weight;
255 }
256
257 // set the deformed point
258 deformed[i] = result;
259 }
260 } else {
261 // no bones, so only apply world transform
262 const SkMatrix& worldTransform = bones[0];
263 worldTransform.mapPoints(deformed, vertices, vertexCount);
264 }
265
266 // change the vertices to point to deformed
267 vertices = deformed;
268 }
269
270 SkPoint* devVerts = outerAlloc.makeArray<SkPoint>(vertexCount);
271 fMatrix->mapPoints(devVerts, vertices, vertexCount);
Mike Reed787a16d2017-05-15 09:29:18 -0400272
Mike Reed81afc042018-05-02 13:15:06 -0400273 {
274 SkRect bounds;
275 // this also sets bounds to empty if we see a non-finite value
Ruiqi Maof5101492018-06-29 14:32:21 -0400276 bounds.set(devVerts, vertexCount);
Mike Reed81afc042018-05-02 13:15:06 -0400277 if (bounds.isEmpty()) {
278 return;
279 }
280 }
281
Ruiqi Maof5101492018-06-29 14:32:21 -0400282 VertState state(vertexCount, indices, indexCount);
Mike Reed787a16d2017-05-15 09:29:18 -0400283 VertState::Proc vertProc = state.chooseProc(vmode);
284
Mike Reed176f19c2017-05-24 13:53:36 -0400285 if (colors || textures) {
286 SkPM4f* dstColors = nullptr;
287 Matrix43* matrix43 = nullptr;
288
289 if (colors) {
Ruiqi Maof5101492018-06-29 14:32:21 -0400290 dstColors = convert_colors(colors, vertexCount, fDst.colorSpace(), &outerAlloc);
Mike Reed176f19c2017-05-24 13:53:36 -0400291
292 SkTriColorShader* triShader = outerAlloc.make<SkTriColorShader>(
Ruiqi Maof5101492018-06-29 14:32:21 -0400293 compute_is_opaque(colors,
294 vertexCount));
Mike Reed176f19c2017-05-24 13:53:36 -0400295 matrix43 = triShader->getMatrix43();
296 if (shader) {
297 shader = outerAlloc.make<SkComposeShader>(sk_ref_sp(triShader), sk_ref_sp(shader),
Mike Reed01b2b832017-06-09 10:51:52 -0400298 bmode, 1);
Mike Reed176f19c2017-05-24 13:53:36 -0400299 } else {
300 shader = triShader;
301 }
302 }
303
304 SkPaint p(paint);
305 p.setShader(sk_ref_sp(shader));
Mike Reed787a16d2017-05-15 09:29:18 -0400306
Mike Reede28bbcf2017-05-26 09:52:05 -0400307 if (!textures) { // only tricolor shader
308 SkASSERT(matrix43);
309 auto blitter = SkCreateRasterPipelineBlitter(fDst, p, *fMatrix, &outerAlloc);
310 while (vertProc(&state)) {
311 if (!update_tricolor_matrix(ctmInv, vertices, dstColors,
312 state.f0, state.f1, state.f2,
313 matrix43)) {
314 continue;
315 }
Mike Reed787a16d2017-05-15 09:29:18 -0400316
Mike Reede28bbcf2017-05-26 09:52:05 -0400317 SkPoint tmp[] = {
318 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
319 };
320 SkScan::FillTriangle(tmp, *fRC, blitter);
Mike Reed787a16d2017-05-15 09:29:18 -0400321 }
Mike Reede28bbcf2017-05-26 09:52:05 -0400322 } else {
323 while (vertProc(&state)) {
324 SkSTArenaAlloc<2048> innerAlloc;
Mike Reed176f19c2017-05-24 13:53:36 -0400325
Mike Reede28bbcf2017-05-26 09:52:05 -0400326 const SkMatrix* ctm = fMatrix;
327 SkMatrix tmpCtm;
328 if (textures) {
329 SkMatrix localM;
Mike Reedefb25fd2018-02-05 12:43:18 -0500330 if (!texture_to_matrix(state, vertices, textures, &localM)) {
331 continue;
332 }
Mike Reede28bbcf2017-05-26 09:52:05 -0400333 tmpCtm = SkMatrix::Concat(*fMatrix, localM);
334 ctm = &tmpCtm;
335 }
336
337 if (matrix43 && !update_tricolor_matrix(ctmInv, vertices, dstColors,
338 state.f0, state.f1, state.f2,
339 matrix43)) {
340 continue;
341 }
342
343 SkPoint tmp[] = {
344 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
345 };
346 auto blitter = SkCreateRasterPipelineBlitter(fDst, p, *ctm, &innerAlloc);
347 SkScan::FillTriangle(tmp, *fRC, blitter);
Mike Reed787a16d2017-05-15 09:29:18 -0400348 }
Mike Reed787a16d2017-05-15 09:29:18 -0400349 }
350 } else {
351 // no colors[] and no texture, stroke hairlines with paint's color.
Mike Reed176f19c2017-05-24 13:53:36 -0400352 SkPaint p;
353 p.setStyle(SkPaint::kStroke_Style);
Mike Reed910ca0f2018-04-25 13:04:05 -0400354 SkAutoBlitterChoose blitter(*this, nullptr, p);
Mike Reed176f19c2017-05-24 13:53:36 -0400355 // Abort early if we failed to create a shader context.
356 if (blitter->isNullBlitter()) {
357 return;
358 }
Mike Reed787a16d2017-05-15 09:29:18 -0400359 SkScan::HairRCProc hairProc = ChooseHairProc(paint.isAntiAlias());
360 const SkRasterClip& clip = *fRC;
361 while (vertProc(&state)) {
362 SkPoint array[] = {
363 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2], devVerts[state.f0]
364 };
365 hairProc(array, 4, clip, blitter.get());
366 }
367 }
368}