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