• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 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 #ifndef Sk4fGradientBase_DEFINED
9 #define Sk4fGradientBase_DEFINED
10 
11 #include "Sk4fGradientPriv.h"
12 #include "SkColor.h"
13 #include "SkGradientShaderPriv.h"
14 #include "SkMatrix.h"
15 #include "SkNx.h"
16 #include "SkPM4f.h"
17 #include "SkShaderBase.h"
18 #include "SkTArray.h"
19 
20 struct Sk4fGradientInterval {
21     Sk4fGradientInterval(const Sk4f& c0, SkScalar t0,
22                          const Sk4f& c1, SkScalar t1);
23 
containsSk4fGradientInterval24     bool contains(SkScalar t) const {
25         // True if t is in [p0,p1].  Note: this helper assumes a
26         // natural/increasing interval - so it's not usable in Sk4fLinearGradient.
27         SkASSERT(fT0 < fT1);
28         return t >= fT0 && t <= fT1;
29     }
30 
31     // Color bias and color gradient, such that for a t in this interval
32     //
33     //   C = fCb + t * fCg;
34     SkPM4f   fCb, fCg;
35     SkScalar fT0, fT1;
36 };
37 
38 class Sk4fGradientIntervalBuffer {
39 public:
40     void init(const SkGradientShaderBase&, SkColorSpace* dstCS, SkShader::TileMode tileMode,
41               bool premulColors, SkScalar alpha, bool reverse);
42 
43     const Sk4fGradientInterval* find(SkScalar t) const;
44     const Sk4fGradientInterval* findNext(SkScalar t, const Sk4fGradientInterval* prev,
45                                          bool increasing) const;
46 
47     using BufferType = SkSTArray<8, Sk4fGradientInterval, true>;
48 
49     const BufferType* operator->() const { return &fIntervals; }
50 
51 private:
52     BufferType fIntervals;
53 };
54 
55 class SkGradientShaderBase::
56 GradientShaderBase4fContext : public Context {
57 public:
58     GradientShaderBase4fContext(const SkGradientShaderBase&,
59                                 const ContextRec&);
60 
getFlags()61     uint32_t getFlags() const override { return fFlags; }
62 
63     void shadeSpan(int x, int y, SkPMColor dst[], int count) final;
64 
65     bool isValid() const;
66 
67 protected:
68     Sk4fGradientIntervalBuffer fIntervals;
69     SkMatrix                   fDstToPos;
70     SkMatrix::MapXYProc        fDstToPosProc;
71     uint8_t                    fFlags;
72     bool                       fColorsArePremul;
73 
74 private:
75     using INHERITED = Context;
76 
77     void addMirrorIntervals(const SkGradientShaderBase&,
78                             const Sk4f& componentScale, bool reverse);
79 };
80 
81 #endif // Sk4fGradientBase_DEFINED
82