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 "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 
     24     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 
     61     uint32_t getFlags() const override { return fFlags; }
     62 
     63     bool isValid() const;
     64 
     65 protected:
     66     Sk4fGradientIntervalBuffer fIntervals;
     67     SkMatrix                   fDstToPos;
     68     SkMatrixPriv::MapXYProc    fDstToPosProc;
     69     uint8_t                    fFlags;
     70     bool                       fColorsArePremul;
     71     bool                       fDither;
     72 
     73 private:
     74     using INHERITED = Context;
     75 
     76     void addMirrorIntervals(const SkGradientShaderBase&,
     77                             const Sk4f& componentScale, bool reverse);
     78 };
     79 
     80 #endif // Sk4fGradientBase_DEFINED
     81