blob: 000be07cea20fdf3c9f168a8904c345cf86fef16 [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
Cary Clark99885412018-04-05 13:09:58 -040080 void toString(SkString* str) const override;
Mike Reed787a16d2017-05-15 09:29:18 -040081
82 // For serialization. This will never be called.
Ben Wagner7ca9a742017-08-17 14:05:04 -040083 Factory getFactory() const override { SK_ABORT("not reached"); return nullptr; }
Mike Reed787a16d2017-05-15 09:29:18 -040084
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 Reed1d8c42e2017-08-29 14:58:19 -040089 bool onAppendStages(const StageRec& rec) const override {
Mike Kleine8de0242018-03-10 12:37:11 -050090 rec.fPipeline->append(SkRasterPipeline::seed_shader);
Mike Reed1d8c42e2017-08-29 14:58:19 -040091 rec.fPipeline->append(SkRasterPipeline::matrix_4x3, &fM43);
Mike Reed176f19c2017-05-24 13:53:36 -040092 return true;
Mike Reed787a16d2017-05-15 09:29:18 -040093 }
94
95private:
Mike Reed176f19c2017-05-24 13:53:36 -040096 Matrix43 fM43;
97 const bool fIsOpaque;
Mike Reed787a16d2017-05-15 09:29:18 -040098
Florin Malita4aed1382017-05-25 10:38:07 -040099 typedef SkShaderBase INHERITED;
Mike Reed787a16d2017-05-15 09:29:18 -0400100};
101
Mike Reed787a16d2017-05-15 09:29:18 -0400102void SkTriColorShader::toString(SkString* str) const {
103 str->append("SkTriColorShader: (");
Mike Kleinb35cb312017-05-19 12:01:01 -0400104
Mike Reed787a16d2017-05-15 09:29:18 -0400105 this->INHERITED::toString(str);
Mike Kleinb35cb312017-05-19 12:01:01 -0400106
Mike Reed787a16d2017-05-15 09:29:18 -0400107 str->append(")");
108}
Mike Reed787a16d2017-05-15 09:29:18 -0400109
Mike Reedefb25fd2018-02-05 12:43:18 -0500110static bool SK_WARN_UNUSED_RESULT
111update_tricolor_matrix(const SkMatrix& ctmInv, const SkPoint pts[], const SkPM4f colors[],
112 int index0, int index1, int index2, Matrix43* result) {
Mike Reed02640952017-05-19 15:32:13 -0400113 SkMatrix m, im;
114 m.reset();
115 m.set(0, pts[index1].fX - pts[index0].fX);
116 m.set(1, pts[index2].fX - pts[index0].fX);
117 m.set(2, pts[index0].fX);
118 m.set(3, pts[index1].fY - pts[index0].fY);
119 m.set(4, pts[index2].fY - pts[index0].fY);
120 m.set(5, pts[index0].fY);
121 if (!m.invert(&im)) {
122 return false;
123 }
124
125 SkMatrix dstToUnit;
126 dstToUnit.setConcat(im, ctmInv);
127
128 Sk4f c0 = colors[index0].to4f(),
129 c1 = colors[index1].to4f(),
130 c2 = colors[index2].to4f();
131
132 Matrix43 colorm;
133 (c1 - c0).store(&colorm.fMat[0]);
134 (c2 - c0).store(&colorm.fMat[4]);
135 c0.store(&colorm.fMat[8]);
136 result->setConcat(colorm, dstToUnit);
137 return true;
138}
139
Mike Reed176f19c2017-05-24 13:53:36 -0400140// Convert the SkColors into float colors. The conversion depends on some conditions:
141// - If the pixmap has a dst colorspace, we have to be "color-correct".
142// Do we map into dst-colorspace before or after we interpolate?
143// - We have to decide when to apply per-color alpha (before or after we interpolate)
144//
145// For now, we will take a simple approach, but recognize this is just a start:
146// - convert colors into dst colorspace before interpolation (matches gradients)
147// - apply per-color alpha before interpolation (matches old version of vertices)
148//
Mike Reed02640952017-05-19 15:32:13 -0400149static SkPM4f* convert_colors(const SkColor src[], int count, SkColorSpace* deviceCS,
150 SkArenaAlloc* alloc) {
151 SkPM4f* dst = alloc->makeArray<SkPM4f>(count);
152 if (!deviceCS) {
153 for (int i = 0; i < count; ++i) {
154 dst[i] = SkPM4f_from_SkColor(src[i], nullptr);
155 }
156 } else {
Mike Reed02640952017-05-19 15:32:13 -0400157 auto srcCS = SkColorSpace::MakeSRGB();
Brian Osman36703d92017-12-12 14:09:31 -0500158 auto dstCS = deviceCS->makeLinearGamma();
Mike Reededf8a762017-05-22 13:41:36 -0400159 SkColorSpaceXform::Apply(dstCS.get(), SkColorSpaceXform::kRGBA_F32_ColorFormat, dst,
160 srcCS.get(), SkColorSpaceXform::kBGRA_8888_ColorFormat, src,
161 count, SkColorSpaceXform::kPremul_AlphaOp);
Mike Reed02640952017-05-19 15:32:13 -0400162 }
163 return dst;
164}
165
166static bool compute_is_opaque(const SkColor colors[], int count) {
167 uint32_t c = ~0;
168 for (int i = 0; i < count; ++i) {
169 c &= colors[i];
170 }
171 return SkColorGetA(c) == 0xFF;
172}
173
Mike Reed787a16d2017-05-15 09:29:18 -0400174void SkDraw::drawVertices(SkVertices::VertexMode vmode, int count,
175 const SkPoint vertices[], const SkPoint textures[],
176 const SkColor colors[], SkBlendMode bmode,
177 const uint16_t indices[], int indexCount,
178 const SkPaint& paint) const {
179 SkASSERT(0 == count || vertices);
180
181 // abort early if there is nothing to draw
182 if (count < 3 || (indices && indexCount < 3) || fRC->isEmpty()) {
183 return;
184 }
Mike Reed02640952017-05-19 15:32:13 -0400185 SkMatrix ctmInv;
186 if (!fMatrix->invert(&ctmInv)) {
187 return;
188 }
Mike Reed787a16d2017-05-15 09:29:18 -0400189
Mike Reed176f19c2017-05-24 13:53:36 -0400190 // make textures and shader mutually consistent
191 SkShader* shader = paint.getShader();
192 if (!(shader && textures)) {
193 shader = nullptr;
194 textures = nullptr;
195 }
196
Mike Reed0271c5b2017-06-02 14:52:22 -0400197 // We can simplify things for certain blendmodes. This is for speed, and SkComposeShader
198 // itself insists we don't pass kSrc or kDst to it.
199 //
200 if (colors && textures) {
201 switch (bmode) {
202 case SkBlendMode::kSrc:
203 colors = nullptr;
204 break;
205 case SkBlendMode::kDst:
206 textures = nullptr;
207 break;
208 default: break;
209 }
210 }
211
212 // we don't use the shader if there are no textures
213 if (!textures) {
214 shader = nullptr;
215 }
216
Mike Reed176f19c2017-05-24 13:53:36 -0400217 constexpr size_t defCount = 16;
218 constexpr size_t outerSize = sizeof(SkTriColorShader) +
219 sizeof(SkComposeShader) +
220 (sizeof(SkPoint) + sizeof(SkPM4f)) * defCount;
Florin Malita14a64302017-05-24 14:53:44 -0400221 SkSTArenaAlloc<outerSize> outerAlloc;
Mike Reed176f19c2017-05-24 13:53:36 -0400222
223 SkPoint* devVerts = outerAlloc.makeArray<SkPoint>(count);
Mike Reed787a16d2017-05-15 09:29:18 -0400224 fMatrix->mapPoints(devVerts, vertices, count);
225
Mike Reed81afc042018-05-02 13:15:06 -0400226 {
227 SkRect bounds;
228 // this also sets bounds to empty if we see a non-finite value
229 bounds.set(devVerts, count);
230 if (bounds.isEmpty()) {
231 return;
232 }
233 }
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;
Mike Reedefb25fd2018-02-05 12:43:18 -0500282 if (!texture_to_matrix(state, vertices, textures, &localM)) {
283 continue;
284 }
Mike Reede28bbcf2017-05-26 09:52:05 -0400285 tmpCtm = SkMatrix::Concat(*fMatrix, localM);
286 ctm = &tmpCtm;
287 }
288
289 if (matrix43 && !update_tricolor_matrix(ctmInv, vertices, dstColors,
290 state.f0, state.f1, state.f2,
291 matrix43)) {
292 continue;
293 }
294
295 SkPoint tmp[] = {
296 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
297 };
298 auto blitter = SkCreateRasterPipelineBlitter(fDst, p, *ctm, &innerAlloc);
299 SkScan::FillTriangle(tmp, *fRC, blitter);
Mike Reed787a16d2017-05-15 09:29:18 -0400300 }
Mike Reed787a16d2017-05-15 09:29:18 -0400301 }
302 } else {
303 // no colors[] and no texture, stroke hairlines with paint's color.
Mike Reed176f19c2017-05-24 13:53:36 -0400304 SkPaint p;
305 p.setStyle(SkPaint::kStroke_Style);
Mike Reed910ca0f2018-04-25 13:04:05 -0400306 SkAutoBlitterChoose blitter(*this, nullptr, p);
Mike Reed176f19c2017-05-24 13:53:36 -0400307 // Abort early if we failed to create a shader context.
308 if (blitter->isNullBlitter()) {
309 return;
310 }
Mike Reed787a16d2017-05-15 09:29:18 -0400311 SkScan::HairRCProc hairProc = ChooseHairProc(paint.isAntiAlias());
312 const SkRasterClip& clip = *fRC;
313 while (vertProc(&state)) {
314 SkPoint array[] = {
315 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2], devVerts[state.f0]
316 };
317 hairProc(array, 4, clip, blitter.get());
318 }
319 }
320}