xref: /aosp_15_r20/external/skia/src/shaders/SkTriColorShader.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2023 Google LLC
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 "src/shaders/SkTriColorShader.h"
9 
10 #include "include/core/SkMatrix.h"
11 #include "include/core/SkPoint.h"
12 #include "include/core/SkTypes.h"
13 #include "include/private/SkColorData.h"
14 #include "src/base/SkVx.h"
15 #include "src/core/SkEffectPriv.h"
16 #include "src/core/SkRasterPipeline.h"
17 #include "src/core/SkRasterPipelineOpList.h"
18 
appendStages(const SkStageRec & rec,const SkShaders::MatrixRec &) const19 bool SkTriColorShader::appendStages(const SkStageRec& rec, const SkShaders::MatrixRec&) const {
20     rec.fPipeline->append(SkRasterPipelineOp::seed_shader);
21     if (fUsePersp) {
22         rec.fPipeline->append(SkRasterPipelineOp::matrix_perspective, &fM33);
23     }
24     rec.fPipeline->append(SkRasterPipelineOp::matrix_4x3, &fM43);
25     return true;
26 }
27 
update(const SkMatrix & ctmInv,const SkPoint pts[],const SkPMColor4f colors[],int index0,int index1,int index2)28 bool SkTriColorShader::update(const SkMatrix& ctmInv,
29                               const SkPoint pts[],
30                               const SkPMColor4f colors[],
31                               int index0,
32                               int index1,
33                               int index2) {
34     SkMatrix m, im;
35     m.reset();
36     m.set(0, pts[index1].fX - pts[index0].fX);
37     m.set(1, pts[index2].fX - pts[index0].fX);
38     m.set(2, pts[index0].fX);
39     m.set(3, pts[index1].fY - pts[index0].fY);
40     m.set(4, pts[index2].fY - pts[index0].fY);
41     m.set(5, pts[index0].fY);
42     if (!m.invert(&im)) {
43         return false;
44     }
45 
46     fM33.setConcat(im, ctmInv);
47 
48     auto c0 = skvx::float4::Load(colors[index0].vec()),
49          c1 = skvx::float4::Load(colors[index1].vec()),
50          c2 = skvx::float4::Load(colors[index2].vec());
51 
52     (c1 - c0).store(&fM43.fMat[0]);
53     (c2 - c0).store(&fM43.fMat[4]);
54     c0.store(&fM43.fMat[8]);
55 
56     if (!fUsePersp) {
57         fM43.setConcat(fM43, fM33);
58     }
59     return true;
60 }
61