| 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 |
|
| 11 |
#include "include/core/SkBitmap.h" |
| 12 |
#include "include/core/SkColor.h" |
| 13 |
#include "include/core/SkColorSpace.h" |
| 14 |
#include "include/core/SkMatrix.h" |
| 15 |
#include "include/core/SkRefCnt.h" |
| 16 |
#include "include/core/SkScalar.h" |
| 17 |
#include "include/effects/SkGradient.h" |
| 18 |
#include "include/private/base/SkAssert.h" |
| 19 |
#include "include/private/base/SkTArray.h" |
| 20 |
#include "include/private/base/SkTemplates.h" |
| 21 |
#include "src/core/SkColorData.h" |
| 22 |
#include "src/shaders/SkShaderBase.h" |
| 23 |
|
| 24 |
#include <cstddef> |
| 25 |
#include <cstdint> |
| 26 |
|
| 27 |
class SkArenaAlloc; |
| 28 |
class SkRasterPipeline; |
| 29 |
class SkReadBuffer; |
| 30 |
class SkShader; |
| 31 |
class SkWriteBuffer; |
| 32 |
enum class SkTileMode; |
| 33 |
struct SkStageRec; |
| 34 |
|
| 35 |
class SkGradientScope { |
| 36 |
public: |
| 37 |
std::optional<SkGradient> unflatten(SkReadBuffer&, SkMatrix* legacyLocalMatrix); |
| 38 |
private: |
| 39 |
skia_private::STArray<16, SkColor4f> fColorStorage; |
| 40 |
skia_private::STArray<16, SkScalar> fPositionStorage; |
| 41 |
}; |
| 42 |
|
| 43 |
class SkGradientBaseShader : public SkShaderBase { |
| 44 |
public: |
| 45 |
using Interpolation = SkGradient::Interpolation; |
| 46 |
|
| 47 |
SkGradientBaseShader(const SkGradient&, const SkMatrix& ptsToUnit); |
| 48 |
~SkGradientBaseShader() override; |
| 49 |
|
| 50 |
ShaderType type() const final { return ShaderType::kGradientBase; } |
| 51 |
|
| 52 |
bool isOpaque() const override; |
| 53 |
|
| 54 |
bool interpolateInPremul() const { |
| 55 |
return fInterpolation.fInPremul == Interpolation::InPremul::kYes; |
| 56 |
} |
| 57 |
|
| 58 |
const SkMatrix& getGradientMatrix() const { return fPtsToUnit; } |
| 59 |
size_t getColorCount() const { return fColorCount; } |
| 60 |
const float* getPositions() const { return fPositions; } |
| 61 |
|
| 62 |
const Interpolation& getInterpolation() const { return fInterpolation; } |
| 63 |
|
| 64 |
static bool ValidGradient(SkSpan<const SkColor4f>, |
| 65 |
SkTileMode tileMode, |
| 66 |
const Interpolation& interpolation); |
| 67 |
|
| 68 |
static sk_sp<SkShader> MakeDegenerateGradient(const SkGradient::Colors&); |
| 69 |
|
| 70 |
// The default SkScalarNearlyZero threshold of .0024 is too big and causes regressions for svg |
| 71 |
// gradients defined in the wild. |
| 72 |
static constexpr SkScalar kDegenerateThreshold = SK_Scalar1 / (1 << 15); |
| 73 |
|
| 74 |
protected: |
| 75 |
void flatten(SkWriteBuffer&) const override; |
| 76 |
|
| 77 |
void commonAsAGradient(GradientInfo*) const; |
| 78 |
|
| 79 |
bool onAsLuminanceColor(SkColor4f*) const override; |
| 80 |
|
| 81 |
bool appendStages(const SkStageRec&, const SkShaders::MatrixRec&) const override; |
| 82 |
|
| 83 |
virtual void appendGradientStages(SkArenaAlloc* alloc, |
| 84 |
SkRasterPipeline* tPipeline, |
| 85 |
SkRasterPipeline* postPipeline) const = 0; |
| 86 |
|
| 87 |
const SkMatrix fPtsToUnit; |
| 88 |
SkTileMode fTileMode; |
| 89 |
|
| 90 |
public: |
| 91 |
static void AppendGradientFillStages(SkRasterPipeline* p, |
| 92 |
SkArenaAlloc* alloc, |
| 93 |
const SkPMColor4f* colors, |
| 94 |
const SkScalar* positions, |
| 95 |
int count); |
| 96 |
|
| 97 |
static void AppendInterpolatedToDstStages(SkRasterPipeline* p, |
| 98 |
SkArenaAlloc* alloc, |
| 99 |
bool colorsAreOpaque, |
| 100 |
const Interpolation& interpolation, |
| 101 |
const SkColorSpace* intermediateColorSpace, |
| 102 |
const SkColorSpace* dstColorSpace); |
| 103 |
|
| 104 |
float getPos(size_t i) const { |
| 105 |
SkASSERT(i < fColorCount); |
| 106 |
return fPositions ? fPositions[i] : SkIntToScalar(i) / (fColorCount - 1); |
| 107 |
} |
| 108 |
|
| 109 |
SkColor getLegacyColor(size_t i) const { |
| 110 |
SkASSERT(i < fColorCount); |
| 111 |
return fColors[i].toSkColor(); |
| 112 |
} |
| 113 |
|
| 114 |
SkSpan<const SkColor4f> colors() const { return {fColors, fColorCount}; } |
| 115 |
SkSpan<const float> positions() const { |
| 116 |
return {fPositions, fPositions ? fColorCount : 0}; |
| 117 |
} |
| 118 |
|
| 119 |
bool firstStopIsImplicit() const { return fFirstStopIsImplicit; } |
| 120 |
bool lastStopIsImplicit() const { return fLastStopIsImplicit; } |
| 121 |
|
| 122 |
const sk_sp<SkColorSpace>& colorSpace() const { return fColorSpace; } |
| 123 |
const Interpolation& interpolation() const { return fInterpolation; } |
| 124 |
|
| 125 |
bool colorsAreOpaque() const { return fColorsAreOpaque; } |
| 126 |
|
| 127 |
SkTileMode getTileMode() const { return fTileMode; } |
| 128 |
|
| 129 |
const SkBitmap& cachedBitmap() const { return fColorsAndOffsetsBitmap; } |
| 130 |
void setCachedBitmap(SkBitmap b) const { fColorsAndOffsetsBitmap = b; } |
| 131 |
|
| 132 |
private: |
| 133 |
SkColor4f* fColors; // points into fStorage |
| 134 |
SkScalar* fPositions; // points into fStorage, or nullptr |
| 135 |
sk_sp<SkColorSpace> fColorSpace; // color space of gradient stops |
| 136 |
Interpolation fInterpolation; |
| 137 |
size_t fColorCount; // length of fColors (and fPositions, if not nullptr) |
| 138 |
bool fFirstStopIsImplicit; |
| 139 |
bool fLastStopIsImplicit; |
| 140 |
|
| 141 |
// When the number of stops exceeds Graphite's uniform-based limit the colors and offsets |
| 142 |
// are stored in this bitmap. It is stored in the shader so it can be cached with a stable |
| 143 |
// id and easily regenerated if purged. |
| 144 |
// TODO(b/293160919) remove this field when we can store bitmaps in the cache by id. |
| 145 |
mutable SkBitmap fColorsAndOffsetsBitmap; |
| 146 |
|
| 147 |
// Reserve inline space for up to 4 stops. |
| 148 |
inline static constexpr size_t kInlineStopCount = 4; |
| 149 |
inline static constexpr size_t kInlineStorageSize = |
| 150 |
(sizeof(SkColor4f) + sizeof(SkScalar)) * kInlineStopCount; |
| 151 |
skia_private::AutoSTMalloc<kInlineStorageSize, uint8_t> fStorage; |
| 152 |
|
| 153 |
bool fColorsAreOpaque; |
| 154 |
}; |
| 155 |
|
| 156 |
/////////////////////////////////////////////////////////////////////////////// |
| 157 |
|
| 158 |
struct SkColor4fXformer { |
| 159 |
SkColor4fXformer(const SkGradientBaseShader* shader, |
| 160 |
SkColorSpace* dst, |
| 161 |
bool forceExplicitPositions = false); |
| 162 |
|
| 163 |
using ColorStorage = skia_private::STArray<4, SkPMColor4f>; |
| 164 |
using PositionStorage = skia_private::STArray<4, float>; |
| 165 |
|
| 166 |
ColorStorage fColors; |
| 167 |
PositionStorage fPositionStorage; |
| 168 |
const float* fPositions; |
| 169 |
sk_sp<SkColorSpace> fIntermediateColorSpace; |
| 170 |
}; |
| 171 |
|
| 172 |
void SkRegisterConicalGradientShaderFlattenable(); |
| 173 |
void SkRegisterLinearGradientShaderFlattenable(); |
| 174 |
void SkRegisterRadialGradientShaderFlattenable(); |
| 175 |
void SkRegisterSweepGradientShaderFlattenable(); |
| 176 |
|
| 177 |
#define MAKE_COLORS_POS_SPANS(colorPtr, posPtr, count) \ |
| 178 |
SkSpan<const SkColor4f> colors{colorPtr, (size_t)count};\ |
| 179 |
SkSpan<const float> pos{}; \ |
| 180 |
do { \ |
| 181 |
if (posPtr) { \ |
| 182 |
pos = {posPtr, (size_t)count}; \ |
| 183 |
} \ |
| 184 |
} while (0) |
| 185 |
|
| 186 |
|
| 187 |
#define GRADIENT_FACTORY_EARLY_EXIT(grad, lm) \ |
| 188 |
do { \ |
| 189 |
const auto& colors = grad.colors(); \ |
| 190 |
const auto& interp = grad.interpolation(); \ |
| 191 |
if (!SkGradientBaseShader::ValidGradient(colors.colors(), colors.tileMode(), interp)) { \ |
| 192 |
return nullptr; \ |
| 193 |
} \ |
| 194 |
if (colors.colors().size() == 1) { \ |
| 195 |
return SkShaders::Color(colors.colors().front(), colors.colorSpace()); \ |
| 196 |
} \ |
| 197 |
if (lm && !lm->invert(nullptr)) { \ |
| 198 |
return nullptr; \ |
| 199 |
} \ |
| 200 |
} while (0) |
| 201 |
|
| 202 |
#endif |