blob: fcdcd9e8c771e12d21ecea13e5eaefb857a93dbe [file] [log] [blame]
fmalitabc590c02016-02-22 09:12:33 -08001/*
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
fmalitaa928b282016-03-18 10:28:23 -070011#include "Sk4fGradientPriv.h"
fmalitabc590c02016-02-22 09:12:33 -080012#include "SkColor.h"
13#include "SkGradientShaderPriv.h"
14#include "SkMatrix.h"
15#include "SkNx.h"
16#include "SkPM4f.h"
17#include "SkShader.h"
18#include "SkTArray.h"
19
Florin Malitada4545b2017-03-23 17:04:54 -040020struct Sk4fGradientInterval {
Florin Malitacf20f782017-04-07 14:56:14 -040021 Sk4fGradientInterval(const Sk4f& c0, SkScalar t0,
22 const Sk4f& c1, SkScalar t1);
Florin Malitada4545b2017-03-23 17:04:54 -040023
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.
Florin Malitacf20f782017-04-07 14:56:14 -040027 SkASSERT(fT0 < fT1);
28 return t >= fT0 && t <= fT1;
Florin Malitada4545b2017-03-23 17:04:54 -040029 }
30
Florin Malitacf20f782017-04-07 14:56:14 -040031 // 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;
Florin Malitada4545b2017-03-23 17:04:54 -040036 bool fZeroRamp;
37};
38
39class Sk4fGradientIntervalBuffer {
40public:
41 void init(const SkColor colors[], const SkScalar pos[], int count,
42 SkShader::TileMode tileMode, bool premulColors, SkScalar alpha, bool reverse);
43
44 const Sk4fGradientInterval* find(SkScalar t) const;
45 const Sk4fGradientInterval* findNext(SkScalar t, const Sk4fGradientInterval* prev,
46 bool increasing) const;
47
48 using BufferType = SkSTArray<8, Sk4fGradientInterval, true>;
49
50 const BufferType* operator->() const { return &fIntervals; }
51
52private:
53 BufferType fIntervals;
54};
55
fmalitabc590c02016-02-22 09:12:33 -080056class SkGradientShaderBase::
57GradientShaderBase4fContext : public SkShader::Context {
58public:
59 GradientShaderBase4fContext(const SkGradientShaderBase&,
60 const ContextRec&);
61
62 uint32_t getFlags() const override { return fFlags; }
63
fmalita7e6fcf82016-03-10 11:18:43 -080064 void shadeSpan(int x, int y, SkPMColor dst[], int count) override;
65 void shadeSpan4f(int x, int y, SkPM4f dst[], int count) override;
66
fmalita088e21b2016-10-05 09:28:42 -070067 bool isValid() const;
68
fmalitabc590c02016-02-22 09:12:33 -080069protected:
fmalita7e6fcf82016-03-10 11:18:43 -080070 virtual void mapTs(int x, int y, SkScalar ts[], int count) const = 0;
71
Florin Malitada4545b2017-03-23 17:04:54 -040072 Sk4fGradientIntervalBuffer fIntervals;
73 SkMatrix fDstToPos;
74 SkMatrix::MapXYProc fDstToPosProc;
75 uint8_t fDstToPosClass;
76 uint8_t fFlags;
77 bool fDither;
78 bool fColorsArePremul;
fmalitabc590c02016-02-22 09:12:33 -080079
80private:
81 using INHERITED = SkShader::Context;
fmalita7e6fcf82016-03-10 11:18:43 -080082
83 void addMirrorIntervals(const SkGradientShaderBase&,
84 const Sk4f& componentScale, bool reverse);
85
fmalita3a2e45a2016-10-14 08:18:24 -070086 template<DstType, ApplyPremul, SkShader::TileMode tileMode>
fmalita7e6fcf82016-03-10 11:18:43 -080087 class TSampler;
88
fmalitadc6c9bf2016-03-21 13:16:51 -070089 template <DstType dstType, ApplyPremul premul>
90 void shadePremulSpan(int x, int y, typename DstTraits<dstType, premul>::Type[],
91 int count) const;
fmalita7e6fcf82016-03-10 11:18:43 -080092
fmalitadc6c9bf2016-03-21 13:16:51 -070093 template <DstType dstType, ApplyPremul premul, SkShader::TileMode tileMode>
94 void shadeSpanInternal(int x, int y, typename DstTraits<dstType, premul>::Type[],
95 int count) const;
fmalitabc590c02016-02-22 09:12:33 -080096};
97
98#endif // Sk4fGradientBase_DEFINED