blob: 33fba60fced98236c713f702d5794a03e295534d [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"
Brian Osman5ea41fc2018-09-26 18:49:27 +000011#include "SkConvertPixels.h"
Mike Reed787a16d2017-05-15 09:29:18 -040012#include "SkDraw.h"
13#include "SkNx.h"
Brian Osmand25b7c12018-09-21 16:01:59 -040014#include "SkPM4f.h"
Mike Reed787a16d2017-05-15 09:29:18 -040015#include "SkRasterClip.h"
16#include "SkScan.h"
Florin Malita4aed1382017-05-25 10:38:07 -040017#include "SkShaderBase.h"
Mike Reed787a16d2017-05-15 09:29:18 -040018#include "SkString.h"
19#include "SkVertState.h"
20
Mike Reed02640952017-05-19 15:32:13 -040021#include "SkArenaAlloc.h"
22#include "SkCoreBlitters.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 Osman5ea41fc2018-09-26 18:49:27 +0000140 SkArenaAlloc* alloc) {
Brian Osmand25b7c12018-09-21 16:01:59 -0400141 SkPMColor4f* dst = alloc->makeArray<SkPMColor4f>(count);
Brian Osman5ea41fc2018-09-26 18:49:27 +0000142 SkImageInfo srcInfo = SkImageInfo::Make(count, 1, kBGRA_8888_SkColorType,
143 kUnpremul_SkAlphaType, SkColorSpace::MakeSRGB());
144 SkImageInfo dstInfo = SkImageInfo::Make(count, 1, kRGBA_F32_SkColorType,
145 kPremul_SkAlphaType, sk_ref_sp(deviceCS));
146 SkConvertPixels(dstInfo, dst, 0, srcInfo, src, 0);
Mike Reed02640952017-05-19 15:32:13 -0400147 return dst;
148}
149
150static bool compute_is_opaque(const SkColor colors[], int count) {
151 uint32_t c = ~0;
152 for (int i = 0; i < count; ++i) {
153 c &= colors[i];
154 }
155 return SkColorGetA(c) == 0xFF;
156}
157
Ruiqi Maof5101492018-06-29 14:32:21 -0400158void SkDraw::drawVertices(SkVertices::VertexMode vmode, int vertexCount,
Mike Reed787a16d2017-05-15 09:29:18 -0400159 const SkPoint vertices[], const SkPoint textures[],
Ruiqi Maof5101492018-06-29 14:32:21 -0400160 const SkColor colors[], const SkVertices::BoneIndices boneIndices[],
161 const SkVertices::BoneWeights boneWeights[], SkBlendMode bmode,
Mike Reed787a16d2017-05-15 09:29:18 -0400162 const uint16_t indices[], int indexCount,
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400163 const SkPaint& paint, const SkVertices::Bone bones[],
164 int boneCount) const {
Ruiqi Maof5101492018-06-29 14:32:21 -0400165 SkASSERT(0 == vertexCount || vertices);
Mike Reed787a16d2017-05-15 09:29:18 -0400166
167 // abort early if there is nothing to draw
Ruiqi Maof5101492018-06-29 14:32:21 -0400168 if (vertexCount < 3 || (indices && indexCount < 3) || fRC->isEmpty()) {
Mike Reed787a16d2017-05-15 09:29:18 -0400169 return;
170 }
Mike Reed02640952017-05-19 15:32:13 -0400171 SkMatrix ctmInv;
172 if (!fMatrix->invert(&ctmInv)) {
173 return;
174 }
Mike Reed787a16d2017-05-15 09:29:18 -0400175
Mike Reed176f19c2017-05-24 13:53:36 -0400176 // make textures and shader mutually consistent
177 SkShader* shader = paint.getShader();
178 if (!(shader && textures)) {
179 shader = nullptr;
180 textures = nullptr;
181 }
182
Mike Reed0271c5b2017-06-02 14:52:22 -0400183 // We can simplify things for certain blendmodes. This is for speed, and SkComposeShader
184 // itself insists we don't pass kSrc or kDst to it.
185 //
186 if (colors && textures) {
187 switch (bmode) {
188 case SkBlendMode::kSrc:
189 colors = nullptr;
190 break;
191 case SkBlendMode::kDst:
192 textures = nullptr;
193 break;
194 default: break;
195 }
196 }
197
198 // we don't use the shader if there are no textures
199 if (!textures) {
200 shader = nullptr;
201 }
202
Ruiqi Maof5101492018-06-29 14:32:21 -0400203 constexpr size_t kDefVertexCount = 16;
Ruiqi Maof5101492018-06-29 14:32:21 -0400204 constexpr size_t kOuterSize = sizeof(SkTriColorShader) +
Mike Reed176f19c2017-05-24 13:53:36 -0400205 sizeof(SkComposeShader) +
Brian Osman81cbd032018-09-21 11:09:15 -0400206 (2 * sizeof(SkPoint) + sizeof(SkColor4f)) * kDefVertexCount;
Ruiqi Maof5101492018-06-29 14:32:21 -0400207 SkSTArenaAlloc<kOuterSize> outerAlloc;
Mike Reed176f19c2017-05-24 13:53:36 -0400208
Ruiqi Maof5101492018-06-29 14:32:21 -0400209 // deform vertices using the skeleton if it is passed in
210 if (bones && boneCount) {
211 // allocate space for the deformed vertices
212 SkPoint* deformed = outerAlloc.makeArray<SkPoint>(vertexCount);
213
Ruiqi Maof5101492018-06-29 14:32:21 -0400214 // deform the vertices
215 if (boneIndices && boneWeights) {
216 for (int i = 0; i < vertexCount; i ++) {
217 const SkVertices::BoneIndices& indices = boneIndices[i];
218 const SkVertices::BoneWeights& weights = boneWeights[i];
219
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400220 // apply the world transform
221 SkPoint worldPoint = bones[0].mapPoint(vertices[i]);
222
Ruiqi Maof5101492018-06-29 14:32:21 -0400223 // apply bone deformations
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400224 deformed[i] = SkPoint::Make(0.0f, 0.0f);
Ruiqi Maof5101492018-06-29 14:32:21 -0400225 for (uint32_t j = 0; j < 4; j ++) {
226 // get the attachment data
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400227 uint32_t index = indices[j];
228 float weight = weights[j];
Ruiqi Maof5101492018-06-29 14:32:21 -0400229
230 // skip the bone if there is no weight
231 if (weight == 0.0f) {
232 continue;
233 }
234 SkASSERT(index != 0);
235
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400236 // deformed += M * v * w
237 deformed[i] += bones[index].mapPoint(worldPoint) * weight;
Ruiqi Maof5101492018-06-29 14:32:21 -0400238 }
Ruiqi Maof5101492018-06-29 14:32:21 -0400239 }
240 } else {
241 // no bones, so only apply world transform
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400242 SkMatrix worldTransform = SkMatrix::I();
243 worldTransform.setAffine(bones[0].values);
Ruiqi Maof5101492018-06-29 14:32:21 -0400244 worldTransform.mapPoints(deformed, vertices, vertexCount);
245 }
246
247 // change the vertices to point to deformed
248 vertices = deformed;
249 }
250
251 SkPoint* devVerts = outerAlloc.makeArray<SkPoint>(vertexCount);
252 fMatrix->mapPoints(devVerts, vertices, vertexCount);
Mike Reed787a16d2017-05-15 09:29:18 -0400253
Mike Reed81afc042018-05-02 13:15:06 -0400254 {
255 SkRect bounds;
256 // this also sets bounds to empty if we see a non-finite value
Ruiqi Maof5101492018-06-29 14:32:21 -0400257 bounds.set(devVerts, vertexCount);
Mike Reed81afc042018-05-02 13:15:06 -0400258 if (bounds.isEmpty()) {
259 return;
260 }
261 }
262
Ruiqi Maof5101492018-06-29 14:32:21 -0400263 VertState state(vertexCount, indices, indexCount);
Mike Reed787a16d2017-05-15 09:29:18 -0400264 VertState::Proc vertProc = state.chooseProc(vmode);
265
Mike Reed176f19c2017-05-24 13:53:36 -0400266 if (colors || textures) {
Brian Osmand25b7c12018-09-21 16:01:59 -0400267 SkPMColor4f* dstColors = nullptr;
Mike Reed176f19c2017-05-24 13:53:36 -0400268 Matrix43* matrix43 = nullptr;
269
270 if (colors) {
Ruiqi Maof5101492018-06-29 14:32:21 -0400271 dstColors = convert_colors(colors, vertexCount, fDst.colorSpace(), &outerAlloc);
Mike Reed176f19c2017-05-24 13:53:36 -0400272
273 SkTriColorShader* triShader = outerAlloc.make<SkTriColorShader>(
Ruiqi Maof5101492018-06-29 14:32:21 -0400274 compute_is_opaque(colors,
275 vertexCount));
Mike Reed176f19c2017-05-24 13:53:36 -0400276 matrix43 = triShader->getMatrix43();
277 if (shader) {
278 shader = outerAlloc.make<SkComposeShader>(sk_ref_sp(triShader), sk_ref_sp(shader),
Mike Reed01b2b832017-06-09 10:51:52 -0400279 bmode, 1);
Mike Reed176f19c2017-05-24 13:53:36 -0400280 } else {
281 shader = triShader;
282 }
283 }
284
285 SkPaint p(paint);
286 p.setShader(sk_ref_sp(shader));
Mike Reed787a16d2017-05-15 09:29:18 -0400287
Mike Reede28bbcf2017-05-26 09:52:05 -0400288 if (!textures) { // only tricolor shader
289 SkASSERT(matrix43);
290 auto blitter = SkCreateRasterPipelineBlitter(fDst, p, *fMatrix, &outerAlloc);
291 while (vertProc(&state)) {
292 if (!update_tricolor_matrix(ctmInv, vertices, dstColors,
293 state.f0, state.f1, state.f2,
294 matrix43)) {
295 continue;
296 }
Mike Reed787a16d2017-05-15 09:29:18 -0400297
Mike Reede28bbcf2017-05-26 09:52:05 -0400298 SkPoint tmp[] = {
299 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
300 };
301 SkScan::FillTriangle(tmp, *fRC, blitter);
Mike Reed787a16d2017-05-15 09:29:18 -0400302 }
Mike Reede28bbcf2017-05-26 09:52:05 -0400303 } else {
304 while (vertProc(&state)) {
305 SkSTArenaAlloc<2048> innerAlloc;
Mike Reed176f19c2017-05-24 13:53:36 -0400306
Mike Reede28bbcf2017-05-26 09:52:05 -0400307 const SkMatrix* ctm = fMatrix;
308 SkMatrix tmpCtm;
309 if (textures) {
310 SkMatrix localM;
Mike Reedefb25fd2018-02-05 12:43:18 -0500311 if (!texture_to_matrix(state, vertices, textures, &localM)) {
312 continue;
313 }
Mike Reede28bbcf2017-05-26 09:52:05 -0400314 tmpCtm = SkMatrix::Concat(*fMatrix, localM);
315 ctm = &tmpCtm;
316 }
317
318 if (matrix43 && !update_tricolor_matrix(ctmInv, vertices, dstColors,
319 state.f0, state.f1, state.f2,
320 matrix43)) {
321 continue;
322 }
323
324 SkPoint tmp[] = {
325 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
326 };
327 auto blitter = SkCreateRasterPipelineBlitter(fDst, p, *ctm, &innerAlloc);
328 SkScan::FillTriangle(tmp, *fRC, blitter);
Mike Reed787a16d2017-05-15 09:29:18 -0400329 }
Mike Reed787a16d2017-05-15 09:29:18 -0400330 }
331 } else {
332 // no colors[] and no texture, stroke hairlines with paint's color.
Mike Reed176f19c2017-05-24 13:53:36 -0400333 SkPaint p;
334 p.setStyle(SkPaint::kStroke_Style);
Mike Reed910ca0f2018-04-25 13:04:05 -0400335 SkAutoBlitterChoose blitter(*this, nullptr, p);
Mike Reed176f19c2017-05-24 13:53:36 -0400336 // Abort early if we failed to create a shader context.
337 if (blitter->isNullBlitter()) {
338 return;
339 }
Mike Reed787a16d2017-05-15 09:29:18 -0400340 SkScan::HairRCProc hairProc = ChooseHairProc(paint.isAntiAlias());
341 const SkRasterClip& clip = *fRC;
342 while (vertProc(&state)) {
343 SkPoint array[] = {
344 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2], devVerts[state.f0]
345 };
346 hairProc(array, 4, clip, blitter.get());
347 }
348 }
349}