Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkGradientBaseShader.h
Go to the documentation of this file.
1/*
2 * Copyright 2012 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 SkGradientShaderPriv_DEFINED
9#define SkGradientShaderPriv_DEFINED
10
23
24#include <cstddef>
25#include <cstdint>
26
27class SkArenaAlloc;
29class SkReadBuffer;
30class SkShader;
31class SkWriteBuffer;
32enum class SkTileMode;
33struct SkStageRec;
34
36public:
38
39 struct Descriptor {
40 Descriptor();
42
43 Descriptor(const SkColor4f colors[],
44 sk_sp<SkColorSpace> colorSpace,
45 const SkScalar positions[],
46 int colorCount,
47 SkTileMode mode,
48 const Interpolation& interpolation);
49
53 int fColorCount; // length of fColors (and fPositions, if not nullptr)
56 };
57
58 class DescriptorScope : public Descriptor {
59 public:
61
62 bool unflatten(SkReadBuffer&, SkMatrix* legacyLocalMatrix);
63
64 private:
67 };
68
69 SkGradientBaseShader(const Descriptor& desc, const SkMatrix& ptsToUnit);
70 ~SkGradientBaseShader() override;
71
72 ShaderType type() const final { return ShaderType::kGradientBase; }
73
74 bool isOpaque() const override;
75
79
80 const SkMatrix& getGradientMatrix() const { return fPtsToUnit; }
81 int getColorCount() const { return fColorCount; }
82 const float* getPositions() const { return fPositions; }
84
85 static bool ValidGradient(const SkColor4f colors[],
86 int count,
87 SkTileMode tileMode,
88 const Interpolation& interpolation);
89
91 const SkScalar pos[],
92 int colorCount,
93 sk_sp<SkColorSpace> colorSpace,
94 SkTileMode mode);
95
96 // The default SkScalarNearlyZero threshold of .0024 is too big and causes regressions for svg
97 // gradients defined in the wild.
98 static constexpr SkScalar kDegenerateThreshold = SK_Scalar1 / (1 << 15);
99
100protected:
101 void flatten(SkWriteBuffer&) const override;
102
103 void commonAsAGradient(GradientInfo*) const;
104
105 bool onAsLuminanceColor(SkColor4f*) const override;
106
107 bool appendStages(const SkStageRec&, const SkShaders::MatrixRec&) const override;
108
110 SkRasterPipeline* tPipeline,
111 SkRasterPipeline* postPipeline) const = 0;
112
115
116public:
118 SkArenaAlloc* alloc,
119 const SkPMColor4f* colors,
120 const SkScalar* positions,
121 int count);
122
124 SkArenaAlloc* alloc,
125 bool colorsAreOpaque,
126 const Interpolation& interpolation,
127 const SkColorSpace* intermediateColorSpace,
128 const SkColorSpace* dstColorSpace);
129
130 SkScalar getPos(int i) const {
132 return fPositions ? fPositions[i] : SkIntToScalar(i) / (fColorCount - 1);
133 }
134
135 SkColor getLegacyColor(int i) const {
137 return fColors[i].toSkColor();
138 }
139
140 SkColor4f* fColors; // points into fStorage
141 SkScalar* fPositions; // points into fStorage, or nullptr
142 int fColorCount; // length of fColors (and fPositions, if not nullptr)
143 sk_sp<SkColorSpace> fColorSpace; // color space of gradient stops
147
148 bool colorsAreOpaque() const { return fColorsAreOpaque; }
149
150 SkTileMode getTileMode() const { return fTileMode; }
151
152 const SkBitmap& cachedBitmap() const { return fColorsAndOffsetsBitmap; }
153 void setCachedBitmap(SkBitmap b) const { fColorsAndOffsetsBitmap = b; }
154
155private:
156 // When the number of stops exceeds Graphite's uniform-based limit the colors and offsets
157 // are stored in this bitmap. It is stored in the shader so it can be cached with a stable
158 // id and easily regenerated if purged.
159 // TODO(b/293160919) remove this field when we can store bitmaps in the cache by id.
160 mutable SkBitmap fColorsAndOffsetsBitmap;
161
162 // Reserve inline space for up to 4 stops.
163 inline static constexpr size_t kInlineStopCount = 4;
164 inline static constexpr size_t kInlineStorageSize =
165 (sizeof(SkColor4f) + sizeof(SkScalar)) * kInlineStopCount;
167
168 bool fColorsAreOpaque;
169};
170
171///////////////////////////////////////////////////////////////////////////////
172
186
192
197
198#endif
int count
SkPoint pos
#define SkASSERT(cond)
Definition SkAssert.h:116
uint32_t SkColor
Definition SkColor.h:37
void SkRegisterLinearGradientShaderFlattenable()
void SkRegisterRadialGradientShaderFlattenable()
void SkRegisterConicalGradientShaderFlattenable()
void SkRegisterSweepGradientShaderFlattenable()
#define SK_Scalar1
Definition SkScalar.h:18
#define SkIntToScalar(x)
Definition SkScalar.h:57
SkTileMode
Definition SkTileMode.h:13
bool unflatten(SkReadBuffer &, SkMatrix *legacyLocalMatrix)
static constexpr SkScalar kDegenerateThreshold
static bool ValidGradient(const SkColor4f colors[], int count, SkTileMode tileMode, const Interpolation &interpolation)
SkColor getLegacyColor(int i) const
const Interpolation & getInterpolation() const
bool isOpaque() const override
void setCachedBitmap(SkBitmap b) const
virtual void appendGradientStages(SkArenaAlloc *alloc, SkRasterPipeline *tPipeline, SkRasterPipeline *postPipeline) const =0
const float * getPositions() const
bool onAsLuminanceColor(SkColor4f *) const override
SkGradientShader::Interpolation Interpolation
SkScalar getPos(int i) const
static sk_sp< SkShader > MakeDegenerateGradient(const SkColor4f colors[], const SkScalar pos[], int colorCount, sk_sp< SkColorSpace > colorSpace, SkTileMode mode)
void flatten(SkWriteBuffer &) const override
bool appendStages(const SkStageRec &, const SkShaders::MatrixRec &) const override
void commonAsAGradient(GradientInfo *) const
sk_sp< SkColorSpace > fColorSpace
ShaderType type() const final
static void AppendGradientFillStages(SkRasterPipeline *p, SkArenaAlloc *alloc, const SkPMColor4f *colors, const SkScalar *positions, int count)
const SkBitmap & cachedBitmap() const
static void AppendInterpolatedToDstStages(SkRasterPipeline *p, SkArenaAlloc *alloc, bool colorsAreOpaque, const Interpolation &interpolation, const SkColorSpace *intermediateColorSpace, const SkColorSpace *dstColorSpace)
const SkMatrix & getGradientMatrix() const
SkTileMode getTileMode() const
float SkScalar
Definition extension.cpp:12
static bool b
PositionStorage fPositionStorage
sk_sp< SkColorSpace > fIntermediateColorSpace
skia_private::STArray< 2, SkColor4f > fColors4f