blob: f1f9ad7cd359d86fe9bace98bed890bcad768e1e [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
Mike Reed787a16d2017-05-15 09:29:18 -040081protected:
Mike Reede92aae62018-10-17 10:21:51 -040082#ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
Mike Reed787a16d2017-05-15 09:29:18 -040083 Context* onMakeContext(const ContextRec& rec, SkArenaAlloc* alloc) const override {
Mike Reed176f19c2017-05-24 13:53:36 -040084 return nullptr;
85 }
Mike Reede92aae62018-10-17 10:21:51 -040086#endif
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 Klein4fee3232018-10-18 17:27:16 -040094 // For serialization. This will never be called.
95 Factory getFactory() const override { return nullptr; }
96 const char* getTypeName() const override { return nullptr; }
97
Mike Reed176f19c2017-05-24 13:53:36 -040098 Matrix43 fM43;
99 const bool fIsOpaque;
Mike Reed787a16d2017-05-15 09:29:18 -0400100
Florin Malita4aed1382017-05-25 10:38:07 -0400101 typedef SkShaderBase INHERITED;
Mike Reed787a16d2017-05-15 09:29:18 -0400102};
103
Mike Reedefb25fd2018-02-05 12:43:18 -0500104static bool SK_WARN_UNUSED_RESULT
Brian Osmand25b7c12018-09-21 16:01:59 -0400105update_tricolor_matrix(const SkMatrix& ctmInv, const SkPoint pts[], const SkPMColor4f colors[],
Mike Reedefb25fd2018-02-05 12:43:18 -0500106 int index0, int index1, int index2, Matrix43* result) {
Mike Reed02640952017-05-19 15:32:13 -0400107 SkMatrix m, im;
108 m.reset();
109 m.set(0, pts[index1].fX - pts[index0].fX);
110 m.set(1, pts[index2].fX - pts[index0].fX);
111 m.set(2, pts[index0].fX);
112 m.set(3, pts[index1].fY - pts[index0].fY);
113 m.set(4, pts[index2].fY - pts[index0].fY);
114 m.set(5, pts[index0].fY);
115 if (!m.invert(&im)) {
116 return false;
117 }
118
119 SkMatrix dstToUnit;
120 dstToUnit.setConcat(im, ctmInv);
121
Brian Osman81cbd032018-09-21 11:09:15 -0400122 Sk4f c0 = Sk4f::Load(colors[index0].vec()),
123 c1 = Sk4f::Load(colors[index1].vec()),
124 c2 = Sk4f::Load(colors[index2].vec());
Mike Reed02640952017-05-19 15:32:13 -0400125
126 Matrix43 colorm;
127 (c1 - c0).store(&colorm.fMat[0]);
128 (c2 - c0).store(&colorm.fMat[4]);
129 c0.store(&colorm.fMat[8]);
130 result->setConcat(colorm, dstToUnit);
131 return true;
132}
133
Mike Reed176f19c2017-05-24 13:53:36 -0400134// Convert the SkColors into float colors. The conversion depends on some conditions:
135// - If the pixmap has a dst colorspace, we have to be "color-correct".
136// Do we map into dst-colorspace before or after we interpolate?
137// - We have to decide when to apply per-color alpha (before or after we interpolate)
138//
139// For now, we will take a simple approach, but recognize this is just a start:
140// - convert colors into dst colorspace before interpolation (matches gradients)
141// - apply per-color alpha before interpolation (matches old version of vertices)
142//
Brian Osmand25b7c12018-09-21 16:01:59 -0400143static SkPMColor4f* convert_colors(const SkColor src[], int count, SkColorSpace* deviceCS,
Brian Osman5ea41fc2018-09-26 18:49:27 +0000144 SkArenaAlloc* alloc) {
Brian Osmand25b7c12018-09-21 16:01:59 -0400145 SkPMColor4f* dst = alloc->makeArray<SkPMColor4f>(count);
Brian Osman5ea41fc2018-09-26 18:49:27 +0000146 SkImageInfo srcInfo = SkImageInfo::Make(count, 1, kBGRA_8888_SkColorType,
147 kUnpremul_SkAlphaType, SkColorSpace::MakeSRGB());
148 SkImageInfo dstInfo = SkImageInfo::Make(count, 1, kRGBA_F32_SkColorType,
149 kPremul_SkAlphaType, sk_ref_sp(deviceCS));
150 SkConvertPixels(dstInfo, dst, 0, srcInfo, src, 0);
Mike Reed02640952017-05-19 15:32:13 -0400151 return dst;
152}
153
154static bool compute_is_opaque(const SkColor colors[], int count) {
155 uint32_t c = ~0;
156 for (int i = 0; i < count; ++i) {
157 c &= colors[i];
158 }
159 return SkColorGetA(c) == 0xFF;
160}
161
Ruiqi Maof5101492018-06-29 14:32:21 -0400162void SkDraw::drawVertices(SkVertices::VertexMode vmode, int vertexCount,
Mike Reed787a16d2017-05-15 09:29:18 -0400163 const SkPoint vertices[], const SkPoint textures[],
Ruiqi Maof5101492018-06-29 14:32:21 -0400164 const SkColor colors[], const SkVertices::BoneIndices boneIndices[],
165 const SkVertices::BoneWeights boneWeights[], SkBlendMode bmode,
Mike Reed787a16d2017-05-15 09:29:18 -0400166 const uint16_t indices[], int indexCount,
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400167 const SkPaint& paint, const SkVertices::Bone bones[],
168 int boneCount) const {
Ruiqi Maof5101492018-06-29 14:32:21 -0400169 SkASSERT(0 == vertexCount || vertices);
Mike Reed787a16d2017-05-15 09:29:18 -0400170
171 // abort early if there is nothing to draw
Ruiqi Maof5101492018-06-29 14:32:21 -0400172 if (vertexCount < 3 || (indices && indexCount < 3) || fRC->isEmpty()) {
Mike Reed787a16d2017-05-15 09:29:18 -0400173 return;
174 }
Mike Reed02640952017-05-19 15:32:13 -0400175 SkMatrix ctmInv;
176 if (!fMatrix->invert(&ctmInv)) {
177 return;
178 }
Mike Reed787a16d2017-05-15 09:29:18 -0400179
Mike Reed176f19c2017-05-24 13:53:36 -0400180 // make textures and shader mutually consistent
181 SkShader* shader = paint.getShader();
182 if (!(shader && textures)) {
183 shader = nullptr;
184 textures = nullptr;
185 }
186
Mike Reed0271c5b2017-06-02 14:52:22 -0400187 // We can simplify things for certain blendmodes. This is for speed, and SkComposeShader
188 // itself insists we don't pass kSrc or kDst to it.
189 //
190 if (colors && textures) {
191 switch (bmode) {
192 case SkBlendMode::kSrc:
193 colors = nullptr;
194 break;
195 case SkBlendMode::kDst:
196 textures = nullptr;
197 break;
198 default: break;
199 }
200 }
201
202 // we don't use the shader if there are no textures
203 if (!textures) {
204 shader = nullptr;
205 }
206
Ruiqi Maof5101492018-06-29 14:32:21 -0400207 constexpr size_t kDefVertexCount = 16;
Ruiqi Maof5101492018-06-29 14:32:21 -0400208 constexpr size_t kOuterSize = sizeof(SkTriColorShader) +
Mike Reed176f19c2017-05-24 13:53:36 -0400209 sizeof(SkComposeShader) +
Brian Osman81cbd032018-09-21 11:09:15 -0400210 (2 * sizeof(SkPoint) + sizeof(SkColor4f)) * kDefVertexCount;
Ruiqi Maof5101492018-06-29 14:32:21 -0400211 SkSTArenaAlloc<kOuterSize> outerAlloc;
Mike Reed176f19c2017-05-24 13:53:36 -0400212
Ruiqi Maof5101492018-06-29 14:32:21 -0400213 // deform vertices using the skeleton if it is passed in
214 if (bones && boneCount) {
215 // allocate space for the deformed vertices
216 SkPoint* deformed = outerAlloc.makeArray<SkPoint>(vertexCount);
217
Ruiqi Maof5101492018-06-29 14:32:21 -0400218 // deform the vertices
219 if (boneIndices && boneWeights) {
220 for (int i = 0; i < vertexCount; i ++) {
221 const SkVertices::BoneIndices& indices = boneIndices[i];
222 const SkVertices::BoneWeights& weights = boneWeights[i];
223
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400224 // apply the world transform
225 SkPoint worldPoint = bones[0].mapPoint(vertices[i]);
226
Ruiqi Maof5101492018-06-29 14:32:21 -0400227 // apply bone deformations
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400228 deformed[i] = SkPoint::Make(0.0f, 0.0f);
Ruiqi Maof5101492018-06-29 14:32:21 -0400229 for (uint32_t j = 0; j < 4; j ++) {
230 // get the attachment data
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400231 uint32_t index = indices[j];
232 float weight = weights[j];
Ruiqi Maof5101492018-06-29 14:32:21 -0400233
234 // skip the bone if there is no weight
235 if (weight == 0.0f) {
236 continue;
237 }
238 SkASSERT(index != 0);
239
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400240 // deformed += M * v * w
241 deformed[i] += bones[index].mapPoint(worldPoint) * weight;
Ruiqi Maof5101492018-06-29 14:32:21 -0400242 }
Ruiqi Maof5101492018-06-29 14:32:21 -0400243 }
244 } else {
245 // no bones, so only apply world transform
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400246 SkMatrix worldTransform = SkMatrix::I();
247 worldTransform.setAffine(bones[0].values);
Ruiqi Maof5101492018-06-29 14:32:21 -0400248 worldTransform.mapPoints(deformed, vertices, vertexCount);
249 }
250
251 // change the vertices to point to deformed
252 vertices = deformed;
253 }
254
255 SkPoint* devVerts = outerAlloc.makeArray<SkPoint>(vertexCount);
256 fMatrix->mapPoints(devVerts, vertices, vertexCount);
Mike Reed787a16d2017-05-15 09:29:18 -0400257
Mike Reed81afc042018-05-02 13:15:06 -0400258 {
259 SkRect bounds;
260 // this also sets bounds to empty if we see a non-finite value
Ruiqi Maof5101492018-06-29 14:32:21 -0400261 bounds.set(devVerts, vertexCount);
Mike Reed81afc042018-05-02 13:15:06 -0400262 if (bounds.isEmpty()) {
263 return;
264 }
265 }
266
Ruiqi Maof5101492018-06-29 14:32:21 -0400267 VertState state(vertexCount, indices, indexCount);
Mike Reed787a16d2017-05-15 09:29:18 -0400268 VertState::Proc vertProc = state.chooseProc(vmode);
269
Mike Reed176f19c2017-05-24 13:53:36 -0400270 if (colors || textures) {
Brian Osmand25b7c12018-09-21 16:01:59 -0400271 SkPMColor4f* dstColors = nullptr;
Mike Reed176f19c2017-05-24 13:53:36 -0400272 Matrix43* matrix43 = nullptr;
273
274 if (colors) {
Ruiqi Maof5101492018-06-29 14:32:21 -0400275 dstColors = convert_colors(colors, vertexCount, fDst.colorSpace(), &outerAlloc);
Mike Reed176f19c2017-05-24 13:53:36 -0400276
277 SkTriColorShader* triShader = outerAlloc.make<SkTriColorShader>(
Ruiqi Maof5101492018-06-29 14:32:21 -0400278 compute_is_opaque(colors,
279 vertexCount));
Mike Reed176f19c2017-05-24 13:53:36 -0400280 matrix43 = triShader->getMatrix43();
281 if (shader) {
282 shader = outerAlloc.make<SkComposeShader>(sk_ref_sp(triShader), sk_ref_sp(shader),
Mike Reed01b2b832017-06-09 10:51:52 -0400283 bmode, 1);
Mike Reed176f19c2017-05-24 13:53:36 -0400284 } else {
285 shader = triShader;
286 }
287 }
288
289 SkPaint p(paint);
290 p.setShader(sk_ref_sp(shader));
Mike Reed787a16d2017-05-15 09:29:18 -0400291
Mike Reede28bbcf2017-05-26 09:52:05 -0400292 if (!textures) { // only tricolor shader
293 SkASSERT(matrix43);
294 auto blitter = SkCreateRasterPipelineBlitter(fDst, p, *fMatrix, &outerAlloc);
295 while (vertProc(&state)) {
296 if (!update_tricolor_matrix(ctmInv, vertices, dstColors,
297 state.f0, state.f1, state.f2,
298 matrix43)) {
299 continue;
300 }
Mike Reed787a16d2017-05-15 09:29:18 -0400301
Mike Reede28bbcf2017-05-26 09:52:05 -0400302 SkPoint tmp[] = {
303 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
304 };
305 SkScan::FillTriangle(tmp, *fRC, blitter);
Mike Reed787a16d2017-05-15 09:29:18 -0400306 }
Mike Reede28bbcf2017-05-26 09:52:05 -0400307 } else {
308 while (vertProc(&state)) {
309 SkSTArenaAlloc<2048> innerAlloc;
Mike Reed176f19c2017-05-24 13:53:36 -0400310
Mike Reede28bbcf2017-05-26 09:52:05 -0400311 const SkMatrix* ctm = fMatrix;
312 SkMatrix tmpCtm;
313 if (textures) {
314 SkMatrix localM;
Mike Reedefb25fd2018-02-05 12:43:18 -0500315 if (!texture_to_matrix(state, vertices, textures, &localM)) {
316 continue;
317 }
Mike Reede28bbcf2017-05-26 09:52:05 -0400318 tmpCtm = SkMatrix::Concat(*fMatrix, localM);
319 ctm = &tmpCtm;
320 }
321
322 if (matrix43 && !update_tricolor_matrix(ctmInv, vertices, dstColors,
323 state.f0, state.f1, state.f2,
324 matrix43)) {
325 continue;
326 }
327
328 SkPoint tmp[] = {
329 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
330 };
331 auto blitter = SkCreateRasterPipelineBlitter(fDst, p, *ctm, &innerAlloc);
332 SkScan::FillTriangle(tmp, *fRC, blitter);
Mike Reed787a16d2017-05-15 09:29:18 -0400333 }
Mike Reed787a16d2017-05-15 09:29:18 -0400334 }
335 } else {
336 // no colors[] and no texture, stroke hairlines with paint's color.
Mike Reed176f19c2017-05-24 13:53:36 -0400337 SkPaint p;
338 p.setStyle(SkPaint::kStroke_Style);
Mike Reed910ca0f2018-04-25 13:04:05 -0400339 SkAutoBlitterChoose blitter(*this, nullptr, p);
Mike Reed176f19c2017-05-24 13:53:36 -0400340 // Abort early if we failed to create a shader context.
341 if (blitter->isNullBlitter()) {
342 return;
343 }
Mike Reed787a16d2017-05-15 09:29:18 -0400344 SkScan::HairRCProc hairProc = ChooseHairProc(paint.isAntiAlias());
345 const SkRasterClip& clip = *fRC;
346 while (vertProc(&state)) {
347 SkPoint array[] = {
348 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2], devVerts[state.f0]
349 };
350 hairProc(array, 4, clip, blitter.get());
351 }
352 }
353}