blob: 442ba089d9260e8024471601bbdfea6ef6b62584 [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"
16#include "SkShader.h"
17#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
72class SkTriColorShader : public SkShader {
73public:
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 }
89 bool onAppendStages(SkRasterPipeline* pipeine, SkColorSpace* dstCS, SkArenaAlloc* alloc,
90 const SkMatrix&, const SkPaint&, const SkMatrix*) const override {
91 pipeine->append(SkRasterPipeline::matrix_4x3, &fM43);
92 // In theory we should never need to clamp. However, either due to imprecision in our
93 // matrix43, or the scan converter passing us pixel centers that in fact are not within
94 // the triangle, we do see occasional (slightly) out-of-range values, so we add these
95 // clamp stages. It would be nice to find a way to detect when these are not needed.
96 pipeine->append(SkRasterPipeline::clamp_0);
97 pipeine->append(SkRasterPipeline::clamp_a);
98 return true;
Mike Reed787a16d2017-05-15 09:29:18 -040099 }
100
101private:
Mike Reed176f19c2017-05-24 13:53:36 -0400102 Matrix43 fM43;
103 const bool fIsOpaque;
Mike Reed787a16d2017-05-15 09:29:18 -0400104
105 typedef SkShader INHERITED;
106};
107
Mike Reed787a16d2017-05-15 09:29:18 -0400108#ifndef SK_IGNORE_TO_STRING
109void SkTriColorShader::toString(SkString* str) const {
110 str->append("SkTriColorShader: (");
Mike Kleinb35cb312017-05-19 12:01:01 -0400111
Mike Reed787a16d2017-05-15 09:29:18 -0400112 this->INHERITED::toString(str);
Mike Kleinb35cb312017-05-19 12:01:01 -0400113
Mike Reed787a16d2017-05-15 09:29:18 -0400114 str->append(")");
115}
116#endif
117
Mike Reed02640952017-05-19 15:32:13 -0400118static bool update_tricolor_matrix(const SkMatrix& ctmInv,
119 const SkPoint pts[], const SkPM4f colors[],
120 int index0, int index1, int index2, Matrix43* result) {
121 SkMatrix m, im;
122 m.reset();
123 m.set(0, pts[index1].fX - pts[index0].fX);
124 m.set(1, pts[index2].fX - pts[index0].fX);
125 m.set(2, pts[index0].fX);
126 m.set(3, pts[index1].fY - pts[index0].fY);
127 m.set(4, pts[index2].fY - pts[index0].fY);
128 m.set(5, pts[index0].fY);
129 if (!m.invert(&im)) {
130 return false;
131 }
132
133 SkMatrix dstToUnit;
134 dstToUnit.setConcat(im, ctmInv);
135
136 Sk4f c0 = colors[index0].to4f(),
137 c1 = colors[index1].to4f(),
138 c2 = colors[index2].to4f();
139
140 Matrix43 colorm;
141 (c1 - c0).store(&colorm.fMat[0]);
142 (c2 - c0).store(&colorm.fMat[4]);
143 c0.store(&colorm.fMat[8]);
144 result->setConcat(colorm, dstToUnit);
145 return true;
146}
147
Mike Reed176f19c2017-05-24 13:53:36 -0400148// Convert the SkColors into float colors. The conversion depends on some conditions:
149// - If the pixmap has a dst colorspace, we have to be "color-correct".
150// Do we map into dst-colorspace before or after we interpolate?
151// - We have to decide when to apply per-color alpha (before or after we interpolate)
152//
153// For now, we will take a simple approach, but recognize this is just a start:
154// - convert colors into dst colorspace before interpolation (matches gradients)
155// - apply per-color alpha before interpolation (matches old version of vertices)
156//
Mike Reed02640952017-05-19 15:32:13 -0400157static SkPM4f* convert_colors(const SkColor src[], int count, SkColorSpace* deviceCS,
158 SkArenaAlloc* alloc) {
159 SkPM4f* dst = alloc->makeArray<SkPM4f>(count);
160 if (!deviceCS) {
161 for (int i = 0; i < count; ++i) {
162 dst[i] = SkPM4f_from_SkColor(src[i], nullptr);
163 }
164 } else {
Mike Reed02640952017-05-19 15:32:13 -0400165 auto srcCS = SkColorSpace::MakeSRGB();
166 auto dstCS = as_CSB(deviceCS)->makeLinearGamma();
Mike Reededf8a762017-05-22 13:41:36 -0400167 SkColorSpaceXform::Apply(dstCS.get(), SkColorSpaceXform::kRGBA_F32_ColorFormat, dst,
168 srcCS.get(), SkColorSpaceXform::kBGRA_8888_ColorFormat, src,
169 count, SkColorSpaceXform::kPremul_AlphaOp);
Mike Reed02640952017-05-19 15:32:13 -0400170 }
171 return dst;
172}
173
174static bool compute_is_opaque(const SkColor colors[], int count) {
175 uint32_t c = ~0;
176 for (int i = 0; i < count; ++i) {
177 c &= colors[i];
178 }
179 return SkColorGetA(c) == 0xFF;
180}
181
Mike Reed787a16d2017-05-15 09:29:18 -0400182void SkDraw::drawVertices(SkVertices::VertexMode vmode, int count,
183 const SkPoint vertices[], const SkPoint textures[],
184 const SkColor colors[], SkBlendMode bmode,
185 const uint16_t indices[], int indexCount,
186 const SkPaint& paint) const {
187 SkASSERT(0 == count || vertices);
188
189 // abort early if there is nothing to draw
190 if (count < 3 || (indices && indexCount < 3) || fRC->isEmpty()) {
191 return;
192 }
Mike Reed02640952017-05-19 15:32:13 -0400193 SkMatrix ctmInv;
194 if (!fMatrix->invert(&ctmInv)) {
195 return;
196 }
Mike Reed787a16d2017-05-15 09:29:18 -0400197
Mike Reed176f19c2017-05-24 13:53:36 -0400198 // make textures and shader mutually consistent
199 SkShader* shader = paint.getShader();
200 if (!(shader && textures)) {
201 shader = nullptr;
202 textures = nullptr;
203 }
204
205 constexpr size_t defCount = 16;
206 constexpr size_t outerSize = sizeof(SkTriColorShader) +
207 sizeof(SkComposeShader) +
208 (sizeof(SkPoint) + sizeof(SkPM4f)) * defCount;
209 char outerStorage[outerSize];
210 SkArenaAlloc outerAlloc(outerStorage, sizeof(outerStorage));
211
212 SkPoint* devVerts = outerAlloc.makeArray<SkPoint>(count);
Mike Reed787a16d2017-05-15 09:29:18 -0400213 fMatrix->mapPoints(devVerts, vertices, count);
214
Mike Reed787a16d2017-05-15 09:29:18 -0400215 VertState state(count, indices, indexCount);
216 VertState::Proc vertProc = state.chooseProc(vmode);
217
Mike Reed176f19c2017-05-24 13:53:36 -0400218 if (colors || textures) {
219 SkPM4f* dstColors = nullptr;
220 Matrix43* matrix43 = nullptr;
221
222 if (colors) {
223 dstColors = convert_colors(colors, count, fDst.colorSpace(), &outerAlloc);
224
225 SkTriColorShader* triShader = outerAlloc.make<SkTriColorShader>(
226 compute_is_opaque(colors, count));
227 matrix43 = triShader->getMatrix43();
228 if (shader) {
229 shader = outerAlloc.make<SkComposeShader>(sk_ref_sp(triShader), sk_ref_sp(shader),
230 bmode);
231 } else {
232 shader = triShader;
233 }
234 }
235
236 SkPaint p(paint);
237 p.setShader(sk_ref_sp(shader));
Mike Reed787a16d2017-05-15 09:29:18 -0400238
239 while (vertProc(&state)) {
Mike Reed176f19c2017-05-24 13:53:36 -0400240 char innerStorage[2048];
241 SkArenaAlloc innerAlloc(innerStorage, sizeof(innerStorage));
Mike Reed787a16d2017-05-15 09:29:18 -0400242
Mike Reed176f19c2017-05-24 13:53:36 -0400243 const SkMatrix* ctm = fMatrix;
244 SkMatrix tmpCtm;
Mike Reed787a16d2017-05-15 09:29:18 -0400245 if (textures) {
Mike Reed176f19c2017-05-24 13:53:36 -0400246 SkMatrix localM;
247 texture_to_matrix(state, vertices, textures, &localM);
248 tmpCtm = SkMatrix::Concat(*fMatrix, localM);
249 ctm = &tmpCtm;
Mike Reed787a16d2017-05-15 09:29:18 -0400250 }
Mike Reed176f19c2017-05-24 13:53:36 -0400251
252 if (matrix43 && !update_tricolor_matrix(ctmInv, vertices, dstColors,
253 state.f0, state.f1, state.f2,
254 matrix43)) {
255 continue;
Mike Reed787a16d2017-05-15 09:29:18 -0400256 }
257
258 SkPoint tmp[] = {
259 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
260 };
Mike Reed176f19c2017-05-24 13:53:36 -0400261 auto blitter = SkCreateRasterPipelineBlitter(fDst, p, *ctm, &innerAlloc);
262 SkScan::FillTriangle(tmp, *fRC, blitter);
Mike Reed787a16d2017-05-15 09:29:18 -0400263 }
264 } else {
265 // no colors[] and no texture, stroke hairlines with paint's color.
Mike Reed176f19c2017-05-24 13:53:36 -0400266 SkPaint p;
267 p.setStyle(SkPaint::kStroke_Style);
268 SkAutoBlitterChoose blitter(fDst, *fMatrix, p);
269 // Abort early if we failed to create a shader context.
270 if (blitter->isNullBlitter()) {
271 return;
272 }
Mike Reed787a16d2017-05-15 09:29:18 -0400273 SkScan::HairRCProc hairProc = ChooseHairProc(paint.isAntiAlias());
274 const SkRasterClip& clip = *fRC;
275 while (vertProc(&state)) {
276 SkPoint array[] = {
277 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2], devVerts[state.f0]
278 };
279 hairProc(array, 4, clip, blitter.get());
280 }
281 }
282}