Mike Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 1 | /* |
| 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 Reed | 176f19c | 2017-05-24 13:53:36 -0400 | [diff] [blame] | 10 | #include "SkComposeShader.h" |
Mike Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 11 | #include "SkDraw.h" |
| 12 | #include "SkNx.h" |
Mike Reed | 0264095 | 2017-05-19 15:32:13 -0400 | [diff] [blame] | 13 | #include "SkPM4fPriv.h" |
Mike Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 14 | #include "SkRasterClip.h" |
| 15 | #include "SkScan.h" |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 16 | #include "SkShaderBase.h" |
Mike Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 17 | #include "SkString.h" |
| 18 | #include "SkVertState.h" |
| 19 | |
Mike Reed | 0264095 | 2017-05-19 15:32:13 -0400 | [diff] [blame] | 20 | #include "SkArenaAlloc.h" |
| 21 | #include "SkCoreBlitters.h" |
| 22 | #include "SkColorSpaceXform.h" |
| 23 | #include "SkColorSpace_Base.h" |
| 24 | |
Mike Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 25 | struct 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 | |
| 49 | private: |
| 50 | float dot(int index, float x, float y) const { |
| 51 | return fMat[index + 0] * x + fMat[index + 4] * y; |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | static SkScan::HairRCProc ChooseHairProc(bool doAntiAlias) { |
| 56 | return doAntiAlias ? SkScan::AntiHairLine : SkScan::HairLine; |
| 57 | } |
| 58 | |
| 59 | static 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 Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 72 | class SkTriColorShader : public SkShaderBase { |
Mike Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 73 | public: |
Mike Reed | 176f19c | 2017-05-24 13:53:36 -0400 | [diff] [blame] | 74 | SkTriColorShader(bool isOpaque) : fIsOpaque(isOpaque) {} |
Mike Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 75 | |
Mike Reed | 176f19c | 2017-05-24 13:53:36 -0400 | [diff] [blame] | 76 | Matrix43* getMatrix43() { return &fM43; } |
Mike Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 77 | |
Mike Reed | 176f19c | 2017-05-24 13:53:36 -0400 | [diff] [blame] | 78 | bool isOpaque() const override { return fIsOpaque; } |
Mike Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 79 | |
| 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 Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 85 | protected: |
| 86 | Context* onMakeContext(const ContextRec& rec, SkArenaAlloc* alloc) const override { |
Mike Reed | 176f19c | 2017-05-24 13:53:36 -0400 | [diff] [blame] | 87 | return nullptr; |
| 88 | } |
Mike Klein | 5df94d5 | 2017-06-01 14:43:51 -0400 | [diff] [blame] | 89 | bool onAppendStages(SkRasterPipeline* pipeline, SkColorSpace* dstCS, SkArenaAlloc* alloc, |
Mike Reed | 176f19c | 2017-05-24 13:53:36 -0400 | [diff] [blame] | 90 | const SkMatrix&, const SkPaint&, const SkMatrix*) const override { |
Mike Klein | 5df94d5 | 2017-06-01 14:43:51 -0400 | [diff] [blame] | 91 | pipeline->append(SkRasterPipeline::seed_shader); |
| 92 | pipeline->append(SkRasterPipeline::matrix_4x3, &fM43); |
Mike Reed | 176f19c | 2017-05-24 13:53:36 -0400 | [diff] [blame] | 93 | // 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 Klein | 5df94d5 | 2017-06-01 14:43:51 -0400 | [diff] [blame] | 97 | pipeline->append(SkRasterPipeline::clamp_0); |
| 98 | pipeline->append(SkRasterPipeline::clamp_a); |
Mike Reed | 176f19c | 2017-05-24 13:53:36 -0400 | [diff] [blame] | 99 | return true; |
Mike Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | private: |
Mike Reed | 176f19c | 2017-05-24 13:53:36 -0400 | [diff] [blame] | 103 | Matrix43 fM43; |
| 104 | const bool fIsOpaque; |
Mike Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 105 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 106 | typedef SkShaderBase INHERITED; |
Mike Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 107 | }; |
| 108 | |
Mike Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 109 | #ifndef SK_IGNORE_TO_STRING |
| 110 | void SkTriColorShader::toString(SkString* str) const { |
| 111 | str->append("SkTriColorShader: ("); |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 112 | |
Mike Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 113 | this->INHERITED::toString(str); |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 114 | |
Mike Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 115 | str->append(")"); |
| 116 | } |
| 117 | #endif |
| 118 | |
Mike Reed | 0264095 | 2017-05-19 15:32:13 -0400 | [diff] [blame] | 119 | static 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 Reed | 176f19c | 2017-05-24 13:53:36 -0400 | [diff] [blame] | 149 | // 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 Reed | 0264095 | 2017-05-19 15:32:13 -0400 | [diff] [blame] | 158 | static 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 Reed | 0264095 | 2017-05-19 15:32:13 -0400 | [diff] [blame] | 166 | auto srcCS = SkColorSpace::MakeSRGB(); |
| 167 | auto dstCS = as_CSB(deviceCS)->makeLinearGamma(); |
Mike Reed | edf8a76 | 2017-05-22 13:41:36 -0400 | [diff] [blame] | 168 | SkColorSpaceXform::Apply(dstCS.get(), SkColorSpaceXform::kRGBA_F32_ColorFormat, dst, |
| 169 | srcCS.get(), SkColorSpaceXform::kBGRA_8888_ColorFormat, src, |
| 170 | count, SkColorSpaceXform::kPremul_AlphaOp); |
Mike Reed | 0264095 | 2017-05-19 15:32:13 -0400 | [diff] [blame] | 171 | } |
| 172 | return dst; |
| 173 | } |
| 174 | |
| 175 | static 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 Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 183 | void 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 Reed | 0264095 | 2017-05-19 15:32:13 -0400 | [diff] [blame] | 194 | SkMatrix ctmInv; |
| 195 | if (!fMatrix->invert(&ctmInv)) { |
| 196 | return; |
| 197 | } |
Mike Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 198 | |
Mike Reed | 176f19c | 2017-05-24 13:53:36 -0400 | [diff] [blame] | 199 | // make textures and shader mutually consistent |
| 200 | SkShader* shader = paint.getShader(); |
| 201 | if (!(shader && textures)) { |
| 202 | shader = nullptr; |
| 203 | textures = nullptr; |
| 204 | } |
| 205 | |
Mike Reed | 0271c5b | 2017-06-02 14:52:22 -0400 | [diff] [blame^] | 206 | // 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 Reed | 176f19c | 2017-05-24 13:53:36 -0400 | [diff] [blame] | 226 | constexpr size_t defCount = 16; |
| 227 | constexpr size_t outerSize = sizeof(SkTriColorShader) + |
| 228 | sizeof(SkComposeShader) + |
| 229 | (sizeof(SkPoint) + sizeof(SkPM4f)) * defCount; |
Florin Malita | 14a6430 | 2017-05-24 14:53:44 -0400 | [diff] [blame] | 230 | SkSTArenaAlloc<outerSize> outerAlloc; |
Mike Reed | 176f19c | 2017-05-24 13:53:36 -0400 | [diff] [blame] | 231 | |
| 232 | SkPoint* devVerts = outerAlloc.makeArray<SkPoint>(count); |
Mike Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 233 | fMatrix->mapPoints(devVerts, vertices, count); |
| 234 | |
Mike Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 235 | VertState state(count, indices, indexCount); |
| 236 | VertState::Proc vertProc = state.chooseProc(vmode); |
| 237 | |
Mike Reed | 176f19c | 2017-05-24 13:53:36 -0400 | [diff] [blame] | 238 | 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), |
| 250 | bmode); |
| 251 | } else { |
| 252 | shader = triShader; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | SkPaint p(paint); |
| 257 | p.setShader(sk_ref_sp(shader)); |
Mike Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 258 | |
Mike Reed | e28bbcf | 2017-05-26 09:52:05 -0400 | [diff] [blame] | 259 | 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 Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 268 | |
Mike Reed | e28bbcf | 2017-05-26 09:52:05 -0400 | [diff] [blame] | 269 | SkPoint tmp[] = { |
| 270 | devVerts[state.f0], devVerts[state.f1], devVerts[state.f2] |
| 271 | }; |
| 272 | SkScan::FillTriangle(tmp, *fRC, blitter); |
Mike Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 273 | } |
Mike Reed | e28bbcf | 2017-05-26 09:52:05 -0400 | [diff] [blame] | 274 | } else { |
| 275 | while (vertProc(&state)) { |
| 276 | SkSTArenaAlloc<2048> innerAlloc; |
Mike Reed | 176f19c | 2017-05-24 13:53:36 -0400 | [diff] [blame] | 277 | |
Mike Reed | e28bbcf | 2017-05-26 09:52:05 -0400 | [diff] [blame] | 278 | 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 Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 298 | } |
Mike Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 299 | } |
| 300 | } else { |
| 301 | // no colors[] and no texture, stroke hairlines with paint's color. |
Mike Reed | 176f19c | 2017-05-24 13:53:36 -0400 | [diff] [blame] | 302 | 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 Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 309 | 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 | } |