blob: 9209a906374613b61a685e777a07fb2d231e1618 [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"
Florin Malitad93e11c2017-05-24 21:15:46 +000017#include "SkShader.h"
fmalitabc590c02016-02-22 09:12:33 -080018#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};
37
38class Sk4fGradientIntervalBuffer {
39public:
40 void init(const SkColor colors[], const SkScalar pos[], int count,
41 SkShader::TileMode tileMode, 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
51private:
52 BufferType fIntervals;
53};
54
fmalitabc590c02016-02-22 09:12:33 -080055class SkGradientShaderBase::
Florin Malitad93e11c2017-05-24 21:15:46 +000056GradientShaderBase4fContext : public SkShader::Context {
fmalitabc590c02016-02-22 09:12:33 -080057public:
58 GradientShaderBase4fContext(const SkGradientShaderBase&,
59 const ContextRec&);
60
61 uint32_t getFlags() const override { return fFlags; }
62
fmalita7e6fcf82016-03-10 11:18:43 -080063 void shadeSpan(int x, int y, SkPMColor dst[], int count) override;
64 void shadeSpan4f(int x, int y, SkPM4f dst[], int count) override;
65
fmalita088e21b2016-10-05 09:28:42 -070066 bool isValid() const;
67
fmalitabc590c02016-02-22 09:12:33 -080068protected:
fmalita7e6fcf82016-03-10 11:18:43 -080069 virtual void mapTs(int x, int y, SkScalar ts[], int count) const = 0;
70
Florin Malitada4545b2017-03-23 17:04:54 -040071 Sk4fGradientIntervalBuffer fIntervals;
72 SkMatrix fDstToPos;
73 SkMatrix::MapXYProc fDstToPosProc;
74 uint8_t fDstToPosClass;
75 uint8_t fFlags;
76 bool fDither;
77 bool fColorsArePremul;
fmalitabc590c02016-02-22 09:12:33 -080078
79private:
Florin Malitad93e11c2017-05-24 21:15:46 +000080 using INHERITED = SkShader::Context;
fmalita7e6fcf82016-03-10 11:18:43 -080081
82 void addMirrorIntervals(const SkGradientShaderBase&,
83 const Sk4f& componentScale, bool reverse);
84
fmalita3a2e45a2016-10-14 08:18:24 -070085 template<DstType, ApplyPremul, SkShader::TileMode tileMode>
fmalita7e6fcf82016-03-10 11:18:43 -080086 class TSampler;
87
fmalitadc6c9bf2016-03-21 13:16:51 -070088 template <DstType dstType, ApplyPremul premul>
89 void shadePremulSpan(int x, int y, typename DstTraits<dstType, premul>::Type[],
90 int count) const;
fmalita7e6fcf82016-03-10 11:18:43 -080091
fmalitadc6c9bf2016-03-21 13:16:51 -070092 template <DstType dstType, ApplyPremul premul, SkShader::TileMode tileMode>
93 void shadeSpanInternal(int x, int y, typename DstTraits<dstType, premul>::Type[],
94 int count) const;
fmalitabc590c02016-02-22 09:12:33 -080095};
96
97#endif // Sk4fGradientBase_DEFINED