blob: 4f7fc13b6331c2e9d7b0e0f68e3c76e8c8444908 [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"
23#include "SkColorSpace_Base.h"
24
Mike Reed787a16d2017-05-15 09:29:18 -040025struct Matrix43 {
26 float fMat[12]; // column major
27
28 Sk4f map(float x, float y) const {
29 return Sk4f::Load(&fMat[0]) * x + Sk4f::Load(&fMat[4]) * y + Sk4f::Load(&fMat[8]);
30 }
31
32 void setConcat(const Matrix43& a, const SkMatrix& b) {
33 fMat[ 0] = a.dot(0, b.getScaleX(), b.getSkewY());
34 fMat[ 1] = a.dot(1, b.getScaleX(), b.getSkewY());
35 fMat[ 2] = a.dot(2, b.getScaleX(), b.getSkewY());
36 fMat[ 3] = a.dot(3, b.getScaleX(), b.getSkewY());
37
38 fMat[ 4] = a.dot(0, b.getSkewX(), b.getScaleY());
39 fMat[ 5] = a.dot(1, b.getSkewX(), b.getScaleY());
40 fMat[ 6] = a.dot(2, b.getSkewX(), b.getScaleY());
41 fMat[ 7] = a.dot(3, b.getSkewX(), b.getScaleY());
42
43 fMat[ 8] = a.dot(0, b.getTranslateX(), b.getTranslateY()) + a.fMat[ 8];
44 fMat[ 9] = a.dot(1, b.getTranslateX(), b.getTranslateY()) + a.fMat[ 9];
45 fMat[10] = a.dot(2, b.getTranslateX(), b.getTranslateY()) + a.fMat[10];
46 fMat[11] = a.dot(3, b.getTranslateX(), b.getTranslateY()) + a.fMat[11];
47 }
48
49private:
50 float dot(int index, float x, float y) const {
51 return fMat[index + 0] * x + fMat[index + 4] * y;
52 }
53};
54
55static SkScan::HairRCProc ChooseHairProc(bool doAntiAlias) {
56 return doAntiAlias ? SkScan::AntiHairLine : SkScan::HairLine;
57}
58
59static bool texture_to_matrix(const VertState& state, const SkPoint verts[],
60 const SkPoint texs[], SkMatrix* matrix) {
61 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
80 SK_TO_STRING_OVERRIDE()
81
82 // For serialization. This will never be called.
83 Factory getFactory() const override { sk_throw(); return nullptr; }
84
Mike Reed787a16d2017-05-15 09:29:18 -040085protected:
86 Context* onMakeContext(const ContextRec& rec, SkArenaAlloc* alloc) const override {
Mike Reed176f19c2017-05-24 13:53:36 -040087 return nullptr;
88 }
Mike Klein5df94d52017-06-01 14:43:51 -040089 bool onAppendStages(SkRasterPipeline* pipeline, SkColorSpace* dstCS, SkArenaAlloc* alloc,
Mike Reed176f19c2017-05-24 13:53:36 -040090 const SkMatrix&, const SkPaint&, const SkMatrix*) const override {
Mike Klein5df94d52017-06-01 14:43:51 -040091 pipeline->append(SkRasterPipeline::seed_shader);
92 pipeline->append(SkRasterPipeline::matrix_4x3, &fM43);
Mike Reed176f19c2017-05-24 13:53:36 -040093 // In theory we should never need to clamp. However, either due to imprecision in our
94 // matrix43, or the scan converter passing us pixel centers that in fact are not within
95 // the triangle, we do see occasional (slightly) out-of-range values, so we add these
96 // clamp stages. It would be nice to find a way to detect when these are not needed.
Mike Klein5df94d52017-06-01 14:43:51 -040097 pipeline->append(SkRasterPipeline::clamp_0);
98 pipeline->append(SkRasterPipeline::clamp_a);
Mike Reed176f19c2017-05-24 13:53:36 -040099 return true;
Mike Reed787a16d2017-05-15 09:29:18 -0400100 }
101
102private:
Mike Reed176f19c2017-05-24 13:53:36 -0400103 Matrix43 fM43;
104 const bool fIsOpaque;
Mike Reed787a16d2017-05-15 09:29:18 -0400105
Florin Malita4aed1382017-05-25 10:38:07 -0400106 typedef SkShaderBase INHERITED;
Mike Reed787a16d2017-05-15 09:29:18 -0400107};
108
Mike Reed787a16d2017-05-15 09:29:18 -0400109#ifndef SK_IGNORE_TO_STRING
110void SkTriColorShader::toString(SkString* str) const {
111 str->append("SkTriColorShader: (");
Mike Kleinb35cb312017-05-19 12:01:01 -0400112
Mike Reed787a16d2017-05-15 09:29:18 -0400113 this->INHERITED::toString(str);
Mike Kleinb35cb312017-05-19 12:01:01 -0400114
Mike Reed787a16d2017-05-15 09:29:18 -0400115 str->append(")");
116}
117#endif
118
Mike Reed02640952017-05-19 15:32:13 -0400119static bool update_tricolor_matrix(const SkMatrix& ctmInv,
120 const SkPoint pts[], const SkPM4f colors[],
121 int index0, int index1, int index2, Matrix43* result) {
122 SkMatrix m, im;
123 m.reset();
124 m.set(0, pts[index1].fX - pts[index0].fX);
125 m.set(1, pts[index2].fX - pts[index0].fX);
126 m.set(2, pts[index0].fX);
127 m.set(3, pts[index1].fY - pts[index0].fY);
128 m.set(4, pts[index2].fY - pts[index0].fY);
129 m.set(5, pts[index0].fY);
130 if (!m.invert(&im)) {
131 return false;
132 }
133
134 SkMatrix dstToUnit;
135 dstToUnit.setConcat(im, ctmInv);
136
137 Sk4f c0 = colors[index0].to4f(),
138 c1 = colors[index1].to4f(),
139 c2 = colors[index2].to4f();
140
141 Matrix43 colorm;
142 (c1 - c0).store(&colorm.fMat[0]);
143 (c2 - c0).store(&colorm.fMat[4]);
144 c0.store(&colorm.fMat[8]);
145 result->setConcat(colorm, dstToUnit);
146 return true;
147}
148
Mike Reed176f19c2017-05-24 13:53:36 -0400149// Convert the SkColors into float colors. The conversion depends on some conditions:
150// - If the pixmap has a dst colorspace, we have to be "color-correct".
151// Do we map into dst-colorspace before or after we interpolate?
152// - We have to decide when to apply per-color alpha (before or after we interpolate)
153//
154// For now, we will take a simple approach, but recognize this is just a start:
155// - convert colors into dst colorspace before interpolation (matches gradients)
156// - apply per-color alpha before interpolation (matches old version of vertices)
157//
Mike Reed02640952017-05-19 15:32:13 -0400158static SkPM4f* convert_colors(const SkColor src[], int count, SkColorSpace* deviceCS,
159 SkArenaAlloc* alloc) {
160 SkPM4f* dst = alloc->makeArray<SkPM4f>(count);
161 if (!deviceCS) {
162 for (int i = 0; i < count; ++i) {
163 dst[i] = SkPM4f_from_SkColor(src[i], nullptr);
164 }
165 } else {
Mike Reed02640952017-05-19 15:32:13 -0400166 auto srcCS = SkColorSpace::MakeSRGB();
167 auto dstCS = as_CSB(deviceCS)->makeLinearGamma();
Mike Reededf8a762017-05-22 13:41:36 -0400168 SkColorSpaceXform::Apply(dstCS.get(), SkColorSpaceXform::kRGBA_F32_ColorFormat, dst,
169 srcCS.get(), SkColorSpaceXform::kBGRA_8888_ColorFormat, src,
170 count, SkColorSpaceXform::kPremul_AlphaOp);
Mike Reed02640952017-05-19 15:32:13 -0400171 }
172 return dst;
173}
174
175static bool compute_is_opaque(const SkColor colors[], int count) {
176 uint32_t c = ~0;
177 for (int i = 0; i < count; ++i) {
178 c &= colors[i];
179 }
180 return SkColorGetA(c) == 0xFF;
181}
182
Mike Reed787a16d2017-05-15 09:29:18 -0400183void SkDraw::drawVertices(SkVertices::VertexMode vmode, int count,
184 const SkPoint vertices[], const SkPoint textures[],
185 const SkColor colors[], SkBlendMode bmode,
186 const uint16_t indices[], int indexCount,
187 const SkPaint& paint) const {
188 SkASSERT(0 == count || vertices);
189
190 // abort early if there is nothing to draw
191 if (count < 3 || (indices && indexCount < 3) || fRC->isEmpty()) {
192 return;
193 }
Mike Reed02640952017-05-19 15:32:13 -0400194 SkMatrix ctmInv;
195 if (!fMatrix->invert(&ctmInv)) {
196 return;
197 }
Mike Reed787a16d2017-05-15 09:29:18 -0400198
Mike Reed176f19c2017-05-24 13:53:36 -0400199 // make textures and shader mutually consistent
200 SkShader* shader = paint.getShader();
201 if (!(shader && textures)) {
202 shader = nullptr;
203 textures = nullptr;
204 }
205
Mike Reed0271c5b2017-06-02 14:52:22 -0400206 // We can simplify things for certain blendmodes. This is for speed, and SkComposeShader
207 // itself insists we don't pass kSrc or kDst to it.
208 //
209 if (colors && textures) {
210 switch (bmode) {
211 case SkBlendMode::kSrc:
212 colors = nullptr;
213 break;
214 case SkBlendMode::kDst:
215 textures = nullptr;
216 break;
217 default: break;
218 }
219 }
220
221 // we don't use the shader if there are no textures
222 if (!textures) {
223 shader = nullptr;
224 }
225
Mike Reed176f19c2017-05-24 13:53:36 -0400226 constexpr size_t defCount = 16;
227 constexpr size_t outerSize = sizeof(SkTriColorShader) +
228 sizeof(SkComposeShader) +
229 (sizeof(SkPoint) + sizeof(SkPM4f)) * defCount;
Florin Malita14a64302017-05-24 14:53:44 -0400230 SkSTArenaAlloc<outerSize> outerAlloc;
Mike Reed176f19c2017-05-24 13:53:36 -0400231
232 SkPoint* devVerts = outerAlloc.makeArray<SkPoint>(count);
Mike Reed787a16d2017-05-15 09:29:18 -0400233 fMatrix->mapPoints(devVerts, vertices, count);
234
Mike Reed787a16d2017-05-15 09:29:18 -0400235 VertState state(count, indices, indexCount);
236 VertState::Proc vertProc = state.chooseProc(vmode);
237
Mike Reed176f19c2017-05-24 13:53:36 -0400238 if (colors || textures) {
239 SkPM4f* dstColors = nullptr;
240 Matrix43* matrix43 = nullptr;
241
242 if (colors) {
243 dstColors = convert_colors(colors, count, fDst.colorSpace(), &outerAlloc);
244
245 SkTriColorShader* triShader = outerAlloc.make<SkTriColorShader>(
246 compute_is_opaque(colors, count));
247 matrix43 = triShader->getMatrix43();
248 if (shader) {
249 shader = outerAlloc.make<SkComposeShader>(sk_ref_sp(triShader), sk_ref_sp(shader),
Mike Reed01b2b832017-06-09 10:51:52 -0400250 bmode, 1);
Mike Reed176f19c2017-05-24 13:53:36 -0400251 } else {
252 shader = triShader;
253 }
254 }
255
256 SkPaint p(paint);
257 p.setShader(sk_ref_sp(shader));
Mike Reed787a16d2017-05-15 09:29:18 -0400258
Mike Reede28bbcf2017-05-26 09:52:05 -0400259 if (!textures) { // only tricolor shader
260 SkASSERT(matrix43);
261 auto blitter = SkCreateRasterPipelineBlitter(fDst, p, *fMatrix, &outerAlloc);
262 while (vertProc(&state)) {
263 if (!update_tricolor_matrix(ctmInv, vertices, dstColors,
264 state.f0, state.f1, state.f2,
265 matrix43)) {
266 continue;
267 }
Mike Reed787a16d2017-05-15 09:29:18 -0400268
Mike Reede28bbcf2017-05-26 09:52:05 -0400269 SkPoint tmp[] = {
270 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
271 };
272 SkScan::FillTriangle(tmp, *fRC, blitter);
Mike Reed787a16d2017-05-15 09:29:18 -0400273 }
Mike Reede28bbcf2017-05-26 09:52:05 -0400274 } else {
275 while (vertProc(&state)) {
276 SkSTArenaAlloc<2048> innerAlloc;
Mike Reed176f19c2017-05-24 13:53:36 -0400277
Mike Reede28bbcf2017-05-26 09:52:05 -0400278 const SkMatrix* ctm = fMatrix;
279 SkMatrix tmpCtm;
280 if (textures) {
281 SkMatrix localM;
282 texture_to_matrix(state, vertices, textures, &localM);
283 tmpCtm = SkMatrix::Concat(*fMatrix, localM);
284 ctm = &tmpCtm;
285 }
286
287 if (matrix43 && !update_tricolor_matrix(ctmInv, vertices, dstColors,
288 state.f0, state.f1, state.f2,
289 matrix43)) {
290 continue;
291 }
292
293 SkPoint tmp[] = {
294 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
295 };
296 auto blitter = SkCreateRasterPipelineBlitter(fDst, p, *ctm, &innerAlloc);
297 SkScan::FillTriangle(tmp, *fRC, blitter);
Mike Reed787a16d2017-05-15 09:29:18 -0400298 }
Mike Reed787a16d2017-05-15 09:29:18 -0400299 }
300 } else {
301 // no colors[] and no texture, stroke hairlines with paint's color.
Mike Reed176f19c2017-05-24 13:53:36 -0400302 SkPaint p;
303 p.setStyle(SkPaint::kStroke_Style);
304 SkAutoBlitterChoose blitter(fDst, *fMatrix, p);
305 // Abort early if we failed to create a shader context.
306 if (blitter->isNullBlitter()) {
307 return;
308 }
Mike Reed787a16d2017-05-15 09:29:18 -0400309 SkScan::HairRCProc hairProc = ChooseHairProc(paint.isAntiAlias());
310 const SkRasterClip& clip = *fRC;
311 while (vertProc(&state)) {
312 SkPoint array[] = {
313 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2], devVerts[state.f0]
314 };
315 hairProc(array, 4, clip, blitter.get());
316 }
317 }
318}